Module: Bitcoin::RPC::RequestHandler
- Included in:
- HttpServer
- Defined in:
- lib/bitcoin/rpc/request_handler.rb
Overview
RPC server’s request handler.
Instance Method Summary collapse
-
#createwallet(wallet_id = 1, wallet_path_prefix = Bitcoin::Wallet::Base.default_path_prefix) ⇒ Object
create wallet.
-
#decoderawtransaction(hex_tx) ⇒ Object
decode tx data.
-
#decodescript(hex_script) ⇒ Object
decode script data.
-
#encryptwallet(passphrase) ⇒ Object
encrypt wallet.
-
#getblockchaininfo ⇒ Object
Returns an object containing various state info regarding blockchain processing.
-
#getblockheader(block_id, verbose) ⇒ Object
get block header information.
-
#getnewaddress(account_name) ⇒ Object
create new bitcoin address for receiving payments.
-
#getpeerinfo ⇒ Object
Returns connected peer information.
-
#getwalletinfo ⇒ Object
get current wallet information.
-
#listaccounts ⇒ Object
get the list of current Wallet accounts.
-
#listwallets(wallet_path_prefix = Bitcoin::Wallet::Base.default_path_prefix) ⇒ Object
get wallet list.
-
#sendrawtransaction(hex_tx) ⇒ Object
broadcast transaction.
-
#stop ⇒ Object
shutdown node.
Instance Method Details
#createwallet(wallet_id = 1, wallet_path_prefix = Bitcoin::Wallet::Base.default_path_prefix) ⇒ Object
create wallet
109 110 111 112 113 |
# File 'lib/bitcoin/rpc/request_handler.rb', line 109 def createwallet(wallet_id = 1, wallet_path_prefix = Bitcoin::Wallet::Base.default_path_prefix) wallet = Bitcoin::Wallet::Base.create(wallet_id, wallet_path_prefix) node.wallet = wallet unless node.wallet {wallet_id: wallet.wallet_id, mnemonic: wallet.master_key.mnemonic} end |
#decoderawtransaction(hex_tx) ⇒ Object
decode tx data.
85 86 87 88 89 90 91 |
# File 'lib/bitcoin/rpc/request_handler.rb', line 85 def decoderawtransaction(hex_tx) begin Bitcoin::Tx.parse_from_payload(hex_tx.htb, strict: true ).to_h rescue Exception raise ArgumentError.new('TX decode failed') end end |
#decodescript(hex_script) ⇒ Object
decode script data.
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/bitcoin/rpc/request_handler.rb', line 94 def decodescript(hex_script) begin script = Bitcoin::Script.parse_from_payload(hex_script.htb) h = script.to_h h.delete(:hex) h[:p2sh] = script.to_p2sh.to_addr unless script.p2sh? h rescue Exception raise ArgumentError.new('Script decode failed') end end |
#encryptwallet(passphrase) ⇒ Object
encrypt wallet.
136 137 138 139 140 |
# File 'lib/bitcoin/rpc/request_handler.rb', line 136 def encryptwallet(passphrase) return nil unless node.wallet node.wallet.encrypt(passphrase) "The wallet 'wallet_id: #{node.wallet.wallet_id}' has been encrypted." end |
#getblockchaininfo ⇒ Object
Returns an object containing various state info regarding blockchain processing.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/bitcoin/rpc/request_handler.rb', line 8 def getblockchaininfo h = {} h[:chain] = Bitcoin.chain_params.network best_block = node.chain.latest_block h[:headers] = best_block.height h[:bestblockhash] = best_block.header.block_id h[:chainwork] = best_block.header.work h[:mediantime] = node.chain.mtp(best_block.block_hash) h end |
#getblockheader(block_id, verbose) ⇒ Object
get block header information.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/bitcoin/rpc/request_handler.rb', line 26 def getblockheader(block_id, verbose) block_hash = block_id.rhex entry = node.chain.find_entry_by_hash(block_hash) raise ArgumentError.new('Block not found') unless entry if verbose { hash: block_id, height: entry.height, version: entry.header.version, versionHex: entry.header.version.to_even_length_hex, merkleroot: entry.header.merkle_root.rhex, time: entry.header.time, mediantime: node.chain.mtp(block_hash), nonce: entry.header.nonce, bits: entry.header.bits.to_even_length_hex, previousblockhash: entry.prev_hash.rhex, nextblockhash: node.chain.next_hash(block_hash).rhex } else entry.header.to_hex end end |
#getnewaddress(account_name) ⇒ Object
create new bitcoin address for receiving payments.
143 144 145 |
# File 'lib/bitcoin/rpc/request_handler.rb', line 143 def getnewaddress(account_name) node.wallet.generate_new_address(account_name) end |
#getpeerinfo ⇒ Object
Returns connected peer information.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/bitcoin/rpc/request_handler.rb', line 50 def getpeerinfo node.pool.peers.map do |peer| local_addr = "#{peer.remote_version.remote_addr.addr_string}:18333" { id: peer.id, addr: "#{peer.host}:#{peer.port}", addrlocal: local_addr, services: peer.remote_version.services.to_even_length_hex.rjust(16, '0'), relaytxes: peer.remote_version.relay, lastsend: peer.last_send, lastrecv: peer.last_recv, bytessent: peer.bytes_sent, bytesrecv: peer.bytes_recv, conntime: peer.conn_time, pingtime: peer.ping_time, minping: peer.min_ping, version: peer.remote_version.version, subver: peer.remote_version.user_agent, inbound: !peer.outbound?, startingheight: peer.remote_version.start_height, best_hash: peer.best_hash, best_height: peer.best_height } end end |
#getwalletinfo ⇒ Object
get current wallet information.
121 122 123 |
# File 'lib/bitcoin/rpc/request_handler.rb', line 121 def getwalletinfo node.wallet ? node.wallet.to_h : {} end |
#listaccounts ⇒ Object
get the list of current Wallet accounts.
126 127 128 129 130 131 132 133 |
# File 'lib/bitcoin/rpc/request_handler.rb', line 126 def listaccounts return {} unless node.wallet accounts = {} node.wallet.accounts.each do |a| accounts[a.name] = node.wallet.get_balance(a) end accounts end |
#listwallets(wallet_path_prefix = Bitcoin::Wallet::Base.default_path_prefix) ⇒ Object
get wallet list.
116 117 118 |
# File 'lib/bitcoin/rpc/request_handler.rb', line 116 def listwallets(wallet_path_prefix = Bitcoin::Wallet::Base.default_path_prefix) Bitcoin::Wallet::Base.wallet_paths(wallet_path_prefix) end |
#sendrawtransaction(hex_tx) ⇒ Object
broadcast transaction
77 78 79 80 81 82 |
# File 'lib/bitcoin/rpc/request_handler.rb', line 77 def sendrawtransaction(hex_tx) tx = Bitcoin::Tx.parse_from_payload(hex_tx.htb, strict: true) # TODO check wether tx is valid node.broadcast(tx) tx.txid end |
#stop ⇒ Object
shutdown node
20 21 22 |
# File 'lib/bitcoin/rpc/request_handler.rb', line 20 def stop node.shutdown end |