Class: Zilliqa::Account::Wallet

Inherits:
Object
  • Object
show all
Defined in:
lib/zilliqa/account/wallet.rb

Instance Method Summary collapse

Constructor Details

#initialize(provider = nil, accounts = {}) ⇒ Wallet

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



8
9
10
11
12
13
14
15
16
# 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

Instance Method Details

#add_by_keystore(keystore, passphrase) ⇒ Object

Adds an account by keystore



44
45
46
47
48
49
50
51
52
# File 'lib/zilliqa/account/wallet.rb', line 44

def add_by_keystore(keystore, passphrase)
   = Zilliqa::Account::Account.from_file(keystore, passphrase)

  @accounts[.address] = 

  @default_account =  unless @default_account

  .address
end

#add_by_private_key(private_key) ⇒ Object

Adds an account to the wallet by private key.



32
33
34
35
36
37
38
39
40
# File 'lib/zilliqa/account/wallet.rb', line 32

def add_by_private_key(private_key)
   = Zilliqa::Account::Account.new(private_key)

  @accounts[.address] = 

  @default_account =  unless @default_account

  .address
end

#createObject

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



20
21
22
23
24
25
26
27
28
29
# File 'lib/zilliqa/account/wallet.rb', line 20

def create
  private_key = Zilliqa::Crypto::KeyTool.generate_private_key
   = Zilliqa::Account::Account.new(private_key)

  @accounts[.address] = 

  @default_account =  unless @default_account

  .address
end

#remove(address) ⇒ Object

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



57
58
59
60
61
62
63
64
65
# 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) ⇒ Object

Sets the default account of the wallet.



68
69
70
# File 'lib/zilliqa/account/wallet.rb', line 68

def set_default(address)
  @default_account = @accounts[address]
end

#sign(tx) ⇒ Object

signs an unsigned transaction with the default account.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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)
     = @accounts[address]
    raise 'Could not sign the transaction with address as it does not exist' unless 

    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) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/zilliqa/account/wallet.rb', line 103

def sign_with(tx, address)
   = @accounts[address]
  address = .address

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

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

  tx.sender_pub_key = .public_key
  sig = .sign_transaction(tx)
  tx.signature = sig.to_s
  tx
end

#transfer(to_addr, amount) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# 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