Module: Bitcoin::RPC::RequestHandler

Defined in:
lib/extensions/bitcoin/rpc/request_handler.rb

Instance Method Summary collapse

Instance Method Details

#createaccount(account_name) ⇒ Object



85
86
87
88
89
90
# File 'lib/extensions/bitcoin/rpc/request_handler.rb', line 85

def createaccount()
   = node.wallet.(Bitcoin::Wallet::Account::PURPOSE_TYPE[:native_segwit], )
  .to_h
rescue
  {}
end

#getassetbalance(account_name, asset_type = Bitcoin::Wallet::AssetFeature::AssetType::OPEN_ASSETS, asset_id = nil) ⇒ Object



72
73
74
# File 'lib/extensions/bitcoin/rpc/request_handler.rb', line 72

def getassetbalance(, asset_type = Bitcoin::Wallet::AssetFeature::AssetType::OPEN_ASSETS , asset_id = nil)
  node.wallet.get_asset_balance(asset_type, asset_id, account_name: )
end

#getbalance(account_name) ⇒ Object



68
69
70
# File 'lib/extensions/bitcoin/rpc/request_handler.rb', line 68

def getbalance()
  node.wallet.get_balance()
end

#getnewaddress(account_name) ⇒ Object

create new bitcoin address for receiving payments.



77
78
79
80
81
82
83
# File 'lib/extensions/bitcoin/rpc/request_handler.rb', line 77

def getnewaddress()
  address = node.wallet.generate_new_address()
  script = Bitcoin::Script.parse_from_addr(address)
  pubkey_hash = script.witness_data[1].bth
  node.filter_add(pubkey_hash)
  address
end

#listcoloredunspentinaccount(account_name, asset_type = Bitcoin::Wallet::AssetFeature::AssetType::OPEN_ASSETS, asset_id = nil, min = 0, max = 999999) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/extensions/bitcoin/rpc/request_handler.rb', line 46

def listcoloredunspentinaccount(, asset_type = Bitcoin::Wallet::AssetFeature::AssetType::OPEN_ASSETS , asset_id = nil, min = 0, max = 999999)
  height = node.chain.latest_block.height
  assets = node.wallet.(asset_type, asset_id, account_name: , current_block_height: height, min: min, max: max).map do |asset|
    out_point = Bitcoin::OutPoint.new(asset.tx_hash, asset.index)
    utxo = node.wallet.utxo_db.get_utxo(out_point)
    next unless utxo
    [asset, utxo]
  end.compact
  assets = assets.map do |(asset, utxo)|
    {
      tx_hash: utxo.tx_hash,
      index: utxo.index,
      value: utxo.value,
      asset_type: asset.asset_type,
      asset_id: asset.asset_id,
      asset_quantity: asset.asset_quantity,
      script_pubkey: utxo.script_pubkey,
      confirmations: height - utxo.block_height
    }
  end
end

#listuncoloredunspentinaccount(account_name, min = 0, max = 999999) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/extensions/bitcoin/rpc/request_handler.rb', line 32

def listuncoloredunspentinaccount(, min = 0, max = 999999)
  height = node.chain.latest_block.height
  utxos = node.wallet.list_uncolored_unspent(account_name: , current_block_height: height, min: min, max: max)
  utxos.map do |u|
    {
      tx_hash: u.tx_hash,
      index: u.index,
      value: u.value,
      script_pubkey: u.script_pubkey,
      confirmations: height - u.block_height
    }
  end
end

#listunspent(min = 0, max = 999999, addresses: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/extensions/bitcoin/rpc/request_handler.rb', line 4

def listunspent(min = 0, max = 999999, addresses: nil)
  height = node.chain.latest_block.height
  utxos = node.wallet.list_unspent(current_block_height: height, min: min, max: max, addresses: addresses)
  utxos.map do |u|
    {
      tx_hash: u.tx_hash,
      index: u.index,
      value: u.value,
      script_pubkey: u.script_pubkey,
      confirmations: height - u.block_height
    }
  end
end

#listunspentinaccount(account_name, min = 0, max = 999999) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/extensions/bitcoin/rpc/request_handler.rb', line 18

def listunspentinaccount(, min = 0, max = 999999)
  height = node.chain.latest_block.height
  utxos = node.wallet.list_unspent(account_name: , current_block_height: height, min: min, max: max)
  utxos.map do |u|
    {
      tx_hash: u.tx_hash,
      index: u.index,
      value: u.value,
      script_pubkey: u.script_pubkey,
      confirmations: height - u.block_height
    }
  end
end

#signrawtransaction(account_name, payload) ⇒ Object



92
93
94
95
96
# File 'lib/extensions/bitcoin/rpc/request_handler.rb', line 92

def signrawtransaction(, payload)
  tx = Bitcoin::Tx.parse_from_payload(payload.htb)
  signed_tx = Bitcoin::Wallet::Signer.sign(node.wallet, , tx)
  { hex: signed_tx.to_payload.bth }
end