@@ -107,18 +107,18 @@
-
diff --git a/js/app.js b/js/app.js
index 7d9375b56..56fc11cde 100644
--- a/js/app.js
+++ b/js/app.js
@@ -11,8 +11,7 @@ angular.module('copay',[
'copay.backup',
'copay.network',
'copay.signin',
- 'copay.peer',
- 'copay.storage'
+ 'copay.peer'
]);
angular.module('copay.header', []);
@@ -23,5 +22,4 @@ angular.module('copay.backup', []);
angular.module('copay.network', []);
angular.module('copay.signin', []);
angular.module('copay.peer', []);
-angular.module('copay.storage', []);
diff --git a/js/config.js b/js/config.js
index 128562a87..5af0cc394 100644
--- a/js/config.js
+++ b/js/config.js
@@ -2,11 +2,15 @@
var config = {
networkName: 'testnet',
- p2pApiKey: 'lwjd5qra8257b9',
- p2pDebug: 3,
- maxPeers: 5,
- requiredCopayers: 2,
- totalCopayers: 3,
+ network: {
+ apiKey: 'lwjd5qra8257b9',
+ maxPeers: 3,
+ debug: 3,
+ },
+ wallet: {
+ requiredCopayers: 2,
+ totalCopayers: 3,
+ },
insight: {
host: 'localhost',
port: 3001
diff --git a/js/controllers/backup.js b/js/controllers/backup.js
index 677f3ab1b..e851e602c 100644
--- a/js/controllers/backup.js
+++ b/js/controllers/backup.js
@@ -2,5 +2,12 @@
angular.module('copay.backup').controller('BackupController',
function($scope, $rootScope, $location) {
+
+
+ if (!$rootScope.wallet.id) {
+ $location.path('signin');
+ }
+
+
$scope.title = 'Backup';
});
diff --git a/js/controllers/home.js b/js/controllers/home.js
index 2524ddd1a..0d762f925 100644
--- a/js/controllers/home.js
+++ b/js/controllers/home.js
@@ -6,15 +6,17 @@ angular.module('copay.home').controller('HomeController',
$scope.oneAtATime = true;
- if (!$rootScope.peerId) {
+ if (!$rootScope.wallet.id) {
$location.path('signin');
}
- $scope.addrs = $rootScope.publicKeyRing.getAddresses();
+
+
+ $scope.addrs = $rootScope.wallet.publicKeyRing.getAddresses();
$scope.selectedAddr = $scope.addrs[0];
$scope.newAddr = function() {
- var a = $rootScope.publicKeyRing.generateAddress();
+ var a = $rootScope.wallet.publicKeyRing.generateAddress();
$scope.addrs.push({ addrStr: a.toString('hex') });
};
diff --git a/js/controllers/send.js b/js/controllers/send.js
index 0c4fa9107..0c05b0af2 100644
--- a/js/controllers/send.js
+++ b/js/controllers/send.js
@@ -1,17 +1,18 @@
'use strict';
angular.module('copay.send').controller('SendController',
- function($scope, $rootScope, $location, Network, Storage) {
+ function($scope, $rootScope, $location, Network) {
$scope.title = 'Send';
- if (!$rootScope.peerId) {
+ if (!$rootScope.wallet.id) {
$location.path('signin');
}
$scope.sendTest = function() {
- var pkr = $rootScope.publicKeyRing;
- var txp = $rootScope.txProposals;
+ var w = $rootScope.wallet;
+ var pkr = w.publicKeyRing;
+ var txp = w.txProposals;
var opts = {remainderOut: { address: pkr.generateAddress(true).toString() }};
// From @cmgustavo's wallet
@@ -31,7 +32,7 @@ console.log('[send.js.29:txp:] BEFORE',txp); //TODO
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789',
unspentTest,
- $rootScope.privateKey,
+ w.privateKey,
opts
);
console.log('[send.js.29:txp:] READY:',txp); //TODO
diff --git a/js/controllers/signin.js b/js/controllers/signin.js
index e319090ff..810b9229b 100644
--- a/js/controllers/signin.js
+++ b/js/controllers/signin.js
@@ -1,15 +1,14 @@
'use strict';
angular.module('copay.signin').controller('SigninController',
- function($scope, $rootScope, $location, Network, Storage) {
-
- var peerData = Storage.get($rootScope.walletId, 'peerData');
+ function($scope, $rootScope, $location, Network) {
+// var peerData = Storage.get($rootScope.walletId, 'peerData');
+// $rootScope.peerId = peerData ? peerData.peerId : null;
$scope.loading = false;
- $rootScope.peerId = peerData ? peerData.peerId : null;
$scope.listWalletIds = function() {
- return Storage.getWalletIds();
+ return copay.Wallet.factory.getWalletIds();
};
$scope.create = function() {
@@ -24,8 +23,6 @@ angular.module('copay.signin').controller('SigninController',
$scope.open = function(walletId) {
$scope.loading = true;
-
-console.log('[signin.js.28:walletId:]',walletId); //TODO
if (Network.openWallet(walletId)) {
Network.init(function() {
$location.path('peer');
@@ -52,9 +49,9 @@ console.log('[signin.js.28:walletId:]',walletId); //TODO
}
};
- if (peerData && peerData.peerId && peerData.connectedPeers.length > 0) {
- $rootScope.peerId = peerData.peerId;
- $scope.join(peerData.connectedPeers);
- }
+ // if (peerData && peerData.peerId && peerData.connectedPeers.length > 0) {
+ // $rootScope.peerId = peerData.peerId;
+ // $scope.join(peerData.connectedPeers);
+ // }
});
diff --git a/js/controllers/transactions.js b/js/controllers/transactions.js
index e6ddbde29..50fe25d5e 100644
--- a/js/controllers/transactions.js
+++ b/js/controllers/transactions.js
@@ -6,7 +6,7 @@ angular.module('copay.transactions').controller('TransactionsController',
$scope.oneAtATime = true;
- if (!$rootScope.peerId) {
+ if (!$rootScope.wallet.id) {
$location.path('signin');
}
@@ -23,5 +23,5 @@ angular.module('copay.transactions').controller('TransactionsController',
}
];
- $scope.txsoutput = $rootScope.txProposals.txps;
+ $scope.txsoutput = $rootScope.wallet.txProposals.txps;
});
diff --git a/js/models/core/PublicKeyRing.js b/js/models/core/PublicKeyRing.js
index 56b0ec6cb..a5e4a8bcb 100644
--- a/js/models/core/PublicKeyRing.js
+++ b/js/models/core/PublicKeyRing.js
@@ -17,6 +17,8 @@ var storage = Storage.default();
function PublicKeyRing(opts) {
opts = opts || {};
+ this.walletId = opts.walletId;
+
this.network = opts.networkName === 'livenet' ?
bitcore.networks.livenet : bitcore.networks.testnet;
@@ -46,16 +48,6 @@ PublicKeyRing.ChangeBranch = function (index) {
return 'm/1/'+index;
};
-PublicKeyRing.decrypt = function (passphrase, encPayload) {
- console.log('[wallet.js.35] TODO READ: passphrase IGNORED');
- return encPayload;
-};
-
-PublicKeyRing.encrypt = function (passphrase, payload) {
- console.log('[wallet.js.92] TODO: passphrase IGNORED');
- return payload;
-};
-
PublicKeyRing.fromObj = function (data) {
if (!data.ts) {
throw new Error('bad data format: Did you use .toObj()?');
@@ -64,13 +56,12 @@ PublicKeyRing.fromObj = function (data) {
var w = new PublicKeyRing(config);
- w.id = data.id;
- w.requiredCopayers = data.requiredCopayers;
- w.totalCopayers = data.totalCopayers;
- w.addressIndex = data.addressIndex;
+ w.walletId = data.walletId;
+ w.requiredCopayers = data.requiredCopayers;
+ w.totalCopayers = data.totalCopayers;
+ w.addressIndex = data.addressIndex;
w.changeAddressIndex = data.changeAddressIndex;
-
- w.copayersBIP32 = data.copayersExtPubKeys.map( function (pk) {
+ w.copayersBIP32 = data.copayersExtPubKeys.map( function (pk) {
return new BIP32(pk);
});
@@ -78,24 +69,9 @@ PublicKeyRing.fromObj = function (data) {
return w;
};
-PublicKeyRing.read = function (encPayload, id, passphrase) {
- if (!encPayload)
- throw new Error('Could not find wallet data');
- var data;
- try {
- data = JSON.parse( PublicKeyRing.decrypt( passphrase, encPayload ));
- } catch (e) {
- throw new Error('error in read: '+ e.toString());
- }
-
- if (data.id !== id)
- throw new Error('Wrong id in data');
- return PublicKeyRing.fromObj(data);
-};
-
PublicKeyRing.prototype.toObj = function() {
return {
- id: this.id,
+ walletId: this.walletId,
networkName: this.network.name,
requiredCopayers: this.requiredCopayers,
totalCopayers: this.totalCopayers,
@@ -114,13 +90,6 @@ PublicKeyRing.prototype.serialize = function () {
};
-PublicKeyRing.prototype.toStore = function (passphrase) {
- if (!this.id)
- throw new Error('wallet has no id');
-
- return PublicKeyRing.encrypt(passphrase,this.serialize());
-};
-
PublicKeyRing.prototype.registeredCopayers = function () {
return this.copayersBIP32.length;
};
@@ -255,8 +224,8 @@ PublicKeyRing.prototype.getRedeemScriptMap = function () {
PublicKeyRing.prototype._checkInPRK = function(inPKR, ignoreId) {
- if (!ignoreId && this.id !== inPKR.id) {
- throw new Error('inPRK id mismatch');
+ if (!ignoreId && this.walletId !== inPKR.walletId) {
+ throw new Error('inPRK walletId mismatch');
}
if (this.network.name !== inPKR.network.name)
diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js
index cc5c09021..13378f66f 100644
--- a/js/models/core/Wallet.js
+++ b/js/models/core/Wallet.js
@@ -13,48 +13,62 @@ var Blockchain = imports.Blockchain;
var copay = copay || require('../../../copay');
-
function Wallet(config) {
this._startInterface(config);
}
-
Wallet.prototype._startInterface = function(config) {
this.storage = new Storage(config.storage);
this.network = new Network(config.network);
this.blockchain = new Blockchain(config.blockchain);
+
+ this.networkName = config.networkName;
+ this.requiredCopayers = config.requiredCopayers;
+ this.totalCopayers = config.totalCopayers;
};
-Wallet.prototype._createNew = function(config, opts) {
+Wallet.prototype.create = function(opts) {
this.id = opts.id || Wallet.getRandomId();
console.log('### CREATING NEW WALLET.' + (opts.id ? ' USING ID: ' + opts.id : ' NEW ID'));
this.privateKey = new copay.PrivateKey({
- networkName: config.networkName
+ networkName: this.networkName
});
console.log('\t### PrivateKey Initialized');
- this.publicKeyRing = opts.publicKeyRing || new copay.PublicKeyRing({
- id: this.id,
- requiredCopayers: opts.requiredCopayers || config.wallet.requiredCopayers,
- totalCopayers: opts.totalCopayers || config.wallet.totalCopayers,
- networkName: config.networkName,
+ this.publicKeyRing = new copay.PublicKeyRing({
+ walletId: this.id,
+ requiredCopayers: opts.requiredCopayers || this.requiredCopayers,
+ totalCopayers: opts.totalCopayers || this.totalCopayers,
+ networkName: this.networkName,
});
- this.publicKeyRing.addCopayer(this.privateKey.getBIP32().extendedPublicKeyString());
- console.log('\t### PublicKeyRing Initialized WalletID: ' + this.publicKeyRing.id);
- this.txProposals = opts.txProposals || new copay.TxProposals({
+ this.publicKeyRing.addCopayer(this.privateKey.getBIP32().extendedPublicKeyString());
+ console.log('\t### PublicKeyRing Initialized WalletID: ' + this.publicKeyRing.walletId);
+
+ this.txProposals = new copay.TxProposals({
walletId: this.id,
publicKeyRing: this.publicKeyRing,
- networkName: config.networkName,
+ networkName: this.networkName,
});
console.log('\t### TxProposals Initialized');
};
-Wallet.prototype._load = function(config, walletId) {
+Wallet.prototype._checkLoad = function(walletId) {
+ return (
+ this.storage.get(walletId, 'publicKeyRing') &&
+ this.storage.get(walletId, 'txProposals') &&
+ this.storage.get(walletId, 'privateKey')
+ );
+}
+
+Wallet.prototype.load = function(walletId) {
+ if (! this._checkLoad(walletId)) return;
+
+
this.id = walletId;
this.publicKeyRing = new copay.PublicKeyRing.fromObj(
this.storage.get(this.id, 'publicKeyRing')
@@ -77,39 +91,61 @@ Wallet.prototype._load = function(config, walletId) {
};
-// CONSTRUCTORS
-Wallet.read = function(config, walletId) {
- var w = new Wallet(config);
- w._load(walletId);
- return w;
+Wallet.prototype.store = function() {
+ Wallet.factory.addWalletId(this.id);
+ this.storage.set(this.id,'publicKeyRing', this.publicKeyRing.toObj());
+ this.storage.set(this.id,'txProposals', this.txProposals.toObj());
+ this.storage.set(this.id,'privateKey', this.privateKey.toObj());
};
-Wallet.create = function(config, opts) {
- var w = new Wallet(config);
- w._createNew(config, opts);
- return w;
+Wallet.prototype.sendTxProposals = function(recipients) {
+ console.log('### SENDING txProposals TO:', recipients||'All', this.txProposals);
+
+ this.network.send( recipients, {
+ type: 'txProposals',
+ txProposals: this.txProposals.toObj(),
+ walletId: this.id,
+ });
};
+Wallet.prototype.sendPublicKeyRing = function(recipients) {
+ console.log('### SENDING publicKeyRing TO:', recipients||'All', this.publicKeyRing.toObj());
+
+ this.network.send(recipients, {
+ type: 'publicKeyRing',
+ publicKeyRing: this.publicKeyRing.toObj(),
+ walletId: this.id,
+ });
+};
+
+
+// // HERE? not sure
+// Wallet.prototype.cleanPeers = function() {
+// this.storage.remove('peerData');
+// };
+//
+
Wallet.getRandomId = function() {
var r = buffertools.toHex(coinUtil.generateNonce());
return r;
};
-Wallet.prototype.store = function() {
- // TODO store each variable
-};
+/*
+ * WalletFactory
+ *
+ */
var WalletFactory = function() {
- this.storage = Storage.
- default ();
+ this.storage = Storage.default();
};
WalletFactory.prototype.create = function(config, opts) {
- var w = new Wallet.create(config, opts);
+ var w = new Wallet(config);
+ w.create(opts);
w.store();
- this._addWalletId(w.id);
+ this.addWalletId(w.id);
return w;
};
@@ -122,21 +158,21 @@ WalletFactory.prototype.remove = function(walletId) {
this._delWalletId(walletId);
};
-WalletFactory.prototype._addWalletId = function(walletId) {
- var ids = this._getWalletIds();
+WalletFactory.prototype.addWalletId = function(walletId) {
+ var ids = this.getWalletIds();
if (ids.indexOf(walletId) == -1) return;
storage.set('walletIds', (ids ? ids + ',' : '') + walletId);
};
WalletFactory.prototype._delWalletId = function(walletId) {
- var ids = this._getWalletIds();
+ var ids = this.getWalletIds();
var index = ids.indexOf(walletId);
if (index == -1) return;
ids.splice(index, 1); // removes walletId
this.storage.set('walletIds', ids.join(','));
};
-WalletFactory.prototype._getWalletIds = function() {
+WalletFactory.prototype.getWalletIds = function() {
var ids = this.storage.get('walletIds');
return ids ? ids.split(',') : [];
};
diff --git a/js/models/network/WebRTC.js b/js/models/network/WebRTC.js
index 15bddfe91..9350eea40 100644
--- a/js/models/network/WebRTC.js
+++ b/js/models/network/WebRTC.js
@@ -220,6 +220,8 @@ Network.prototype.start = function(openCallback) {
Network.prototype._sendToOne = function(peerId, data, cb) {
if (peerId !== this.peerId) {
+console.log('[WebRTC.js.222:peerId:]',peerId, data); //TODO
+
var conns = this.peer.connections[peerId];
if (conns) {
@@ -240,10 +242,12 @@ Network.prototype._sendToOne = function(peerId, data, cb) {
Network.prototype.send = function(peerIds, data, cb) {
var self=this;
+console.log('[WebRTC.js.242:peerIds:]',peerIds); //TODO
if (!peerIds) {
peerIds = this.connectedPeers;
data.isBroadcast = 1;
}
+console.log('[WebRTC.js.246:peerIds:]',peerIds, data); //TODO
if (Array.isArray(peerIds)) {
var l = peerIds.length;
@@ -273,6 +277,7 @@ Network.prototype.connectTo = function(peerId, openCallback, closeCallback ) {
self._setupConnectionHandlers(dataConn, false, openCallback, closeCallback);
};
+
Network.prototype.disconnect = function(peerId, cb) {
console.log('[Network.js.268:disconnect:]'); //TODO
var self = this;
diff --git a/js/models/storage/Base.js b/js/models/storage/Base.js
index 0a91137ac..8f61f4dfe 100644
--- a/js/models/storage/Base.js
+++ b/js/models/storage/Base.js
@@ -6,15 +6,15 @@ function Storage() {
}
// get value by key
-Storage.prototype.get = function(k) {
+Storage.prototype.get = function(walletId,k) {
};
// set value for key
-Storage.prototype.set = function(k,v) {
+Storage.prototype.set = function(walletId,v) {
};
// remove value for key
-Storage.prototype.remove = function(k) {
+Storage.prototype.remove = function(walletId, k) {
};
// remove all values
diff --git a/js/models/storage/Plain.js b/js/models/storage/Plain.js
index 1a1cf7e69..04c3f2641 100644
--- a/js/models/storage/Plain.js
+++ b/js/models/storage/Plain.js
@@ -6,19 +6,23 @@ function Storage() {
this.data = {};
}
+Storage.prototype._key = function(walletId, k) {
+ return walletId + '::' + k;
+};
// get value by key
-Storage.prototype.get = function(k) {
- return JSON.parse(localStorage.getItem(k));
+Storage.prototype.get = function(walletId, k) {
+ return JSON.parse(localStorage.getItem(this._key(walletId,k)));
};
+
// set value for key
-Storage.prototype.set = function(k,v) {
- localStorage.setItem(k, JSON.stringify(v));
+Storage.prototype.set = function(walletId, k,v) {
+ localStorage.setItem(this._key(walletId,k), JSON.stringify(v));
};
// remove value for key
-Storage.prototype.remove = function(k) {
- localStorage.removeItem(k);
+Storage.prototype.remove = function(walletId, k) {
+ localStorage.removeItem(this._key(walletId,k));
};
// remove all values
diff --git a/js/services/network.js b/js/services/network.js
index 8b5b7cb40..e25d84bcc 100644
--- a/js/services/network.js
+++ b/js/services/network.js
@@ -1,33 +1,24 @@
'use strict';
angular.module('copay.network')
- .factory('Network', function($rootScope, Storage) {
+ .factory('Network', function($rootScope) {
var peer;
var _refreshUx = function() {
- var cp = $rootScope.cp;
+ var net = $rootScope.wallet.network;
console.log('*** UPDATING UX'); //TODO
- $rootScope.peerId = cp.peerId;
- $rootScope.connectedPeers = cp.connectedPeers;
+ $rootScope.peedId = net.peerId;
+ $rootScope.connectedPeers = net.connectedPeers;
$rootScope.$digest();
};
- var _store = function() {
- Storage.set($rootScope.walletId, 'peerData', {
- peerId: $rootScope.peerId,
- connectedPeers: $rootScope.connectedPeers
- });
- };
-
-
// set new inbound connections
var _setNewPeer = function(newPeer) {
- var cp = $rootScope.cp;
+ var w = $rootScope.wallet;
console.log('#### Setting new PEER:', newPeer);
- sendPublicKeyRing(newPeer);
- sendTxProposals(newPeer);
-
+ w.sendPublicKeyRing(newPeer);
+ w.sendTxProposals(newPeer);
};
var _handleNetworkChange = function(newPeer) {
@@ -36,186 +27,154 @@ angular.module('copay.network')
if (newPeer)
_setNewPeer(newPeer);
- _store();
_refreshUx();
};
// TODO -> probably not in network.js
var storeOpenWallet = function() {
- var id = $rootScope.walletId;
- Storage.addWalletId(id);
- Storage.set(id, 'wallet',$rootScope.wallet.toObj());
- console.log('\t### Wallet %s Stored', id);
+ var w = $rootScope.wallet;
+ w.store();
+ console.log('\t### Wallet %s Stored', w.id);
};
- // TODO -> probably not in network.js
- var createWallet = function(walletId) {
-
- opts.walletId = walletId;
- var w = new copay.Wallet(opts, config);
-
- // Store it on rootScope
- $rootScope.walletId = pkr.id;
- $rootScope.wallet = w;
-
-//TODO
-// w.store();
- };
-
- var openWallet = function (walletId) {
-
-
- var w = Wallet.read(walletId);
-
- if (w && w.publicKeyRing.copayersExtPubKeys.length && w.privateKey) {
- console.log('### WALLET OPENED:', w.walletId);
- $rootScope.walletId = walletId;
+ // TODO -> probably not in network.js
+ var createWallet = function(walletId) {
+ var w = $rootScope.wallet || new copay.Wallet(config);
+ w.create({id: walletId});
+ w.store();
$rootScope.wallet = w;
- }
- };
- var closeWallet = function() {
- console.log('### CLOSING WALLET');
- $rootScope.delete('walletId');
- $rootScope.delete('w');
- };
+ console.log('createWallet ENDED'); //TODO
+ };
- var _checkWallet = function(walletId, allowChange) {
- console.log('[network.js.79:_checkWallet:]',walletId); //TODO
-
- if ($rootScope.walletId && $rootScope.walletId === walletId)
- return;
-
- if ($rootScope.walletId && $rootScope.walletId !== walletId) {
- throw new Error('message to wrong walletID');
- }
-
-
- if (!openWallet(walletId)) {
- createWallet(walletId);
- }
- };
+ var openWallet = function (walletId) {
+ var w = $rootScope.wallet || new copay.Wallet(config);
+ w.load(walletId);
+ if (w && w.publicKeyRing && w.privateKey) {
+ console.log('### WALLET OPENED:', w.walletId);
+ $rootScope.wallet = w;
+ }
+ };
- var sendTxProposals = function(recipients) {
- var cp = $rootScope.cp;
- console.log('### SENDING txProposals TO:', recipients||'All', $rootScope.txProposals);
- cp.send( recipients, {
- type: 'txProposals',
- txProposals: $rootScope.txProposals.toObj(),
- walletId: $rootScope.walletId,
- });
- };
- var sendPublicKeyRing = function(recipients) {
- var cp = $rootScope.cp;
- console.log('### SENDING publicKeyRing TO:', recipients||'All');
-
- cp.send(recipients, {
- type: 'publicKeyRing',
- publicKeyRing: $rootScope.publicKeyRing.toObj(),
- });
- };
+ var closeWallet = function() {
+ var w = $rootScope.wallet;
+ if (w && w.id)
+ w.store();
- var _handlePublicKeyRing = function(senderId, data, isInbound) {
- var cp = $rootScope.cp;
- _checkWallet(data.publicKeyRing.id);
- var shouldSend = false;
+ console.log('### CLOSING WALLET');
+ delete $rootScope['wallet'];
+ };
- var recipients, pkr = $rootScope.publicKeyRing;
- var inPKR = copay.PublicKeyRing.fromObj(data.publicKeyRing);
-console.log('[network.js.176:inPKR:]',inPKR); //TODO
-console.log('[network.js.178:pkr:]',pkr); //TODO
- if (pkr.merge(inPKR, true) && !data.isBroadcast) {
- console.log('### BROADCASTING PKR');
- recipients = null;
- shouldSend = true;
- }
- else if (isInbound && !data.isBroadcast) {
- // always replying to connecting peer
- console.log('### REPLYING PKR TO:', senderId);
- recipients = senderId;
- shouldSend = true;
- }
+ var _checkWallet = function(walletId) {
+ console.log('[network.js.79:_checkWallet:]',walletId); //TODO
- console.log('[network.js.189:shouldSend:]',shouldSend); //TODO
- if (shouldSend) {
- sendPublicKeyRing(recipients);
- }
- _refreshUx();
- };
- var _handleTxProposals = function(senderId, data, isInbound) {
- var cp = $rootScope.cp;
- _checkWallet(data.txProposals.walletId);
+ if ($rootScope.wallet && $rootScope.wallet.id === walletId)
+ return;
- var shouldSend = false;
- console.log('RECV TXPROPOSAL:',data); //TODO
- var recipients, pkr = $rootScope.txProposals;
+ if ($rootScope.wallet && $rootScope.wallet.id && $rootScope.wallet.id !== walletId) {
+ throw new Error('message to wrong walletID');
+ }
+
+ if (!openWallet(walletId)) {
+ createWallet(walletId);
+ }
+ };
- var inTxProposals = copay.TxProposals.fromObj(data.txProposals);
- var mergeInfo = pkr.merge(inTxProposals, true);
- if ( mergeInfo.merged && !data.isBroadcast) {
- console.log('### BROADCASTING txProposals');
- recipients = null;
- shouldSend = true;
- }
- else if (isInbound && !data.isBroadcast) {
- // always replying to connecting peer
- console.log('### REPLYING txProposals TO:', senderId);
- recipients = senderId;
- shouldSend = true;
- }
+ var _handlePublicKeyRing = function(senderId, data, isInbound) {
+ _checkWallet(data.walletId);
+ var shouldSend = false;
- if (shouldSend) {
- sendTxProposals(recipients);
- }
- };
+ var w = $rootScope.wallet;
+ var recipients, pkr = w.publicKeyRing;
+ var inPKR = copay.PublicKeyRing.fromObj(data.publicKeyRing);
+ if (pkr.merge(inPKR, true) && !data.isBroadcast) {
+ console.log('### BROADCASTING PKR');
+ recipients = null;
+ shouldSend = true;
+ }
+ else if (isInbound && !data.isBroadcast) {
+ // always replying to connecting peer
+ console.log('### REPLYING PKR TO:', senderId);
+ recipients = senderId;
+ shouldSend = true;
+ }
- var _handleData = function(senderId, data, isInbound) {
+ if (shouldSend) {
+ w.sendPublicKeyRing(recipients);
+ }
+ _refreshUx();
+ };
- switch(data.type) {
- case 'publicKeyRing':
- _handlePublicKeyRing(senderId, data, isInbound);
- break;
- case 'txProposals':
- _handleTxProposals(senderId, data, isInbound);
- break;
- case 'abort':
- disconnect();
- _refreshUx();
- break;
- }
- };
- var _setupHandlers = function () {
- var cp = $rootScope.cp;
- cp.on('networkChange', _handleNetworkChange);
- cp.on('data', _handleData);
- };
+ var _handleTxProposals = function(senderId, data, isInbound) {
+ _checkWallet(data.walletId);
- // public methods
- var init = function(cb) {
- var cp = $rootScope.cp = new copay.Network({
- apiKey: config.p2pApiKey,
- debug: config.p2pDebug,
- maxPeers: config.maxPeers, // TODO: This should be on wallet configuration
- });
- _setupHandlers();
+ var shouldSend = false;
+ var w = $rootScope.wallet;
+ console.log('RECV TXPROPOSAL:',data); //TODO
- cp.start(function(peerId) {
- return cb();
- });
+ var recipients;
+ var inTxProposals = copay.TxProposals.fromObj(data.txProposals);
+ var mergeInfo = w.txProposals.merge(inTxProposals, true);
+ if ( mergeInfo.merged && !data.isBroadcast) {
+ console.log('### BROADCASTING txProposals');
+ recipients = null;
+ shouldSend = true;
+ }
+ else if (isInbound && !data.isBroadcast) {
+ // always replying to connecting peer
+ console.log('### REPLYING txProposals TO:', senderId);
+ recipients = senderId;
+ shouldSend = true;
+ }
+
+ if (shouldSend) {
+ w.sendTxProposals(recipients);
+ }
+ };
+
+ var _handleData = function(senderId, data, isInbound) {
+
+ switch(data.type) {
+ case 'publicKeyRing':
+ _handlePublicKeyRing(senderId, data, isInbound);
+ break;
+ case 'txProposals':
+ _handleTxProposals(senderId, data, isInbound);
+ break;
+ case 'abort':
+ disconnect();
+ _refreshUx();
+ break;
+ }
+ };
+
+ // public methods
+ var init = function(cb) {
+ if (!$rootScope.wallet) {
+ // create an empty Wallet
+ $rootScope.wallet = new copay.Wallet(config);
+ }
+ var net = $rootScope.wallet.network;
+ net.on('networkChange', _handleNetworkChange);
+ net.on('data', _handleData);
+ net.start(function(peerId) {
+ return cb();
+ });
};
var disconnect = function() {
- if ($rootScope.cp) {
- $rootScope.cp.disconnect();
+ var w = $rootScope.wallet;
+ var net = w.network;
+
+ if (net) {
+ net.disconnect();
}
closeWallet();
- Storage.remove('peerData');
- $rootScope.isLogged = false;
};
var connect = function(peerId, openCallback, failCallback) {
- if ($rootScope.cp) {
- $rootScope.cp.connectTo(peerId, openCallback, function () {
+ if ($rootScope.wallet.network) {
+ $rootScope.wallet.network.connectTo(peerId, openCallback, function () {
disconnect();
failCallback();
});
@@ -224,16 +183,21 @@ console.log('[network.js.178:pkr:]',pkr); //TODO
return failCallback();
};
+ var sendTxProposals = function(recipients) {
+ var w = $rootScope.wallet;
+ w.sendTxProposals(recipients);
+ };
+
return {
init: init,
connect: connect,
disconnect: disconnect,
- sendTxProposals: sendTxProposals,
-// TODO Move to Wallet.
createWallet: createWallet,
openWallet: openWallet,
storeOpenWallet: storeOpenWallet,
+
+ sendTxProposals: sendTxProposals,
}
});
diff --git a/js/services/storage.js b/js/services/storage.js
deleted file mode 100644
index 489710a5d..000000000
--- a/js/services/storage.js
+++ /dev/null
@@ -1,33 +0,0 @@
-'use strict';
-
-angular.module('copay.storage')
- .factory('Storage', function($rootScope) {
-
- return {
- getGlobal: function(key) {
- return JSON.parse(localStorage.getItem(key));
- },
- setGlobal: function(key, data) {
- localStorage.setItem(key, JSON.stringify(data));
- },
- get: function(walletId, key) {
- if (!walletId) return;
- return JSON.parse(localStorage.getItem(_key(walletId, key)));
- },
- set: function(walletId, key, data) {
- if (!walletId) return;
- var k = _key(walletId, key);
- localStorage.setItem(k, JSON.stringify(data));
- _pushKey(walletId, k);
- },
- remove: function(walletId, key) {
- localStorage.removeItem(_key(walletId, key));
- },
- clearAll: function(walletId) {
- var keys = localStorage.getItem(walletId);
- keys.split(',').forEach(function(k) {
- localStorage.removeItem(key);
- });
- },
- };
- });
diff --git a/test/test.publickeyring.js b/test/test.publickeyring.js
index c96a3f4ef..ac29a9647 100644
--- a/test/test.publickeyring.js
+++ b/test/test.publickeyring.js
@@ -29,6 +29,7 @@ var createW = function (networkName) {
var newEpk = w.addCopayer();
copayers.push(newEpk);
}
+ w.walletId = '1234567';
return {w:w, copayers: copayers};
};
@@ -72,19 +73,17 @@ describe('PublicKeyRing model', function() {
it('show be able to tostore and read', function () {
var k = createW();
var w = k.w;
- w.id = 'lwjd5qra8257b9';
var copayers = k.copayers;
for(var i=0; i<3; i++)
w.generateAddress(true);
for(var i=0; i<5; i++)
w.generateAddress(false);
- var data = w.toStore();
+ var data = w.toObj();
should.exist(data);
- var ID = w.id;
-
- var w2 = PublicKeyRing.read(data, ID, 'dummy' );
+ var w2 = PublicKeyRing.fromObj(data);
+ w2.walletId.should.equal(w.walletId);
w2.isComplete().should.equal(true);
w2.addCopayer.bind().should.throw();
for(var i =0; i<5; i++)
@@ -157,7 +156,7 @@ describe('PublicKeyRing model', function() {
var w2 = new PublicKeyRing({
networkName: 'livenet',
- id: w.id,
+ walletId: w.walletId,
});
w2.merge(w).should.equal(true);
w2.requiredCopayers.should.equal(3);
@@ -176,23 +175,23 @@ describe('PublicKeyRing model', function() {
};
var w = new PublicKeyRing(config);
- w.id = 'lwjd5qra8257b9';
+ w.walletId = 'lwjd5qra8257b9';
var w2 = new PublicKeyRing({
networkName: 'testnet', //wrong
- id: w.id,
+ walletId: w.walletId,
});
(function() { w2.merge(w);}).should.throw();
var w3 = new PublicKeyRing({
networkName: 'livenet',
- id: w.id,
+ walletId: w.walletId,
requiredCopayers: 2, // wrong
});
(function() { w3.merge(w);}).should.throw();
var w4 = new PublicKeyRing({
networkName: 'livenet',
- id: w.id,
+ walletId: w.walletId,
totalCopayers: 3, // wrong
});
(function() { w4.merge(w);}).should.throw();