From 96e9882a62b48a61d1272e9fe1686f82ace9ca69 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 4 Apr 2014 19:36:24 -0300 Subject: [PATCH 1/5] fix after comments --- js/models/PublicKeyRing.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/models/PublicKeyRing.js b/js/models/PublicKeyRing.js index a078c51f3..37342f7e1 100644 --- a/js/models/PublicKeyRing.js +++ b/js/models/PublicKeyRing.js @@ -17,10 +17,10 @@ var log = imports.log || console.log; var storage = Storage.default(); /* - * This follow Electrum convetion, as described on + * This follow Electrum convetion, as described in * https://bitcointalk.org/index.php?topic=274182.0 * - * We should probably adapt the next standard once its ready as discussed at: + * We should probably adopt the next standard once it's ready, as discussed in: * http://sourceforge.net/p/bitcoin/mailman/message/32148600/ * */ From b1b8a44a7d70ebc3990332ce4a6b590e0ffa0ea5 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 4 Apr 2014 19:40:30 -0300 Subject: [PATCH 2/5] fix params --- js/models/PublicKeyRing.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/models/PublicKeyRing.js b/js/models/PublicKeyRing.js index 37342f7e1..1a2da535a 100644 --- a/js/models/PublicKeyRing.js +++ b/js/models/PublicKeyRing.js @@ -161,7 +161,7 @@ PublicKeyRing.prototype.addCopayer = function (newEpk) { }; -PublicKeyRing.prototype.getCopayersPubKeys = function (index, isChange) { +PublicKeyRing.prototype.getCopayersPubKeys = function () { this._checkKeys(); var pubKeys = []; From 8b42f8508bb76700e4a1f201c9307503728a9f3a Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 4 Apr 2014 19:59:00 -0300 Subject: [PATCH 3/5] fix params --- js/models/PublicKeyRing.js | 8 ++++---- test/test.publickeyring.js | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/js/models/PublicKeyRing.js b/js/models/PublicKeyRing.js index 1a2da535a..3d47a523a 100644 --- a/js/models/PublicKeyRing.js +++ b/js/models/PublicKeyRing.js @@ -161,7 +161,7 @@ PublicKeyRing.prototype.addCopayer = function (newEpk) { }; -PublicKeyRing.prototype.getCopayersPubKeys = function () { +PublicKeyRing.prototype.getCopayersPubKeys = function (index, isChange) { this._checkKeys(); var pubKeys = []; @@ -186,7 +186,7 @@ PublicKeyRing.prototype._checkIndexRange = function (index, isChange) { PublicKeyRing.prototype.getRedeemScript = function (index, isChange) { this._checkIndexRange(index, isChange); - var pubKeys = this.getCopayersPubKeys(); + var pubKeys = this.getCopayersPubKeys(index, isChange); var script = Script.createMultisig(this.requiredCopayers, pubKeys); return script; }; @@ -207,9 +207,9 @@ PublicKeyRing.prototype.generateAddress = function(isChange) { var ret = this.getAddress(isChange ? this.changeAddressIndex : this.addressIndex, isChange); if (isChange) - this.addressIndex++; - else this.changeAddressIndex++; + else + this.addressIndex++; return ret; diff --git a/test/test.publickeyring.js b/test/test.publickeyring.js index 38c992905..9f2ec58fd 100644 --- a/test/test.publickeyring.js +++ b/test/test.publickeyring.js @@ -101,6 +101,11 @@ describe('PublicKeyRing model', function() { a.isValid().should.equal(true); a.isScript().should.equal(true); a.network().name.should.equal('livenet'); + + if (i>1) { + w.getAddress(i-1,isChange).should + .not.equal(w.getAddress(i-2,isChange)); + } } } }); From f089246b88def2e9d9223b2f5d64f23ca09cbd7f Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 4 Apr 2014 20:16:15 -0300 Subject: [PATCH 4/5] add more tests --- js/models/PublicKeyRing.js | 14 +++++++++++--- test/test.publickeyring.js | 27 +++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/js/models/PublicKeyRing.js b/js/models/PublicKeyRing.js index 3d47a523a..ee33dde9c 100644 --- a/js/models/PublicKeyRing.js +++ b/js/models/PublicKeyRing.js @@ -84,6 +84,8 @@ PublicKeyRing.read = function (id, passphrase) { w.requiredCopayers = data.neededCopayers; w.totalCopayers = data.totalCopayers; + w.addressIndex = data.addressIndex; + w.changeAddressIndex = data.changeAddressIndex; // this.bip32 = ; w.copayersBIP32 = data.copayersExtPubKeys.map( function (pk) { @@ -95,16 +97,22 @@ PublicKeyRing.read = function (id, passphrase) { return w; }; -PublicKeyRing.prototype.serialize = function () { - return JSON.stringify({ +PublicKeyRing.prototype._toObj = function() { + return { id: this.id, network: this.network.name, requiredCopayers: this.neededCopayers, totalCopayers: this.totalCopayers, + changeAddressIndex: this.changeAddressIndex, + addressIndex: this.addressIndex, copayersExtPubKeys: this.copayersBIP32.map( function (b) { return b.extendedPublicKeyString(); }), - }); + }; +}; + +PublicKeyRing.prototype.serialize = function () { + return JSON.stringify(this._toObj()); }; diff --git a/test/test.publickeyring.js b/test/test.publickeyring.js index 9f2ec58fd..af5fcaeda 100644 --- a/test/test.publickeyring.js +++ b/test/test.publickeyring.js @@ -75,6 +75,10 @@ describe('PublicKeyRing model', function() { var k = createW(); var w = k.w; var copayers = k.copayers; + for(var i=0; i<3; i++) + w.generateAddress(true); + for(var i=0; i<5; i++) + w.generateAddress(false); w.store().should.equal(true); var ID = w.id; @@ -86,7 +90,9 @@ describe('PublicKeyRing model', function() { w2.addCopayer.bind().should.throw(); for(var i =0; i<5; i++) w2.addCopayer.bind(copayers[i]).should.throw(); - + + w2.changeAddressIndex.should.equal(3); + w2.addressIndex.should.equal(5); }); @@ -101,7 +107,6 @@ describe('PublicKeyRing model', function() { a.isValid().should.equal(true); a.isScript().should.equal(true); a.network().name.should.equal('livenet'); - if (i>1) { w.getAddress(i-1,isChange).should .not.equal(w.getAddress(i-2,isChange)); @@ -124,12 +129,26 @@ describe('PublicKeyRing model', function() { var as = w.getAddresses(); as.length.should.equal(12); - for(var i in as) { - var a = new Address(as[i]); + for(var j in as) { + var a = new Address(as[j]); a.isValid().should.equal(true); } }); + it('should count generation indexes', function () { + var k = createW(); + var w = k.w; + + for(var i=0; i<3; i++) + w.generateAddress(true); + for(var i=0; i<5; i++) + w.generateAddress(false); + + w.changeAddressIndex.should.equal(3); + w.addressIndex.should.equal(5); + }); + + }); From 1775d9874619f8249321121538f7ee920a630cb9 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 7 Apr 2014 11:46:45 -0300 Subject: [PATCH 5/5] #merge support to PubKeyRing --- js/models/PublicKeyRing.js | 88 +++++++++++++++++++++++++++-- test/test.network.js | 21 ------- test/test.publickeyring.js | 110 +++++++++++++++++++++++++++++++++++++ 3 files changed, 192 insertions(+), 27 deletions(-) delete mode 100644 test/test.network.js diff --git a/js/models/PublicKeyRing.js b/js/models/PublicKeyRing.js index ee33dde9c..e047ecb9e 100644 --- a/js/models/PublicKeyRing.js +++ b/js/models/PublicKeyRing.js @@ -76,13 +76,13 @@ PublicKeyRing.read = function (id, passphrase) { if (data.id !== id) throw new Error('Wrong id in data'); - var config = { network: data.network === 'livenet' ? + var config = { network: data.networkName === 'livenet' ? bitcore.networks.livenet : bitcore.networks.testnet }; var w = new PublicKeyRing(config); - w.requiredCopayers = data.neededCopayers; + w.requiredCopayers = data.requiredCopayers; w.totalCopayers = data.totalCopayers; w.addressIndex = data.addressIndex; w.changeAddressIndex = data.changeAddressIndex; @@ -97,22 +97,24 @@ PublicKeyRing.read = function (id, passphrase) { return w; }; -PublicKeyRing.prototype._toObj = function() { +PublicKeyRing.prototype.toObj = function() { return { id: this.id, - network: this.network.name, - requiredCopayers: this.neededCopayers, + networkName: this.network.name, + requiredCopayers: this.requiredCopayers, totalCopayers: this.totalCopayers, + changeAddressIndex: this.changeAddressIndex, addressIndex: this.addressIndex, copayersExtPubKeys: this.copayersBIP32.map( function (b) { return b.extendedPublicKeyString(); }), + ts: parseInt(Date.now() / 1000), }; }; PublicKeyRing.prototype.serialize = function () { - return JSON.stringify(this._toObj()); + return JSON.stringify(this.toObj()); }; @@ -236,4 +238,78 @@ PublicKeyRing.prototype.getAddresses = function() { return ret; }; +PublicKeyRing.prototype._checkInPRK = function(inPKR) { + if (this.id !== inPKR.id) + throw new Error('inPRK id mismatch'); + + if (this.network.name !== inPKR.networkName) + throw new Error('inPRK network mismatch'); + + if ( + this.requiredCopayers && inPKR.requiredCopayers && + (this.requiredCopayers !== inPKR.requiredCopayers)) + throw new Error('inPRK requiredCopayers mismatch'); + + if ( + this.totalCopayers && inPKR.totalCopayers && + (this.totalCopayers !== inPKR.totalCopayers)) + throw new Error('inPRK requiredCopayers mismatch'); + + if (! inPKR.ts) + throw new Error('no ts at inPRK'); +}; + + +PublicKeyRing.prototype._mergeIndexes = function(inPKR) { + var hasChanged = false; + + // Indexes + if (inPKR.changeAddressIndex > this.changeAddressIndex) { + this.changeAddressIndex = inPKR.changeAddressIndex; + hasChanged = true; + } + + if (inPKR.addressIndex > this.addressIndex) { + this.addressIndex = inPKR.addressIndex; + hasChanged = true; + } + return hasChanged; +}; + +PublicKeyRing.prototype._mergePubkeys = function(inPKR) { + var hasChanged = false; + var l= this.copayersBIP32.length; + + var self = this; + + inPKR.copayersExtPubKeys.forEach( function(epk) { + var haveIt = false; + for(var j=0; j