Class: Glueby::Internal::Wallet::AbstractWalletAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/glueby/internal/wallet/abstract_wallet_adapter.rb

Overview

This is an abstract class for wallet adapters. If you want to use a wallet component with Glueby modules, you can use it by adding subclass of this abstract class for the wallet component.

Instance Method Summary collapse

Instance Method Details

#balance(wallet_id, only_finalized = true) ⇒ Integer

Returns amount of tapyrus that the wallet has.

Parameters:

  • wallet_id (String)
    • The wallet id that is offered by ‘create_wallet()` method.

  • only_finalized (Boolean) (defaults to: true)
    • The balance includes only finalized UTXO value if it

    is true. Default is true.

Returns:

  • (Integer)

    The balance of the wallet. The unit is ‘tapyrus’. 1 TPC equals to 10^8 ‘tapyrus’.

Raises:

  • (NotImplementedError)


64
65
66
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 64

def balance(wallet_id, only_finalized = true)
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

#broadcast(wallet_id, tx) { ... } ⇒ String

Broadcast the transaction to the Tapyrus Network.

Parameters:

  • wallet_id (String)
    • The wallet id that is offered by ‘create_wallet()` method.

  • tx (Tapyrus::Tx)
    • The transaction to be broadcasterd.

Yields:

  • Option. If a block given, the block is called before actual broadcasting. @yieldparam [Tapyrus::Tx] tx - The tx that is going to be broadcasted as is.

Returns:

  • (String)

    txid

Raises:

  • (NotImplementedError)


137
138
139
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 137

def broadcast(wallet_id, tx, &block)
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

#change_address(wallet_id) ⇒ String

Returns an address to change coin.

Parameters:

  • wallet_id (String)
    • The wallet id that is offered by ‘create_wallet()` method.

Returns:

  • (String)

    P2PKH address

Raises:

  • (NotImplementedError)


154
155
156
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 154

def change_address(wallet_id)
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

#create_pay_to_contract_address(wallet_id, contents) ⇒ String

Create and returns pay to contract address

Parameters:

  • wallet_id (String)
    • The wallet id that is offered by ‘create_wallet()` method.

  • contents (String)
    • The data to be used for generating pay-to-contract address

Returns:

  • (String)

    pay to contract P2PKH address

Raises:

  • (NotImplementedError)


193
194
195
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 193

def create_pay_to_contract_address(wallet_id, contents)
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

#create_pubkey(wallet_id) ⇒ Tapyrus::Key

Returns a new public key.

This method is expected to returns a new public key. The key would generate internally. This key is provided for contracts that need public key such as multi sig transaction.

Parameters:

  • wallet_id (String)
    • The wallet id that is offered by ‘create_wallet()` method.

Returns:

  • (Tapyrus::Key)

Raises:

  • (NotImplementedError)


174
175
176
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 174

def create_pubkey(wallet_id)
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

#create_wallet(wallet_id = nil) ⇒ String

Creates a new wallet inside the wallet component and returns ‘wallet_id`. The created wallet is loaded from at first.

Returns:

  • (String)

    wallet_id

Raises:



13
14
15
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 13

def create_wallet(wallet_id = nil)
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

#delete_wallet(wallet_id) ⇒ Boolean

Delete the wallet inside the wallet component. This method expect that the wallet will be removed completely in the wallet and it cannot restore. It is assumed that the main use-case of this method is for testing.

Parameters:

  • wallet_id (String)
    • The wallet id that is offered by ‘create_wallet()` method.

Returns:

  • (Boolean)

    The boolean value whether the deletion was success.

Raises:

  • (NotImplementedError)


24
25
26
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 24

def delete_wallet(wallet_id)
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

#get_addresses(wallet_id, label = nil) ⇒ Array<String>

Returns an array of addresses

This method is expected to return the list of addresses that wallet has.

Parameters:

  • wallet_id (String)
    • The wallet id that is offered by ‘create_wallet()` method.

  • label (String) (defaults to: nil)

    The label to filter the addresses.

Returns:

  • (Array<String>)

    array of P2PKH address

Raises:

  • (NotImplementedError)


