module Bitcoin::RPC::RequestHandler

RPC server's request handler.

Public Instance Methods

createwallet(wallet_id = 1, wallet_path_prefix = Bitcoin::Wallet::Base.default_path_prefix) click to toggle source

create wallet

# 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) click to toggle source

decode tx data.

# 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) click to toggle source

decode script data.

# 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) click to toggle source

encrypt wallet.

# 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() click to toggle source

Returns an object containing various state info regarding blockchain processing.

# 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) click to toggle source

get block header information. @param [String] block_id block hash(big endian)

# 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) click to toggle source

create new bitcoin address for receiving payments.

# File lib/bitcoin/rpc/request_handler.rb, line 143
def getnewaddress(account_name)
  node.wallet.generate_new_address(account_name)
end
getpeerinfo() click to toggle source

Returns connected peer information.

# 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() click to toggle source

get current wallet information.

# File lib/bitcoin/rpc/request_handler.rb, line 121
def getwalletinfo
  node.wallet ? node.wallet.to_h : {}
end
listaccounts() click to toggle source

get the list of current Wallet accounts.

# 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) click to toggle source

get wallet list.

# 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) click to toggle source

broadcast transaction

# 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() click to toggle source

shutdown node

# File lib/bitcoin/rpc/request_handler.rb, line 20
def stop
  node.shutdown
end