wallet refactor

This commit is contained in:
Manuel Araoz 2014-08-19 10:20:35 -04:00
parent 1c3c7f0bde
commit 28e00b24cc
2 changed files with 11 additions and 9 deletions

View File

@ -33,20 +33,16 @@ function Wallet(opts) {
'publicKeyRing', 'txProposals', 'privateKey', 'version',
'reconnectDelay'
].forEach(function(k) {
if (typeof opts[k] === 'undefined')
throw new Error('missing required option for Wallet: ' + k);
preconditions.checkArgument(typeof opts[k] !== 'undefined',
'missing required option for Wallet: ' + k);
self[k] = opts[k];
});
if (copayConfig.forceNetwork && this.getNetworkName() !== copayConfig.networkName)
throw new Error('Network forced to ' + copayConfig.networkName +
' and tried to create a Wallet with network ' + this.getNetworkName());
this.log('creating ' + opts.requiredCopayers + ' of ' + opts.totalCopayers + ' wallet');
preconditions.checkArgument(!copayConfig.forceNetwork || this.getNetworkName() === copayConfig.networkName,
'Network forced to ' + copayConfig.networkName +
' and tried to create a Wallet with network ' + this.getNetworkName());
this.id = opts.id || Wallet.getRandomId();
this.lock = new WalletLock(this.storage, this.id, opts.lockTimeOutMin);
this.name = opts.name;
this.verbose = opts.verbose;
@ -56,6 +52,7 @@ function Wallet(opts) {
this.registeredPeerIds = [];
this.addressBook = opts.addressBook || {};
this.publicKey = this.privateKey.publicHex;
this.lastTimestamp = opts.lastTimestamp || undefined;
this.paymentRequests = opts.paymentRequests || {};
@ -540,6 +537,7 @@ Wallet.prototype.toObj = function() {
txProposals: this.txProposals.toObj(),
privateKey: this.privateKey ? this.privateKey.toObj() : undefined,
addressBook: this.addressBook,
lastTimestamp: this.lastTimestamp,
};
return walletObj;
@ -578,6 +576,8 @@ Wallet.fromObj = function(o, storage, network, blockchain) {
opts.txProposals = new TxProposals({
networkName: this.networkName,
});
opts.lastTimestamp = o.lastTimestamp;
opts.storage = storage;
opts.network = network;

View File

@ -23,6 +23,7 @@ var preconditions = require('preconditions').singleton();
*/
function Network(opts) {
preconditions.checkArgument(opts);
var self = this;
opts = opts || {};
this.maxPeers = opts.maxPeers || 12;
@ -121,6 +122,7 @@ Network.prototype._addConnectedCopayer = function(copayerId) {
Network.prototype.getKey = function() {
if (!this.key) {
preconditions.checkState(this.privkey);
var key = new bitcore.Key();
key.private = new Buffer(this.privkey, 'hex');
key.regenerateSync();