185
186
187
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 185

def get_addresses(wallet_id, label = nil)
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

#get_addresses_info(addresses) ⇒ Array<Hash>

Returns information for the addresses

Parameters:

  • addresses (String, Array<String>)
    • The p2pkh address to get information about

Returns:

  • (Array<Hash>)

    The array of hash instance which has keys wallet_id, label and purpose. Returns blank array if the key correspond with the address is not exist.

Raises:

  • (NotImplementedError)


163
164
165
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 163

def get_addresses_info(addresses)
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

#has_address?(wallet_id, address) ⇒ Boolean

Return if wallet has the address

Parameters:

  • wallet_id (String)

    The wallet id that is offered by ‘create_wallet()` method.

  • address (String)

    address

Returns:

  • (Boolean)

    true if the wallet has an address, false otherwise

Raises:

  • (NotImplementedError)


228
229
230
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 228

def has_address?(wallet_id, address)
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

#list_unspent(wallet_id, only_finalized = true, label = nil, color_id: nil) ⇒ Array of UTXO

Returns the UTXOs that the wallet has. If label is specified, return UTXOs filtered with label

## The UTXO structure

  • txid: [String] Transaction id

  • vout: [Integer] Output index

  • amount: [Integer] Amount of the UTXO as tapyrus unit

  • finalized: [Boolean] Whether the UTXO is finalized

  • color_id: [String] Color id of the UTXO. If it is TPC UTXO, color_id is nil.

  • script_pubkey: [String] Script pubkey of the UTXO

Parameters:

  • wallet_id (String)
    • The wallet id that is offered by ‘create_wallet()` method.

  • only_finalized (Boolean) (defaults to: true)
    • The UTXOs includes only finalized UTXO value if it

    is true. Default is true.

  • label (String) (defaults to: nil)
    • Label for filtering UTXOs

    • If label is nil or :unlabeled, only unlabeled UTXOs will be returned.

    • If label=:all, it will return all utxos

  • color_id (Tapyrus::Color::ColorIdentifier) (defaults to: nil)
    • The color identifier.

    It will return only UTXOs with specified color_id. If color_id is nil, it will return all UTXOs. If Tapyrus::Color::ColorIdentifier.default is specified, it will return uncolored UTXOs(i.e. TPC)

Returns:

  • (Array of UTXO)

Raises:

  • (NotImplementedError)


105
106
107
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 105

def list_unspent(wallet_id, only_finalized = true, label = nil, color_id: nil)
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

#list_unspent_with_count(wallet_id, only_finalized = true, label = nil, color_id: nil, page: 1, per: 25) ⇒ Array<Utxo>

Returns all tokens with specified color_id

Parameters:

  • wallet_id (String)
    • The wallet id that is offered by ‘create_wallet()` method.

  • only_finalized (Boolean) (defaults to: true)
    • includes only finalized UTXO value if it

    is true. Default is true.

  • color_id (Tapyrus::Color::ColorIdentifier) (defaults to: nil)

    The color identifier associated with UTXO. It will return only UTXOs with specified color_id. If color_id is nil, it will return all UTXOs. If Tapyrus::Color::ColorIdentifier.default is specified, it will return uncolored UTXOs(i.e. TPC)

  • page (Integer) (defaults to: 1)
    • The page parameter is responsible for specifying the current page being viewed within the paginated results. default is 1.

  • per (Integer) (defaults to: 25)
    • The per parameter is used to determine the number of items to display per page. default is 25.

Returns:

  • (Array<Utxo>)

    The array of the utxos with specified color_id

Raises:

  • (NotImplementedError)


79
80
81
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 79

def list_unspent_with_count(wallet_id, only_finalized = true, label = nil, color_id: nil, page: 1, per: 25)
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

#load_wallet(wallet_id) ⇒ Object

Load the wallet inside the wallet component. This method makes the wallet to possible to use on other methods like ‘balance`, `list_unspent`, etc. All the methods that gets `wallet_id` as a argument needs to load the wallet before use it. If the wallet component doesn’t need such as load operation to prepare to use the wallet, ‘load_wallet` can be empty method.

Parameters:

  • wallet_id (String)
    • The wallet id that is offered by ‘create_wallet()` method.

