Skip to content

fix(wasm-sdk): preserve user-supplied addresses in withTrustedContext - #3912

Merged
QuantumExplorer merged 2 commits into
dashpay:v3.1-devfrom
thepastaclaw:fix/wasm-trusted-custom-addresses
Jun 16, 2026
Merged

QuantumExplorer merged 2 commits into
dashpay:v3.1-devfrom
thepastaclaw:fix/wasm-trusted-custom-addresses

Conversation

@thepastaclaw

@thepastaclaw thepastaclaw commented Jun 15, 2026 •

Copy link
Copy Markdown
Collaborator

Issue being fixed or feature implemented

Fixes #3172.

`WasmSdkBuilder.withAddresses([...]).withTrustedContext(ctx)` was silently overwriting the caller's explicit address list with whatever masternode addresses the trusted context had discovered. That defeated the only knob users had for pointing the WASM SDK at a specific node (testing, private deployments, a sticky single-node sandbox).

What was done?

Introduce a `has_user_addresses: bool` flag on `WasmSdkBuilder`:

  • Set to `true` only by `withAddresses(...)`. Preset constructors (`mainnet`, `testnet`, `local`, `newDevnet`) leave it `false`, so they continue to adopt the context's discovered addresses.
  • Propagated through every chain method (`withProofs`, `withSettings`, `withVersion`, `withContextProvider`, `withLogs`, and `withTrustedContext` itself).
  • `withTrustedContext` now substitutes discovered addresses only when the flag is `false`; otherwise it attaches the context for proof verification and leaves the address list alone.

Cleanups bundled in a follow-up refactor commit: chain-method returns use `..self` struct-update syntax instead of copying `trusted_context` / `has_user_addresses` by hand at every site.

How Has This Been Tested?

New Rust unit tests in `packages/wasm-sdk/src/sdk.rs` (7 added, all green; existing 15 still green):

  • `with_addresses_sets_user_addresses_flag` / `preset_constructors_do_not_set_user_addresses_flag` — flag initial values.
  • `chain_methods_preserve_user_addresses_flag` — flag survives the full chain (`withProofs`, `withSettings`, `withVersion`, `withContextProvider`, `withLogs`).
  • `with_trusted_context_preserves_user_addresses` — direct repro of the bug. Builds the SDK with `withAddresses([...]) + withTrustedContext(ctx_with_discovered)` and asserts the post-build `Sdk::address_list()` contains the user's addresses and none of the discovered ones.
  • `with_trusted_context_replaces_preset_addresses` — anti-overcorrection guard: presets must still pick up discovered addresses.
  • `with_trusted_context_no_discovered_keeps_existing_addresses` — empty discovered list never wipes the user list.
  • `with_trusted_context_propagates_user_addresses_flag` — flag survives `withTrustedContext` so later chain steps still see it.

A `#[cfg(test)] WasmTrustedContext::for_testing(discovered)` helper builds a real-but-unused inner provider against `http://127.0.0.1:22444\` (Regtest skips the HTTPS guard; `verify_domain_resolves` only does `to_socket_addrs` on the IP literal — no DNS, no network dial).

Validation run locally:

  • `cargo test -p wasm-sdk --lib` → 22 passed, 0 failed
  • `cargo clippy -p wasm-sdk --lib --all-targets` → clean (no new warnings)
  • `cargo fmt -p wasm-sdk -- --check` → clean

A wasm32 build was attempted but failed in the local toolchain on `secp256k1-sys` (clang missing wasm sysroot configuration) — unrelated to this change; the host `cargo check` over the same Rust source is clean.

Breaking Changes

None. The public `WasmSdkBuilder` JS surface is unchanged; the new field is `#[wasm_bindgen]`-invisible. Preset + trusted-context callers see the same address-substitution behavior as before; only the previously broken `withAddresses + withTrustedContext` path changes — and only to honor what the user asked for.

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have added "!" to the title and described breaking changes in the corresponding section if my code contains any
  • I have made corresponding changes to the documentation if needed

Summary by CodeRabbit

  • Bug Fixes

    • Improved address handling in SDK builder—user-provided addresses are now correctly preserved when using trusted context.
  • Tests

    • Added comprehensive unit tests for address management, builder chaining, and trusted context integration scenarios.

WasmSdkBuilder.withAddresses([...]).withTrustedContext(ctx) used to
silently overwrite the caller's explicit address list with whatever
masternode addresses the trusted context discovered. That defeated
the only knob users had to point the SDK at a specific node (testing,
private deployments, a sticky single-node sandbox).

Track an explicit has_user_addresses flag on the builder, set only
by withAddresses(...). Preset constructors (mainnet/testnet/local/
newDevnet) leave it false so they continue to adopt the context's
discovered addresses. All chain methods (withProofs, withSettings,
withVersion, withContextProvider, withLogs, withTrustedContext
itself) propagate the flag.

Adds Rust unit tests covering the flag transitions and verifies via
the built Sdk's address list that user-supplied addresses survive
the trusted-context substitution while preset defaults are still
replaced. A #[cfg(test)] helper builds a stub WasmTrustedContext
with synthetic discovered addresses without touching the network.

Fixes dashpay#3172
- Use struct functional-update syntax (..self) in the five chain
  methods that return a new WasmSdkBuilder, dropping per-call
  field copies for trusted_context and has_user_addresses.
- Collapse the post-build address inspection in tests behind a
  sdk_addresses(&WasmSdk) helper.
- Cover withLogs propagation in the chain-flag test so the commit's
  claim that every chain method preserves the flag is exercised.

