Meter Docs
Search…
meterify.eth.accounts
The meterify.eth.accounts contains functions to generate Meter accounts and sign transactions and data.
Note: Take precautions to clear memory properly, store the private keys safely, and test transaction receiving and sending functionality properly before using in production!
1
import {Accounts} from 'web3-eth-accounts';
2
3
// Passing in the eth or web3 package is necessary to allow retrieving chainId, gasPrice and nonce automatically
4
// for accounts.signTransaction().
5
const accounts = new Accounts('ws://localhost:8546', null, options);
Copied!

create

1
meterify.eth.accounts.create([entropy]);
Copied!
Generates an account object with private key and public key.
Examples:
1
meterify.eth.accounts.create();
2
> {
3
address: "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01",
4
privateKey: "0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709",
5
signTransaction: function(tx){...},
6
sign: function(data){...},
7
encrypt: function(password){...}
8
}
9
10
meterify.eth.accounts.create('[email protected]#@#@±±±±!!!!678543213456764321§34567543213456785432134567');
11
> {
12
address: "0xF2CD2AA0c7926743B1D4310b2BC984a0a453c3d4",
13
privateKey: "0xd7325de5c2c1cf0009fac77d3d04a9c004b038883446b065871bc3e831dcd098",
14
signTransaction: function(tx){...},
15
sign: function(data){...},
16
encrypt: function(password){...}
17
}
18
19
meterify.eth.accounts.create(web3.utils.randomHex(32));
20
> {
21
address: "0xe78150FaCD36E8EB00291e251424a0515AA1FF05",
22
privateKey: "0xcc505ee6067fba3f6fc2050643379e190e087aeffe5d958ab9f2f3ed3800fa4e",
23
signTransaction: function(tx){...},
24
sign: function(data){...},
25
encrypt: function(password){...}
26
}
Copied!
Parameters:
    1.
    entropy - String(optional): A random string to increase entropy. If given it should be at least 32 characters. If none is given a random string will be generated using randomhex.
Returns:
Object - The account object with the following structure:
    1.
    address - String: The account address.
    2.
    privateKey - String: The accounts private key. This should never be shared or stored unencrypted in local storage! Also make sure to null the memory after usage.
    3.
    signTransaction(tx [, callback]) - Function: The function to sign transactions. See meterify.eth.accounts.signTransaction() for more.
    4.
    sign(data) - Function: The function to sign transactions. See meterify.eth.accounts.sign() for more.

privateKeyToAccount

1
meterify.eth.accounts.privateKeyToAccount(privateKey);
Copied!
Creates an account object from a private key.

Parameters

1
2
meterify.eth.accounts.privateKeyToAccount('0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709');
3
> {
4
address: '0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01',
5
privateKey: '0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709',
6
signTransaction: function(tx){...},
7
sign: function(data){...},
8
encrypt: function(password){...}
9
}
Copied!
Parameters:
    1.
    privateKey - String: The private key hex string beginning with 0x.
Returns:
Object - The account object with the structure seen here.

signTransaction

1
meterify.eth.accounts.signTransaction(tx, privateKey [, callback]);
Copied!
Signs a Meter transaction with a given private key.

Parameters

1
meterify.eth.accounts.signTransaction({
2
to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
3
value: '1000000000',
4
gas: 2000000,
5
gasPrice: '234567897654321',
6
nonce: 0,
7
chainId: 1
8
}, '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318')
9
.then(console.log);
Copied!

Parameters

1
> {
2
messageHash: '0x6893a6ee8df79b0f5d64a180cd1ef35d030f3e296a5361cf04d02ce720d32ec5',
3
r: '0x9ebb6ca057a0535d6186462bc0b465b561c94a295bdb0621fc19208ab149a9c',
4
s: '0x440ffd775ce91a833ab410777204d5341a6f9fa91216a6f3ee2c051fea6a0428',
5
v: '0x25',
6
rawTransaction: '0xf86a8086d55698372431831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008025a009ebb6ca057a0535d6186462bc0b465b561c94a295bdb0621fc19208ab149a9ca0440ffd775ce91a833ab410777204d5341a6f9fa91216a6f3ee2c051fea6a0428',
7
transactionHash: '0xd8f64a42b57be0d565f385378db2f6bf324ce14a594afc05de90436e9ce01f60'
8
}
Copied!

