-
Notifications
You must be signed in to change notification settings - Fork 775
/
Copy pathgenesis.ts
60 lines (55 loc) · 1.98 KB
/
genesis.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
50
51
52
53
54
55
56
57
58
59
60
import { addHexPrefix, bigIntToHex } from './bytes.js'
import { isHexString } from './internal.js'
import type { PrefixedHexString } from './types.js'
export type StoragePair = [key: PrefixedHexString, value: PrefixedHexString]
export type AccountState = [
balance: PrefixedHexString,
code: PrefixedHexString,
storage: Array<StoragePair>,
nonce: PrefixedHexString,
]
/**
* If you are using a custom chain {@link Common}, pass the genesis state.
*
* Pattern 1 (with genesis state see {@link GenesisState} for format):
*
* ```javascript
* {
* '0x0...01': '0x100', // For EoA
* }
* ```
*
* Pattern 2 (with complex genesis state, containing contract accounts and storage).
* Note that in {@link AccountState} there are two
* accepted types. This allows to easily insert accounts in the genesis state:
*
* A complex genesis state with Contract and EoA states would have the following format:
*
* ```javascript
* {
* '0x0...01': '0x100', // For EoA
* '0x0...02': ['0x1', '0xRUNTIME_BYTECODE', [[storageKey1, storageValue1], [storageKey2, storageValue2]]] // For contracts
* }
* ```
*/
export interface GenesisState {
[key: string]: PrefixedHexString | AccountState
}
/**
* Parses the geth genesis state into Blockchain {@link GenesisState}
* @param json representing the `alloc` key in a Geth genesis file
*/
export function parseGethGenesisState(json: any) {
const state: GenesisState = {}
for (const address of Object.keys(json.alloc)) {
let { balance, code, storage, nonce } = json.alloc[address]
// create a map with lowercase for easy lookups
const prefixedAddress = addHexPrefix(address.toLowerCase())
balance = isHexString(balance) ? balance : bigIntToHex(BigInt(balance))
code = code !== undefined ? addHexPrefix(code) : undefined
storage = storage !== undefined ? Object.entries(storage) : undefined
nonce = nonce !== undefined ? addHexPrefix(nonce) : undefined
state[prefixedAddress] = [balance, code, storage, nonce]
}
return state
}