Skip to content

Commit

Permalink
scaffold is grabbing user key from metamask snaps
Browse files Browse the repository at this point in the history
  • Loading branch information
oceans404 committed Feb 23, 2024
1 parent 4ac23d0 commit 1575c0c
Show file tree
Hide file tree
Showing 6 changed files with 1,050 additions and 14 deletions.
127 changes: 127 additions & 0 deletions packages/nextjs/app/nillion/page.tsx
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;
4 changes: 4 additions & 0 deletions packages/nextjs/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export const menuLinks: HeaderMenuLink[] = [
label: "Home",
href: "/",
},
{
label: "Nillion",
href: "/nillion",
},
{
label: "Debug Contracts",
href: "/debug",
Expand Down
148 changes: 147 additions & 1 deletion packages/nextjs/contracts/deployedContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,152 @@
*/
import { GenericContractsDeclaration } from "~~/utils/scaffold-eth/contract";

const deployedContracts = {} as const;
const deployedContracts = {
31337: {
YourContract: {
address: "0x5FbDB2315678afecb367f032d93F642f64180aa3",
abi: [
{
inputs: [
{
internalType: "address",
name: "_owner",
type: "address",
},
],
stateMutability: "nonpayable",
type: "constructor",
},
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: "address",
name: "greetingSetter",
type: "address",
},
{
indexed: false,
internalType: "string",
name: "newGreeting",
type: "string",
},
{
indexed: false,
internalType: "bool",
name: "premium",
type: "bool",
},
{
indexed: false,
internalType: "uint256",
name: "value",
type: "uint256",
},
],
name: "GreetingChange",
type: "event",
},
{
inputs: [],
name: "greeting",
outputs: [
{
internalType: "string",
name: "",
type: "string",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "owner",
outputs: [
{
internalType: "address",
name: "",
type: "address",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "premium",
outputs: [
{
internalType: "bool",
name: "",
type: "bool",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [
{
internalType: "string",
name: "_newGreeting",
type: "string",
},
],
name: "setGreeting",
outputs: [],
stateMutability: "payable",
type: "function",
},
{
inputs: [],
name: "totalCounter",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [
{
internalType: "address",
name: "",
type: "address",
},
],
name: "userGreetingCounter",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "withdraw",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
{
stateMutability: "payable",
type: "receive",
},
],
inheritedFunctions: {},
},
},
} as const;

export default deployedContracts satisfies GenericContractsDeclaration;
1 change: 1 addition & 0 deletions packages/nextjs/utils/scaffold-eth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from "./notification";
export * from "./block";
export * from "./decodeTxData";
export * from "./getParsedError";
export * from "./shortenKeyHelper";
10 changes: 10 additions & 0 deletions packages/nextjs/utils/scaffold-eth/shortenKeyHelper.ts
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
};
Loading

0 comments on commit 1575c0c

Please sign in to comment.