Merge pull request #98 from matiu/feature/ux6

Feature/ux6
This commit is contained in:
Ryan X. Charles 2014-04-20 12:51:02 -03:00
commit 9ebdd86f02
8 changed files with 87 additions and 56 deletions

View file

@ -64,7 +64,7 @@
<span ng-show="$root.wallet.publicKeyRing.totalCopayers - $root.wallet.publicKeyRing.registeredCopayers()==1"> <span ng-show="$root.wallet.publicKeyRing.totalCopayers - $root.wallet.publicKeyRing.registeredCopayers()==1">
One key is One key is
</span> </span>
missing. Share this secret with your other copayers for them to join your wallet: <b>{{$root.wallet.network.peerId}}</b> missing. Share this secret with your other copayers for them to join your wallet: <b>{{$root.wallet.getMyPeerId()}}</b>
</div> </div>
</div> </div>

View file

@ -1,6 +1,3 @@
'use strict';
angular.module('copay.signin').controller('SigninController', angular.module('copay.signin').controller('SigninController',
function($scope, $rootScope, $location, walletFactory, controllerUtils) { function($scope, $rootScope, $location, walletFactory, controllerUtils) {
@ -20,22 +17,29 @@ angular.module('copay.signin').controller('SigninController',
$scope.open = function(walletId, opts) { $scope.open = function(walletId, opts) {
$scope.loading = true; $scope.loading = true;
console.log('[signin.js.23:walletId:]',walletId); //TODO
var w = walletFactory.open(walletId, opts); var w = walletFactory.open(walletId, opts);
controllerUtils.setupUxHandlers(w); controllerUtils.setupUxHandlers(w);
w.netStart();
}; };
$scope.join = function(cid) { $scope.join = function(secret) {
$scope.loading = true; $scope.loading = true;
walletFactory.network.on('openError', function() {
controllerUtils.onError($scope);
$rootScope.$digest(); walletFactory.network.on('joinError', function() {
controllerUtils.onErrorDigest($scope);
}); });
walletFactory.connectTo(cid, function(data) {
$scope.open(data.walletId, data.opts); walletFactory.joinCreateSession(secret, function(w) {
console.log('[signin.js.33] joinCreateSession RETURN', w); //TODO
controllerUtils.setupUxHandlers(w);
w.setupNetHandlers();
}); });
}; };
// //
// if (cid) { // if (cid) {
// var w = walletFactory.(walletId); // var w = walletFactory.(walletId);

View file

@ -83,8 +83,11 @@ PublicKeyRing.prototype.getCopayerId = function(i, prefix) {
buf = Buffer.concat([prefix, buf]); buf = Buffer.concat([prefix, buf]);
} }
return util.ripe160(buf).toString('hex'); return util.ripe160(buf).toString('hex');
} };
PublicKeyRing.prototype.myCopayerId = function(i, prefix) {
return this.getCopayerId(0,prefix);
};
PublicKeyRing.prototype.registeredCopayers = function () { PublicKeyRing.prototype.registeredCopayers = function () {
return this.copayersBIP32.length; return this.copayersBIP32.length;

View file

@ -97,6 +97,7 @@ Wallet.prototype._handleTxProposals = function(senderId, data, isInbound) {
}; };
Wallet.prototype._handleData = function(senderId, data, isInbound) { Wallet.prototype._handleData = function(senderId, data, isInbound) {
console.log('_handleData: senderId:',senderId); //TODO
if (this.id !== data.walletId) { if (this.id !== data.walletId) {
this.emit('badMessage',senderId); this.emit('badMessage',senderId);
@ -136,19 +137,27 @@ Wallet.prototype._optsToObj = function () {
}; };
Wallet.prototype.generatePeerId = function(index) { Wallet.prototype.getPeerId = function(index) {
var idBuf = new Buffer(this.id); // if (typeof index === 'undefined') {
if (typeof index === 'undefined') { // // return my own peerId
// return my own peerId // var gen = this.privateKey.getId(idBuf);
var gen = this.privateKey.getId(idBuf); // return gen;
return gen; // }
}
// return peer number 'index' peerId // return peer number 'index' peerId
return this.publicKeyRing.getCopayerId(index, idBuf); //
var idBuf;
// TODO idBuf DISABLED FOR NOW
//idBuf = new Buffer(this.id);
return this.publicKeyRing.getCopayerId(index || 0, idBuf);
};
Wallet.prototype.getMyPeerId = function() {
return this.getPeerId(0);
}; };
Wallet.prototype.netStart = function() { Wallet.prototype.netStart = function() {
console.log('[Wallet.js.159:netStart:]'); //TODO
var self = this; var self = this;
var net = this.network; var net = this.network;
net.removeAllListeners(); net.removeAllListeners();
@ -162,15 +171,17 @@ Wallet.prototype.netStart = function() {
net.on('close', function() { net.on('close', function() {
self.emit('close'); self.emit('close');
}); });
var myPeerId = self.getMyPeerId();
var startOpts = { var startOpts = {
peerId: self.generatePeerId() peerId: myPeerId
} };
net.start(function(peerId) { net.start(function() {
console.log('[Wallet.js.177] NET START: emit CREATED'); //TODO
self.emit('created'); self.emit('created');
var myId = self.generatePeerId();
for (var i=0; i<self.publicKeyRing.registeredCopayers(); i++) { for (var i=0; i<self.publicKeyRing.registeredCopayers(); i++) {
var otherPeerId = self.generatePeerId(i); var otherPeerId = self.getPeerId(i);
if (otherPeerId !== myId) { if (otherPeerId !== myPeerId) {
net.connectTo(otherPeerId); net.connectTo(otherPeerId);
} }
} }

View file

@ -39,10 +39,13 @@ WalletFactory.prototype.log = function(){
WalletFactory.prototype._checkRead = function(walletId) { WalletFactory.prototype._checkRead = function(walletId) {
var s = this.storage; var s = this.storage;
var ret = s.get(walletId, 'publicKeyRing') && var ret =
s.get(walletId, 'txProposals') && (
s.get(walletId, 'opts') && s.get(walletId, 'publicKeyRing') &&
s.get(walletId, 'privateKey') s.get(walletId, 'txProposals') &&
s.get(walletId, 'opts') &&
s.get(walletId, 'privateKey')
)?true:false;
; ;
return ret?true:false; return ret?true:false;
}; };
@ -53,17 +56,14 @@ WalletFactory.prototype.read = function(walletId) {
var s = this.storage; var s = this.storage;
var opts = s.get(walletId, 'opts'); var opts = s.get(walletId, 'opts');
opts.id = walletId; opts.id = walletId;
opts.publicKeyRing = new PublicKeyRing.fromObj(s.get(walletId, 'publicKeyRing')); opts.publicKeyRing = new PublicKeyRing.fromObj(s.get(walletId, 'publicKeyRing'));
opts.txProposals = new TxProposals.fromObj(s.get(walletId, 'txProposals')); opts.txProposals = new TxProposals.fromObj(s.get(walletId, 'txProposals'));
opts.privateKey = new PrivateKey.fromObj(s.get(walletId, 'privateKey')); opts.privateKey = new PrivateKey.fromObj(s.get(walletId, 'privateKey'));
opts.storage = this.storage; opts.storage = this.storage;
opts.network = this.network; opts.network = this.network;
opts.blockchain = this.blockchain; opts.blockchain = this.blockchain;
opts.verbose = this.verbose; opts.verbose = this.verbose;
var w = new Wallet(opts); var w = new Wallet(opts);
// JIC: Add our key // JIC: Add our key
@ -81,10 +81,13 @@ WalletFactory.prototype.read = function(walletId) {
WalletFactory.prototype.create = function(opts) { WalletFactory.prototype.create = function(opts) {
var s = WalletFactory.storage; var s = WalletFactory.storage;
opts = opts || {}; opts = opts || {};
this.log('### CREATING NEW WALLET.' + (opts.id ? ' USING ID: ' + opts.id : ' NEW ID')); this.log('### CREATING NEW WALLET.' +
(opts.id ? ' USING ID: ' + opts.id : ' NEW ID') +
(opts.privateKey ? ' USING PrivateKey: ' + opts.privateKey.getId() : ' NEW PrivateKey')
);
opts.privateKey = opts.privateKey || new PrivateKey({ networkName: this.networkName }); opts.privateKey = opts.privateKey || new PrivateKey({ networkName: this.networkName });
this.log('\t### PrivateKey Initialized');
var requiredCopayers = opts.requiredCopayers || this.walletDefaults.requiredCopayers; var requiredCopayers = opts.requiredCopayers || this.walletDefaults.requiredCopayers;
var totalCopayers = opts.totalCopayers || this.walletDefaults.totalCopayers; var totalCopayers = opts.totalCopayers || this.walletDefaults.totalCopayers;
@ -134,12 +137,19 @@ WalletFactory.prototype.remove = function(walletId) {
}; };
WalletFactory.prototype.connectTo = function(peerId, cb) { WalletFactory.prototype.joinCreateSession = function(peerId, cb) {
var self=this; var self = this;
//Create our PrivateK
var privateKey = new PrivateKey({ networkName: this.networkName });
this.log('\t### PrivateKey Initialized');
self.network.setPeerId(privateKey.getId());
self.network.start(function() { self.network.start(function() {
self.network.connectTo(peerId) self.network.connectTo(peerId);
self.network.on('walletId', function(data) { self.network.on('walletId', function(data) {
return cb(data); data.opts.privateKey = privateKey;
return cb(self.open(data.walletId, data.opts));
}); });
}); });
}; };

View file

@ -182,10 +182,11 @@ Network.prototype._setupPeerHandlers = function(openCallback) {
var self=this; var self=this;
var p = this.peer; var p = this.peer;
p.on('open', function(peerId) { p.on('open', function() {
self.peerId = peerId; self.connectedPeers = [self.peerId];
self.connectedPeers = [peerId];
return openCallback(peerId); console.log('[WebRTC.js.187] LENGTH', self.connectedPeers.length); //TODO
return openCallback();
}); });
p.on('error', function(err) { p.on('error', function(err) {
@ -210,23 +211,21 @@ Network.prototype._setupPeerHandlers = function(openCallback) {
}); });
}; };
Network.prototype.setPeerId = function(peerId) {
if (this.started) {
throw new Error ('network already started: can not change peerId')
}
this.peerId = peerId;
};
Network.prototype.start = function(openCallback, opts) { Network.prototype.start = function(openCallback, opts) {
opts = opts || {}; opts = opts || {};
// Start PeerJS Peer // Start PeerJS Peer
var self = this; var self = this;
if (this.started) {
// network already started, restarting network layer
opts.connectedPeers = this.connectedPeers;
Network._arrayRemove(this.peerId, opts.connectedPeers);
this.disconnect(function() {
self.start(openCallback, opts);
}, true); // fast disconnect
return;
}
if (this.started) return openCallback();
opts = opts || {};
opts.connectedPeers = opts.connectedPeers || []; opts.connectedPeers = opts.connectedPeers || [];
this.peerId = this.peerId || opts.peerId; this.peerId = this.peerId || opts.peerId;
@ -237,6 +236,8 @@ Network.prototype.start = function(openCallback, opts) {
this.connectTo(otherPeerId); this.connectTo(otherPeerId);
} }
this.started = true; this.started = true;
console.log('[WebRTC.js.237] started TRUE'); //TODO
}; };
Network.prototype._sendToOne = function(peerId, data, cb) { Network.prototype._sendToOne = function(peerId, data, cb) {
@ -283,7 +284,7 @@ console.log('[WebRTC.js.258:peerId:]',peerId); //TODO
Network.prototype.connectTo = function(peerId) { Network.prototype.connectTo = function(peerId) {
var self = this; var self = this;
console.log('### STARTING TO CONNECT TO:' + peerId ); console.log('### STARTING CONNECTION TO:' + peerId );
var dataConn = this.peer.connect(peerId, { var dataConn = this.peer.connect(peerId, {
serialization: 'none', serialization: 'none',

View file

@ -28,6 +28,7 @@ angular.module('copay.controllerUtils').factory('controllerUtils', function ($ro
}); });
w.on('created', function() { w.on('created', function() {
console.log('[controllerUtils.js.30:created:] RECV '); //TODO
$location.path('peer'); $location.path('peer');
$rootScope.wallet = w; $rootScope.wallet = w;
@ -42,8 +43,8 @@ angular.module('copay.controllerUtils').factory('controllerUtils', function ($ro
}); });
w.on('openError', root.onErrorDigest); w.on('openError', root.onErrorDigest);
w.on('close', root.onErrorDigest); w.on('close', root.onErrorDigest);
w.netStart(); w.netStart();
console.log('[controllerUtils.js.45] setupUxHandlers END'); //TODO
}; };
root.handleTransactionByAddress = function(scope, cb) { root.handleTransactionByAddress = function(scope, cb) {

View file

@ -74,7 +74,8 @@ describe('PrivateKey model', function() {
}); });
it('fromObj toObj roundtrip', function () { it('fromObj toObj roundtrip', function () {
var w1 = new PrivateKey(config); var w1 = new PrivateKey(config);
var w2 = PrivateKey.fromObj(w1.toObj()); var o = JSON.parse(JSON.stringify(w1.toObj()))
var w2 = PrivateKey.fromObj(o);
w2.toObj().extendedPrivateKeyString.should.equal(w1.toObj().extendedPrivateKeyString); w2.toObj().extendedPrivateKeyString.should.equal(w1.toObj().extendedPrivateKeyString);
w2.getId().should.equal(w1.getId()); w2.getId().should.equal(w1.getId());