onSignature

This commit is contained in:
Matias Alejo Garcia 2014-11-26 17:25:57 -03:00
commit e49a61980e
4 changed files with 174 additions and 71 deletions

View file

@ -38,9 +38,12 @@ function TxProposal(opts) {
this.comment = opts.comment || null;
this.readonly = opts.readonly || null;
this.merchant = opts.merchant || null;
this.paymentAckMemo = null;
this.paymentAckMemo = opts.paymentAckMemo || null;
this.paymentProtocolURL = opts.paymentProtocolURL || null;
// not from obj
this._pubkeysForScriptCache = {};
// New Tx Proposal
if (_.isEmpty(this.seenBy) && opts.creator) {
var now = Date.now();
@ -93,13 +96,32 @@ TxProposal.prototype.isFullySigned = function() {
return this.builder && this.builder.isFullySigned();
};
TxProposal.prototype.getMySignatures = function() {
preconditions.checkState(this._mySignatures, 'Still no signatures from us');
return _.clone(this._mySignatures);
};
TxProposal.prototype._setMySignatures = function(signaturesBefore) {
var mySigs = [];
_.each(this.getSignatures(), function(signatures, index) {
var diff = _.difference(signatures, signaturesBefore[index]);
preconditions.checkState(diff.length == 1, 'more that one signature added!');
mySigs.push(diff[0].toString('hex'));
})
this._mySignatures = mySigs;
return;
};
TxProposal.prototype.sign = function(keys, signerId) {
var before = this.countSignatures();
var signaturesBefore = this.getSignatures();
this.builder.sign(keys);
var signaturesAdded = this.countSignatures() > before;
if (signaturesAdded) {
this.signedBy[signerId] = Date.now();
this._setMySignatures(signaturesBefore);
}
return signaturesAdded;
};
@ -153,10 +175,14 @@ TxProposal.prototype.addMerchantData = function(merchantData) {
this._checkPayPro();
};
TxProposal.prototype.getScriptSigs = function() {
var tx = this.builder.build();
var sigs = _.map(tx.ins, function(value) {
value.s.toString('hex');
TxProposal.prototype.getSignatures = function() {
var ins = this.builder.build().ins;
var sigs = _.map(ins, function(value) {
var script = new bitcore.Script(value.s);
var nchunks = script.chunks.length;
return _.map(script.chunks.slice(1, nchunks - 1), function(buffer) {
return buffer.toString('hex');
});
});
return sigs;
@ -185,36 +211,42 @@ TxProposal.prototype.isPending = function(maxRejectCount) {
* getSignersPubKey
* @desc get Pubkeys of signers, for each input
*
* @return {string[][]} array of arrays for pubkeys for each input
* @return {string[][]} array of hashes for signing pubkeys for each input
*/
TxProposal.prototype.getSignersPubKeys = function(forceUpdate) {
var self = this;
var signersPubKey = [];
if (!this._signersPubKey || forceUpdate) {
if (!self._signersPubKey || forceUpdate) {
log.debug('Verifing signatures...');
log.debug('PERFORMANCE WARN: Verifying *all* TX signatures:', self.getId());
var tx = this.builder.build();
var tx = self.builder.build();
_.each(tx.ins, function(input, index) {
var scriptSig = new Script(input.s);
var signatureCount = scriptSig.countSignatures();
if (!self._pubkeysForScriptCache[input.s]) {
var scriptSig = new Script(input.s);
var signatureCount = scriptSig.countSignatures();
var info = TxProposal._infoFromRedeemScript(scriptSig);
var txSigHash = tx.hashForSignature(info.script, parseInt(index), Transaction.SIGHASH_ALL);
var inputSignersPubKey = TxProposal._verifySignatures(info.keys, scriptSig, txSigHash);
var info = TxProposal._infoFromRedeemScript(scriptSig);
var txSigHash = tx.hashForSignature(info.script, parseInt(index), Transaction.SIGHASH_ALL);
var inputSignersPubKey = TxProposal._verifySignatures(info.keys, scriptSig, txSigHash);
// Does scriptSig has strings that are not signatures?
if (inputSignersPubKey.length !== signatureCount)
throw new Error('Invalid signature');
// Does scriptSig has strings that are not signatures?
if (inputSignersPubKey.length !== signatureCount)
throw new Error('Invalid signature');
signersPubKey[index] = inputSignersPubKey;
self._pubkeysForScriptCache[input.s] = inputSignersPubKey;
}
signersPubKey[index] = self._pubkeysForScriptCache[input.s];
});
this._signersPubKey = signersPubKey;
self._signersPubKey = signersPubKey;
}
return this._signersPubKey;
return self._signersPubKey;
};
TxProposal.prototype.getId = function() {
@ -229,6 +261,7 @@ TxProposal.prototype.getId = function() {
TxProposal.prototype.toObj = function() {
var o = JSON.parse(JSON.stringify(this));
delete o['builder'];
delete o['_pubkeysForScriptCache'];
o.builderObj = this.builder.toObj();
return o;
};
@ -313,6 +346,8 @@ TxProposal._verifySignatures = function(inKeys, scriptSig, txSigHash) {
for (var i = 1; i <= scriptSig.countSignatures(); i++) {
var chunk = scriptSig.chunks[i];
var sigRaw = new Buffer(chunk.slice(0, chunk.length - 1));
log.debug('\t Verifying CHUNK:', i);
for (var j in keys) {
var k = keys[j];
if (k.keyObj.verifySignatureSync(txSigHash, sigRaw)) {

View file

@ -337,34 +337,6 @@ Wallet.prototype._onPublicKeyRing = function(senderId, data) {
}
};
/**
* @desc
* Demultiplexes calls to update TxProposal updates
*
* @param {string} senderId - the copayer that sent this event
* @param {Object} m - the data received
* @emits txProposalEvent
*/
Wallet.prototype._processProposalEvents = function(senderId, m) {
var ev;
if (m) {
if (m.new) {
ev = {
type: 'new',
cId: senderId
}
} else if (m.newCopayer && m.newCopayer.length) {
ev = {
type: 'signed',
cId: m.newCopayer[0]
};
}
}
if (ev)
this.emitAndKeepAlive('txProposalEvent', ev);
};
/**
* @desc
* Retrieves a keymap from a transaction proposal set extracts a maps from
@ -373,12 +345,12 @@ Wallet.prototype._processProposalEvents = function(senderId, m) {
* @param {TxProposals} txp - the transaction proposals
* @return {Object} [pubkey] -> copayerId
*/
Wallet.prototype._getKeyMap = function(txp) {
Wallet.prototype._getPubkeyToCopayerMap = function(txp) {
preconditions.checkArgument(txp);
var inSig0, keyMapAll = {},
self = this;
var signersPubKeys = txp.getSignersPubKeys();
var signersPubKeys = txp.getSignersPubKeys();
_.each(signersPubKeys, function(inputSignersPubKey, i) {
var keyMap = self.publicKeyRing.copayersForPubkeys(inputSignersPubKey, txp.inputChainPaths);
@ -520,7 +492,7 @@ Wallet.prototype._processIncomingNewTxProposal = function(txp, cb) {
/**
* @desc
* Handles a 'TXPROPOSAL' network message
* Handles a NEW 'TXPROPOSAL' network message
*
* @param {string} senderId - the id of the sender
* @param {Object} data - the data received
@ -538,7 +510,6 @@ Wallet.prototype._onTxProposal = function(senderId, data) {
if (localTx) {
log.debug('Ignoring existing tx Proposal:' + incomingNtxid);
console.log('')
return;
}
@ -548,7 +519,7 @@ Wallet.prototype._onTxProposal = function(senderId, data) {
return;
}
var keyMap = self._getKeyMap(incomingTx);
var keyMap = self._getPubkeyToCopayerMap(incomingTx);
incomingTx.setCopayers(senderId, keyMap);
self.txProposals.add(incomingTx);
@ -559,6 +530,27 @@ Wallet.prototype._onTxProposal = function(senderId, data) {
});
};
Wallet.prototype._onSignatures = function(senderId, data) {
var self = this;
try {
var localTx = this.txProposals.get(data.ntxid);
} catch (e) {
log.info('Ignoring signature for unknown tx Proposal:' + data.ntxid);
return;
};
var keyMap = self._getPubkeyToCopayerMap(locaTx);
// TODO look senderIdin keyMap
localTx.addSignature(pubkeys, data.signature, keyMap);
this.emitAndKeepAlive('txProposalEvent', {
type: 'signed',
cId: senderId,
});
};
/**
* @desc
* Handle a REJECT message received
@ -730,6 +722,9 @@ Wallet.prototype._onData = function(senderId, data, ts) {
case 'txProposal':
this._onTxProposal(senderId, data);
break;
case 'signature':
this._onSignature(senderId, data);
break;
case 'indexes':
this._onIndexes(senderId, data);
break;
@ -1266,8 +1261,8 @@ Wallet.prototype.sendSignature = function(ntxid) {
var txp = this.txProposals.get(ntxid);
this._sendToPeers(null, {
type: 'sign',
signatures: txp.getScriptSigs(),
type: 'signature',
signatures: txp.getMySignatures(),
walletId: this.id,
});
};