-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
76 additions
and
71 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export type LedgerState = "detecting" | "notfound" | "signing" | "succeeded" | "failed" | ||
export type LedgerConfirmation = "Sign with Ledger" | "Ledger: Detecting" | "Ledger: Signing" | "Ledger: Succeeded" | "Ledger: Failed" | ||
|
||
export function getConfirmationTextByState(ledgerState: LedgerState | undefined): LedgerConfirmation { | ||
return ledgerState === undefined | ||
? "Sign with Ledger" | ||
: (ledgerState === "detecting") || (ledgerState === "notfound") | ||
? "Ledger: Detecting" | ||
: ledgerState === "signing" | ||
? "Ledger: Signing" | ||
: ledgerState === "succeeded" | ||
? "Ledger: Succeeded" | ||
: "Ledger: Failed" | ||
} |
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,55 @@ | ||
import { useCallback, useState } from "react" | ||
import { Account } from "../accounts/Account" | ||
import { LedgerAlephium } from "../ledger/utils" | ||
import { LedgerState } from "./types" | ||
|
||
export function useLedgerApp({ | ||
selectedAccount, | ||
unsignedTx, | ||
onSubmit, | ||
navigate, | ||
onReject, | ||
} : { | ||
selectedAccount: Account | undefined, | ||
unsignedTx: string | undefined, | ||
onSubmit: (signature: string) => void, | ||
navigate: (n: number) => void, | ||
onReject?: () => void | ||
}) { | ||
const [ledgerState, setLedgerState] = useState<LedgerState>() | ||
const [ledgerApp, setLedgerApp] = useState<LedgerAlephium>() | ||
|
||
const ledgerSign = useCallback(async () => { | ||
if (selectedAccount === undefined) { | ||
return | ||
} | ||
setLedgerState(oldState => oldState === undefined ? "detecting" : oldState) | ||
|
||
let app: LedgerAlephium | undefined | ||
if (unsignedTx !== undefined) { | ||
try { | ||
app = await LedgerAlephium.create() | ||
setLedgerApp(app) | ||
setLedgerState("signing") | ||
const signature = await app.signUnsignedTx(selectedAccount, Buffer.from(unsignedTx, "hex")) | ||
setLedgerState("succeeded") | ||
onSubmit(signature) | ||
} catch (e) { | ||
if (app === undefined) { | ||
setLedgerState(oldState => oldState === undefined || oldState === "detecting" ? "notfound" : oldState) | ||
setTimeout(ledgerSign, 1000) | ||
} else { | ||
await app.close() | ||
setLedgerState("failed") | ||
if (onReject !== undefined) { | ||
onReject() | ||
} else { | ||
navigate(-1) | ||
} | ||
} | ||
} | ||
} | ||
}, [selectedAccount, onSubmit, onReject, navigate, unsignedTx]) | ||
|
||
return { ledgerState, ledgerApp, ledgerSign } | ||
} |