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

Commit 3888e5d

Browse files
committed
Add bigint support
1 parent f8394b3 commit 3888e5d

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ function stripHexPrefix(str: string): string {
206206
}
207207

208208
/** Transform an integer into its hexadecimal value */
209-
function intToHex(integer: number): string {
209+
function intToHex(integer: number | bigint): string {
210210
if (integer < 0) {
211211
throw new Error('Invalid integer as argument, must be unsigned!')
212212
}
@@ -220,7 +220,7 @@ function padToEven(a: string): string {
220220
}
221221

222222
/** Transform an integer into a Buffer */
223-
function intToBuffer(integer: number): Buffer {
223+
function intToBuffer(integer: number | bigint): Buffer {
224224
const hex = intToHex(integer)
225225
return Buffer.from(hex, 'hex')
226226
}
@@ -234,7 +234,7 @@ function toBuffer(v: Input): Buffer {
234234
} else {
235235
return Buffer.from(v)
236236
}
237-
} else if (typeof v === 'number') {
237+
} else if (typeof v === 'number' || typeof v === 'bigint') {
238238
if (!v) {
239239
return Buffer.from([])
240240
} else {

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import BN = require('bn.js')
22

3-
export type Input = Buffer | string | number | Uint8Array | BN | List | null
3+
export type Input = Buffer | string | number | bigint | Uint8Array | BN | List | null
44

55
// Use interface extension instead of type alias to
66
// make circular declaration possible.

test/dataTypes.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ describe('RLP encoding (list):', function() {
6161
// })
6262
})
6363

64+
describe('RLP encoding (BigInt):', function() {
65+
it('should encode a BigInt value', function() {
66+
const encodedBN = RLP.encode(BigInt(3))
67+
assert.equal(encodedBN[0], 3)
68+
})
69+
})
70+
6471
describe('RLP encoding (BN):', function() {
6572
it('should encode a BN value', function() {
6673
const encodedBN = RLP.encode(new BN(3))

0 commit comments

Comments
 (0)