Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
Merge pull request #25 from matiu/backports
Browse files Browse the repository at this point in the history
Backports
  • Loading branch information
matiu committed Nov 30, 2018
2 parents 3f68400 + 25fe78d commit b07691f
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 48 deletions.
9 changes: 7 additions & 2 deletions lib/transaction/input/multisig.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ var TransactionSignature = require('../signature');
/**
* @constructor
*/
function MultiSigInput(input, pubkeys, threshold, signatures) {
function MultiSigInput(input, pubkeys, threshold, signatures, opts) {
opts = opts || {};
Input.apply(this, arguments);
var self = this;
pubkeys = pubkeys || input.publicKeys;
threshold = threshold || input.threshold;
signatures = signatures || input.signatures;
this.publicKeys = _.sortBy(pubkeys, function(publicKey) { return publicKey.toString('hex'); });
if (opts.noSorting) {
this.publicKeys = pubkeys
} else {
this.publicKeys = _.sortBy(pubkeys, function(publicKey) { return publicKey.toString('hex'); });
}
$.checkState(Script.buildMultisigOut(this.publicKeys, threshold).equals(this.output.script),
'Provided public keys don\'t match to the provided output script');
this.publicKeyIndex = {};
Expand Down
10 changes: 8 additions & 2 deletions lib/transaction/input/multisigscripthash.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ var TransactionSignature = require('../signature');
/**
* @constructor
*/
function MultiSigScriptHashInput(input, pubkeys, threshold, signatures) {
function MultiSigScriptHashInput(input, pubkeys, threshold, signatures, opts) {
/* jshint maxstatements:20 */
opts = opts || {};
Input.apply(this, arguments);
var self = this;
pubkeys = pubkeys || input.publicKeys;
threshold = threshold || input.threshold;
signatures = signatures || input.signatures;
this.publicKeys = _.sortBy(pubkeys, function(publicKey) { return publicKey.toString('hex'); });
if (opts.noSorting) {
this.publicKeys = pubkeys
} else {
this.publicKeys = _.sortBy(pubkeys, function(publicKey) { return publicKey.toString('hex'); });
}
this.redeemScript = Script.buildMultisigOut(this.publicKeys, threshold);
$.checkState(Script.buildScriptHashOut(this.redeemScript).equals(this.output.script),
'Provided public keys don\'t hash to the provided output');
Expand Down
17 changes: 9 additions & 8 deletions lib/transaction/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,11 @@ Transaction.prototype._newTransaction = function() {
* @param {(Array.<Transaction~fromObject>|Transaction~fromObject)} utxo
* @param {Array=} pubkeys
* @param {number=} threshold
* @param {Object=} opts - Several options:
* - noSorting: defaults to false, if true and is multisig, don't
* sort the given public keys before creating the script
*/
Transaction.prototype.from = function(utxo, pubkeys, threshold) {
Transaction.prototype.from = function(utxo, pubkeys, threshold, opts) {
if (_.isArray(utxo)) {
var self = this;
_.each(utxo, function(utxo) {
Expand All @@ -540,7 +543,7 @@ Transaction.prototype.from = function(utxo, pubkeys, threshold) {
return this;
}
if (pubkeys && threshold) {
this._fromMultisigUtxo(utxo, pubkeys, threshold);
this._fromMultisigUtxo(utxo, pubkeys, threshold, opts);
} else {
this._fromNonP2SH(utxo);
}
Expand Down Expand Up @@ -568,7 +571,7 @@ Transaction.prototype._fromNonP2SH = function(utxo) {
}));
};

Transaction.prototype._fromMultisigUtxo = function(utxo, pubkeys, threshold) {
Transaction.prototype._fromMultisigUtxo = function(utxo, pubkeys, threshold, opts) {
$.checkArgument(threshold <= pubkeys.length,
'Number of required signatures must be greater than the number of public keys');
var clazz;
Expand All @@ -588,7 +591,7 @@ Transaction.prototype._fromMultisigUtxo = function(utxo, pubkeys, threshold) {
prevTxId: utxo.txId,
outputIndex: utxo.outputIndex,
script: Script.empty()
}, pubkeys, threshold));
}, pubkeys, threshold, opts));
};

/**
Expand Down Expand Up @@ -816,13 +819,11 @@ Transaction.prototype._getOutputAmount = function() {
*/
Transaction.prototype._getInputAmount = function() {
if (_.isUndefined(this._inputAmount)) {
var self = this;
this._inputAmount = 0;
_.each(this.inputs, function(input) {
this._inputAmount = _.sumBy(this.inputs, function(input) {
if (_.isUndefined(input.output)) {
throw new errors.Transaction.Input.MissingPreviousOutput();
}
self._inputAmount += input.output.satoshis;
return input.output.satoshis;
});
}
return this._inputAmount;
Expand Down
41 changes: 27 additions & 14 deletions test/block/block.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 27 additions & 16 deletions test/block/blockheader.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,34 @@ var dataRawId = '000000000b99b16390660d79fcc138d2ad0c89a0d044c4201a02bdf1f61ffa1
var data = require('../data/blk86756-testnet');

describe('BlockHeader', function() {

var version = data.version;
var prevblockidbuf = new Buffer(data.prevblockidhex, 'hex');
var merklerootbuf = new Buffer(data.merkleroothex, 'hex');
var time = data.time;
var bits = data.bits;
var nonce = data.nonce;
var bh = new BlockHeader({
version: version,
prevHash: prevblockidbuf,
merkleRoot: merklerootbuf,
time: time,
bits: bits,
nonce: nonce
var version;
var prevblockidbuf;
var merklerootbuf;
var time;
var bits;
var nonce;
var bh;
var bhhex;
var bhbuf;

before(function () {
version = data.version;
prevblockidbuf = new Buffer(data.prevblockidhex, 'hex');
merklerootbuf = new Buffer(data.merkleroothex, 'hex');
time = data.time;
bits = data.bits;
nonce = data.nonce;
bh = new BlockHeader({
version: version,
prevHash: prevblockidbuf,
merkleRoot: merklerootbuf,
time: time,
bits: bits,
nonce: nonce
});
bhhex = data.blockheaderhex;
bhbuf = new Buffer(bhhex, 'hex');
});
var bhhex = data.blockheaderhex;
var bhbuf = new Buffer(bhhex, 'hex');

it('should make a new blockheader', function() {
BlockHeader(bhbuf).toBuffer().toString('hex').should.equal(bhhex);
Expand Down
18 changes: 12 additions & 6 deletions test/block/merkleblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ var transactionVector = require('../data/tx_creation');


describe('MerkleBlock', function() {
var blockhex = data.HEX[0];
var blockbuf = new Buffer(blockhex,'hex');
var blockJSON = JSON.stringify(data.JSON[0]);
var blockObject = JSON.parse(JSON.stringify(data.JSON[0]));
var blockhex;
var blockbuf;
var blockJSON;
var blockObject;

before(function() {
blockhex = data.HEX[0];
blockbuf = new Buffer(blockhex,'hex');
blockJSON = JSON.stringify(data.JSON[0]);
blockObject = JSON.parse(JSON.stringify(data.JSON[0]));
});

describe('#constructor', function() {
it('should make a new merkleblock from buffer', function() {
Expand Down Expand Up @@ -155,7 +162,7 @@ describe('MerkleBlock', function() {
describe('#filterdTxsHash', function() {

it('should validate good merkleblocks', function() {
var hashOfFilteredTx = '6f64fd5aa9dd01f74c03656d376625cf80328d83d9afebe60cc68b8f0e245bd9'
var hashOfFilteredTx = '6f64fd5aa9dd01f74c03656d376625cf80328d83d9afebe60cc68b8f0e245bd9'
var b = MerkleBlock(data.JSON[3]);
b.filterdTxsHash()[0].should.equal(hashOfFilteredTx);
});
Expand Down Expand Up @@ -227,4 +234,3 @@ describe('MerkleBlock', function() {
});

});

0 comments on commit b07691f

Please sign in to comment.