From 90a73e195e5fb20af5aa3571b754c73601cd0675 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 13 Apr 2018 16:06:41 +1000 Subject: [PATCH] use safe-buffer --- package.json | 2 +- src/script_signature.js | 7 ++++--- test/{signature.js => script_signature.js} | 5 +++-- 3 files changed, 8 insertions(+), 6 deletions(-) rename test/{signature.js => script_signature.js} (90%) diff --git a/package.json b/package.json index f785872..0b73b4e 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "merkle-lib": "^2.0.10", "pushdata-bitcoin": "^1.0.1", "randombytes": "^2.0.1", - "safe-buffer": "^5.0.1", + "safe-buffer": "^5.1.1", "typeforce": "^1.11.3", "varuint-bitcoin": "^1.0.4", "wif": "^2.0.1" diff --git a/src/script_signature.js b/src/script_signature.js index f628c99..29344e8 100644 --- a/src/script_signature.js +++ b/src/script_signature.js @@ -1,5 +1,6 @@ var bip66 = require('bip66') var BigInteger = require('bigi') +var Buffer = require('safe-buffer').Buffer var typeforce = require('typeforce') var types = require('./types') @@ -39,11 +40,11 @@ function encode (signature, hashType) { var hashTypeMod = hashType & ~0x80 if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType) - var hashTypeBuffer = new Buffer(1) + var hashTypeBuffer = Buffer.allocUnsafe(1) hashTypeBuffer.writeUInt8(hashType, 0) - var r = new Buffer(signature.r.toDERInteger()) - var s = new Buffer(signature.s.toDERInteger()) + var r = Buffer.from(signature.r.toDERInteger()) + var s = Buffer.from(signature.s.toDERInteger()) return Buffer.concat([ bip66.encode(r, s), diff --git a/test/signature.js b/test/script_signature.js similarity index 90% rename from test/signature.js rename to test/script_signature.js index 26b2136..d57e575 100644 --- a/test/signature.js +++ b/test/script_signature.js @@ -3,6 +3,7 @@ var assert = require('assert') var bscriptSig = require('../src/script').signature var BigInteger = require('bigi') +var Buffer = require('safe-buffer').Buffer var fixtures = require('./fixtures/signature.json') describe('Script Signatures', function () { @@ -45,7 +46,7 @@ describe('Script Signatures', function () { describe('decode', function () { fixtures.valid.forEach(function (f) { it('decodes ' + f.hex, function () { - var decode = bscriptSig.decode(new Buffer(f.hex, 'hex')) + var decode = bscriptSig.decode(Buffer.from(f.hex, 'hex')) assert.deepEqual(toRaw(decode.signature), f.raw) assert.strictEqual(decode.hashType, f.hashType) @@ -54,7 +55,7 @@ describe('Script Signatures', function () { fixtures.invalid.forEach(function (f) { it('throws on ' + f.hex, function () { - var buffer = new Buffer(f.hex, 'hex') + var buffer = Buffer.from(f.hex, 'hex') assert.throws(function () { bscriptSig.decode(buffer)