From b5a9074515fbc514a48f7b43afb6a55b80170865 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 10 Sep 2018 17:11:57 -0700 Subject: [PATCH] wallet: add rpc methods for tx count and monotonic time --- lib/wallet/rpc.js | 109 +++++++++++++++++++++++++++++++++++++++++++ lib/wallet/wallet.js | 46 ++++++++++++++++++ 2 files changed, 155 insertions(+) diff --git a/lib/wallet/rpc.js b/lib/wallet/rpc.js index a0b8fda7c..faed377ac 100644 --- a/lib/wallet/rpc.js +++ b/lib/wallet/rpc.js @@ -141,6 +141,7 @@ class RPC extends RPCBase { this.add('gettransaction', this.getTransaction); this.add('getunconfirmedbalance', this.getUnconfirmedBalance); this.add('getwalletinfo', this.getWalletInfo); + this.add('getblocksbytime', this.getBlocksByTime); this.add('importprivkey', this.importPrivKey); this.add('importwallet', this.importWallet); this.add('importaddress', this.importAddress); @@ -154,6 +155,10 @@ class RPC extends RPCBase { this.add('listreceivedbyaddress', this.listReceivedByAddress); this.add('listsinceblock', this.listSinceBlock); this.add('listtransactions', this.listTransactions); + this.add('listhistorycount', this.listHistoryCount); + this.add('listhistory', this.listHistory); + this.add('listhistoryafter', this.listHistoryAfter); + this.add('listhistorybytime', this.listHistoryByTime); this.add('listunspent', this.listUnspent); this.add('lockunspent', this.lockUnspent); this.add('move', this.move); @@ -755,6 +760,20 @@ class RPC extends RPCBase { }; } + async getBlocksByTime(args, help) { + if (help || args.length < 1 || args.length > 2) + throw new RPCError(errs.MISC_ERROR, 'getblocksbytime "time"'); + + const valid = new Validator(args); + const time = valid.u32(0); + const limit = valid.u32(1, 10); + + if (limit > 1000) + throw new RPCError(errs.INVALID_PARAMETER, 'Limit above max of 1000.'); + + return await this.wdb.getBlocksByTime(time, limit, 'hex'); + } + async importPrivKey(args, help) { if (help || args.length < 1 || args.length > 3) { throw new RPCError(errs.MISC_ERROR, @@ -1236,6 +1255,96 @@ class RPC extends RPCBase { return out; } + async listHistoryCount(args, help) { + if (help || args.length > 2) { + throw new RPCError(errs.MISC_ERROR, + 'listhistorycount ( "account" )'); + } + const wallet = this.wallet; + const valid = new Validator(args); + const name = valid.str(0, 'default'); + + return await wallet.getTXCount(name); + } + + async listHistory(args, help) { + if (help || args.length > 4) { + throw new RPCError(errs.MISC_ERROR, + 'listhistory "account" ( limit, reverse )'); + } + + const wallet = this.wallet; + const valid = new Validator(args); + const name = valid.str(0); + const limit = valid.u32(1, 10); + const reverse = valid.bool(2, false); + + if (limit > 100) + throw new RPCError(INVALID_PARAMETER, 'Limit above max of 100.'); + + const txrecords = await wallet.listHistory(name, limit, reverse); + + const out = []; + for (let i = 0; i < txrecords.length; i++) + out.push(await this._toListTX(txrecords[i])); + + return out; + } + + async listHistoryAfter(args, help) { + if (help || args.length > 4) { + throw new RPCError(errs.MISC_ERROR, + 'listhistory "account", "txid" ( limit, reverse )'); + } + + const wallet = this.wallet; + const valid = new Validator(args); + const name = valid.str(0); + const txid = valid.buf(1); + const limit = valid.u32(2, 10); + const reverse = valid.bool(3, false); + + if (limit > 100) + throw new RPCError(INVALID_PARAMETER, 'Limit above max of 100.'); + + const txrecords = await wallet.listHistoryAfter(name, txid.reverse(), limit, reverse); + + const out = []; + for (let i = 0; i < txrecords.length; i++) + out.push(await this._toListTX(txrecords[i])); + + return out; + } + + async listHistoryByTime(args, help) { + if (help || args.length > 4) { + throw new RPCError(errs.MISC_ERROR, + 'listhistory "account", "timestamp" ( limit, reverse )'); + } + + const wallet = this.wallet; + const valid = new Validator(args); + const name = valid.str(0); + const timestamp = valid.str(1); + const limit = valid.u32(2, 10); + const reverse = valid.bool(3, false); + + if (limit > 100) + throw new RPCError(INVALID_PARAMETER, 'Limit above max of 100.'); + + const time = new Date(timestamp); + if (time == 'Invalid Date') + throw new RPCError(INVALID_PARAMETER, 'Invalid timestamp.'); + + const txrecords = wallet.listHistoryByTime(name, time, limit, reverse); + + const out = []; + for (let i = 0; i < txrecords.length; i++) + out.push(await this._toListTX(txrecords[i])); + + return out; + } + async listUnspent(args, help) { if (help || args.length > 3) { throw new RPCError(errs.MISC_ERROR, diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index b42469535..af895c6ca 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -1982,6 +1982,52 @@ class Wallet extends EventEmitter { return this.txdb.getLocked(); } + /** + * Get the count of all transactions in wallet + * and account history. + * @param {(String|Number)?} acct + * @returns {Promise} - Returns Number. + */ + async getTXCount(acct) { + const account = await this.ensureIndex(acct); + return this.txdb.getTXCount(account); + } + + /** + * @param {(String|Number)?} acct + * @param {Number?} limit + * @param {Boolean?} reverse + * @returns {Promise} - Returns Number. + */ + async listHistory(acct, limit, reverse) { + const account = await this.ensureIndex(acct); + return this.txdb.listHistory(account, limit, reverse); + } + + /** + * @param {(String|Number)} acct + * @param {String} txid + * @param {Number?} limit + * @param {Boolean?} reverse + * @returns {Promise} - Returns Number. + */ + async listHistoryAfter(acct, txid, limit, reverse) { + const account = await this.ensureIndex(acct); + return this.txdb.listHistoryAfter(account, txid, limit, reverse); + } + + /** + * @param {(String|Number)} acct + * @param {Date} time + * @param {Number?} limit + * @param {Boolean?} reverse + * @returns {Promise} - Returns Number. + */ + async listHistoryByTime(acct, time, reverse) { + const account = await this.ensureIndex(acct); + return this.txdb.listHistoryByTime(account, time, limit, reverse); + } + /** * Get all transactions in transaction history. * @param {(String|Number)?} acct