From d22539c6ab6e3ccab5f3da8ade289a170885d318 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Tue, 8 Mar 2016 20:09:21 +0000 Subject: [PATCH 1/2] Support BN.isBN in toBuffer() --- index.js | 2 ++ package.json | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) 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", From 1ec8717a5f1c439ba192be0ffbfb0c4642ec98e3 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 9 Nov 2016 23:55:28 +0000 Subject: [PATCH 2/2] Add test for obsolete (?) toArray --- test/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 () {