-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "Migrate to signing device" option in account manager
- Loading branch information
Showing
7 changed files
with
96 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"nostrudel": minor | ||
--- | ||
|
||
Add "Migrate to signing device" option in account manager |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
62 changes: 62 additions & 0 deletions
62
src/views/settings/accounts/components/migrate-to-device.tsx
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,62 @@ | ||
import { Button, Flex, Heading, Link, useToast } from "@chakra-ui/react"; | ||
import { PasswordSigner, SerialPortSigner, SimpleSigner } from "applesauce-signer"; | ||
import { useState } from "react"; | ||
|
||
import useAsyncErrorHandler from "../../../../hooks/use-async-error-handler"; | ||
import useCurrentAccount from "../../../../hooks/use-current-account"; | ||
import SerialPortAccount from "../../../../classes/accounts/serial-port-account"; | ||
import accountService from "../../../../services/account"; | ||
|
||
export default function MigrateAccountToDevice() { | ||
if (!SerialPortSigner.SUPPORTED) return null; | ||
|
||
const toast = useToast(); | ||
const current = useCurrentAccount(); | ||
const [loading, setLoading] = useState(false); | ||
|
||
const migrate = useAsyncErrorHandler(async () => { | ||
try { | ||
setLoading(true); | ||
if (!current?.signer) throw new Error("Account missing signer"); | ||
const device = new SerialPortSigner(); | ||
|
||
if (current.signer instanceof SimpleSigner) { | ||
// send key to device | ||
await device.restore(current.signer.key); | ||
} else if (current.signer instanceof PasswordSigner) { | ||
// unlock the signer first | ||
if (!current.signer.unlocked) { | ||
const password = window.prompt("Decryption password"); | ||
if (password === null) throw new Error("Password required"); | ||
await current.signer.unlock(password); | ||
} | ||
|
||
await device.restore(current.signer.key!); | ||
} else throw new Error("Unsupported signer type"); | ||
|
||
// replace existing account | ||
const deviceAccount = new SerialPortAccount(current.pubkey); | ||
accountService.replaceAccount(current.pubkey, deviceAccount); | ||
accountService.switchAccount(deviceAccount.pubkey); | ||
} catch (error) { | ||
if (error instanceof Error) toast({ description: error.message, status: "error" }); | ||
} | ||
setLoading(false); | ||
}, [setLoading, current]); | ||
|
||
return ( | ||
<Flex direction="column" gap="2"> | ||
<Heading size="md" mt="2"> | ||
Migrate to{" "} | ||
<Link isExternal href="https://github.com/lnbits/nostr-signing-device" color="blue.500"> | ||
nostr-signing-device | ||
</Link> | ||
</Heading> | ||
<Flex gap="2" maxW="lg"> | ||
<Button colorScheme="purple" isLoading={loading} onClick={migrate}> | ||
Start migration | ||
</Button> | ||
</Flex> | ||
</Flex> | ||
); | ||
} |
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