wallet: refactor.
This commit is contained in:
parent
aa3988aa2f
commit
30597b83f6
@ -420,7 +420,7 @@ class WalletDB extends EventEmitter {
|
||||
height -= 1;
|
||||
}
|
||||
|
||||
await this.scan(height);
|
||||
return this.scan(height);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -800,7 +800,11 @@ class WalletDB extends EventEmitter {
|
||||
|
||||
this.save(b, wallet);
|
||||
|
||||
wallet.id = old;
|
||||
|
||||
await b.write();
|
||||
|
||||
wallet.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1067,7 +1071,7 @@ class WalletDB extends EventEmitter {
|
||||
* @returns {Promise} - Returns Array.
|
||||
*/
|
||||
|
||||
getAccounts(wid) {
|
||||
async getAccounts(wid) {
|
||||
return this.db.values({
|
||||
gte: layout.n.min(wid),
|
||||
lte: layout.n.max(wid),
|
||||
@ -1228,7 +1232,7 @@ class WalletDB extends EventEmitter {
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
getHashes() {
|
||||
async getHashes() {
|
||||
return this.db.keys({
|
||||
gte: layout.p.min(),
|
||||
lte: layout.p.max(),
|
||||
@ -1241,7 +1245,7 @@ class WalletDB extends EventEmitter {
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
getOutpoints() {
|
||||
async getOutpoints() {
|
||||
return this.db.keys({
|
||||
gte: layout.o.min(),
|
||||
lte: layout.o.max(),
|
||||
@ -1258,7 +1262,7 @@ class WalletDB extends EventEmitter {
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
getWalletHashes(wid) {
|
||||
async getWalletHashes(wid) {
|
||||
return this.db.keys({
|
||||
gte: layout.P.min(wid),
|
||||
lte: layout.P.max(wid),
|
||||
@ -1273,7 +1277,7 @@ class WalletDB extends EventEmitter {
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
getAccountHashes(wid, account) {
|
||||
async getAccountHashes(wid, account) {
|
||||
return this.db.keys({
|
||||
gte: layout.r.min(wid, account),
|
||||
lte: layout.r.max(wid, account),
|
||||
@ -1295,9 +1299,9 @@ class WalletDB extends EventEmitter {
|
||||
|
||||
const paths = [];
|
||||
|
||||
for (const item of items) {
|
||||
const [, hash] = layout.P.parse(item.key);
|
||||
const path = Path.fromRaw(item.value);
|
||||
for (const {key, value} of items) {
|
||||
const [, hash] = layout.P.parse(key);
|
||||
const path = Path.fromRaw(value);
|
||||
|
||||
path.hash = hash;
|
||||
path.name = await this.getAccountName(wid, path.account);
|
||||
@ -1314,7 +1318,7 @@ class WalletDB extends EventEmitter {
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
getWallets() {
|
||||
async getWallets() {
|
||||
return this.db.keys({
|
||||
gte: layout.l.min(),
|
||||
lte: layout.l.max(),
|
||||
@ -1458,7 +1462,7 @@ class WalletDB extends EventEmitter {
|
||||
*/
|
||||
|
||||
async getWalletsByTX(tx) {
|
||||
const result = new Set();
|
||||
const wids = new Set();
|
||||
|
||||
if (!tx.isCoinbase()) {
|
||||
for (const {prevout} of tx.inputs) {
|
||||
@ -1473,7 +1477,7 @@ class WalletDB extends EventEmitter {
|
||||
continue;
|
||||
|
||||
for (const wid of map.wids)
|
||||
result.add(wid);
|
||||
wids.add(wid);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1489,13 +1493,13 @@ class WalletDB extends EventEmitter {
|
||||
continue;
|
||||
|
||||
for (const wid of map.wids)
|
||||
result.add(wid);
|
||||
wids.add(wid);
|
||||
}
|
||||
|
||||
if (result.size === 0)
|
||||
if (wids.size === 0)
|
||||
return null;
|
||||
|
||||
return result;
|
||||
return wids;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1644,7 +1648,7 @@ class WalletDB extends EventEmitter {
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
getPathMap(hash) {
|
||||
async getPathMap(hash) {
|
||||
return this.getMap(layout.p.build(hash));
|
||||
}
|
||||
|
||||
@ -1667,7 +1671,7 @@ class WalletDB extends EventEmitter {
|
||||
* @param {Number} wid
|
||||
*/
|
||||
|
||||
removePathMap(b, hash, wid) {
|
||||
async removePathMap(b, hash, wid) {
|
||||
return this.removeMap(b, layout.p.build(hash), wid);
|
||||
}
|
||||
|
||||
@ -1688,7 +1692,7 @@ class WalletDB extends EventEmitter {
|
||||
* @param {Number} wid
|
||||
*/
|
||||
|
||||
addBlockMap(b, height, wid) {
|
||||
async addBlockMap(b, height, wid) {
|
||||
return this.addMap(b, layout.b.build(height), wid);
|
||||
}
|
||||
|
||||
@ -1699,7 +1703,7 @@ class WalletDB extends EventEmitter {
|
||||
* @param {Number} wid
|
||||
*/
|
||||
|
||||
removeBlockMap(b, height, wid) {
|
||||
async removeBlockMap(b, height, wid) {
|
||||
return this.removeMap(b, layout.b.build(height), wid);
|
||||
}
|
||||
|
||||
@ -1709,7 +1713,7 @@ class WalletDB extends EventEmitter {
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
getTXMap(hash) {
|
||||
async getTXMap(hash) {
|
||||
return this.getMap(layout.T.build(hash));
|
||||
}
|
||||
|
||||
@ -1720,7 +1724,7 @@ class WalletDB extends EventEmitter {
|
||||
* @param {Number} wid
|
||||
*/
|
||||
|
||||
addTXMap(b, hash, wid) {
|
||||
async addTXMap(b, hash, wid) {
|
||||
return this.addMap(b, layout.T.build(hash), wid);
|
||||
}
|
||||
|
||||
@ -1731,7 +1735,7 @@ class WalletDB extends EventEmitter {
|
||||
* @param {Number} wid
|
||||
*/
|
||||
|
||||
removeTXMap(b, hash, wid) {
|
||||
async removeTXMap(b, hash, wid) {
|
||||
return this.removeMap(b, layout.T.build(hash), wid);
|
||||
}
|
||||
|
||||
@ -1741,7 +1745,7 @@ class WalletDB extends EventEmitter {
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
getOutpointMap(hash, index) {
|
||||
async getOutpointMap(hash, index) {
|
||||
return this.getMap(layout.o.build(hash, index));
|
||||
}
|
||||
|
||||
@ -1764,7 +1768,7 @@ class WalletDB extends EventEmitter {
|
||||
* @param {Number} wid
|
||||
*/
|
||||
|
||||
removeOutpointMap(b, hash, index, wid) {
|
||||
async removeOutpointMap(b, hash, index, wid) {
|
||||
return this.removeMap(b, layout.o.build(hash, index), wid);
|
||||
}
|
||||
|
||||
|
||||
@ -1498,6 +1498,15 @@ describe('Wallet', function() {
|
||||
assert.strictEqual((await bob.getBalance()).unconfirmed, 30000);
|
||||
});
|
||||
|
||||
it('should remove a wallet', async () => {
|
||||
const wallet = await wdb.create({
|
||||
id: 'alice100'
|
||||
});
|
||||
assert(await wdb.get('alice100'));
|
||||
await wdb.remove('alice100');
|
||||
assert(!await wdb.get('alice100'));
|
||||
});
|
||||
|
||||
it('should cleanup', () => {
|
||||
consensus.COINBASE_MATURITY = 100;
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user