Parameters

    1.
    tx - Object: The transaction's properties object as follows:
      1.
      nonce - String: (optional) The nonce to use when signing this transaction. Default will use meterify.eth.getTransactionCount().
      2.
      chainId - String: (optional) The chain id to use when signing this transaction. Default will use meterify.eth.net.getChainId().
      3.
      to - String: (optional) The receiver of the transaction, can be empty when deploying a contract.
      4.
      data - String: (optional) The call data of the transaction, can be empty for simple value transfers.
      5.
      value - String: (optional) The value of the transaction in wei.
      6.
      gasPrice - String: (optional) The gas price set by this transaction. If empty, it will use meterify.eth.getGasPrice()
      7.
      gas - String: The gas provided by the transaction.
    2.
    privateKey - String: The private key to sign with.
    3.
    callback - Function: (optional) Optional callback, returns an error object as first parameter and the result as second.
Returns
Promise returning Object: The signed data RLP encoded transaction, or if returnSignature is true the signature values as follows:
    1.
    messageHash - String: The hash of the given message.
    2.
    r - String: First 32 bytes of the signature
    3.
    s - String: Next 32 bytes of the signature
    4.
    v - String: Recovery value + 27
    5.
    rawTransaction - String: The RLP encoded transaction, ready to be send using meterify.eth.sendSignedTransaction.
    6.
    transactionHash - String: The transaction hash for the RLP encoded transaction.

recoverTransaction

1
meterify.eth.accounts.recoverTransaction(rawTransaction);
Copied!
Recovers the Meter address which was used to sign the given RLP encoded transaction.
Example:
1
meterify.eth.accounts.recoverTransaction('0xf86180808401ef364594f0109fc8df283027b6285cc889f5aa624eac1f5580801ca031573280d608f75137e33fc14655f097867d691d5c4c44ebe5ae186070ac3d5ea0524410802cdc025034daefcdfa08e7d2ee3f0b9d9ae184b2001fe0aff07603d9');
2
> "0xF0109fC8DF283027b6285cc889F5aA624EaC1F55"
Copied!
Parameters:
    1.
    signature - String: The RLP encoded transaction.
Returns:
String: The Meter address used to sign this transaction.

hashMessage

1
meterify.eth.accounts.hashMessage(message);
Copied!
Hashes the given message to be passed meterify.eth.accounts.recover() function.
The data will be UTF-8 HEX decoded and enveloped as follows: "\x19Ethereum Signed Message:\n" + message.length + message and hashed using keccak256.
Example:
1
2
meterify.eth.accounts.hashMessage("Hello World")
3
> "0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2"
4
5
// the below results in the same hash
6
meterify.eth.accounts.hashMessage(web3.utils.utf8ToHex("Hello World"))
7
> "0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2"
Copied!
Parameters:
    1.
    message - String: A message to hash, if it's HEX it will be UTF8 decoded before.
Returns:
String: The hashed message

sign

1
meterify.eth.accounts.sign(data, privateKey);
Copied!
Signs arbitrary data. This data is before UTF-8 HEX decoded and enveloped as follows: "\x19Ethereum Signed Message:\n" + message.length + message.

Parameters

1
meterify.eth.accounts.sign('Some data', '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318');
2
> {
3
message: 'Some data',
4
messageHash: '0x1da44b586eb0729ff70a73c326926f6ed5a25f5b056e7f47fbc6e58d86871655',
5
v: '0x1c',
6
r: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd',
7
s: '0x6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a029',
8
signature: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a0291c'
9
}
Copied!
Parameters:
    1.
    data - String: The data to sign.
    2.
    privateKey - String: The private key to sign with.
Returns:
Object: The signed data RLP encoded signature, or if returnSignature is true the signature values as follows:
    1.
    message - String: The the given message.
    2.
    messageHash - String: The hash of the given message.
    3.
    r - String: First 32 bytes of the signature.
    4.
    s - String: Next 32 bytes of the signature.
    5.
    v - String: Recovery value + 27.

recover

1
meterify.eth.accounts.recover(signatureObject);
2
meterify.eth.accounts.recover(message, signature [, preFixed]);
3
meterify.eth.accounts.recover(message, v, r, s [, preFixed]);
Copied!
Recovers the Meter address which was used to sign the given data.

Parameters

