From 3888e5d640090fc96dbd54cd96650416ef39928a Mon Sep 17 00:00:00 2001 From: Cayman Date: Sat, 7 Dec 2019 13:46:06 -0600 Subject: [PATCH 1/2] Add bigint support --- src/index.ts | 6 +++--- src/types.ts | 2 +- test/dataTypes.spec.ts | 7 +++++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 89a0124..32d2875 100644 --- a/src/index.ts +++ b/src/index.ts @@ -206,7 +206,7 @@ function stripHexPrefix(str: string): string { } /** Transform an integer into its hexadecimal value */ -function intToHex(integer: number): string { +function intToHex(integer: number | bigint): string { if (integer < 0) { throw new Error('Invalid integer as argument, must be unsigned!') } @@ -220,7 +220,7 @@ function padToEven(a: string): string { } /** Transform an integer into a Buffer */ -function intToBuffer(integer: number): Buffer { +function intToBuffer(integer: number | bigint): Buffer { const hex = intToHex(integer) return Buffer.from(hex, 'hex') } @@ -234,7 +234,7 @@ function toBuffer(v: Input): Buffer { } else { return Buffer.from(v) } - } else if (typeof v === 'number') { + } else if (typeof v === 'number' || typeof v === 'bigint') { if (!v) { return Buffer.from([]) } else { diff --git a/src/types.ts b/src/types.ts index 70a8c9b..59cdabc 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,6 +1,6 @@ import BN = require('bn.js') -export type Input = Buffer | string | number | Uint8Array | BN | List | null +export type Input = Buffer | string | number | bigint | Uint8Array | BN | List | null // Use interface extension instead of type alias to // make circular declaration possible. diff --git a/test/dataTypes.spec.ts b/test/dataTypes.spec.ts index da210e8..2dfbdca 100644 --- a/test/dataTypes.spec.ts +++ b/test/dataTypes.spec.ts @@ -61,6 +61,13 @@ describe('RLP encoding (list):', function() { // }) }) +describe('RLP encoding (BigInt):', function() { + it('should encode a BigInt value', function() { + const encodedBN = RLP.encode(BigInt(3)) + assert.equal(encodedBN[0], 3) + }) +}) + describe('RLP encoding (BN):', function() { it('should encode a BN value', function() { const encodedBN = RLP.encode(new BN(3)) From d1138f973ff3ed0a3e82fe436e6e17f40fac0543 Mon Sep 17 00:00:00 2001 From: Cayman Date: Mon, 6 Apr 2020 22:28:22 -0500 Subject: [PATCH 2/2] Skip bigint test on node < v10 --- test/dataTypes.spec.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/dataTypes.spec.ts b/test/dataTypes.spec.ts index 2dfbdca..d15003d 100644 --- a/test/dataTypes.spec.ts +++ b/test/dataTypes.spec.ts @@ -1,3 +1,4 @@ +import { version } from 'process' import * as assert from 'assert' import * as RLP from '../src' const BN = require('bn.js') @@ -62,6 +63,13 @@ describe('RLP encoding (list):', function() { }) describe('RLP encoding (BigInt):', function() { + before(function() { + const nodeVersionNumber = Number(version.match(/^v([0-9]+)/)![1]) + if (nodeVersionNumber < 10) { + // tslint:disable-next-line no-invalid-this + this.skip() + } + }) it('should encode a BigInt value', function() { const encodedBN = RLP.encode(BigInt(3)) assert.equal(encodedBN[0], 3)