copay/js/models/core/Wallet.js

193 lines
4.9 KiB
JavaScript
Raw Normal View History

2014-04-10 13:57:41 -07:00
'use strict';
2014-04-14 14:30:08 -07:00
var imports = require('soop').imports();
2014-04-14 13:17:56 -07:00
2014-04-14 14:30:08 -07:00
var bitcore = require('bitcore');
var coinUtil = bitcore.util;
var buffertools = bitcore.buffertools;
var http = require('http');
2014-04-10 13:57:41 -07:00
2014-04-14 14:30:08 -07:00
var Storage = imports.Storage;
var Network = imports.Network;
var Blockchain = imports.Blockchain;
2014-04-14 13:17:56 -07:00
var copay = copay || require('../../../copay');
2014-04-14 13:42:10 -07:00
function Wallet(config) {
this._startInterface(config);
}
2014-04-10 13:57:41 -07:00
2014-04-14 13:42:10 -07:00
Wallet.prototype._startInterface = function(config) {
2014-04-14 14:30:08 -07:00
this.storage = new Storage(config.storage);
this.network = new Network(config.network);
2014-04-14 13:17:56 -07:00
this.blockchain = new Blockchain(config.blockchain);
2014-04-14 13:42:10 -07:00
};
2014-04-14 13:17:56 -07:00
2014-04-14 14:30:08 -07:00
2014-04-14 13:42:10 -07:00
Wallet.prototype._createNew = function(config, opts) {
2014-04-14 14:30:08 -07:00
this.id = opts.id || Wallet.getRandomId();
console.log('### CREATING NEW WALLET.' + (opts.id ? ' USING ID: ' + opts.id : ' NEW ID'));
2014-04-14 13:17:56 -07:00
2014-04-14 14:30:08 -07:00
this.privateKey = new copay.PrivateKey({
networkName: config.networkName
});
2014-04-14 13:17:56 -07:00
console.log('\t### PrivateKey Initialized');
2014-04-14 13:42:10 -07:00
2014-04-14 13:17:56 -07:00
this.publicKeyRing = opts.publicKeyRing || new copay.PublicKeyRing({
2014-04-14 14:30:08 -07:00
id: this.id,
2014-04-14 13:17:56 -07:00
requiredCopayers: opts.requiredCopayers || config.wallet.requiredCopayers,
2014-04-14 14:30:08 -07:00
totalCopayers: opts.totalCopayers || config.wallet.totalCopayers,
2014-04-14 13:17:56 -07:00
networkName: config.networkName,
2014-04-10 13:57:41 -07:00
});
2014-04-14 13:17:56 -07:00
this.publicKeyRing.addCopayer(this.privateKey.getBIP32().extendedPublicKeyString());
2014-04-14 14:30:08 -07:00
console.log('\t### PublicKeyRing Initialized WalletID: ' + this.publicKeyRing.id);
2014-04-14 13:17:56 -07:00
2014-04-14 13:42:10 -07:00
this.txProposals = opts.txProposals || new copay.TxProposals({
2014-04-14 14:30:08 -07:00
walletId: this.id,
2014-04-14 13:17:56 -07:00
publicKeyRing: this.publicKeyRing,
networkName: config.networkName,
2014-04-10 13:57:41 -07:00
});
2014-04-14 13:17:56 -07:00
console.log('\t### TxProposals Initialized');
2014-04-14 13:42:10 -07:00
};
2014-04-15 08:17:28 -07:00
Wallet.prototype._checkLoad = function(config, walletId) {
return (
this.storage.get(this.id, 'publicKeyRing') &&
this.storage.get(this.id, 'txProposals') &&
this.storage.get(this.id, 'privateKey')
);
}
2014-04-14 13:42:10 -07:00
Wallet.prototype._load = function(config, walletId) {
2014-04-15 08:17:28 -07:00
if (! this._checkLoad(config,walletId)) return;
2014-04-14 14:30:08 -07:00
this.id = walletId;
this.publicKeyRing = new copay.PublicKeyRing.fromObj(
2014-04-14 13:42:10 -07:00
this.storage.get(this.id, 'publicKeyRing')
);
2014-04-14 14:30:08 -07:00
this.txProposals = new copay.TxProposals.fromObj(
2014-04-14 13:42:10 -07:00
this.storage.get(this.id, 'txProposals')
);
2014-04-14 14:30:08 -07:00
this.privateKey = new copay.PrivateKey.fromObj(
2014-04-14 13:42:10 -07:00
this.storage.get(this.id, 'privateKey')
); //TODO secure
// JIC: Add our key
try {
this.publicKeyRing.addCopayer(
this.privateKey.getBIP32().extendedPublicKeyString()
);
} catch (e) {
console.log('NOT NECCESARY AN ERROR:', e); //TODO
};
};
2014-04-15 06:22:50 -07:00
Wallet.prototype.store = function() {
2014-04-15 08:17:28 -07:00
Wallet.factory.addWalletId(this.id);
2014-04-15 06:22:50 -07:00
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());
};
2014-04-15 08:17:28 -07:00
Wallet.prototype.sendTxProposals = function(recipients) {
console.log('### SENDING txProposals TO:', recipients||'All', this.txProposals);
this.network.send( recipients, {
type: 'txProposals',
txProposals: this.txProposals.toObj(),
walletId: this.id,
});
};
Wallet.prototype.sendPublicKeyRing = function(recipients) {
console.log('### SENDING publicKeyRing TO:', recipients||'All');
console.log('[Wallet.js.100]', this.publicKeyRing.toObj()); //TODO
this.network.send(recipients, {
type: 'publicKeyRing',
publicKeyRing: this.publicKeyRing.toObj(),
walletId: this.id,
});
};
// // HERE? not sure
// Wallet.prototype.cleanPeers = function() {
// this.storage.remove('peerData');
// };
//
2014-04-14 13:42:10 -07:00
// CONSTRUCTORS
Wallet.read = function(config, walletId) {
var w = new Wallet(config);
2014-04-15 08:17:28 -07:00
w._load(config, walletId);
2014-04-14 13:42:10 -07:00
return w;
};
Wallet.create = function(config, opts) {
var w = new Wallet(config);
w._createNew(config, opts);
return w;
};
2014-04-10 13:57:41 -07:00
2014-04-14 14:30:08 -07:00
Wallet.getRandomId = function() {
var r = buffertools.toHex(coinUtil.generateNonce());
return r;
};
2014-04-15 08:17:28 -07:00
/*
* WalletFactory
*
*/
2014-04-10 13:57:41 -07:00
2014-04-14 14:30:08 -07:00
var WalletFactory = function() {
2014-04-15 08:17:28 -07:00
this.storage = Storage.default();
2014-04-14 14:30:08 -07:00
};
WalletFactory.prototype.create = function(config, opts) {
var w = new Wallet.create(config, opts);
w.store();
2014-04-15 08:17:28 -07:00
this.addWalletId(w.id);
2014-04-14 14:30:08 -07:00
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);
};
2014-04-10 13:57:41 -07:00
2014-04-15 08:17:28 -07:00
WalletFactory.prototype.addWalletId = function(walletId) {
var ids = this.getWalletIds();
2014-04-14 14:30:08 -07:00
if (ids.indexOf(walletId) == -1) return;
storage.set('walletIds', (ids ? ids + ',' : '') + walletId);
};
WalletFactory.prototype._delWalletId = function(walletId) {
2014-04-15 08:17:28 -07:00
var ids = this.getWalletIds();
2014-04-14 14:30:08 -07:00
var index = ids.indexOf(walletId);
if (index == -1) return;
ids.splice(index, 1); // removes walletId
this.storage.set('walletIds', ids.join(','));
};
2014-04-15 08:17:28 -07:00
WalletFactory.prototype.getWalletIds = function() {
2014-04-14 14:30:08 -07:00
var ids = this.storage.get('walletIds');
return ids ? ids.split(',') : [];
};
Wallet.factory = new WalletFactory();
module.exports = require('soop')(Wallet);