AI Network
Search…
ain.wallet

wallet

1
ain.wallet
Copied!
The AIN Wallet that can hold multiple accounts. An Account object consists of an address, a private_key, and a public_key.

Example

1
ain.wallet;
2
> Wallet {
3
accounts: {
4
"0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01": {
5
address: "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01",
6
private_key: "...",
7
public_key: "..."
8
},
9
"0xF2CD2AA0c7926743B1D4310b2BC984a0a453c3d4": {
10
address: "0xF2CD2AA0c7926743B1D4310b2BC984a0a453c3d4",
11
private_key: "...",
12
public_key: "..."
13
}
14
},
15
create: function(){},
16
add: function(){},
17
addFromHDWallet: function(){},
18
setDefaultAccount: function(){},
19
removeDefaultAccount: function(){},
20
remove: function(){},
21
clear: function(){},
22
toV3Keystore: function(){},
23
fromV3Keystore: function(){},
24
sign: function(){},
25
signTransaction: function(){},
26
recover: function(){},
27
defaultAccount: "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01",
28
length: 2,
29
}
Copied!

create

1
ain.wallet.create(numberOfAccounts)
Copied!

Parameters

Name
Type
Description
numberOfAccounts
Number
The number of accounts to create.

Returns

Newly created Account object(s).

Example

1
ain.wallet.create(3)
2
.then(accounts => {
3
// can access new accounts with accounts[i]
4
})
Copied!

add

1
ain.wallet.add(account)
Copied!

Parameters

Name
Type
Description
account
String | Object
A private key or an account object.

Returns

Name
Type
Description
address
String
The address of the added account.

Example

1
ain.wallet.add("0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709");
Copied!

addFromHDWallet

1
ain.wallet.addFromHDWallet(seedPhrase, index = 0)
Copied!

Parameters

Name
Type
Description
seedPhrase
String
The seed phrase (12 or 24 words) of the wallet.
index
number
The index of the account you're trying to add.

Returns

Name
Type
Description
address
String
The address of the added account.

Example

1
ain.wallet.add("apple banana carrot ...", 1);
Copied!

remove

1
ain.wallet.remove(account)
Copied!

Parameters

Name
Type
Description
account
String | Object
A public key or an account object.

Example

1
ain.wallet.remove("0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01");
Copied!

clear

Removes accounts added to the wallet and reset defaultAccount to null.
1
ain.wallet.clear()
Copied!

Example

1
ain.wallet.clear();
Copied!

defaultAccount

1
ain.wallet.defaultAccount
Copied!

Returns

Name
Type
Description
account
String
The public key of the default account. Default value is undefined.

Example

1
console.log(ain.wallet.defaultAccount);
2
3
> "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01"
Copied!

setDefaultAccount

1
ain.wallet.setDefaultAccount(address)
Copied!

Parameters

Name
Type
Description
address
String
The address of the account to set as the defaultAccount. The account should already exist in ain.wallet.

Example

1
ain.wallet.setDefaultAccount("0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01");
Copied!

removeDefaultAccount

1
ain.wallet.removeDefaultAccount()
Copied!

Example

1
ain.wallet.removeDefaultAccount();
Copied!

getBalance

1
ain.wallet.getBalance([key])
Copied!

Parameters

Name
Type
Description
key
String
[Optional, defaults to ain.wallet.defaultAccount] A private key to sign the data with, or an address if the account is loaded to the wallet.

Returns

Promise returns a number, which is the AIN balance of the address.

Example

1
const balance = await ain.wallet.getBalance("0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01");
Copied!

sign

1
ain.wallet.sign(data[, key])
Copied!

Parameters

Name
Type
Description
data
String
The data to sign.
key
String
[Optional, defaults to ain.wallet.defaultAccount] A private key to sign the data with, or an address if the account is loaded to the wallet.

Returns

Name
Type
Description
signature
String
The signature of the data.

Example

1
const signature = ain.wallet.sign("This is an important message.", "0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709");
Copied!

signTransaction

1
ain.wallet.signTransaction(transaction)
Copied!

Parameters

transaction is an Object with following properties:
Name
Type
Description
nonce
Number
Nonce of the transaction sender or the timestamp at which the transaction was created. It can be -1 if the transaction is loosely ordered.
timestamp
Number
The unix time at which the transaction was created.
operation
Operation | UpdateOperation
The operation of the transaction. It it's a singular operation, it will have type, ref, value and optionally rule_type fields. If it's a multi-operation, it will have type field set to "SET", and op_list field with its value as a list of singular operations.
parent_tx_hash
String
Hash of parent transaction for nested transaction. nullwhen it doesn't have a parent transaction.
Optional parameter:
Name
Type
Description
key
String
[Optional, defaults to ain.wallet.defaultAccount] A private key to sign the transaction with, or an address if the account is loaded to the wallet.

Returns

The signature of the transaction.

Example

1
const signature = ain.wallet.signTransaction(
2
{
3
nonce: 11,
4
timestamp: 1569895376000,
5
operation: {type: "SET_VALUE", ref: "afan/user/user123/balance", value: 100}
6
},
7
"0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01"
8
);
Copied!

recover

1
ain.wallet.recover(signature)
Copied!

Parameters

Name
Type
Description
signature
String
The signature.

Returns

Name
Type
Description
address
String
The address used to sign.

Example

1
const addr = ain.wallet.recover("0xSIGNATURE");
Copied!

transfer

1
ain.wallet.transfer({to, value[, from, nonce]})
Copied!

Parameters

Name
Type
Description
to
String
The address of the recipient of the transferred AIN.
value
Number
The amount of AIN that will be transferred.
from
String
[Optional] The address of the sender of AIN. The from account should be added to wallet in order to sign the transfer transaction. If the argument is not specified and the defaultAccount is set, the defaultAccount will be used.
nonce
Number
[Optional, defaults to current_nonce + 1] Nonce of the transaction sender or the timestamp at which the transaction was created. It can be -1 if the transaction is not strictly ordered.

Returns

Promise returns an object with 'result' and 'txHash' fields, or 'code' and 'error_message'. A result is true if the transaction was executed properly. txHash is the string hash of the transaction sent.

Example

1
const addr = ain.wallet.transfer("0xabcd...1234", 1000, "0xefgh...5678");
Copied!
Last modified 2yr ago