1
meterify.eth.accounts.recover({
2
messageHash: '0x1da44b586eb0729ff70a73c326926f6ed5a25f5b056e7f47fbc6e58d86871655',
3
v: '0x1c',
4
r: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd',
5
s: '0x6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a029'
6
})
7
> "0x2c7536E3605D9C16a7a3D7b1898e529396a65c23"
8
9
// message, signature
10
meterify.eth.accounts.recover('Some data', '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a0291c');
11
> "0x2c7536E3605D9C16a7a3D7b1898e529396a65c23"
12
13
// message, v, r, s
14
meterify.eth.accounts.recover('Some data', '0x1c', '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd', '0x6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a029');
15
> "0x2c7536E3605D9C16a7a3D7b1898e529396a65c23"
Copied!

Parameters:

    1.
    message or signatureObject - String or Object: Either signed message or hash, or the signature object as following values:
      1.
      messageHash - String: The hash of the given message already prefixed with "\x19Ethereum Signed Message:\n" + message.length + message.
      2.
      r - String: First 32 bytes of the signature.
      3.
      s - String: Next 32 bytes of the signature.
      4.
      v - String: Recovery value + 27
    2.
    signature - String: The raw RLP encoded signature, OR parameter 2-4 as v, r, s values.
    3.
    preFixed - Boolean (optional, default: false): If the last parameter is true, the given message will NOT automatically be prefixed with "\x19Ethereum Signed Message:\n" + message.length + message, and assumed to be already prefixed.
Returns:
String: The Meter address used to sign this data.

encrypt

1
meterify.eth.accounts.encrypt(privateKey, password);
Copied!
Encrypts a private key to the web3 keystore v3 standard.
Example:
1
meterify.eth.accounts.encrypt('0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318', 'test!')
2
> {
3
version: 3,
4
id: '04e9bcbb-96fa-497b-94d1-14df4cd20af6',
5
address: '2c7536e3605d9c16a7a3d7b1898e529396a65c23',
6
crypto: {
7
ciphertext: 'a1c25da3ecde4e6a24f3697251dd15d6208520efc84ad97397e906e6df24d251',
8
cipherparams: { iv: '2885df2b63f7ef247d753c82fa20038a' },
9
cipher: 'aes-128-ctr',
10
kdf: 'scrypt',
11
kdfparams: {
12
dklen: 32,
13
salt: '4531b3c174cc3ff32a6a7a85d6761b410db674807b2d216d022318ceee50be10',
14
n: 262144,
15
r: 8,
16
p: 1
17
},
18
mac: 'b8b010fff37f9ae5559a352a185e86f9b9c1d7f7a9f1bd4e82a5dd35468fc7f6'
19
}
20
}
Copied!
Parameters:
    1.
    privateKey - String: The private key to encrypt.password
    2.
    String: The password used for encryption.
Returns:
Object: The encrypted keystore v3 JSON.

decrypt

1
meterify.eth.accounts.decrypt(keystoreJsonV3, password);
Copied!
Decrypts a keystore v3 JSON, and creates the account.
Example:
1
meterify.eth.accounts.decrypt({
2
version: 3,
3
id: '04e9bcbb-96fa-497b-94d1-14df4cd20af6',
4
address: '2c7536e3605d9c16a7a3d7b1898e529396a65c23',
5
crypto: {
6
ciphertext: 'a1c25da3ecde4e6a24f3697251dd15d6208520efc84ad97397e906e6df24d251',
7
cipherparams: { iv: '2885df2b63f7ef247d753c82fa20038a' },
8
cipher: 'aes-128-ctr',
9
kdf: 'scrypt',
10
kdfparams: {
11
dklen: 32,
12
salt: '4531b3c174cc3ff32a6a7a85d6761b410db674807b2d216d022318ceee50be10',
13
n: 262144,
14
r: 8,
15
p: 1
16
},
17
mac: 'b8b010fff37f9ae5559a352a185e86f9b9c1d7f7a9f1bd4e82a5dd35468fc7f6'
18
}
19
}, 'test!');
20
> {
21
address: "0x2c7536E3605D9C16a7a3D7b1898e529396a65c23",
22
privateKey: "0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318",
23
signTransaction: function(tx){...},
24
sign: function(data){...},
25
encrypt: function(password){...}
26
}
Copied!
Parameters:
    1.
    keystoreJsonV3 - String: The encrypted keystore v3 JSON.
    2.
    password - String: The password used for encryption.
Returns:
Object: The decrypted account.

wallet

