What is ethereumjs-tx?
The ethereumjs-tx package is a JavaScript library for creating, signing, and serializing Ethereum transactions. It is part of the ethereumjs suite of tools and is widely used for interacting with the Ethereum blockchain at a low level.
What are ethereumjs-tx's main functionalities?
Creating a Transaction
This feature allows you to create a new Ethereum transaction by specifying parameters such as nonce, gas price, gas limit, recipient address, value, and data.
const Tx = require('ethereumjs-tx').Transaction;
const txParams = {
nonce: '0x00',
gasPrice: '0x09184e72a000',
gasLimit: '0x2710',
to: '0x0000000000000000000000000000000000000000',
value: '0x00',
data: '0x00',
};
const tx = new Tx(txParams);
Signing a Transaction
This feature allows you to sign a transaction using a private key. The transaction must be signed before it can be sent to the Ethereum network.
const privateKey = Buffer.from('your_private_key', 'hex');
tx.sign(privateKey);
Serializing a Transaction
This feature allows you to serialize a signed transaction into a format that can be sent over the network. The serialized transaction is typically represented as a hexadecimal string.
const serializedTx = tx.serialize();
console.log(serializedTx.toString('hex'));
Other packages similar to ethereumjs-tx
web3
Web3.js is a comprehensive library for interacting with the Ethereum blockchain. It provides higher-level abstractions for sending transactions, interacting with smart contracts, and querying blockchain data. Unlike ethereumjs-tx, which focuses on low-level transaction creation and signing, Web3.js offers a broader range of functionalities.
ethers
Ethers.js is a lightweight library for interacting with the Ethereum blockchain. It provides utilities for creating and signing transactions, interacting with smart contracts, and querying blockchain data. Ethers.js is known for its simplicity and ease of use, making it a popular alternative to ethereumjs-tx for developers who prefer a more streamlined API.
eth-lib
Eth-lib is a low-level library for Ethereum that provides utilities for creating and signing transactions, encoding and decoding data, and working with cryptographic primitives. It is similar to ethereumjs-tx in that it focuses on low-level operations, but it also includes additional utilities for working with Ethereum data structures.
SYNOPSIS
or #ethereumjs on freenode
![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)
INSTALL
npm install ethereumjs-tx
USAGE
const EthereumTx = require('ethereumjs-tx')
const privateKey = Buffer.from('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex')
const txParams = {
nonce: '0x00',
gasPrice: '0x09184e72a000',
gasLimit: '0x2710',
to: '0x0000000000000000000000000000000000000000',
value: '0x00',
data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057',
chainId: 3
}
const tx = new EthereumTx(txParams)
tx.sign(privateKey)
const serializedTx = tx.serialize()
Note: this package expects ECMAScript 6 (ES6) as a minimum environment. From browsers lacking ES6 support, please use a shim (like es6-shim) before including any of the builds from this repo.
BROWSER
For a browser build please see https://github.com/ethereumjs/browser-builds.
API
./docs/
LICENSE
MPL-2.0