Raises:



38
39
40
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 38

def load_wallet(wallet_id)
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

#lock_unspent(wallet_id, utxo) ⇒ Object

Attempt to lock a specified utxo and update lock_at field

Parameters:

  • wallet_id (String)

    The wallet id that is offered by ‘create_wallet()` method.

  • utxo (Hash)

    The utxo hash object



113
114
115
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 113

def lock_unspent(wallet_id, utxo)
  true
end

#pay_to_contract_key(wallet_id, payment_base, contents) ⇒ Tapyrus::Key

Create pay to contract key

Parameters:

  • wallet_id (String)
    • The wallet id that is offered by ‘create_wallet()` method.

  • payment_base (Tapyrus::Key)
    • The public key used to generate pay to contract public key

  • contents (String)
    • The data to be used for generating pay-to-contract address

Returns:

  • (Tapyrus::Key)

    Key for pay to contract

Raises:

  • (NotImplementedError)


202
203
204
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 202

def pay_to_contract_key(wallet_id, payment_base, contents)
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

#receive_address(wallet_id, label = nil) ⇒ String

Returns an address to receive coin.

Parameters:

  • wallet_id (String)
    • The wallet id that is offered by ‘create_wallet()` method.

  • label (String) (defaults to: nil)

    The label associated with this address.

Returns:

  • (String)

    P2PKH address

Raises:

  • (NotImplementedError)


146
147
148
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 146

def receive_address(wallet_id, label = nil)
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

#sign_to_pay_to_contract_address(wallet_id, tx, utxo, payment_base, contents, sighashtype: ) ⇒ Tapyrus::Tx

Sign to the pay to contract input

Parameters:

  • wallet_id (String)
    • The wallet id that is offered by ‘create_wallet()` method.

  • tx (Tapyrus::Tx)
    • The tx that has pay to contract input in the inputs list

  • utxo (Hash)
    • The utxo that indicates pay to contract output to be signed

  • payment_base (String)
    • The public key that is used to generate pay to contract public key

  • contents (String)
    • The data to be used for generating pay-to-contract address

  • sighashtype (Integer) (defaults to: )
    • The sighash flag for each signature that would be produced here.

Options Hash (utxo):

  • txid (String)
    • Transaction id

  • vout (Integer)
    • Output index

  • amount (Integer)
    • The amount the output has

  • script_pubkey (String)
    • The script_pubkey hex string

Returns:

  • (Tapyrus::Tx)

Raises:



220
221
222
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 220

def sign_to_pay_to_contract_address(wallet_id, tx, utxo, payment_base, contents, sighashtype: Tapyrus::SIGHASH_TYPE[:all])
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

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

Sign to the transaction with a key in the wallet.

Parameters:

  • wallet_id (String)
    • The wallet id that is offered by ‘create_wallet()` method.

  • tx (Tapyrus::Tx)
    • The transaction will be signed.

  • prevtxs (Array) (defaults to: [])
    • array of hash that represents unbroadcasted transaction outputs used by signing tx.

    Each hash has ‘txid`, `vout`, `scriptPubKey`, `amount` fields.

  • sighashtype (Integer) (defaults to: )
    • The sighash flag for each signature that would be produced here.

Returns:

  • (Tapyrus::Tx)

Raises:



126
127
128
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 126

def sign_tx(wallet_id, tx, prevtxs = [], sighashtype: Tapyrus::SIGHASH_TYPE[:all])
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

#unload_wallet(wallet_id) ⇒ Boolean

Unload the wallet inside the wallet component.

Parameters:

  • wallet_id (String)
    • The wallet id that is offered by ‘create_wallet()` method.

Returns:

  • (Boolean)

    The boolean value whether the unloading was success.

Raises:

  • (NotImplementedError)


46
47
48
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 46

def unload_wallet(wallet_id)
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

#walletsArray of String

Returns list of loaded wallet_id.

Returns:

  • (Array of String)
    • Array of wallet_id

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/glueby/internal/wallet/abstract_wallet_adapter.rb', line 53

def wallets
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end