Skip to content
This repository was archived by the owner on Apr 6, 2020. It is now read-only.

Commit 343ea82

Browse files
authored
Merge pull request #166 from ethereumjs/custom-network-doc
Add docs in the README and examples on how to customize the network/hardfork
2 parents f0234ab + 4f3d366 commit 343ea82

File tree

5 files changed

+82
-8
lines changed

5 files changed

+82
-8
lines changed

README.md

+13-6
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,26 @@ const txParams = {
2929
data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057',
3030
}
3131

32-
const tx = new EthereumTx(txParams)
32+
// The second parameter is not necessary if these values are used
33+
const tx = new EthereumTx(txParams, { chain: 'mainnet', hardfork: 'petersburg' })
3334
tx.sign(privateKey)
3435
const serializedTx = tx.serialize()
3536
```
3637

3738
# Chain and Hardfork Support
3839

39-
This library uses the [ethereumjs-common](https://github.com/ethereumjs/ethereumjs-common)
40-
package to support different chain and hardfork options, see API documentation
41-
for details.
40+
The `Transaction` and `FakeTransaction` constructors receives a second parameter that lets you specify the chain and hardfork
41+
to be used. By default, `mainnet` and `petersburg` will be used.
4242

43-
Currently all hardforks up to `petersburg` are supported, `EIP-155` replay protection
44-
is activated since the `spuriousDragon` hardfork.
43+
There are two ways of customizing these. The first one, as shown in the previous section, is by
44+
using an object with `chain` and `hardfork` names. You can see en example of this in [./examples/ropsten-tx.ts](./examples/ropsten-tx.ts).
45+
46+
The second option is by passing the option `common` set to an instance of [ethereumjs-common](https://github.com/ethereumjs/ethereumjs-common)' Common. This is specially useful for custom networks or chains/hardforks not yet supported by `ethereumjs-common`. You can see en example of this in [./examples/custom-chain-tx.ts](./examples/custom-chain-tx.ts).
47+
48+
# EIP-155 support
49+
50+
`EIP-155` replay protection is activated since the `spuriousDragon` hardfork. To disable it, set the
51+
hardfork in the `Transaction`'s constructor.
4552

4653
# API
4754

examples/custom-chain-tx.ts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Transaction } from '../src'
2+
import Common from 'ethereumjs-common'
3+
import { bufferToHex, privateToAddress } from 'ethereumjs-util'
4+
5+
// In this example we create a transaction for a custom network.
6+
//
7+
// All of these network's params are the same than mainnets', except for name, chainId, and
8+
// networkId, so we use the Common.forCustomChain method.
9+
const customCommon = Common.forCustomChain(
10+
'mainnet',
11+
{
12+
name: 'my-network',
13+
networkId: 123,
14+
chainId: 2134,
15+
},
16+
'petersburg',
17+
)
18+
19+
// We pass our custom Common object whenever we create a transaction
20+
21+
const tx = new Transaction(
22+
{
23+
nonce: 0,
24+
gasPrice: 100,
25+
gasLimit: 1000000000,
26+
value: 100000,
27+
},
28+
{ common: customCommon },
29+
)
30+
31+
// Once we created the transaction using the custom Common object, we can use it as a normal tx.
32+
33+
// Here we sign it and validate its signature
34+
const privateKey = new Buffer(
35+
'e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109',
36+
'hex',
37+
)
38+
39+
tx.sign(privateKey)
40+
41+
if (
42+
tx.validate() &&
43+
bufferToHex(tx.getSenderAddress()) === bufferToHex(privateToAddress(privateKey))
44+
) {
45+
console.log('Valid signature')
46+
} else {
47+
console.log('Invalid signature')
48+
}
49+
50+
console.log("The transaction's chain id is", tx.getChainId())

examples/ropsten-tx.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Transaction } from '../src'
2+
import { bufferToHex } from 'ethereumjs-util'
3+
4+
const tx = new Transaction(
5+
'0xf9010b82930284d09dc30083419ce0942d18de92e0f9aee1a29770c3b15c6cf8ac5498e580b8a42f43f4fb0000000000000000000000000000000000000000000000000000016b78998da900000000000000000000000000000000000000000000000000000000000cb1b70000000000000000000000000000000000000000000000000000000000000fa00000000000000000000000000000000000000000000000000000000001363e4f00000000000000000000000000000000000000000000000000000000000186a029a0fac36e66d329af0e831b2e61179b3ec8d7c7a8a2179e303cfed3364aff2bc3e4a07cb73d56e561ccbd838818dd3dea5fa0b5158577ffc61c0e6ec1f0ed55716891',
6+
{ chain: 'ropsten', hardfork: 'petersburg' },
7+
)
8+
9+
if (
10+
tx.validate() &&
11+
bufferToHex(tx.getSenderAddress()) === '0x9dfd2d2b2ed960923f7bf2e8883d73f213f3b24b'
12+
) {
13+
console.log('Correctly created the tx')
14+
} else {
15+
console.error('Invalid tx')
16+
}

examples/transactions.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Transaction } from '../src'
55

66
// We create an unsigned transaction.
77
// Notice we don't set the `to` field because we are creating a new contract.
8+
// This transaction's chain is set to mainnet
89
const tx = new Transaction({
910
nonce: 0,
1011
gasPrice: 100,
@@ -49,7 +50,7 @@ const rawTx = [
4950
'0x5bd428537f05f9830e93792f90ea6a3e2d1ee84952dd96edbae9f658f831ab13',
5051
]
5152

52-
const tx2 = new Transaction(rawTx)
53+
const tx2 = new Transaction(rawTx) // This is also a maninnet transaction
5354

5455
// Note rlp.decode will actually produce an array of buffers `new Transaction` will
5556
// take either an array of buffers or an array of hex strings.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"author": "mjbecze <mb@ethdev.com>",
3838
"license": "MPL-2.0",
3939
"dependencies": {
40-
"ethereumjs-common": "^1.0.0",
40+
"ethereumjs-common": "^1.3.0",
4141
"ethereumjs-util": "^6.0.0"
4242
},
4343
"devDependencies": {

0 commit comments

Comments
 (0)