Skip to content
This repository has been archived by the owner on Jan 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #85 from ChainSafe/add-bigint
Browse files Browse the repository at this point in the history
Add bigint support
  • Loading branch information
holgerd77 authored Apr 30, 2020
2 parents f8394b3 + d1138f9 commit b89c0c7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!')
}
Expand All @@ -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')
}
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
15 changes: 15 additions & 0 deletions test/dataTypes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { version } from 'process'
import * as assert from 'assert'
import * as RLP from '../src'
const BN = require('bn.js')
Expand Down Expand Up @@ -61,6 +62,20 @@ 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)
})
})

describe('RLP encoding (BN):', function() {
it('should encode a BN value', function() {
const encodedBN = RLP.encode(new BN(3))
Expand Down

0 comments on commit b89c0c7

Please sign in to comment.