mirror of https://github.com/BTCPrivate/copay.git
WIP for automatic network join
This commit is contained in:
parent
ef3c8e2865
commit
d62dbeffca
|
@ -81,8 +81,9 @@
|
|||
<div ng-show="!loading">
|
||||
<div class="row">
|
||||
<div class="large-6 columns">
|
||||
<h3>Join a Network Wallet</h3>
|
||||
<input type="text" class="form-control" placeholder="Peer ID" ng-model="connectionId" autofocus>
|
||||
<h3>Join Wallet Creation</h3>
|
||||
<input type="text" class="form-control" placeholder="Paste wallet ID here"
|
||||
ng-model="connectionId" autofocus>
|
||||
</div>
|
||||
<div class="large-3 columns">
|
||||
<button class="button primary expand round" type="button" ng-click="join(connectionId)">Join</button>
|
||||
|
|
|
@ -23,12 +23,12 @@ var config = {
|
|||
verbose: 1,
|
||||
},
|
||||
blockchain: {
|
||||
host: 'localhost',
|
||||
port: 3001
|
||||
host: 'test.insight.is',
|
||||
port: 80
|
||||
},
|
||||
socket: {
|
||||
host: 'localhost',
|
||||
port: 3001
|
||||
host: 'test.insight.is',
|
||||
port: 80
|
||||
},
|
||||
verbose: 1,
|
||||
};
|
||||
|
|
|
@ -33,7 +33,6 @@ angular.module('copay.setup').controller('SetupController',
|
|||
};
|
||||
var w = walletFactory.create(opts);
|
||||
controllerUtils.setupUxHandlers(w);
|
||||
w.netStart();
|
||||
};
|
||||
|
||||
});
|
||||
|
|
|
@ -22,7 +22,6 @@ angular.module('copay.signin').controller('SigninController',
|
|||
|
||||
var w = walletFactory.open(walletId);
|
||||
controllerUtils.setupUxHandlers(w);
|
||||
w.netStart();
|
||||
};
|
||||
|
||||
$scope.join = function(cid) {
|
||||
|
@ -31,10 +30,7 @@ angular.module('copay.signin').controller('SigninController',
|
|||
controllerUtils.onError($scope);
|
||||
$rootScope.$digest();
|
||||
});
|
||||
walletFactory.connectTo(cid, function(w) {
|
||||
controllerUtils.setupUxHandlers(w);
|
||||
w.netStart();
|
||||
});
|
||||
walletFactory.connectTo(cid, $scope.open.bind($scope));
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -16,13 +16,17 @@ function PrivateKey(opts) {
|
|||
var init = opts.extendedPrivateKeyString || this.network.name;
|
||||
this.bip = opts.BIP32 || new BIP32(init);
|
||||
this.privateKeyCache = opts.privateKeyCache || {};
|
||||
this._calcId();
|
||||
};
|
||||
|
||||
PrivateKey.prototype._calcId = function() {
|
||||
this.id = util.ripe160(this.bip.extendedPublicKey).toString('hex');
|
||||
PrivateKey.prototype.getId = function(prefix) {
|
||||
var buf = this.bip.extendedPublicKey;
|
||||
if (prefix) {
|
||||
buf = Buffer.concat([prefix, this.bip.extendedPublicKey]);
|
||||
}
|
||||
return util.ripe160(buf).toString('hex');
|
||||
};
|
||||
|
||||
|
||||
PrivateKey.fromObj = function(obj) {
|
||||
return new PrivateKey(obj);
|
||||
};
|
||||
|
|
|
@ -133,6 +133,13 @@ Wallet.prototype._optsToObj = function () {
|
|||
return obj;
|
||||
};
|
||||
|
||||
|
||||
Wallet.prototype.generatePeerId = function() {
|
||||
var gen = this.privateKey.getId(new Buffer(this.id));
|
||||
console.log(gen);
|
||||
return gen;
|
||||
};
|
||||
|
||||
Wallet.prototype.netStart = function() {
|
||||
var self = this;
|
||||
var net = this.network;
|
||||
|
@ -147,9 +154,12 @@ Wallet.prototype.netStart = function() {
|
|||
net.on('close', function() {
|
||||
self.emit('close');
|
||||
});
|
||||
var startOpts = {
|
||||
peerId: this.generatePeerId()
|
||||
}
|
||||
net.start(function(peerId) {
|
||||
self.emit('created');
|
||||
});
|
||||
}, startOpts);
|
||||
};
|
||||
|
||||
Wallet.prototype.store = function(isSync) {
|
||||
|
@ -236,7 +246,7 @@ Wallet.prototype.getTxProposals = function() {
|
|||
self.txProposals.txps.forEach(function(txp) {
|
||||
var i = {txp:txp};
|
||||
i.ntxid = txp.builder.build().getNormalizedHash();
|
||||
i.signedByUs = txp.signedBy[self.privateKey.id]?true:false;
|
||||
i.signedByUs = txp.signedBy[self.privateKey.getId()]?true:false;
|
||||
ret.push(i);
|
||||
});
|
||||
return ret;
|
||||
|
@ -267,7 +277,7 @@ Wallet.prototype.sign = function(ntxid) {
|
|||
var ret = txp.builder.sign(keys);
|
||||
|
||||
if (ret.signaturesAdded) {
|
||||
txp.signedBy[this.privateKey.id] = Date.now();
|
||||
txp.signedBy[this.privateKey.getId()] = Date.now();
|
||||
this.log('[Wallet.js.230:ret:]',ret); //TODO
|
||||
if (ret.isFullySigned) {
|
||||
this.log('[Wallet.js.231] BROADCASTING TX!!!'); //TODO
|
||||
|
@ -294,8 +304,8 @@ Wallet.prototype.addSeenToTxProposals = function() {
|
|||
var self=this;
|
||||
|
||||
this.txProposals.txps.forEach(function(txp) {
|
||||
if (!txp.seenBy[self.privateKey.id]) {
|
||||
txp.seenBy[self.privateKey.id] = Date.now();
|
||||
if (!txp.seenBy[self.privateKey.getId()]) {
|
||||
txp.seenBy[self.privateKey.getId()] = Date.now();
|
||||
ret = true;
|
||||
}
|
||||
});
|
||||
|
|
|
@ -75,6 +75,7 @@ WalletFactory.prototype.read = function(walletId) {
|
|||
this.log('NOT NECCESARY AN ERROR:', e); //TODO
|
||||
}
|
||||
this.log('### WALLET OPENED:', w.id);
|
||||
w.netStart();
|
||||
return w;
|
||||
};
|
||||
|
||||
|
@ -112,10 +113,12 @@ WalletFactory.prototype.create = function(opts) {
|
|||
opts.totalCopayers = totalCopayers;
|
||||
var w = new Wallet(opts);
|
||||
w.store();
|
||||
w.netStart();
|
||||
return w;
|
||||
};
|
||||
|
||||
WalletFactory.prototype.open = function(walletId) {
|
||||
this.log('Opening walletId:' + walletId);
|
||||
var w = this.read(walletId) || this.create({
|
||||
id: walletId,
|
||||
verbose: this.verbose,
|
||||
|
@ -123,44 +126,6 @@ WalletFactory.prototype.open = function(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.getExtendedPublicKeyString());
|
||||
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 = typeof opts.spendUnconfirmed === undefined
|
||||
?this.walletDefaults.spendUnconfirmed : opts.spendUnconfirmed;
|
||||
|
||||
opts.requiredCopayers = requiredCopayers;
|
||||
opts.totalCopayers = totalCopayers;
|
||||
var w = new Wallet(opts);
|
||||
w.store();
|
||||
return w;
|
||||
};
|
||||
|
||||
WalletFactory.prototype.getWalletIds = function() {
|
||||
return this.storage.getWalletIds();
|
||||
}
|
||||
|
@ -175,8 +140,7 @@ WalletFactory.prototype.connectTo = function(peerId, cb) {
|
|||
self.network.start(function() {
|
||||
self.network.connectTo(peerId)
|
||||
self.network.on('walletId', function(walletId) {
|
||||
self.log('Opening walletId:' + walletId);
|
||||
return cb(self.open(walletId));
|
||||
return cb(walletId);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
|
||||
|
||||
function Peer(id) {
|
||||
this.id = id;
|
||||
};
|
||||
|
||||
|
||||
module.exports = require('soop')(Peer);
|
|
@ -29,6 +29,7 @@ function Network(opts) {
|
|||
if (opts[k]) self.opts[k]=opts[k];
|
||||
});
|
||||
this.connectedPeers = [];
|
||||
this.started = false;
|
||||
}
|
||||
|
||||
Network.parent=EventEmitter;
|
||||
|
@ -215,12 +216,16 @@ Network.prototype._setupPeerHandlers = function(openCallback) {
|
|||
});
|
||||
};
|
||||
|
||||
Network.prototype.start = function(openCallback) {
|
||||
Network.prototype.start = function(openCallback, opts) {
|
||||
// Start PeerJS Peer
|
||||
if (this.peer) return openCallback(); // This is for connectTo-> peer is started before
|
||||
if (this.started) return openCallback(); // This is for connectTo-> peer is started before
|
||||
|
||||
opts = opts || {};
|
||||
this.peerId = this.peerId || opts.peerId;
|
||||
|
||||
this.peer = new Peer(this.peerId, this.opts);
|
||||
this._setupPeerHandlers(openCallback);
|
||||
this.started = true;
|
||||
};
|
||||
|
||||
Network.prototype._sendToOne = function(peerId, data, cb) {
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
|
||||
var imports = require('soop').imports();
|
||||
var EventEmitter= imports.EventEmitter || require('events').EventEmitter;
|
||||
|
||||
function Network(opts) {
|
||||
}
|
||||
|
||||
Network.parent=EventEmitter;
|
||||
|
||||
Network.prototype.start = function(openCallback, opts) {
|
||||
// start! :D
|
||||
};
|
||||
|
||||
Network.prototype.send = function(peerIds, data, cb) {
|
||||
// send! c:
|
||||
};
|
||||
|
||||
Network.prototype.connectTo = function(peerId) {
|
||||
// connect C:
|
||||
};
|
||||
|
||||
|
||||
Network.prototype.disconnect = function(cb) {
|
||||
// disconect :c
|
||||
};
|
||||
|
||||
module.exports = require('soop')(Network);
|
|
@ -69,16 +69,15 @@ describe('PrivateKey model', function() {
|
|||
|
||||
it('should calculate .id', function () {
|
||||
var w1 = new PrivateKey(config);
|
||||
should.exist(w1.id);
|
||||
w1.id.length.should.equal(40);
|
||||
should.exist(w1.getId());
|
||||
w1.getId().length.should.equal(40);
|
||||
});
|
||||
it('fromObj toObj roundtrip', function () {
|
||||
var w1 = new PrivateKey(config);
|
||||
var w2 = PrivateKey.fromObj(w1.toObj());
|
||||
|
||||
w2.toObj().extendedPrivateKeyString.should.equal(w1.toObj().extendedPrivateKeyString);
|
||||
w2.id.should.equal(w1.id);
|
||||
|
||||
w2.getId().should.equal(w1.getId());
|
||||
|
||||
JSON.stringify(w2.get(1,1).storeObj()).should
|
||||
.equal(JSON.stringify(w1.get(1,1).storeObj()));
|
||||
|
|
|
@ -3,12 +3,12 @@
|
|||
var chai = chai || require('chai');
|
||||
var should = chai.should();
|
||||
|
||||
var WebRTC = require('../js/models/network/WebRTC');
|
||||
var FakeNetwork = require('./mocks/FakeNetwork');
|
||||
var Insight = require('../js/models/blockchain/Insight');
|
||||
var FakeStorage = require('./mocks/FakeStorage');
|
||||
|
||||
var WalletFactory = typeof copay === 'undefined' ? require('soop').load('../js/models/core/WalletFactory',{
|
||||
Network: WebRTC,
|
||||
Network: FakeNetwork,
|
||||
Blockchain: Insight,
|
||||
Storage: FakeStorage,
|
||||
}) : copay.WalletFactory;
|
||||
|
|
Loading…
Reference in New Issue