fix join wallet WIP

This commit is contained in:
Matias Alejo Garcia 2014-04-16 20:58:57 -03:00
parent e676f4e69d
commit 8856683a17
6 changed files with 96 additions and 13 deletions

View File

@ -41,7 +41,6 @@ angular.module('copay.header').controller('HeaderController',
w.disconnect();
delete $rootScope['wallet'];
$location.path('signin');
$rootScope.$digest();
}
};

View File

@ -46,6 +46,14 @@ console.log('[signin.js.42:create:]'); //TODO
$scope.join = function(cid) {
console.log('[signin.js.42:join:]'); //TODO
$scope.loading = true;
walletFactory.connectTo(cid, function(w) {
console.log('[signin.js.50]'); //TODO
_setupUxHandlers(w);
w.netStart();
});
};
//
// if (cid) {
// var w = walletFactory.(walletId);
@ -62,7 +70,6 @@ console.log('[signin.js.42:join:]'); //TODO
// });
// });
// }
};
// if (peerData && peerData.peerId && peerData.connectedPeers.length > 0) {
// $rootScope.peerId = peerData.peerId;

View File

@ -39,7 +39,6 @@ Wallet.getRandomId = function() {
};
Wallet.prototype._handlePublicKeyRing = function(senderId, data, isInbound) {
this.openWalletId(data.walletId);
this.log('RECV PUBLICKEYRING:',data);
var shouldSend = false;
@ -66,7 +65,6 @@ Wallet.prototype._handlePublicKeyRing = function(senderId, data, isInbound) {
Wallet.prototype._handleTxProposals = function(senderId, data, isInbound) {
this.openWalletId(data.walletId);
this.log('RECV TXPROPOSAL:',data); //TODO
var shouldSend = false;
@ -94,6 +92,11 @@ Wallet.prototype._handleTxProposals = function(senderId, data, isInbound) {
};
Wallet.prototype._handleData = function(senderId, data, isInbound) {
if (this.id !== data.walletId)
throw new Error('wrong message received: Bad wallet ID');
switch(data.type) {
case 'publicKeyRing':
this._handlePublicKeyRing(senderId, data, isInbound);
@ -108,9 +111,13 @@ Wallet.prototype._handleData = function(senderId, data, isInbound) {
};
Wallet.prototype._handleNetworkChange = function(newPeer) {
console.log('[Wallet.js.112:newPeer:]',newPeer); //TODO
if (!newPeer) return;
console.log('[Wallet.js.112:newPeer:]',newPeer); //TODO
this.log('#### Setting new PEER:', newPeer);
this.sendWalletId(newPeer);
this.sendPublicKeyRing(newPeer);
this.sendTxProposals(newPeer);
};
@ -118,8 +125,8 @@ Wallet.prototype._handleNetworkChange = function(newPeer) {
Wallet.prototype.netStart = function() {
var self = this;
var net = this.network;
net.on('networkChange', function() { self._handleNetworkChange(); } );
net.on('data', function() { self._handleData();});
net.on('networkChange', self._handleNetworkChange.bind(self) );
net.on('data', self._handleData.bind(self) );
net.on('open', function() {}); // TODO
net.on('close', function() {}); // TODO
net.start(function(peerId) {
@ -151,6 +158,17 @@ Wallet.prototype.sendTxProposals = function(recipients) {
this.emit('txProposalsUpdated', this.txProposals);
};
Wallet.prototype.sendWalletId = function(recipients) {
this.log('### SENDING walletId TO:', recipients||'All', this.walletId);
this.network.send(recipients, {
type: 'walletId',
walletId: this.id,
});
};
Wallet.prototype.sendPublicKeyRing = function(recipients) {
this.log('### SENDING publicKeyRing TO:', recipients||'All', this.publicKeyRing.toObj());
@ -308,8 +326,13 @@ Wallet.prototype.createTxSync = function(toAddress, amountSatStr, utxos, opts) {
};
Wallet.prototype.connectTo = function(peerId) {
this.network.connectTo(peerId);
throw new Error('Wallet.connectTo.. not yet implemented!');
};
Wallet.prototype.disconnect = function() {
this.network.disconnect();
};
// // HERE? not sure
// Wallet.prototype.cleanPeers = function() {
// this.storage.remove('peerData');

View File

@ -60,6 +60,7 @@ WalletFactory.prototype.read = function(walletId) {
opts.storage = this.storage;
opts.network = this.network;
opts.blockchain = this.blockchain;
opts.verbose = this.verbose;
var w = new Wallet(opts);
@ -102,6 +103,8 @@ WalletFactory.prototype.create = function(opts) {
opts.storage = this.storage;
opts.network = this.network;
opts.blockchain = this.blockchain;
opts.verbose = this.verbose;
opts.spendUnconfirmed = opts.spendUnconfirmed || this.walletDefaults.spendUnconfirmed;
opts.requiredCopayers = requiredCopayers;
opts.totalCopayers = totalCopayers;
@ -112,9 +115,44 @@ WalletFactory.prototype.create = function(opts) {
};
WalletFactory.prototype.open = function(walletId) {
if(!WalletFactory.read(walletId)) {
WalletFactory.create({id: walletId});
}
var w = this.read(walletId) || this.create({id: walletId});
return w;
};
WalletFactory.prototype.openRemote = function(peedId) {
var s = WalletFactory.storage;
opts = opts || {};
this.log('### CREATING NEW WALLET.' + (opts.id ? ' USING ID: ' + opts.id : ' NEW ID'));
opts.privateKey = opts.privateKey || new PrivateKey({ networkName: this.networkName });
this.log('\t### PrivateKey Initialized');
var requiredCopayers = opts.requiredCopayers || this.walletDefaults.requiredCopayers;
var totalCopayers = opts.totalCopayers || this.walletDefaults.totalCopayers;
opts.publicKeyRing = opts.publicKeyRing || new PublicKeyRing({
networkName: this.networkName,
requiredCopayers: requiredCopayers,
totalCopayers: totalCopayers,
});
opts.publicKeyRing.addCopayer(opts.privateKey.getBIP32().extendedPublicKeyString());
this.log('\t### PublicKeyRing Initialized');
opts.txProposals = opts.txProposals || new TxProposals({
networkName: this.networkName,
});
this.log('\t### TxProposals Initialized');
opts.storage = this.storage;
opts.network = this.network;
opts.blockchain = this.blockchain;
opts.spendUnconfirmed = opts.spendUnconfirmed || this.walletDefaults.spendUnconfirmed;
opts.requiredCopayers = requiredCopayers;
opts.totalCopayers = totalCopayers;
var w = new Wallet(opts);
w.store();
this.addWalletId(w.id);
return w;
};
WalletFactory.prototype.getWalletIds = function() {
@ -142,4 +180,16 @@ WalletFactory.prototype.addWalletId = function(walletId) {
this.storage.setGlobal('walletIds', ids);
};
WalletFactory.prototype.connectTo = function(peerId, cb) {
var self=this;
self.network.start(function() {
self.network.connectTo(peerId)
self.network.on('walletId', function(walletId) {
console.log('[WalletFactory.js.187]'); //TODO
return cb(self.open(walletId));
});
});
};
module.exports = require('soop')(WalletFactory);

View File

@ -103,6 +103,9 @@ Network.prototype._onData = function(data, isInbound) {
case 'disconnect':
this._onClose(obj.sender);
break;
case 'walletId':
this.emit('walletId', obj.data.walletId);
break;
default:
this.emit('data', obj.sender, obj.data, isInbound);
}
@ -158,7 +161,6 @@ Network.prototype._setupConnectionHandlers = function(dataConn, isInbound) {
dataConn.on('close', function() {
if (self.closing) return;
console.log('### CLOSE RECV FROM:', dataConn.peer);
self._onClose(dataConn.peer);
this.emit('close');
});
@ -166,6 +168,8 @@ Network.prototype._setupConnectionHandlers = function(dataConn, isInbound) {
Network.prototype._notify = function(newPeer) {
this._showConnectedPeers();
console.log('[WebRTC.js.172]', newPeer); //TODO
this.emit('networkChange', newPeer);
};
@ -269,7 +273,7 @@ Network.prototype.connectTo = function(peerId) {
};
Network.prototype.disconnect = function(peerId, cb) {
Network.prototype.disconnect = function(cb) {
var self = this;
self.closing = 1;

View File

@ -5,7 +5,7 @@ var should = chai.should();
var WebRTC = require('../js/models/network/WebRTC');
var Insight = require('../js/models/blockchain/Insight');
var FakeStorage = require('./FakeStorage');
var FakeStorage = require('./mocks/FakeStorage');
var WalletFactory = typeof copay === 'undefined' ? require('soop').load('../js/models/core/WalletFactory',{
Network: WebRTC,