-
Notifications
You must be signed in to change notification settings - Fork 128
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
sim
committed
Dec 27, 2021
1 parent
23f316e
commit 7b65fe8
Showing
18 changed files
with
1,093 additions
and
301 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,57 @@ | ||
import { useEffect, useState } from "react" | ||
import { useTranslation } from "react-i18next" | ||
import { useNavigate } from "react-router-dom" | ||
import UsbIcon from "@mui/icons-material/Usb" | ||
import { Button } from "components/general" | ||
import { Card, Grid, Page } from "components/layout" | ||
import { FormError } from "components/form" | ||
import useAuth from "../hooks/useAuth" | ||
import * as ledger from "./ledger" | ||
|
||
const AccessWithLedger = () => { | ||
const { t } = useTranslation() | ||
const navigate = useNavigate() | ||
const { connectLedger, wallet } = useAuth() | ||
const [isLoading, setIsLoading] = useState(false) | ||
const [error, setError] = useState<Error>() | ||
|
||
const connect = async () => { | ||
setIsLoading(true) | ||
setError(undefined) | ||
|
||
try { | ||
const address = await ledger.getTerraAddress() | ||
connectLedger(address) | ||
} catch (error) { | ||
setError(error as Error) | ||
} finally { | ||
setIsLoading(false) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
if (wallet) navigate("/wallet", { replace: true }) | ||
}, [navigate, wallet]) | ||
|
||
return ( | ||
<Page title={t("Access with ledger")} small> | ||
<Card> | ||
<Grid gap={20} className="center"> | ||
<p className="center"> | ||
<UsbIcon style={{ fontSize: 56 }} /> | ||
</p> | ||
|
||
{t("Plug in the Ledger Wallet")} | ||
|
||
{error && <FormError>{error.message}</FormError>} | ||
|
||
<Button onClick={connect} loading={isLoading} color="primary" block> | ||
{t("Connect")} | ||
</Button> | ||
</Grid> | ||
</Card> | ||
</Page> | ||
) | ||
} | ||
|
||
export default AccessWithLedger |
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,41 @@ | ||
import { Key, SignDoc, SignatureV2 } from "@terra-money/terra.js" | ||
import * as ledger from "./ledger" | ||
|
||
class LedgerKey extends Key { | ||
public sign(): Promise<Buffer> { | ||
throw new Error( | ||
"LedgerKey does not use sign() -- use createSignature() directly." | ||
) | ||
} | ||
|
||
public async createSignatureAmino(tx: SignDoc): Promise<SignatureV2> { | ||
const pubkeyBuffer = await ledger.getPubKey() | ||
|
||
if (!pubkeyBuffer) { | ||
throw new Error("failed getting public key from ledger") | ||
} | ||
|
||
const signatureBuffer = await ledger.sign(tx.toAminoJSON()) | ||
|
||
if (!signatureBuffer) { | ||
throw new Error("failed signing from ledger") | ||
} | ||
|
||
if (!this.publicKey) { | ||
throw new Error("public key is undefined") | ||
} | ||
|
||
return new SignatureV2( | ||
this.publicKey, | ||
new SignatureV2.Descriptor( | ||
new SignatureV2.Descriptor.Single( | ||
SignatureV2.SignMode.SIGN_MODE_LEGACY_AMINO_JSON, | ||
signatureBuffer.toString("base64") | ||
) | ||
), | ||
tx.sequence | ||
) | ||
} | ||
} | ||
|
||
export default LedgerKey |
Oops, something went wrong.