diff --git a/index.js b/index.js index 2645f5fa..55d112b1 100644 --- a/index.js +++ b/index.js @@ -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()) diff --git a/package.json b/package.json index 524e3f6e..c1fcea31 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/index.js b/test/index.js index f77abaac..966a6f36 100644 --- a/test/index.js +++ b/test/index.js @@ -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 () {