class Laksa::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/laksa/account/wallet.rb, line 7
def initialize(provider = nil, accounts = {})
  @provider = provider
  @accounts = accounts
  if accounts.length > 0
    @default_account = accounts[0] 
  else
    @default_account = nil
  end
end
to_checksum_address(address) click to toggle source

to_checksum_address

takes hex-encoded string and returns the corresponding address

@param {string} address @returns {string}

# File lib/laksa/account/wallet.rb, line 77
def self.to_checksum_address(address)
  address = address.downcase.gsub('0x', '')

  s1 = Digest::SHA256.hexdigest(Util.decode_hex(address))
  v = s1.to_i(base=16)

  ret = ['0x']
  address.each_char.each_with_index do |c, idx|
    if '1234567890'.include?(c)
      ret << c 
    else
      ret << ((v & (2 ** (255 - 6 * idx))) < 1 ? c.downcase : c.upcase)
    end
  end

  ret.join
end

Public Instance Methods

add_by_keystore(keystore, passphrase) click to toggle source

Adds an account by keystore

# File lib/laksa/account/wallet.rb, line 43
def add_by_keystore(keystore, passphrase)
  account = Laksa::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/laksa/account/wallet.rb, line 31
def add_by_private_key(private_key)
  account = Laksa::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/laksa/account/wallet.rb, line 19
def create
  private_key = Laksa::Crypto::KeyTool.generate_private_key
  account = Laksa::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/laksa/account/wallet.rb, line 56
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/laksa/account/wallet.rb, line 67
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/laksa/account/wallet.rb, line 96
def sign(tx)
  tx_params = tx.tx_params
  if tx_params.sender_pub_key
    # attempt to find the address
    address = Laksa::Crypto::KeyTool.get_address_from_public_key(tx_params.sender_pub_key)
    account = @accounts[address]
    raise 'Could not sign the transaction with address as it does not exist' unless account 

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

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

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

  if tx.nonce == nil
    result = @provider.GetBalance(account.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