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,7 +190,7 @@ 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');
@ -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

@ -24,7 +24,7 @@ var is_browser = (typeof process == 'undefined' || typeof process.versions === '
var PublicKeyRing = is_browser ? copay.PublicKeyRing :
require('soop').load('../js/models/core/PublicKeyRing', {
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);
@ -163,11 +163,12 @@ describe('TxProposals model', function() {
});
var selectedUtxos = b.getSelectedUnspent();
var inputChainPaths = selectedUtxos.map(function(utxo) {
return pkr.pathForAddress(utxo.address);
});
b.setHashToScriptMap(pkr.getRedeemScriptMap(inputChainPaths));
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',
@ -724,4 +725,4 @@ var txpv1 = {
"2NEodmgBa4SH3VwE2asgW34vMYe8VThBZNo": "5221024739614847d5233a46913482c17c6860194ad78abb3bf47de46223047d8a0b5821024c6dc65a52c5eaaa080b96888091544f8ab8712caa7e0b69ea4b45f6f059557452ae"
}
}
};
};

View File

@ -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]);
}
@ -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);