Add some test and refactor getCosigner

This commit is contained in:
Yemel Jardi 2014-07-04 09:45:02 -03:00
commit 7562c3f9e7
4 changed files with 54 additions and 18 deletions

View file

@ -177,22 +177,16 @@ PublicKeyRing.prototype.getRedeemScript = function(index, isChange, cosigner) {
// TODO this could be cached // TODO this could be cached
PublicKeyRing.prototype.getAddress = function(index, isChange, id) { PublicKeyRing.prototype.getAddress = function(index, isChange, id) {
var cosigner = typeof id === 'string' ? this.getCosigner(id) : id; var cosigner = this.getCosigner(id);
var script = this.getRedeemScript(index, isChange, cosigner); var script = this.getRedeemScript(index, isChange, cosigner);
var address = Address.fromScript(script, this.network.name); var address = Address.fromScript(script, this.network.name);
this.addressToPath[address.toString()] = Structure.FullBranch(index, isChange, cosigner); this.addressToPath[address.toString()] = Structure.FullBranch(index, isChange, cosigner);
return address; return address;
}; };
PublicKeyRing.prototype.getSharedIndex = function() {
return this.getIndex(Structure.SHARED_INDEX);
};
// 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.getIndex = function(id) {
var cosigner = typeof id === 'string' ? this.getCosigner(id) : id; var cosigner = this.getCosigner(id);
var index = this.indexes.filter(function(i) { return i.cosigner == cosigner }); var index = this.indexes.filter(function(i) { return i.cosigner == cosigner });
if (index.length != 1) throw new Error('no index for cosigner'); if (index.length != 1) throw new Error('no index for cosigner');
return index[0]; return index[0];
@ -214,10 +208,9 @@ 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 cosigner = this.getCosigner(pubkey); var addrIndex = this.getIndex(pubkey);
var addrIndex = this.getIndex(cosigner);
var index = isChange ? addrIndex.getChangeIndex() : addrIndex.getReceiveIndex(); var index = isChange ? addrIndex.getChangeIndex() : addrIndex.getReceiveIndex();
var ret = this.getAddress(index, isChange, cosigner); var ret = this.getAddress(index, isChange, addrIndex.cosigner);
addrIndex.increment(isChange); addrIndex.increment(isChange);
return ret; return ret;
}; };
@ -229,7 +222,9 @@ PublicKeyRing.prototype.getAddresses = function(opts) {
}; };
PublicKeyRing.prototype.getCosigner = function(pubKey) { PublicKeyRing.prototype.getCosigner = function(pubKey) {
preconditions.checkArgument(pubKey); if (typeof pubKey == 'undefined') return Structure.SHARED_INDEX;
if (typeof pubKey == 'number') return pubKey;
var sorted = this.copayersHK.map(function(h, i){ var sorted = this.copayersHK.map(function(h, i){
return h.eckey.public.toString('hex'); return h.eckey.public.toString('hex');
}).sort(function(h1, h2){ return h1.localeCompare(h2); }); }).sort(function(h1, h2){ return h1.localeCompare(h2); });

View file

@ -47,6 +47,18 @@ describe('AddressIndex model', function() {
cosigners.indexOf(2).should.equal(-1); cosigners.indexOf(2).should.equal(-1);
}); });
it('should serialize to object list and back', function() {
var is = AddressIndex.init(3);
should.exist(is);
is.length.should.equal(4);
var list = AddressIndex.serialize(is);
list.length.should.equal(4);
var is2 = AddressIndex.fromList(list);
is2.length.should.equal(4);
});
it('show be able to store and read', function() { it('show be able to store and read', function() {
var i = createAI(); var i = createAI();
var changeN = 2; var changeN = 2;

View file

@ -343,14 +343,14 @@ describe('Wallet model', function() {
var w = createW(); var w = createW();
var aiObj = { var aiObj = {
indexes: [{ indexes: [{
cosigner: Structure.SHARED_INDEX, cosigner: 0,
changeIndex: 3, changeIndex: 3,
receiveIndex: 2 receiveIndex: 2
}] }]
}; };
w._handleIndexes('senderID', aiObj, true); w._handleIndexes('senderID', aiObj, true);
w.publicKeyRing.getSharedIndex().getReceiveIndex(2); w.publicKeyRing.getIndex(0).getReceiveIndex(2);
w.publicKeyRing.getSharedIndex().getChangeIndex(3); w.publicKeyRing.getIndex(0).getChangeIndex(3);
}); });
it('handle network pubKeyRings correctly', function() { it('handle network pubKeyRings correctly', function() {
@ -367,7 +367,7 @@ describe('Wallet model', function() {
requiredCopayers: w.requiredCopayers, requiredCopayers: w.requiredCopayers,
totalCopayers: w.totalCopayers, totalCopayers: w.totalCopayers,
indexes: [{ indexes: [{
cosigner: Structure.SHARED_INDEX, cosigner: 0,
changeIndex: 2, changeIndex: 2,
receiveIndex: 3 receiveIndex: 3
}], }],
@ -377,8 +377,8 @@ describe('Wallet model', function() {
w._handlePublicKeyRing('senderID', { w._handlePublicKeyRing('senderID', {
publicKeyRing: pkrObj publicKeyRing: pkrObj
}, true); }, true);
w.publicKeyRing.getSharedIndex().getReceiveIndex(2); w.publicKeyRing.getIndex(0).getReceiveIndex(2);
w.publicKeyRing.getSharedIndex().getChangeIndex(3); w.publicKeyRing.getIndex(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]);
} }

View file

@ -69,6 +69,35 @@ describe('Unit: Testing Filters', function() {
})); }));
}); });
describe('removeEmpty addresses', function() {
it('should work with empty lists', inject(function($filter) {
var removeEmpty = $filter('removeEmpty');
expect(removeEmpty([]).length).to.equal(0);
}));
it('should filter empty addresses from other copayers', inject(function($filter) {
var removeEmpty = $filter('removeEmpty');
var addresses = [{
owned: true,
balance: 0
}, {
owned: false,
balance: 0
}, {
owned: true,
balance: 0
}, {
owned: false,
balance: 0
}];
expect(removeEmpty(addresses).length).to.equal(2);
addresses[1].owned = true;
expect(removeEmpty(addresses).length).to.equal(3);
addresses[3].balance = 10;
expect(removeEmpty(addresses).length).to.equal(4);
}));
});
describe('noFractionNumber bits', function() { describe('noFractionNumber bits', function() {
beforeEach(function() { beforeEach(function() {
config.unitToSatoshi = 100; config.unitToSatoshi = 100;