Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wallet-http: Return total number of transactions zapped. #920

Merged
merged 3 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
.eslint*
.git*
.mocharc*
.nyc_output/
.yarnignore
bench/
browser/hsd*
build/
coverage/
docker_data/
docs/
eslint.config.cjs
node_modules/
npm-debug.log
package-lock.json
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# HSD Release Notes & Changelog

## Unreleased

### Wallet Changes

#### Wallet HTTP API
- `POST /wallet/:id/zap` returned object has a new property: `zapped: number`,
indicating the number of transactions that were zapped.

#### Wallet/WalletDB API
- Wallet.zap now returns number of transactions zapped instead of hashes.


## v7.0.0

**When upgrading to this version of hsd, you must pass `--wallet-migrate=5` when
Expand Down
75 changes: 42 additions & 33 deletions lib/wallet/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const HDPublicKey = require('../hd/public');
const {Resource} = require('../dns/resource');
const common = require('./common');

/** @typedef {import('../types').NetworkType} NetworkType */
/** @typedef {ReturnType<Validator['fromRequest']>} RequestValidator */

/**
* HTTP
* @alias module:wallet.HTTP
Expand Down Expand Up @@ -459,7 +462,7 @@ class HTTP extends Server {
this.post('/wallet/:id/send', async (req, res) => {
const valid = Validator.fromRequest(req);

const options = TransactionOptions.fromValidator(valid);
const options = TransactionOptions.fromValidator(valid, this.network);
const tx = await req.wallet.send(options);

const details = await req.wallet.getDetails(tx.hash());
Expand All @@ -474,7 +477,7 @@ class HTTP extends Server {

// TODO: Add create TX with locks for used Coins and/or
// adds to the pending list.
const options = TransactionOptions.fromValidator(valid);
const options = TransactionOptions.fromValidator(valid, this.network);
const tx = await req.wallet.createTX(options);

if (sign)
Expand All @@ -496,12 +499,12 @@ class HTTP extends Server {

enforce(raw, 'TX is required.');

const tx = MTX.decode(raw);
tx.view = await req.wallet.getCoinView(tx);
const mtx = MTX.decode(raw);
mtx.view = await req.wallet.getCoinView(mtx);

await req.wallet.sign(tx, passphrase);
await req.wallet.sign(mtx, passphrase);

res.json(200, tx.getJSON(this.network));
res.json(200, mtx.getJSON(this.network));
});

// Zap Wallet TXs
Expand All @@ -510,11 +513,14 @@ class HTTP extends Server {
const acct = valid.str('account');
const age = valid.u32('age');

enforce(age, 'Age is required.');
enforce(age != null, 'Age is required.');

await req.wallet.zap(acct, age);
const total = await req.wallet.zap(acct, age);

res.json(200, { success: true });
res.json(200, {
success: true,
zapped: total
});
});

// Abandon Wallet TX
Expand Down Expand Up @@ -1072,7 +1078,7 @@ class HTTP extends Server {
enforce(name, 'Name is required.');
enforce(broadcast ? sign : true, 'Must sign when broadcasting.');

const options = TransactionOptions.fromValidator(valid);
const options = TransactionOptions.fromValidator(valid, this.network);

if (broadcast) {
// TODO: Add abort signal to close when request closes.
Expand Down Expand Up @@ -1109,7 +1115,7 @@ class HTTP extends Server {
enforce(lockup != null, 'Lockup is required.');
enforce(broadcast ? sign : true, 'Must sign when broadcasting.');

const options = TransactionOptions.fromValidator(valid);
const options = TransactionOptions.fromValidator(valid, this.network);

if (broadcast) {
// TODO: Add abort signal to close when request closes.
Expand Down Expand Up @@ -1148,7 +1154,7 @@ class HTTP extends Server {
enforce(broadcastBid != null, 'broadcastBid is required.');
enforce(broadcastBid ? sign : true, 'Must sign when broadcasting.');

const options = TransactionOptions.fromValidator(valid);
const options = TransactionOptions.fromValidator(valid, this.network);
const auctionTXs = await req.wallet.createAuctionTXs(
name,
bid,
Expand Down Expand Up @@ -1189,7 +1195,7 @@ class HTTP extends Server {

enforce(broadcast ? sign : true, 'Must sign when broadcasting.');

const options = TransactionOptions.fromValidator(valid);
const options = TransactionOptions.fromValidator(valid, this.network);

if (broadcast) {
let tx;
Expand Down Expand Up @@ -1235,7 +1241,7 @@ class HTTP extends Server {

enforce(broadcast ? sign : true, 'Must sign when broadcasting.');

const options = TransactionOptions.fromValidator(valid);
const options = TransactionOptions.fromValidator(valid, this.network);

if (broadcast) {
let tx;
Expand Down Expand Up @@ -1291,7 +1297,7 @@ class HTTP extends Server {
return res.json(400);
}

const options = TransactionOptions.fromValidator(valid);
const options = TransactionOptions.fromValidator(valid, this.network);

if (broadcast) {
// TODO: Add abort signal to close when request closes.
Expand Down Expand Up @@ -1324,7 +1330,7 @@ class HTTP extends Server {
enforce(broadcast ? sign : true, 'Must sign when broadcasting.');
enforce(name, 'Must pass name.');

const options = TransactionOptions.fromValidator(valid);
const options = TransactionOptions.fromValidator(valid, this.network);

if (broadcast) {
// TODO: Add abort signal to close when request closes.
Expand Down Expand Up @@ -1358,7 +1364,7 @@ class HTTP extends Server {
enforce(address, 'Must pass address.');

const addr = Address.fromString(address, this.network);
const options = TransactionOptions.fromValidator(valid);
const options = TransactionOptions.fromValidator(valid, this.network);

if (broadcast) {
// TODO: Add abort signal to close when request closes.
Expand Down Expand Up @@ -1391,7 +1397,7 @@ class HTTP extends Server {
enforce(broadcast ? sign : true, 'Must sign when broadcasting.');
enforce(name, 'Must pass name.');

const options = TransactionOptions.fromValidator(valid);
const options = TransactionOptions.fromValidator(valid, this.network);

if (broadcast) {
// TODO: Add abort signal to close when request closes.
Expand Down Expand Up @@ -1424,7 +1430,7 @@ class HTTP extends Server {
enforce(broadcast ? sign : true, 'Must sign when broadcasting.');
enforce(name, 'Must pass name.');

const options = TransactionOptions.fromValidator(valid);
const options = TransactionOptions.fromValidator(valid, this.network);

if (broadcast) {
// TODO: Add abort signal to close when request closes.
Expand Down Expand Up @@ -1455,7 +1461,7 @@ class HTTP extends Server {
enforce(broadcast ? sign : true, 'Must sign when broadcasting.');
enforce(name, 'Must pass name.');

const options = TransactionOptions.fromValidator(valid);
const options = TransactionOptions.fromValidator(valid, this.network);

if (broadcast) {
// TODO: Add abort signal to close when request closes.
Expand Down Expand Up @@ -1571,7 +1577,6 @@ class HTTP extends Server {

/**
* Handle new websocket.
* @private
* @param {WebSocket} socket
*/

