|
|
@ -579,48 +579,70 @@ API.prototype.getTxProposals = function(opts, cb) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
API.prototype.signTxProposal = function(txp, cb) { |
|
|
|
$.checkArgument(txp.creatorId); |
|
|
|
API.prototype._getSignaturesFor = function(txp, data) { |
|
|
|
|
|
|
|
//Derive proper key to sign, for each input
|
|
|
|
var privs = [], |
|
|
|
derived = {}; |
|
|
|
|
|
|
|
var network = new Bitcore.Address(txp.toAddress).network.name; |
|
|
|
var xpriv = new Bitcore.HDPrivateKey(data.xPrivKey, network); |
|
|
|
|
|
|
|
_.each(txp.inputs, function(i) { |
|
|
|
if (!derived[i.path]) { |
|
|
|
derived[i.path] = xpriv.derive(i.path).privateKey; |
|
|
|
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); |
|
|
|
|
|
|
|
var signatures = _.map(privs, function(priv, i) { |
|
|
|
return t.getSignatures(priv); |
|
|
|
}); |
|
|
|
|
|
|
|
signatures = _.map(_.sortBy(_.flatten(signatures), 'inputIndex'), function(s) { |
|
|
|
return s.signature.toDER().toString('hex'); |
|
|
|
}); |
|
|
|
|
|
|
|
return signatures; |
|
|
|
}; |
|
|
|
|
|
|
|
API.prototype.getSignatures = function(txp, cb) { |
|
|
|
$.checkArgument(txp.creatorId); |
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck(function(err, data) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
if (!Verifier.checkTxProposal(data, txp)) { |
|
|
|
return cb(new ServerCompromisedError('Server sent fake transaction proposal')); |
|
|
|
return cb(new ServerCompromisedError('Transaction proposal is invalid')); |
|
|
|
} |
|
|
|
|
|
|
|
//Derive proper key to sign, for each input
|
|
|
|
var privs = [], |
|
|
|
derived = {}; |
|
|
|
|
|
|
|
var network = new Bitcore.Address(txp.toAddress).network.name; |
|
|
|
var xpriv = new Bitcore.HDPrivateKey(data.xPrivKey, network); |
|
|
|
|
|
|
|
_.each(txp.inputs, function(i) { |
|
|
|
if (!derived[i.path]) { |
|
|
|
derived[i.path] = xpriv.derive(i.path).privateKey; |
|
|
|
privs.push(derived[i.path]); |
|
|
|
} |
|
|
|
}); |
|
|
|
return cb(null, self._getSignaturesFor(txp, data)); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
var t = new Bitcore.Transaction(); |
|
|
|
API.prototype.signTxProposal = function(txp, cb) { |
|
|
|
$.checkArgument(txp.creatorId); |
|
|
|
|
|
|
|
_.each(txp.inputs, function(i) { |
|
|
|
t.from(i, i.publicKeys, txp.requiredSignatures); |
|
|
|
}); |
|
|
|
var self = this; |
|
|
|
|
|
|
|
t.to(txp.toAddress, txp.amount) |
|
|
|
.change(txp.changeAddress.address); |
|
|
|
this._loadAndCheck(function(err, data) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var signatures = _.map(privs, function(priv, i) { |
|
|
|
return t.getSignatures(priv); |
|
|
|
}); |
|
|
|
if (!Verifier.checkTxProposal(data, txp)) { |
|
|
|
return cb(new ServerCompromisedError('Server sent fake transaction proposal')); |
|
|
|
} |
|
|
|
|
|
|
|
signatures = _.map(_.sortBy(_.flatten(signatures), 'inputIndex'), function(s) { |
|
|
|
return s.signature.toDER().toString('hex'); |
|
|
|
}); |
|
|
|
var signatures = txp.signatures || self._getSignaturesFor(txp, data); |
|
|
|
|
|
|
|
var url = '/v1/txproposals/' + txp.id + '/signatures/'; |
|
|
|
var args = { |
|
|
|