copay/js/models/core/Wallet.js

231 lines
5.8 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-15 10:28:49 -07:00
Wallet.prototype.log = function(){
if (!this.verbose) return;
console.this.log(arguments);
}
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-15 08:52:28 -07:00
this.networkName = config.networkName;
this.requiredCopayers = config.wallet.requiredCopayers;
this.totalCopayers = config.wallet.totalCopayers;
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-15 08:52:28 -07:00
Wallet.prototype.create = function(opts) {
2014-04-14 14:30:08 -07:00
this.id = opts.id || Wallet.getRandomId();
2014-04-15 10:28:49 -07:00
this.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({
2014-04-15 08:52:28 -07:00
networkName: this.networkName
2014-04-14 14:30:08 -07:00
});
2014-04-15 10:28:49 -07:00
this.log('\t### PrivateKey Initialized');
2014-04-14 13:42:10 -07:00
2014-04-15 08:52:28 -07:00
this.publicKeyRing = new copay.PublicKeyRing({
walletId: this.id,
requiredCopayers: opts.requiredCopayers || this.requiredCopayers,
totalCopayers: opts.totalCopayers || this.totalCopayers,
networkName: this.networkName,
2014-04-10 13:57:41 -07:00
});
2014-04-15 08:52:28 -07:00
2014-04-14 13:17:56 -07:00
this.publicKeyRing.addCopayer(this.privateKey.getBIP32().extendedPublicKeyString());
2014-04-15 10:28:49 -07:00
this.log('\t### PublicKeyRing Initialized WalletID: ' + this.publicKeyRing.walletId);
2014-04-14 13:17:56 -07:00
2014-04-15 08:52:28 -07:00
this.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,
2014-04-15 08:52:28 -07:00
networkName: this.networkName,
2014-04-10 13:57:41 -07:00
});
2014-04-15 10:28:49 -07:00
this.log('\t### TxProposals Initialized');
2014-04-14 13:42:10 -07:00
};
2014-04-15 08:52:28 -07:00
Wallet.prototype._checkLoad = function(walletId) {
2014-04-15 11:25:55 -07:00
var ret = this.storage.get(walletId, 'publicKeyRing') &&
2014-04-15 08:52:28 -07:00
this.storage.get(walletId, 'txProposals') &&
this.storage.get(walletId, 'privateKey')
2014-04-15 11:25:55 -07:00
;
console.log('[Wallet.js.71]',
this.storage.get(walletId, 'publicKeyRing'),
this.storage.get(walletId, 'txProposals'),
this.storage.get(walletId, 'privateKey'));
console.log('[Wallet.js.73:ret:]',walletId, ret); //TODO
return ret;
2014-04-15 08:17:28 -07:00
}
2014-04-15 08:52:28 -07:00
Wallet.prototype.load = function(walletId) {
if (! this._checkLoad(walletId)) return;
2014-04-15 08:17:28 -07:00
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) {
2014-04-15 10:28:49 -07:00
this.log('NOT NECCESARY AN ERROR:', e); //TODO
2014-04-14 13:42:10 -07:00
};
};
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) {
2014-04-15 10:28:49 -07:00
this.log('### SENDING txProposals TO:', recipients||'All', this.txProposals);
2014-04-15 08:17:28 -07:00
this.network.send( recipients, {
type: 'txProposals',
txProposals: this.txProposals.toObj(),
walletId: this.id,
});
};
Wallet.prototype.sendPublicKeyRing = function(recipients) {
2014-04-15 10:28:49 -07:00
this.log('### SENDING publicKeyRing TO:', recipients||'All', this.publicKeyRing.toObj());
2014-04-15 08:17:28 -07:00
this.network.send(recipients, {
type: 'publicKeyRing',
publicKeyRing: this.publicKeyRing.toObj(),
walletId: this.id,
});
};
2014-04-15 12:50:16 -07:00
Wallet.prototype.generateAddress = function() {
var addr = this.publicKeyRing.generateAddress();
this.store();
this.network.send(null, {
type: 'publicKeyRing',
publicKeyRing: this.publicKeyRing.toObj(),
walletId: this.id
});
return addr;
};
2014-04-15 08:17:28 -07:00
2014-04-15 11:25:55 -07:00
Wallet.prototype.getTxProposals = function() {
var ret = [];
this.txProposals.txps.forEach(function(txp) {
var i = {txp:txp};
i.signedByUs = txp.signedBy[this.privateKey.id]?true:false;
ret.push(i);
});
return ret;
};
Wallet.prototype.addSeenToTxProposals = function() {
var ret=false;
this.txProposals.txps.forEach(function(txp) {
if (!txp.seenBy[this.privateKey.id]) {
txp.seenBy[this.privateKey.id] = Date.now();
ret = true;
}
});
return ret;
};
2014-04-15 08:17:28 -07:00
// // HERE? not sure
// Wallet.prototype.cleanPeers = function() {
// this.storage.remove('peerData');
// };
//
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) {
2014-04-15 08:52:28 -07:00
var w = new Wallet(config);
w.create(opts);
2014-04-14 14:30:08 -07:00
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);
};
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-15 09:21:14 -07:00
if (ids.indexOf(walletId) !== -1) return;
ids.push(walletId);
this.storage.setGlobal('walletIds', ids);
2014-04-14 14:30:08 -07:00
};
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);
2014-04-15 09:21:14 -07:00
if (index === -1) return;
2014-04-14 14:30:08 -07:00
ids.splice(index, 1); // removes walletId
2014-04-15 09:21:14 -07:00
this.storage.setGlobal('walletIds', ids);
2014-04-14 14:30:08 -07:00
};
2014-04-15 08:17:28 -07:00
WalletFactory.prototype.getWalletIds = function() {
2014-04-15 09:21:14 -07:00
var ids = this.storage.getGlobal('walletIds');
return ids || [];
2014-04-14 14:30:08 -07:00
};
Wallet.factory = new WalletFactory();
module.exports = require('soop')(Wallet);