Skip to content

Commit

Permalink
refactor(utils): keep be bignumberish conversion by default
Browse files Browse the repository at this point in the history
  • Loading branch information
cedoor committed Feb 29, 2024
1 parent 3d26467 commit 24e1793
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 73 deletions.
46 changes: 8 additions & 38 deletions packages/utils/src/conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,13 @@ export function bigIntToBuffer(n: bigint): Buffer {
}

/**
* Converts a BigNumberish type to a bigint, handling various input types.
* It uses big-endian byte order.
* Converts a BigNumberish type to a bigint. If the input is already a bigint,
* the return value will be the bigint itself, otherwise it will be converted
* to a bigint using big-endian byte order.
* @param n The BigNumberish value to convert.
* @returns The bigint representation of the BigNumberish value.
*/
export function beBigNumberishToBigInt(n: BigNumberish): bigint {
export function bigNumberishToBigInt(n: BigNumberish): bigint {
if (
typeof n === "number" ||
typeof n === "bigint" ||
Expand All @@ -173,47 +174,16 @@ export function beBigNumberishToBigInt(n: BigNumberish): bigint {
}

/**
* Converts a BigNumberish type to a bigint. Alias for beBigNumberishToBigInt.
* @param n The BigNumberish value to convert.
* @returns The bigint representation of the BigNumberish value.
*/
export function bigNumberishToBigInt(n: BigNumberish): bigint {
return beBigNumberishToBigInt(n)
}

/**
* Converts a BigNumberish type to a buffer, handling various input types and converting
* them to bigint first if necessary. It uses big-endian byte order.
* Converts a BigNumberish type to a buffer. If the input is already a buffer,
* the return value will be the buffer itself, otherwise it will be converted
* to a buffer using big-endian byte order.
* @param n The BigNumberish value to convert.
* @returns The buffer representation of the BigNumberish value.
*/
export function beBigNumberishToBuffer(n: BigNumberish): Buffer {
export function bigNumberishToBuffer(n: BigNumberish): Buffer {
if (n instanceof Buffer) {
return n
}

return bigIntToBuffer(bigNumberishToBigInt(n))
}

/**
* Converts a BigNumberish type to a buffer, handling various input types and converting
* them to bigint first if necessary. It uses little-endian byte order.
* @param n The BigNumberish value to convert.
* @returns The buffer representation of the BigNumberish value.
*/
export function leBigNumberishToBuffer(n: BigNumberish): Buffer {
if (n instanceof Buffer) {
return Buffer.from(n).reverse()
}

return leBigIntToBuffer(bigNumberishToBigInt(n))
}

/**
* Converts a BigNumberish type to a buffer. Alias for beBigNumberishToBuffer.
* @param n The BigNumberish value to convert.
* @returns The buffer representation of the BigNumberish value.
*/
export function bigNumberishToBuffer(n: BigNumberish): Buffer {
return beBigNumberishToBuffer(n)
}
35 changes: 0 additions & 35 deletions packages/utils/tests/conversions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
bigNumberishToBuffer,
hexadecimalToBigInt,
leBigIntToBuffer,
leBigNumberishToBuffer,
leBufferToBigInt
} from "../src/conversions"

Expand Down Expand Up @@ -70,40 +69,6 @@ describe("Conversions", () => {
})
})

describe("# leBigNumberishToBuffer", () => {
it("Should convert a big numberish (number) to a LE buffer", async () => {
const n = 1234

const result = leBigNumberishToBuffer(n)

expect(result).toStrictEqual(Buffer.from([0xd2, 0x04]))
})

it("Should convert a big numberish (bigint) to a LE buffer", async () => {
const result = leBigNumberishToBuffer(testBigInt1LE)

expect(result).toStrictEqual(Buffer.from(testBytes1))
})

it("Should convert a big numberish (stringified bigint) to a LE buffer", async () => {
const result = leBigNumberishToBuffer(testBigInt1LE.toString())

expect(result).toStrictEqual(Buffer.from(testBytes1))
})

it("Should convert a big numberish (hexadecimal) to a LE buffer", async () => {
const result = leBigNumberishToBuffer(testHex1LE)

expect(result).toStrictEqual(Buffer.from(testBytes1))
})

it("Should convert a big numberish (buffer) to a LE buffer", async () => {
const result = leBigNumberishToBuffer(Buffer.from(testHex1LE.slice(2), "hex"))

expect(result).toStrictEqual(Buffer.from(testBytes1))
})
})

describe("# bigNumberishToBigInt", () => {
it("Should convert a BE big numberish (number) to a bigint", async () => {
const n = 1234
Expand Down

0 comments on commit 24e1793

Please sign in to comment.