wallet: refactor.

This commit is contained in:
Christopher Jeffrey 2016-10-04 04:19:29 -07:00
parent ee4014a12f
commit 3599ab5d9a
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -479,10 +479,10 @@ WalletDB.prototype._get = co(function* get(wid) {
wallet = Wallet.fromRaw(this, data);
this.register(wallet);
yield wallet.open();
this.register(wallet);
return wallet;
});
@ -818,7 +818,7 @@ WalletDB.prototype.savePath = co(function* savePath(wallet, path) {
var wid = wallet.wid;
var hash = path.hash;
var batch = this.batch(wallet);
var wallets;
var wallets, result;
if (this.filter)
this.filter.add(hash, 'hex');
@ -830,10 +830,11 @@ WalletDB.prototype.savePath = co(function* savePath(wallet, path) {
if (!wallets)
wallets = [];
if (wallets.indexOf(wid) !== -1)
return;
// Keep these motherfuckers sorted.
result = utils.binaryInsert(wallets, wid, cmp, true);
wallets.push(wid);
if (result === -1)
return;
this.pathMapCache.set(hash, wallets);
wallet.pathCache.set(hash, path);
@ -842,37 +843,6 @@ WalletDB.prototype.savePath = co(function* savePath(wallet, path) {
batch.put(layout.P(wid, hash), path.toRaw());
});
/**
* Retrieve paths by hash.
* @param {Hash} hash
* @returns {Promise}
*/
WalletDB.prototype.getPaths = co(function* getPaths(hash) {
var wallets = yield this.getWalletsByHash(hash);
var i, wid, path, paths, wallet;
if (!wallets)
return;
paths = [];
for (i = 0; i < wallets.length; i++) {
wid = wallets[i];
wallet = yield this.get(wid);
if (!wallet)
continue;
path = yield wallet.getPath(hash);
if (path)
paths.push(path);
}
return paths;
});
/**
* Retrieve path by hash.
* @param {WalletID} wid
@ -977,14 +947,20 @@ WalletDB.prototype.encryptKeys = co(function* encryptKeys(wallet, key) {
for (i = 0; i < paths.length; i++) {
path = paths[i];
if (path.data && !path.encrypted) {
iv = new Buffer(path.hash, 'hex');
iv = iv.slice(0, 16);
path.data = crypto.encipher(path.data, key, iv);
path.encrypted = true;
wallet.pathCache.set(path.hash, path);
batch.put(layout.P(wid, path.hash), path.toRaw());
}
if (!path.data)
continue;
assert(!path.encrypted);
iv = new Buffer(path.hash, 'hex');
iv = iv.slice(0, 16);
path.data = crypto.encipher(path.data, key, iv);
path.encrypted = true;
wallet.pathCache.set(path.hash, path);
batch.put(layout.P(wid, path.hash), path.toRaw());
}
});
@ -1003,14 +979,20 @@ WalletDB.prototype.decryptKeys = co(function* decryptKeys(wallet, key) {
for (i = 0; i < paths.length; i++) {
path = paths[i];
if (path.data && path.encrypted) {
iv = new Buffer(path.hash, 'hex');
iv = iv.slice(0, 16);
path.data = crypto.decipher(path.data, key, iv);
path.encrypted = false;
wallet.pathCache.set(path.hash, path);
batch.put(layout.P(wid, path.hash), path.toRaw());
}
if (!path.data)
continue;
assert(path.encrypted);
iv = new Buffer(path.hash, 'hex');
iv = iv.slice(0, 16);
path.data = crypto.decipher(path.data, key, iv);
path.encrypted = false;
wallet.pathCache.set(path.hash, path);
batch.put(layout.P(wid, path.hash), path.toRaw());
}
});
@ -1145,7 +1127,7 @@ WalletDB.prototype.getWidsByHashes = co(function* getWidsByHashes(hashes) {
wids = yield this.getWalletsByHash(hash);
for (j = 0; j < wids.length; j++)
utils.binaryInsert(result, wids[j], compare, true);
utils.binaryInsert(result, wids[j], cmp, true);
}
return result;
@ -1665,7 +1647,7 @@ function serializeWallets(wallets) {
return p.render();
}
function compare(a, b) {
function cmp(a, b) {
return a - b;
}