This repository has been archived by the owner on Apr 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #166 from ethereumjs/custom-network-doc
Add docs in the README and examples on how to customize the network/hardfork
- Loading branch information
Showing
5 changed files
with
82 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Transaction } from '../src' | ||
import Common from 'ethereumjs-common' | ||
import { bufferToHex, privateToAddress } from 'ethereumjs-util' | ||
|
||
// In this example we create a transaction for a custom network. | ||
// | ||
// All of these network's params are the same than mainnets', except for name, chainId, and | ||
// networkId, so we use the Common.forCustomChain method. | ||
const customCommon = Common.forCustomChain( | ||
'mainnet', | ||
{ | ||
name: 'my-network', | ||
networkId: 123, | ||
chainId: 2134, | ||
}, | ||
'petersburg', | ||
) | ||
|
||
// We pass our custom Common object whenever we create a transaction | ||
|
||
const tx = new Transaction( | ||
{ | ||
nonce: 0, | ||
gasPrice: 100, | ||
gasLimit: 1000000000, | ||
value: 100000, | ||
}, | ||
{ common: customCommon }, | ||
) | ||
|
||
// 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 = new Buffer( | ||
'e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', | ||
'hex', | ||
) | ||
|
||
tx.sign(privateKey) | ||
|
||
if ( | ||
tx.validate() && | ||
bufferToHex(tx.getSenderAddress()) === bufferToHex(privateToAddress(privateKey)) | ||
) { | ||
console.log('Valid signature') | ||
} else { | ||
console.log('Invalid signature') | ||
} | ||
|
||
console.log("The transaction's chain id is", tx.getChainId()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Transaction } from '../src' | ||
import { bufferToHex } from 'ethereumjs-util' | ||
|
||
const tx = new Transaction( | ||
'0xf9010b82930284d09dc30083419ce0942d18de92e0f9aee1a29770c3b15c6cf8ac5498e580b8a42f43f4fb0000000000000000000000000000000000000000000000000000016b78998da900000000000000000000000000000000000000000000000000000000000cb1b70000000000000000000000000000000000000000000000000000000000000fa00000000000000000000000000000000000000000000000000000000001363e4f00000000000000000000000000000000000000000000000000000000000186a029a0fac36e66d329af0e831b2e61179b3ec8d7c7a8a2179e303cfed3364aff2bc3e4a07cb73d56e561ccbd838818dd3dea5fa0b5158577ffc61c0e6ec1f0ed55716891', | ||
{ chain: 'ropsten', hardfork: 'petersburg' }, | ||
) | ||
|
||
if ( | ||
tx.validate() && | ||
bufferToHex(tx.getSenderAddress()) === '0x9dfd2d2b2ed960923f7bf2e8883d73f213f3b24b' | ||
) { | ||
console.log('Correctly created the tx') | ||
} else { | ||
console.error('Invalid tx') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ | |
"author": "mjbecze <[email protected]>", | ||
"license": "MPL-2.0", | ||
"dependencies": { | ||
"ethereumjs-common": "^1.0.0", | ||
"ethereumjs-common": "^1.3.0", | ||
"ethereumjs-util": "^6.0.0" | ||
}, | ||
"devDependencies": { | ||
|