copay/js/models/core/WalletFactory.js

185 lines
5.2 KiB
JavaScript
Raw Normal View History

2014-04-16 13:50:10 -07:00
'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;
2014-04-20 16:24:24 -07:00
if (console)
console.log.apply(console, arguments);
2014-04-16 13:50:10 -07:00
};
WalletFactory.prototype._checkRead = function(walletId) {
var s = this.storage;
2014-04-20 08:41:28 -07:00
var ret =
(
s.get(walletId, 'publicKeyRing') &&
s.get(walletId, 'txProposals') &&
s.get(walletId, 'opts') &&
s.get(walletId, 'privateKey')
)?true:false;
2014-04-16 13:50:10 -07:00
;
2014-04-18 07:19:39 -07:00
return ret?true:false;
2014-04-16 13:50:10 -07:00
};
2014-04-25 15:12:13 -07:00
WalletFactory.prototype.fromObj = function(obj) {
var opts = obj.opts;
opts.publicKeyRing = new PublicKeyRing.fromObj(obj.publicKeyRing);
opts.txProposals = new TxProposals.fromObj(obj.txProposals);
opts.privateKey = new PrivateKey.fromObj(obj.privateKey);
2014-04-16 13:50:10 -07:00
opts.storage = this.storage;
opts.network = this.network;
opts.blockchain = this.blockchain;
2014-04-16 16:58:57 -07:00
opts.verbose = this.verbose;
2014-04-16 13:50:10 -07:00
var w = new Wallet(opts);
// JIC: Add our key
try {
w.publicKeyRing.addCopayer(
2014-04-17 13:01:31 -07:00
w.privateKey.getExtendedPublicKeyString()
2014-04-16 13:50:10 -07:00
);
} catch (e) {
// No really an error, just to be sure.
2014-04-16 13:50:10 -07:00
}
this.log('### WALLET OPENED:', w.id);
2014-04-25 15:12:13 -07:00
return w;
};
WalletFactory.prototype.read = function(walletId) {
if (! this._checkRead(walletId))
return false;
var s = this.storage;
var opts = s.get(walletId, 'opts');
opts.id = walletId;
opts.publicKeyRing = s.get(walletId, 'publicKeyRing');
opts.txProposals = s.get(walletId, 'txProposals');
opts.privateKey = s.get(walletId, 'privateKey');
w = this.formObj(opts);
2014-04-16 13:50:10 -07:00
return w;
};
WalletFactory.prototype.create = function(opts) {
var s = WalletFactory.storage;
opts = opts || {};
2014-04-20 08:41:28 -07:00
this.log('### CREATING NEW WALLET.' +
(opts.id ? ' USING ID: ' + opts.id : ' NEW ID') +
(opts.privateKey ? ' USING PrivateKey: ' + opts.privateKey.getId() : ' NEW PrivateKey')
);
2014-04-16 13:50:10 -07:00
opts.privateKey = opts.privateKey || new PrivateKey({ networkName: this.networkName });
2014-04-20 08:41:28 -07:00
2014-04-16 13:50:10 -07:00
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,
});
2014-04-17 13:01:31 -07:00
opts.publicKeyRing.addCopayer(opts.privateKey.getExtendedPublicKeyString());
2014-04-16 13:50:10 -07:00
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;
2014-04-16 16:58:57 -07:00
opts.verbose = this.verbose;
2014-04-16 13:50:10 -07:00
opts.spendUnconfirmed = opts.spendUnconfirmed || this.walletDefaults.spendUnconfirmed;
opts.requiredCopayers = requiredCopayers;
opts.totalCopayers = totalCopayers;
2014-04-17 13:01:31 -07:00
var w = new Wallet(opts);
2014-04-16 13:50:10 -07:00
w.store();
return w;
};
2014-04-18 14:25:51 -07:00
WalletFactory.prototype.open = function(walletId, opts) {
2014-04-18 10:40:16 -07:00
this.log('Opening walletId:' + walletId);
2014-04-18 14:25:51 -07:00
opts = opts || {};
opts.id = walletId;
opts.verbose = this.verbose;
var w = this.read(walletId) || this.create(opts);
w.store();
2014-04-16 16:58:57 -07:00
return w;
};
WalletFactory.prototype.getWallets = function() {
var ret = this.storage.getWallets();
ret.forEach(function(i) {
i.show = i.name ? ( (i.name + ' <'+i.id+'>') ) : i.id;
});
return ret;
};
2014-04-16 13:50:10 -07:00
WalletFactory.prototype.remove = function(walletId) {
// TODO remove wallet contents
2014-04-21 03:27:45 -07:00
this.log('TODO: remove wallet contents');
2014-04-16 13:50:10 -07:00
};
2014-04-16 16:58:57 -07:00
2014-04-23 09:44:20 -07:00
WalletFactory.prototype.joinCreateSession = function(copayerId, cb) {
2014-04-20 08:41:28 -07:00
var self = this;
//Create our PrivateK
var privateKey = new PrivateKey({ networkName: this.networkName });
this.log('\t### PrivateKey Initialized');
2014-04-24 19:13:55 -07:00
var opts = {
copayerId: privateKey.getId(),
signingKeyHex: privateKey.getSigningKey(),
};
self.network.cleanUp();
self.network.start(opts, function() {
2014-04-23 09:44:20 -07:00
self.network.connectTo(copayerId);
2014-04-24 19:13:55 -07:00
self.network.on('onlyYou', function(sender, data) {
return cb();
});
2014-04-20 12:16:09 -07:00
self.network.on('data', function(sender, data) {
if (data.type ==='walletId') {
data.opts.privateKey = privateKey;
var w = self.open(data.walletId, data.opts);
2014-04-23 09:44:20 -07:00
w.firstCopayerId = copayerId;
2014-04-20 12:16:09 -07:00
return cb(w);
}
2014-04-16 16:58:57 -07:00
});
});
};
2014-04-16 13:50:10 -07:00
module.exports = require('soop')(WalletFactory);