Class: Glueby::Internal::Wallet::TapyrusCoreWalletAdapter

Inherits:
AbstractWalletAdapter show all
Includes:
Util
Defined in:
lib/glueby/internal/wallet/tapyrus_core_wallet_adapter.rb

Defined Under Namespace

Modules: Util

Constant Summary collapse

WALLET_PREFIX =
'wallet-'
RPC_WALLET_ERROR_ERROR_CODE =

Unspecified problem with wallet (key not found etc.)

-4 # Unspecified problem with wallet (key not found etc.)
RPC_WALLET_NOT_FOUND_ERROR_CODE =

Invalid wallet specified

-18 # Invalid wallet specified

Instance Method Summary collapse

Methods included from Util

tpc_to_tapyrus

Methods inherited from AbstractWalletAdapter

#create_pay_to_contract_address, #get_addresses, #get_addresses_info, #has_address?, #list_unspent_with_count, #lock_unspent, #pay_to_contract_key, #sign_to_pay_to_contract_address

Instance Method Details

#balance(wallet_id, only_finalized = true) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/glueby/internal/wallet/tapyrus_core_wallet_adapter.rb', line 76

def balance(wallet_id, only_finalized = true)
  perform_as(wallet_id) do |client|
    confirmed = tpc_to_tapyrus(client.getbalance)
    return confirmed if only_finalized

    confirmed + tpc_to_tapyrus(client.getunconfirmedbalance)
  end
end

#broadcast(wallet_id, tx, &block) ⇒ Object



136
137
138
139
140
141
# File 'lib/glueby/internal/wallet/tapyrus_core_wallet_adapter.rb', line 136

def broadcast(wallet_id, tx, &block)
  perform_as(wallet_id) do |client|
    block.call(tx) if block
    client.sendrawtransaction(tx.to_hex)
  end
end

#change_address(wallet_id) ⇒ Object



149
150
151
152
153
# File 'lib/glueby/internal/wallet/tapyrus_core_wallet_adapter.rb', line 149

def change_address(wallet_id)
  perform_as(wallet_id) do |client|
    client.getrawchangeaddress
  end
end

#create_pubkey(wallet_id) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/glueby/internal/wallet/tapyrus_core_wallet_adapter.rb', line 155

def create_pubkey(wallet_id)
  perform_as(wallet_id) do |client|
    address = client.getnewaddress('')
    info = client.getaddressinfo(address)
    Tapyrus::Key.new(pubkey: info['pubkey'], key_type: Tapyrus::Key::TYPES[:compressed])
  end
end

#create_wallet(wallet_id = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/glueby/internal/wallet/tapyrus_core_wallet_adapter.rb', line 30

def create_wallet(wallet_id = nil)
  wallet_id = SecureRandom.hex(16) unless wallet_id
  begin
    RPC.client.createwallet(wallet_name(wallet_id))
  rescue Tapyrus::RPC::Error => ex
    if ex.rpc_error && ex.rpc_error['code'] == RPC_WALLET_ERROR_ERROR_CODE && /Wallet wallet-#{wallet_id} already exists\./ =~ ex.rpc_error['message']
      raise Errors::WalletAlreadyCreated, "Wallet #{wallet_id} has been already created."
    else
      raise ex
    end
  end

  wallet_id
end

#delete_wallet(wallet_id) ⇒ Object

On TapyrusCoreWallet, there are no way to delete real wallet via RPC. So, here just unload.



47
48
49
# File 'lib/glueby/internal/wallet/tapyrus_core_wallet_adapter.rb', line 47

def delete_wallet(wallet_id)
  unload_wallet(wallet_id)
end

#list_unspent(wallet_id, only_finalized = true, label = nil, color_id: nil) ⇒ Object

If label=nil, it will return unlabeled utxos to protect labeled utxos for specific purpose If label=:all, it will return all utxos



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/glueby/internal/wallet/tapyrus_core_wallet_adapter.rb', line 87

