-
Notifications
You must be signed in to change notification settings - Fork 760
/
account.ts
661 lines (583 loc) · 19.4 KB
/
account.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
import { RLP } from '@ethereumjs/rlp'
import { keccak256 } from 'ethereum-cryptography/keccak.js'
import { secp256k1 } from 'ethereum-cryptography/secp256k1.js'
import {
bigIntToUnpaddedBytes,
bytesToBigInt,
bytesToHex,
bytesToInt,
concatBytes,
equalsBytes,
hexToBytes,
intToUnpaddedBytes,
toBytes,
utf8ToBytes,
} from './bytes.js'
import { BIGINT_0, KECCAK256_NULL, KECCAK256_RLP } from './constants.js'
import { assertIsBytes, assertIsHexString, assertIsString } from './helpers.js'
import { stripHexPrefix } from './internal.js'
import type { BigIntLike, BytesLike, PrefixedHexString } from './types.js'
export interface AccountData {
nonce?: BigIntLike
balance?: BigIntLike
storageRoot?: BytesLike
codeHash?: BytesLike
}
export interface PartialAccountData {
nonce?: BigIntLike | null
balance?: BigIntLike | null
storageRoot?: BytesLike | null
codeHash?: BytesLike | null
codeSize?: BigIntLike | null
version?: BigIntLike | null
}
export type AccountBodyBytes = [Uint8Array, Uint8Array, Uint8Array, Uint8Array]
/**
* Account class to load and maintain the basic account objects.
* Supports partial loading and access required for verkle with null
* as the placeholder.
*
* Note: passing undefined in constructor is different from null
* While undefined leads to default assignment, null is retained
* to track the information not available/loaded because of partial
* witness access
*/
export class Account {
_nonce: bigint | null = null
_balance: bigint | null = null
_storageRoot: Uint8Array | null = null
_codeHash: Uint8Array | null = null
// codeSize and version is separately stored in VKT
_codeSize: number | null = null
_version: number | null = null
get version() {
if (this._version !== null) {
return this._version
} else {
throw Error(`version=${this._version} not loaded`)
}
}
set version(_version: number) {
this._version = _version
}
get nonce() {
if (this._nonce !== null) {
return this._nonce
} else {
throw Error(`nonce=${this._nonce} not loaded`)
}
}
set nonce(_nonce: bigint) {
this._nonce = _nonce
}
get balance() {
if (this._balance !== null) {
return this._balance
} else {
throw Error(`balance=${this._balance} not loaded`)
}
}
set balance(_balance: bigint) {
this._balance = _balance
}
get storageRoot() {
if (this._storageRoot !== null) {
return this._storageRoot
} else {
throw Error(`storageRoot=${this._storageRoot} not loaded`)
}
}
set storageRoot(_storageRoot: Uint8Array) {
this._storageRoot = _storageRoot
}
get codeHash() {
if (this._codeHash !== null) {
return this._codeHash
} else {
throw Error(`codeHash=${this._codeHash} not loaded`)
}
}
set codeHash(_codeHash: Uint8Array) {
this._codeHash = _codeHash
}
get codeSize() {
if (this._codeSize !== null) {
return this._codeSize
} else {
throw Error(`codeSize=${this._codeSize} not loaded`)
}
}
set codeSize(_codeSize: number) {
this._codeSize = _codeSize
}
/**
* This constructor assigns and validates the values.
* Use the static factory methods to assist in creating an Account from varying data types.
* undefined get assigned with the defaults present, but null args are retained as is
*/
constructor(
nonce: bigint | null = BIGINT_0,
balance: bigint | null = BIGINT_0,
storageRoot: Uint8Array | null = KECCAK256_RLP,
codeHash: Uint8Array | null = KECCAK256_NULL,
codeSize: number | null = 0,
version: number | null = 0,
) {
this._nonce = nonce
this._balance = balance
this._storageRoot = storageRoot
this._codeHash = codeHash
if (codeSize === null && codeHash !== null && !this.isContract()) {
codeSize = 0
}
this._codeSize = codeSize
this._version = version
this._validate()
}
private _validate() {
if (this._nonce !== null && this._nonce < BIGINT_0) {
throw new Error('nonce must be greater than zero')
}
if (this._balance !== null && this._balance < BIGINT_0) {
throw new Error('balance must be greater than zero')
}
if (this._storageRoot !== null && this._storageRoot.length !== 32) {
throw new Error('storageRoot must have a length of 32')
}
if (this._codeHash !== null && this._codeHash.length !== 32) {
throw new Error('codeHash must have a length of 32')
}
if (this._codeSize !== null && this._codeSize < BIGINT_0) {
throw new Error('codeSize must be greater than zero')
}
}
/**
* Returns an array of Uint8Arrays of the raw bytes for the account, in order.
*/
raw(): Uint8Array[] {
return [
bigIntToUnpaddedBytes(this.nonce),
bigIntToUnpaddedBytes(this.balance),
this.storageRoot,
this.codeHash,
]
}
/**
* Returns the RLP serialization of the account as a `Uint8Array`.
*/
serialize(): Uint8Array {
return RLP.encode(this.raw())
}
serializeWithPartialInfo(): Uint8Array {
const partialData = []
const zeroEncoded = intToUnpaddedBytes(0)
const oneEncoded = intToUnpaddedBytes(1)
if (this._nonce !== null) {
partialData.push([oneEncoded, bigIntToUnpaddedBytes(this._nonce)])
} else {
partialData.push([zeroEncoded])
}
if (this._balance !== null) {
partialData.push([oneEncoded, bigIntToUnpaddedBytes(this._balance)])
} else {
partialData.push([zeroEncoded])
}
if (this._storageRoot !== null) {
partialData.push([oneEncoded, this._storageRoot])
} else {
partialData.push([zeroEncoded])
}
if (this._codeHash !== null) {
partialData.push([oneEncoded, this._codeHash])
} else {
partialData.push([zeroEncoded])
}
if (this._codeSize !== null) {
partialData.push([oneEncoded, intToUnpaddedBytes(this._codeSize)])
} else {
partialData.push([zeroEncoded])
}
if (this._version !== null) {
partialData.push([oneEncoded, intToUnpaddedBytes(this._version)])
} else {
partialData.push([zeroEncoded])
}
return RLP.encode(partialData)
}
/**
* Returns a `Boolean` determining if the account is a contract.
*/
isContract(): boolean {
if (this._codeHash === null && this._codeSize === null) {
throw Error(`Insufficient data as codeHash=null and codeSize=null`)
}
return (
(this._codeHash !== null && !equalsBytes(this._codeHash, KECCAK256_NULL)) ||
(this._codeSize !== null && this._codeSize !== 0)
)
}
/**
* Returns a `Boolean` determining if the account is empty complying to the definition of
* account emptiness in [EIP-161](https://eips.ethereum.org/EIPS/eip-161):
* "An account is considered empty when it has no code and zero nonce and zero balance."
*/
isEmpty(): boolean {
// helpful for determination in partial accounts
if (
(this._balance !== null && this.balance !== BIGINT_0) ||
(this._nonce === null && this.nonce !== BIGINT_0) ||
(this._codeHash !== null && !equalsBytes(this.codeHash, KECCAK256_NULL))
) {
return false
}
return (
this.balance === BIGINT_0 &&
this.nonce === BIGINT_0 &&
equalsBytes(this.codeHash, KECCAK256_NULL)
)
}
}
// Account constructors
export function createAccount(accountData: AccountData) {
const { nonce, balance, storageRoot, codeHash } = accountData
if (nonce === null || balance === null || storageRoot === null || codeHash === null) {
throw Error(`Partial fields not supported in fromAccountData`)
}
return new Account(
nonce !== undefined ? bytesToBigInt(toBytes(nonce)) : undefined,
balance !== undefined ? bytesToBigInt(toBytes(balance)) : undefined,
storageRoot !== undefined ? toBytes(storageRoot) : undefined,
codeHash !== undefined ? toBytes(codeHash) : undefined,
)
}
export function createAccountFromBytesArray(values: Uint8Array[]) {
const [nonce, balance, storageRoot, codeHash] = values
return new Account(bytesToBigInt(nonce), bytesToBigInt(balance), storageRoot, codeHash)
}
export function createPartialAccount(partialAccountData: PartialAccountData) {
const { nonce, balance, storageRoot, codeHash, codeSize, version } = partialAccountData
if (
nonce === null &&
balance === null &&
storageRoot === null &&
codeHash === null &&
codeSize === null &&
version === null
) {
throw Error(`All partial fields null`)
}
return new Account(
nonce !== undefined && nonce !== null ? bytesToBigInt(toBytes(nonce)) : nonce,
balance !== undefined && balance !== null ? bytesToBigInt(toBytes(balance)) : balance,
storageRoot !== undefined && storageRoot !== null ? toBytes(storageRoot) : storageRoot,
codeHash !== undefined && codeHash !== null ? toBytes(codeHash) : codeHash,
codeSize !== undefined && codeSize !== null ? bytesToInt(toBytes(codeSize)) : codeSize,
version !== undefined && version !== null ? bytesToInt(toBytes(version)) : version,
)
}
export function createAccountFromRLP(serialized: Uint8Array) {
const values = RLP.decode(serialized) as Uint8Array[]
if (!Array.isArray(values)) {
throw new Error('Invalid serialized account input. Must be array')
}
return createAccountFromBytesArray(values)
}
export function createPartialAccountFromRLP(serialized: Uint8Array) {
const values = RLP.decode(serialized) as Uint8Array[][]
if (!Array.isArray(values)) {
throw new Error('Invalid serialized account input. Must be array')
}
let nonce = null
if (!Array.isArray(values[0])) {
throw new Error('Invalid partial nonce encoding. Must be array')
} else {
const isNotNullIndicator = bytesToInt(values[0][0])
if (isNotNullIndicator !== 0 && isNotNullIndicator !== 1) {
throw new Error(`Invalid isNullIndicator=${isNotNullIndicator} for nonce`)
}
if (isNotNullIndicator === 1) {
nonce = bytesToBigInt(values[0][1])
}
}
let balance = null
if (!Array.isArray(values[1])) {
throw new Error('Invalid partial balance encoding. Must be array')
} else {
const isNotNullIndicator = bytesToInt(values[1][0])
if (isNotNullIndicator !== 0 && isNotNullIndicator !== 1) {
throw new Error(`Invalid isNullIndicator=${isNotNullIndicator} for balance`)
}
if (isNotNullIndicator === 1) {
balance = bytesToBigInt(values[1][1])
}
}
let storageRoot = null
if (!Array.isArray(values[2])) {
throw new Error('Invalid partial storageRoot encoding. Must be array')
} else {
const isNotNullIndicator = bytesToInt(values[2][0])
if (isNotNullIndicator !== 0 && isNotNullIndicator !== 1) {
throw new Error(`Invalid isNullIndicator=${isNotNullIndicator} for storageRoot`)
}
if (isNotNullIndicator === 1) {
storageRoot = values[2][1]
}
}
let codeHash = null
if (!Array.isArray(values[3])) {
throw new Error('Invalid partial codeHash encoding. Must be array')
} else {
const isNotNullIndicator = bytesToInt(values[3][0])
if (isNotNullIndicator !== 0 && isNotNullIndicator !== 1) {
throw new Error(`Invalid isNullIndicator=${isNotNullIndicator} for codeHash`)
}
if (isNotNullIndicator === 1) {
codeHash = values[3][1]
}
}
let codeSize = null
if (!Array.isArray(values[4])) {
throw new Error('Invalid partial codeSize encoding. Must be array')
} else {
const isNotNullIndicator = bytesToInt(values[4][0])
if (isNotNullIndicator !== 0 && isNotNullIndicator !== 1) {
throw new Error(`Invalid isNullIndicator=${isNotNullIndicator} for codeSize`)
}
if (isNotNullIndicator === 1) {
codeSize = bytesToInt(values[4][1])
}
}
let version = null
if (!Array.isArray(values[5])) {
throw new Error('Invalid partial version encoding. Must be array')
} else {
const isNotNullIndicator = bytesToInt(values[5][0])
if (isNotNullIndicator !== 0 && isNotNullIndicator !== 1) {
throw new Error(`Invalid isNullIndicator=${isNotNullIndicator} for version`)
}
if (isNotNullIndicator === 1) {
version = bytesToInt(values[5][1])
}
}
return createPartialAccount({ balance, nonce, storageRoot, codeHash, codeSize, version })
}
/**
* Checks if the address is a valid. Accepts checksummed addresses too.
*/
export const isValidAddress = function (hexAddress: string): hexAddress is PrefixedHexString {
try {
assertIsString(hexAddress)
} catch (e: any) {
return false
}
return /^0x[0-9a-fA-F]{40}$/.test(hexAddress)
}
/**
* Returns a checksummed address.
*
* If an eip1191ChainId is provided, the chainId will be included in the checksum calculation. This
* has the effect of checksummed addresses for one chain having invalid checksums for others.
* For more details see [EIP-1191](https://eips.ethereum.org/EIPS/eip-1191).
*
* WARNING: Checksums with and without the chainId will differ and the EIP-1191 checksum is not
* backwards compatible to the original widely adopted checksum format standard introduced in
* [EIP-55](https://eips.ethereum.org/EIPS/eip-55), so this will break in existing applications.
* Usage of this EIP is therefore discouraged unless you have a very targeted use case.
*/
export const toChecksumAddress = function (
hexAddress: string,
eip1191ChainId?: BigIntLike,
): PrefixedHexString {
assertIsHexString(hexAddress)
const address = stripHexPrefix(hexAddress).toLowerCase()
let prefix = ''
if (eip1191ChainId !== undefined) {
const chainId = bytesToBigInt(toBytes(eip1191ChainId))
prefix = chainId.toString() + '0x'
}
const bytes = utf8ToBytes(prefix + address)
const hash = bytesToHex(keccak256(bytes)).slice(2)
let ret = ''
for (let i = 0; i < address.length; i++) {
if (parseInt(hash[i], 16) >= 8) {
ret += address[i].toUpperCase()
} else {
ret += address[i]
}
}
return `0x${ret}`
}
/**
* Checks if the address is a valid checksummed address.
*
* See toChecksumAddress' documentation for details about the eip1191ChainId parameter.
*/
export const isValidChecksumAddress = function (
hexAddress: string,
eip1191ChainId?: BigIntLike,
): boolean {
return isValidAddress(hexAddress) && toChecksumAddress(hexAddress, eip1191ChainId) === hexAddress
}
/**
* Generates an address of a newly created contract.
* @param from The address which is creating this new address
* @param nonce The nonce of the from account
*/
export const generateAddress = function (from: Uint8Array, nonce: Uint8Array): Uint8Array {
assertIsBytes(from)
assertIsBytes(nonce)
if (bytesToBigInt(nonce) === BIGINT_0) {
// in RLP we want to encode null in the case of zero nonce
// read the RLP documentation for an answer if you dare
return keccak256(RLP.encode([from, Uint8Array.from([])])).subarray(-20)
}
// Only take the lower 160bits of the hash
return keccak256(RLP.encode([from, nonce])).subarray(-20)
}
/**
* Generates an address for a contract created using CREATE2.
* @param from The address which is creating this new address
* @param salt A salt
* @param initCode The init code of the contract being created
*/
export const generateAddress2 = function (
from: Uint8Array,
salt: Uint8Array,
initCode: Uint8Array,
): Uint8Array {
assertIsBytes(from)
assertIsBytes(salt)
assertIsBytes(initCode)
if (from.length !== 20) {
throw new Error('Expected from to be of length 20')
}
if (salt.length !== 32) {
throw new Error('Expected salt to be of length 32')
}
const address = keccak256(concatBytes(hexToBytes('0xff'), from, salt, keccak256(initCode)))
return address.subarray(-20)
}
/**
* Checks if the private key satisfies the rules of the curve secp256k1.
*/
export const isValidPrivate = function (privateKey: Uint8Array): boolean {
return secp256k1.utils.isValidPrivateKey(privateKey)
}
/**
* Checks if the public key satisfies the rules of the curve secp256k1
* and the requirements of Ethereum.
* @param publicKey The two points of an uncompressed key, unless sanitize is enabled
* @param sanitize Accept public keys in other formats
*/
export const isValidPublic = function (publicKey: Uint8Array, sanitize: boolean = false): boolean {
assertIsBytes(publicKey)
if (publicKey.length === 64) {
// Convert to SEC1 for secp256k1
// Automatically checks whether point is on curve
try {
secp256k1.ProjectivePoint.fromHex(concatBytes(Uint8Array.from([4]), publicKey))
return true
} catch (e) {
return false
}
}
if (!sanitize) {
return false
}
try {
secp256k1.ProjectivePoint.fromHex(publicKey)
return true
} catch (e) {
return false
}
}
/**
* Returns the ethereum address of a given public key.
* Accepts "Ethereum public keys" and SEC1 encoded keys.
* @param pubKey The two points of an uncompressed key, unless sanitize is enabled
* @param sanitize Accept public keys in other formats
*/
export const pubToAddress = function (pubKey: Uint8Array, sanitize: boolean = false): Uint8Array {
assertIsBytes(pubKey)
if (sanitize && pubKey.length !== 64) {
pubKey = secp256k1.ProjectivePoint.fromHex(pubKey).toRawBytes(false).slice(1)
}
if (pubKey.length !== 64) {
throw new Error('Expected pubKey to be of length 64')
}
// Only take the lower 160bits of the hash
return keccak256(pubKey).subarray(-20)
}
export const publicToAddress = pubToAddress
/**
* Returns the ethereum public key of a given private key.
* @param privateKey A private key must be 256 bits wide
*/
export const privateToPublic = function (privateKey: Uint8Array): Uint8Array {
assertIsBytes(privateKey)
// skip the type flag and use the X, Y points
return secp256k1.ProjectivePoint.fromPrivateKey(privateKey).toRawBytes(false).slice(1)
}
/**
* Returns the ethereum address of a given private key.
* @param privateKey A private key must be 256 bits wide
*/
export const privateToAddress = function (privateKey: Uint8Array): Uint8Array {
return publicToAddress(privateToPublic(privateKey))
}
/**
* Converts a public key to the Ethereum format.
*/
export const importPublic = function (publicKey: Uint8Array): Uint8Array {
assertIsBytes(publicKey)
if (publicKey.length !== 64) {
publicKey = secp256k1.ProjectivePoint.fromHex(publicKey).toRawBytes(false).slice(1)
}
return publicKey
}
/**
* Returns the zero address.
*/
export const zeroAddress = function (): PrefixedHexString {
return bytesToHex(new Uint8Array(20))
}
/**
* Checks if a given address is the zero address.
*/
export const isZeroAddress = function (hexAddress: string): boolean {
try {
assertIsString(hexAddress)
} catch (e: any) {
return false
}
const zeroAddr = zeroAddress()
return zeroAddr === hexAddress
}
export function accountBodyFromSlim(body: AccountBodyBytes) {
const [nonce, balance, storageRoot, codeHash] = body
return [
nonce,
balance,
storageRoot.length === 0 ? KECCAK256_RLP : storageRoot,
codeHash.length === 0 ? KECCAK256_NULL : codeHash,
]
}
const emptyUint8Arr = new Uint8Array(0)
export function accountBodyToSlim(body: AccountBodyBytes) {
const [nonce, balance, storageRoot, codeHash] = body
return [
nonce,
balance,
equalsBytes(storageRoot, KECCAK256_RLP) ? emptyUint8Arr : storageRoot,
equalsBytes(codeHash, KECCAK256_NULL) ? emptyUint8Arr : codeHash,
]
}
/**
* Converts a slim account (per snap protocol spec) to the RLP encoded version of the account
* @param body Array of 4 Uint8Array-like items to represent the account
* @returns RLP encoded version of the account
*/
export function accountBodyToRLP(body: AccountBodyBytes, couldBeSlim = true) {
const accountBody = couldBeSlim ? accountBodyFromSlim(body) : body
return RLP.encode(accountBody)
}