fix peers, pubkey WIP
This commit is contained in:
parent
a658ec8452
commit
9ca83e35fc
11 changed files with 228 additions and 142 deletions
11
index.html
11
index.html
|
|
@ -85,6 +85,14 @@
|
||||||
<div class="row" ng-controller="PeerController" ng-init="init()">
|
<div class="row" ng-controller="PeerController" ng-init="init()">
|
||||||
<div class="large-6 columns">
|
<div class="large-6 columns">
|
||||||
<h1>I am <p class="text-muted">{{$root.peerId}}</p></h1>
|
<h1>I am <p class="text-muted">{{$root.peerId}}</p></h1>
|
||||||
|
|
||||||
|
<ul class="no-bullet">
|
||||||
|
<li> [DEBUG] Pubkeys that you have: {{$root.publicKeyRing.registeredCopayers()}}
|
||||||
|
<li class="panel" style="word-wrap: break-word;" ng-repeat="pub in $root.publicKeyRing.copayersBIP32">
|
||||||
|
${{pub.extendedPublicKeyString()}}
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<h3 class="panel-title">Copayers ({{$root.connectedPeers.length}}/5)</h3>
|
||||||
</div>
|
</div>
|
||||||
<div class="large-6 columns">
|
<div class="large-6 columns">
|
||||||
<h3 class="panel-title">Copayers ({{$root.connectedPeers.length}}/5)</h3>
|
<h3 class="panel-title">Copayers ({{$root.connectedPeers.length}}/5)</h3>
|
||||||
|
|
@ -214,11 +222,12 @@
|
||||||
<script src="lib/angular-foundation/mm-foundation-tpls.min.js"></script>
|
<script src="lib/angular-foundation/mm-foundation-tpls.min.js"></script>
|
||||||
<script src="lib/peerjs/peer.js"></script>
|
<script src="lib/peerjs/peer.js"></script>
|
||||||
<script src="lib/bitcore.js"></script>
|
<script src="lib/bitcore.js"></script>
|
||||||
<script src="lib/copay.js"></script>
|
<script src="lib/copayBundle.js"></script>
|
||||||
|
|
||||||
|
|
||||||
<script src="js/app.js"></script>
|
<script src="js/app.js"></script>
|
||||||
<script src="js/config.js"></script>
|
<script src="js/config.js"></script>
|
||||||
|
<script src="js/routes.js"></script>
|
||||||
<script src="js/directives.js"></script>
|
<script src="js/directives.js"></script>
|
||||||
<script src="js/filters.js"></script>
|
<script src="js/filters.js"></script>
|
||||||
<script src="js/services/network.js"></script>
|
<script src="js/services/network.js"></script>
|
||||||
|
|
|
||||||
46
js/config.js
46
js/config.js
|
|
@ -1,45 +1,5 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
//Setting up route
|
var config = {
|
||||||
angular
|
networkName: 'testnet',
|
||||||
.module('copay')
|
};
|
||||||
.config(function($routeProvider) {
|
|
||||||
|
|
||||||
$routeProvider
|
|
||||||
.when('/', {
|
|
||||||
templateUrl: 'signin.html'
|
|
||||||
})
|
|
||||||
.when('/signin', {
|
|
||||||
templateUrl: 'signin.html'
|
|
||||||
})
|
|
||||||
.when('/home', {
|
|
||||||
templateUrl: 'home.html'
|
|
||||||
})
|
|
||||||
.when('/join/:id', {
|
|
||||||
templateUrl: 'join.html'
|
|
||||||
})
|
|
||||||
.when('/peer', {
|
|
||||||
templateUrl: 'peer.html'
|
|
||||||
})
|
|
||||||
.when('/transactions', {
|
|
||||||
templateUrl: 'transactions.html'
|
|
||||||
})
|
|
||||||
.when('/send', {
|
|
||||||
templateUrl: 'send.html'
|
|
||||||
})
|
|
||||||
.when('/backup', {
|
|
||||||
templateUrl: 'backup.html'
|
|
||||||
})
|
|
||||||
.otherwise({
|
|
||||||
templateUrl: '404.html'
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
//Setting HTML5 Location Mode
|
|
||||||
angular
|
|
||||||
.module('copay')
|
|
||||||
.config(function($locationProvider) {
|
|
||||||
$locationProvider
|
|
||||||
.html5Mode(false);
|
|
||||||
//.hashPrefix('!');
|
|
||||||
});
|
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,6 @@ angular.module('copay.signin').controller('SigninController',
|
||||||
$scope.loading = true;
|
$scope.loading = true;
|
||||||
|
|
||||||
if (cid) {
|
if (cid) {
|
||||||
$rootScope.connectedTo.push(cid);
|
|
||||||
|
|
||||||
Network.init(function() {
|
Network.init(function() {
|
||||||
Network.connect(cid, function() {
|
Network.connect(cid, function() {
|
||||||
$location.path('peer');
|
$location.path('peer');
|
||||||
|
|
@ -29,11 +27,9 @@ angular.module('copay.signin').controller('SigninController',
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (peerData && peerData.peerId && peerData.connectedTo.length > 0) {
|
if (peerData && peerData.peerId && peerData.connectedPeers.length > 0) {
|
||||||
$rootScope.peerId = peerData.peerId;
|
$rootScope.peerId = peerData.peerId;
|
||||||
$rootScope.connectedPeers = peerData.connectedPeers;
|
$scope.join(peerData.connectedPeers);
|
||||||
|
|
||||||
$scope.join(peerData.connectedTo[0]);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
var copay = require('copay');
|
||||||
angular.element(document).ready(function() {
|
angular.element(document).ready(function() {
|
||||||
// Init the app
|
// Init the app
|
||||||
angular.bootstrap(document, ['copay']);
|
angular.bootstrap(document, ['copay']);
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ var CHANGE_BRANCH = 'm/1/';
|
||||||
function PublicKeyRing(opts) {
|
function PublicKeyRing(opts) {
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
|
|
||||||
this.network = opts.network === 'livenet' ?
|
this.network = opts.networkName === 'livenet' ?
|
||||||
bitcore.networks.livenet : bitcore.networks.testnet;
|
bitcore.networks.livenet : bitcore.networks.testnet;
|
||||||
|
|
||||||
this.requiredCopayers = opts.requiredCopayers || 3;
|
this.requiredCopayers = opts.requiredCopayers || 3;
|
||||||
|
|
@ -60,7 +60,7 @@ PublicKeyRing.encrypt = function (passphrase, payload) {
|
||||||
};
|
};
|
||||||
|
|
||||||
PublicKeyRing.read = function (id, passphrase) {
|
PublicKeyRing.read = function (id, passphrase) {
|
||||||
var encPayload = storage.read(id);
|
var encPayload = storage.get(id);
|
||||||
if (!encPayload)
|
if (!encPayload)
|
||||||
throw new Error('Could not find wallet data');
|
throw new Error('Could not find wallet data');
|
||||||
var data;
|
var data;
|
||||||
|
|
@ -74,9 +74,7 @@ PublicKeyRing.read = function (id, passphrase) {
|
||||||
if (data.id !== id)
|
if (data.id !== id)
|
||||||
throw new Error('Wrong id in data');
|
throw new Error('Wrong id in data');
|
||||||
|
|
||||||
var config = { network: data.networkName === 'livenet' ?
|
var config = { networkName: data.networkName };
|
||||||
bitcore.networks.livenet : bitcore.networks.testnet
|
|
||||||
};
|
|
||||||
|
|
||||||
var w = new PublicKeyRing(config);
|
var w = new PublicKeyRing(config);
|
||||||
|
|
||||||
|
|
@ -121,7 +119,7 @@ PublicKeyRing.prototype.store = function (passphrase) {
|
||||||
if (!this.id)
|
if (!this.id)
|
||||||
throw new Error('wallet has no id');
|
throw new Error('wallet has no id');
|
||||||
|
|
||||||
storage.save(this.id, PublicKeyRing.encrypt(passphrase,this.serialize()));
|
storage.set(this.id, PublicKeyRing.encrypt(passphrase,this.serialize()));
|
||||||
this.dirty = 0;
|
this.dirty = 0;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -134,7 +132,7 @@ PublicKeyRing.prototype.registeredCopayers = function () {
|
||||||
|
|
||||||
|
|
||||||
PublicKeyRing.prototype.haveAllRequiredPubKeys = function () {
|
PublicKeyRing.prototype.haveAllRequiredPubKeys = function () {
|
||||||
return this.registeredCopayers() === this.totalCopayers;
|
return this.registeredCopayers() >= this.totalCopayers;
|
||||||
};
|
};
|
||||||
|
|
||||||
PublicKeyRing.prototype._checkKeys = function() {
|
PublicKeyRing.prototype._checkKeys = function() {
|
||||||
|
|
@ -236,9 +234,16 @@ PublicKeyRing.prototype.getAddresses = function() {
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
PublicKeyRing.prototype._checkInPRK = function(inPKR) {
|
PublicKeyRing.prototype._checkInPRK = function(inPKR, ignoreId) {
|
||||||
if (this.id !== inPKR.id)
|
|
||||||
|
|
||||||
|
if (!inPKR.ts) {
|
||||||
|
throw new Error('inPRK bad format: Did you use .toObj()?');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ignoreId && this.id !== inPKR.id) {
|
||||||
throw new Error('inPRK id mismatch');
|
throw new Error('inPRK id mismatch');
|
||||||
|
}
|
||||||
|
|
||||||
if (this.network.name !== inPKR.networkName)
|
if (this.network.name !== inPKR.networkName)
|
||||||
throw new Error('inPRK network mismatch');
|
throw new Error('inPRK network mismatch');
|
||||||
|
|
@ -296,10 +301,10 @@ PublicKeyRing.prototype._mergePubkeys = function(inPKR) {
|
||||||
return hasChanged;
|
return hasChanged;
|
||||||
};
|
};
|
||||||
|
|
||||||
PublicKeyRing.prototype.merge = function(inPKR) {
|
PublicKeyRing.prototype.merge = function(inPKR, ignoreId) {
|
||||||
var hasChanged = false;
|
var hasChanged = false;
|
||||||
|
|
||||||
this._checkInPRK(inPKR);
|
this._checkInPRK(inPKR, ignoreId);
|
||||||
|
|
||||||
if (this._mergeIndexes(inPKR))
|
if (this._mergeIndexes(inPKR))
|
||||||
hasChanged = true;
|
hasChanged = true;
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,11 @@ function Storage() {
|
||||||
this.data = {};
|
this.data = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
Storage.prototype.read = function(k) {
|
Storage.prototype.get = function(k) {
|
||||||
return this.data[k];
|
return this.data[k];
|
||||||
};
|
};
|
||||||
|
|
||||||
Storage.prototype.save = function(k,v) {
|
Storage.prototype.set = function(k,v) {
|
||||||
this.data[k]=v;
|
this.data[k]=v;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
45
js/routes.js
Normal file
45
js/routes.js
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
//Setting up route
|
||||||
|
angular
|
||||||
|
.module('copay')
|
||||||
|
.config(function($routeProvider) {
|
||||||
|
|
||||||
|
$routeProvider
|
||||||
|
.when('/', {
|
||||||
|
templateUrl: 'signin.html'
|
||||||
|
})
|
||||||
|
.when('/signin', {
|
||||||
|
templateUrl: 'signin.html'
|
||||||
|
})
|
||||||
|
.when('/home', {
|
||||||
|
templateUrl: 'home.html'
|
||||||
|
})
|
||||||
|
.when('/join/:id', {
|
||||||
|
templateUrl: 'join.html'
|
||||||
|
})
|
||||||
|
.when('/peer', {
|
||||||
|
templateUrl: 'peer.html'
|
||||||
|
})
|
||||||
|
.when('/transactions', {
|
||||||
|
templateUrl: 'transactions.html'
|
||||||
|
})
|
||||||
|
.when('/send', {
|
||||||
|
templateUrl: 'send.html'
|
||||||
|
})
|
||||||
|
.when('/backup', {
|
||||||
|
templateUrl: 'backup.html'
|
||||||
|
})
|
||||||
|
.otherwise({
|
||||||
|
templateUrl: '404.html'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
//Setting HTML5 Location Mode
|
||||||
|
angular
|
||||||
|
.module('copay')
|
||||||
|
.config(function($locationProvider) {
|
||||||
|
$locationProvider
|
||||||
|
.html5Mode(false);
|
||||||
|
//.hashPrefix('!');
|
||||||
|
});
|
||||||
|
|
@ -4,9 +4,7 @@ angular.module('copay.network')
|
||||||
.factory('Network', function($rootScope, Storage) {
|
.factory('Network', function($rootScope, Storage) {
|
||||||
var peer;
|
var peer;
|
||||||
$rootScope.connectedPeers = [];
|
$rootScope.connectedPeers = [];
|
||||||
$rootScope.connectedTo = [];
|
|
||||||
$rootScope.peerId = null;
|
$rootScope.peerId = null;
|
||||||
$rootScope.publicKeyRing = [];
|
|
||||||
|
|
||||||
// Array helpers
|
// Array helpers
|
||||||
var _arrayDiff = function(a, b) {
|
var _arrayDiff = function(a, b) {
|
||||||
|
|
@ -42,12 +40,11 @@ angular.module('copay.network')
|
||||||
var _saveDataStorage = function() {
|
var _saveDataStorage = function() {
|
||||||
Storage.save('peerData', {
|
Storage.save('peerData', {
|
||||||
peerId: $rootScope.peerId,
|
peerId: $rootScope.peerId,
|
||||||
connectedTo: $rootScope.connectedTo,
|
|
||||||
connectedPeers: $rootScope.connectedPeers
|
connectedPeers: $rootScope.connectedPeers
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var _sender = function(pid, data, cb) {
|
var _sendToOne = function(pid, data, cb) {
|
||||||
if (pid !== $rootScope.peerId) {
|
if (pid !== $rootScope.peerId) {
|
||||||
var conns = peer.connections[pid];
|
var conns = peer.connections[pid];
|
||||||
|
|
||||||
|
|
@ -67,40 +64,79 @@ angular.module('copay.network')
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var _onData = function(data) {
|
var _onData = function(data, isOutbound) {
|
||||||
var obj = JSON.parse(data);
|
var obj = JSON.parse(data);
|
||||||
|
console.log('### RECEIVED TYPE: %s FROM %s', obj.data.type, obj.sender);
|
||||||
switch(obj.data.type) {
|
switch(obj.data.type) {
|
||||||
case 'connectedPeers':
|
case 'peerList':
|
||||||
_connectToPeers(obj.data.peers);
|
if (_connectToPeers(obj.data.peers)) {
|
||||||
break;
|
//TODO Remove log
|
||||||
case 'getPeers':
|
console.log('### BROADCASTING PEER LIST');
|
||||||
_send(obj.sender, {
|
_send( $rootScope.connectedPeers, {
|
||||||
type: 'connectToPeers',
|
type: 'peerList',
|
||||||
peers: $rootScope.connectedPeers
|
peers: $rootScope.connectedPeers,
|
||||||
});
|
isBroadcast: 1,
|
||||||
|
});
|
||||||
|
$rootScope.$digest();
|
||||||
|
}
|
||||||
|
else if (!isOutbound && !obj.data.isBroadcast) {
|
||||||
|
// replying always to connecting peer
|
||||||
|
console.log('### REPLYING PEERLIST TO:', obj.sender );
|
||||||
|
_send( obj.sender, {
|
||||||
|
type: 'peerList',
|
||||||
|
peers: $rootScope.connectedPeers
|
||||||
|
});
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'disconnect':
|
case 'disconnect':
|
||||||
_onClose(obj.sender);
|
_onClose(obj.sender);
|
||||||
break;
|
break;
|
||||||
|
case 'publicKeyRing':
|
||||||
|
console.log('### RECEIVED PKR FROM:', obj.sender);
|
||||||
|
|
||||||
|
if ($rootScope.publicKeyRing.merge(obj.data.publicKeyRing, true)) {
|
||||||
|
//TODO Remove log
|
||||||
|
console.log('### BROADCASTING PRK');
|
||||||
|
_send( $rootScope.connectedPeers, {
|
||||||
|
type: 'publicKeyRing',
|
||||||
|
publicKeyRing: $rootScope.publicKeyRing.toObj(),
|
||||||
|
isBroadcast: 1,
|
||||||
|
});
|
||||||
|
$rootScope.$digest();
|
||||||
|
}
|
||||||
|
else if (!isOutbound && !obj.data.isBroadcast) {
|
||||||
|
// replying always to connecting peer
|
||||||
|
console.log('### REPLYING PRK TO:', obj.sender );
|
||||||
|
_send( obj.sender, {
|
||||||
|
type: 'publicKeyRing',
|
||||||
|
publicKeyRing: $rootScope.publicKeyRing.toObj(),
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO Remove log
|
||||||
|
console.log('*** PRK:', $rootScope.publicKeyRing.toObj());
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var _onClose = function(pid) {
|
var _onClose = function(pid) {
|
||||||
$rootScope.connectedPeers = _arrayRemove(pid, $rootScope.connectedPeers);
|
$rootScope.connectedPeers = _arrayRemove(pid, $rootScope.connectedPeers);
|
||||||
$rootScope.connectedTo = _arrayRemove(pid, $rootScope.connectedTo);
|
|
||||||
|
|
||||||
_saveDataStorage();
|
_saveDataStorage();
|
||||||
|
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
};
|
};
|
||||||
|
|
||||||
var _connectToPeers = function(peers) {
|
var _connectToPeers = function(peers) {
|
||||||
var arrayDiff = _arrayDiff(peers, $rootScope.connectedTo);
|
var ret = false;
|
||||||
|
var arrayDiff1= _arrayDiff(peers, $rootScope.connectedPeers);
|
||||||
|
var arrayDiff = _arrayDiff(arrayDiff1, [$rootScope.peerId]);
|
||||||
arrayDiff.forEach(function(pid) {
|
arrayDiff.forEach(function(pid) {
|
||||||
_connect(pid);
|
console.log('### CONNECTING TO:',pid);
|
||||||
|
ret = true;
|
||||||
|
connect(pid);
|
||||||
});
|
});
|
||||||
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
// public methods
|
// public methods
|
||||||
|
|
@ -110,71 +146,95 @@ angular.module('copay.network')
|
||||||
debug: 3
|
debug: 3
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
$rootScope.publicKeyRing = new copay.PublicKeyRing({
|
||||||
|
network: config.networkName,
|
||||||
|
});
|
||||||
|
$rootScope.publicKeyRing.addCopayer();
|
||||||
|
console.log('### PublicKeyRing Initialized');
|
||||||
|
|
||||||
|
|
||||||
peer.on('open', function(pid) {
|
peer.on('open', function(pid) {
|
||||||
|
console.log('### PEER OPEN. I AM:' + pid);
|
||||||
$rootScope.peerId = pid;
|
$rootScope.peerId = pid;
|
||||||
_arrayPushOnce(pid, $rootScope.connectedPeers);
|
|
||||||
_saveDataStorage();
|
_saveDataStorage();
|
||||||
|
|
||||||
cb();
|
cb();
|
||||||
|
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
});
|
});
|
||||||
|
|
||||||
peer.on('connection', function(conn) {
|
peer.on('connection', function(dataConn) {
|
||||||
if (conn.label === 'wallet') {
|
if (dataConn.label === 'wallet') {
|
||||||
conn.on('open', function() {
|
console.log('### NEW INBOUND CONNECTION'); //TODO
|
||||||
if (!_inArray(conn.peer, $rootScope.connectedTo)) {
|
dataConn.on('open', function() {
|
||||||
var c = peer.connect(conn.peer, {
|
if (!_inArray(dataConn.peer, $rootScope.connectedPeers)) {
|
||||||
label: 'wallet',
|
console.log('### INBOUND DATA CONNECTION READY TO:' + dataConn.peer); //TODO
|
||||||
serialization: 'none',
|
_arrayPushOnce(dataConn.peer, $rootScope.connectedPeers);
|
||||||
reliable: false,
|
_saveDataStorage();
|
||||||
metadata: { message: 'hi copayer!' }
|
|
||||||
});
|
|
||||||
|
|
||||||
c.on('open', function() {
|
$rootScope.$digest();
|
||||||
$rootScope.connectedTo.push(conn.peer);
|
|
||||||
_arrayPushOnce(conn.peer, $rootScope.connectedPeers);
|
|
||||||
_saveDataStorage();
|
|
||||||
|
|
||||||
$rootScope.$digest();
|
|
||||||
});
|
|
||||||
|
|
||||||
c.on('data', _onData);
|
|
||||||
|
|
||||||
c.on('close', function() {
|
|
||||||
_onClose(c.peer);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
dataConn.on('data', _onData);
|
||||||
|
dataConn.on('error', function(e) {
|
||||||
|
console.log('### ## INBOUND DATA ERROR',e ); //TODO
|
||||||
|
_onClose(dataConn.peer);
|
||||||
|
});
|
||||||
|
dataConn.on('close', function() {
|
||||||
|
_onClose(dataConn.peer);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var connect = function(pid, cb) {
|
var connect = function(pid, cb) {
|
||||||
if (pid !== $rootScope.peerId) {
|
if (pid !== $rootScope.peerId) {
|
||||||
var c = peer.connect(pid, {
|
|
||||||
|
console.log('### STARTING CONNECT TO:' + pid );
|
||||||
|
|
||||||
|
var dataConn = peer.connect(pid, {
|
||||||
label: 'wallet',
|
label: 'wallet',
|
||||||
serialization: 'none',
|
serialization: 'none',
|
||||||
reliable: false,
|
reliable: true,
|
||||||
metadata: { message: 'hi copayer!' }
|
metadata: { message: 'hi copayer!' }
|
||||||
});
|
});
|
||||||
|
|
||||||
c.on('open', function() {
|
dataConn.on('open', function() {
|
||||||
_arrayPushOnce(pid, $rootScope.connectedTo);
|
|
||||||
_arrayPushOnce(pid, $rootScope.connectedPeers);
|
|
||||||
|
|
||||||
_send(pid, { type: 'getPeers' });
|
console.log('### OUTBOUND DATA CONN READY TO:' + pid );
|
||||||
|
_arrayPushOnce(pid, $rootScope.connectedPeers);
|
||||||
_saveDataStorage();
|
_saveDataStorage();
|
||||||
|
|
||||||
|
console.log('#### SENDING PEER LIST: ' +$rootScope.connectedPeers);
|
||||||
|
_send(pid, {
|
||||||
|
type: 'peerList',
|
||||||
|
peers: $rootScope.connectedPeers
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
console.log('#### SENDING PKR ');
|
||||||
|
_send(dataConn.peer, {
|
||||||
|
type: 'publicKeyRing',
|
||||||
|
publicKeyRing: $rootScope.publicKeyRing.toObj(),
|
||||||
|
});
|
||||||
|
|
||||||
if (typeof cb === 'function') cb();
|
if (typeof cb === 'function') cb();
|
||||||
|
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
});
|
});
|
||||||
|
|
||||||
c.on('data', _onData);
|
dataConn.on('data', function(data) {
|
||||||
|
_onData(data,true);
|
||||||
|
});
|
||||||
|
|
||||||
c.on('close', function() {
|
dataConn.on('error', function(e) {
|
||||||
_onClose(c.peer);
|
console.log('### ## INBOUND DATA ERROR',e ); //TODO
|
||||||
|
_onClose(dataConn.peer);
|
||||||
|
});
|
||||||
|
|
||||||
|
dataConn.on('close', function() {
|
||||||
|
_onClose(dataConn.peer);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -182,32 +242,28 @@ angular.module('copay.network')
|
||||||
var _send = function(pids, data, cb) {
|
var _send = function(pids, data, cb) {
|
||||||
if (Array.isArray(pids))
|
if (Array.isArray(pids))
|
||||||
pids.forEach(function(pid) {
|
pids.forEach(function(pid) {
|
||||||
_sender(pid, data, cb);
|
_sendToOne(pid, data, cb);
|
||||||
});
|
});
|
||||||
else if (typeof pids === 'string')
|
else if (typeof pids === 'string')
|
||||||
_sender(pids, data, cb);
|
_sendToOne(pids, data, cb);
|
||||||
};
|
};
|
||||||
|
|
||||||
var disconnect = function(cb) {
|
var disconnect = function(cb) {
|
||||||
|
Storage.remove('peerData');
|
||||||
var conns = $rootScope.connectedPeers.length;
|
var conns = $rootScope.connectedPeers.length;
|
||||||
var i = 1;
|
var i = 1;
|
||||||
|
|
||||||
_send($rootScope.connectedPeers, { type: 'disconnect' }, function() {
|
_send($rootScope.connectedPeers, { type: 'disconnect' }, function() {
|
||||||
i += 1;
|
i += 1;
|
||||||
|
|
||||||
if (i === conns) {
|
if (i === conns) {
|
||||||
|
|
||||||
|
$rootScope.connectedPeers = [];
|
||||||
|
$rootScope.peerId = null;
|
||||||
peer.disconnect();
|
peer.disconnect();
|
||||||
peer.destroy();
|
peer.destroy();
|
||||||
|
|
||||||
if (typeof cb === 'function') cb();
|
if (typeof cb === 'function') cb();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Storage.remove('peerData');
|
|
||||||
|
|
||||||
$rootScope.connectedPeers = [];
|
|
||||||
$rootScope.connectedTo = [];
|
|
||||||
$rootScope.peerId = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ angular.module('copay.storage')
|
||||||
return JSON.parse(localStorage.getItem(key));
|
return JSON.parse(localStorage.getItem(key));
|
||||||
},
|
},
|
||||||
|
|
||||||
save: function(key, data) {
|
set: function(key, data) {
|
||||||
localStorage.setItem(key, JSON.stringify(data));
|
localStorage.setItem(key, JSON.stringify(data));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,11 @@ var FakeStorage = function(){
|
||||||
this.storage = {};
|
this.storage = {};
|
||||||
};
|
};
|
||||||
|
|
||||||
FakeStorage.prototype.read = function (id) {
|
FakeStorage.prototype.set = function (id) {
|
||||||
return this.storage[id];
|
return this.storage[id];
|
||||||
};
|
};
|
||||||
|
|
||||||
FakeStorage.prototype.save = function(id, payload) {
|
FakeStorage.prototype.get = function(id, payload) {
|
||||||
this.storage[id] = payload;
|
this.storage[id] = payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,13 +13,13 @@ var aMasterPubKey = 'tprv8ZgxMBicQKsPdSVTiWXEqCCzqRaRr9EAQdn5UVMpT9UHX67Dh1FmzEM
|
||||||
|
|
||||||
|
|
||||||
var config = {
|
var config = {
|
||||||
network:'livenet',
|
networkName:'livenet',
|
||||||
};
|
};
|
||||||
|
|
||||||
var createW = function (network) {
|
var createW = function (networkName) {
|
||||||
|
|
||||||
var config = {
|
var config = {
|
||||||
network: network || 'livenet',
|
networkName: networkName || 'livenet',
|
||||||
};
|
};
|
||||||
|
|
||||||
var w = new PublicKeyRing(config);
|
var w = new PublicKeyRing(config);
|
||||||
|
|
@ -39,7 +39,7 @@ describe('PublicKeyRing model', function() {
|
||||||
|
|
||||||
it('should create an instance (livenet)', function () {
|
it('should create an instance (livenet)', function () {
|
||||||
var w = new PublicKeyRing({
|
var w = new PublicKeyRing({
|
||||||
network: config.network
|
networkName: config.networkName
|
||||||
});
|
});
|
||||||
should.exist(w);
|
should.exist(w);
|
||||||
w.network.name.should.equal('livenet');
|
w.network.name.should.equal('livenet');
|
||||||
|
|
@ -158,7 +158,7 @@ describe('PublicKeyRing model', function() {
|
||||||
w.generateAddress(false);
|
w.generateAddress(false);
|
||||||
|
|
||||||
var w2 = new PublicKeyRing({
|
var w2 = new PublicKeyRing({
|
||||||
network: 'livenet',
|
networkName: 'livenet',
|
||||||
id: w.id,
|
id: w.id,
|
||||||
});
|
});
|
||||||
w2.merge(w.toObj()).should.equal(true);
|
w2.merge(w.toObj()).should.equal(true);
|
||||||
|
|
@ -182,33 +182,48 @@ describe('PublicKeyRing model', function() {
|
||||||
w.generateAddress(false);
|
w.generateAddress(false);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
var w2 = new PublicKeyRing({
|
||||||
|
networkName: 'livenet',
|
||||||
|
});
|
||||||
|
(function() { w2.merge(w.toObj());}).should.throw();
|
||||||
|
(function() { w2.merge(w,true);}).should.throw();
|
||||||
|
|
||||||
|
console.log('[test.publickeyring.js.190]'); //TODO
|
||||||
|
w2.merge(w.toObj(),true).should.equal(true);
|
||||||
|
|
||||||
|
console.log('[test.publickeyring.js.193]'); //TODO
|
||||||
|
|
||||||
|
|
||||||
var w3 = new PublicKeyRing({
|
var w3 = new PublicKeyRing({
|
||||||
network: 'livenet',
|
networkName: 'livenet',
|
||||||
id: w.id,
|
id: w.id,
|
||||||
requiredCopayers: 2,
|
requiredCopayers: 2,
|
||||||
});
|
});
|
||||||
(function() { w3.merge(w.toObj());}).should.throw();
|
(function() { w3.merge(w.toObj());}).should.throw();
|
||||||
|
|
||||||
var w4 = new PublicKeyRing({
|
var w4 = new PublicKeyRing({
|
||||||
network: 'testnet',
|
networkName: 'testnet',
|
||||||
id: w.id,
|
id: w.id,
|
||||||
});
|
});
|
||||||
(function() { w4.merge(w.toObj());}).should.throw();
|
(function() { w4.merge(w.toObj());}).should.throw();
|
||||||
|
|
||||||
var w5 = new PublicKeyRing({
|
var w5 = new PublicKeyRing({
|
||||||
network: 'livenet',
|
networkName: 'livenet',
|
||||||
id: w.id,
|
id: w.id,
|
||||||
totalCopayers: 4,
|
totalCopayers: 4,
|
||||||
});
|
});
|
||||||
(function() { w5.merge(w.toObj());}).should.throw();
|
(function() { w5.merge(w.toObj());}).should.throw();
|
||||||
|
|
||||||
var w6 = new PublicKeyRing({
|
var w6 = new PublicKeyRing({
|
||||||
network: 'livenet',
|
networkName: 'livenet',
|
||||||
id: w.id,
|
id: w.id,
|
||||||
});
|
});
|
||||||
(function() { w6.merge(w);}).should.throw();
|
(function() { w6.merge(w);}).should.throw();
|
||||||
w.networkName= 'livenet';
|
w.networkName= 'livenet';
|
||||||
(function() { w6.merge(w);}).should.throw();
|
(function() { w6.merge(w);}).should.throw();
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -222,7 +237,7 @@ describe('PublicKeyRing model', function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
var w2 = new PublicKeyRing({
|
var w2 = new PublicKeyRing({
|
||||||
network: 'livenet',
|
networkName: 'livenet',
|
||||||
id: w.id,
|
id: w.id,
|
||||||
});
|
});
|
||||||
should.exist(w);
|
should.exist(w);
|
||||||
|
|
@ -248,7 +263,7 @@ describe('PublicKeyRing model', function() {
|
||||||
for(var i=0; i<5; i++) {
|
for(var i=0; i<5; i++) {
|
||||||
w.haveAllRequiredPubKeys().should.equal(false);
|
w.haveAllRequiredPubKeys().should.equal(false);
|
||||||
var w2 = new PublicKeyRing({
|
var w2 = new PublicKeyRing({
|
||||||
network: 'livenet',
|
networkName: 'livenet',
|
||||||
id: w.id,
|
id: w.id,
|
||||||
});
|
});
|
||||||
w2.addCopayer();
|
w2.addCopayer();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue