test in Txproposals working again

This commit is contained in:
Matias Alejo Garcia 2014-08-01 01:09:46 -03:00
commit 5d2b50f77f
8 changed files with 72 additions and 47 deletions

View file

@ -13,10 +13,10 @@ var preconditions = require('preconditions').instance();
function TxProposal(opts) {
preconditions.checkArgument(opts);
preconditions.checkArgument(opts.inputChainPaths);
preconditions.checkArgument(opts.creator);
preconditions.checkArgument(opts.createdTs);
preconditions.checkArgument(opts.builder);
preconditions.checkArgument(opts.inputChainPaths,'no inputChainPaths');
preconditions.checkArgument(opts.creator,'no creator');
preconditions.checkArgument(opts.createdTs,'no createdTs');
preconditions.checkArgument(opts.builder,'no builder');
this.creator = opts.creator;
@ -53,9 +53,14 @@ TxProposal.prototype.setSent = function(sentTxid) {
};
TxProposal.fromObj = function(o, forceOpts) {
console.log('[TxProposal.js.56]'); //TODO
preconditions.checkArgument(o.builderObj);
console.log('[TxProposal.js.59]'); //TODO
delete o['builder'];
console.log('[TxProposal.js.62]'); //TODO
try {
// force opts is requested.
for (var k in forceOpts) {
@ -63,14 +68,20 @@ TxProposal.fromObj = function(o, forceOpts) {
}
o.builder = TransactionBuilder.fromObj(o.builderObj);
} catch (e) {
console.log('[TxProposal.js.71]'); //TODO
if (!o.version) {
o.builder = new BuilderMockV0(o.builderObj);
o.readonly = 1;
};
}
console.log('[TxProposal.js.78]', o); //TODO
var t = new TxProposal(o);
t._check();
t._updateSignedBy();
console.log('[TxProposal.js.78]'); //TODO
return t;
};
@ -114,13 +125,14 @@ TxProposal._verifySignatures = function(inKeys, scriptSig, txSigHash) {
};
TxProposal._infoFromRedeemScript = function(s) {
console.log('[TxProposal.js.127]',s.getBuffer().toString('hex')); //TODO
var redeemScript = new Script(s.chunks[s.chunks.length - 1]);
if (!redeemScript)
throw new Error('Bad scriptSig');
throw new Error('Bad scriptSig (no redeemscript)');
var pubkeys = redeemScript.capture();
if (!pubkeys || !pubkeys.length)
throw new Error('Bad scriptSig');
throw new Error('Bad scriptSig (no pubkeys)');
return {
keys: pubkeys,

View file

@ -12,7 +12,7 @@ var buffertools = bitcore.buffertools;
var preconditions = require('preconditions').instance();
function TxProposalsSet(opts) {
function TxProposals(opts) {
opts = opts || {};
this.walletId = opts.walletId;
this.network = opts.networkName === 'livenet' ?
@ -20,8 +20,8 @@ function TxProposalsSet(opts) {
this.txps = {};
}
TxProposalsSet.fromObj = function(o, forceOpts) {
var ret = new TxProposalsSet({
TxProposals.fromObj = function(o, forceOpts) {
var ret = new TxProposals({
networkName: o.networkName,
walletId: o.walletId,
});
@ -36,11 +36,11 @@ TxProposalsSet.fromObj = function(o, forceOpts) {
return ret;
};
TxProposalsSet.prototype.getNtxids = function() {
TxProposals.prototype.getNtxids = function() {
return Object.keys(this.txps);
};
TxProposalsSet.prototype.toObj = function() {
TxProposals.prototype.toObj = function() {
var ret = [];
for (var id in this.txps) {
var t = this.txps[id];
@ -55,15 +55,7 @@ TxProposalsSet.prototype.toObj = function() {
};
TxProposalsSet.prototype.mergeFromObj = function(txProposalObj, allowedPubKeys, opts) {
var inTxp = TxProposal.fromObj(txProposalObj, opts);
var mergeInfo = this.txProposals.merge(inTxp, allowedPubKeys);
mergeInfo.inTxp = inTxp;
return mergeInfo;
};
TxProposalsSet.prototype.merge = function(inTxp, allowedPubKeys) {
TxProposals.prototype.merge = function(inTxp, allowedPubKeys) {
var myTxps = this.txps;
var ntxid = inTxp.getId();
@ -75,7 +67,8 @@ TxProposalsSet.prototype.merge = function(inTxp, allowedPubKeys) {
var v0 = myTxps[ntxid];
var v1 = inTxp;
ret = v0.merge(v1, allowedPubKeys);
} else {
}
else {
this.txps[ntxid] = inTxp;
ret.hasChanged = true;
ret.events.push({
@ -87,21 +80,31 @@ TxProposalsSet.prototype.merge = function(inTxp, allowedPubKeys) {
return ret;
};
TxProposals.prototype.mergeFromObj = function(txProposalObj, allowedPubKeys, opts) {
var inTxp = TxProposal.fromObj(txProposalObj, opts);
var mergeInfo = this.merge(inTxp, allowedPubKeys);
mergeInfo.inTxp = inTxp;
return mergeInfo;
};
// Add a LOCALLY CREATED (trusted) tx proposal
TxProposalsSet.prototype.add = function(data) {
TxProposals.prototype.add = function(data) {
var txp = new TxProposal(data);
var ntxid = txp.getId();
this.txps[ntxid] = txp;
return ntxid;
};
TxProposalsSet.prototype.setSent = function(ntxid, txid) {
//sent TxProposalsSet are local an not broadcasted.
TxProposals.prototype.setSent = function(ntxid, txid) {
//sent TxProposals are local an not broadcasted.
this.txps[ntxid].setSent(txid);
};
TxProposalsSet.prototype.getTxProposal = function(ntxid, copayers) {
TxProposals.prototype.getTxProposal = function(ntxid, copayers) {
var txp = this.txps[ntxid];
var i = JSON.parse(JSON.stringify(txp));
i.builder = txp.builder;
@ -139,7 +142,7 @@ TxProposalsSet.prototype.getTxProposal = function(ntxid, copayers) {
};
//returns the unspent txid-vout used in PENDING Txs
TxProposalsSet.prototype.getUsedUnspent = function(maxRejectCount) {
TxProposals.prototype.getUsedUnspent = function(maxRejectCount) {
var ret = {};
for (var i in this.txps) {
var u = this.txps[i].builder.getSelectedUnspent();
@ -154,4 +157,4 @@ TxProposalsSet.prototype.getUsedUnspent = function(maxRejectCount) {
return ret;
};
module.exports = TxProposalsSet;
module.exports = TxProposals;

View file

@ -17,7 +17,7 @@ var Address = bitcore.Address;
var HDParams = require('./HDParams');
var PublicKeyRing = require('./PublicKeyRing');
var TxProposalsSet = require('./TxProposalsSet');
var TxProposals = require('./TxProposals');
var PrivateKey = require('./PrivateKey');
var copayConfig = require('../../../config');
@ -132,13 +132,14 @@ Wallet.prototype._handlePublicKeyRing = function(senderId, data, isInbound) {
Wallet.prototype._handleTxProposal = function(senderId, data) {
this.log('RECV TXPROPOSAL: ', data);
var mergeInfo;
try {
mergeInfo = this.txProposals.mergeFromObj(data.txProposal, senderId, Wallet.builderOpts);
} catch (e) {
console.log('[Wallet.js.141]',e); //TODO
var corruptEvent = {
type: 'corrupt',
cId: mergeInfo.inTxp.creator
cId: senderId,
error: e,
};
this.emit('txProposalEvent', corruptEvent);
return;
@ -386,7 +387,7 @@ Wallet.fromObj = function(o, storage, network, blockchain) {
opts.addressBook = o.addressBook;
opts.publicKeyRing = PublicKeyRing.fromObj(o.publicKeyRing);
opts.txProposals = TxProposalsSet.fromObj(o.txProposals, Wallet.builderOpts);
opts.txProposals = TxProposals.fromObj(o.txProposals, Wallet.builderOpts);
opts.privateKey = PrivateKey.fromObj(o.privateKey);
opts.storage = storage;
@ -494,7 +495,7 @@ Wallet.prototype.generateAddress = function(isChange, cb) {
};
Wallet.prototype.getTxProposalsSet = function() {
Wallet.prototype.getTxProposals = function() {
var ret = [];
var copayers = this.getRegisteredCopayerIds();
for (var ntxid in this.txProposals.txps) {

View file

@ -2,7 +2,7 @@
var imports = require('soop').imports();
var TxProposalsSet = require('./TxProposalsSet');
var TxProposals = require('./TxProposals');
var PublicKeyRing = require('./PublicKeyRing');
var PrivateKey = require('./PrivateKey');
var Wallet = require('./Wallet');