This repository has been archived by the owner on Nov 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.ts
242 lines (223 loc) · 6.29 KB
/
index.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import * as rlp from 'rlp'
const ethUtil = require('ethereumjs-util')
const Buffer = require('safe-buffer').Buffer
interface TrieGetCb {
(err: any, value: Buffer | null): void
}
interface TriePutCb {
(err?: any): void
}
interface Trie {
root: Buffer
copy(): Trie
getRaw(key: Buffer, cb: TrieGetCb): void
putRaw(key: Buffer | string, value: Buffer, cb: TriePutCb): void
get(key: Buffer | string, cb: TrieGetCb): void
put(key: Buffer | string, value: Buffer | string, cb: TriePutCb): void
}
export default class Account {
/**
* The account's nonce.
*/
public nonce!: Buffer
/**
* The account's balance in wei.
*/
public balance!: Buffer
/**
* The stateRoot for the storage of the contract.
*/
public stateRoot!: Buffer
/**
* The hash of the code of the contract.
*/
public codeHash!: Buffer
/**
* Creates a new account object
*
* ~~~
* var data = [
* '0x02', //nonce
* '0x0384', //balance
* '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', //stateRoot
* '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', //codeHash
* ]
*
* var data = {
* nonce: '',
* balance: '0x03e7',
* stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
* codeHash: '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470',
* }
*
* const account = new Account(data)
* ~~~
*
* @param data
* An account can be initialized with either a `buffer` containing the RLP serialized account.
* Or an `Array` of buffers relating to each of the account Properties, listed in order below.
*
* For `Object` and `Array` each of the elements can either be a `Buffer`, hex `String`, `Number`, or an object with a `toBuffer` method such as `Bignum`.
*/
constructor(data?: any) {
const fields = [
{
name: 'nonce',
default: Buffer.alloc(0),
},
{
name: 'balance',
default: Buffer.alloc(0),
},
{
name: 'stateRoot',
length: 32,
default: ethUtil.KECCAK256_RLP,
},
{
name: 'codeHash',
length: 32,
default: ethUtil.KECCAK256_NULL,
},
]
ethUtil.defineProperties(this, fields, data)
}
/**
* Returns the RLP serialization of the account as a `Buffer`.
*
* @return {Buffer}
*/
serialize(): Buffer {
return rlp.encode([this.nonce, this.balance, this.stateRoot, this.codeHash])
}
/**
* Returns a `Boolean` deteremining if the account is a contract.
*
* @return {boolean}
*/
isContract(): boolean {
return this.codeHash.toString('hex') !== ethUtil.KECCAK256_NULL_S
}
/**
* Fetches the code from the trie.
* @param trie The [trie](https://github.com/ethereumjs/merkle-patricia-tree) storing the accounts
* @param cb The callback
*/
getCode(trie: Trie, cb: TrieGetCb): void {
if (!this.isContract()) {
cb(null, Buffer.alloc(0))
return
}
trie.getRaw(this.codeHash, cb)
}
/**
* Stores the code in the trie.
*
* ~~~
* // Requires manual merkle-patricia-tree install
* const SecureTrie = require('merkle-patricia-tree/secure')
* const Account = require('./index.js').default
*
* let code = Buffer.from(
* '73095e7baea6a6c7c4c2dfeb977efac326af552d873173095e7baea6a6c7c4c2dfeb977efac326af552d873157',
* 'hex',
* )
*
* let raw = {
* nonce: '',
* balance: '0x03e7',
* stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
* codeHash: '0xb30fb32201fe0486606ad451e1a61e2ae1748343cd3d411ed992ffcc0774edd4',
* }
* let account = new Account(raw)
* let trie = new SecureTrie()
*
* account.setCode(trie, code, function(err, codeHash) {
* console.log(`Code with hash 0x${codeHash.toString('hex')} set to trie`)
* account.getCode(trie, function(err, code) {
* console.log(`Code ${code.toString('hex')} read from trie`)
* })
* })
* ~~~
*
* @param trie The [trie](https://github.com/ethereumjs/merkle-patricia-tree) storing the accounts.
* @param {Buffer} code
* @param cb The callback.
*
*/
setCode(trie: Trie, code: Buffer, cb: (err: any, codeHash: Buffer) => void): void {
this.codeHash = ethUtil.keccak256(code)
if (this.codeHash.toString('hex') === ethUtil.KECCAK256_NULL_S) {
cb(null, Buffer.alloc(0))
return
}
trie.putRaw(this.codeHash, code, (err: any) => {
cb(err, this.codeHash)
})
}
/**
* Fetches `key` from the account's storage.
* @param trie
* @param key
* @param cb
*/
getStorage(trie: Trie, key: Buffer | string, cb: TrieGetCb) {
const t = trie.copy()
t.root = this.stateRoot
t.get(key, cb)
}
/**
* Stores a `val` at the `key` in the contract's storage.
*
* Example for `getStorage` and `setStorage`:
*
* ~~~
* // Requires manual merkle-patricia-tree install
* const SecureTrie = require('merkle-patricia-tree/secure')
* const Account = require('./index.js').default
*
* let raw = {
* nonce: '',
* balance: '0x03e7',
* stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
* codeHash: '0xb30fb32201fe0486606ad451e1a61e2ae1748343cd3d411ed992ffcc0774edd4',
* }
* let account = new Account(raw)
* let trie = new SecureTrie()
* let key = Buffer.from('0000000000000000000000000000000000000000', 'hex')
* let value = Buffer.from('01', 'hex')
*
* account.setStorage(trie, key, value, function(err, value) {
* account.getStorage(trie, key, function(err, value) {
* console.log(`Value ${value.toString('hex')} set and retrieved from trie.`)
* })
* })
* ~~~
*
* @param trie
* @param key
* @param val
* @param cb
*/
setStorage(trie: Trie, key: Buffer | string, val: Buffer | string, cb: () => void) {
const t = trie.copy()
t.root = this.stateRoot
t.put(key, val, (err: any) => {
if (err) return cb()
this.stateRoot = t.root
cb()
})
}
/**
* Returns a `Boolean` determining if the account is empty.
*
* @return {boolean} if account is empty
*/
isEmpty() {
return (
this.balance.toString('hex') === '' &&
this.nonce.toString('hex') === '' &&
this.codeHash.toString('hex') === ethUtil.KECCAK256_NULL_S
)
}
}