What is ethereumjs-account?
The ethereumjs-account package is a JavaScript library for managing Ethereum accounts. It provides functionalities for creating, manipulating, and serializing Ethereum accounts, making it easier to handle account-related operations in Ethereum-based applications.
What are ethereumjs-account's main functionalities?
Creating a new account
This feature allows you to create a new Ethereum account. The code sample demonstrates how to create a new account instance using the Account class from the ethereumjs-account package.
const { Account } = require('ethereumjs-account');
const newAccount = new Account();
console.log(newAccount);
Serializing an account
This feature allows you to serialize an Ethereum account into a buffer. The code sample shows how to serialize an account and convert it to a hexadecimal string.
const { Account } = require('ethereumjs-account');
const account = new Account();
const serialized = account.serialize();
console.log(serialized.toString('hex'));
Deserializing an account
This feature allows you to deserialize a buffer back into an Ethereum account. The code sample demonstrates how to deserialize a hexadecimal string buffer into an account instance.
const { Account } = require('ethereumjs-account');
const serialized = Buffer.from('...', 'hex');
const account = Account.deserialize(serialized);
console.log(account);
Updating account balance
This feature allows you to update the balance of an Ethereum account. The code sample shows how to set the balance of an account to 2 ETH (in wei).
const { Account } = require('ethereumjs-account');
const account = new Account();
account.balance = '0x1bc16d674ec80000'; // 2 ETH in wei
console.log(account.balance.toString('hex'));
Other packages similar to ethereumjs-account
web3
The web3 package is a comprehensive library for interacting with the Ethereum blockchain. It includes functionalities for managing accounts, sending transactions, and interacting with smart contracts. Compared to ethereumjs-account, web3 offers a broader range of features but may be more complex to use for simple account management tasks.
ethers
The ethers package is another popular library for interacting with the Ethereum blockchain. It provides utilities for managing wallets, sending transactions, and interacting with smart contracts. Ethers is known for its simplicity and ease of use, making it a good alternative to ethereumjs-account for developers looking for a more user-friendly experience.
eth-lib
The eth-lib package is a lightweight library for Ethereum-related operations, including account management, transaction creation, and signing. It offers a more minimalistic approach compared to ethereumjs-account, making it suitable for developers who need basic functionalities without the overhead of a larger library.
SYNOPSIS
or #ethereumjs on freenode
or #ethereumjs on freenode
Implements schema and functions relating to accounts stored ethereum's state Trie
NOTE: this is different from ethereumjs-accounts which should be used if you want to key management and web3 sugar.
INSTALL
npm install ethereumjs-account
BROWSER
This module work with browserify
API
new Account([data])
Creates a new account object
data
- an account can be initialized with either a buffer
containing the RLP serialized account.
Or an Array
of buffers relating to each of the account Properties, listed in order below. For example:
var raw = [
'0x02',
'0x0384',
'0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
'0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
];
var account = new Account(raw);
Or lastly an Object
containing the Properties of the account:
var raw = {
nonce: '',
balance: '0x03e7',
stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
codeHash: '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
};
var account = new Account(raw);
For Object
and Array
each of the elements can either be a Buffer
, hex String
, Number
, or an object with a toBuffer
method such as Bignum
.
Account
Properties
nonce
- The account's nonce.balance
- The account's balance in wei.stateRoot
- The stateRoot for the storage of the contract.codeHash
- The hash of the code of the contract.
Account
Methods
account.isEmpty()
Returns a Boolean
determining if the account is empty.
account.isContract()
Returns a Boolean
deteremining if the account is a contract.
account.serialize()
Returns the RLP serialization of the account as a Buffer
.
account.toJSON([object])
Returns the account as JSON.
object
- A Boolean
that defaults to false. If object
is true then this will return an Object
, else it will return an Array
.
account.getCode(trie, cb)
Fetches the code from the trie.
trie
- The trie storing the accounts.cb
- The callback.
account.setCode(trie, code, cb)
Stores the code in the trie.
trie
- The trie storing the accounts.code
- A Buffer
.cb
- The callback.
account.getStorage(trie, key, cb)
Fetches key
from the account's storage.
account.setStorage(trie, key, val, cb)
Stores a val
at the key
in the contract's storage.