From 432f74488a237ea3a4a4285e9b6499f1a6e4c10f Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 14 May 2014 20:55:34 -0300 Subject: [PATCH 1/5] change name to bitcore.HierarchicalKey --- js/models/core/PrivateKey.js | 10 ++++----- js/models/core/PublicKeyRing.js | 38 ++++++++++++++++----------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/js/models/core/PrivateKey.js b/js/models/core/PrivateKey.js index f7212e1e3..8f0643b59 100644 --- a/js/models/core/PrivateKey.js +++ b/js/models/core/PrivateKey.js @@ -3,7 +3,7 @@ var imports = require('soop').imports(); var bitcore = require('bitcore'); -var BIP32 = bitcore.BIP32; +var HK = bitcore.HierarchicalKey; var WalletKey = bitcore.WalletKey; var networks = bitcore.networks; var util = bitcore.util; @@ -14,7 +14,7 @@ function PrivateKey(opts) { this.network = opts.networkName === 'testnet' ? networks.testnet : networks.livenet; var init = opts.extendedPrivateKeyString || this.network.name; - this.bip = opts.BIP32 || new BIP32(init); + this.bip = opts.HK || new HK(init); this.privateKeyCache = opts.privateKeyCache || {}; }; @@ -47,7 +47,7 @@ PrivateKey.prototype.getExtendedPrivateKeyString = function() { return this.bip.extendedPrivateKeyString(); }; -PrivateKey.prototype._getBIP32 = function(path) { +PrivateKey.prototype._getHK = function(path) { if (typeof path === 'undefined') { return this.bip; } @@ -58,8 +58,8 @@ PrivateKey.prototype.get = function(index,isChange) { var path = PublicKeyRing.Branch(index, isChange); var pk = this.privateKeyCache[path]; if (!pk) { - var derivedBIP32 = this._getBIP32(path); - pk = this.privateKeyCache[path] = derivedBIP32.eckey.private.toString('hex'); + var derivedHK = this._getHK(path); + pk = this.privateKeyCache[path] = derivedHK.eckey.private.toString('hex'); } else { //console.log('cache hit!'); } diff --git a/js/models/core/PublicKeyRing.js b/js/models/core/PublicKeyRing.js index d9073c792..c76d0914d 100644 --- a/js/models/core/PublicKeyRing.js +++ b/js/models/core/PublicKeyRing.js @@ -4,7 +4,7 @@ var imports = require('soop').imports(); var bitcore = require('bitcore'); -var BIP32 = bitcore.BIP32; +var HK = bitcore.HierarchicalKey; var Address = bitcore.Address; var Script = bitcore.Script; var coinUtil = bitcore.util; @@ -26,7 +26,7 @@ function PublicKeyRing(opts) { this.requiredCopayers = opts.requiredCopayers || 3; this.totalCopayers = opts.totalCopayers || 5; - this.copayersBIP32 = opts.copayersBIP32 || []; + this.copayersHK = opts.copayersHK || []; this.changeAddressIndex= opts.changeAddressIndex || 0; this.addressIndex= opts.addressIndex || 0; @@ -74,7 +74,7 @@ PublicKeyRing.prototype.toObj = function() { changeAddressIndex: this.changeAddressIndex, addressIndex: this.addressIndex, - copayersExtPubKeys: this.copayersBIP32.map( function (b) { + copayersExtPubKeys: this.copayersHK.map( function (b) { return b.extendedPublicKeyString(); }), nicknameFor: this.nicknameFor, @@ -87,7 +87,7 @@ PublicKeyRing.prototype.getCopayerId = function(i) { }; PublicKeyRing.prototype.registeredCopayers = function () { - return this.copayersBIP32.length; + return this.copayersHK.length; }; PublicKeyRing.prototype.isComplete = function () { @@ -109,14 +109,14 @@ PublicKeyRing.prototype._checkKeys = function() { }; PublicKeyRing.prototype._newExtendedPublicKey = function () { - return new BIP32(this.network.name) + return new HK(this.network.name) .extendedPublicKeyString(); }; PublicKeyRing.prototype._updateBip = function (index) { var path = PublicKeyRing.ID_BRANCH; - var bip32 = this.copayersBIP32[index].derive(path); - this.copayerIds[index]= bip32.eckey.public.toString('hex'); + var hk = this.copayersHK[index].derive(path); + this.copayerIds[index]= hk.eckey.public.toString('hex'); }; PublicKeyRing.prototype._setNicknameForIndex = function (index, nickname) { @@ -139,14 +139,14 @@ PublicKeyRing.prototype.addCopayer = function (newEpk, nickname) { newEpk = this._newExtendedPublicKey(); } - this.copayersBIP32.forEach(function(b){ + this.copayersHK.forEach(function(b){ if (b.extendedPublicKeyString() === newEpk) throw new Error('already have that key'); }); - var i=this.copayersBIP32.length; - var bip = new BIP32(newEpk); - this.copayersBIP32.push(bip); + var i=this.copayersHK.length; + var bip = new HK(newEpk); + this.copayersHK.push(bip); this._updateBip(i); if (nickname) { this._setNicknameForIndex(i,nickname); @@ -161,10 +161,10 @@ PublicKeyRing.prototype.getPubKeys = function (index, isChange) { var pubKeys = this.publicKeysCache[path]; if (!pubKeys) { pubKeys = []; - var l = this.copayersBIP32.length; + var l = this.copayersHK.length; for(var i=0; i Date: Wed, 14 May 2014 20:56:12 -0300 Subject: [PATCH 2/5] fix tests --- test/test.PrivateKey.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test.PrivateKey.js b/test/test.PrivateKey.js index 71ae071be..15edb360e 100644 --- a/test/test.PrivateKey.js +++ b/test/test.PrivateKey.js @@ -7,7 +7,6 @@ var Transaction = bitcore.Transaction; var buffertools = bitcore.buffertools; var WalletKey = bitcore.WalletKey; var Key = bitcore.Key; -var BIP32 = bitcore.BIP32; var bignum = bitcore.bignum; var networks = bitcore.networks; var Address = bitcore.Address; From 2e99d9a8ea8b58c44e8804ea4bf3f87d1024d532 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 14 May 2014 21:02:01 -0300 Subject: [PATCH 3/5] add version to networking --- .gitignore | 1 + copay.js | 3 +-- index.html | 3 ++- js/controllers/footer.js | 5 ++--- js/models/core/Wallet.js | 3 ++- js/models/core/WalletFactory.js | 27 +++++++++++++++++++++------ js/services/passphrase.js | 6 +----- js/services/walletFactory.js | 2 +- test/test.TxProposals.js | 1 - test/test.Wallet.js | 1 + test/test.Walletfactory.js | 10 +++++----- util/build.js | 9 +++++++++ 12 files changed, 46 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 23900fae9..94dc3faf8 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,4 @@ js/copayBundle.js config.js webapp chrome-extension +version.js diff --git a/copay.js b/copay.js index f35ba8732..8f127e2d9 100644 --- a/copay.js +++ b/copay.js @@ -18,6 +18,5 @@ var WalletFactory = require('soop').load('./js/models/core/WalletFactory',{ Storage: StorageLocalEncrypted, }); module.exports.WalletFactory = WalletFactory; - - +module.exports.version = require('./version'); module.exports.API = require('./API'); diff --git a/index.html b/index.html index 9a3d18c05..6fd3248a3 100644 --- a/index.html +++ b/index.html @@ -122,7 +122,7 @@
Copay - v. 0.0.5 + v{{version}}
@@ -658,6 +658,7 @@ on supported browsers please check http://www.w + diff --git a/js/controllers/footer.js b/js/controllers/footer.js index b07562713..bd71d68df 100644 --- a/js/controllers/footer.js +++ b/js/controllers/footer.js @@ -1,7 +1,6 @@ 'use strict'; -angular.module('copay.footer').controller('FooterController', - function($scope) { +angular.module('copay.footer').controller('FooterController', function($scope, $http) { if (config.themes && Array.isArray(config.themes) && config.themes[0]) { $scope.themes = config.themes; @@ -15,5 +14,5 @@ angular.module('copay.footer').controller('FooterController', $scope.change_theme = function(name) { $scope.theme = 'css/tpl-' + name + '.css'; }; - + $scope.version = copay.version; }); diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index 3eda3de05..358ab197c 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -18,7 +18,7 @@ function Wallet(opts) { //required params ['storage', 'network', 'blockchain', 'requiredCopayers', 'totalCopayers', 'spendUnconfirmed', - 'publicKeyRing', 'txProposals', 'privateKey' + 'publicKeyRing', 'txProposals', 'privateKey', 'version' ].forEach(function(k) { if (typeof opts[k] === 'undefined') throw new Error('missing key:' + k); self[k] = opts[k]; @@ -146,6 +146,7 @@ Wallet.prototype._optsToObj = function() { totalCopayers: this.totalCopayers, name: this.name, netKey: this.netKey, + version: this.version, }; return obj; diff --git a/js/models/core/WalletFactory.js b/js/models/core/WalletFactory.js index 0db065eeb..80177dc8a 100644 --- a/js/models/core/WalletFactory.js +++ b/js/models/core/WalletFactory.js @@ -13,14 +13,9 @@ var Wallet = require('./Wallet'); /* * WalletFactory * - * - * var wallet = WF.read(config,walletId); -> always go to storage - * var wallet = WF.create(config,walletId); -> create wallets, with the given ID (or random is not given) - * - * var wallet = WF.open(config,walletId); -> try to read walletId, if fails, create a new wallet with that id */ -function WalletFactory(config) { +function WalletFactory(config, version) { var self = this; this.storage = new Storage(config.storage); this.network = new Network(config.network); @@ -29,6 +24,7 @@ function WalletFactory(config) { this.networkName = config.networkName; this.verbose = config.verbose; this.walletDefaults = config.wallet; + this.version = version; } WalletFactory.prototype.log = function(){ @@ -129,11 +125,28 @@ WalletFactory.prototype.create = function(opts) { opts.spendUnconfirmed = opts.spendUnconfirmed || this.walletDefaults.spendUnconfirmed; opts.requiredCopayers = requiredCopayers; opts.totalCopayers = totalCopayers; + opts.version = this.version; var w = new Wallet(opts); w.store(); return w; }; + +WalletFactory.prototype._checkVersion = function(inVersion) { + var thisV = this.version.split('.'); + var thisV0 = parseInt(thisV[0]); + var inV = inVersion.split('.'); + var inV0 = parseInt(inV[0]); + + //We only check for major version differences + if( thisV0 < inV0 ) { + throw new Error('Major difference in software versions' + + '. Received:' + inVersion + + '. Current version:' + this.version + + '. Aborting.'); + } +}; + WalletFactory.prototype.open = function(walletId, opts) { opts = opts || {}; opts.id = walletId; @@ -141,6 +154,8 @@ WalletFactory.prototype.open = function(walletId, opts) { this.storage._setPassphrase(opts.passphrase); var w = this.read(walletId); + this._checkVersion(w.version); + if (w) { w.store(); diff --git a/js/services/passphrase.js b/js/services/passphrase.js index 12043a3be..21fd495ae 100644 --- a/js/services/passphrase.js +++ b/js/services/passphrase.js @@ -1,7 +1,3 @@ 'use strict'; -var passphrase; -angular.module('copay.passphrase').factory('Passphrase', function($rootScope) { - passphrase = passphrase || new copay.Passphrase(config.passphrase); - return passphrase; -}); +angular.module('copay.passphrase').value('Passphrase', new copay.Passphrase(config.passphrase)); diff --git a/js/services/walletFactory.js b/js/services/walletFactory.js index ba7e48107..49af6ef26 100644 --- a/js/services/walletFactory.js +++ b/js/services/walletFactory.js @@ -1,4 +1,4 @@ 'use strict'; -angular.module('copay.walletFactory').value('walletFactory', new copay.WalletFactory(config)); +angular.module('copay.walletFactory').value('walletFactory', new copay.WalletFactory(config, copay.version)); diff --git a/test/test.TxProposals.js b/test/test.TxProposals.js index 647b634d4..1d76b1bae 100644 --- a/test/test.TxProposals.js +++ b/test/test.TxProposals.js @@ -7,7 +7,6 @@ var Transaction = bitcore.Transaction; var buffertools = bitcore.buffertools; var WalletKey = bitcore.WalletKey; var Key = bitcore.Key; -var BIP32 = bitcore.BIP32; var bignum = bitcore.bignum; var Script = bitcore.Script; var Builder = bitcore.TransactionBuilder; diff --git a/test/test.Wallet.js b/test/test.Wallet.js index 60bdb9359..53a235fe9 100644 --- a/test/test.Wallet.js +++ b/test/test.Wallet.js @@ -53,6 +53,7 @@ describe('Wallet model', function() { c.networkName = config.networkName; c.verbose = config.verbose; + c.version = '0.0.1'; return new Wallet(c); } diff --git a/test/test.Walletfactory.js b/test/test.Walletfactory.js index a3feeba50..8a5648a0e 100644 --- a/test/test.Walletfactory.js +++ b/test/test.Walletfactory.js @@ -46,12 +46,12 @@ describe('WalletFactory model', function() { }); it('should be able to create wallets', function() { - var wf = new WalletFactory(config); + var wf = new WalletFactory(config, '0.0.1'); var w = wf.create(); should.exist(w); }); it('should be able to get wallets', function() { - var wf = new WalletFactory(config); + var wf = new WalletFactory(config, '0.0.1'); var w = wf.create(); var w2 = wf.read(w.id); @@ -60,9 +60,9 @@ describe('WalletFactory model', function() { }); it('#fromObj #toObj round trip', function() { - var o = '{"opts":{"id":"dbfe10c3fae71cea","spendUnconfirmed":1,"requiredCopayers":3,"totalCopayers":5,"netKey":"LppzFYqlgT0="},"publicKeyRing":{"walletId":"dbfe10c3fae71cea","networkName":"testnet","requiredCopayers":3,"totalCopayers":5,"changeAddressIndex":3,"addressIndex":3,"copayersExtPubKeys":["tpubD6NzVbkrYhZ4YGK8ZhZ8WVeBXNAAoTYjjpw9twCPiNGrGQYFktP3iVQkKmZNiFnUcAFMJRxJVJF6Nq9MDv2kiRceExJaHFbxUCGUiRhmy97","tpubD6NzVbkrYhZ4YKGDJkzWdQsQV3AcFemaQKiwNhV4RL8FHnBFvinidGdQtP8RKj3h34E65RkdtxjrggZYqsEwJ8RhhN2zz9VrjLnrnwbXYNc","tpubD6NzVbkrYhZ4YkDiewjb32Pp3Sz9WK2jpp37KnL7RCrHAyPpnLfgdfRnTdpn6DTWmPS7niywfgWiT42aJb1J6CjWVNmkgsMCxuw7j9DaGKB","tpubD6NzVbkrYhZ4XEtUAz4UUTWbprewbLTaMhR8NUvSJUEAh4Sidxr6rRPFdqqVRR73btKf13wUjds2i8vVCNo8sbKrAnyoTr3o5Y6QSbboQjk","tpubD6NzVbkrYhZ4Yj9AAt6xUVuGPVd8jXCrEE6V2wp7U3PFh8jYYvVad31b4VUXEYXzSnkco4fktu8r4icBsB2t3pCR3WnhVLedY2hxGcPFLKD"],"nicknameFor":{},"publicKeysCache":{"m/0/1/0":["0314368b8efa07e8c7dad30498d0a7e3aa575db1fef833347c6d381c1a33a17b17","02cfd95f89ab46bd3bd86954dd9f83dbab0cd2e4466dee587e8e4d8d733fc0d748","02568969eb6212fe946450be6c5b3353fc754a40b2cdc4aed501a8976fec371da8","0360f870a088ae0ef1c37035a9b6a462ca8dcdd5da275f4e2dcd19f44b81d3e7e4","0300ad8f1bded838b02e127bb25961fbcee718db2df81f680f889692acdcbdd73d"],"m/0/1/1":["024f97a9adb2fa9306c4e3d9244f5e5355c7e2c6b3dd4122ba804e17dc9729df5d","0214834a5adcbc4ad0f3bbbc1c280b8ac480387fcc9a1fd988c1526ed496d923c4","024e72338bd5e976375d076bd71a9649e9141b4cbfc9e16cb7109b354b3e913a05","0322045ea35c3118aa7ab9f2c9f182b0120956b0aa65cc72b9d093f145327a4b17","030dc2450c72df366c1960739c577a2efd4451070bd78effcb6f71d1bcd7dfc7a8"],"m/0/1/2":["0247de59deb66783b8f9b0c326234a9569d00866c2a73f599e77a4d0cab5cbce8f","0376e49f0ac3647404034aae0dc8dd927c34a634ef24ea36f56a272f75fce9539b","032fbaa2593bd1eea4a46e7ac15f15802cdd1eb65a7d5bc4364ddd9d52f0838234","03a81f2a7e1f7191aa0b0c6e0a4ccefc71edd3564e86014972fe338045f68d5a5a","02eb8a012ea9a709392502cacda6ef5115d6d2319ab470d546d9068ab941621a99"],"m/0/0/0":["036dcbd378b4352120d6b720b6294dd2d0dd02801fcf010bb69dadbec1f3999279","022089eedb85dc45d1efa418e1ea226588deedebc1d85acca15ff72783e33636c0","0388aa5fd432b74c56427396f350d236c3ca8f7b2f62da513ce4c2e6ff04a67e9c","02fc4caa7449db7483d2e1fccdacac6fa2f736278c758af9966402589b5632f13e","02e4a15b885d8b2d586f82fa85d16179644e60a154674bde0ec3004810b1bdab99"],"m/0/0/1":["039afa26b2f341c76c7b3c3d0672438f35ac6ebb67b1ddfefac9cd79b7b24418c1","021acaaf500d431ebc396f50630767b01c91ce98ae48e968775ceaad932b7e3b8e","022a947259c4a9f76d5e95c0849df31d01233df41d0d75d631b89317a48d8cddce","03d38d9f94217da780303d9a8987c86d737ef39683febc0cd6632cddbfa62186fd","0394d2581b307fe2af19721888d922aab58ab198ef88cedf9506177e30d807811e"],"m/0/0/2":["037825ffce15d34f9bd6c02bcda7701826706471a4d6ab5004eb965f98811c2098","023768dd6d3c71b7df5733ccda5b2d8b454d5b4c4179d91a6fda74db8b869a2406","021a79e91f003f308764d43039e9b5d56bc8f33ca2f4d30ec6cc5a37c0d09dc273","02437f1e388b273936319f79a5d22958ef5ebff9c8cd7b6f6f72518445b1e30867","0373b0881cb4fd02baa62589023fdfe9739c6148cf104d907549f2528eb80146f5"]}},"txProposals":{"txps":[],"walletId":"dbfe10c3fae71cea","networkName":"testnet"},"privateKey":{"extendedPrivateKeyString":"tprv8ZgxMBicQKsPeoHLg3tY75z4xLeEe8MqAXLNcRA6J6UTRvHV8VZTXznt9eoTmSk1fwSrwZtMhY3XkNsceJ14h6sCXHSWinRqMSSbY8tfhHi","networkName":"testnet","privateKeyCache":{}}}'; + var o = '{"opts":{"id":"dbfe10c3fae71cea","spendUnconfirmed":1,"requiredCopayers":3,"totalCopayers":5,"netKey":"LppzFYqlgT0=","version":"0.0.5"},"publicKeyRing":{"walletId":"dbfe10c3fae71cea","networkName":"testnet","requiredCopayers":3,"totalCopayers":5,"changeAddressIndex":3,"addressIndex":3,"copayersExtPubKeys":["tpubD6NzVbkrYhZ4YGK8ZhZ8WVeBXNAAoTYjjpw9twCPiNGrGQYFktP3iVQkKmZNiFnUcAFMJRxJVJF6Nq9MDv2kiRceExJaHFbxUCGUiRhmy97","tpubD6NzVbkrYhZ4YKGDJkzWdQsQV3AcFemaQKiwNhV4RL8FHnBFvinidGdQtP8RKj3h34E65RkdtxjrggZYqsEwJ8RhhN2zz9VrjLnrnwbXYNc","tpubD6NzVbkrYhZ4YkDiewjb32Pp3Sz9WK2jpp37KnL7RCrHAyPpnLfgdfRnTdpn6DTWmPS7niywfgWiT42aJb1J6CjWVNmkgsMCxuw7j9DaGKB","tpubD6NzVbkrYhZ4XEtUAz4UUTWbprewbLTaMhR8NUvSJUEAh4Sidxr6rRPFdqqVRR73btKf13wUjds2i8vVCNo8sbKrAnyoTr3o5Y6QSbboQjk","tpubD6NzVbkrYhZ4Yj9AAt6xUVuGPVd8jXCrEE6V2wp7U3PFh8jYYvVad31b4VUXEYXzSnkco4fktu8r4icBsB2t3pCR3WnhVLedY2hxGcPFLKD"],"nicknameFor":{},"publicKeysCache":{"m/0/1/0":["0314368b8efa07e8c7dad30498d0a7e3aa575db1fef833347c6d381c1a33a17b17","02cfd95f89ab46bd3bd86954dd9f83dbab0cd2e4466dee587e8e4d8d733fc0d748","02568969eb6212fe946450be6c5b3353fc754a40b2cdc4aed501a8976fec371da8","0360f870a088ae0ef1c37035a9b6a462ca8dcdd5da275f4e2dcd19f44b81d3e7e4","0300ad8f1bded838b02e127bb25961fbcee718db2df81f680f889692acdcbdd73d"],"m/0/1/1":["024f97a9adb2fa9306c4e3d9244f5e5355c7e2c6b3dd4122ba804e17dc9729df5d","0214834a5adcbc4ad0f3bbbc1c280b8ac480387fcc9a1fd988c1526ed496d923c4","024e72338bd5e976375d076bd71a9649e9141b4cbfc9e16cb7109b354b3e913a05","0322045ea35c3118aa7ab9f2c9f182b0120956b0aa65cc72b9d093f145327a4b17","030dc2450c72df366c1960739c577a2efd4451070bd78effcb6f71d1bcd7dfc7a8"],"m/0/1/2":["0247de59deb66783b8f9b0c326234a9569d00866c2a73f599e77a4d0cab5cbce8f","0376e49f0ac3647404034aae0dc8dd927c34a634ef24ea36f56a272f75fce9539b","032fbaa2593bd1eea4a46e7ac15f15802cdd1eb65a7d5bc4364ddd9d52f0838234","03a81f2a7e1f7191aa0b0c6e0a4ccefc71edd3564e86014972fe338045f68d5a5a","02eb8a012ea9a709392502cacda6ef5115d6d2319ab470d546d9068ab941621a99"],"m/0/0/0":["036dcbd378b4352120d6b720b6294dd2d0dd02801fcf010bb69dadbec1f3999279","022089eedb85dc45d1efa418e1ea226588deedebc1d85acca15ff72783e33636c0","0388aa5fd432b74c56427396f350d236c3ca8f7b2f62da513ce4c2e6ff04a67e9c","02fc4caa7449db7483d2e1fccdacac6fa2f736278c758af9966402589b5632f13e","02e4a15b885d8b2d586f82fa85d16179644e60a154674bde0ec3004810b1bdab99"],"m/0/0/1":["039afa26b2f341c76c7b3c3d0672438f35ac6ebb67b1ddfefac9cd79b7b24418c1","021acaaf500d431ebc396f50630767b01c91ce98ae48e968775ceaad932b7e3b8e","022a947259c4a9f76d5e95c0849df31d01233df41d0d75d631b89317a48d8cddce","03d38d9f94217da780303d9a8987c86d737ef39683febc0cd6632cddbfa62186fd","0394d2581b307fe2af19721888d922aab58ab198ef88cedf9506177e30d807811e"],"m/0/0/2":["037825ffce15d34f9bd6c02bcda7701826706471a4d6ab5004eb965f98811c2098","023768dd6d3c71b7df5733ccda5b2d8b454d5b4c4179d91a6fda74db8b869a2406","021a79e91f003f308764d43039e9b5d56bc8f33ca2f4d30ec6cc5a37c0d09dc273","02437f1e388b273936319f79a5d22958ef5ebff9c8cd7b6f6f72518445b1e30867","0373b0881cb4fd02baa62589023fdfe9739c6148cf104d907549f2528eb80146f5"]}},"txProposals":{"txps":[],"walletId":"dbfe10c3fae71cea","networkName":"testnet"},"privateKey":{"extendedPrivateKeyString":"tprv8ZgxMBicQKsPeoHLg3tY75z4xLeEe8MqAXLNcRA6J6UTRvHV8VZTXznt9eoTmSk1fwSrwZtMhY3XkNsceJ14h6sCXHSWinRqMSSbY8tfhHi","networkName":"testnet","privateKeyCache":{}}}'; - var wf = new WalletFactory(config); + var wf = new WalletFactory(config, '0.0.5'); var w = wf.fromObj(JSON.parse(o)); should.exist(w); @@ -76,7 +76,7 @@ describe('WalletFactory model', function() { it('BIP32 length problem', function() { var sconfig = {"networkName":"testnet","network":{"key":"g23ihfh82h35rf","host":"162.242.219.26","port":10009,"path":"/","maxPeers":15,"debug":3},"limits":{"totalCopayers":10,"mPlusN":15},"wallet":{"requiredCopayers":2,"totalCopayers":3,"spendUnconfirmed":1,"verbose":0},"blockchain":{"host":"test.insight.is","port":3001},"socket":{"host":"test.insight.is","port":3001},"verbose":0,"themes":["default"]}; - var wf = new WalletFactory(sconfig); + var wf = new WalletFactory(sconfig, '0.0.1'); var opts = {'requiredCopayers': 2, 'totalCopayers':3}; var w = wf.create(opts); diff --git a/util/build.js b/util/build.js index f1d576c94..396bfca9c 100755 --- a/util/build.js +++ b/util/build.js @@ -24,6 +24,12 @@ var pack = function (params) { return browserPack(params); }; +var createVersion = function() { + var json = JSON.parse(fs.readFileSync('./package.json', 'utf8')); + var content = 'module.exports="' + json.version + '";'; + fs.writeFileSync("./version.js", content); +}; + var createBundle = function(opts) { opts.dir = opts.dir || 'js/'; @@ -45,6 +51,7 @@ var createBundle = function(opts) { b.require('./copay', { expose: 'copay' }); + b.require('./version'); // b.external('bitcore'); b.require('./js/models/core/WalletFactory'); b.require('./js/models/core/Wallet'); @@ -95,6 +102,8 @@ if (require.main === module) { .option('-d, --dontminify', 'Don\'t minify the code.') .option('-o, --stdout', 'Specify output as stdout') .parse(process.argv); + + createVersion(); var copayBundle = createBundle(program); copayBundle.pipe(program.stdout ? process.stdout : fs.createWriteStream('js/copayBundle.js')); } From 63210ba277a293ca3acf4b33cfb8bbcc115f9093 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 15 May 2014 00:01:16 -0300 Subject: [PATCH 4/5] fix in version checking --- js/models/core/WalletFactory.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/js/models/core/WalletFactory.js b/js/models/core/WalletFactory.js index 80177dc8a..24a69bfed 100644 --- a/js/models/core/WalletFactory.js +++ b/js/models/core/WalletFactory.js @@ -154,13 +154,12 @@ WalletFactory.prototype.open = function(walletId, opts) { this.storage._setPassphrase(opts.passphrase); var w = this.read(walletId); - this._checkVersion(w.version); if (w) { + this._checkVersion(w.version); w.store(); } - return w; }; From 5154dc708573017454dea5db8a2204a720cc29e7 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 15 May 2014 17:43:41 -0300 Subject: [PATCH 5/5] broadcast txproposal 1 by 1 --- js/models/core/TxProposals.js | 10 ++++-- js/models/core/Wallet.js | 63 +++++++++++++++++++++------------- js/services/controllerUtils.js | 14 ++++---- 3 files changed, 53 insertions(+), 34 deletions(-) diff --git a/js/models/core/TxProposals.js b/js/models/core/TxProposals.js index 8a59223a2..eaf9c75e4 100644 --- a/js/models/core/TxProposals.js +++ b/js/models/core/TxProposals.js @@ -72,10 +72,17 @@ TxProposals.fromObj = function(o) { return ret; }; +TxProposals.prototype.getNtxids = function() { + return Object.keys(this.txps); +}; -TxProposals.prototype.toObj = function() { +TxProposals.prototype.toObj = function(onlyThisNtxid) { var ret = []; for(var id in this.txps){ + + if (onlyThisNtxid && id != onlyThisNtxid) + continue; + var t = this.txps[id]; if (!t.sent) ret.push(t.toObj()); @@ -87,7 +94,6 @@ TxProposals.prototype.toObj = function() { }; }; - TxProposals.prototype._startMerge = function(myTxps, theirTxps) { var fromUs=0, fromTheirs=0, merged =0; var toMerge = {}, ready={}; diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index a30e63e24..398fefe36 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -89,16 +89,26 @@ Wallet.prototype._handleTxProposals = function(senderId, data, isInbound) { var recipients; var inTxp = copay.TxProposals.fromObj(data.txProposals); + var ids = inTxp.getNtxids(); + + if (ids.lenght>1) { + this.emit('badMessage', senderId); + this.log('Received BAD TxProposal messsage FROM:', senderId); //TODO + return; + } + + var newId = ids[0]; var mergeInfo = this.txProposals.merge(inTxp, true); var addSeen = this.addSeenToTxProposals(); if (mergeInfo.hasChanged || addSeen) { this.log('### BROADCASTING txProposals. '); recipients = null; - TODO: only diff - this.sendTxProposals(recipients); + this.sendTxProposals(recipients, newId); + } + if (data.lastInBatch) { + this.emit('txProposalsUpdated', this.txProposals); + this.store(); } - this.emit('txProposalsUpdated', this.txProposals); - this.store(); }; Wallet.prototype._handleData = function(senderId, data, isInbound) { @@ -290,13 +300,18 @@ Wallet.prototype.toEncryptedObj = function() { Wallet.prototype.sendTxProposals = function(recipients, ntxid) { this.log('### SENDING txProposals TO:', recipients || 'All', this.txProposals); - var toSend = ntxid ? [ntxid] : this.getTxProposals.getNtxids(); + var toSend = ntxid ? [ntxid] : this.txProposals.getNtxids(); + + var last = toSend[toSend]; + for(var i in toSend) { var id = toSend[i]; + var lastInBatch = (i == toSend.length - 1); this.network.send(recipients, { type: 'txProposals', txProposals: this.txProposals.toObj(id), walletId: this.id, + lastInBatch: lastInBatch, }); } }; @@ -459,15 +474,15 @@ Wallet.prototype.addressIsOwn = function(addrStr, opts) { return ret; }; -Wallet.prototype.getBalance = function(safe, cb) { +Wallet.prototype.getBalance = function(cb) { var balance = 0; + var safeBalance = 0; var balanceByAddr = {}; - var isMain = {}; var COIN = bitcore.util.COIN; - var f = safe ? this.getSafeUnspent.bind(this) : this.getUnspent.bind(this); - f(function(utxos) { - for (var i = 0; i < utxos.length; i++) { - var u = utxos[i]; + + this.getUnspent(function(safeUnspent, unspent) { + for (var i = 0; i < unspent.length; i++) { + var u = unspent[i]; var amt = u.amount * COIN; balance += amt; balanceByAddr[u.address] = (balanceByAddr[u.address] || 0) + amt; @@ -478,30 +493,30 @@ Wallet.prototype.getBalance = function(safe, cb) { balanceByAddr[a] = balanceByAddr[a] / COIN; } balance = balance / COIN; - return cb(balance, balanceByAddr, isMain); + + for (var i = 0; i < safeUnspent.length; i++) { + var u = safeUnspent[i]; + var amt = u.amount * COIN; + safeBalance += amt; + } + safeBalance = safeBalance / COIN; + return cb(balance, balanceByAddr, safeBalance); }); }; Wallet.prototype.getUnspent = function(cb) { - this.blockchain.getUnspent(this.getAddressesStr(), function(unspentList) { - return cb(unspentList); - }); -}; - -Wallet.prototype.getSafeUnspent = function(cb) { var self = this; this.blockchain.getUnspent(this.getAddressesStr(), function(unspentList) { - var ret = []; + var safeUnspendList = []; var maxRejectCount = self.totalCopayers - self.requiredCopayers; var uu = self.txProposals.getUsedUnspent(maxRejectCount); for (var i in unspentList) { if (uu.indexOf(unspentList[i].txid) === -1) - ret.push(unspentList[i]); + safeUnspendList.push(unspentList[i]); } - - return cb(ret); + return cb(safeUnspendList, unspentList); }); }; @@ -518,8 +533,8 @@ Wallet.prototype.createTx = function(toAddress, amountSatStr, opts, cb) { opts.spendUnconfirmed = this.spendUnconfirmed; } - self.getSafeUnspent(function(unspentList) { - var ntxid = self.createTxSync(toAddress, amountSatStr, unspentList, opts); + self.getUnspent(function(safeUnspent) { + var ntxid = self.createTxSync(toAddress, amountSatStr, safeUnspent, opts); if (ntxid) { self.sendPublicKeyRing(); // For the new change Address self.sendTxProposals(null, ntxid); diff --git a/js/services/controllerUtils.js b/js/services/controllerUtils.js index a4abebd72..357bf6af8 100644 --- a/js/services/controllerUtils.js +++ b/js/services/controllerUtils.js @@ -101,23 +101,21 @@ angular.module('copay.controllerUtils') }; root.updateBalance = function(cb) { + + console.log('Updating balance...'); $rootScope.balanceByAddr = {}; var w = $rootScope.wallet; $rootScope.addrInfos = w.getAddressesInfo(); if ($rootScope.addrInfos.length === 0) return; $rootScope.loading = true; - w.getBalance(false, function(balance, balanceByAddr) { + w.getBalance(function(balance, balanceByAddr, safeBalance) { + $rootScope.loading = false; $rootScope.totalBalance = balance; $rootScope.balanceByAddr = balanceByAddr; $rootScope.selectedAddr = $rootScope.addrInfos[0].address.toString(); - $rootScope.loading = false; - $rootScope.$digest(); - if (cb) cb(); - }); - w.getBalance(true, function(balance) { - $rootScope.availableBalance = balance; - $rootScope.loading = false; + $rootScope.availableBalance = safeBalance; $rootScope.$digest(); + console.log('Done updating balance.'); //TODO if (cb) cb(); }); root.setSocketHandlers();