-
Notifications
You must be signed in to change notification settings - Fork 13
fix(react): keep page-side asset-metadata & bech32 on the configured network #189
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
Open
WiktorStarczewski
wants to merge
3
commits into
next
Choose a base branch
from
wiktor/fix-asset-metadata-testnet
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
packages/react-sdk/src/__tests__/utils/accountBech32.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { useMidenStore } from "../../store/MidenStore"; | ||
| import type { MidenConfig } from "../../types"; | ||
|
|
||
| // Network factories surfaced as spies so we can assert which network the | ||
| // bech32 path selects from the configured endpoint. | ||
| const { mockDevnet, mockTestnet, mainnetSpy } = vi.hoisted(() => ({ | ||
| mockDevnet: vi.fn(() => ({ network: "devnet" })), | ||
| mockTestnet: vi.fn(() => ({ network: "testnet" })), | ||
| mainnetSpy: vi.fn(() => ({ network: "mainnet" })), | ||
| })); | ||
|
|
||
| vi.mock("@miden-sdk/miden-sdk", () => { | ||
| const makeId = (hex: string) => ({ | ||
| toString: () => hex, | ||
| // Force the Address.fromAccountId path; this fallback stays unused. | ||
| toBech32: undefined, | ||
| }); | ||
| return { | ||
| Account: class Account {}, | ||
| AccountId: { | ||
| fromHex: vi.fn((hex: string) => makeId(hex)), | ||
| fromBech32: vi.fn((b: string) => makeId(b)), | ||
| }, | ||
| AccountInterface: { BasicWallet: 0 }, | ||
| Address: { | ||
| // toBech32 echoes the selected network so assertions can read it back. | ||
| fromAccountId: vi.fn(() => ({ | ||
| toBech32: (net: { network: string }) => `bech32-${net.network}`, | ||
| })), | ||
| fromBech32: vi.fn(), | ||
| }, | ||
| NetworkId: { | ||
| devnet: mockDevnet, | ||
| testnet: mockTestnet, | ||
| mainnet: mainnetSpy, | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| import { toBech32AccountId } from "../../utils/accountBech32"; | ||
|
|
||
| const ID = "0x1234567890abcdef"; | ||
|
|
||
| const setNetwork = (rpcUrl: string | undefined, ready: boolean) => { | ||
| useMidenStore.getState().setConfig({ rpcUrl } as MidenConfig); | ||
| useMidenStore.getState().setClient((ready ? {} : null) as never); | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| useMidenStore.getState().reset(); | ||
| mockDevnet.mockClear(); | ||
| mockTestnet.mockClear(); | ||
| mainnetSpy.mockClear(); | ||
| }); | ||
|
|
||
| describe("accountBech32 network resolution", () => { | ||
| it("uses devnet when configured for devnet", () => { | ||
| setNetwork("https://rpc.devnet.miden.io", true); | ||
| expect(toBech32AccountId(ID)).toBe("bech32-devnet"); | ||
| expect(mockDevnet).toHaveBeenCalled(); | ||
| expect(mockTestnet).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("uses testnet when configured for testnet", () => { | ||
| setNetwork("https://rpc.testnet.miden.io", true); | ||
| expect(toBech32AccountId(ID)).toBe("bech32-testnet"); | ||
| expect(mockTestnet).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("treats a localhost node as devnet", () => { | ||
| setNetwork("http://localhost:57291", true); | ||
| expect(toBech32AccountId(ID)).toBe("bech32-devnet"); | ||
| expect(mockDevnet).toHaveBeenCalled(); | ||
| expect(mockTestnet).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("falls back to the testnet default when ready with no endpoint", () => { | ||
| setNetwork(undefined, true); | ||
| expect(toBech32AccountId(ID)).toBe("bech32-testnet"); | ||
| expect(mockTestnet).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns the raw id (never testnet) before the provider is ready", () => { | ||
| // The init window: a devnet-configured app must not get a testnet-tagged | ||
| // address while config.rpcUrl hasn't landed in the store yet. | ||
| setNetwork(undefined, false); | ||
| expect(toBech32AccountId(ID)).toBe(ID); | ||
| expect(mockDevnet).not.toHaveBeenCalled(); | ||
| expect(mockTestnet).not.toHaveBeenCalled(); | ||
| expect(mainnetSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns the raw id for an unrecognized custom endpoint rather than guessing testnet", () => { | ||
| setNetwork("https://my-private-node.example", true); | ||
| expect(toBech32AccountId(ID)).toBe(ID); | ||
| expect(mockTestnet).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the fix might be fine (waiting for the client to be initialized sounds like something you want to do anyway), but honestly falling back to
Endpoint.testnet()when nothing is provided sounds like it should probably be an error if it allows for these kinds of race conditions. Also, using the store to persist the RPC config feels like an antipattern as well. Can't we derive the RPC from the client itself?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah so the only place with default is the initialisation of the client - if you just say , it willl go to testnet (mainnet in 2 months). I think this is a worthwhile shortcut. The main issue is other places should never fall back, and this is what I'm doing in this PR.
Re, using store to persist the RPC - that's a good one. A little bit more involved, but I think its worth it. I'll add.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I mentioned
I mostly meant deriving the RPC from the
WebClientstruct. BecauseWebClient::create_client()already takes the endpoint as a parameter, we could store that and use that instead?