@@ -29,8 +29,8 @@ export function decodeAccount(buf: Uint8Array): Array<Uint8Array> {
29
29
} else if ( nonceLen <= 0x7f ) {
30
30
nonce = buf . subarray ( 2 , 3 )
31
31
} else {
32
- nonce = buf . subarray ( 3 , 3 + nonceLen - 0x80 )
33
32
offset = 3 + nonceLen - 0x80
33
+ nonce = buf . subarray ( 3 , offset )
34
34
}
35
35
36
36
let balance : Uint8Array
@@ -39,12 +39,14 @@ export function decodeAccount(buf: Uint8Array): Array<Uint8Array> {
39
39
balance = new Uint8Array ( 0 )
40
40
offset ++
41
41
} else if ( balanceFirstByte <= 0x7f ) {
42
- balance = buf . subarray ( offset , offset + 1 )
43
- offset ++
42
+ let end = offset + 1
43
+ balance = buf . subarray ( offset , end )
44
+ offset = end
44
45
} else {
45
46
offset ++
46
- balance = buf . subarray ( offset , offset + balanceFirstByte - 0x80 )
47
- offset = offset + balanceFirstByte - 0x80
47
+ let end = offset + balanceFirstByte - 0x80
48
+ balance = buf . subarray ( offset , end )
49
+ offset = end
48
50
}
49
51
50
52
// stateRoot and codeHash are 32 byte hashes
@@ -194,7 +196,7 @@ export function hashBranch(children: Array<Uint8Array | null>): Uint8Array {
194
196
}
195
197
196
198
// How many bytes do we need to encode length?
197
- let lenOfLen = dataLen > 255 ? 2 : 1
199
+ let lenOfLen = 1 + i32 ( dataLen > 255 )
198
200
let totalLen = 1 + lenOfLen + dataLen
199
201
let buf = new Uint8Array ( totalLen )
200
202
let offset = 0
@@ -265,13 +267,14 @@ export function hashBranchNode(branchNodeChildren: Array<usize>): usize {
265
267
// bytes for hashes = len(0xa0 + hash) = 33*branch_num_children
266
268
// bytes for empty nodes (0x80) = (17 - branch_num_children)
267
269
268
- let child_indexes = Array . create < u8 > ( 17 )
269
- let child_hash_ptrs = Array . create < usize > ( 17 )
270
+ let child_indexes = new Array < u8 > ( )
271
+ let child_hash_ptrs = new Array < usize > ( )
270
272
for ( let i = 0 ; i < 17 ; i ++ ) {
271
273
// read child index
272
- if ( branchNodeChildren [ i ] > 0 ) {
274
+ let branchNodeChild = branchNodeChildren [ i ]
275
+ if ( branchNodeChild > 0 ) {
273
276
child_indexes . push ( i as u8 )
274
- child_hash_ptrs . push ( branchNodeChildren [ i ] )
277
+ child_hash_ptrs . push ( branchNodeChild )
275
278
}
276
279
}
277
280
@@ -396,10 +399,11 @@ export class Decoded {
396
399
* @param base The base to parse the integer into
397
400
*/
398
401
function safeParseInt ( v : string , base : u32 ) : u32 {
399
- if ( v . slice ( 0 , 2 ) == '00' ) {
402
+ // v.slice(0, 2) == '00'
403
+ if ( v . charCodeAt ( 0 ) == 0x30 && v . charCodeAt ( 1 ) == 0x30 ) {
400
404
throw new Error ( 'invalid RLP: extra zeros' )
401
405
}
402
- return I32 . parseInt ( v , base ) as u32
406
+ return < u32 > I32 . parseInt ( v , base )
403
407
}
404
408
405
409
/** Transform an integer into its hexadecimal value */
@@ -412,16 +416,16 @@ function intToHex(integer: u32): string {
412
416
res . push ( hexAlphabet [ r ] )
413
417
} while ( integer )
414
418
let hex = res . reverse ( ) . join ( '' )
415
- return hex . length % 2 ? '0' + hex : hex
419
+ return hex . length & 1 ? '0' + hex : hex
416
420
}
417
421
418
422
function bytesToHex ( bytes : Uint8Array ) : string {
419
423
let len = bytes . length
420
424
let res = new Uint8Array ( len * 2 )
421
425
for ( let i = 0 ; i < len ; i ++ ) {
422
426
let hex = intToHex ( bytes [ i ] )
423
- res [ i * 2 + 0 ] = hex . charCodeAt ( 0 )
424
- res [ i * 2 + 1 ] = hex . charCodeAt ( 1 )
427
+ unchecked ( ( res [ i * 2 + 0 ] = hex . charCodeAt ( 0 ) ) )
428
+ unchecked ( ( res [ i * 2 + 1 ] = hex . charCodeAt ( 1 ) ) )
425
429
}
426
430
return String . UTF8 . decodeUnsafe ( res . dataStart as usize , res . byteLength )
427
431
}
@@ -434,16 +438,18 @@ function hexToBytes(hex: string): Uint8Array {
434
438
let byteLength = hex . length / 2
435
439
let res = new Uint8Array ( byteLength )
436
440
for ( let i = 0 ; i < byteLength ; i ++ ) {
437
- res [ i ] = I32 . parseInt ( hex . substring ( i * 2 , 2 ) , 16 ) as u8
441
+ res [ i ] = U8 . parseInt ( hex . substring ( i * 2 , 2 ) , 16 )
438
442
}
439
443
return res
440
444
}
441
445
442
446
function concatUint8Array ( arr1 : Uint8Array , arr2 : Uint8Array ) : Uint8Array {
443
- let len = arr1 . byteLength + arr2 . byteLength
444
- let res = new Uint8Array ( len )
445
- memory . copy ( res . dataStart as usize , arr1 . dataStart as usize , arr1 . byteLength )
446
- memory . copy ( ( res . dataStart as usize ) + arr1 . byteLength , arr2 . dataStart as usize , arr2 . byteLength )
447
+ let len1 = arr1 . byteLength
448
+ let len2 = arr2 . byteLength
449
+ let res = new Uint8Array ( len1 + len2 )
450
+ let dst = res . dataStart as usize
451
+ memory . copy ( dst , arr1 . dataStart as usize , len1 )
452
+ memory . copy ( dst + len1 , arr2 . dataStart as usize , len2 )
447
453
return res
448
454
}
449
455
@@ -468,11 +474,11 @@ function concatUint8Arrays(arrays: Array<Uint8Array>): Uint8Array {
468
474
**/
469
475
export function encode ( input : RLPData ) : Uint8Array {
470
476
let children = input . children
471
- if ( children . length ) {
472
- let output = new Array < Uint8Array > ( )
473
- output . push ( new Uint8Array ( 0 ) )
477
+ let len = children . length
478
+ if ( len ) {
479
+ let output = [ new Uint8Array ( 0 ) ]
474
480
let totalLen = 0
475
- for ( let i = 0 , len = children . length ; i < len ; i ++ ) {
481
+ for ( let i = 0 ; i < len ; i ++ ) {
476
482
let e = encode ( children [ i ] )
477
483
output . push ( e )
478
484
totalLen += output [ i + 1 ] . byteLength
@@ -482,12 +488,13 @@ export function encode(input: RLPData): Uint8Array {
482
488
} else {
483
489
//debug_mem((input.buffer.buffer as usize) + input.buffer.byteOffset, input.buffer.byteLength);
484
490
let inputBuffer = input . buffer
485
- if ( inputBuffer . length == 1 && inputBuffer [ 0 ] < 128 ) {
491
+ len = inputBuffer . length
492
+ if ( len == 1 && inputBuffer [ 0 ] < 128 ) {
486
493
return inputBuffer
487
494
}
488
- let len_encoded = encodeLength ( inputBuffer . length , 128 )
489
- //debug_mem(len_encoded.dataStart, len_encoded .byteLength);
490
- return concatUint8Array ( len_encoded , inputBuffer )
495
+ let encodedLen = encodeLength ( len , 128 )
496
+ //debug_mem(len_encoded.dataStart, encodedLen .byteLength);
497
+ return concatUint8Array ( encodedLen , inputBuffer )
491
498
}
492
499
}
493
500
@@ -505,12 +512,12 @@ function encodeLength(len: u32, offset: u32): Uint8Array {
505
512
let int_as_bytes = new Uint8Array ( 2 )
506
513
//let int_view = DataView.wrap(int_as_bytes.buffer, 0, 2);
507
514
let int_view = new DataView ( int_as_bytes . buffer , 0 , 2 )
508
- int_view . setUint16 ( 0 , int as u16 , false )
515
+ int_view . setUint16 ( 0 , int as u16 )
509
516
return int_as_bytes
510
517
}
511
518
throw new Error ( 'longer lengths unsupported' )
512
- //return hexToBytes(intToHex(len + offset));
513
- return hexToBytes ( intToHex ( len + offset ) )
519
+ // return hexToBytes(intToHex(len + offset));
520
+ // return hexToBytes(intToHex(len + offset))
514
521
} else {
515
522
/*
516
523
let hexLength = intToHex(len);
@@ -529,7 +536,7 @@ function encodeLength(len: u32, offset: u32): Uint8Array {
529
536
lLength = 2
530
537
len_as_bytes = new Uint8Array ( 2 )
531
538
let len_view = new DataView ( len_as_bytes . buffer , 0 , 2 )
532
- len_view . setUint16 ( 0 , len as u16 , false )
539
+ len_view . setUint16 ( 0 , len as u16 )
533
540
} else {
534
541
throw new Error ( 'longer lengths unsupported' )
535
542
}
@@ -545,7 +552,7 @@ function encodeLength(len: u32, offset: u32): Uint8Array {
545
552
firstByte_as_bytes = new Uint8Array ( 2 )
546
553
//let int_view = DataView.wrap(int_as_bytes.buffer, 0, 2);
547
554
let int_view = new DataView ( firstByte_as_bytes . buffer , 0 , 2 )
548
- int_view . setUint16 ( 0 , firstByte as u16 , false )
555
+ int_view . setUint16 ( 0 , firstByte as u16 )
549
556
//return int_as_bytes;
550
557
} else {
551
558
throw new Error ( 'longer lengths unsupported' )
0 commit comments