Class: OpenAssets::Provider::BitcoinCoreProvider
- Inherits:
-
BlockChainProviderBase
- Object
- BlockChainProviderBase
- OpenAssets::Provider::BitcoinCoreProvider
- Defined in:
- lib/openassets/provider/bitcoin_core_provider.rb
Overview
The implementation of BlockChain provider using Bitcoin Core.
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#decode_tx_to_btc_tx(tx) ⇒ Object
Convert decode tx string to Bitcion::Protocol::Tx.
-
#get_transaction(transaction_hash, verbose = 0) ⇒ String
Get raw transaction.
-
#import_address(address) ⇒ Object
Adds an address or pubkey script to the wallet without the associated private key, allowing you to watch for transactions affecting that address or pubkey script without being able to spend any of its outputs.
-
#initialize(config) ⇒ BitcoinCoreProvider
constructor
A new instance of BitcoinCoreProvider.
-
#list_unspent(addresses = [], min = 1, max = 9999999) ⇒ Object
Get an array of unspent transaction outputs belonging to this wallet.
- #request(command, *params) ⇒ Object
-
#send_transaction(tx) ⇒ String
Validates a transaction and broadcasts it to the peer-to-peer network.
-
#sign_transaction(tx) ⇒ Bitcoin::Protocol::Tx
Signs a transaction in the serialized transaction format using private keys.
Constructor Details
#initialize(config) ⇒ BitcoinCoreProvider
Returns a new instance of BitcoinCoreProvider.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/openassets/provider/bitcoin_core_provider.rb', line 11 def initialize(config) @config = config commands = request(:help).split("\n").inject([]) do |memo_ary, line| if !line.empty? && !line.start_with?('==') memo_ary << line.split(' ').first.to_sym end memo_ary end BitcoinCoreProvider.class_eval do commands.each do |command| define_method(command) do |*params| request(command, *params) end end end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
9 10 11 |
# File 'lib/openassets/provider/bitcoin_core_provider.rb', line 9 def config @config end |
Instance Method Details
#decode_tx_to_btc_tx(tx) ⇒ Object
Convert decode tx string to Bitcion::Protocol::Tx
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/openassets/provider/bitcoin_core_provider.rb', line 72 def decode_tx_to_btc_tx(tx) hash = { 'version' => tx['version'], 'lock_time' => tx['locktime'], 'hex' => tx['hex'], 'txid' => tx['txid'], 'blockhash' => tx['blockhash'], 'confirmations' => tx['confirmations'], 'time' => tx['time'], 'blocktime' => tx['blocktime'], 'in' => tx['vin'].map{|input| {'output_index' => input['vout'], 'previous_transaction_hash' => input['txid'], 'coinbase' => input['coinbase'], 'scriptSig' => input['scriptSig']['asm'], 'sequence' => input['sequence']}}, 'out' => tx['vout'].map{|out| {'amount' => out['value'], 'scriptPubKey' => out['scriptPubKey']['asm']} } } Bitcoin::Protocol::Tx.from_hash(hash) end |
#get_transaction(transaction_hash, verbose = 0) ⇒ String
Get raw transaction.
41 42 43 44 45 46 47 |
# File 'lib/openassets/provider/bitcoin_core_provider.rb', line 41 def get_transaction(transaction_hash, verbose = 0) begin getrawtransaction(transaction_hash, verbose) rescue OpenAssets::Provider::ApiError => e nil end end |
#import_address(address) ⇒ Object
Adds an address or pubkey script to the wallet without the associated private key, allowing you to watch for transactions affecting that address or pubkey script without being able to spend any of its outputs.
67 68 69 |
# File 'lib/openassets/provider/bitcoin_core_provider.rb', line 67 def import_address(address) importaddress(address) end |
#list_unspent(addresses = [], min = 1, max = 9999999) ⇒ Object
Get an array of unspent transaction outputs belonging to this wallet.
33 34 35 |
# File 'lib/openassets/provider/bitcoin_core_provider.rb', line 33 def list_unspent(addresses = [], min = 1 , max = 9999999) listunspent(min, max, addresses) end |
#request(command, *params) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/openassets/provider/bitcoin_core_provider.rb', line 92 def request(command, *params) data = { :method => command, :params => params, :id => 'jsonrpc' } post(server_url, @config[:timeout], @config[:open_timeout], data.to_json, content_type: :json) do |respdata, request, result| raise ApiError, result. if !result.kind_of?(Net::HTTPSuccess) && respdata.empty? response = JSON.parse(respdata.gsub(/\\u([\da-fA-F]{4})/) { [$1].pack('H*').unpack('n*').pack('U*').encode('ISO-8859-1').force_encoding('UTF-8') }) raise ApiError, response['error'] if response['error'] response['result'] end end |
#send_transaction(tx) ⇒ String
Validates a transaction and broadcasts it to the peer-to-peer network.
61 62 63 |
# File 'lib/openassets/provider/bitcoin_core_provider.rb', line 61 def send_transaction(tx) sendrawtransaction(tx) end |
#sign_transaction(tx) ⇒ Bitcoin::Protocol::Tx
Signs a transaction in the serialized transaction format using private keys.
52 53 54 55 56 |
# File 'lib/openassets/provider/bitcoin_core_provider.rb', line 52 def sign_transaction(tx) signed_tx = respond_to?(:signrawtransactionwithwallet) ? signrawtransactionwithwallet(tx) : signrawtransaction(tx) raise OpenAssets::Error, 'Could not sign the transaction.' unless signed_tx['complete'] Bitcoin::Protocol::Tx.new(signed_tx['hex'].htb) end |