1
meterify.eth.accounts.wallet;
Copied!
Contains an in memory wallet with multiple accounts. These accounts can be used when using meterify.eth.sendTransaction().
Example:
1
2
meterify.eth.accounts.wallet;
3
> Wallet {
4
0: {...}, // account by index
5
"0xF0109fC8DF283027b6285cc889F5aA624EaC1F55": {...}, // same account by address
6
"0xf0109fc8df283027b6285cc889f5aa624eac1f55": {...}, // same account by address lowercase
7
1: {...},
8
"0xD0122fC8DF283027b6285cc889F5aA624EaC1d23": {...},
9
"0xd0122fc8df283027b6285cc889f5aa624eac1d23": {...},
10
11
add: function(){},
12
remove: function(){},
13
save: function(){},
14
load: function(){},
15
clear: function(){},
16
17
length: 2,
18
}
Copied!

wallet.create

1
meterify.eth.accounts.wallet.create(numberOfAccounts [, entropy]);
Copied!
Generates one or more accounts in the wallet. If wallets already exist they will not be overridden.
Example:
1
2
meterify.eth.accounts.wallet.create(2, '54674321§3456764321§345674321§3453647544±±±§±±±!!!43534534534534');
3
> Wallet {
4
0: {...},
5
"0xF0109fC8DF283027b6285cc889F5aA624EaC1F55": {...},
6
"0xf0109fc8df283027b6285cc889f5aa624eac1f55": {...},
7
...
8
}
Copied!
Parameters:
    1.
    numberOfAccounts - Number: Number of accounts to create. Leave empty to create an empty wallet.
    2.
    entropy - String(optional): A string with random characters as additional entropy when generating accounts. If given it should be at least 32 characters.
Returns:
Object: The wallet object.

wallet.add

1
meterify.eth.accounts.wallet.add(account);
Copied!
Adds an account using a private key or account object to the wallet.
Example:
1
2
meterify.eth.accounts.wallet.add('0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318');
3
> {
4
index: 0,
5
address: '0x2c7536E3605D9C16a7a3D7b1898e529396a65c23',
6
privateKey: '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318',
7
signTransaction: function(tx){...},
8
sign: function(data){...},
9
encrypt: function(password){...}
10
}
11
12
meterify.eth.accounts.wallet.add({
13
privateKey: '0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709',
14
address: '0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01'
15
});
16
> {
17
index: 0,
18
address: '0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01',
19
privateKey: '0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709',
20
signTransaction: function(tx){...},
21
sign: function(data){...},
22
encrypt: function(password){...}
23
}
Copied!
Parameters:
    1.
    account - String or Object: A private key or account object created with meterify.eth.accounts.create().
Returns:
Object: The added account.

wallet.remove

1
meterify.eth.accounts.wallet.remove(account);
Copied!
Removes an account from the wallet.
Example:
1
2
meterify.eth.accounts.wallet;
3
> Wallet {
4
0: {...},
5
"0xF0109fC8DF283027b6285cc889F5aA624EaC1F55": {...}
6
1: {...},
7
"0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01": {...}
8
...
9
}
10
11
meterify.eth.accounts.wallet.remove('0xF0109fC8DF283027b6285cc889F5aA624EaC1F55');
12
> true
13
14
meterify.eth.accounts.wallet.remove(3);
15
> false
Copied!
Parameters:
    1.
    account - String or Number: The account address, or index in the wallet.
Returns:
Boolean: true if the wallet was removed. false if it couldn't be found.

wallet.clear

1
meterify.eth.accounts.wallet.clear();
Copied!
Securely empties the wallet and removes all its accounts.
Example:
1
2
meterify.eth.accounts.wallet.clear();
3
> Wallet {
4
add: function(){},
5
remove: function(){},
6
save: function(){},
7
load: function(){},
8
clear: function(){},
9
10
length: 0
11
}
Copied!
Parameters:
none
Returns:
Object: The wallet object.

wallet.encrypt

1
meterify.eth.accounts.wallet.encrypt(password);
Copied!
Encrypts all wallet accounts to an array of encrypted keystore v3 objects.
Example:
1
2
meterify.eth.accounts.wallet.encrypt('test');
3
> [ { version: 3,
4
id: 'dcf8ab05-a314-4e37-b972-bf9b86f91372',
5
address: '06f702337909c06c82b09b7a22f0a2f0855d1f68',
6
crypto:
7
{ ciphertext: '0de804dc63940820f6b3334e5a4bfc8214e27fb30bb7e9b7b74b25cd7eb5c604',
8
cipherparams: [Object],
9
cipher: 'aes-128-ctr',
10
kdf: 'scrypt',
11
kdfparams: [Object],
12
mac: 'b2aac1485bd6ee1928665642bf8eae9ddfbc039c3a673658933d320bac6952e3' } },
13
{ version: 3,
14
id: '9e1c7d24-b919-4428-b10e-0f3ef79f7cf0',
15
address: 'b5d89661b59a9af0b34f58d19138baa2de48baaf',
16
crypto:
17
{ ciphertext: 'd705ebed2a136d9e4db7e5ae70ed1f69d6a57370d5fbe06281eb07615f404410',
18
cipherparams: [Object],
19
cipher: 'aes-128-ctr',
20
kdf: 'scrypt',
21
kdfparams: [Object],
22
mac: 'af9eca5eb01b0f70e909f824f0e7cdb90c350a802f04a9f6afe056602b92272b' } }
23
]
Copied!
Parameters:
    1.
    password - String: The password which will be used for encryption.
