copay/js/models/core/WalletFactory.js

232 lines
6.4 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
*
*/
2014-05-14 17:02:01 -07:00
function WalletFactory(config, version) {
2014-04-16 13:50:10 -07:00
var self = this;
2014-05-21 07:10:19 -07:00
config = config || {};
2014-04-16 13:50:10 -07:00
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;
2014-04-16 13:50:10 -07:00
this.walletDefaults = config.wallet;
this.version = version;
2014-04-16 13:50:10 -07:00
}
WalletFactory.prototype.log = function(){
if (!this.verbose) return;
2014-06-03 14:38:56 -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');
2014-06-02 14:59:38 -07:00
return !!ret;
2014-04-16 13:50:10 -07:00
};
2014-04-25 15:12:13 -07:00
WalletFactory.prototype.fromObj = function(obj) {
var w = Wallet.fromObj(obj, this.storage, this.network, this.blockchain);
w.verbose = this.verbose;
2014-04-25 15:12:13 -07:00
return w;
};
2014-05-01 14:32:22 -07:00
WalletFactory.prototype.fromEncryptedObj = function(base64, password) {
this.storage._setPassphrase(password);
var walletObj = this.storage.import(base64);
2014-06-02 14:59:38 -07:00
if (!walletObj) return false;
var w = this.fromObj(walletObj);
2014-06-02 14:59:38 -07:00
if (!w) return false;
return w;
2014-05-01 14:32:22 -07:00
};
WalletFactory.prototype.read = function(walletId) {
2014-04-25 15:12:13 -07:00
if (! this._checkRead(walletId))
2014-06-02 14:59:38 -07:00
return false;
2014-04-25 15:12:13 -07:00
2014-04-28 07:22:41 -07:00
var obj = {};
2014-04-25 15:12:13 -07:00
var s = this.storage;
2014-04-28 07:22:41 -07:00
obj.id = walletId;
obj.opts = s.get(walletId, 'opts');
obj.publicKeyRing = s.get(walletId, 'publicKeyRing');
obj.txProposals = s.get(walletId, 'txProposals');
obj.privateKey = s.get(walletId, 'privateKey');
var w = this.fromObj(obj);
2014-04-16 13:50:10 -07:00
return w;
};
WalletFactory.prototype.create = function(opts) {
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
2014-06-09 13:52:51 -07:00
opts.privateKey = opts.privateKey || new PrivateKey({ retworkName: 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,
});
opts.publicKeyRing.addCopayer(
opts.privateKey.deriveBIP45Branch().extendedPublicKeyString(),
opts.nickname);
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');
this.storage._setPassphrase(opts.passphrase);
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
opts.spendUnconfirmed = opts.spendUnconfirmed || this.walletDefaults.spendUnconfirmed;
2014-06-02 13:40:29 -07:00
opts.reconnectDelay = opts.reconnectDelay || this.walletDefaults.reconnectDelay;
2014-04-16 13:50:10 -07:00
opts.requiredCopayers = requiredCopayers;
opts.totalCopayers = totalCopayers;
2014-05-21 07:10:19 -07:00
opts.version = opts.version || this.version;
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-05-14 17:02:01 -07:00
WalletFactory.prototype._checkVersion = function(inVersion) {
var thisV = this.version.split('.');
var thisV0 = parseInt(thisV[0]);
var inV = inVersion.split('.');
var inV0 = parseInt(inV[0]);
//We only check for major version differences
if( thisV0 < inV0 ) {
throw new Error('Major difference in software versions' +
'. Received:' + inVersion +
'. Current version:' + this.version +
'. Aborting.');
}
};
WalletFactory.prototype._checkNetwork = function(inNetworkName) {
if( this.networkName !== inNetworkName ) {
throw new Error('This Wallet is configured for '
+ inNetworkName
+ ' while currently Copay is configured for: '
+ this.networkName
+ '. Check your settings.'
);
}
};
2014-04-18 14:25:51 -07:00
WalletFactory.prototype.open = function(walletId, opts) {
opts = opts || {};
opts.id = walletId;
opts.verbose = this.verbose;
this.storage._setPassphrase(opts.passphrase);
var w = this.read(walletId);
if (w) {
2014-05-14 20:01:16 -07:00
this._checkVersion(w.version);
2014-06-09 14:01:15 -07:00
this._checkNetwork(w.getNetworkName());
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-05-14 14:24:24 -07:00
WalletFactory.prototype.decodeSecret = function(secret) {
try {
return Wallet.decodeSecret(secret);
} catch (e) {
return false;
}
2014-06-09 14:01:15 -07:00
};
2014-04-16 16:58:57 -07:00
2014-05-01 14:34:30 -07:00
WalletFactory.prototype.joinCreateSession = function(secret, nickname, passphrase, cb) {
2014-04-20 08:41:28 -07:00
var self = this;
2014-05-14 14:24:24 -07:00
var s = self.decodeSecret(secret);
if (!s) return cb('badSecret');
2014-05-01 14:34:30 -07:00
2014-04-20 08:41:28 -07:00
//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(),
2014-04-30 08:58:40 -07:00
netKey: s.netKey,
2014-04-24 19:13:55 -07:00
};
self.network.cleanUp();
self.network.start(opts, function() {
2014-04-30 08:58:40 -07:00
self.network.connectTo(s.pubKey);
// This is a hack to reconize if the connection was rejected or the peer wasn't there.
var connectedOnce = false;
self.network.on('connected', function(sender, data) {
connectedOnce = true;
});
2014-04-24 19:13:55 -07:00
self.network.on('onlyYou', function(sender, data) {
return cb(connectedOnce ? 'walletFull' : 'joinError');
2014-04-24 19:13:55 -07:00
});
2014-04-20 12:16:09 -07:00
self.network.on('data', function(sender, data) {
if (data.type ==='walletId') {
data.opts.privateKey = privateKey;
2014-05-01 05:41:18 -07:00
data.opts.nickname = nickname;
2014-05-01 14:34:30 -07:00
data.opts.passphrase = passphrase;
data.opts.id = data.walletId;
var w = self.create(data.opts);
2014-05-09 10:35:57 -07:00
w.seedCopayer(s.pubKey);
2014-04-30 08:58:40 -07:00
return cb(null, w);
2014-04-20 12:16:09 -07:00
}
2014-04-16 16:58:57 -07:00
});
});
};
2014-04-16 13:50:10 -07:00
module.exports = require('soop')(WalletFactory);