Skip to content

Commit

Permalink
wallet: add rpc methods for tx count and monotonic time
Browse files Browse the repository at this point in the history
  • Loading branch information
braydonf committed Jun 27, 2019
1 parent fe9f3f3 commit b5a9074
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
109 changes: 109 additions & 0 deletions lib/wallet/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
46 changes: 46 additions & 0 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b5a9074

Please sign in to comment.