From efa706e68c4b41162d9fe40bea0e646d2687b4ea Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Sat, 21 Feb 2015 12:53:02 -0300 Subject: [PATCH] fix signatures --- lib/client/api.js | 14 ++++++++------ test/integration/server.js | 27 ++++++++++++++------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/lib/client/api.js b/lib/client/api.js index d4e8635..bf97fa1 100644 --- a/lib/client/api.js +++ b/lib/client/api.js @@ -488,23 +488,25 @@ API.prototype.signTxProposal = function(txp, cb) { _.each(txp.inputs, function(i) { if (!derived[i.path]) { derived[i.path] = xpriv.derive(i.path).privateKey; + privs.push(derived[i.path]); } - privs.push(derived[i.path]); }); var t = new Bitcore.Transaction(); + _.each(txp.inputs, function(i) { t.from(i, i.publicKeys, txp.requiredSignatures); }); t.to(txp.toAddress, txp.amount) - .change(txp.changeAddress.address) - .sign(privs); + .change(txp.changeAddress.address); var signatures = _.map(privs, function(priv, i) { - return _.find(t.getSignatures(priv), { - inputIndex: i - }).signature.toDER().toString('hex'); + return t.getSignatures(priv); + }); + + signatures = _.map(_.sortBy(_.flatten(signatures), 'inputIndex'), function(s) { + return s.signature.toDER().toString('hex'); }); var url = '/v1/txproposals/' + txp.id + '/signatures/'; diff --git a/test/integration/server.js b/test/integration/server.js index 166c53c..7cd85e9 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -125,33 +125,34 @@ helpers.stubBroadcastFail = function() { }; -helpers.clientSign = function(tx, xprivHex) { +helpers.clientSign = function(txp, xprivHex) { //Derive proper key to sign, for each input var privs = [], derived = {}; var xpriv = new Bitcore.HDPrivateKey(xprivHex); - _.each(tx.inputs, function(i) { + _.each(txp.inputs, function(i) { if (!derived[i.path]) { derived[i.path] = xpriv.derive(i.path).privateKey; + privs.push(derived[i.path]); } - privs.push(derived[i.path]); }); var t = new Bitcore.Transaction(); - _.each(tx.inputs, function(i) { - t.from(i, i.publicKeys, tx.requiredSignatures); + _.each(txp.inputs, function(i) { + t.from(i, i.publicKeys, txp.requiredSignatures); }); - t.to(tx.toAddress, tx.amount) - .change(tx.changeAddress.address) - .sign(privs); + t.to(txp.toAddress, txp.amount) + .change(txp.changeAddress.address); var signatures = _.map(privs, function(priv, i) { - return _.find(t.getSignatures(priv), { - inputIndex: i - }).signature.toDER().toString('hex'); + return t.getSignatures(priv); + }); + + signatures = _.map(_.sortBy(_.flatten(signatures), 'inputIndex'), function(s) { + return s.signature.toDER().toString('hex'); }); return signatures; @@ -1080,7 +1081,7 @@ describe('Copay server', function() { }); }); - it.only('should sign and broadcast a tx', function(done) { + it('should sign and broadcast a tx', function(done) { helpers.stubBroadcast('1122334455'); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 10, null, TestData.copayers[0].privKey); server.createTx(txOpts, function(err, txp) { @@ -1486,7 +1487,7 @@ describe('Copay server', function() { }, function(err, notifications) { should.not.exist(err); var types = _.pluck(notifications, 'type'); - types.should.deep.equal(['NewCopayer', 'NewAddress', 'NewAddress', 'NewAddress', 'NewAddress']); + types.should.deep.equal(['NewCopayer', 'NewAddress', 'NewAddress', 'NewAddress', 'NewTxProposal']); done(); }); });