Added methods for storing/reading from sinlge key-value pair
This commit is contained in:
parent
0f37ea1929
commit
cd3f2b8b90
4 changed files with 231 additions and 12 deletions
|
|
@ -94,7 +94,6 @@ Storage.prototype.getGlobal = function(k, cb) {
|
|||
// set value for key
|
||||
Storage.prototype.setGlobal = function(k, v, cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
|
||||
this.storage.setItem(k, typeof v === 'object' ? JSON.stringify(v) : v, cb);
|
||||
};
|
||||
|
||||
|
|
@ -147,6 +146,7 @@ Storage.prototype.readWallet = function(walletId, cb) {
|
|||
var keys = _.filter(allKeys, function(k) {
|
||||
if (k.indexOf(walletId + '::') === 0) return true;
|
||||
});
|
||||
if (keys.length === 0) return cb(null);
|
||||
var count = keys.length;
|
||||
_.each(keys, function(k) {
|
||||
self._read(k, function(v) {
|
||||
|
|
@ -157,6 +157,19 @@ Storage.prototype.readWallet = function(walletId, cb) {
|
|||
});
|
||||
};
|
||||
|
||||
Storage.prototype.readWallet2 = function(walletId, cb) {
|
||||
var self = this;
|
||||
this.storage.allKeys(function(allKeys) {
|
||||
var keys = _.filter(allKeys, function(k) {
|
||||
if ((k === 'wallet::' + walletId) || k.indexOf('wallet::' + walletId) === 0) return true;
|
||||
});
|
||||
if (keys.length !== 1) throw new Error('WALLETNOTFOUND');
|
||||
self._read(keys[0], function(v) {
|
||||
return cb(v);
|
||||
})
|
||||
});
|
||||
};
|
||||
|
||||
Storage.prototype.getMany = function(walletId, keys, cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
|
||||
|
|
@ -256,6 +269,28 @@ Storage.prototype.getWallets = function(cb) {
|
|||
});
|
||||
};
|
||||
|
||||
Storage.prototype.getWallets2 = function(cb) {
|
||||
var self = this;
|
||||
var re = /wallet::([^_]+)(_?(.*))/;
|
||||
|
||||
this.storage.allKeys(function(allKeys) {
|
||||
var wallets = _.compact(_.map(allKeys, function(key) {
|
||||
if (key.indexOf('wallet::') !== 0)
|
||||
return null;
|
||||
var match = key.match(re);
|
||||
if (match.length != 4)
|
||||
return null;
|
||||
return {
|
||||
id: match[1],
|
||||
name: match[3] ? match[3] : undefined,
|
||||
};
|
||||
}));
|
||||
|
||||
return cb(wallets);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Storage.prototype.deleteWallet = function(walletId, cb) {
|
||||
preconditions.checkArgument(walletId);
|
||||
preconditions.checkArgument(cb);
|
||||
|
|
@ -288,17 +323,32 @@ Storage.prototype.deleteWallet = function(walletId, cb) {
|
|||
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Storage.prototype.deleteWallet2 = function(walletId, cb) {
|
||||
preconditions.checkArgument(walletId);
|
||||
preconditions.checkArgument(cb);
|
||||
|
||||
var self = this;
|
||||
this.getWallets2(function(wallets) {
|
||||
var w = _.findWhere(wallets, {
|
||||
id: walletId
|
||||
});
|
||||
if (!w)
|
||||
return cb(new Error('WNOTFOUND: Wallet not found'));
|
||||
self.removeGlobal('wallet::' + walletId + (w.name ? '_' + w.name : ''), function() {
|
||||
return cb();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Storage.prototype.setLastOpened = function(walletId, cb) {
|
||||
this.setGlobal('lastOpened', walletId, cb);
|
||||
}
|
||||
};
|
||||
|
||||
Storage.prototype.getLastOpened = function(cb) {
|
||||
this.getGlobal('lastOpened', cb);
|
||||
}
|
||||
};
|
||||
|
||||
//obj contains keys to be set
|
||||
Storage.prototype.setFromObj = function(walletId, obj, cb) {
|
||||
|
|
@ -319,6 +369,17 @@ Storage.prototype.setFromObj = function(walletId, obj, cb) {
|
|||
}
|
||||
};
|
||||
|
||||
Storage.prototype.setFromObj2 = function(walletId, obj, cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
var self = this;
|
||||
|
||||
var key = 'wallet::' + walletId + ((obj.opts && obj.opts.name) ? '_' + obj.opts.name : '');
|
||||
self._write(key, obj, function() {
|
||||
return cb();
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
// remove all values
|
||||
Storage.prototype.clearAll = function(cb) {
|
||||
this.storage.clear(cb);
|
||||
|
|
@ -334,4 +395,5 @@ Storage.prototype.export = function(obj) {
|
|||
return this._encrypt(string);
|
||||
};
|
||||
|
||||
|
||||
module.exports = Storage;
|
||||
|
|
|
|||
|
|
@ -142,6 +142,39 @@ WalletFactory.prototype.import = function(base64, passphrase, skipFields) {
|
|||
return w;
|
||||
};
|
||||
|
||||
WalletFactory.prototype.migrateWallet = function(walletId, passphrase, cb) {
|
||||
var self = this;
|
||||
|
||||
self.storage.setPassphrase(passphrase);
|
||||
self.storage.setFromObj = self.storage.setFromObj2;
|
||||
|
||||
var reconfigureStorage = function() {
|
||||
self.storage.getWallets = self.storage.getWallets2;
|
||||
self.storage.readWallet = self.storage.readWallet2;
|
||||
self.storage.deleteWallet = self.storage.deleteWallet2;
|
||||
self.storage.get = function() {
|
||||
throw 'DO NOT USE'
|
||||
};
|
||||
self.storage.set = function() {
|
||||
throw 'DO NOT USE'
|
||||
};
|
||||
};
|
||||
|
||||
self.read(walletId, null, function(err, wallet) {
|
||||
if (err) return cb(err);
|
||||
|
||||
wallet.store();
|
||||
self.storage.deleteWallet(walletId, function() {
|
||||
self.storage.removeGlobal('nameFor::' + walletId, function() {
|
||||
reconfigureStorage();
|
||||
return cb();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @desc Retrieve a wallet from storage
|
||||
* @param {string} walletId - the wallet id
|
||||
|
|
@ -153,7 +186,9 @@ WalletFactory.prototype.read = function(walletId, skipFields, cb) {
|
|||
err;
|
||||
var obj = {};
|
||||
|
||||
console.log('aáaa');
|
||||
this.storage.readWallet(walletId, function(ret) {
|
||||
console.log('bbb');
|
||||
_.each(Wallet.PERSISTED_PROPERTIES, function(p) {
|
||||
obj[p] = ret[p];
|
||||
});
|
||||
|
|
@ -301,12 +336,15 @@ WalletFactory.prototype.open = function(walletId, passphrase, cb) {
|
|||
preconditions.checkArgument(cb);
|
||||
var self = this;
|
||||
self.storage.setPassphrase(passphrase);
|
||||
self.read(walletId, null, function(err, w) {
|
||||
if (err) return cb(err);
|
||||
|
||||
w.store(function(err) {
|
||||
self.storage.setLastOpened(walletId, function() {
|
||||
return cb(err, w);
|
||||
self.migrateWallet(walletId, passphrase, function() {
|
||||
self.read(walletId, null, function(err, w) {
|
||||
if (err) return cb(err);
|
||||
|
||||
w.store(function(err) {
|
||||
self.storage.setLastOpened(walletId, function() {
|
||||
return cb(err, w);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue