forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom-chain-tx.ts
49 lines (41 loc) · 1.35 KB
/
custom-chain-tx.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { Address } from '@ethereumjs/util'
import { Common } from '@ethereumjs/common'
import { LegacyTransaction } from '../dist/cjs/index'
import { hexToBytes } from '@ethereumjs/util'
// In this example we create a transaction for a custom network.
// This custom network has the same params as mainnet,
// except for name, chainId, and networkId,
// so we use the `Common.custom` method.
const customCommon = Common.custom(
{
name: 'my-network',
networkId: 123,
chainId: 2134,
},
{
baseChain: 'mainnet',
hardfork: 'petersburg',
}
)
// We pass our custom Common object whenever we create a transaction
const opts = { common: customCommon }
const tx = LegacyTransaction.fromTxData(
{
nonce: 0,
gasPrice: 100,
gasLimit: 1000000000,
value: 100000,
},
opts
)
// Once we created the transaction using the custom Common object, we can use it as a normal tx.
// Here we sign it and validate its signature
const privateKey = hexToBytes('0xe331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109')
const signedTx = tx.sign(privateKey)
const address = Address.fromPrivateKey(privateKey)
if (signedTx.isValid() && signedTx.getSenderAddress().equals(address)) {
console.log('Valid signature')
} else {
console.log('Invalid signature')
}
console.log("The transaction's chain id is: ", signedTx.common.chainId().toString())