-
Notifications
You must be signed in to change notification settings - Fork 121
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
feat: disable submit button for masp tx when using disconnected ledger device #1572
Merged
mateuszjasiuk
merged 2 commits into
feat/ledger-masp-integration-branch
from
feat/disable-submit-button-for-masp-tx-when-ledger-is-not-connected
Jan 31, 2025
Merged
Changes from all commits
Commits
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 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 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 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 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 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 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,75 @@ | ||
import { ledgerUSBList } from "@namada/sdk/web"; | ||
import { LedgerError } from "@zondax/ledger-namada"; | ||
import { isLedgerAccountAtom } from "atoms/accounts"; | ||
import { atom } from "jotai"; | ||
|
||
import { atomWithQuery } from "jotai-tanstack-query"; | ||
import { getSdkInstance } from "utils/sdk"; | ||
|
||
export type LedgerStatus = { | ||
connected: boolean; | ||
errorMessage: string; | ||
}; | ||
|
||
const ledgerStatusStopAtom = atom(false); | ||
|
||
export const ledgerStatusAtom = atomWithQuery<LedgerStatus | undefined>(() => { | ||
return { | ||
refetchInterval: 1000, | ||
queryKey: ["ledger-status"], | ||
queryFn: async () => { | ||
const devices = await ledgerUSBList(); | ||
|
||
if (devices.length > 0) { | ||
try { | ||
// Disable console.warn to prevent the warning from showing up in the UI in the loop | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(console as any).warnOld = console.warn; | ||
console.warn = () => {}; | ||
|
||
const sdk = await getSdkInstance(); | ||
const ledger = await sdk.initLedger(); | ||
const { | ||
version: { returnCode, errorMessage }, | ||
} = await ledger.status(); | ||
|
||
const connected = returnCode === LedgerError.NoErrors; | ||
|
||
await ledger.closeTransport(); | ||
|
||
return { | ||
connected, | ||
errorMessage, | ||
}; | ||
} catch (e) { | ||
return { | ||
connected: false, | ||
errorMessage: `${e}`, | ||
}; | ||
} finally { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
console.warn = (console as any).warnOld; | ||
} | ||
} | ||
|
||
return { | ||
connected: false, | ||
errorMessage: "Ledger device not detected", | ||
}; | ||
}, | ||
}; | ||
}); | ||
|
||
export const ledgerStatusDataAtom = atom( | ||
(get) => { | ||
const isLedgerAccount = get(isLedgerAccountAtom); | ||
const ledgetStatusStop = get(ledgerStatusStopAtom); | ||
|
||
if (isLedgerAccount && !ledgetStatusStop) { | ||
return get(ledgerStatusAtom).data; | ||
} | ||
}, | ||
(_, set, stop: boolean) => { | ||
set(ledgerStatusStopAtom, stop); | ||
} | ||
); |
This file contains 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 @@ | ||
export * from "./atoms"; |
This file contains 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 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 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
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 something may need to change here after testing. What I found was that this loop continues to execute after I've connected and submitted a Tx, and I hit a few cases with different errors thrown. It will definitely be an issue if we try to open transport on Web when the Keychain is attempting to open transport to sign. I'll give it some thought! Maybe if we, say, detect it once, if it's undetected, require the user to click to Connect, once connected, fetch build params then submit. I think maybe it would be better if validation happens once, or, if the check is discontinued after first successful connection? Not sure, will take some thought