mv wallet test from walletfactory to wallet
This commit is contained in:
parent
1ff109b5e5
commit
abf74695b6
5 changed files with 150 additions and 94 deletions
|
|
@ -142,6 +142,13 @@ Identity.isAvailable = function(email, opts, cb) {
|
|||
};
|
||||
|
||||
|
||||
/**
|
||||
* store
|
||||
*
|
||||
* @param opts
|
||||
* @param cb
|
||||
* @return {undefined}
|
||||
*/
|
||||
Identity.prototype.store = function(opts, cb) {
|
||||
var self = this;
|
||||
self.profile.store(opts, function(err) {
|
||||
|
|
@ -319,7 +326,7 @@ Identity.prototype.read_Old = function(walletId, skipFields, cb) {
|
|||
*/
|
||||
|
||||
|
||||
Identity.prototype._getWallet = function(opts) {
|
||||
Identity.prototype._newWallet = function(opts) {
|
||||
return new Wallet(opts);
|
||||
};
|
||||
|
||||
|
|
@ -396,12 +403,12 @@ Identity.prototype.createWallet = function(opts, cb) {
|
|||
|
||||
|
||||
var self = this;
|
||||
var w = this._getWallet(opts);
|
||||
var w = this._newWallet(opts);
|
||||
this.profile.addWallet(w.id, function(err) {
|
||||
if (err) return cb(err);
|
||||
w.store(function(err) {
|
||||
if (err) return cb(err);
|
||||
self.storage.setLastOpened(w.id, function(err) {
|
||||
self.profile.setLastOpenedTs(w.id, function(err) {
|
||||
return cb(err, w);
|
||||
});
|
||||
});
|
||||
|
|
@ -445,7 +452,7 @@ Identity.prototype.openWallet = function(walletId, passphrase, cb) {
|
|||
if (err) return cb(err);
|
||||
|
||||
w.store(function(err) {
|
||||
self.storage.setLastOpened(walletId, function() {
|
||||
self.profile.setLastOpenedTs(walletId, function() {
|
||||
return cb(err, w);
|
||||
});
|
||||
});
|
||||
|
|
@ -454,42 +461,25 @@ Identity.prototype.openWallet = function(walletId, passphrase, cb) {
|
|||
};
|
||||
|
||||
|
||||
TODO
|
||||
from profile
|
||||
implement lastOpen
|
||||
Identity.prototype.listWallets = function(cb) {
|
||||
var self = this;
|
||||
this.storage.getWallets(function(wallets) {
|
||||
wallets.forEach(function(i) {
|
||||
i.show = i.name ? ((i.name + ' <' + i.id + '>')) : i.id;
|
||||
});
|
||||
self.storage.getLastOpened(function(lastId) {
|
||||
var last = _.findWhere(wallets, {
|
||||
id: lastId
|
||||
});
|
||||
if (last)
|
||||
last.lastOpened = true;
|
||||
return cb(null, wallets);
|
||||
})
|
||||
});
|
||||
Identity.prototype.listWallets = function() {
|
||||
return this.profile.listWallets();
|
||||
};
|
||||
|
||||
/**
|
||||
* @desc Deletes this wallet. This involves removing it from the storage instance
|
||||
* @TODO: delete is a reserved javascript keyword. NEVER USE IT.
|
||||
* @param {string} walletId
|
||||
* @TODO: Why is there a callback?
|
||||
* @callback cb
|
||||
* @return {?} the result of the callback
|
||||
* @return {err}
|
||||
*/
|
||||
Identity.prototype.deleteWallet = function(walletId, cb) {
|
||||
var self = this;
|
||||
self.storage.deleteWallet(walletId, function(err) {
|
||||
|
||||
Wallet.delete(walletId, this.storage, function(err) {
|
||||
if (err) return cb(err);
|
||||
self.storage.setLastOpened(null, function(err) {
|
||||
self.profile.deleteWallet(walletId, function(err) {
|
||||
return cb(err);
|
||||
});
|
||||
});
|
||||
})
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -45,19 +45,19 @@ Profile.open = function(storage, cb) {
|
|||
});
|
||||
};
|
||||
|
||||
Profile.prototype.getWallet = function(walletId, cb) {
|
||||
Profile.prototype.getWallet = function(walletId, cb) {
|
||||
return this.walletInfos[walletId];
|
||||
};
|
||||
|
||||
Profile.prototype.listWallets = function(opts, cb) {
|
||||
return _.sortBy(this.walletInfos,function(winfo){
|
||||
Profile.prototype.listWallets = function(opts, cb) {
|
||||
return _.sortBy(this.walletInfos, function(winfo) {
|
||||
return winfo.lastOpenedTs || winfo.createdTs;
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Profile.prototype.deleteWallet = function(walletId, cb) {
|
||||
if (!this.walletInfos[walletId])
|
||||
Profile.prototype.deleteWallet = function(walletId, cb) {
|
||||
if (!this.walletInfos[walletId])
|
||||
return cb(new Error('WNOEXIST: Wallet not on profile'));
|
||||
|
||||
delete this.walletInfos[walletId];
|
||||
|
|
@ -67,8 +67,8 @@ Profile.prototype.deleteWallet = function(walletId, cb) {
|
|||
}, cb);
|
||||
};
|
||||
|
||||
Profile.prototype.addToWallet = function(walletId, info, cb) {
|
||||
if (!this.walletInfos[walletId])
|
||||
Profile.prototype.addToWallet = function(walletId, info, cb) {
|
||||
if (!this.walletInfos[walletId])
|
||||
return cb(new Error('WNOEXIST: Wallet not on profile'));
|
||||
|
||||
this.walletInfos[walletId] = _.extend(this.walletInfos[walletId], info);
|
||||
|
|
@ -80,17 +80,27 @@ Profile.prototype.addToWallet = function(walletId, info, cb) {
|
|||
|
||||
|
||||
|
||||
Profile.prototype.addWallet = function(walletId, info, cb) {
|
||||
if (this.walletInfos[walletId])
|
||||
Profile.prototype.addWallet = function(walletId, info, cb) {
|
||||
if (this.walletInfos[walletId])
|
||||
return cb(new Error('WEXIST: Wallet already on profile'));
|
||||
|
||||
this.walletInfos[walletId] = _.extend(info, {createdTs: Date.now(), id: walletId} );
|
||||
this.walletInfos[walletId] = _.extend(info, {
|
||||
createdTs: Date.now(),
|
||||
id: walletId
|
||||
});
|
||||
|
||||
this.store({
|
||||
overwrite: true
|
||||
}, cb);
|
||||
};
|
||||
|
||||
|
||||
Profile.prototype.setLasOpenedTs = function(walletId, cb) {
|
||||
return this.addToWallet(walletId, {
|
||||
lastOpenedTs: Date.now()
|
||||
}, cb);
|
||||
};
|
||||
|
||||
Profile.prototype.store = function(opts, cb) {
|
||||
var self = this;
|
||||
var val = self.toObj();
|
||||
|
|
|
|||
|
|
@ -57,6 +57,9 @@ var copayConfig = require('../../config');
|
|||
*/
|
||||
function Wallet(opts) {
|
||||
var self = this;
|
||||
preconditions.checkArgument(opts);
|
||||
|
||||
opts.reconnectDelay = opts.reconnectDelay || 500;
|
||||
|
||||
//required params
|
||||
['storage', 'network', 'blockchain',
|
||||
|
|
@ -177,6 +180,25 @@ Wallet.getMaxRequiredCopayers = function(totalCopayers) {
|
|||
return Wallet.COPAYER_PAIR_LIMITS[totalCopayers];
|
||||
};
|
||||
|
||||
/**
|
||||
* delete
|
||||
*
|
||||
* @param walletId
|
||||
* @param storage
|
||||
* @param cb
|
||||
* @return {undefined}
|
||||
*/
|
||||
Wallet.delete = function(walletId, storage, cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
|
||||
storage.deletePrefix('wallet::' + walletId, function(err) {
|
||||
if (err) return cb(err);
|
||||
storage.deletePrefix(walletId + '::', function(err) {
|
||||
return cb(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @desc Set the copayer id for the owner of this wallet
|
||||
|
|
@ -874,7 +896,7 @@ Wallet.prototype.store = function(cb) {
|
|||
var self = this;
|
||||
this.keepAlive();
|
||||
|
||||
var val = this.toObj();
|
||||
var val = this.toObj();
|
||||
var key = 'wallet::' + this.id + ((val.opts && val.opts.name) ? '_' + obj.opts.name : '');
|
||||
this.storage.set(key, val, function(err) {
|
||||
log.debug('Wallet stored');
|
||||
|
|
@ -919,9 +941,23 @@ Wallet.prototype.toObj = function() {
|
|||
* @param {Storage} storage - a Storage instance to store the data of the wallet
|
||||
* @param {Network} network - a Network instance to communicate with peers
|
||||
* @param {Blockchain} blockchain - a Blockchain instance to retrieve state from the blockchain
|
||||
* @param skipFields
|
||||
*/
|
||||
Wallet.fromObj = function(o, storage, network, blockchain) {
|
||||
Wallet.fromObj = function(o, storage, network, blockchain, skipFields) {
|
||||
|
||||
|
||||
if (skipFields) {
|
||||
_.each(skipFields, function(k) {
|
||||
if (o[k]) {
|
||||
delete o[k];
|
||||
} else {
|
||||
throw new Error('unknown field:' + k);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// TODO Why moving everything to opts. This needs refactoring.
|
||||
|
||||
// clone opts
|
||||
var opts = JSON.parse(JSON.stringify(o.opts));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue