Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

Commit

Permalink
Merge pull request #29 from axic/patch/isbn
Browse files Browse the repository at this point in the history
Support BN.isBN in toBuffer()
  • Loading branch information
holgerd77 committed Jan 5, 2018
2 parents 3e72947 + 1ec8717 commit 757ba73
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ exports.toBuffer = function (v) {
v = exports.intToBuffer(v)
} else if (v === null || v === undefined) {
v = Buffer.allocUnsafe(0)
} else if (BN.isBN(v)) {
v = v.toArrayLike(Buffer)
} else if (v.toArray) {
// converts a BN to a Buffer
v = Buffer.from(v.toArray())
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
},
"homepage": "https://github.com/ethereumjs/ethereumjs-util",
"dependencies": {
"bn.js": "^4.8.0",
"bn.js": "^4.11.0",
"create-hash": "^1.1.2",
"ethjs-util": "^0.1.3",
"keccak": "^1.0.2",
Expand Down
4 changes: 3 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,10 @@ describe('toBuffer', function () {
assert.deepEqual(ethUtils.toBuffer(null), Buffer.allocUnsafe(0))
// undefined
assert.deepEqual(ethUtils.toBuffer(), Buffer.allocUnsafe(0))
// 'toArray'
// 'toBN'
assert.deepEqual(ethUtils.toBuffer(new BN(1)), Buffer.from([1]))
// 'toArray'
assert.deepEqual(ethUtils.toBuffer({ toArray: function () { return [ 1 ] } }), Buffer.from([1]))
})
it('should fail', function () {
assert.throws(function () {
Expand Down

0 comments on commit 757ba73

Please sign in to comment.