From 8929d3d50962e16bf492ed850a9e2365c1a47493 Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Wed, 7 Jun 2017 16:52:40 +0300 Subject: [PATCH] Add `safe-buffer` to dependencies --- lib/der.js | 21 +++++++++++---------- lib/elliptic/index.js | 19 ++++++++++--------- lib/js/bn/index.js | 9 +++++---- lib/js/ecpoint.js | 5 +++-- lib/js/ecpointg.js | 5 +++-- lib/js/index.js | 5 +++-- package.json | 9 +++++---- test/bn/index.js | 11 ++++++----- test/bn/util.js | 8 ++++---- test/ecpoint.js | 37 +++++++++++++++++++------------------ test/privatekey.js | 3 ++- test/publickey.js | 21 +++++++++++---------- test/signature.js | 9 +++++---- test/util.js | 15 ++++++++------- 14 files changed, 95 insertions(+), 82 deletions(-) diff --git a/lib/der.js b/lib/der.js index 18a9035..22cff98 100644 --- a/lib/der.js +++ b/lib/der.js @@ -1,7 +1,8 @@ 'use strict' +var Buffer = require('safe-buffer').Buffer var bip66 = require('bip66') -var EC_PRIVKEY_EXPORT_DER_COMPRESSED = new Buffer([ +var EC_PRIVKEY_EXPORT_DER_COMPRESSED = Buffer.from([ // begin 0x30, 0x81, 0xd3, 0x02, 0x01, 0x01, 0x04, 0x20, // private key @@ -23,7 +24,7 @@ var EC_PRIVKEY_EXPORT_DER_COMPRESSED = new Buffer([ 0x00 ]) -var EC_PRIVKEY_EXPORT_DER_UNCOMPRESSED = new Buffer([ +var EC_PRIVKEY_EXPORT_DER_UNCOMPRESSED = Buffer.from([ // begin 0x30, 0x82, 0x01, 0x13, 0x02, 0x01, 0x01, 0x04, 0x20, // private key @@ -49,13 +50,13 @@ var EC_PRIVKEY_EXPORT_DER_UNCOMPRESSED = new Buffer([ 0x00 ]) -var ZERO_BUFFER_32 = new Buffer([ +var ZERO_BUFFER_32 = Buffer.from([ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]) exports.privateKeyExport = function (privateKey, publicKey, compressed) { - var result = new Buffer(compressed ? EC_PRIVKEY_EXPORT_DER_COMPRESSED : EC_PRIVKEY_EXPORT_DER_UNCOMPRESSED) + var result = Buffer.from(compressed ? EC_PRIVKEY_EXPORT_DER_COMPRESSED : EC_PRIVKEY_EXPORT_DER_UNCOMPRESSED) privateKey.copy(result, compressed ? 8 : 9) publicKey.copy(result, compressed ? 181 : 214) return result @@ -103,18 +104,18 @@ exports.privateKeyImport = function (privateKey) { } exports.signatureExport = function (sigObj) { - var r = Buffer.concat([new Buffer([0]), sigObj.r]) + var r = Buffer.concat([Buffer.from([0]), sigObj.r]) for (var lenR = 33, posR = 0; lenR > 1 && r[posR] === 0x00 && !(r[posR + 1] & 0x80); --lenR, ++posR); - var s = Buffer.concat([new Buffer([0]), sigObj.s]) + var s = Buffer.concat([Buffer.from([0]), sigObj.s]) for (var lenS = 33, posS = 0; lenS > 1 && s[posS] === 0x00 && !(s[posS + 1] & 0x80); --lenS, ++posS); return bip66.encode(r.slice(posR), s.slice(posS)) } exports.signatureImport = function (sig) { - var r = new Buffer(ZERO_BUFFER_32) - var s = new Buffer(ZERO_BUFFER_32) + var r = Buffer.from(ZERO_BUFFER_32) + var s = Buffer.from(ZERO_BUFFER_32) try { var sigObj = bip66.decode(sig) @@ -133,8 +134,8 @@ exports.signatureImport = function (sig) { } exports.signatureImportLax = function (sig) { - var r = new Buffer(ZERO_BUFFER_32) - var s = new Buffer(ZERO_BUFFER_32) + var r = Buffer.from(ZERO_BUFFER_32) + var s = Buffer.from(ZERO_BUFFER_32) var length = sig.length var index = 0 diff --git a/lib/elliptic/index.js b/lib/elliptic/index.js index 55bea03..074ff3b 100644 --- a/lib/elliptic/index.js +++ b/lib/elliptic/index.js @@ -1,4 +1,5 @@ 'use strict' +var Buffer = require('safe-buffer').Buffer var createHash = require('create-hash') var BN = require('bn.js') var EC = require('elliptic').ec @@ -68,7 +69,7 @@ exports.privateKeyExport = function (privateKey, compressed) { var d = new BN(privateKey) if (d.cmp(ecparams.n) >= 0 || d.isZero()) throw new Error(messages.EC_PRIVATE_KEY_EXPORT_DER_FAIL) - return new Buffer(ec.keyFromPrivate(privateKey).getPublic(compressed, true)) + return Buffer.from(ec.keyFromPrivate(privateKey).getPublic(compressed, true)) } exports.privateKeyTweakAdd = function (privateKey, tweak) { @@ -96,14 +97,14 @@ exports.publicKeyCreate = function (privateKey, compressed) { var d = new BN(privateKey) if (d.cmp(ecparams.n) >= 0 || d.isZero()) throw new Error(messages.EC_PUBLIC_KEY_CREATE_FAIL) - return new Buffer(ec.keyFromPrivate(privateKey).getPublic(compressed, true)) + return Buffer.from(ec.keyFromPrivate(privateKey).getPublic(compressed, true)) } exports.publicKeyConvert = function (publicKey, compressed) { var pair = loadPublicKey(publicKey) if (pair === null) throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL) - return new Buffer(pair.getPublic(compressed, true)) + return Buffer.from(pair.getPublic(compressed, true)) } exports.publicKeyVerify = function (publicKey) { @@ -117,7 +118,7 @@ exports.publicKeyTweakAdd = function (publicKey, tweak, compressed) { tweak = new BN(tweak) if (tweak.cmp(ecparams.n) >= 0) throw new Error(messages.EC_PUBLIC_KEY_TWEAK_ADD_FAIL) - return new Buffer(ecparams.g.mul(tweak).add(pair.pub).encode(true, compressed)) + return Buffer.from(ecparams.g.mul(tweak).add(pair.pub).encode(true, compressed)) } exports.publicKeyTweakMul = function (publicKey, tweak, compressed) { @@ -127,7 +128,7 @@ exports.publicKeyTweakMul = function (publicKey, tweak, compressed) { tweak = new BN(tweak) if (tweak.cmp(ecparams.n) >= 0 || tweak.isZero()) throw new Error(messages.EC_PUBLIC_KEY_TWEAK_MUL_FAIL) - return new Buffer(pair.pub.mul(tweak).encode(true, compressed)) + return Buffer.from(pair.pub.mul(tweak).encode(true, compressed)) } exports.publicKeyCombine = function (publicKeys, compressed) { @@ -141,7 +142,7 @@ exports.publicKeyCombine = function (publicKeys, compressed) { for (var j = 1; j < pairs.length; ++j) point = point.add(pairs[j].pub) if (point.isInfinity()) throw new Error(messages.EC_PUBLIC_KEY_COMBINE_FAIL) - return new Buffer(point.encode(true, compressed)) + return Buffer.from(point.encode(true, compressed)) } exports.signatureNormalize = function (signature) { @@ -149,7 +150,7 @@ exports.signatureNormalize = function (signature) { var s = new BN(signature.slice(32, 64)) if (r.cmp(ecparams.n) >= 0 || s.cmp(ecparams.n) >= 0) throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL) - var result = new Buffer(signature) + var result = Buffer.from(signature) if (s.cmp(ec.nh) === 1) ecparams.n.sub(s).toArrayLike(Buffer, 'be', 32).copy(result, 32) return result @@ -225,7 +226,7 @@ exports.recover = function (message, signature, recovery, compressed) { if (sigr.isZero() || sigs.isZero()) throw new Error() var point = ec.recoverPubKey(message, sigObj, recovery) - return new Buffer(point.encode(true, compressed)) + return Buffer.from(point.encode(true, compressed)) } catch (err) { throw new Error(messages.ECDSA_RECOVER_FAIL) } @@ -243,5 +244,5 @@ exports.ecdhUnsafe = function (publicKey, privateKey, compressed) { var scalar = new BN(privateKey) if (scalar.cmp(ecparams.n) >= 0 || scalar.isZero()) throw new Error(messages.ECDH_FAIL) - return new Buffer(pair.pub.mul(scalar).encode(true, compressed)) + return Buffer.from(pair.pub.mul(scalar).encode(true, compressed)) } diff --git a/lib/js/bn/index.js b/lib/js/bn/index.js index b40e93b..34df1dc 100644 --- a/lib/js/bn/index.js +++ b/lib/js/bn/index.js @@ -1,4 +1,5 @@ 'use strict' +var Buffer = require('safe-buffer').Buffer var optimized = require('./optimized') function BN () { @@ -39,7 +40,7 @@ BN.prototype.toBuffer = function () { var w = this.words for (var i = this.length; i < 10; ++i) w[i] = 0 - return new Buffer([ + return Buffer.from([ (w[9] >>> 14) & 0xFF, (w[9] >>> 6) & 0xFF, (w[9] & 0x3F) << 2 | ((w[8] >>> 24) & 0x03), // 0, 1, 2 (w[8] >>> 16) & 0xFF, (w[8] >>> 8) & 0xFF, w[8] & 0xFF, // 3, 4, 5 @@ -644,10 +645,10 @@ BN.prototype.inspect = function () { return buffer.slice(i) } -BN.n = BN.fromBuffer(new Buffer('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141', 'hex')) +BN.n = BN.fromBuffer(Buffer.from('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141', 'hex')) BN.nh = BN.n.clone().ishrn(1) -BN.nc = BN.fromBuffer(new Buffer('000000000000000000000000000000014551231950B75FC4402DA1732FC9BEBF', 'hex')) -BN.p = BN.fromBuffer(new Buffer('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 'hex')) +BN.nc = BN.fromBuffer(Buffer.from('000000000000000000000000000000014551231950B75FC4402DA1732FC9BEBF', 'hex')) +BN.p = BN.fromBuffer(Buffer.from('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 'hex')) BN.psn = BN.p.sub(BN.n) BN.tmp = new BN() BN.tmp.words = new Array(10) diff --git a/lib/js/ecpoint.js b/lib/js/ecpoint.js index 3d064d7..a6fc041 100644 --- a/lib/js/ecpoint.js +++ b/lib/js/ecpoint.js @@ -1,4 +1,5 @@ 'use strict' +var Buffer = require('safe-buffer').Buffer var BN = require('./bn') var ECJPoint = require('./ecjpoint') @@ -57,11 +58,11 @@ ECPoint.prototype.toPublicKey = function (compressed) { var publicKey if (compressed) { - publicKey = new Buffer(33) + publicKey = Buffer.alloc(33) publicKey[0] = y.isOdd() ? 0x03 : 0x02 x.toBuffer().copy(publicKey, 1) } else { - publicKey = new Buffer(65) + publicKey = Buffer.alloc(65) publicKey[0] = 0x04 x.toBuffer().copy(publicKey, 1) y.toBuffer().copy(publicKey, 33) diff --git a/lib/js/ecpointg.js b/lib/js/ecpointg.js index a4c5ba9..0144364 100644 --- a/lib/js/ecpointg.js +++ b/lib/js/ecpointg.js @@ -1,11 +1,12 @@ 'use strict' +var Buffer = require('safe-buffer').Buffer var BN = require('./bn') var ECPoint = require('./ecpoint') var ECJPoint = require('./ecjpoint') function ECPointG () { - this.x = BN.fromBuffer(new Buffer('79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798', 'hex')) - this.y = BN.fromBuffer(new Buffer('483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8', 'hex')) + this.x = BN.fromBuffer(Buffer.from('79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798', 'hex')) + this.y = BN.fromBuffer(Buffer.from('483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8', 'hex')) this.inf = false this._precompute() diff --git a/lib/js/index.js b/lib/js/index.js index 579100a..e95f601 100644 --- a/lib/js/index.js +++ b/lib/js/index.js @@ -1,4 +1,5 @@ 'use strict' +var Buffer = require('safe-buffer').Buffer var createHash = require('create-hash') var HmacDRBG = require('drbg.js/hmac') var messages = require('../messages.json') @@ -94,7 +95,7 @@ exports.signatureNormalize = function (signature) { var s = BN.fromBuffer(signature.slice(32, 64)) if (r.isOverflow() || s.isOverflow()) throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL) - var result = new Buffer(signature) + var result = Buffer.from(signature) if (s.isHigh()) BN.n.sub(s).toBuffer().copy(result, 32) return result @@ -194,7 +195,7 @@ exports.recover = function (message, signature, recovery, compressed) { kpx = sigr.add(BN.n) } - var kpPublicKey = Buffer.concat([new Buffer([0x02 + (recovery & 0x01)]), kpx.toBuffer()]) + var kpPublicKey = Buffer.concat([Buffer.from([0x02 + (recovery & 0x01)]), kpx.toBuffer()]) var kp = ECPoint.fromPublicKey(kpPublicKey) if (kp === null) break diff --git a/package.json b/package.json index ffc8b62..a0f6bf7 100644 --- a/package.json +++ b/package.json @@ -55,11 +55,12 @@ "drbg.js": "^1.0.1", "elliptic": "^6.2.3", "nan": "^2.2.1", - "prebuild-install": "^2.0.0" + "prebuild-install": "^2.0.0", + "safe-buffer": "^5.1.0" }, "devDependencies": { "bignum": "^0.12.5", - "browserify": "^13.0.0", + "browserify": "^14.4.0", "karma": "^1.3.0", "karma-browserify": "^5.0.4", "karma-chrome-launcher": "^2.0.0", @@ -68,9 +69,9 @@ "karma-firefox-launcher": "^1.0.0", "karma-tap": "^3.1.1", "node-gyp": "^3.3.1", - "nyc": "^10.0.0", + "nyc": "^11.0.2", "prebuild": "^6.0.0", - "prebuild-ci": "^1.0.9", + "prebuild-ci": "^2.2.2", "standard": "*", "tape": "^4.5.1", "xorshift.js": "^1.0.3" diff --git a/test/bn/index.js b/test/bn/index.js index c995c2e..0d21eaf 100644 --- a/test/bn/index.js +++ b/test/bn/index.js @@ -1,4 +1,5 @@ 'use strict' +var Buffer = require('safe-buffer').Buffer var test = require('tape') var BigNum = require('bignum') @@ -129,14 +130,14 @@ test('BN', function (t) { }) t.test('0x04000000 -> 0x04000000', function (t) { - var b32 = bnUtil.fillZeros(new Buffer([0xff, 0xff, 0xff, 0xff])) + var b32 = bnUtil.fillZeros(Buffer.from([0xff, 0xff, 0xff, 0xff])) var bn = BN.fromBuffer(b32) bnUtil.testBN(t, bn.normSign(), BigNum.fromBuffer(b32)) t.end() }) t.test('-0x04000000 -> -0x04000000', function (t) { - var b32 = bnUtil.fillZeros(new Buffer([0xff, 0xff, 0xff, 0xff])) + var b32 = bnUtil.fillZeros(Buffer.from([0xff, 0xff, 0xff, 0xff])) var bn = BN.fromBuffer(b32) bn.negative = 1 bnUtil.testBN(t, bn.normSign(), BigNum.fromBuffer(b32).neg()) @@ -726,7 +727,7 @@ test('BN', function (t) { t.test('ishrn', function (t) { t.test('51 bits eq 1, shift from 0 to 26', function (t) { - var b32 = bnUtil.fillZeros(new Buffer('07ffffffffffff', 'hex')) + var b32 = bnUtil.fillZeros(Buffer.from('07ffffffffffff', 'hex')) for (var i = 0; i < 26; ++i) { bnUtil.testBN(t, BN.fromBuffer(b32).ishrn(i), BigNum.fromBuffer(b32).shiftRight(i)) } @@ -734,7 +735,7 @@ test('BN', function (t) { }) t.test('256 bits eq 1, shift from 0 to 26', function (t) { - var b32 = new Buffer(32) + var b32 = Buffer.alloc(32) b32.fill(0xff) for (var i = 0; i < 26; ++i) { bnUtil.testBN(t, BN.fromBuffer(b32).ishrn(i), BigNum.fromBuffer(b32).shiftRight(i)) @@ -1002,7 +1003,7 @@ test('BN', function (t) { }) t.test('return null for quadratic nonresidue', function (t) { - var b32 = new Buffer('16e5f9d306371e9b876f04025fb8c8ed10f8b8864119a149803357e77bcdd3b1', 'hex') + var b32 = Buffer.from('16e5f9d306371e9b876f04025fb8c8ed10f8b8864119a149803357e77bcdd3b1', 'hex') t.same(BN.fromBuffer(b32).redSqrt(), null) t.end() }) diff --git a/test/bn/util.js b/test/bn/util.js index 0d7cc90..3160fa4 100644 --- a/test/bn/util.js +++ b/test/bn/util.js @@ -1,14 +1,14 @@ 'use strict' +var Buffer = require('safe-buffer').Buffer var BigNum = require('bignum') var BN_MAX256 = BigNum.pow(2, 256).sub(1) -var N = BigNum.fromBuffer(new Buffer('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141', 'hex')) +var N = BigNum.fromBuffer(Buffer.from('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141', 'hex')) var NH = N.shiftRight(1) -var P = BigNum.fromBuffer(new Buffer('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 'hex')) +var P = BigNum.fromBuffer(Buffer.from('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 'hex')) var K = BigNum(1).shiftLeft(256).sub(P) -var ZERO_BUFFER32 = new Buffer(32) -ZERO_BUFFER32.fill(0) +var ZERO_BUFFER32 = Buffer.alloc(32, 0) function fillZeros (buffer) { return Buffer.concat([ZERO_BUFFER32, buffer]).slice(-32) diff --git a/test/ecpoint.js b/test/ecpoint.js index 987e392..380fe94 100644 --- a/test/ecpoint.js +++ b/test/ecpoint.js @@ -1,4 +1,5 @@ 'use strict' +var Buffer = require('safe-buffer').Buffer var test = require('tape') var ECPoint = require('../lib/js/ecpoint') var ECJPoint = require('../lib/js/ecjpoint') @@ -6,9 +7,9 @@ var BN = require('../lib/js/bn') var util = require('./util') -var pbuf = new Buffer('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 'hex') -var zerobuf = new Buffer('0000000000000000000000000000000000000000000000000000000000000000', 'hex') -var onebuf = new Buffer('0000000000000000000000000000000000000000000000000000000000000001', 'hex') +var pbuf = Buffer.from('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 'hex') +var zerobuf = Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex') +var onebuf = Buffer.from('0000000000000000000000000000000000000000000000000000000000000001', 'hex') test('ECPoint', function (t) { util.setSeed(util.env.seed) @@ -17,7 +18,7 @@ test('ECPoint', function (t) { t.test('length from 0 to 100 except 33 and 65', function (t) { for (var size = 0; size < 100; ++size) { if (size === 33 || size === 65) continue - var publicKey = new Buffer(size) + var publicKey = Buffer.alloc(size) t.same(ECPoint.fromPublicKey(publicKey), null) } @@ -26,7 +27,7 @@ test('ECPoint', function (t) { t.test('short key', function (t) { t.test('length eq 33, first byte from 0 to 255, but not 2 and 3', function (t) { - var publicKey = new Buffer(33) + var publicKey = Buffer.alloc(33) for (var first = 0; first < 256; ++first) { if (first === 0x02 || first === 0x03) continue publicKey[0] = first @@ -37,20 +38,20 @@ test('ECPoint', function (t) { }) t.test('x eq p', function (t) { - var publicKey = Buffer.concat([new Buffer([0x02]), pbuf]) + var publicKey = Buffer.concat([Buffer.from([0x02]), pbuf]) t.same(ECPoint.fromPublicKey(publicKey), null) t.end() }) t.test('y is quadratic nonresidue', function (t) { - var publicKey = new Buffer('02fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140', 'hex') + var publicKey = Buffer.from('02fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140', 'hex') t.same(ECPoint.fromPublicKey(publicKey), null) t.end() }) t.test('0x03 should change y sign', function (t) { - var p1 = ECPoint.fromPublicKey(Buffer.concat([new Buffer([0x02]), onebuf])) - var p2 = ECPoint.fromPublicKey(Buffer.concat([new Buffer([0x03]), onebuf])) + var p1 = ECPoint.fromPublicKey(Buffer.concat([Buffer.from([0x02]), onebuf])) + var p2 = ECPoint.fromPublicKey(Buffer.concat([Buffer.from([0x03]), onebuf])) t.notDeepEqual(p1, null) t.notDeepEqual(p2, null) @@ -64,7 +65,7 @@ test('ECPoint', function (t) { t.test('full key', function (t) { t.test('length eq 65, first byte from 0 to 255, but not 4, 6 and 7', function (t) { - var publicKey = new Buffer(65) + var publicKey = Buffer.alloc(65) for (var first = 0; first < 256; ++first) { if (first === 0x04 || first === 0x06 || first === 0x07) continue publicKey[0] = first @@ -75,31 +76,31 @@ test('ECPoint', function (t) { }) t.test('x eq p', function (t) { - var publicKey = Buffer.concat([new Buffer([0x04]), pbuf, zerobuf]) + var publicKey = Buffer.concat([Buffer.from([0x04]), pbuf, zerobuf]) t.same(ECPoint.fromPublicKey(publicKey), null) t.end() }) t.test('y eq p', function (t) { - var publicKey = Buffer.concat([new Buffer([0x04]), zerobuf, pbuf]) + var publicKey = Buffer.concat([Buffer.from([0x04]), zerobuf, pbuf]) t.same(ECPoint.fromPublicKey(publicKey), null) t.end() }) t.test('first byte is 0x06, y is event', function (t) { - var publicKey = Buffer.concat([new Buffer([0x06]), zerobuf, zerobuf]) + var publicKey = Buffer.concat([Buffer.from([0x06]), zerobuf, zerobuf]) t.same(ECPoint.fromPublicKey(publicKey), null) t.end() }) t.test('first byte is 0x06, y is odd', function (t) { - var publicKey = Buffer.concat([new Buffer([0x07]), zerobuf, onebuf]) + var publicKey = Buffer.concat([Buffer.from([0x07]), zerobuf, onebuf]) t.same(ECPoint.fromPublicKey(publicKey), null) t.end() }) t.test('x*x*x + 7 != y*y', function (t) { - var publicKey = Buffer.concat([new Buffer([0x04]), zerobuf, zerobuf]) + var publicKey = Buffer.concat([Buffer.from([0x04]), zerobuf, zerobuf]) t.same(ECPoint.fromPublicKey(publicKey), null) t.end() }) @@ -114,21 +115,21 @@ test('ECPoint', function (t) { t.test('compressed & y is even', function (t) { var p = new ECPoint(BN.fromBuffer(zerobuf), BN.fromBuffer(zerobuf)) var publicKey = p.toPublicKey(true) - t.same(publicKey, Buffer.concat([new Buffer([0x02]), zerobuf])) + t.same(publicKey, Buffer.concat([Buffer.from([0x02]), zerobuf])) t.end() }) t.test('compressed & y is odd', function (t) { var p = new ECPoint(BN.fromBuffer(zerobuf), BN.fromBuffer(onebuf)) var publicKey = p.toPublicKey(true) - t.same(publicKey, Buffer.concat([new Buffer([0x03]), zerobuf])) + t.same(publicKey, Buffer.concat([Buffer.from([0x03]), zerobuf])) t.end() }) t.test('uncompressed', function (t) { var p = new ECPoint(BN.fromBuffer(zerobuf), BN.fromBuffer(zerobuf)) var publicKey = p.toPublicKey(false) - t.same(publicKey, Buffer.concat([new Buffer([0x04]), zerobuf, zerobuf])) + t.same(publicKey, Buffer.concat([Buffer.from([0x04]), zerobuf, zerobuf])) t.end() }) diff --git a/test/privatekey.js b/test/privatekey.js index c38611a..7ea3a25 100644 --- a/test/privatekey.js +++ b/test/privatekey.js @@ -1,4 +1,5 @@ 'use strict' +var Buffer = require('safe-buffer').Buffer var BN = require('bn.js') var messages = require('../lib/messages') @@ -85,7 +86,7 @@ module.exports = function (t, secp256k1) { t.test('invalid format', function (t) { t.throws(function () { - var buffer = new Buffer([0x00]) + var buffer = Buffer.from([0x00]) secp256k1.privateKeyImport(buffer) }, new RegExp('^Error: ' + messages.EC_PRIVATE_KEY_IMPORT_DER_FAIL + '$')) t.end() diff --git a/test/publickey.js b/test/publickey.js index 6aa9cde..ea76b9e 100644 --- a/test/publickey.js +++ b/test/publickey.js @@ -1,4 +1,5 @@ 'use strict' +var Buffer = require('safe-buffer').Buffer var BN = require('bn.js') var messages = require('../lib/messages') @@ -128,7 +129,7 @@ module.exports = function (t, secp256k1) { t.test('x overflow (first byte is 0x03)', function (t) { var publicKey = Buffer.concat([ - new Buffer([ 0x03 ]), + Buffer.from([ 0x03 ]), util.ec.curve.p.toArrayLike(Buffer, 'be', 32) ]) t.false(secp256k1.publicKeyVerify(publicKey)) @@ -137,7 +138,7 @@ module.exports = function (t, secp256k1) { t.test('x overflow', function (t) { var publicKey = Buffer.concat([ - new Buffer([ 0x04 ]), + Buffer.from([ 0x04 ]), util.ec.curve.p.toArrayLike(Buffer, 'be', 32) ]) t.false(secp256k1.publicKeyVerify(publicKey)) @@ -146,8 +147,8 @@ module.exports = function (t, secp256k1) { t.test('y overflow', function (t) { var publicKey = Buffer.concat([ - new Buffer([ 0x04 ]), - new Buffer(32), + Buffer.from([ 0x04 ]), + Buffer.alloc(32), util.ec.curve.p.toArrayLike(Buffer, 'be', 32) ]) t.false(secp256k1.publicKeyVerify(publicKey)) @@ -156,8 +157,8 @@ module.exports = function (t, secp256k1) { t.test('y is even, first byte is 0x07', function (t) { var publicKey = Buffer.concat([ - new Buffer([ 0x07 ]), - new Buffer(32), + Buffer.from([ 0x07 ]), + Buffer.alloc(32), util.ec.curve.p.subn(1).toArrayLike(Buffer, 'be', 32) ]) t.false(secp256k1.publicKeyVerify(publicKey)) @@ -165,7 +166,7 @@ module.exports = function (t, secp256k1) { }) t.test('y**2 !== x*x*x + 7', function (t) { - var publicKey = Buffer.concat([new Buffer([0x04]), util.getTweak(), util.getTweak()]) + var publicKey = Buffer.concat([Buffer.from([0x04]), util.getTweak(), util.getTweak()]) t.false(secp256k1.publicKeyVerify(publicKey)) t.end() }) @@ -255,7 +256,7 @@ module.exports = function (t, secp256k1) { var tweak = util.getTweak() var publicPoint = util.ec.g.mul(new BN(privateKey)) - var publicKey = new Buffer(publicPoint.encode(null, true)) + var publicKey = Buffer.from(publicPoint.encode(null, true)) var expected = util.ec.g.mul(new BN(tweak)).add(publicPoint) var compressed = secp256k1.publicKeyTweakAdd(publicKey, tweak, true) @@ -352,7 +353,7 @@ module.exports = function (t, secp256k1) { util.repeat(t, 'random tests', util.env.repeat, function (t) { var privateKey = util.getPrivateKey() var publicPoint = util.ec.g.mul(new BN(privateKey)) - var publicKey = new Buffer(publicPoint.encode(null, true)) + var publicKey = Buffer.from(publicPoint.encode(null, true)) var tweak = util.getTweak() if (new BN(tweak).cmp(util.BN_ZERO) === 0) { @@ -427,7 +428,7 @@ module.exports = function (t, secp256k1) { t.throws(function () { var privateKey = util.getPrivateKey() var publicKey1 = util.getPublicKey(privateKey).compressed - var publicKey2 = new Buffer(publicKey1) + var publicKey2 = Buffer.from(publicKey1) publicKey2[0] = publicKey2[0] ^ 0x01 secp256k1.publicKeyCombine([publicKey1, publicKey2], true) }, new RegExp('^Error: ' + messages.EC_PUBLIC_KEY_COMBINE_FAIL + '$')) diff --git a/test/signature.js b/test/signature.js index 59558c0..c685c9f 100644 --- a/test/signature.js +++ b/test/signature.js @@ -1,4 +1,5 @@ 'use strict' +var Buffer = require('safe-buffer').Buffer var messages = require('../lib/messages') var util = require('./util') @@ -98,13 +99,13 @@ module.exports = function (t, secp256k1) { t.test('parse fail', function (t) { t.throws(function () { - secp256k1.signatureImport(new Buffer(1)) + secp256k1.signatureImport(Buffer.alloc(1)) }, new RegExp('^Error: ' + messages.ECDSA_SIGNATURE_PARSE_DER_FAIL + '$')) t.end() }) t.test('parse not bip66 signature', function (t) { - var signature = new Buffer('308002204171936738571ff75ec0c56c010f339f1f6d510ba45ad936b0762b1b2162d8020220152670567fa3cc92a5ea1a6ead11741832f8aede5ca176f559e8a46bb858e3f6', 'hex') + var signature = Buffer.from('308002204171936738571ff75ec0c56c010f339f1f6d510ba45ad936b0762b1b2162d8020220152670567fa3cc92a5ea1a6ead11741832f8aede5ca176f559e8a46bb858e3f6', 'hex') t.throws(function () { secp256k1.signatureImport(signature) }) @@ -124,13 +125,13 @@ module.exports = function (t, secp256k1) { t.test('parse fail', function (t) { t.throws(function () { - secp256k1.signatureImportLax(new Buffer(1)) + secp256k1.signatureImportLax(Buffer.alloc(1)) }, new RegExp('^Error: ' + messages.ECDSA_SIGNATURE_PARSE_DER_FAIL + '$')) t.end() }) t.test('parse not bip66 signature', function (t) { - var signature = new Buffer('308002204171936738571ff75ec0c56c010f339f1f6d510ba45ad936b0762b1b2162d8020220152670567fa3cc92a5ea1a6ead11741832f8aede5ca176f559e8a46bb858e3f6', 'hex') + var signature = Buffer.from('308002204171936738571ff75ec0c56c010f339f1f6d510ba45ad936b0762b1b2162d8020220152670567fa3cc92a5ea1a6ead11741832f8aede5ca176f559e8a46bb858e3f6', 'hex') t.doesNotThrow(function () { secp256k1.signatureImportLax(signature) }) diff --git a/test/util.js b/test/util.js index a22baab..5017a7d 100644 --- a/test/util.js +++ b/test/util.js @@ -1,4 +1,5 @@ 'use strict' +var Buffer = require('safe-buffer').Buffer var crypto = require('crypto') var BN = require('bn.js') var EC = require('elliptic').ec @@ -33,8 +34,8 @@ function getPrivateKey () { function getPublicKey (privateKey) { var publicKey = ec.keyFromPrivate(privateKey).getPublic() return { - compressed: new Buffer(publicKey.encode(null, true)), - uncompressed: new Buffer(publicKey.encode(null, false)) + compressed: Buffer.from(publicKey.encode(null, true)), + uncompressed: Buffer.from(publicKey.encode(null, false)) } } @@ -81,7 +82,7 @@ function sign (message, privateKey) { function ecdh (publicKey, privateKey) { var secret = ec.keyFromPrivate(privateKey) var point = ec.keyFromPublic(publicKey).getPublic() - var sharedSecret = new Buffer(point.mul(secret.priv).encode(null, true)) + var sharedSecret = Buffer.from(point.mul(secret.priv).encode(null, true)) return crypto.createHash('sha256').update(sharedSecret).digest() } @@ -90,17 +91,17 @@ function ecdhUnsafe (publicKey, privateKey) { var point = ec.keyFromPublic(publicKey).getPublic() var shared = point.mul(secret.priv) return { - compressed: new Buffer(shared.encode(null, true)), - uncompressed: new Buffer(shared.encode(null, false)) + compressed: Buffer.from(shared.encode(null, true)), + uncompressed: Buffer.from(shared.encode(null, false)) } } var env = { - repeat: parseInt(global.__env__ && global.__env__.RANDOM_TESTS_REPEAT || + repeat: parseInt((global.__env__ && global.__env__.RANDOM_TESTS_REPEAT) || process.env.RANDOM_TESTS_REPEAT || 1, 10), - seed: global.__env__ && global.__env__.SEED || + seed: (global.__env__ && global.__env__.SEED) || process.env.SEED || crypto.randomBytes(32) }