Class: OpenAssets::Provider::BitcoinCoreProvider

Inherits:
BlockChainProviderBase show all
Defined in:
lib/openassets/provider/bitcoin_core_provider.rb

Overview

The implementation of BlockChain provider using Bitcoin Core.

Constant Summary collapse

RPC_API =
[
    :addmultisigaddress, :addnode, :backupwallet, :createmultisig, :createrawtransaction, :decoderawtransaction,
    :decodescript, :dumpprivkey, :dumpwallet, :encryptwallet, :estimatefee, :estimatepriority, :generate,
    :getaccountaddress, :getaccount, :getaddednodeinfo, :getaddressesbyaccount, :getbalance, :getbestblockhash,
    :getblock, :getblockchaininfo, :getblockcount, :getblockhash, :getchaintips, :getconnectioncount, :getdifficulty,
    :getgenerate, :gethashespersec, :getinfo, :getmempoolinfo, :getmininginfo, :getnettotals, :getnetworkhashps,
    :getnetworkinfo, :getnewaddress, :getpeerinfo, :getrawchangeaddress, :getrawmempool, :getrawtransaction,
    :getreceivedbyaccount, :getreceivedbyaddress, :gettransaction, :gettxout, :gettxoutproof, :gettxoutsetinfo,
    :getunconfirmedbalance, :getwalletinfo, :getwork, :help, :importaddress, :importprivkey, :importwallet,
    :keypoolrefill, :listaccounts, :listaddressgroupings, :listlockunspent, :listreceivedbyaccount, :listreceivedbyaddress,
    :listsinceblock, :listtransactions, :listunspent, :lockunspent, :move, :ping, :prioritisetransaction, :sendfrom,
    :sendmany, :sendrawtransaction, :sendtoaddress, :setaccount, :setgenerate, :settxfee, :signmessage, :signrawtransaction,
    :stop, :submitblock, :validateaddress, :verifychain, :verifymessage, :verifytxoutproof, :walletlock, :walletpassphrase,
    :walletpassphrasechange
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ BitcoinCoreProvider

Returns a new instance of BitcoinCoreProvider.



27
28
29
# File 'lib/openassets/provider/bitcoin_core_provider.rb', line 27

def initialize(config)
  @config = config
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *params) ⇒ Object



67
68
69
70
# File 'lib/openassets/provider/bitcoin_core_provider.rb', line 67

def method_missing(method, *params)
  super unless RPC_API.include?(method)
  request(method, *params)
end

Instance Attribute Details

#config ⇒ Object (readonly)

Returns the value of attribute config.



25
26
27
# File 'lib/openassets/provider/bitcoin_core_provider.rb', line 25

def config
  @config
end

Instance Method Details

#get_transaction(transaction_hash, verbose = 0) ⇒ String

Get raw transaction.

Parameters:

  • transaction_hash (String) —

    The transaction hash.

  • verbose (String) (defaults to: 0) —

    Whether to get the serialized or decoded transaction. 0: serialized transaction (Default). 1: decode transaction.

Returns:

  • (String) —

    (if verbose=0)—the serialized transaction. (if verbose=1)—the decoded transaction. (if transaction not found)—nil.



43
44
45
46
47
48
49
# File 'lib/openassets/provider/bitcoin_core_provider.rb', line 43

def get_transaction(transaction_hash, verbose = 0)
  begin
    request('getrawtransaction', transaction_hash, verbose)
  rescue OpenAssets::Provider::ApiError => e
    nil
  end
end

#list_unspent(addresses = [], min = 1, max = 9999999) ⇒ Object

Get an array of unspent transaction outputs belonging to this wallet.

Parameters:

  • addresses (Array) (defaults to: []) —

    If present, only outputs which pay an address in this array will be returned.

  • min (Integer) (defaults to: 1) —

    The minimum number of confirmations the transaction containing an output must have in order to be returned. Default is 1.

  • max (Integer) (defaults to: 9999999) —

    The maximum number of confirmations the transaction containing an output may have in order to be returned. Default is 9999999.



35
36
37
# File 'lib/openassets/provider/bitcoin_core_provider.rb', line 35

def list_unspent(addresses = [], min = 1 , max = 9999999)
  request('listunspent', min, max, addresses)
end

#send_transaction(tx) ⇒ String

Validates a transaction and broadcasts it to the peer-to-peer network.

Parameters:

  • tx (String) —

    The serialized format transaction.

Returns:

  • (String) —

    The TXID or error message.



63
64
65
# File 'lib/openassets/provider/bitcoin_core_provider.rb', line 63

def send_transaction(tx)
  request('sendrawtransaction', tx)
end

#sign_transaction(tx) ⇒ Bitcoin::Protocol::Tx

Signs a transaction in the serialized transaction format using private keys.

Parameters:

  • tx (String) —

    The serialized format transaction.

Returns:

  • (Bitcoin::Protocol::Tx) —

    The signed transaction.

Raises:



54
55
56
57
58
# File 'lib/openassets/provider/bitcoin_core_provider.rb', line 54

def sign_transaction(tx)
  signed_tx = request('signrawtransaction', tx)
  raise OpenAssets::Error, 'Could not sign the transaction.' unless signed_tx['complete']
  Bitcoin::Protocol::Tx.new(signed_tx['hex'].htb)
end