@@ -29,8 +29,8 @@ export function decodeAccount(buf: Uint8Array): Array<Uint8Array> {
2929 } else if ( nonceLen <= 0x7f ) {
3030 nonce = buf . subarray ( 2 , 3 )
3131 } else {
32- nonce = buf . subarray ( 3 , 3 + nonceLen - 0x80 )
3332 offset = 3 + nonceLen - 0x80
33+ nonce = buf . subarray ( 3 , offset )
3434 }
3535
3636 let balance : Uint8Array
@@ -39,12 +39,14 @@ export function decodeAccount(buf: Uint8Array): Array<Uint8Array> {
3939 balance = new Uint8Array ( 0 )
4040 offset ++
4141 } 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
4445 } else {
4546 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
4850 }
4951
5052 // stateRoot and codeHash are 32 byte hashes
@@ -194,7 +196,7 @@ export function hashBranch(children: Array<Uint8Array | null>): Uint8Array {
194196 }
195197
196198 // How many bytes do we need to encode length?
197- let lenOfLen = dataLen > 255 ? 2 : 1
199+ let lenOfLen = 1 + i32 ( dataLen > 255 )
198200 let totalLen = 1 + lenOfLen + dataLen
199201 let buf = new Uint8Array ( totalLen )
200202 let offset = 0
@@ -265,13 +267,14 @@ export function hashBranchNode(branchNodeChildren: Array<usize>): usize {
265267 // bytes for hashes = len(0xa0 + hash) = 33*branch_num_children
266268 // bytes for empty nodes (0x80) = (17 - branch_num_children)
267269
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 > ( )
270272 for ( let i = 0 ; i < 17 ; i ++ ) {
271273 // read child index
272- if ( branchNodeChildren [ i ] > 0 ) {
274+ let branchNodeChild = branchNodeChildren [ i ]
275+ if ( branchNodeChild > 0 ) {
273276 child_indexes . push ( i as u8 )
274- child_hash_ptrs . push ( branchNodeChildren [ i ] )
277+ child_hash_ptrs . push ( branchNodeChild )
275278 }
276279 }
277280
@@ -396,10 +399,11 @@ export class Decoded {
396399 * @param base The base to parse the integer into
397400 */
398401function 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 ) {
400404 throw new Error ( 'invalid RLP: extra zeros' )
401405 }
402- return I32 . parseInt ( v , base ) as u32
406+ return < u32 > I32 . parseInt ( v , base )
403407}
404408
405409/** Transform an integer into its hexadecimal value */
@@ -412,16 +416,16 @@ function intToHex(integer: u32): string {
412416 res . push ( hexAlphabet [ r ] )
413417 } while ( integer )
414418 let hex = res . reverse ( ) . join ( '' )
415- return hex . length % 2 ? '0' + hex : hex
419+ return hex . length & 1 ? '0' + hex : hex
416420}
417421
418422function bytesToHex ( bytes : Uint8Array ) : string {
419423 let len = bytes . length
420424 let res = new Uint8Array ( len * 2 )
421425 for ( let i = 0 ; i < len ; i ++ ) {
422426 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 ) ) )
425429 }
426430 return String . UTF8 . decodeUnsafe ( res . dataStart as usize , res . byteLength )
427431}
@@ -434,16 +438,18 @@ function hexToBytes(hex: string): Uint8Array {
434438 let byteLength = hex . length / 2
435439 let res = new Uint8Array ( byteLength )
436440 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 )
438442 }
439443 return res
440444}
441445
442446function 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 )
447453 return res
448454}
449455
@@ -468,11 +474,11 @@ function concatUint8Arrays(arrays: Array<Uint8Array>): Uint8Array {
468474 **/
469475export function encode ( input : RLPData ) : Uint8Array {
470476 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 ) ]
474480 let totalLen = 0
475- for ( let i = 0 , len = children . length ; i < len ; i ++ ) {
481+ for ( let i = 0 ; i < len ; i ++ ) {
476482 let e = encode ( children [ i ] )
477483 output . push ( e )
478484 totalLen += output [ i + 1 ] . byteLength
@@ -482,12 +488,13 @@ export function encode(input: RLPData): Uint8Array {
482488 } else {
483489 //debug_mem((input.buffer.buffer as usize) + input.buffer.byteOffset, input.buffer.byteLength);
484490 let inputBuffer = input . buffer
485- if ( inputBuffer . length == 1 && inputBuffer [ 0 ] < 128 ) {
491+ len = inputBuffer . length
492+ if ( len == 1 && inputBuffer [ 0 ] < 128 ) {
486493 return inputBuffer
487494 }
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 )
491498 }
492499}
493500
@@ -505,12 +512,12 @@ function encodeLength(len: u32, offset: u32): Uint8Array {
505512 let int_as_bytes = new Uint8Array ( 2 )
506513 //let int_view = DataView.wrap(int_as_bytes.buffer, 0, 2);
507514 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 )
509516 return int_as_bytes
510517 }
511518 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))
514521 } else {
515522 /*
516523 let hexLength = intToHex(len);
@@ -529,7 +536,7 @@ function encodeLength(len: u32, offset: u32): Uint8Array {
529536 lLength = 2
530537 len_as_bytes = new Uint8Array ( 2 )
531538 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 )
533540 } else {
534541 throw new Error ( 'longer lengths unsupported' )
535542 }
@@ -545,7 +552,7 @@ function encodeLength(len: u32, offset: u32): Uint8Array {
545552 firstByte_as_bytes = new Uint8Array ( 2 )
546553 //let int_view = DataView.wrap(int_as_bytes.buffer, 0, 2);
547554 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 )
549556 //return int_as_bytes;
550557 } else {
551558 throw new Error ( 'longer lengths unsupported' )
0 commit comments