Skip to content

Commit

Permalink
util: move misplaced functions (#1825)
Browse files Browse the repository at this point in the history
* util: move misplaced functions

* add unpadBuffer back

* Fix tttttttttttttypo

* make unpadHexString return Hex String!
  • Loading branch information
acolytec3 authored Apr 4, 2022
1 parent 9341b6a commit 95f4779
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 39 deletions.
4 changes: 2 additions & 2 deletions packages/util/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
18 changes: 17 additions & 1 deletion packages/util/src/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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))
}
22 changes: 1 addition & 21 deletions packages/util/src/types.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -55,15 +48,6 @@ export interface TransformableToBuffer {
export type NestedUint8Array = Array<Uint8Array | NestedUint8Array>
export type NestedBufferArray = Array<Buffer | NestedBufferArray>

/**
* 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
*/
Expand Down Expand Up @@ -134,7 +118,3 @@ export function toType<T extends TypeOutput>(
throw new Error('unknown outputType')
}
}

export const bigIntToHex = (num: bigint) => {
return '0x' + num.toString(16)
}
17 changes: 16 additions & 1 deletion packages/util/test/bytes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
validateNoLeadingZeroes,
bufferToBigInt,
bigIntToBuffer,
bigIntToUnpaddedBuffer,
bigIntToHex,
} from '../src'

tape('zeros function', function (t) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
})
14 changes: 0 additions & 14 deletions packages/util/test/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
intToBuffer,
bufferToHex,
intToHex,
bigIntToUnpaddedBuffer,
toBuffer,
bigIntToHex,
bigIntToBuffer,
Expand Down Expand Up @@ -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()
})

0 comments on commit 95f4779

Please sign in to comment.