module Tapyrus::RPC::RequestHandler

RPC server's request handler.

Public Instance Methods

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

create wallet

# File lib/tapyrus/rpc/request_handler.rb, line 112
def createwallet(wallet_id = 1, wallet_path_prefix = Tapyrus::Wallet::Base.default_path_prefix)
  wallet = Tapyrus::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/tapyrus/rpc/request_handler.rb, line 88
def decoderawtransaction(hex_tx)
  begin
    Tapyrus::Tx.parse_from_payload(hex_tx.htb).to_h
  rescue Exception
    raise ArgumentError.new('TX decode failed')
  end
end
decodescript(hex_script) click to toggle source

decode script data.

# File lib/tapyrus/rpc/request_handler.rb, line 97
def decodescript(hex_script)
  begin
    script = Tapyrus::Script.parse_from_payload(hex_script.htb)
    h = script.to_h
    h.delete(:hex)
    h[:p2sh] = script.to_p2sh.addresses.first unless script.p2sh?
    h
  rescue Exception
    raise ArgumentError.new('Script decode failed')
  end
end
encryptwallet(passphrase) click to toggle source

encrypt wallet.

# File lib/tapyrus/rpc/request_handler.rb, line 137
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/tapyrus/rpc/request_handler.rb, line 6
def getblockchaininfo
  h = {}
  h[:chain] = Tapyrus.chain_params.network
  best_block = node.chain.latest_block
  h[:headers] = best_block.height
  h[:bestblockhash] = best_block.header.block_id
  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/tapyrus/rpc/request_handler.rb, line 23
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,
      features: entry.header.features,
      featuresHex: entry.header.features.to_even_length_hex.ljust(8, '0'),
      merkleroot: entry.header.merkle_root.rhex,
      immutablemerkleroot: entry.header.im_merkle_root.rhex,
      time: entry.header.time,
      mediantime: node.chain.mtp(block_hash),
      xfield_type: entry.header.x_field_type,
      xfield: entry.header.x_field,
      proof: entry.header.proof,
      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 tapyrus address for receiving payments.

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

Returns connected peer information.

# File lib/tapyrus/rpc/request_handler.rb, line 49
def getpeerinfo
  node
    .pool
    .peers
    .map do |peer|
      local_addr = "#{peer.remote_version.remote_addr.ip}: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/tapyrus/rpc/request_handler.rb, line 124
def getwalletinfo
  node.wallet ? node.wallet.to_h : {}
end
listaccounts() click to toggle source

get the list of current Wallet accounts.

# File lib/tapyrus/rpc/request_handler.rb, line 129
def listaccounts
  return {} unless node.wallet
  accounts = {}
  node.wallet.accounts.each { |a| accounts[a.name] = node.wallet.get_balance(a) }
  accounts
end
listwallets(wallet_path_prefix = Tapyrus::Wallet::Base.default_path_prefix) click to toggle source

get wallet list.

# File lib/tapyrus/rpc/request_handler.rb, line 119
def listwallets(wallet_path_prefix = Tapyrus::Wallet::Base.default_path_prefix)
  Tapyrus::Wallet::Base.wallet_paths(wallet_path_prefix)
end
sendrawtransaction(hex_tx) click to toggle source

broadcast transaction

# File lib/tapyrus/rpc/request_handler.rb, line 79
def sendrawtransaction(hex_tx)
  tx = Tapyrus::Tx.parse_from_payload(hex_tx.htb)

  # TODO check wether tx is valid
  node.broadcast(tx)
  tx.txid
end
stop() click to toggle source

shutdown node

# File lib/tapyrus/rpc/request_handler.rb, line 17
def stop
  node.shutdown
end