Skip to content

feat: allow wallets on error state for stop API #476

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

Merged
merged 4 commits into from
Aug 26, 2024
Merged
Changes from 1 commit
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
29 changes: 26 additions & 3 deletions src/middlewares/wallet.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,20 @@

const friendlyWalletState = require('../helpers/constants');
const { initializedWallets, isHsmWallet, hsmWalletIds } = require('../services/wallets.service');
const settings = require('../settings');

Check failure on line 10 in src/middlewares/wallet.middleware.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Expected 1 empty line after require statement not followed by another require
import { HathorWallet } from '@hathor/wallet-lib';

Check failure on line 11 in src/middlewares/wallet.middleware.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Import in body of module; reorder to top

Check failure on line 11 in src/middlewares/wallet.middleware.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Cannot use import declarations in modules that export using CommonJS (module.exports = 'foo' or exports.bar = 'hi')

Check failure on line 11 in src/middlewares/wallet.middleware.js

View workflow job for this annotation

GitHub Actions / test (20.x)

`@hathor/wallet-lib` import should occur before import of `../helpers/constants`
import {Request, Response, NextFunction} from 'express';

Check failure on line 12 in src/middlewares/wallet.middleware.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Import in body of module; reorder to top

Check failure on line 12 in src/middlewares/wallet.middleware.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Cannot use import declarations in modules that export using CommonJS (module.exports = 'foo' or exports.bar = 'hi')

Check failure on line 12 in src/middlewares/wallet.middleware.js

View workflow job for this annotation

GitHub Actions / test (20.x)

`express` import should occur before import of `../helpers/constants`

Check failure on line 12 in src/middlewares/wallet.middleware.js

View workflow job for this annotation

GitHub Actions / test (20.x)

A space is required after '{'

Check warning on line 12 in src/middlewares/wallet.middleware.js

View workflow job for this annotation

GitHub Actions / test (20.x)

'Request' is defined but never used

Check warning on line 12 in src/middlewares/wallet.middleware.js

View workflow job for this annotation

GitHub Actions / test (20.x)

'Response' is defined but never used

Check warning on line 12 in src/middlewares/wallet.middleware.js

View workflow job for this annotation

GitHub Actions / test (20.x)

'NextFunction' is defined but never used

Check failure on line 12 in src/middlewares/wallet.middleware.js

View workflow job for this annotation

GitHub Actions / test (20.x)

A space is required before '}'

/**
* Extracts wallet from initializedWallets based on the X-Wallet-ID header and
* validates the call, if valid, injects the wallet instance in the request
* instance and proceed with the call stack.
*
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*
*/
async function walletMiddleware(req, res, next) {
const sendError = (message, state) => {
res.send({
Expand Down Expand Up @@ -43,11 +55,22 @@
}
}

if (!wallet.isReady()) {
sendError('Wallet is not ready.', wallet.state);
return;
// Does not require `/wallet` prefix since this is being routed in the walletRouter.
// Matches `/stop` and `/stop/` since both are valid.
if (/^\/stop\/?$/.test(req.path)) {
// A special case for `/wallet/stop` is when
if (!(wallet.isReady() || wallet.state === HathorWallet.ERROR)) {
sendError('Wallet needs to be ready or unrecoverable to be stopped.', wallet.state);
return;
}
} else {
if (!wallet.isReady()) {

Check failure on line 67 in src/middlewares/wallet.middleware.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Unexpected if as the only statement in an else block
sendError('Wallet is not ready.', wallet.state);
return;
}
}


// Adding to req parameter, so we don't need to get it in all requests
req.wallet = wallet;
req.walletId = walletId;
Expand Down