From e30274e5dfb03c20ee0e1d5511af630de81c523b Mon Sep 17 00:00:00 2001 From: acolytec3 <17355484+acolytec3@users.noreply.github.com> Date: Tue, 29 Mar 2022 12:27:57 -0400 Subject: [PATCH 1/4] util: move misplaced functions --- packages/util/src/account.ts | 6 +++--- packages/util/src/bytes.ts | 26 ++++++++++++++++---------- packages/util/src/types.ts | 22 +--------------------- packages/util/test/bytes.spec.ts | 15 +++++++++++++++ packages/util/test/types.spec.ts | 14 -------------- 5 files changed, 35 insertions(+), 48 deletions(-) diff --git a/packages/util/src/account.ts b/packages/util/src/account.ts index 0d93356daa..c1f45f3ca9 100644 --- a/packages/util/src/account.ts +++ b/packages/util/src/account.ts @@ -2,12 +2,12 @@ import { rlp } from './externals' import { Point, utils } from 'ethereum-cryptography/secp256k1' import { stripHexPrefix } from './internal' import { KECCAK256_RLP, KECCAK256_NULL } from './constants' -import { zeros, bufferToHex, toBuffer, bufferToBigInt } from './bytes' +import { zeros, bufferToHex, toBuffer, bufferToBigInt, bigIntToUnpaddedBuffer } from './bytes' import { keccak, keccak256, keccakFromString, rlphash } from './hash' import { assertIsString, assertIsHexString, assertIsBuffer } from './helpers' -import { BigIntLike, BufferLike, bigIntToUnpaddedBuffer } from './types' +import { BigIntLike, BufferLike } from './types' -const _0n = BigInt(0) +const _0tttttttttttttn = BigInt(0) export interface AccountData { nonce?: BigIntLike diff --git a/packages/util/src/bytes.ts b/packages/util/src/bytes.ts index e5a81d33c6..ad0a1ae349 100644 --- a/packages/util/src/bytes.ts +++ b/packages/util/src/bytes.ts @@ -101,16 +101,6 @@ const stripZeros = function (a: any): Buffer | number[] | string { return a } -/** - * Trims leading zeros from a `Buffer`. - * @param a (Buffer) - * @return (Buffer) - */ -export const unpadBuffer = function (a: Buffer): Buffer { - assertIsBuffer(a) - return stripZeros(a) as Buffer -} - /** * Trims leading zeros from an `Array` (of numbers). * @param a (number[]) @@ -353,3 +343,19 @@ export function bufArrToArr(arr: Buffer | NestedBufferArray): Uint8Array | Neste } return arr.map((a) => bufArrToArr(a)) } + +/** + * Converts a {@link bigint} to a `0x` prefixed hex string + */ +export const bigIntToHex = (num: bigint) => { + return '0x' + num.toString(16) +} + +/** + * Convert value from bigint to an unpadded Buffer + * (useful for RLP transport) + * @param value value to convert + */ +export function bigIntToUnpaddedBuffer(value: bigint): Buffer { + return unpadBuffer(bigIntToBuffer(value)) +} diff --git a/packages/util/src/types.ts b/packages/util/src/types.ts index 1e54cf60e3..f1a620c8af 100644 --- a/packages/util/src/types.ts +++ b/packages/util/src/types.ts @@ -1,13 +1,6 @@ import { isHexString } from './internal' import { Address } from './address' -import { - unpadBuffer, - toBuffer, - ToBufferInputTypes, - bigIntToBuffer, - bufferToBigInt, - bufferToHex, -} from './bytes' +import { toBuffer, ToBufferInputTypes, bufferToBigInt, bufferToHex } from './bytes' /* * A type that represents an input that can be converted to a BigInt. @@ -55,15 +48,6 @@ export interface TransformableToBuffer { export type NestedUint8Array = Array export type NestedBufferArray = Array -/** - * Convert value from bigint to an unpadded Buffer - * (useful for RLP transport) - * @param value value to convert - */ -export function bigIntToUnpaddedBuffer(value: bigint): Buffer { - return unpadBuffer(bigIntToBuffer(value)) -} - /** * Type output options */ @@ -134,7 +118,3 @@ export function toType( throw new Error('unknown outputType') } } - -export const bigIntToHex = (num: bigint) => { - return '0x' + num.toString(16) -} diff --git a/packages/util/test/bytes.spec.ts b/packages/util/test/bytes.spec.ts index 95fc5c932d..84b10f08b8 100644 --- a/packages/util/test/bytes.spec.ts +++ b/packages/util/test/bytes.spec.ts @@ -24,6 +24,8 @@ import { validateNoLeadingZeroes, bufferToBigInt, bigIntToBuffer, + bigIntToUnpaddedBuffer, + bigIntToHex, } from '../src' tape('zeros function', function (t) { @@ -456,3 +458,16 @@ tape('bigIntToBuffer', (st) => { st.deepEqual(toBuffer('0x123'), bigIntToBuffer(num)) st.end() }) + +tape('bigIntToUnpaddedBuffer', function (t) { + t.test('should equal unpadded buffer value', function (st) { + st.ok(bigIntToUnpaddedBuffer(BigInt(0)).equals(Buffer.from([]))) + st.ok(bigIntToUnpaddedBuffer(BigInt(100)).equals(Buffer.from('64', 'hex'))) + st.end() + }) +}) + +tape('bigIntToHex', (st) => { + st.equal(bigIntToHex(BigInt(1)), '0x1') + st.end() +}) diff --git a/packages/util/test/types.spec.ts b/packages/util/test/types.spec.ts index ec009b22f1..844ba21c26 100644 --- a/packages/util/test/types.spec.ts +++ b/packages/util/test/types.spec.ts @@ -5,7 +5,6 @@ import { intToBuffer, bufferToHex, intToHex, - bigIntToUnpaddedBuffer, toBuffer, bigIntToHex, bigIntToBuffer, @@ -135,16 +134,3 @@ tape('toType', function (t) { }) }) }) - -tape('bigIntToUnpaddedBuffer', function (t) { - t.test('should equal unpadded buffer value', function (st) { - st.ok(bigIntToUnpaddedBuffer(BigInt(0)).equals(Buffer.from([]))) - st.ok(bigIntToUnpaddedBuffer(BigInt(100)).equals(Buffer.from('64', 'hex'))) - st.end() - }) -}) - -tape('bigIntToHex', (st) => { - st.equal(bigIntToHex(BigInt(1)), '0x1') - st.end() -}) From 8a820734932f3f2dc2b5f90bcf368ee4cff44a53 Mon Sep 17 00:00:00 2001 From: acolytec3 <17355484+acolytec3@users.noreply.github.com> Date: Tue, 29 Mar 2022 12:36:18 -0400 Subject: [PATCH 2/4] add unpadBuffer back --- packages/util/src/bytes.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/util/src/bytes.ts b/packages/util/src/bytes.ts index ad0a1ae349..8920d29b1b 100644 --- a/packages/util/src/bytes.ts +++ b/packages/util/src/bytes.ts @@ -101,6 +101,16 @@ const stripZeros = function (a: any): Buffer | number[] | string { return a } +/** + * Trims leading zeros from a `Buffer`. + * @param a (Buffer) + * @return (Buffer) + */ +export const unpadBuffer = function (a: Buffer): Buffer { + assertIsBuffer(a) + return stripZeros(a) as Buffer +} + /** * Trims leading zeros from an `Array` (of numbers). * @param a (number[]) From 68845c6da98345671fe2f5cf0aa6a9d11f2ffca9 Mon Sep 17 00:00:00 2001 From: acolytec3 <17355484+acolytec3@users.noreply.github.com> Date: Tue, 29 Mar 2022 12:39:58 -0400 Subject: [PATCH 3/4] Fix tttttttttttttypo --- packages/util/src/account.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/util/src/account.ts b/packages/util/src/account.ts index c1f45f3ca9..348beb6823 100644 --- a/packages/util/src/account.ts +++ b/packages/util/src/account.ts @@ -7,7 +7,7 @@ import { keccak, keccak256, keccakFromString, rlphash } from './hash' import { assertIsString, assertIsHexString, assertIsBuffer } from './helpers' import { BigIntLike, BufferLike } from './types' -const _0tttttttttttttn = BigInt(0) +const _0n = BigInt(0) export interface AccountData { nonce?: BigIntLike From 3240172193a39908b3e9919a12e765d2883cf2ec Mon Sep 17 00:00:00 2001 From: acolytec3 <17355484+acolytec3@users.noreply.github.com> Date: Tue, 29 Mar 2022 12:46:52 -0400 Subject: [PATCH 4/4] make unpadHexString return Hex String! --- packages/util/src/bytes.ts | 2 +- packages/util/test/bytes.spec.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/util/src/bytes.ts b/packages/util/src/bytes.ts index 8920d29b1b..3b7594b328 100644 --- a/packages/util/src/bytes.ts +++ b/packages/util/src/bytes.ts @@ -129,7 +129,7 @@ export const unpadArray = function (a: number[]): number[] { export const unpadHexString = function (a: string): string { assertIsHexString(a) a = stripHexPrefix(a) - return stripZeros(a) as string + return ('0x' + stripZeros(a)) as string } export type ToBufferInputTypes = diff --git a/packages/util/test/bytes.spec.ts b/packages/util/test/bytes.spec.ts index 84b10f08b8..6ba432cca4 100644 --- a/packages/util/test/bytes.spec.ts +++ b/packages/util/test/bytes.spec.ts @@ -96,7 +96,7 @@ tape('unpadHexString', function (t) { t.test('should unpad a hex prefixed string', function (st) { const str = '0x0000000006600' const r = unpadHexString(str) - st.equal(r, '6600') + st.equal(r, '0x6600') st.end() }) t.test('should throw if input is not hex-prefixed', function (st) {