Returns:
Array: The encrypted keystore v3.

wallet.decrypt

1
meterify.eth.accounts.wallet.decrypt(keystoreArray, password);
Copied!
Decrypts keystore v3 objects.
Example:
1
2
meterify.eth.accounts.wallet.decrypt([
3
{ version: 3,
4
id: '83191a81-aaca-451f-b63d-0c5f3b849289',
5
address: '06f702337909c06c82b09b7a22f0a2f0855d1f68',
6
crypto:
7
{ ciphertext: '7d34deae112841fba86e3e6cf08f5398dda323a8e4d29332621534e2c4069e8d',
8
cipherparams: { iv: '497f4d26997a84d570778eae874b2333' },
9
cipher: 'aes-128-ctr',
10
kdf: 'scrypt',
11
kdfparams:
12
{ dklen: 32,
13
salt: '208dd732a27aa4803bb760228dff18515d5313fd085bbce60594a3919ae2d88d',
14
n: 262144,
15
r: 8,
16
p: 1 },
17
mac: '0062a853de302513c57bfe3108ab493733034bf3cb313326f42cf26ea2619cf9' } },
18
{ version: 3,
19
id: '7d6b91fa-3611-407b-b16b-396efb28f97e',
20
address: 'b5d89661b59a9af0b34f58d19138baa2de48baaf',
21
crypto:
22
{ ciphertext: 'cb9712d1982ff89f571fa5dbef447f14b7e5f142232bd2a913aac833730eeb43',
23
cipherparams: { iv: '8cccb91cb84e435437f7282ec2ffd2db' },
24
cipher: 'aes-128-ctr',
25
kdf: 'scrypt',
26
kdfparams:
27
{ dklen: 32,
28
salt: '08ba6736363c5586434cd5b895e6fe41ea7db4785bd9b901dedce77a1514e8b8',
29
n: 262144,
30
r: 8,
31
p: 1 },
32
mac: 'd2eb068b37e2df55f56fa97a2bf4f55e072bef0dd703bfd917717d9dc54510f0' } }
33
], 'test');
34
> Wallet {
35
0: {...},
36
1: {...},
37
"0xF0109fC8DF283027b6285cc889F5aA624EaC1F55": {...},
38
"0xD0122fC8DF283027b6285cc889F5aA624EaC1d23": {...}
39
...
40
}
Copied!
Parameters:
    1.
    keystoreArray - Array: The encrypted keystore v3 objects to decrypt.
    2.
    password - String: The password which will be used for encryption.
Returns:
Object: The wallet object.

wallet.save

1
meterify.eth.accounts.wallet.save(password [, keyName]);
Copied!
Stores the wallet encrypted and as string in local storage.
Example:
1
2
meterify.eth.accounts.wallet.save('test#!#x27;);
3
> true
Copied!
Note: Browser only.
Parameters:
    1.
    password - String: The password to encrypt the wallet.
    2.
    keyName - String: (optional) The key used for the local storage position, defaults to "web3js_wallet".
Returns:
Boolean

wallet.load

1
meterify.eth.accounts.wallet.load(password [, keyName]);
Copied!
Loads a wallet from local storage and decrypts it.
Example:
1
2
meterify.eth.accounts.wallet.load('test#!#x27;, 'myWalletKey');
3
> Wallet {
4
0: {...},
5
1: {...},
6
"0xF0109fC8DF283027b6285cc889F5aA624EaC1F55": {...},
7
"0xD0122fC8DF283027b6285cc889F5aA624EaC1d23": {...}
8
...
9
}
Copied!
Note: Browser only.
Parameters:
    1.
    password - String: The password to decrypt the wallet.
    2.
    keyName - String: (optional) The key used for the local storage position, defaults to "web3js_wallet".
Returns:
Object: The wallet object.
Last modified 1yr ago