Skip to content

Audit gRPC-created requests, wallet transfers and user roles - #666

Merged
RodriFS merged 2 commits into
mainfrom
outflow-monitor-audit-gaps
Nov 12, 2026
Merged

RodriFS merged 2 commits into
mainfrom
outflow-monitor-audit-gaps

Conversation

@RodriFS

@RodriFS RodriFS commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Each request that sends funds must have an audit entry that shows who created it. Today, gRPC requests have no audit entries, and transfer audits point to the wallet, not to the request. This PR adds those audit entries, identifies the API token or the user and their roles, and keeps each entry consistent with the request state.

Changes

gRPC caller identity

  • The auth interceptor puts the token id and name in HttpContext.Items. It never stores the token value.
  • AuditService reads it on each audit. The username becomes API token {id} ({name}), and the details get ApiTokenId and ApiTokenName.
  • API audits now record the caller IP.

gRPC request audits (RequestWithdrawal, BumpWithdrawal, CancelWithdrawal, OpenChannel)

  • A Success entry is written when the request is ready, before its job is scheduled. Thus, the creation comes before the job's own audits.
  • If a saved request fails, the request is cancelled or set to Failed, and a Failure entry records the error. This also releases its locked UTXOs.
  • If a request is refused before it is saved, a Failure entry is written without an object id.
  • After the job is scheduled, the request stays active and the call returns success. A client retry thus cannot open a second channel.
  • The failure cleanup never throws, so the client always gets the original error.

RBF cancellations

  • A new AuditActionType.RbfCancel replaces Cancel for RBF cancellations, in the API and in the UI. Cancel now only means a cancelled request.

Wallet transfers

  • Transfer audits now use the withdrawal request as the object. The details keep the wallet id as SourceWalletId.

User roles

  • Audits from a logged-in user add their roles as UserRoles, so a later check can use the roles at the time of the action.
  • Audits with no details stay null. Details that are not a JSON object go under Details. Keys from the caller are never replaced.

Tradeoffs and scope

  • If scheduling a job fails, the same request gets a Success entry and then a Failure entry. We accept this so the creation always comes before the job's audits.
  • Existing audit rows do not change. Old RBF cancellations stay as Cancel, and old transfers stay on the wallet.
  • UserRoles comes from the login claims. It can be out of date until the security stamp is validated again.

Validation

  • dotnet build passes.
  • dotnet test: 824 passed, 17 skipped, 0 failed.
  • New tests cover the token identity, the role and detail rules, the audit order, failures after scheduling, and a failing cleanup.
  • No manual test against a running node.

Review order

  1. Helpers/AuthenticatedApiToken.cs and GRPCAuthInterceptor: how the caller identity enters the request.
  2. AuditService: how each audit gets the token or the user roles, and the rules for the details.
  3. AuditLog.cs: the new RbfCancel action.
  4. NodeGuardService: start with FailRequestAndAuditAsync. Then read RequestWithdrawal, ReplaceWithdrawalAsync, and OpenChannel for the order of audit, scheduling, and failure.
  5. Withdrawals.razor, AuditTrail.razor, and TransferFundsModal.razor: the UI uses the same actions and object ids.
  6. Tests, in the same order.

@RodriFS
RodriFS requested a review from Jossec101 November 12, 2026 07:46
Jossec101 added a commit that referenced this pull request Nov 12, 2026
…668)

* feat: SubscribeWalletEvents streams a wallet's withdrawal events over gRPC

- New bidirectional RPC: the first client message subscribes to a wallet; the server answers
  WALLET_EVENT_SUBSCRIBED, then one WalletEvent per withdrawal change (created, signature added, signing,
  broadcast, confirmed, bumped, cancelled, rejected, failed, replacement reverted), with RBF links.
- In-process WalletEventHub (live only, bounded per subscriber; a slow subscriber is dropped with
  RESOURCE_EXHAUSTED, shutdown ends calls with UNAVAILABLE).
- The withdrawal repositories publish after each successful save, diffing status/TxId against the stored
  row only when someone subscribes to the wallet.
- GRPCAuthInterceptor now authenticates streaming calls too (previously unary only) and exposes the token.
- Kestrel HTTP/2 keepalive pings so idle streams survive proxies.

* refactor: let the withdrawal event publisher decide when to read a request's state before an update

- WalletWithdrawalRequestRepository.Update no longer checks for subscribers: it always calls
  ReadBeforeUpdate before the write and PublishUpdated after a successful one.
- The publisher reads the stored status/TxId only when someone subscribes to the wallet and the request
  was saved; it returns null on a failed read and never throws.
- IsWatched is now private to the publisher.

* fix: keep only the API token's id and name on an authenticated gRPC call

- GRPCAuthInterceptor stores an AuthenticatedApiToken(Id, Name) instead of the APIToken row, so the token
  value never travels down the call.
- Same record, file and key as #666 (HttpContext.Items "NodeGuard.ApiToken"; UserState is HttpContext.Items
  under ASP.NET Core), so AuditService identifies the token on streaming calls once #666 merges.
- GetApiToken keeps its name and now returns AuthenticatedApiToken.
Comment thread src/Shared/TransferFundsModal.razor Resolved
Comment thread src/Shared/TransferFundsModal.razor Resolved
Comment thread src/Services/AuditService.cs Resolved
Comment thread src/Services/AuditService.cs Resolved
@RodriFS

RodriFS commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

@Jossec101 btw, your review found problems that were introduced before this PR. I fixed them anyway because they were easy wins

Jossec101
Jossec101 previously approved these changes Nov 12, 2026
@RodriFS

RodriFS commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

sorry @Jossec101, i had to rebase because of merge conflicts

@RodriFS
RodriFS enabled auto-merge (squash) November 12, 2026 15:00
@RodriFS
RodriFS merged commit 9afdf5b into main Nov 12, 2026
6 checks passed
@RodriFS
RodriFS deleted the outflow-monitor-audit-gaps branch November 12, 2026 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants