class Glueby::Internal::Wallet

# Glueby::Internal::Wallet

This module provides the way to deal about wallet that includes key management, address management, getting UTXOs.

## How to use

First, you need to configure which wallet implementation is used in Glueby::Internal::Wallet. For now, below wallets are supported.

Here shows an example to use Tapyrus Core wallet.

“`ruby # Setup Tapyrus Core RPC connection config = {schema: 'http', host: '127.0.0.1', port: 12381, user: 'user', password: 'pass'} Glueby::Internal::RPC.configure(config)

# Setup wallet adapter Glueby::Internal::Wallet.wallet_adapter = Glueby::Internal::Wallet::TapyrusCoreWalletAdapter.new

# Create wallet wallet = Glueby::Internal::Wallet.create wallet.balance # => 0 wallet.list_unspent “`

Attributes

id[R]

Public Class Methods

create(wallet_id = nil) click to toggle source
# File lib/glueby/internal/wallet.rb, line 37
def create(wallet_id = nil)
  begin
    wallet_id = wallet_adapter.create_wallet(wallet_id)
  rescue Errors::WalletAlreadyCreated => _
    # Ignore when wallet is already created.
  end
  new(wallet_id)
end
load(wallet_id) click to toggle source
# File lib/glueby/internal/wallet.rb, line 46
def load(wallet_id)
  begin
    wallet_adapter.load_wallet(wallet_id)
  rescue Errors::WalletAlreadyLoaded => _
    # Ignore when wallet is already loaded.
  end
  new(wallet_id)
end
new(wallet_id) click to toggle source
# File lib/glueby/internal/wallet.rb, line 77
def initialize(wallet_id)
  @id = wallet_id
end
wallet_adapter() click to toggle source
# File lib/glueby/internal/wallet.rb, line 69
def wallet_adapter
  @wallet_adapter or
    raise Errors::ShouldInitializeWalletAdapter, 'You should initialize wallet adapter using `Glueby::Internal::Wallet.wallet_adapter = some wallet adapter instance`.'
end
wallet_adapter=(adapter) click to toggle source
# File lib/glueby/internal/wallet.rb, line 59
def wallet_adapter=(adapter)
  if adapter.is_a?(ActiveRecordWalletAdapter)
    BlockSyncer.register_syncer(ActiveRecordWalletAdapter::Syncer)
  else
    BlockSyncer.unregister_syncer(ActiveRecordWalletAdapter::Syncer)
  end

  @wallet_adapter = adapter
end
wallets() click to toggle source
# File lib/glueby/internal/wallet.rb, line 55
def wallets
  wallet_adapter.wallets.map { |id| new(id) }
end

Public Instance Methods

balance(only_finalized = true) click to toggle source
# File lib/glueby/internal/wallet.rb, line 81
def balance(only_finalized = true)
  wallet_adapter.balance(id, only_finalized)
end
broadcast(tx, without_fee_provider: false, &block) click to toggle source

Broadcast a transaction via Tapyrus Core RPC @param [Tapyrus::Tx] tx The tx that would be broadcasted @option [Boolean] without_fee_provider The flag to avoid to use FeeProvider temporary. @param [Proc] block The block that is called before broadcasting. It can be used to handle tx that is modified by FeeProvider.

# File lib/glueby/internal/wallet.rb, line 118
def broadcast(tx, without_fee_provider: false, &block)
  tx = FeeProvider.provide(tx) if !without_fee_provider && Glueby.configuration.fee_provider_bears?
  wallet_adapter.broadcast(id, tx, &block)
  tx
end
change_address() click to toggle source
# File lib/glueby/internal/wallet.rb, line 128
def change_address
  wallet_adapter.change_address(id)
end
collect_uncolored_outputs(amount, label = nil, only_finalized = true) click to toggle source
# File lib/glueby/internal/wallet.rb, line 136
def collect_uncolored_outputs(amount, label = nil, only_finalized = true)
  utxos = list_unspent(only_finalized, label)

  utxos.inject([0, []]) do |sum, output|
    next sum if output[:color_id]

    new_sum = sum[0] + output[:amount]
    new_outputs = sum[1] << output
    return [new_sum, new_outputs] if new_sum >= amount

    [new_sum, new_outputs]
  end
  raise Glueby::Contract::Errors::InsufficientFunds
end
create_pubkey() click to toggle source
# File lib/glueby/internal/wallet.rb, line 132
def create_pubkey
  wallet_adapter.create_pubkey(id)
end
delete() click to toggle source
# File lib/glueby/internal/wallet.rb, line 93
def delete
  wallet_adapter.delete_wallet(id)
end
get_addresses(label = nil) click to toggle source
# File lib/glueby/internal/wallet.rb, line 151
def get_addresses(label = nil)
  wallet_adapter.get_addresses(id, label)
end
list_unspent(only_finalized = true, label = nil) click to toggle source

@param only_finalized [Boolean] The flag to get a UTXO with status only finalized @param label [String] This label is used to filtered the UTXOs with labeled if a key or Utxo is labeled.

- If label is not specified (label=nil), all UTXOs will be returned.
- If label=:unlabeled, only unlabeled UTXOs will be returned.
# File lib/glueby/internal/wallet.rb, line 89
def list_unspent(only_finalized = true, label = nil)
  wallet_adapter.list_unspent(id, only_finalized, label)
end
receive_address(label = nil) click to toggle source
# File lib/glueby/internal/wallet.rb, line 124
def receive_address(label = nil)
  wallet_adapter.receive_address(id, label)
end
sign_tx(tx, prev_txs = [], for_fee_provider_input: false) click to toggle source

@param [Tapyrus::Tx] tx The tx that is signed @param [Array<Hash>] prev_txs An array of hash that represents unbroadcasted transaction outputs used by signing tx

@option prev_txs [String] :txid
@option prev_txs [Integer] :vout
@option prev_txs [String] :scriptPubkey
@option prev_txs [Integer] :amount

@param [Boolean] for_fee_provider_input The flag to notify whether the caller is FeeProvider and called for signing a input that is by FeeProvider.

# File lib/glueby/internal/wallet.rb, line 104
def sign_tx(tx, prev_txs = [], for_fee_provider_input: false)
  sighashtype = Tapyrus::SIGHASH_TYPE[:all]

  if !for_fee_provider_input && Glueby.configuration.fee_provider_bears?
    sighashtype |= Tapyrus::SIGHASH_TYPE[:anyonecanpay]
  end

  wallet_adapter.sign_tx(id, tx, prev_txs, sighashtype: sighashtype)
end

Private Instance Methods

wallet_adapter() click to toggle source
# File lib/glueby/internal/wallet.rb, line 157
def wallet_adapter
  self.class.wallet_adapter
end