all tests BUT hardcoded imports working

This commit is contained in:
Matias Alejo Garcia 2014-07-29 13:09:47 -03:00
parent 0ee93403f3
commit 578d05e638
5 changed files with 135 additions and 134 deletions

View File

@ -39,7 +39,7 @@ HDPath.indicesForPath = function(path) {
return {
isChange: s[3] === '1',
index: parseInt(s[4]),
cosigner: parseInt(s[2])
copayerIndex: parseInt(s[2])
};
};

View File

@ -190,12 +190,12 @@ PublicKeyRing.prototype.getAddress = function(index, isChange, id) {
};
// Overloaded to receive a PubkeyString or a consigner index
PublicKeyRing.prototype.getIndex = function(id) {
PublicKeyRing.prototype.getHDParams = function(id) {
var copayerIndex = this.getCosigner(id);
var index = this.indexes.filter(function(i) { return i.copayerIndex == copayerIndex });
if (index.length != 1) throw new Error('no index for copayerIndex');
if (index.length != 1) throw new Error('no index for copayerIndex');
return index[0];
return index[0];
};
PublicKeyRing.prototype.pathForAddress = function(address) {
@ -214,10 +214,10 @@ PublicKeyRing.prototype.getScriptPubKeyHex = function(index, isChange, pubkey) {
//generate a new address, update index.
PublicKeyRing.prototype.generateAddress = function(isChange, pubkey) {
isChange = !!isChange;
var addrIndex = this.getIndex(pubkey);
var index = isChange ? addrIndex.getChangeIndex() : addrIndex.getReceiveIndex();
var ret = this.getAddress(index, isChange, addrIndex.copayerIndex);
addrIndex.increment(isChange);
var HDParams = this.getHDParams(pubkey);
var index = isChange ? HDParams.getChangeIndex() : HDParams.getReceiveIndex();
var ret = this.getAddress(index, isChange, HDParams.copayerIndex);
HDParams.increment(isChange);
return ret;
};
@ -393,7 +393,7 @@ PublicKeyRing.prototype.mergeIndexes = function(indexes) {
var hasChanged = false;
indexes.forEach(function(theirs) {
var mine = self.getIndex(theirs.copayerIndex);
var mine = self.getHDParams(theirs.copayerIndex);
hasChanged |= mine.merge(theirs);
});

View File

@ -118,8 +118,8 @@ describe('PublicKeyRing model', function() {
}).should.throw();
}
w2.getIndex(k.pub).getChangeIndex().should.equal(changeN);
w2.getIndex(k.pub).getReceiveIndex().should.equal(addressN);
w2.getHDParams(k.pub).getChangeIndex().should.equal(changeN);
w2.getHDParams(k.pub).getReceiveIndex().should.equal(addressN);
});
@ -174,8 +174,8 @@ describe('PublicKeyRing model', function() {
for (var i = 0; i < 2; i++)
w.generateAddress(false, k.pub);
w.getIndex(k.pub).getChangeIndex().should.equal(3);
w.getIndex(k.pub).getReceiveIndex().should.equal(2);
w.getHDParams(k.pub).getChangeIndex().should.equal(3);
w.getHDParams(k.pub).getReceiveIndex().should.equal(2);
});
it('should set backup ready', function() {
@ -251,8 +251,8 @@ describe('PublicKeyRing model', function() {
w2.merge(w).should.equal(true);
w2.requiredCopayers.should.equal(3);
w2.totalCopayers.should.equal(5);
w2.getIndex(k.pub).getChangeIndex().should.equal(2);
w2.getIndex(k.pub).getReceiveIndex().should.equal(3);
w2.getHDParams(k.pub).getChangeIndex().should.equal(2);
w2.getHDParams(k.pub).getReceiveIndex().should.equal(3);
w2.merge(w).should.equal(false);
});
@ -440,24 +440,24 @@ describe('PublicKeyRing model', function() {
});
it('#getIndex should return the right one', function() {
it('#getHDParams should return the right one', function() {
var config = {
networkName: 'livenet',
};
var p = new PublicKeyRing(config);
var i = p.getIndex(HDPath.SHARED_INDEX);
var i = p.getHDParams(HDPath.SHARED_INDEX);
should.exist(i);
i.copayerIndex.should.equal(HDPath.SHARED_INDEX);
});
it('#getIndex should throw error', function() {
it('#getHDParams should throw error', function() {
var config = {
networkName: 'livenet',
};
var p = new PublicKeyRing(config);
(function badCosigner() {
return p.getIndex(54);
return p.getHDParams(54);
}).should.throw();
});

View File

@ -23,8 +23,8 @@ var TxProposals = copay.TxProposals || require('../js/models/TxProposal');
var is_browser = (typeof process == 'undefined' || typeof process.versions === 'undefined')
var PublicKeyRing = is_browser ? copay.PublicKeyRing :
require('soop').load('../js/models/core/PublicKeyRing', {
Storage: fakeStorage
});
Storage: fakeStorage
});
var config = {
networkName: 'testnet',
@ -73,7 +73,7 @@ var vopts = {
describe('TxProposals model', function() {
var isChange = false;
var index = 0;
var addressIndex = 0;
it('verify TXs', function(done) {
@ -94,8 +94,8 @@ describe('TxProposals model', function() {
networkName: config.networkName,
});
unspentTest[0].address = pkr.getAddress(index, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange, pub);
unspentTest[0].address = pkr.getAddress(addressIndex, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(addressIndex, isChange, pub);
w.add(createTx(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789',
@ -109,9 +109,9 @@ describe('TxProposals model', function() {
var tx = b.build();
tx.isComplete().should.equal(false);
var ringIndex = pkr.getIndex(pub);
b.sign(priv2.getAll(ringIndex.getReceiveIndex(), ringIndex.getChangeIndex(), ringIndex.cosigner));
b.sign(priv3.getAll(ringIndex.getReceiveIndex(), ringIndex.getChangeIndex(), ringIndex.cosigner));
var ringIndex = pkr.getHDParams(pub);
b.sign(priv2.getAll(ringIndex.getReceiveIndex(), ringIndex.getChangeIndex(), ringIndex.copayerIndex));
b.sign(priv3.getAll(ringIndex.getReceiveIndex(), ringIndex.getChangeIndex(), ringIndex.copayerIndex));
tx = b.build();
tx.isComplete().should.equal(true);
@ -152,22 +152,23 @@ describe('TxProposals model', function() {
};
var b = new TransactionBuilder(opts)
.setUnspent(utxos)
.setOutputs([{
address: toAddress,
amountSatStr: amountSatStr,
}]);
.setUnspent(utxos)
.setOutputs([{
address: toAddress,
amountSatStr: amountSatStr,
}]);
var selectedUtxos = b.getSelectedUnspent();
var inputChainPaths = selectedUtxos.map(function(utxo) {
return pkr.pathForAddress(utxo.address);
});
var selectedUtxos = b.getSelectedUnspent();
var inputChainPaths = selectedUtxos.map(function(utxo) {
return pkr.pathForAddress(utxo.address);
});
b.setHashToScriptMap(pkr.getRedeemScriptMap(inputChainPaths));
var signRet;
if (priv) {
var pkeys = priv.getForPaths(inputChainPaths);
@ -199,8 +200,8 @@ describe('TxProposals model', function() {
var start = new Date().getTime();
var pkr = createPKR([priv]);
var ts = Date.now();
unspentTest[0].address = pkr.getAddress(index, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange, pub);
unspentTest[0].address = pkr.getAddress(addressIndex, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(addressIndex, isChange, pub);
w.add(createTx(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789',
@ -225,8 +226,8 @@ describe('TxProposals model', function() {
var pkr = createPKR([priv]);
var ts = Date.now();
unspentTest[0].address = pkr.getAddress(index, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange, pub);
unspentTest[0].address = pkr.getAddress(addressIndex, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(addressIndex, isChange, pub);
w.add(createTx(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789',
@ -274,8 +275,8 @@ describe('TxProposals model', function() {
var w = new TxProposals({
networkName: config.networkName,
});
unspentTest[0].address = pkr.getAddress(index, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange, pub);
unspentTest[0].address = pkr.getAddress(addressIndex, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(addressIndex, isChange, pub);
w.add(createTx(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789',
@ -299,8 +300,8 @@ describe('TxProposals model', function() {
networkName: config.networkName,
publicKeyRing: w.publicKeyRing,
});
unspentTest[0].address = pkr.getAddress(index, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange, pub);
unspentTest[0].address = pkr.getAddress(addressIndex, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(addressIndex, isChange, pub);
w2.add(createTx(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789',
@ -375,7 +376,7 @@ describe('TxProposals model', function() {
};
var addressToSign = pkr.generateAddress(false, pub);
unspentTest[0].address = addressToSign.toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange, pub);
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(addressIndex, isChange, pub);
var tx, txb;
var w = new TxProposals({
@ -490,8 +491,8 @@ describe('TxProposals model', function() {
var w = new TxProposals({
networkName: config.networkName,
});
unspentTest[0].address = pkr.getAddress(index, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange, pub);
unspentTest[0].address = pkr.getAddress(addressIndex, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(addressIndex, isChange, pub);
w.add(createTx(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789',
@ -511,8 +512,8 @@ describe('TxProposals model', function() {
var w2 = new TxProposals({
networkName: config.networkName,
});
unspentTest[0].address = pkr.getAddress(index, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange, pub);
unspentTest[0].address = pkr.getAddress(addressIndex, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(addressIndex, isChange, pub);
w2.add(createTx(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789',
@ -530,8 +531,8 @@ describe('TxProposals model', function() {
var w3 = new TxProposals({
networkName: config.networkName,
});
unspentTest[0].address = pkr.getAddress(index, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange, pub);
unspentTest[0].address = pkr.getAddress(addressIndex, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(addressIndex, isChange, pub);
w3.add(createTx(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789',
@ -593,8 +594,8 @@ describe('TxProposals model', function() {
});
var ts = Date.now();
unspentTest[0].address = pkr.getAddress(index, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange, pub);
unspentTest[0].address = pkr.getAddress(addressIndex, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(addressIndex, isChange, pub);
w.add(createTx(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789',
@ -682,46 +683,46 @@ describe('TxProposals model', function() {
var txpv1 = {
"creator": "0361fb4252367715405a0d27f99cc74a671133292e8d725e009536d7257c8c01b0",
"createdTs": 1406310417996,
"seenBy": {
"0361fb4252367715405a0d27f99cc74a671133292e8d725e009536d7257c8c01b0": 1406310417996,
"02ba1599c64da4d80e25985be46c50e944b65f02e2b48c930528ce763d6710158f": 1406310418162
},
"signedBy": {
"0361fb4252367715405a0d27f99cc74a671133292e8d725e009536d7257c8c01b0": 1406310417996,
"02ba1599c64da4d80e25985be46c50e944b65f02e2b48c930528ce763d6710158f": 1406310645549
},
"rejectedBy": {},
"sentTs": 1406310645873,
"sentTxid": "87296c50e8601437d63d556afb27c3b8e3819214be0a9d756d401a8286c0ec43",
"inputChainPaths": ["m/45'/0/1/1"],
"comment": "test 6",
"builderObj": {
"version": 1,
"outs": [{
"address": "mph66bnLvcn9KUSMrpikUBUZZkN2C1Z5tg",
"amountSatStr": 100
}],
"utxos": [{
"address": "2NEodmgBa4SH3VwE2asgW34vMYe8VThBZNo",
"txid": "8f8deda12dad6248e655054632a27f6891ebb37e8d2b3dd1bff87e71fd451ac7",
"vout": 1,
"ts": 1406312717,
"scriptPubKey": "a914ec7bce12d0e82a7d2b5431f6d89ca70af317f5a187",
"amount": 0.009798,
"confirmations": 0,
"confirmationsFromCache": false
}],
"opts": {
"spendUnconfirmed": true,
"remainderOut": {
"address": "2N74XAozMH3JB3XgeBkRvRw1J8TtfLTtvny"
}
},
"scriptSig": ["00483045022100f167ad33b8bef4c65af8d19c1a849d1770cc8d1e35bffebe6b5459dcbe655c7802207b37370b308ba668fe19f8e8bc462c9fbdc6c67f79900670758d228d83ea96da014730440220038ad3f4cc7b0738b593454ec189913ae4b442bc83da153d68d9a0077bd1b09102202b5728a08f302e97de61ea37280b48ccdd575f0d235c22f5e0ecac6a4ab0f46401475221024739614847d5233a46913482c17c6860194ad78abb3bf47de46223047d8a0b5821024c6dc65a52c5eaaa080b96888091544f8ab8712caa7e0b69ea4b45f6f059557452ae"],
"hashToScriptMap": {
"2NEodmgBa4SH3VwE2asgW34vMYe8VThBZNo": "5221024739614847d5233a46913482c17c6860194ad78abb3bf47de46223047d8a0b5821024c6dc65a52c5eaaa080b96888091544f8ab8712caa7e0b69ea4b45f6f059557452ae"
"creator": "0361fb4252367715405a0d27f99cc74a671133292e8d725e009536d7257c8c01b0",
"createdTs": 1406310417996,
"seenBy": {
"0361fb4252367715405a0d27f99cc74a671133292e8d725e009536d7257c8c01b0": 1406310417996,
"02ba1599c64da4d80e25985be46c50e944b65f02e2b48c930528ce763d6710158f": 1406310418162
},
"signedBy": {
"0361fb4252367715405a0d27f99cc74a671133292e8d725e009536d7257c8c01b0": 1406310417996,
"02ba1599c64da4d80e25985be46c50e944b65f02e2b48c930528ce763d6710158f": 1406310645549
},
"rejectedBy": {},
"sentTs": 1406310645873,
"sentTxid": "87296c50e8601437d63d556afb27c3b8e3819214be0a9d756d401a8286c0ec43",
"inputChainPaths": ["m/45'/0/1/1"],
"comment": "test 6",
"builderObj": {
"version": 1,
"outs": [{
"address": "mph66bnLvcn9KUSMrpikUBUZZkN2C1Z5tg",
"amountSatStr": 100
}],
"utxos": [{
"address": "2NEodmgBa4SH3VwE2asgW34vMYe8VThBZNo",
"txid": "8f8deda12dad6248e655054632a27f6891ebb37e8d2b3dd1bff87e71fd451ac7",
"vout": 1,
"ts": 1406312717,
"scriptPubKey": "a914ec7bce12d0e82a7d2b5431f6d89ca70af317f5a187",
"amount": 0.009798,
"confirmations": 0,
"confirmationsFromCache": false
}],
"opts": {
"spendUnconfirmed": true,
"remainderOut": {
"address": "2N74XAozMH3JB3XgeBkRvRw1J8TtfLTtvny"
}
},
"scriptSig": ["00483045022100f167ad33b8bef4c65af8d19c1a849d1770cc8d1e35bffebe6b5459dcbe655c7802207b37370b308ba668fe19f8e8bc462c9fbdc6c67f79900670758d228d83ea96da014730440220038ad3f4cc7b0738b593454ec189913ae4b442bc83da153d68d9a0077bd1b09102202b5728a08f302e97de61ea37280b48ccdd575f0d235c22f5e0ecac6a4ab0f46401475221024739614847d5233a46913482c17c6860194ad78abb3bf47de46223047d8a0b5821024c6dc65a52c5eaaa080b96888091544f8ab8712caa7e0b69ea4b45f6f059557452ae"],
"hashToScriptMap": {
"2NEodmgBa4SH3VwE2asgW34vMYe8VThBZNo": "5221024739614847d5233a46913482c17c6860194ad78abb3bf47de46223047d8a0b5821024c6dc65a52c5eaaa080b96888091544f8ab8712caa7e0b69ea4b45f6f059557452ae"
}
};
}
};

View File

@ -39,7 +39,7 @@ describe('Wallet model', function() {
(function() {
new Wallet(config)
}).should.
throw();
throw();
});
it('should getNetworkName', function() {
var w = cachedCreateW();
@ -266,7 +266,7 @@ describe('Wallet model', function() {
var w = cachedCreateW2();
var l = w.getAddressesStr();
for (var i = 0; i < l.length; i++)
w.addressIsOwn(l[i]).should.equal(true);
w.addressIsOwn(l[i]).should.equal(true);
w.addressIsOwn(l[0], {
excludeMain: true
@ -315,14 +315,14 @@ describe('Wallet model', function() {
o.opts.reconnectDelay = 100;
var w2 = Wallet.fromObj(o,
new Storage(config.storage),
new Network(config.network),
new Blockchain(config.blockchain));
should.exist(w2);
w2.publicKeyRing.requiredCopayers.should.equal(w.publicKeyRing.requiredCopayers);
should.exist(w2.publicKeyRing.getCopayerId);
should.exist(w2.txProposals.toObj);
should.exist(w2.privateKey.toObj);
new Storage(config.storage),
new Network(config.network),
new Blockchain(config.blockchain));
should.exist(w2);
w2.publicKeyRing.requiredCopayers.should.equal(w.publicKeyRing.requiredCopayers);
should.exist(w2.publicKeyRing.getCopayerId);
should.exist(w2.txProposals.toObj);
should.exist(w2.privateKey.toObj);
});
it('#getSecret decodeSecret', function() {
@ -339,15 +339,15 @@ describe('Wallet model', function() {
(function() {
Wallet.decodeSecret('4fp61K187CsYmjoRQC5iAdC5eGmbCRsAAXfwEwetSQgHvZs27eWKaLaNHRoKM');
}).should.not.
throw();
throw();
(function() {
Wallet.decodeSecret('4fp61K187CsYmjoRQC5iAdC5eGmbCRsAAXfwEwetSQgHvZs27eWKaLaNHRoK');
}).should.
throw();
throw();
(function() {
Wallet.decodeSecret('12345');
}).should.
throw();
throw();
});
//this test fails randomly
@ -381,14 +381,14 @@ describe('Wallet model', function() {
var w = createW();
var aiObj = {
indexes: [{
cosigner: 0,
copayerIndex: 0,
changeIndex: 3,
receiveIndex: 2
}]
};
w._handleIndexes('senderID', aiObj, true);
w.publicKeyRing.getIndex(0).getReceiveIndex(2);
w.publicKeyRing.getIndex(0).getChangeIndex(3);
w.publicKeyRing.getHDParams(0).getReceiveIndex(2);
w.publicKeyRing.getHDParams(0).getChangeIndex(3);
});
it('handle network pubKeyRings correctly', function() {
@ -405,7 +405,7 @@ describe('Wallet model', function() {
requiredCopayers: w.requiredCopayers,
totalCopayers: w.totalCopayers,
indexes: [{
cosigner: 0,
copayerIndex: 0,
changeIndex: 2,
receiveIndex: 3
}],
@ -415,8 +415,8 @@ describe('Wallet model', function() {
w._handlePublicKeyRing('senderID', {
publicKeyRing: pkrObj
}, true);
w.publicKeyRing.getIndex(0).getReceiveIndex(2);
w.publicKeyRing.getIndex(0).getChangeIndex(3);
w.publicKeyRing.getHDParams(0).getReceiveIndex(2);
w.publicKeyRing.getHDParams(0).getChangeIndex(3);
for (var i = 0; i < w.requiredCopayers; i++) {
w.publicKeyRing.toObj().copayersExtPubKeys[i].should.equal(cepk[i]);
}
@ -557,21 +557,21 @@ describe('Wallet model', function() {
});
var roundErrorChecks = [{
unspent: [1.0001],
balance: 100010000
}, {
unspent: [1.0002, 1.0003, 1.0004],
balance: 300090000
}, {
unspent: [0.000002, 1.000003, 2.000004],
balance: 300000900
}, {
unspent: [0.0001, 0.0003],
balance: 40000
}, {
unspent: [0.0001, 0.0003, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0002],
balance: 110000
},
unspent: [1.0001],
balance: 100010000
}, {
unspent: [1.0002, 1.0003, 1.0004],
balance: 300090000
}, {
unspent: [0.000002, 1.000003, 2.000004],
balance: 300000900
}, {
unspent: [0.0001, 0.0003],
balance: 40000
}, {
unspent: [0.0001, 0.0003, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0002],
balance: 110000
},
];
var roundWallet = cachedCreateW2();
@ -788,8 +788,8 @@ describe('Wallet model', function() {
// check updated all indexes
var cosignersChecked = []
updateIndex.args.forEach(function(i) {
cosignersChecked.indexOf(i[0].cosigner).should.equal(-1);
cosignersChecked.push(i[0].cosigner);
cosignersChecked.indexOf(i[0].copayerIndex).should.equal(-1);
cosignersChecked.push(i[0].copayerIndex);
});
sinon.assert.callCount(updateIndex, 4);
@ -810,7 +810,7 @@ describe('Wallet model', function() {
var index = {
changeIndex: 1,
receiveIndex: 2,
cosigner: 2,
copayerIndex: 2,
}
w.updateIndex(index, function(err) {
index.receiveIndex.should.equal(9);
@ -873,7 +873,7 @@ describe('Wallet model', function() {
(function() {
w.setAddressBook(contacts[0].address, contacts[0].label);
}).should.
throw();
throw();
});
it('should show/hide everywhere', function() {
@ -1049,11 +1049,11 @@ describe('Wallet model', function() {
var opts = {};
opts.signhash = signhash;
var txb = new TransactionBuilder(opts)
.setUnspent(utxos)
.setOutputs(outs)
.sign(['cVBtNonMyTydnS3NnZyipbduXo9KZfF1aUZ3uQHcvJB6UARZbiWG',
'cRVF68hhZp1PUQCdjr2k6aVYb2cn6uabbySDPBizAJ3PXF7vDXTL'
]);
.setUnspent(utxos)
.setOutputs(outs)
.sign(['cVBtNonMyTydnS3NnZyipbduXo9KZfF1aUZ3uQHcvJB6UARZbiWG',
'cRVF68hhZp1PUQCdjr2k6aVYb2cn6uabbySDPBizAJ3PXF7vDXTL'
]);
var txp = {
'txProposal': {
'builderObj': txb.toObj()