module Glueby

Constants

VERSION

Public Class Methods

configuration() click to toggle source

Returns the global [Configuration](RSpec/Core/Configuration) object.

# File lib/glueby.rb, line 24
def self.configuration
  @configuration ||= Glueby::Configuration.new
end
configure() { |configuration| ... } click to toggle source

Yields the global configuration to a block. @yield [Configuration] global configuration

@example

Glueby.configure do |config|
  config.wallet_adapter = :activerecord
  config.rpc_config = { schema: 'http', host: '127.0.0.1', port: 12381, user: 'user', password: 'pass' }
end
# File lib/glueby.rb, line 36
def self.configure
  yield configuration if block_given?
end
new() click to toggle source
# File lib/glueby/fee_provider.rb, line 29
def initialize
  @wallet = begin
              Internal::Wallet.load(WALLET_ID)
            rescue Internal::Wallet::Errors::WalletNotFound => _
              Internal::Wallet.create(WALLET_ID)
            end

  @fixed_fee = (FeeProvider.config && FeeProvider.config[:fixed_fee]) || DEFAULT_FIXED_FEE
  @utxo_pool_size = (FeeProvider.config && FeeProvider.config[:utxo_pool_size]) || DEFAULT_UTXO_POOL_SIZE
end
table_name_prefix() click to toggle source

Add prefix to activerecord table names

# File lib/glueby.rb, line 19
def self.table_name_prefix
  'glueby_'
end

Public Instance Methods

provide(tx) click to toggle source

Provide an input for fee to the tx. @param [Tapyrus::Tx] tx - The tx that is provided fee as a input. It should be signed with ANYONECANPAY flag. @return [Tapyrus::Tx] @raise [ArgumentError] If the signatures that the tx inputs has don't have ANYONECANPAY flag. @raise [Glueby::FeeProvider::NoUtxosInUtxoPool] If there are no UTXOs for paying fee in FeeProvider's UTXO pool

# File lib/glueby/fee_provider.rb, line 45
def provide(tx)
  tx.inputs.each do |txin|
    sig = get_signature(txin.script_sig)
    unless sig[-1].unpack1('C') & Tapyrus::SIGHASH_TYPE[:anyonecanpay] == Tapyrus::SIGHASH_TYPE[:anyonecanpay]
      raise ArgumentError, 'All the signatures that the tx inputs has should have ANYONECANPAY flag.'
    end
  end

  utxo = utxo_for_fee
  out_point = Tapyrus::OutPoint.new(utxo[:txid].rhex, utxo[:vout])
  tx.inputs << Tapyrus::TxIn.new(out_point: out_point)

  wallet.sign_tx(tx, for_fee_provider_input: true)
end

Private Instance Methods

get_signature(script_sig) click to toggle source

Get Signature from P2PKH or CP2PKH script sig

# File lib/glueby/fee_provider.rb, line 69
def get_signature(script_sig)
  script_sig.chunks.first.pushed_data
end
utxo_for_fee() click to toggle source
# File lib/glueby/fee_provider.rb, line 62
def utxo_for_fee
  utxo = wallet.list_unspent.select { |o| !o[:color_id] && o[:amount] == fixed_fee }.sample
  raise NoUtxosInUtxoPool, 'No UTXOs in Fee Provider UTXO pool. UTXOs should be created with "glueby:fee_provider:manage_utxo_pool" rake task' unless utxo
  utxo
end