generated from scaffold-eth/scaffold-eth-2
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scaffold is grabbing user key from metamask snaps
- Loading branch information
Showing
6 changed files
with
1,050 additions
and
14 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,127 @@ | ||
"use client"; | ||
|
||
import { useState } from "react"; | ||
import Link from "next/link"; | ||
import type { NextPage } from "next"; | ||
import CopyToClipboard from "react-copy-to-clipboard"; | ||
import { useAccount } from "wagmi"; | ||
import { DocumentDuplicateIcon, MagnifyingGlassIcon } from "@heroicons/react/24/outline"; | ||
import { Address } from "~~/components/scaffold-eth"; | ||
import { shortenKeyHelper } from "~~/utils/scaffold-eth"; | ||
|
||
const Home: NextPage = () => { | ||
const { address: connectedAddress } = useAccount(); | ||
const [connectedToSnap, setConnectedToSnap] = useState<boolean>(false); | ||
const [userKey, setUserKey] = useState<string | null>(null); | ||
|
||
async function connectAndCallSnap() { | ||
const nillionSnapId = "npm:nillion-user-key-manager"; | ||
if (window.ethereum) { | ||
try { | ||
// Request permission to connect to the Snap. | ||
await window.ethereum.request({ | ||
// @ts-ignore | ||
method: "wallet_requestSnaps", | ||
params: { | ||
// @ts-ignore | ||
[nillionSnapId]: {}, | ||
}, | ||
}); | ||
|
||
// Invoke the 'read_user_key' method of the Snap | ||
const response: { user_key: string } = await window.ethereum.request({ | ||
// @ts-ignore | ||
method: "wallet_invokeSnap", | ||
params: { | ||
// @ts-ignore | ||
snapId: nillionSnapId, | ||
request: { method: "read_user_key" }, | ||
}, | ||
}); | ||
|
||
if (response && response.user_key) { | ||
setUserKey(response.user_key); | ||
setConnectedToSnap(true); | ||
} | ||
} catch (error) { | ||
console.error("Error interacting with Snap:", error); | ||
} | ||
} | ||
} | ||
|
||
return ( | ||
<> | ||
<div className="flex items-center flex-col flex-grow pt-10"> | ||
<div className="px-5"> | ||
<h1 className="text-center"> | ||
<span className="block text-4xl font-bold">Build with Nillion</span> | ||
{!connectedToSnap && ( | ||
<button className="btn btn-sm btn-primary mt-4" onClick={connectAndCallSnap}> | ||
Connect to Snap | ||
</button> | ||
)} | ||
</h1> | ||
|
||
{connectedToSnap && ( | ||
<div> | ||
<div className="flex justify-center items-center space-x-2"> | ||
<p className="my-2 font-medium">Connected Address:</p> | ||
<Address address={connectedAddress} /> | ||
</div> | ||
|
||
<div className="flex justify-center items-center space-x-2"> | ||
<p className="my-2 font-medium">Connected Nillion User Key:</p> | ||
{userKey && ( | ||
<span className="flex"> | ||
{shortenKeyHelper(userKey)} | ||
<CopyToClipboard text={userKey}> | ||
<DocumentDuplicateIcon | ||
className="ml-1.5 text-xl font-normal text-sky-600 h-5 w-5 cursor-pointer" | ||
aria-hidden="true" | ||
/> | ||
</CopyToClipboard> | ||
</span> | ||
)} | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
|
||
<div className="flex-grow bg-base-300 w-full mt-16 px-8 py-12"> | ||
<div className="flex justify-center items-center gap-12 flex-col sm:flex-row"> | ||
{!connectedToSnap ? ( | ||
<div className="flex flex-col bg-base-100 px-10 py-10 text-center items-center max-w-m rounded-3xl"> | ||
<MagnifyingGlassIcon className="h-8 w-8 fill-secondary" /> | ||
<p> | ||
To connect with your Nillion user key... | ||
<ol className="block my-4"> | ||
<li>- Download the MetaMask Flask browser extension to get access to MetaMask Snap</li> | ||
<li> | ||
- Visit{" "} | ||
<Link | ||
href="https://github.com/nillion-oss/nillion-snap" | ||
target="_blank" | ||
passHref | ||
className="link" | ||
> | ||
Nillion Key Management UI | ||
</Link>{" "} | ||
to generate a user key | ||
</li> | ||
<li>- Come back and connect to the snap</li> | ||
</ol> | ||
</p> | ||
</div> | ||
) : ( | ||
<div className="flex flex-col bg-base-100 px-10 py-10 text-center items-center max-w-m rounded-3xl"> | ||
Connected as {userKey} | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default Home; |
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,10 @@ | ||
export const shortenKeyHelper = (key: string) => { | ||
if (key.length <= 10) { | ||
return key; // return short keys | ||
} | ||
|
||
const firstFive = key.substring(0, 5); // first 5 | ||
const lastFive = key.substring(key.length - 5); // last 5 | ||
|
||
return firstFive + "..." + lastFive; // concat | ||
}; |
Oops, something went wrong.