No behavior change; all 7 sdk::tests still pass.
@coderabbitai

coderabbitai Bot commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: db033683-8d6f-4b8a-ad59-6b5f09021c53

📥 Commits

Reviewing files that changed from the base of the PR and between 4f25567 and 424834b.

📒 Files selected for processing (2)
  • packages/wasm-sdk/src/context_provider.rs
  • packages/wasm-sdk/src/sdk.rs

📝 Walkthrough

Walkthrough

Adds a has_user_addresses: bool field to WasmSdkBuilder, set to true only by the explicit withAddresses constructor and false by all network presets. The withTrustedContext method now substitutes discovered addresses only when this flag is false. All builder chaining methods propagate the flag via struct update syntax. A WasmTrustedContext::for_testing helper and comprehensive unit tests are added.

Changes

WasmSdkBuilder address preservation fix

Layer / File(s) Summary
WasmSdkBuilder flag field and constructor initialization
packages/wasm-sdk/src/sdk.rs
Adds has_user_addresses: bool to WasmSdkBuilder; new_with_addresses sets it true, all four preset constructors (mainnet, testnet, newDevnet, local) set it false.
withTrustedContext address gating and builder chain propagation
packages/wasm-sdk/src/sdk.rs
withTrustedContext guards discovered-address substitution behind !has_user_addresses; withTrustedContext, with_context_provider, with_version, with_settings, and with_proofs all adopt struct update syntax (..self) to carry the flag through each chain step.
Test helper and unit tests
packages/wasm-sdk/src/context_provider.rs, packages/wasm-sdk/src/sdk.rs
WasmTrustedContext::for_testing builds a loopback-backed context for tests; unit tests assert flag initialization per constructor, flag survival through chained calls, address preservation for user-provided lists, address replacement for presets, no address wipe when discovered list is empty, and flag survival through withTrustedContext.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant WasmSdkBuilder
    participant WasmTrustedContext

    Caller->>WasmSdkBuilder: withAddresses(['my-node:1443'], 'testnet')<br/>(has_user_addresses = true)
    Caller->>WasmTrustedContext: prefetchTestnet() → discovered ~60 nodes
    Caller->>WasmSdkBuilder: withTrustedContext(context)
    WasmSdkBuilder->>WasmSdkBuilder: has_user_addresses == true → skip address replacement
    WasmSdkBuilder->>WasmSdkBuilder: attach context provider only
    WasmSdkBuilder-->>Caller: builder retains ['my-node:1443'] + trusted context
    Caller->>WasmSdkBuilder: build()
    WasmSdkBuilder-->>Caller: SDK connects to my-node:1443 with proof verification
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • shumkov
  • QuantumExplorer

Poem

🐇 Hoppity-hop, the addresses stay,
No sneaky context to whisk them away!
has_user_addresses stands firm and true,
Preset or custom — each gets its due.
The builder chain carries the flag all along,
Tests prove the fix, and nothing goes wrong! 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly describes the main fix: preserving user-supplied addresses in withTrustedContext, which is the core bug being addressed.
Linked Issues check ✅ Passed The PR fully implements the solution proposed in #3172: tracking user intent with has_user_addresses flag, preserving user-provided addresses in withTrustedContext, enabling custom nodes with proof verification.
Out of Scope Changes check ✅ Passed All changes are directly scoped to addressing the linked issue: adding for_testing helper, implementing address preservation logic, and comprehensive unit tests for the fix.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@thepastaclaw

thepastaclaw commented Jun 15, 2026 •

Copy link
Copy Markdown
Collaborator Author

✅ Review complete (commit 424834b)

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

Clean, surgical fix that adds a has_user_addresses provenance flag to WasmSdkBuilder so withTrustedContext no longer silently overwrites caller-supplied DAPI addresses. Verified the flag is correctly set (true only in new_with_addresses), propagated via ..self through every chain method (with_proofs, with_settings, with_version, with_context_provider, with_logs, with_trusted_context), and consulted at sdk.rs:320 to gate discovered-address substitution. Tests cover bug repro, preset-still-substitutes counterpart, empty discovered list, and flag propagation. No in-scope blocking, security, FFI, or correctness issues found.

GitHub does not allow PastaClaw to approve their own PR, so this clean review is posted as a COMMENT review rather than an APPROVE review.

No findings.

@QuantumExplorer
QuantumExplorer merged commit 36a67e6 into dashpay:v3.1-dev Jun 16, 2026
19 of 20 checks passed
@codecov

codecov Bot commented Jun 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.19%. Comparing base (e2039e5) to head (424834b).
⚠️ Report is 19 commits behind head on v3.1-dev.

Additional details and impacted files
@@             Coverage Diff              @@
##           v3.1-dev    #3912      +/-   ##
============================================
+ Coverage     71.20%   72.19%   +0.99%     
============================================
  Files            20       21       +1     
  Lines          2837     2946     +109     
============================================
+ Hits           2020     2127     +107     
- Misses          817      819       +2     
Components Coverage Δ
dpp ∅ <ø> (∅)
drive ∅ <ø> (∅)
drive-abci ∅ <ø> (∅)
sdk ∅ <ø> (∅)
dapi-client ∅ <ø> (∅)
platform-version ∅ <ø> (∅)
platform-value ∅ <ø> (∅)
platform-wallet ∅ <ø> (∅)
drive-proof-verifier ∅ <ø> (∅)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(wasm-sdk): Custom addresses overwritten by withTrustedContext(), preventing trusted mode with custom nodes

2 participants