Accounts
The web3 accounts package contains functions to generate Ethereum accounts and sign transactions & data.
For using accounts functions, first install Web3 package using npm i web3
or yarn add web3
based on your package manager usage.
After that, Accounts functions will be available as mentioned in following snippet.
import {Web3} from 'web3';
const web3 = new Web3();
const account = web3.eth.accounts.create();
const result = web3.eth.accounts.hashMessage("Test Message");
For using individual package install web3-eth-accounts
package using npm i web3-eth-accounts
or yarn add web3-eth-accounts
and only import required functions.
This is more efficient approach for building lightweight applications.
import {create,hashMessage} from 'web3-eth-accounts';
const account = create();
const result = hashMessage("Test Message");
Functions
create
▸ create(): Web3Account
Generates and returns a Web3Account object that includes the private and public key For creation of private key, it uses an audited package ethereum-cryptography/secp256k1 that is cryptographically secure random number with certain characteristics. Read more: https://www.npmjs.com/package/ethereum-cryptography#secp256k1-curve
Returns
Web3Account
A Web3Account object
web3.eth.accounts.create();
{
address: '0xbD504f977021b5E5DdccD8741A368b147B3B38bB',
privateKey: '0x964ced1c69ad27a311c432fdc0d8211e987595f7eb34ab405a5f16bdc9563ec5',
signTransaction: [Function: signTransaction],
sign: [Function: sign],
encrypt: [AsyncFunction: encrypt]
}
decrypt
▸ decrypt(keystore
, password
, nonStrict?
): Promise
<Web3Account
>
Decrypts a v3 keystore JSON, and creates the account.
Parameters
Name | Type | Description |
---|---|---|
keystore | string | KeyStore | the encrypted Keystore object or string to decrypt |
password | string | Uint8Array | The password that was used for encryption |
nonStrict? | boolean | if true and given a json string, the keystore will be parsed as lowercase. |
Returns
Promise
<Web3Account
>
Returns the decrypted Web3Account object Decrypting scrypt
web3.eth.accounts.decrypt({
version: 3,
id: 'c0cb0a94-4702-4492-b6e6-eb2ac404344a',
address: 'cda9a91875fc35c8ac1320e098e584495d66e47c',
crypto: {
ciphertext: 'cb3e13e3281ff3861a3f0257fad4c9a51b0eb046f9c7821825c46b210f040b8f',
cipherparams: { iv: 'bfb43120ae00e9de110f8325143a2709' },
cipher: 'aes-128-ctr',
kdf: 'scrypt',
kdfparams: {
n: 8192,
r: 8,
p: 1,
dklen: 32,
salt: '210d0ec956787d865358ac45716e6dd42e68d48e346d795746509523aeb477dd'
},
mac: 'efbf6d3409f37c0084a79d5fdf9a6f5d97d11447517ef1ea8374f51e581b7efd'
}
}, '123').then(console.log);
> {
address: '0xcdA9A91875fc35c8Ac1320E098e584495d66e47c',
privateKey: '67f476289210e3bef3c1c75e4de993ff0a00663df00def84e73aa7411eac18a6',
signTransaction: [Function: signTransaction],
sign: [Function: sign],
encrypt: [AsyncFunction: encrypt]
}
encrypt
▸ encrypt(privateKey
, password
, options?
): Promise
<KeyStore
>
encrypt a private key with a password, returns a V3 JSON Keystore
Read more: https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition
Parameters
Name | Type | Description |
---|---|---|
privateKey | Bytes | The private key to encrypt, 32 bytes. |
password | string | Uint8Array | The password used for encryption. |
options? | CipherOptions | Options to configure to encrypt the keystore either scrypt or pbkdf2 |
Returns
Promise
<KeyStore
>
Returns a V3 JSON Keystore
Encrypt using scrypt options:
web3.eth.accounts.encrypt(
'0x67f476289210e3bef3c1c75e4de993ff0a00663df00def84e73aa7411eac18a6',
'123',
{
n: 8192,
iv: web3.utils.hexToBytes('0xbfb43120ae00e9de110f8325143a2709'),
salt: web3.utils.hexToBytes('0x210d0ec956787d865358ac45716e6dd42e68d48e346d795746509523aeb477dd'),
}).then(console.log)
> {
version: 3,
id: 'c0cb0a94-4702-4492-b6e6-eb2ac404344a',
address: 'cda9a91875fc35c8ac1320e098e584495d66e47c',
crypto: {
ciphertext: 'cb3e13e3281ff3861a3f0257fad4c9a51b0eb046f9c7821825c46b210f040b8f',
cipherparams: { iv: 'bfb43120ae00e9de110f8325143a2709' },
cipher: 'aes-128-ctr',
kdf: 'scrypt',
kdfparams: {
n: 8192,
r: 8,
p: 1,
dklen: 32,
salt: '210d0ec956787d865358ac45716e6dd42e68d48e346d795746509523aeb477dd'
},
mac: 'efbf6d3409f37c0084a79d5fdf9a6f5d97d11447517ef1ea8374f51e581b7efd'
}
}
Encrypting using pbkdf2 options:
web3.eth.accounts.encrypt('0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709',
'123',
{
iv: 'bfb43120ae00e9de110f8325143a2709',
salt: '210d0ec956787d865358ac45716e6dd42e68d48e346d795746509523aeb477dd',
c: 262144,
kdf: 'pbkdf2',
}).then(console.log)
>
{
version: 3,
id: '77381417-0973-4e4b-b590-8eb3ace0fe2d',
address: 'b8ce9ab6943e0eced004cde8e3bbed6568b2fa01',
crypto: {
ciphertext: '76512156a34105fa6473ad040c666ae7b917d14c06543accc0d2dc28e6073b12',
cipherparams: { iv: 'bfb43120ae00e9de110f8325143a2709' },
cipher: 'aes-128-ctr',
kdf: 'pbkdf2',
kdfparams: {
dklen: 32,
salt: '210d0ec956787d865358ac45716e6dd42e68d48e346d795746509523aeb477dd',
c: 262144,
prf: 'hmac-sha256'
},
mac: '46eb4884e82dc43b5aa415faba53cc653b7038e9d61cc32fd643cf8c396189b7'
}
}
hashMessage
▸ hashMessage(message
): string
Hashes the given message. The data will be UTF-8 HEX
decoded and enveloped as follows:
"\\x19Ethereum Signed Message:\\n" + message.length + message
and hashed using keccak256.
Parameters
Name | Type | Description |
---|---|---|
message | string | A message to hash, if its HEX it will be UTF8 decoded. |
Returns
string
The hashed message
web3.eth.accounts.hashMessage("Hello world")
> "0x8144a6fa26be252b86456491fbcd43c1de7e022241845ffea1c3df066f7cfede"
web3.eth.accounts.hashMessage(web3.utils.utf8ToHex("Hello world")) // Will be hex decoded in hashMessage
> "0x8144a6fa26be252b86456491fbcd43c1de7e022241845ffea1c3df066f7cfede"
parseAndValidatePrivateKey
▸ parseAndValidatePrivateKey(data
, ignoreLength?
): Uint8Array
Get the private key Uint8Array after the validation. Note: This function is not exported through main web3 package, so for using it directly import from accounts package.
Parameters
Name | Type | Description |
---|---|---|
data | Bytes | Private key |
ignoreLength? | boolean | Optional, ignore length check during validation |
Returns
Uint8Array
The Uint8Array private key
parseAndValidatePrivateKey("0x08c673022000ece7964ea4db2d9369c50442b2869cbd8fc21baaca59e18f642c")
> Uint8Array(32) [
186, 26, 143, 168, 235, 179, 90, 75,
101, 63, 84, 221, 152, 150, 30, 203,
8, 113, 94, 226, 53, 213, 216, 5,
194, 159, 17, 53, 219, 97, 121, 248
]
privateKeyToAccount
▸ privateKeyToAccount(privateKey
, ignoreLength?
): Web3Account
Get an Account object from the privateKey
Parameters
Name | Type | Description |
---|---|---|
privateKey | Bytes | String or Uint8Array of 32 bytes |
ignoreLength? | boolean | if true, will not error check length |
Returns
Web3Account
A Web3Account object
The Web3Account.signTransaction
is not stateful if directly imported from accounts package and used. Network access is required to get the account nonce
and chainId
to sign the transaction, so use Web3.eth.accounts.signTransaction for signing transactions.
web3.eth.accounts.privateKeyToAccount("0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709");
> {
address: '0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01',
privateKey: '0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709',
sign,
signTransaction,
encrypt,
}
privateKeyToAddress
▸ privateKeyToAddress(privateKey
): string
Get the ethereum Address from a private key