From 95f47790ed7ebfd7246a02f8e46d5c7c005c5db2 Mon Sep 17 00:00:00 2001 From: acolytec3 <17355484+acolytec3@users.noreply.github.com> Date: Mon, 4 Apr 2022 07:46:07 -0400 Subject: [PATCH] util: move misplaced functions (#1825) * util: move misplaced functions * add unpadBuffer back * Fix tttttttttttttypo * make unpadHexString return Hex String! --- packages/util/src/account.ts | 4 ++-- packages/util/src/bytes.ts | 18 +++++++++++++++++- packages/util/src/types.ts | 22 +--------------------- packages/util/test/bytes.spec.ts | 17 ++++++++++++++++- packages/util/test/types.spec.ts | 14 -------------- 5 files changed, 36 insertions(+), 39 deletions(-) diff --git a/packages/util/src/account.ts b/packages/util/src/account.ts index 0d93356daa..348beb6823 100644 --- a/packages/util/src/account.ts +++ b/packages/util/src/account.ts @@ -2,10 +2,10 @@ 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) diff --git a/packages/util/src/bytes.ts b/packages/util/src/bytes.ts index e5a81d33c6..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 = @@ -353,3 +353,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..6ba432cca4 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) { @@ -94,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) { @@ -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() -})