all tests BUT hardcoded imports working

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

View file

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

View file

@ -190,7 +190,7 @@ PublicKeyRing.prototype.getAddress = function(index, isChange, id) {
}; };
// Overloaded to receive a PubkeyString or a consigner index // 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 copayerIndex = this.getCosigner(id);
var index = this.indexes.filter(function(i) { return i.copayerIndex == copayerIndex }); 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');
@ -214,10 +214,10 @@ PublicKeyRing.prototype.getScriptPubKeyHex = function(index, isChange, pubkey) {
//generate a new address, update index. //generate a new address, update index.
PublicKeyRing.prototype.generateAddress = function(isChange, pubkey) { PublicKeyRing.prototype.generateAddress = function(isChange, pubkey) {
isChange = !!isChange; isChange = !!isChange;
var addrIndex = this.getIndex(pubkey); var HDParams = this.getHDParams(pubkey);
var index = isChange ? addrIndex.getChangeIndex() : addrIndex.getReceiveIndex(); var index = isChange ? HDParams.getChangeIndex() : HDParams.getReceiveIndex();
var ret = this.getAddress(index, isChange, addrIndex.copayerIndex); var ret = this.getAddress(index, isChange, HDParams.copayerIndex);
addrIndex.increment(isChange); HDParams.increment(isChange);
return ret; return ret;
}; };
@ -393,7 +393,7 @@ PublicKeyRing.prototype.mergeIndexes = function(indexes) {
var hasChanged = false; var hasChanged = false;
indexes.forEach(function(theirs) { indexes.forEach(function(theirs) {
var mine = self.getIndex(theirs.copayerIndex); var mine = self.getHDParams(theirs.copayerIndex);
hasChanged |= mine.merge(theirs); hasChanged |= mine.merge(theirs);
}); });

View file

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

View file

@ -24,7 +24,7 @@ var is_browser = (typeof process == 'undefined' || typeof process.versions === '
var PublicKeyRing = is_browser ? copay.PublicKeyRing : var PublicKeyRing = is_browser ? copay.PublicKeyRing :
require('soop').load('../js/models/core/PublicKeyRing', { require('soop').load('../js/models/core/PublicKeyRing', {
Storage: fakeStorage Storage: fakeStorage
}); });
var config = { var config = {
networkName: 'testnet', networkName: 'testnet',
@ -73,7 +73,7 @@ var vopts = {
describe('TxProposals model', function() { describe('TxProposals model', function() {
var isChange = false; var isChange = false;
var index = 0; var addressIndex = 0;
it('verify TXs', function(done) { it('verify TXs', function(done) {
@ -94,8 +94,8 @@ describe('TxProposals model', function() {
networkName: config.networkName, networkName: config.networkName,
}); });
unspentTest[0].address = pkr.getAddress(index, isChange, pub).toString(); unspentTest[0].address = pkr.getAddress(addressIndex, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange, pub); unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(addressIndex, isChange, pub);
w.add(createTx( w.add(createTx(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt', '15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789', '123456789',
@ -109,9 +109,9 @@ describe('TxProposals model', function() {
var tx = b.build(); var tx = b.build();
tx.isComplete().should.equal(false); tx.isComplete().should.equal(false);
var ringIndex = pkr.getIndex(pub); var ringIndex = pkr.getHDParams(pub);
b.sign(priv2.getAll(ringIndex.getReceiveIndex(), ringIndex.getChangeIndex(), ringIndex.cosigner)); b.sign(priv2.getAll(ringIndex.getReceiveIndex(), ringIndex.getChangeIndex(), ringIndex.copayerIndex));
b.sign(priv3.getAll(ringIndex.getReceiveIndex(), ringIndex.getChangeIndex(), ringIndex.cosigner)); b.sign(priv3.getAll(ringIndex.getReceiveIndex(), ringIndex.getChangeIndex(), ringIndex.copayerIndex));
tx = b.build(); tx = b.build();
tx.isComplete().should.equal(true); tx.isComplete().should.equal(true);
@ -163,11 +163,12 @@ describe('TxProposals model', function() {
}); });
var selectedUtxos = b.getSelectedUnspent(); var selectedUtxos = b.getSelectedUnspent();
var inputChainPaths = selectedUtxos.map(function(utxo) { var inputChainPaths = selectedUtxos.map(function(utxo) {
return pkr.pathForAddress(utxo.address); return pkr.pathForAddress(utxo.address);
}); });
b.setHashToScriptMap(pkr.getRedeemScriptMap(inputChainPaths));
b.setHashToScriptMap(pkr.getRedeemScriptMap(inputChainPaths));
var signRet; var signRet;
if (priv) { if (priv) {
var pkeys = priv.getForPaths(inputChainPaths); var pkeys = priv.getForPaths(inputChainPaths);
@ -199,8 +200,8 @@ describe('TxProposals model', function() {
var start = new Date().getTime(); var start = new Date().getTime();
var pkr = createPKR([priv]); var pkr = createPKR([priv]);
var ts = Date.now(); var ts = Date.now();
unspentTest[0].address = pkr.getAddress(index, isChange, pub).toString(); unspentTest[0].address = pkr.getAddress(addressIndex, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange, pub); unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(addressIndex, isChange, pub);
w.add(createTx( w.add(createTx(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt', '15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789', '123456789',
@ -225,8 +226,8 @@ describe('TxProposals model', function() {
var pkr = createPKR([priv]); var pkr = createPKR([priv]);
var ts = Date.now(); var ts = Date.now();
unspentTest[0].address = pkr.getAddress(index, isChange, pub).toString(); unspentTest[0].address = pkr.getAddress(addressIndex, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange, pub); unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(addressIndex, isChange, pub);
w.add(createTx( w.add(createTx(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt', '15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789', '123456789',
@ -274,8 +275,8 @@ describe('TxProposals model', function() {
var w = new TxProposals({ var w = new TxProposals({
networkName: config.networkName, networkName: config.networkName,
}); });
unspentTest[0].address = pkr.getAddress(index, isChange, pub).toString(); unspentTest[0].address = pkr.getAddress(addressIndex, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange, pub); unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(addressIndex, isChange, pub);
w.add(createTx( w.add(createTx(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt', '15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789', '123456789',
@ -299,8 +300,8 @@ describe('TxProposals model', function() {
networkName: config.networkName, networkName: config.networkName,
publicKeyRing: w.publicKeyRing, publicKeyRing: w.publicKeyRing,
}); });
unspentTest[0].address = pkr.getAddress(index, isChange, pub).toString(); unspentTest[0].address = pkr.getAddress(addressIndex, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange, pub); unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(addressIndex, isChange, pub);
w2.add(createTx( w2.add(createTx(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt', '15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789', '123456789',
@ -375,7 +376,7 @@ describe('TxProposals model', function() {
}; };
var addressToSign = pkr.generateAddress(false, pub); var addressToSign = pkr.generateAddress(false, pub);
unspentTest[0].address = addressToSign.toString(); 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 tx, txb;
var w = new TxProposals({ var w = new TxProposals({
@ -490,8 +491,8 @@ describe('TxProposals model', function() {
var w = new TxProposals({ var w = new TxProposals({
networkName: config.networkName, networkName: config.networkName,
}); });
unspentTest[0].address = pkr.getAddress(index, isChange, pub).toString(); unspentTest[0].address = pkr.getAddress(addressIndex, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange, pub); unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(addressIndex, isChange, pub);
w.add(createTx( w.add(createTx(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt', '15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789', '123456789',
@ -511,8 +512,8 @@ describe('TxProposals model', function() {
var w2 = new TxProposals({ var w2 = new TxProposals({
networkName: config.networkName, networkName: config.networkName,
}); });
unspentTest[0].address = pkr.getAddress(index, isChange, pub).toString(); unspentTest[0].address = pkr.getAddress(addressIndex, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange, pub); unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(addressIndex, isChange, pub);
w2.add(createTx( w2.add(createTx(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt', '15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789', '123456789',
@ -530,8 +531,8 @@ describe('TxProposals model', function() {
var w3 = new TxProposals({ var w3 = new TxProposals({
networkName: config.networkName, networkName: config.networkName,
}); });
unspentTest[0].address = pkr.getAddress(index, isChange, pub).toString(); unspentTest[0].address = pkr.getAddress(addressIndex, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange, pub); unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(addressIndex, isChange, pub);
w3.add(createTx( w3.add(createTx(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt', '15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789', '123456789',
@ -593,8 +594,8 @@ describe('TxProposals model', function() {
}); });
var ts = Date.now(); var ts = Date.now();
unspentTest[0].address = pkr.getAddress(index, isChange, pub).toString(); unspentTest[0].address = pkr.getAddress(addressIndex, isChange, pub).toString();
unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(index, isChange, pub); unspentTest[0].scriptPubKey = pkr.getScriptPubKeyHex(addressIndex, isChange, pub);
w.add(createTx( w.add(createTx(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt', '15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789', '123456789',
@ -724,4 +725,4 @@ var txpv1 = {
"2NEodmgBa4SH3VwE2asgW34vMYe8VThBZNo": "5221024739614847d5233a46913482c17c6860194ad78abb3bf47de46223047d8a0b5821024c6dc65a52c5eaaa080b96888091544f8ab8712caa7e0b69ea4b45f6f059557452ae" "2NEodmgBa4SH3VwE2asgW34vMYe8VThBZNo": "5221024739614847d5233a46913482c17c6860194ad78abb3bf47de46223047d8a0b5821024c6dc65a52c5eaaa080b96888091544f8ab8712caa7e0b69ea4b45f6f059557452ae"
} }
} }
}; };

View file

@ -381,14 +381,14 @@ describe('Wallet model', function() {
var w = createW(); var w = createW();
var aiObj = { var aiObj = {
indexes: [{ indexes: [{
cosigner: 0, copayerIndex: 0,
changeIndex: 3, changeIndex: 3,
receiveIndex: 2 receiveIndex: 2
}] }]
}; };
w._handleIndexes('senderID', aiObj, true); w._handleIndexes('senderID', aiObj, true);
w.publicKeyRing.getIndex(0).getReceiveIndex(2); w.publicKeyRing.getHDParams(0).getReceiveIndex(2);
w.publicKeyRing.getIndex(0).getChangeIndex(3); w.publicKeyRing.getHDParams(0).getChangeIndex(3);
}); });
it('handle network pubKeyRings correctly', function() { it('handle network pubKeyRings correctly', function() {
@ -405,7 +405,7 @@ describe('Wallet model', function() {
requiredCopayers: w.requiredCopayers, requiredCopayers: w.requiredCopayers,
totalCopayers: w.totalCopayers, totalCopayers: w.totalCopayers,
indexes: [{ indexes: [{
cosigner: 0, copayerIndex: 0,
changeIndex: 2, changeIndex: 2,
receiveIndex: 3 receiveIndex: 3
}], }],
@ -415,8 +415,8 @@ describe('Wallet model', function() {
w._handlePublicKeyRing('senderID', { w._handlePublicKeyRing('senderID', {
publicKeyRing: pkrObj publicKeyRing: pkrObj
}, true); }, true);
w.publicKeyRing.getIndex(0).getReceiveIndex(2); w.publicKeyRing.getHDParams(0).getReceiveIndex(2);
w.publicKeyRing.getIndex(0).getChangeIndex(3); w.publicKeyRing.getHDParams(0).getChangeIndex(3);
for (var i = 0; i < w.requiredCopayers; i++) { for (var i = 0; i < w.requiredCopayers; i++) {
w.publicKeyRing.toObj().copayersExtPubKeys[i].should.equal(cepk[i]); w.publicKeyRing.toObj().copayersExtPubKeys[i].should.equal(cepk[i]);
} }
@ -788,8 +788,8 @@ describe('Wallet model', function() {
// check updated all indexes // check updated all indexes
var cosignersChecked = [] var cosignersChecked = []
updateIndex.args.forEach(function(i) { updateIndex.args.forEach(function(i) {
cosignersChecked.indexOf(i[0].cosigner).should.equal(-1); cosignersChecked.indexOf(i[0].copayerIndex).should.equal(-1);
cosignersChecked.push(i[0].cosigner); cosignersChecked.push(i[0].copayerIndex);
}); });
sinon.assert.callCount(updateIndex, 4); sinon.assert.callCount(updateIndex, 4);
@ -810,7 +810,7 @@ describe('Wallet model', function() {
var index = { var index = {
changeIndex: 1, changeIndex: 1,
receiveIndex: 2, receiveIndex: 2,
cosigner: 2, copayerIndex: 2,
} }
w.updateIndex(index, function(err) { w.updateIndex(index, function(err) {
index.receiveIndex.should.equal(9); index.receiveIndex.should.equal(9);