|
1 | 1 | "use client"; |
| 2 | +import PasskeyAccount from "@/account/passkeyAccount"; |
| 3 | +import Field from "@/components/Field"; |
2 | 4 | import Login from "@/components/Login"; |
| 5 | +import { ethClient } from "@/config"; |
| 6 | +import { useWalletStore } from "@/stores/walletStore"; |
| 7 | +import { submitUserOp } from "@/utils/bundler"; |
| 8 | +import { getXYCoordinates } from "@/utils/webauthn"; |
| 9 | +import { client, parsers } from "@passwordless-id/webauthn"; |
| 10 | +import { RegistrationEncoded } from "@passwordless-id/webauthn/dist/esm/types"; |
3 | 11 | import Link from "next/link"; |
| 12 | +import { useRouter } from "next/navigation"; |
| 13 | +import { useState } from "react"; |
| 14 | +import toast from "react-hot-toast"; |
| 15 | +import { Address, hashMessage, zeroAddress } from "viem"; |
| 16 | +import Passkey from "@/abis/Passkey.json" |
| 17 | +import { PASSKEY } from "@/constants"; |
4 | 18 |
|
5 | 19 | const WelcomePage = () => { |
| 20 | + const [passkeyName, setPasskeyName] = useState<string>(""); |
| 21 | + const createWallet = useWalletStore((state) => state.onCreateWallet); |
| 22 | + const wallets = useWalletStore((state) => state.wallets); |
| 23 | + const route = useRouter(); |
| 24 | + |
| 25 | + const onCreateWallet = async () => { |
| 26 | + const randomString = Math.random().toString(36).substring(2, 15); |
| 27 | + const regData: RegistrationEncoded = await client.register( |
| 28 | + passkeyName, |
| 29 | + randomString, |
| 30 | + { |
| 31 | + authenticatorType: "auto", |
| 32 | + userVerification: "required", |
| 33 | + } |
| 34 | + ); |
| 35 | + const parsedData = parsers.parseRegistration(regData); |
| 36 | + |
| 37 | + let passkey = getXYCoordinates(parsedData.credential.publicKey); |
| 38 | + |
| 39 | + const account = new PasskeyAccount( |
| 40 | + parsedData.credential.id, |
| 41 | + passkey[0] as bigint, |
| 42 | + passkey[1] as bigint |
| 43 | + ); |
| 44 | + |
| 45 | + const [userOp, userOpHash] = await account.sendTransactionOperation( |
| 46 | + ethClient, |
| 47 | + [ |
| 48 | + { |
| 49 | + target: account.getSender(), |
| 50 | + value: 0n, |
| 51 | + data: "", |
| 52 | + }, |
| 53 | + ] |
| 54 | + ); |
| 55 | + const userOpReceipt: any = await toast.promise(submitUserOp(userOp, true), { |
| 56 | + loading: "Wallet creating...", |
| 57 | + success: (data) => ( |
| 58 | + <div> |
| 59 | + Transaction Success -{" "} |
| 60 | + <a href={`https://vicscan.xyz/tx/${userOpHash}`} target="_blank"> |
| 61 | + Click to view on scan |
| 62 | + </a> |
| 63 | + </div> |
| 64 | + ), |
| 65 | + error: (err) => ( |
| 66 | + <div> |
| 67 | + Transaction Fail -{" "} |
| 68 | + <a href={`https://vicscan.xyz/tx/${userOpHash}`} target="_blank"> |
| 69 | + Click to view on scan |
| 70 | + </a> |
| 71 | + </div> |
| 72 | + ), |
| 73 | + }); |
| 74 | + |
| 75 | + if (userOpReceipt && userOpReceipt.success) { |
| 76 | + createWallet({ |
| 77 | + id: wallets.length, |
| 78 | + name: `Account ${wallets.length}`, |
| 79 | + senderAddress: account.getSender(), |
| 80 | + passkeyCredentialId: parsedData.credential.id, |
| 81 | + }); |
| 82 | + |
| 83 | + route.replace("/"); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + const onLogin = async () => { |
| 88 | + const randomString = Math.random().toString(36).substring(2, 15); |
| 89 | + const authData = await client.authenticate([], randomString, { |
| 90 | + authenticatorType: "auto", |
| 91 | + userVerification: "required", |
| 92 | + }); |
| 93 | + |
| 94 | + const keyId = hashMessage(authData.credentialId) |
| 95 | + |
| 96 | + |
| 97 | + const walletAddress = await ethClient.readContract({ |
| 98 | + abi: Passkey.abi, |
| 99 | + address: PASSKEY, |
| 100 | + functionName: "getWallet", |
| 101 | + args: [keyId], |
| 102 | + }) |
| 103 | + |
| 104 | + console.log(walletAddress) |
| 105 | + |
| 106 | + if (walletAddress && walletAddress != zeroAddress) { |
| 107 | + createWallet({ |
| 108 | + id: wallets.length, |
| 109 | + name: `Account ${wallets.length}`, |
| 110 | + senderAddress: walletAddress as Address, |
| 111 | + passkeyCredentialId: authData.credentialId, |
| 112 | + }); |
| 113 | + |
| 114 | + route.replace("/"); |
| 115 | + } |
| 116 | + }; |
| 117 | + |
6 | 118 | return ( |
7 | 119 | <Login |
8 | 120 | title="Abstraction Wallet." |
9 | 121 | description="Your gateway to blockchain world." |
10 | 122 | image="/images/login-pic-1.png" |
11 | 123 | > |
12 | | - <button className="btn-primary w-full mb-3"> |
13 | | - <Link href="/create">Create new wallet</Link>{" "} |
14 | | - </button> |
15 | | - <button className="btn-secondary w-full mb-3"> |
16 | | - <Link href="/login">Login with exited wallet</Link>{" "} |
17 | | - </button> |
| 124 | + <Field |
| 125 | + className="flex-1 mb-3" |
| 126 | + placeholder="Enter your passkey name" |
| 127 | + value={passkeyName} |
| 128 | + onChange={(e) => setPasskeyName(e.target.value)} |
| 129 | + required |
| 130 | + /> |
| 131 | + <div className="justify-center w-full mt-6 flex md:flex-1"> |
| 132 | + <button className="btn-primary mb-3 mr-2 w-full md:w-1/2" onClick={onCreateWallet}> |
| 133 | + Create new wallet |
| 134 | + </button> |
| 135 | + <button className="btn-secondary mb-3 w-full md:w-1/2" onClick={onLogin}> |
| 136 | + Login with exited wallet |
| 137 | + </button> |
| 138 | + </div> |
18 | 139 | <div className="mt-5 text-title-1s text-theme-tertiary md:mt-4 md:text-base-1s"> |
19 | 140 | Lost your wallet?{" "} |
20 | 141 | <Link |
|
0 commit comments