Merge pull request #64 from matiu/feature/walletFactory2

new WalletFactory
This commit is contained in:
Manuel Aráoz 2014-04-16 18:49:05 -03:00
commit 5f21f2e80d
14 changed files with 465 additions and 352 deletions

View File

@ -10,11 +10,13 @@ var Insight = module.exports.Insight = require('./js/models/blockchain/Insight')
var StorageLocalPlain = module.exports.StorageLocalPlain = require('./js/models/storage/LocalPlain');
var StorageLocalEncrypted = module.exports.StorageLocalEncrypted = require('./js/models/storage/LocalEncrypted');
module.exports.Wallet = require('soop').load('./js/models/core/Wallet',{
var WalletFactory = require('soop').load('./js/models/core/WalletFactory',{
Network: WebRTC,
Blockchain: Insight,
Storage: StorageLocalPlain,
});
module.exports.WalletFactory = WalletFactory;
//var walletFactory = new WalletFactory(config);
module.exports.API = require('./API');

View File

@ -156,17 +156,20 @@
<div class="row">
<div class="large-12 columns">
<h3>Pending Transactions <small>({{txsoutput.length}})</small></h3>
<div class="panel pending" ng-repeat="txp in txsoutput ">
<div class="panel pending" ng-repeat="txp in txsoutput">
{{txp}}
<div class="row">
<p class="large-5 columns"> Address 1</p>
<i class="large-2 columns fi-arrow-right size-16 text-center"></i>
<p class="large-5 columns"> Address 2</p>
<div class="large-12 columns m0">
<div class="large-12 columns m0" ng-show="txp.signedByUs"> YOU SIGNED! </div>
<div class="large-12 columns m0" ng-show="!txp.signedByUs">
<div class="line"></div>
<button class="primary round large-4 columns"><i class="large-2 columns fi-x size-16 text-center"></i> Ignore</button>
<small class="large-4 columns text-center">Faltan 3 cosigners</small>
<button class="secondary round large-4 columns"><i class="large-2 columns fi-check size-16 text-center"></i> Sign</button>
<button class="secondary round large-4 columns" ng-click="sign(txp.ntxid)"><i class="large-2 columns fi-check size-16 text-center"></i> Sign</button>
</div>
</div> <!-- end of row -->
</div> <!-- end of pending -->

View File

@ -9,7 +9,7 @@ angular.module('copay',[
'copay.transactions',
'copay.send',
'copay.backup',
'copay.network',
'copay.walletFactory',
'copay.signin',
'copay.peer'
]);
@ -19,7 +19,7 @@ angular.module('copay.home', []);
angular.module('copay.transactions', []);
angular.module('copay.send', []);
angular.module('copay.backup', []);
angular.module('copay.network', []);
angular.module('copay.walletFactory', []);
angular.module('copay.signin', []);
angular.module('copay.peer', []);

View File

@ -10,6 +10,8 @@ var config = {
wallet: {
requiredCopayers: 2,
totalCopayers: 3,
spendUnconfirmed: 1,
verbose: 1,
},
blockchain: {
host: 'test.insight.is',

View File

@ -13,25 +13,8 @@ angular.module('copay.send').controller('SendController',
var w = $rootScope.wallet;
var pkr = w.publicKeyRing;
var txp = w.txProposals;
var opts = {remainderOut: { address: pkr.generateAddress(true).toString() }};
// From @cmgustavo's wallet
w.listUnspent(function (unspentTest) {
console.log('[send.js.19:unspentTest:]',unspentTest); //TODO
txp.create(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789',
unspentTest,
w.privateKey,
opts
);
console.log('[send.js.29:txp:] READY:',txp); //TODO
Network.storeOpenWallet();
Network.sendTxProposals();
$rootScope.$digest;
});
w.createTx( '15q6HKjWHAksHcH91JW23BJEuzZgFwydBt', '12345',function() {
$rootScope.$digest();
});
};
});

View File

@ -1,7 +1,7 @@
'use strict';
angular.module('copay.signin').controller('SigninController',
function($scope, $rootScope, $location, Network) {
function($scope, $rootScope, $location, walletFactory) {
// var peerData = Storage.get($rootScope.walletId, 'peerData');
// $rootScope.peerId = peerData ? peerData.peerId : null;
@ -10,42 +10,45 @@ angular.module('copay.signin').controller('SigninController',
$scope.selectedWalletId = false;
$scope.listWalletIds = function() {
return copay.Wallet.factory.getWalletIds();
return walletFactory.getWalletIds();
};
var _setupUxHandlers = function(w) {
w.on('open', function(){
$location.path('peer');
$rootScope.wallet = w;
$rootScope.$digest();
});
w.on('openError', function(){
$scope.loading = false;
$rootScope.flashMessage = {type:'error', message: 'Wallet not found'};
$location.path('signin');
});
};
$scope.create = function() {
console.log('[signin.js.42:create:]'); //TODO
$scope.loading = true;
Network.createWallet();
Network.init(function() {
$location.path('peer');
$rootScope.$digest();
});
var w = walletFactory.create();
_setupUxHandlers(w);
w.netStart();
};
$scope.open = function(walletId) {
$scope.loading = true;
Network.openWallet(walletId);
if ($rootScope.wallet && $rootScope.wallet.id) {
Network.init(function() {
$location.path('peer');
$rootScope.$digest();
});
}
else {
$scope.loading = false;
$rootScope.flashMessage = {type:'error', message: 'Wallet not found'};
$location.path('signin');
}
var w = walletFactory.open(walletId);
_setupUxHandlers(w);
w.netStart();
};
$scope.join = function(cid) {
console.log('[signin.js.42:join:]'); //TODO
$scope.loading = true;
if (cid) {
Network.init(function() {
Network.init(null, function() {
Network.connect(cid,
function() {
$location.path('peer');

View File

@ -24,4 +24,11 @@ angular.module('copay.transactions').controller('TransactionsController',
];
$scope.txsoutput = $rootScope.wallet.getTxProposals();
$scope.sign = function (ntxid) {
var w = $rootScope.wallet;
var ret = w.sign(ntxid);
console.log('[transactions.js.28:ret:]',ret); //TODO
$scope.txsoutput = $rootScope.wallet.getTxProposals();
};
});

View File

@ -59,7 +59,7 @@ Insight.prototype.listUnspent = function(addresses, cb) {
self._request(options, function(err, res) {
if (res && res.length > 0) {
all = all.concat(res);
}
}
callback();
});
}, function() {

View File

@ -7,99 +7,134 @@ var coinUtil = bitcore.util;
var buffertools = bitcore.buffertools;
var Builder = bitcore.TransactionBuilder;
var http = require('http');
var Storage = imports.Storage;
var Network = imports.Network;
var Blockchain = imports.Blockchain;
var EventEmitter= imports.EventEmitter || require('events').EventEmitter;
var copay = copay || require('../../../copay');
function Wallet(config) {
this._startInterface(config);
function Wallet(opts) {
var self = this;
//required params
['storage', 'network', 'blockchain',
'requiredCopayers', 'totalCopayers', 'spendUnconfirmed',
'publicKeyRing', 'txProposals', 'privateKey'
].forEach( function(k){
if (typeof opts[k] === 'undefined') throw new Error('missing key:' + k);
self[k] = opts[k];
});
this.id = opts.id || Wallet.getRandomId();
this.publicKeyRing.walletId = this.id;
this.txProposals.walletId = this.id;
}
Wallet.parent=EventEmitter;
Wallet.prototype.log = function(){
if (!this.verbose) return;
console.log(arguments);
};
console.this.log(arguments);
}
Wallet.getRandomId = function() {
var r = buffertools.toHex(coinUtil.generateNonce());
return r;
};
Wallet.prototype._startInterface = function(config) {
this.storage = new Storage(config.storage);
this.network = new Network(config.network);
this.blockchain = new Blockchain(config.blockchain);
Wallet.prototype._handlePublicKeyRing = function(senderId, data, isInbound) {
this.openWalletId(data.walletId);
this.log('RECV PUBLICKEYRING:',data);
this.networkName = config.networkName;
this.requiredCopayers = config.wallet.requiredCopayers;
this.totalCopayers = config.wallet.totalCopayers;
var shouldSend = false;
var recipients, pkr = this.publicKeyRing;
var inPKR = copay.PublicKeyRing.fromObj(data.publicKeyRing);
if (pkr.merge(inPKR, true) && !data.isBroadcast) {
this.log('### BROADCASTING PKR');
recipients = null;
shouldSend = true;
}
else if (isInbound && !data.isBroadcast) {
// always replying to connecting peer
this.log('### REPLYING PKR TO:', senderId);
recipients = senderId;
shouldSend = true;
}
if (shouldSend) {
this.sendPublicKeyRing(recipients);
}
this.store();
};
Wallet.prototype.create = function(opts) {
opts = opts || {};
this.id = opts.id || Wallet.getRandomId();
this.log('### CREATING NEW WALLET.' + (opts.id ? ' USING ID: ' + opts.id : ' NEW ID'));
Wallet.prototype._handleTxProposals = function(senderId, data, isInbound) {
this.openWalletId(data.walletId);
this.log('RECV TXPROPOSAL:',data); //TODO
this.privateKey = new copay.PrivateKey({
networkName: this.networkName
});
this.log('\t### PrivateKey Initialized');
var shouldSend = false;
var recipients;
var inTxp = copay.TxProposals.fromObj(data.txProposals);
var mergeInfo = this.txProposals.merge(inTxp, true);
this.publicKeyRing = new copay.PublicKeyRing({
walletId: this.id,
requiredCopayers: opts.requiredCopayers || this.requiredCopayers,
totalCopayers: opts.totalCopayers || this.totalCopayers,
networkName: this.networkName,
});
var addSeen = this.addSeenToTxProposals();
if ((mergeInfo.merged && !data.isBroadcast) || addSeen) {
this.log('### BROADCASTING txProposals. ' );
recipients = null;
shouldSend = true;
}
else if (isInbound && !data.isBroadcast) {
// always replying to connecting peer
this.log('### REPLYING txProposals TO:', senderId);
recipients = senderId;
shouldSend = true;
}
this.publicKeyRing.addCopayer(this.privateKey.getBIP32().extendedPublicKeyString());
this.log('\t### PublicKeyRing Initialized WalletID: ' + this.publicKeyRing.walletId);
this.txProposals = new copay.TxProposals({
walletId: this.id,
networkName: this.networkName,
});
this.log('\t### TxProposals Initialized');
if (shouldSend)
this.sendTxProposals(recipients);
this.store();
};
Wallet.prototype._checkLoad = function(walletId) {
var ret = this.storage.get(walletId, 'publicKeyRing') &&
this.storage.get(walletId, 'txProposals') &&
this.storage.get(walletId, 'privateKey')
;
return ret;
}
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')
);
this.txProposals = new copay.TxProposals.fromObj(
this.storage.get(this.id, 'txProposals')
);
this.privateKey = new copay.PrivateKey.fromObj(
this.storage.get(this.id, 'privateKey')
); //TODO secure
// JIC: Add our key
try {
this.publicKeyRing.addCopayer(
this.privateKey.getBIP32().extendedPublicKeyString()
);
} catch (e) {
this.log('NOT NECCESARY AN ERROR:', e); //TODO
};
Wallet.prototype._handleData = function(senderId, data, isInbound) {
switch(data.type) {
case 'publicKeyRing':
this._handlePublicKeyRing(senderId, data, isInbound);
break;
case 'txProposals':
this._handleTxProposals(senderId, data, isInbound);
break;
case 'abort':
this.emit('abort');
break;
}
};
Wallet.prototype._handleNetworkChange = function(newPeer) {
if (!newPeer) return;
this.log('#### Setting new PEER:', newPeer);
this.sendPublicKeyRing(newPeer);
this.sendTxProposals(newPeer);
};
Wallet.prototype.netStart = function(cb) {
var self = this;
var net = this.network;
net.on('networkChange', function() { self._handleNetworkChange() } );
net.on('data', function() { self._handleData();});
net.on('open', function() {}); // TODO
net.on('close', function() {}); // TODO
net.start(function(peerId) {
self.emit('created');
return cb();
});
};
Wallet.prototype.store = function() {
Wallet.factory.addWalletId(this.id);
this.storage.set(this.id,'opts', {
id: this.id,
spendUnconfirmed: this.spendUnconfirmed,
requiredCopayers: this.requiredCopayers,
totalCopayers: this.totalCopayers,
});
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());
@ -114,6 +149,7 @@ Wallet.prototype.sendTxProposals = function(recipients) {
txProposals: this.txProposals.toObj(),
walletId: this.id,
});
this.emit('txProposalsUpdated', this.txProposals);
};
Wallet.prototype.sendPublicKeyRing = function(recipients) {
@ -124,38 +160,68 @@ Wallet.prototype.sendPublicKeyRing = function(recipients) {
publicKeyRing: this.publicKeyRing.toObj(),
walletId: this.id,
});
this.emit('publicKeyRingUpdated', this.publicKeyRing);
};
Wallet.prototype.generateAddress = function() {
var addr = this.publicKeyRing.generateAddress();
this.store();
this.network.send(null, {
type: 'publicKeyRing',
publicKeyRing: this.publicKeyRing.toObj(),
walletId: this.id
});
this.sendPublicKeyRing();
return addr;
};
Wallet.prototype.getTxProposals = function() {
var ret = [];
this.txProposals.txps.forEach(function(txp) {
var self= this;
self.txProposals.txps.forEach(function(txp) {
var i = {txp:txp};
i.signedByUs = txp.signedBy[this.privateKey.id]?true:false;
i.ntxid = txp.builder.build().getNormalizedHash();
i.signedByUs = txp.signedBy[self.privateKey.id]?true:false;
ret.push(i);
});
return ret;
};
// TODO: this can be precalculated.
Wallet.prototype._findTxByNtxid = function(ntxid) {
var ret;
var l = this.txProposals.txps.length;
var id = ntxid.toString('hex');
for(var i=0; i<l; i++) {
var txp = this.txProposals.txps[i];
var id2 = txp.builder.build().getNormalizedHash().toString('hex');
if (id === id2 ) {
ret = txp;
}
}
return ret;
};
Wallet.prototype.sign = function(ntxid) {
var txp = this._findTxByNtxid(ntxid);
if (!txp) return;
var pkr = this.publicKeyRing;
var keys = this.privateKey.getAll(pkr.addressIndex, pkr.changeAddressIndex);
var ret = txp.builder.sign(keys);
if (ret.signaturesAdded) {
txp.signedBy[this.privateKey.id] = Date.now();
w.store();
this.sendTxProposals();
}
return ret;
};
Wallet.prototype.addSeenToTxProposals = function() {
var ret=false;
var self=this;
this.txProposals.txps.forEach(function(txp) {
if (!txp.seenBy[this.privateKey.id]) {
txp.seenBy[this.privateKey.id] = Date.now();
if (!txp.seenBy[self.privateKey.id]) {
txp.seenBy[self.privateKey.id] = Date.now();
ret = true;
}
});
@ -181,7 +247,33 @@ Wallet.prototype.listUnspent = function(cb) {
this.blockchain.listUnspent(this.getAddressesStr(), cb);
};
Wallet.prototype.createTx = function(toAddress, amountSatStr, utxos, opts) {
Wallet.prototype.createTx = function(toAddress, amountSatStr, opts, cb) {
var self = this;
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
opts = opts || {};
if (typeof opts.spendUnconfirmed === 'undefined') {
opts.spendUnconfirmed = this.spendUnconfirmed;
}
if (!opts.remainderOut) {
opts.remainderOut={ address: this.publicKeyRing.generateAddress(true).toString()};
}
self.listUnspent(function(utxos) {
// TODO check enough funds, etc.
self.createTxSync(toAddress, amountSatStr, utxos, opts);
self.store();
self.sendTxProposals();
return cb();
});
};
Wallet.prototype.createTxSync = function(toAddress, amountSatStr, utxos, opts) {
var pkr = this.publicKeyRing;
var priv = this.privateKey;
opts = opts || {};
@ -216,65 +308,14 @@ Wallet.prototype.createTx = function(toAddress, amountSatStr, utxos, opts) {
});
};
Wallet.prototype.sign = function(txp) {
Wallet.prototype.connectTo = function(peerId) {
this.network.connectTo(peerId);
};
// // HERE? not sure
// Wallet.prototype.cleanPeers = function() {
// this.storage.remove('peerData');
// };
//
Wallet.getRandomId = function() {
var r = buffertools.toHex(coinUtil.generateNonce());
return r;
};
/*
* WalletFactory
*
*/
var WalletFactory = function() {
this.storage = Storage.default();
};
WalletFactory.prototype.create = function(config, opts) {
var w = new Wallet(config);
w.create(opts);
w.store();
return w;
};
WalletFactory.prototype.get = function(config, walletId) {
return Wallet.read(config, walletId);
};
WalletFactory.prototype.remove = function(walletId) {
// TODO remove wallet contents, not only the id (Wallet.remove?)
this._delWalletId(walletId);
};
WalletFactory.prototype.addWalletId = function(walletId) {
var ids = this.getWalletIds();
if (ids.indexOf(walletId) !== -1) return;
ids.push(walletId);
this.storage.setGlobal('walletIds', ids);
};
WalletFactory.prototype._delWalletId = function(walletId) {
var ids = this.getWalletIds();
var index = ids.indexOf(walletId);
if (index === -1) return;
ids.splice(index, 1); // removes walletId
this.storage.setGlobal('walletIds', ids);
};
WalletFactory.prototype.getWalletIds = function() {
var ids = this.storage.getGlobal('walletIds');
return ids || [];
};
Wallet.factory = new WalletFactory();
;
module.exports = require('soop')(Wallet);

View File

@ -0,0 +1,145 @@
'use strict';
var imports = require('soop').imports();
var Storage = imports.Storage;
var Network = imports.Network;
var Blockchain = imports.Blockchain;
var TxProposals = require('./TxProposals');
var PublicKeyRing = require('./PublicKeyRing');
var PrivateKey = require('./PrivateKey');
var Wallet = require('./Wallet');
/*
* WalletFactory
*
*
* var wallet = WF.read(config,walletId); -> always go to storage
* var wallet = WF.create(config,walletId); -> create wallets, with the given ID (or random is not given)
*
* var wallet = WF.open(config,walletId); -> try to read walletId, if fails, create a new wallet with that id
*/
function WalletFactory(config) {
var self = this;
this.storage = new Storage(config.storage);
this.network = new Network(config.network);
this.blockchain = new Blockchain(config.blockchain);
this.networkName = config.networkName;
this.verbose = config.verbose;
this.walletDefaults = config.wallet;
}
WalletFactory.prototype.log = function(){
if (!this.verbose) return;
console.log(arguments);
};
WalletFactory.prototype._checkRead = function(walletId) {
var s = this.storage;
var ret = s.get(walletId, 'publicKeyRing') &&
s.get(walletId, 'txProposals') &&
s.get(walletId, 'opts') &&
s.get(walletId, 'privateKey')
;
return ret;
};
WalletFactory.prototype.read = function(walletId) {
if (! this._checkRead(walletId)) return false;
var s = this.storage;
var opts = s.get(walletId, 'opts');
opts.publicKeyRing = new PublicKeyRing.fromObj(s.get(walletId, 'publicKeyRing'));
opts.txProposals = new TxProposals.fromObj(s.get(walletId, 'txProposals'));
opts.privateKey = new PrivateKey.fromObj(s.get(walletId, 'privateKey'));
opts.storage = this.storage;
opts.network = this.network;
opts.blockchain = this.blockchain;
var w = new Wallet(opts);
// JIC: Add our key
try {
w.publicKeyRing.addCopayer(
w.privateKey.getBIP32().extendedPublicKeyString()
);
} catch (e) {
this.log('NOT NECCESARY AN ERROR:', e); //TODO
}
this.log('### WALLET OPENED:', w.id);
return w;
};
WalletFactory.prototype.create = function(opts) {
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.open = function(walletId) {
if(!WalletFactory.read(walletId)) {
WalletFactory.create({id: walletId});
}
};
WalletFactory.prototype.getWalletIds = function() {
var ids = this.storage.getGlobal('walletIds');
return ids || [];
}
WalletFactory.prototype._delWalletId = function(walletId) {
var ids = this.getWalletIds();
var index = ids.indexOf(walletId);
if (index === -1) return;
ids.splice(index, 1); // removes walletId
this.storage.setGlobal('walletIds', ids);
};
WalletFactory.prototype.remove = function(walletId) {
WalletFactory._delWalletId(walletId);
// TODO remove wallet contents, not only the id (Wallet.remove?)
};
WalletFactory.prototype.addWalletId = function(walletId) {
var ids = this.getWalletIds();
if (ids.indexOf(walletId) !== -1) return;
ids.push(walletId);
this.storage.setGlobal('walletIds', ids);
};
module.exports = require('soop')(WalletFactory);

View File

@ -131,8 +131,7 @@ Network.prototype._addPeer = function(peerId, isInbound) {
}
};
Network.prototype._setupConnectionHandlers = function(
dataConn, isInbound, openCallback, closeCallback) {
Network.prototype._setupConnectionHandlers = function(dataConn, isInbound) {
var self=this;
@ -144,7 +143,7 @@ Network.prototype._setupConnectionHandlers = function(
self._addPeer(dataConn.peer, isInbound);
self._notify( isInbound ? dataConn.peer : null);
if (typeof openCallback === 'function') openCallback();
this.emit('open');
}
});
@ -161,7 +160,7 @@ Network.prototype._setupConnectionHandlers = function(
console.log('### CLOSE RECV FROM:', dataConn.peer);
self._onClose(dataConn.peer);
if (typeof closeCallback === 'function') closeCallback();
this.emit('close');
});
};
@ -174,7 +173,6 @@ Network.prototype._setupPeerHandlers = function(openCallback) {
var self=this;
var p = this.peer;
p.on('open', function(peerId) {
self.peerId = peerId;
self.connectedPeers = [peerId];
@ -218,8 +216,6 @@ 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) {
@ -257,7 +253,7 @@ Network.prototype.send = function(peerIds, data, cb) {
self._sendToOne(peerIds, data, cb);
};
Network.prototype.connectTo = function(peerId, openCallback, closeCallback ) {
Network.prototype.connectTo = function(peerId) {
var self = this;
console.log('### STARTING TO CONNECT TO:' + peerId );
@ -269,7 +265,7 @@ Network.prototype.connectTo = function(peerId, openCallback, closeCallback ) {
metadata: { message: 'hi copayer!' }
});
self._setupConnectionHandlers(dataConn, false, openCallback, closeCallback);
self._setupConnectionHandlers(dataConn, false);
};

View File

@ -12,155 +12,31 @@ angular.module('copay.network')
$rootScope.$digest();
};
// set new inbound connections
var _setNewPeer = function(newPeer) {
var closeWallet = function() {
var w = $rootScope.wallet;
log('#### Setting new PEER:', newPeer);
w.sendPublicKeyRing(newPeer);
w.sendTxProposals(newPeer);
if (w && w.id) w.store();
log('### CLOSING WALLET');
delete $rootScope['wallet'];
};
var _handleNetworkChange = function(newPeer) {
var cp = $rootScope.cp;
if (newPeer)
_setNewPeer(newPeer);
_refreshUx();
};
// TODO -> probably not in network.js
var storeOpenWallet = function() {
var w = $rootScope.wallet;
w.store();
log('\t### Wallet %s Stored', w.id);
};
// 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;
log('createWallet ENDED'); //TODO
};
var openWallet = function (walletId) {
var w = $rootScope.wallet || new copay.Wallet(config);
w.load(walletId);
if (w && w.publicKeyRing && w.privateKey) {
log('### WALLET OPENED:', w.walletId);
$rootScope.wallet = w;
}
};
var closeWallet = function() {
var w = $rootScope.wallet;
if (w && w.id)
w.store();
log('### CLOSING WALLET');
delete $rootScope['wallet'];
};
var _checkWallet = function(walletId) {
log('[network.js.79:_checkWallet:]',walletId); //TODO
if ($rootScope.wallet && $rootScope.wallet.id === walletId)
return;
if ($rootScope.wallet && $rootScope.wallet.id && $rootScope.wallet.id !== walletId) {
throw new Error('message to wrong walletID');
}
if (!openWallet(walletId)) {
createWallet(walletId);
}
};
var _handlePublicKeyRing = function(senderId, data, isInbound) {
_checkWallet(data.walletId);
var shouldSend = false;
var w = $rootScope.wallet;
var recipients, pkr = w.publicKeyRing;
var inPKR = copay.PublicKeyRing.fromObj(data.publicKeyRing);
if (pkr.merge(inPKR, true) && !data.isBroadcast) {
log('### BROADCASTING PKR');
recipients = null;
shouldSend = true;
}
else if (isInbound && !data.isBroadcast) {
// always replying to connecting peer
log('### REPLYING PKR TO:', senderId);
recipients = senderId;
shouldSend = true;
}
if (shouldSend) {
w.sendPublicKeyRing(recipients);
}
// public methods
var init = function(walletId, cb) {
if (!$rootScope.wallet) {
// create an empty Wallet
$rootScope.wallet = new copay.Wallet(config);
}
var w = $rootScope.wallet;
console.log('[network.js.30:walletId:]',walletId); //TODO
if (!walletId) w.openWalletId();
w.on('created', _refreshUx);
w.on('txProposals', _refreshUx);
w.on('publicKeyRing', _refreshUx);
w.on('abort', function() {
disconnect();
_refreshUx();
};
var _handleTxProposals = function(senderId, data, isInbound) {
_checkWallet(data.walletId);
var shouldSend = false;
var w = $rootScope.wallet;
log('RECV TXPROPOSAL:',data); //TODO
var recipients;
var inTxProposals = copay.TxProposals.fromObj(data.txProposals);
var mergeInfo = w.txProposals.merge(inTxProposals, true);
var addSeen = w.addSeenToTxProposals();
if ((mergeInfo.merged && !data.isBroadcast) || addSeen) {
log('### BROADCASTING txProposals. ' );
recipients = null;
shouldSend = true;
}
else if (isInbound && !data.isBroadcast) {
// always replying to connecting peer
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();
});
});
w.netStart(cb);
};
var disconnect = function() {
@ -174,14 +50,9 @@ angular.module('copay.network')
};
var connect = function(peerId, openCallback, failCallback) {
if ($rootScope.wallet.network) {
$rootScope.wallet.network.connectTo(peerId, openCallback, function () {
disconnect();
failCallback();
});
}
else
return failCallback();
$rootScope.wallet.connectTo(peerId);
$rootScope.wallet.on('open', openCallback);
$rootScope.wallet.on('close', failCallback);
};
var sendTxProposals = function(recipients) {
@ -193,11 +64,6 @@ angular.module('copay.network')
init: init,
connect: connect,
disconnect: disconnect,
createWallet: createWallet,
openWallet: openWallet,
storeOpenWallet: storeOpenWallet,
sendTxProposals: sendTxProposals,
}
});

View File

@ -0,0 +1,8 @@
'use strict';
var wf;
angular.module('copay.walletFactory').factory('walletFactory', function($rootScope) {
wf = wf || new copay.WalletFactory(config);
return wf;
});

View File

@ -0,0 +1,57 @@
'use strict';
var chai = chai || require('chai');
var should = chai.should();
var WebRTC = require('../js/models/network/WebRTC');
var Insight = require('../js/models/blockchain/Insight');
var FakeStorage = require('./FakeStorage');
var WalletFactory = typeof copay === 'undefined' ? require('soop').load('../js/models/core/WalletFactory',{
Network: WebRTC,
Blockchain: Insight,
Storage: FakeStorage,
}) : copay.WalletFactory;
var addCopayers = function (w) {
for(var i=0; i<4; i++) {
w.publicKeyRing.addCopayer();
}
};
describe('WalletFactory model', function() {
var config = {
wallet: {
requiredCopayers: 3,
totalCopayers: 5,
spendUnconfirmed: 1,
},
blockchain: {
host: 'test.insight.is',
port: 80
},
networkName: 'testnet',
};
describe('factory', function() {
it('should create the factory', function() {
var wf = new WalletFactory(config);
should.exist(wf);
});
it('should be able to create wallets', function() {
var wf = new WalletFactory(config);
var w = wf.create();
should.exist(w);
});
it('should be able to get wallets', function() {
var wf = new WalletFactory(config);
var w = wf.create();
var w2 = wf.read(w.id);
should.exist(w2);
w2.id.should.equal(w.id);
});
});
});