Expand Down Expand Up @@ -1823,22 +1828,24 @@ class TransactionOptions {
* TransactionOptions
* @alias module:http.TransactionOptions
* @constructor
* @param {Validator} valid
* @param {RequestValidator} [valid]
* @param {(NetworkType|Network)?} [network]
*/

constructor(valid) {
constructor(valid, network) {
if (valid)
return this.fromValidator(valid);
return this.fromValidator(valid, network);
}

/**
* Inject properties from Validator.
* @private
* @param {Validator} valid
* @param {RequestValidator} valid
* @param {(NetworkType|Network)?} [network]
* @returns {TransactionOptions}
*/

fromValidator(valid) {
fromValidator(valid, network) {
assert(valid);

this.rate = valid.u64('rate');
Expand All @@ -1863,10 +1870,11 @@ class TransactionOptions {
for (const output of outputs) {
const valid = new Validator(output);

let addr = valid.str('address');
const addrstr = valid.str('address');
let addr;

if (addr)
addr = Address.fromString(addr, this.network);
if (addrstr)
addr = Address.fromString(addrstr, network);

let covenant = valid.obj('covenant');

Expand All @@ -1884,15 +1892,16 @@ class TransactionOptions {
return this;
}

/*
/**
* Instantiate transaction options
* from Validator.
* @param {Validator} valid
* @param {RequestValidator} [valid]
* @param {(NetworkType|Network)?} [network]
* @returns {TransactionOptions}
*/

static fromValidator(valid) {
return new this().fromValidator(valid);
static fromValidator(valid, network) {
return new this().fromValidator(valid, network);
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/wallet/txdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -4028,7 +4028,7 @@ class TXDB {
* Zap pending transactions older than `age`.
* @param {Number} acct
* @param {Number} age - Age delta.
* @returns {Promise<Hash[]>} - zapped tx hashes.
* @returns {Promise<Number>} - zapped tx hashes.
*/

async zap(acct, age) {
Expand All @@ -4043,7 +4043,7 @@ class TXDB {

let txs = await this.listUnconfirmedByTime(acct, options);

const hashes = [];
let zapped = 0;

while (txs.length) {
for (const wtx of txs) {
Expand All @@ -4052,13 +4052,13 @@ class TXDB {

await this.remove(wtx.hash);

hashes.push(wtx.hash);
zapped++;
}

txs = await this.listUnconfirmedByTime(acct, options);
}

return hashes;
return zapped;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -5032,7 +5032,7 @@ class Wallet extends EventEmitter {
* Zap stale TXs from wallet.
* @param {(Number|String)?} acct
* @param {Number} age - Age threshold (unix time, default=72 hours).
* @returns {Promise<Hash[]>}
* @returns {Promise<Number>}
*/

async zap(acct, age) {
Expand All @@ -5049,7 +5049,7 @@ class Wallet extends EventEmitter {
* @private
* @param {(Number|String)?} acct
* @param {Number} age
* @returns {Promise<Hash[]>}
* @returns {Promise<Number>}
*/

async _zap(acct, age) {
Expand Down
Loading
Loading