Class: Laksa::Account::Wallet
- Inherits:
-
Object
- Object
- Laksa::Account::Wallet
- Defined in:
- lib/laksa/account/wallet.rb
Class Method Summary collapse
-
.to_checksum_address(address) ⇒ Object
to_checksum_address.
Instance Method Summary collapse
-
#add_by_keystore(keystore, passphrase) ⇒ Object
Adds an account by keystore.
-
#add_by_private_key(private_key) ⇒ Object
Adds an account to the wallet by private key.
-
#create ⇒ Object
Creates a new keypair with a randomly-generated private key.
-
#initialize(provider = nil, accounts = {}) ⇒ Wallet
constructor
Takes an array of Account objects and instantiates a Wallet instance.
-
#remove(address) ⇒ Object
Removes an account from the wallet and returns boolean to indicate failure or success.
-
#set_default(address) ⇒ Object
Sets the default account of the wallet.
-
#sign(tx) ⇒ Object
signs an unsigned transaction with the default account.
- #sign_with(tx, address) ⇒ Object
Constructor Details
#initialize(provider = nil, accounts = {}) ⇒ Wallet
Takes an array of Account objects and instantiates a Wallet instance.
7 8 9 10 11 12 13 14 15 |
# 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 |
Class Method Details
.to_checksum_address(address) ⇒ Object
to_checksum_address
takes hex-encoded string and returns the corresponding address
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# 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 |
Instance Method Details
#add_by_keystore(keystore, passphrase) ⇒ Object
Adds an account by keystore
43 44 45 46 47 48 49 50 51 |
# 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) ⇒ Object
Adds an account to the wallet by private key.
31 32 33 34 35 36 37 38 39 |
# 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 ⇒ Object
Creates a new keypair with a randomly-generated private key. The new account is accessible by address.
19 20 21 22 23 24 25 26 27 28 |
# 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) ⇒ Object
Removes an account from the wallet and returns boolean to indicate failure or success.
56 57 58 59 60 61 62 63 64 |
# 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) ⇒ Object
Sets the default account of the wallet.
67 68 69 |
# File 'lib/laksa/account/wallet.rb', line 67 def set_default(address) @default_account = @accounts[address] end |
#sign(tx) ⇒ Object
signs an unsigned transaction with the default account.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# 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) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# 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 |