Comparing version 0.12.10 to 0.12.11
{ | ||
"name": "bitcore", | ||
"main": "./bitcore.min.js", | ||
"version": "0.12.10", | ||
"version": "0.12.11", | ||
"homepage": "http://bitcore.io", | ||
@@ -6,0 +6,0 @@ "authors": [ |
@@ -72,3 +72,7 @@ 'use strict'; | ||
data.transactions.forEach(function(tx) { | ||
transactions.push(Transaction().fromJSON(tx)); | ||
if (tx instanceof Transaction) { | ||
transactions.push(tx); | ||
} else { | ||
transactions.push(Transaction().fromJSON(tx)); | ||
} | ||
}); | ||
@@ -75,0 +79,0 @@ var info = { |
@@ -7,2 +7,3 @@ 'use strict'; | ||
var BufferUtil = require('../util/buffer'); | ||
var JSUtil = require('../util/js'); | ||
@@ -31,2 +32,3 @@ var Signature = function Signature(r, s) { | ||
obj.compressed : this.compressed; //whether the recovered pubkey is compressed | ||
this.nhashtype = obj.nhashtype || this.nhashtype || undefined; | ||
return this; | ||
@@ -289,5 +291,10 @@ }; | ||
Signature.prototype.hasDefinedHashtype = function() { | ||
if (this.nhashtype < Signature.SIGHASH_ALL || this.nhashtype > Signature.SIGHASH_SINGLE) { | ||
if (!JSUtil.isNaturalNumber(this.nhashtype)) { | ||
return false; | ||
} | ||
// accept with or without Signature.SIGHASH_ANYONECANPAY by ignoring the bit | ||
var temp = this.nhashtype & ~Signature.SIGHASH_ANYONECANPAY; | ||
if (temp < Signature.SIGHASH_ALL || temp > Signature.SIGHASH_SINGLE) { | ||
return false; | ||
} | ||
return true; | ||
@@ -294,0 +301,0 @@ }; |
@@ -841,2 +841,43 @@ 'use strict'; | ||
/** | ||
* Comes from bitcoind's script DecodOP_N function | ||
* @param {number} opcode | ||
* @returns {number} numeric value in range of 0 to 16 | ||
*/ | ||
Script.prototype._decodeOP_N = function(opcode) { | ||
if (opcode === Opcode.OP_0) { | ||
return 0; | ||
} else if (opcode >= Opcode.OP_1 && opcode <= Opcode.OP_16) { | ||
return opcode - (Opcode.OP_1 - 1); | ||
} else { | ||
throw new Error('Invalid opcode: ' + JSON.stringify(opcode)); | ||
} | ||
}; | ||
/** | ||
* Comes from bitcoind's script GetSigOpCount(boolean) function | ||
* @param {boolean} use current (true) or pre-version-0.6 (false) logic | ||
* @returns {number} number of signature operations required by this script | ||
*/ | ||
Script.prototype.getSignatureOperationsCount = function(accurate) { | ||
accurate = (_.isUndefined(accurate) ? true : accurate); | ||
var self = this; | ||
var n = 0; | ||
var lastOpcode = Opcode.OP_INVALIDOPCODE; | ||
_.each(self.chunks, function getChunk(chunk) { | ||
var opcode = chunk.opcodenum; | ||
if (opcode == Opcode.OP_CHECKSIG || opcode == Opcode.OP_CHECKSIGVERIFY) { | ||
n++; | ||
} else if (opcode == Opcode.OP_CHECKMULTISIG || opcode == Opcode.OP_CHECKMULTISIGVERIFY) { | ||
if (accurate && lastOpcode >= Opcode.OP_1 && lastOpcode <= Opcode.OP_16) { | ||
n += self._decodeOP_N(lastOpcode); | ||
} else { | ||
n += 20; | ||
} | ||
} | ||
lastOpcode = opcode; | ||
}); | ||
return n; | ||
}; | ||
module.exports = Script; |
@@ -19,10 +19,14 @@ 'use strict'; | ||
} | ||
if (JSUtil.isValidJSON(args)) { | ||
return Output.fromJSON(args); | ||
} else if (_.isObject(args)) { | ||
if (_.isObject(args)) { | ||
this.satoshis = args.satoshis; | ||
if (_.isString(args.script) && JSUtil.isHexa(args.script)) { | ||
args.script = new buffer.Buffer(args.script, 'hex'); | ||
if (bufferUtil.isBuffer(args.script)) { | ||
this._scriptBuffer = args.script; | ||
} else { | ||
if (_.isString(args.script) && JSUtil.isHexa(args.script)) { | ||
args.script = new buffer.Buffer(args.script, 'hex'); | ||
} | ||
this.setScript(args.script); | ||
} | ||
this.setScript(args.script); | ||
} else if (JSUtil.isValidJSON(args)) { | ||
return Output.fromJSON(args); | ||
} else { | ||
@@ -37,3 +41,9 @@ throw new TypeError('Unrecognized argument for Output'); | ||
get: function() { | ||
return this._script; | ||
if (this._script) { | ||
return this._script; | ||
} else { | ||
this.setScriptFromBuffer(this._scriptBuffer); | ||
return this._script; | ||
} | ||
} | ||
@@ -40,0 +50,0 @@ }); |
{ | ||
"name": "bitcore", | ||
"version": "0.12.9", | ||
"version": "0.12.10", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "bn.js": { |
{ | ||
"name": "bitcore", | ||
"version": "0.12.10", | ||
"version": "0.12.11", | ||
"description": "A pure and powerful JavaScript Bitcoin library.", | ||
@@ -5,0 +5,0 @@ "author": "BitPay <dev@bitpay.com>", |
'use strict'; | ||
var _ = require('lodash'); | ||
var should = require('chai').should(); | ||
@@ -37,2 +38,13 @@ var bitcore = require('../..'); | ||
it('should set nhashtype', function() { | ||
var sig = Signature().set({ | ||
nhashtype: Signature.SIGHASH_ALL | ||
}); | ||
sig.nhashtype.should.equal(Signature.SIGHASH_ALL); | ||
sig.set({ | ||
nhashtype: Signature.SIGHASH_ALL | Signature.SIGHASH_ANYONECANPAY | ||
}); | ||
sig.nhashtype.should.equal(Signature.SIGHASH_ALL | Signature.SIGHASH_ANYONECANPAY); | ||
}); | ||
}); | ||
@@ -101,2 +113,21 @@ | ||
describe('#toTxFormat', function() { | ||
it('should parse this known signature and rebuild it with updated zero-padded sighash types', function() { | ||
var original = '30450221008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa02200993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e7201'; | ||
var buf = new Buffer(original, 'hex'); | ||
var sig = Signature.fromTxFormat(buf); | ||
sig.nhashtype.should.equal(Signature.SIGHASH_ALL); | ||
sig.set({ | ||
nhashtype: Signature.SIGHASH_ALL | Signature.SIGHASH_ANYONECANPAY | ||
}); | ||
sig.toTxFormat().toString('hex').should.equal(original.slice(0, -2) + '81'); | ||
sig.set({ | ||
nhashtype: Signature.SIGHASH_SINGLE | ||
}); | ||
sig.toTxFormat().toString('hex').should.equal(original.slice(0, -2) + '03'); | ||
}); | ||
}); | ||
describe('#fromTxFormat', function() { | ||
@@ -113,3 +144,3 @@ | ||
it('should parse this known signature and rebuild it', function() { | ||
var hex = "3044022007415aa37ce7eaa6146001ac8bdefca0ddcba0e37c5dc08c4ac99392124ebac802207d382307fd53f65778b07b9c63b6e196edeadf0be719130c5db21ff1e700d67501"; | ||
var hex = '3044022007415aa37ce7eaa6146001ac8bdefca0ddcba0e37c5dc08c4ac99392124ebac802207d382307fd53f65778b07b9c63b6e196edeadf0be719130c5db21ff1e700d67501'; | ||
var buf = new Buffer(hex, 'hex'); | ||
@@ -269,2 +300,33 @@ var sig = Signature.fromTxFormat(buf); | ||
describe('#hasDefinedHashtype', function() { | ||
it('should reject invalid sighash types and accept valid ones', function() { | ||
var sig = new Signature(); | ||
sig.hasDefinedHashtype().should.equal(false); | ||
var testCases = [ | ||
[undefined, false], | ||
[null, false], | ||
[0, false], | ||
[1.1, false], | ||
[-1, false], | ||
[-1.1, false], | ||
['', false], | ||
['1', false], | ||
[Signature.SIGHASH_ANYONECANPAY, false], | ||
[Signature.SIGHASH_ANYONECANPAY | Signature.SIGHASH_ALL, true], | ||
[Signature.SIGHASH_ANYONECANPAY | Signature.SIGHASH_NONE, true], | ||
[Signature.SIGHASH_ANYONECANPAY | Signature.SIGHASH_SINGLE, true], | ||
[Signature.SIGHASH_ALL, true], | ||
[Signature.SIGHASH_NONE, true], | ||
[Signature.SIGHASH_SINGLE, true], | ||
[Signature.SIGHASH_SINGLE + 1, false], | ||
[(Signature.SIGHASH_ANYONECANPAY | Signature.SIGHASH_SINGLE) + 1, false], | ||
[(Signature.SIGHASH_ANYONECANPAY | Signature.SIGHASH_ALL) - 1, false], | ||
]; | ||
_.each(testCases, function(testCase) { | ||
sig.nhashtype = testCase[0]; | ||
sig.hasDefinedHashtype().should.equal(testCase[1]); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -474,3 +474,3 @@ 'use strict'; | ||
describe('#buildMultisigOut', function() { | ||
var pubkey_hexs = [ | ||
var pubKeyHexes = [ | ||
'022df8750480ad5b26950b25c7ba79d3e37d75f640f8e5d9bcd5b150a0f85014da', | ||
@@ -483,3 +483,3 @@ '03e3818b65bcc73a7d64064106a859cc1a5a728c4345ff0b641209fba0d90de6e9', | ||
]; | ||
var sortkeys = pubkey_hexs.slice(0, 3).map(PublicKey); | ||
var sortkeys = pubKeyHexes.slice(0, 3).map(PublicKey); | ||
it('should create sorted script by default', function() { | ||
@@ -506,3 +506,3 @@ var s = Script.buildMultisigOut(sortkeys, 2); | ||
var test_mn = function(m, n) { | ||
var pubkeys = pubkey_hexs.slice(0, n).map(PublicKey); | ||
var pubkeys = pubKeyHexes.slice(0, n).map(PublicKey); | ||
var s = Script.buildMultisigOut(pubkeys, m); | ||
@@ -781,3 +781,44 @@ s.isMultisigOut().should.equal(true); | ||
describe('#getSignatureOperationsCount', function() { | ||
// comes from bitcoind src/test/sigopcount_tests | ||
// only test calls to function with boolean param, not signature ref param | ||
var pubKeyHexes = [ | ||
'022df8750480ad5b26950b25c7ba79d3e37d75f640f8e5d9bcd5b150a0f85014da', | ||
'03e3818b65bcc73a7d64064106a859cc1a5a728c4345ff0b641209fba0d90de6e9', | ||
'021f2f6e1e50cb6a953935c3601284925decd3fd21bc445712576873fb8c6ebc18', | ||
]; | ||
it('should return zero for empty scripts', function() { | ||
Script().getSignatureOperationsCount(false).should.equal(0); | ||
Script().getSignatureOperationsCount(true).should.equal(0); | ||
}); | ||
it('should handle multi-sig multisig scripts from string', function() { | ||
var s1 = 'OP_1 01 FF OP_2 OP_CHECKMULTISIG'; | ||
Script(s1).getSignatureOperationsCount(true).should.equal(2); | ||
s1 += ' OP_IF OP_CHECKSIG OP_ENDIF'; | ||
Script(s1).getSignatureOperationsCount(true).should.equal(3); | ||
Script(s1).getSignatureOperationsCount(false).should.equal(21); | ||
}); | ||
it('should handle multi-sig-out scripts from utility function', function() { | ||
var sortKeys = pubKeyHexes.slice(0, 3).map(PublicKey); | ||
var s2 = Script.buildMultisigOut(sortKeys, 1); | ||
Script(s2).getSignatureOperationsCount(true).should.equal(3); | ||
Script(s2).getSignatureOperationsCount(false).should.equal(20); | ||
}); | ||
it('should handle P2SH-multisig-in scripts from utility', function() { | ||
// create a well-formed signature, does not need to match pubkeys | ||
var signature = bitcore.crypto.Signature.fromString('30060201FF0201FF'); | ||
var signatures = [ signature.toBuffer() ]; | ||
var p2sh = Script.buildP2SHMultisigIn(pubKeyHexes, 1, signatures, {}); | ||
p2sh.getSignatureOperationsCount(true).should.equal(0); | ||
p2sh.getSignatureOperationsCount(false).should.equal(0); | ||
}); | ||
it('should default the one and only argument to true', function() { | ||
var s1 = 'OP_1 01 FF OP_2 OP_CHECKMULTISIG'; | ||
var trueCount = Script(s1).getSignatureOperationsCount(true); | ||
var falseCount = Script(s1).getSignatureOperationsCount(false); | ||
var defaultCount = Script(s1).getSignatureOperationsCount(); | ||
trueCount.should.not.equal(falseCount); | ||
trueCount.should.equal(defaultCount); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12784159
153
324320