def list_unspent(wallet_id, only_finalized = true, label = nil, color_id: nil)
  perform_as(wallet_id) do |client|
    min_conf = only_finalized ? 1 : 0
    res = client.listunspent(min_conf)

    if [:unlabeled, nil].include?(label)
      res = res.filter { |i| i['label'] == "" }
    elsif label && (label != :all)
      res = res.filter { |i| i['label'] == label }
    else
      res
    end

    if color_id
      res = res.filter do |i|
        script = Tapyrus::Script.parse_from_payload(i['scriptPubKey'].htb)
        script.cp2pkh? || script.cp2sh? && color_id == Tapyrus::Color::ColorIdentifier.parse_from_payload(script.chunks[0].pushed_data)
      end
    end

    res.map do |i|
      script = Tapyrus::Script.parse_from_payload(i['scriptPubKey'].htb)
      color_id = if script.cp2pkh? || script.cp2sh?
        Tapyrus::Color::ColorIdentifier.parse_from_payload(script.chunks[0].pushed_data).to_hex
      end
      {
        txid: i['txid'],
        vout: i['vout'],
        script_pubkey: i['scriptPubKey'],
        color_id: color_id,
        amount: tpc_to_tapyrus(i['amount']),
        finalized: i['confirmations'] != 0,
        label: i['label']
      }
    end
  end
end

#load_wallet(wallet_id) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/glueby/internal/wallet/tapyrus_core_wallet_adapter.rb', line 51

def load_wallet(wallet_id)
  RPC.client.loadwallet(wallet_name(wallet_id))
rescue Tapyrus::RPC::Error => ex
  if ex.rpc_error && ex.rpc_error['code'] == RPC_WALLET_ERROR_ERROR_CODE && /Duplicate -wallet filename specified/ =~ ex.rpc_error['message']
    raise Errors::WalletAlreadyLoaded, "Wallet #{wallet_id} has been already loaded."
  elsif ex.rpc_error && ex.rpc_error['code'] == RPC_WALLET_NOT_FOUND_ERROR_CODE
    raise Errors::WalletNotFound, "Wallet #{wallet_id} does not found"
  else
    raise ex
  end
end

#receive_address(wallet_id, label = nil) ⇒ Object



143
144
145
146
147
# File 'lib/glueby/internal/wallet/tapyrus_core_wallet_adapter.rb', line 143

def receive_address(wallet_id, label = nil)
  perform_as(wallet_id) do |client|
    client.getnewaddress(label || '')
  end
end

#sign_tx(wallet_id, tx, prevtxs = [], sighashtype: ) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/glueby/internal/wallet/tapyrus_core_wallet_adapter.rb', line 125

def sign_tx(wallet_id, tx, prevtxs = [], sighashtype: Tapyrus::SIGHASH_TYPE[:all])
  perform_as(wallet_id) do |client|
    res = client.signrawtransactionwithwallet(tx.to_hex, prevtxs, encode_sighashtype(sighashtype))
    if res['complete']
      Tapyrus::Tx.parse_from_payload(res['hex'].htb)
    else
      raise res['errors'].to_json
    end
  end
end

#unload_wallet(wallet_id) ⇒ Object



63
64
65
# File 'lib/glueby/internal/wallet/tapyrus_core_wallet_adapter.rb', line 63

def unload_wallet(wallet_id)
  RPC.client.unloadwallet(wallet_name(wallet_id))
end

#walletsObject



67
68
69
70
71
72
73
74
# File 'lib/glueby/internal/wallet/tapyrus_core_wallet_adapter.rb', line 67

def wallets
  RPC.client.listwallets.map do |wallet_name|
    match = /\A#{WALLET_PREFIX}(?<wallet_id>[0-9A-Fa-f]{32})\z/.match(wallet_name)
    next unless match

    match[:wallet_id]
  end.compact
end