Skip to content

Commit

Permalink
Add safe-buffer to dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Jun 7, 2017
1 parent 5e6ddc0 commit 8929d3d
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 82 deletions.
21 changes: 11 additions & 10 deletions lib/der.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
19 changes: 10 additions & 9 deletions lib/elliptic/index.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -141,15 +142,15 @@ 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) {
var r = new BN(signature.slice(0, 32))
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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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))
}
9 changes: 5 additions & 4 deletions lib/js/bn/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
var Buffer = require('safe-buffer').Buffer
var optimized = require('./optimized')

function BN () {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions lib/js/ecpoint.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
var Buffer = require('safe-buffer').Buffer
var BN = require('./bn')
var ECJPoint = require('./ecjpoint')

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions lib/js/ecpointg.js
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
5 changes: 3 additions & 2 deletions lib/js/index.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
11 changes: 6 additions & 5 deletions test/bn/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
var Buffer = require('safe-buffer').Buffer
var test = require('tape')
var BigNum = require('bignum')

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -726,15 +727,15 @@ 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))
}
t.end()
})

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))
Expand Down Expand Up @@ -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()
})
Expand Down
8 changes: 4 additions & 4 deletions test/bn/util.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Loading

0 comments on commit 8929d3d

Please sign in to comment.