class Zilliqa::Account::Wallet

Public Class Methods

new(provider = nil, accounts = {}) click to toggle source

Takes an array of Account objects and instantiates a Wallet instance.

# File lib/zilliqa/account/wallet.rb, line 8
def initialize(provider = nil, accounts = {})
  @provider = provider
  @accounts = accounts
  if accounts.length > 0
    @default_account = accounts[0]
  else
    @default_account = nil
  end
end

Public Instance Methods

add_by_keystore(keystore, passphrase) click to toggle source

Adds an account by keystore

# File lib/zilliqa/account/wallet.rb, line 44
def add_by_keystore(keystore, passphrase)
  account = Zilliqa::Account::Account.from_file(keystore, passphrase)

  @accounts[account.address] = account

  @default_account = account unless @default_account

  account.address
end
add_by_private_key(private_key) click to toggle source

Adds an account to the wallet by private key.

# File lib/zilliqa/account/wallet.rb, line 32
def add_by_private_key(private_key)
  account = Zilliqa::Account::Account.new(private_key)

  @accounts[account.address] = account

  @default_account = account unless @default_account

  account.address
end
create() click to toggle source

Creates a new keypair with a randomly-generated private key. The new account is accessible by address.

# File lib/zilliqa/account/wallet.rb, line 20
def create
  private_key = Zilliqa::Crypto::KeyTool.generate_private_key
  account = Zilliqa::Account::Account.new(private_key)

  @accounts[account.address] = account

  @default_account = account unless @default_account

  account.address
end
remove(address) click to toggle source

Removes an account from the wallet and returns boolean to indicate failure or success.

# File lib/zilliqa/account/wallet.rb, line 57
def remove(address)
  if @accounts.has_key?(address)
    @accounts.delete(address)

    true
  else
    false
  end
end
set_default(address) click to toggle source

Sets the default account of the wallet.

# File lib/zilliqa/account/wallet.rb, line 68
def set_default(address)
  @default_account = @accounts[address]
end
sign(tx) click to toggle source

signs an unsigned transaction with the default account.

# File lib/zilliqa/account/wallet.rb, line 88
def sign(tx)
  if tx.sender_pub_key
    # attempt to find the address
    address = Zilliqa::Crypto::KeyTool.get_address_from_public_key(tx.sender_pub_key)
    account = @accounts[address]
    raise 'Could not sign the transaction with address as it does not exist' unless account

    sign_with(tx, address)
  else
    raise 'This wallet has no default account.' unless @default_account

    sign_with(tx, @default_account.address)
  end
end
sign_with(tx, address) click to toggle source
# File lib/zilliqa/account/wallet.rb, line 103
def sign_with(tx, address)
  account = @accounts[address]
  address = account.address

  raise 'The selected account does not exist on this Wallet instance.' unless account

  if tx.nonce.nil?
    result = @provider.GetBalance(address)
    tx.nonce = result['nonce'].to_i + 1
  end

  tx.sender_pub_key = account.public_key
  sig = account.sign_transaction(tx)
  tx.signature = sig.to_s
  tx
end
transfer(to_addr, amount) click to toggle source
# File lib/zilliqa/account/wallet.rb, line 73
def transfer(to_addr, amount)
  gas_price = Integer(@provider.GetMinimumGasPrice)
  gas_limit = 1

  tx = sign(Zilliqa::Account::Transaction.new({
    version: MAINNET,
    amount: amount.to_s,
    to_addr: to_addr,
    gas_price: gas_price.to_s,
    gas_limit: gas_limit
  }, @provider))
  tx.submit!
end