-
Notifications
You must be signed in to change notification settings - Fork 854
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow mnemonic creation for accounts onboarded with ledger (#3224)
* wip * force close parent and spacing tweak
- Loading branch information
Showing
18 changed files
with
479 additions
and
254 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
140 changes: 140 additions & 0 deletions
140
packages/app-extension/src/components/Unlocked/Settings/AddConnectWallet/CreateMnemonic.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,140 @@ | ||
import { useEffect, useState } from "react"; | ||
import type { | ||
Blockchain, | ||
SignedWalletDescriptor, | ||
} from "@coral-xyz/common"; | ||
import { | ||
getAddMessage, | ||
UI_RPC_METHOD_BLOCKCHAIN_KEYRINGS_ADD, | ||
UI_RPC_METHOD_FIND_WALLET_DESCRIPTOR, | ||
UI_RPC_METHOD_KEYRING_IMPORT_WALLET, | ||
UI_RPC_METHOD_KEYRING_SET_MNEMONIC, | ||
UI_RPC_METHOD_SIGN_MESSAGE_FOR_PUBLIC_KEY | ||
} from "@coral-xyz/common"; | ||
import { | ||
useBackgroundClient, | ||
} from "@coral-xyz/recoil"; | ||
import { useCustomTheme } from "@coral-xyz/themes"; | ||
import { ethers } from "ethers"; | ||
|
||
import { MnemonicInput } from "../../../common/Account/MnemonicInput"; | ||
import { | ||
useDrawerContext, | ||
WithMiniDrawer, | ||
} from "../../../common/Layout/Drawer"; | ||
import { useNavigation } from "../../../common/Layout/NavStack"; | ||
|
||
const { base58 } = ethers.utils; | ||
|
||
import { ConfirmCreateWallet } from "./"; | ||
|
||
export function CreateMnemonic({ | ||
blockchain, | ||
keyringExists, | ||
}: { | ||
blockchain: Blockchain; | ||
keyringExists: boolean; | ||
}) { | ||
const nav = useNavigation(); | ||
const theme = useCustomTheme(); | ||
const background = useBackgroundClient(); | ||
const { close: closeParentDrawer } = useDrawerContext(); | ||
|
||
const [openDrawer, setOpenDrawer] = useState(false); | ||
const [publicKey, setPublicKey] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
const prevTitle = nav.title; | ||
nav.setOptions({ headerTitle: "" }); | ||
return () => { | ||
nav.setOptions({ headerTitle: prevTitle }); | ||
}; | ||
}, [theme]); | ||
|
||
// TODO replace the left nav button to go to the previous step if step > 0 | ||
|
||
const onComplete = async (mnemonic: string, signedWalletDescriptor: SignedWalletDescriptor) => { | ||
let publicKey: string; | ||
await background.request({ | ||
method: UI_RPC_METHOD_KEYRING_SET_MNEMONIC, | ||
params: [mnemonic] | ||
}) | ||
if (keyringExists) { | ||
// Using the keyring mnemonic and the blockchain keyring exists, just | ||
// import the path | ||
publicKey = await background.request({ | ||
method: UI_RPC_METHOD_KEYRING_IMPORT_WALLET, | ||
params: [blockchain, signedWalletDescriptor], | ||
}); | ||
} else { | ||
// Blockchain keyring doesn't exist, init | ||
publicKey = await background.request({ | ||
method: UI_RPC_METHOD_BLOCKCHAIN_KEYRINGS_ADD, | ||
params: [blockchain, signedWalletDescriptor], | ||
}); | ||
} | ||
setPublicKey(publicKey); | ||
setOpenDrawer(true); | ||
}; | ||
|
||
return ( | ||
<> | ||
<MnemonicInput | ||
readOnly | ||
buttonLabel="Next" | ||
subtitle="Write it down and store it in a safe place." | ||
onNext={async (mnemonic: string) => { | ||
const walletDescriptor = await background.request({ | ||
method: UI_RPC_METHOD_FIND_WALLET_DESCRIPTOR, | ||
params: [blockchain, 0, mnemonic], | ||
}); | ||
|
||
const signature = await background.request({ | ||
method: UI_RPC_METHOD_SIGN_MESSAGE_FOR_PUBLIC_KEY, | ||
params: [ | ||
blockchain, | ||
walletDescriptor.publicKey, | ||
base58.encode( | ||
Buffer.from( | ||
getAddMessage(walletDescriptor.publicKey), | ||
"utf-8" | ||
) | ||
), | ||
[mnemonic, [walletDescriptor.derivationPath]], | ||
] | ||
}) | ||
|
||
await onComplete(mnemonic, { | ||
...walletDescriptor, | ||
signature | ||
}) | ||
}} | ||
/> | ||
<WithMiniDrawer | ||
openDrawer={openDrawer} | ||
setOpenDrawer={(open: boolean) => { | ||
// Must close parent when the confirm create wallet drawer closes because | ||
// the next button in the mnemonic input screen is no longer valid as the users | ||
// keyring has a mnemonic once it has been clicked once | ||
if (!open) closeParentDrawer() | ||
setOpenDrawer(open) | ||
}} | ||
backdropProps={{ | ||
style: { | ||
opacity: 0.8, | ||
background: "#18181b", | ||
}, | ||
}} | ||
> | ||
<ConfirmCreateWallet | ||
blockchain={blockchain} | ||
publicKey={publicKey!} | ||
onClose={() => { | ||
setOpenDrawer(false); | ||
closeParentDrawer(); | ||
}} | ||
/> | ||
</WithMiniDrawer> | ||
</> | ||
); | ||
} |
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
Oops, something went wrong.
1262caa
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.
Successfully deployed to the following URLs:
backpack – ./
backpack-200ms.vercel.app
backpack-git-master-200ms.vercel.app
devnet.backpack.app
backpack.app
www.backpack.app