Monad 백서

Monad

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 56쪽

//github.com/category-labs/mera). # How to use Mera with React Native Source: https://docs.monad.xyz/guides/mera/react-native Create and reuse passkey-derived Monad accounts in a React Native app. [Mera](https://mera.category.xyz/) v0.2.0 adds React Native support through a native WebAuthn client. A user can create a passkey on the web, sign in with the synced passkey on iOS or Android, and derive the same Monad account in each app. The web app and mobile app must use the same relying party ID (`rpId`). The passkey provider must also sync the passkey to the mobile device and support the WebAuthn PRF extension. <Note> The [React Native demo](https://github.com/category-labs/mera/tree/main/demos/mobile) shows the complete flow, including passkey sign-in, biometric-gated device storage, account recovery, and transaction signing. </Note> ## Requirements * Node.js 24 or later * An initialized Expo project with `expo` installed * `@category-labs/mera` v0.2.0 or later * An HTTPS host that you control for the relying party ID and platform association files * iOS 18 or later, or Android 9 or later with a passkey provider that supports PRF * Xcode for iOS, or Android Studio and a JDK with `keytool` on your `PATH` for Android The Expo examples below use a development build because `react-native-passkey` calls native platform APIs. ## Install Run the following commands from the root of an existing Expo project. To create a new project instead: ```bash theme={null} npx create-expo-app@latest my-monad-app --template blank-typescript --yes cd my-monad-app ``` The project creation command installs `expo` and creates the app entry point used in this guide. You do not need to clone the Mera demo. Install Mera, the [`react-native-passkey`](https://github.com/f-23/react-native-passkey) client, and the other dependencies: ```bash theme={null} npm install @category-labs/mera react-native-passkey viem @scure/bip32 @scure/bip39 npx expo install expo-crypto ``` ## Add the Hermes crypto polyfill Mera needs `crypto.getRandomValues`, which Hermes does not provide. Create a polyfill using `expo-crypto`: ```ts title="src/polyfills.ts" theme={null} import { getRandomValues } from "expo-crypto"; if (typeof globalThis.crypto?.getRandomValues !== "function") { Object.defineProperty(globalThis, "crypto", { configurable: true, value: { ...globalThis.crypto, getRandomValues }, }); } ``` Import the polyfill from the app entry point before importing code that uses Mera: ```ts title="index.ts" theme={null} import "./src/polyfills"; import { registerRootComponent } from "expo"; import App from "./App"; registerRootComponent(App); ``` ## Link the app to the passkey domain Choose a host such as `accounts.example.com` for the relying party ID. Use only the host name, without `https://` or a path. To reuse a passkey created by a web app, the web app must create it with this same `rpId`. Each mobile platform also requires a public association file that authorizes the native app to use credentials for the host. ### iOS Serve the following JSON from `https://accounts.example.com/.well-known/apple-app-site-association`: ```json theme={null} { "webcredentials": { "apps": ["TEAM_ID.com.example.app"] } } ``` Replace `TEAM_ID` with your Apple team ID and `com.example.app` with the app's bundle identifier. Add the associated domain to the Expo configuration: ```ts title="app.config.ts" theme={null} import type { ExpoConfig } from "expo/config"; const rpId = "accounts.example.com"; const config: ExpoConfig = { name: "My Monad App", slug: "my-monad-app", ios: { bundleIdentifier: "com.example.app", associatedDomains: [`webcredentials:${rpId}`], }, android: { package: "com.example.app", }, extra: { rpId }, }; export default config; ``` ### Android Serve the following JSON from `https://accounts.example.com/.well-known/assetlinks.json`: ```json theme={null} [ { "relation": ["delegate_permission/common.get_login_creds"], "target": { "namespace": "android_app", "package_name": "com.example.app", "sha256_cert_fingerprints": ["SHA256_FINGERPRINT"] } } ] ``` Replace the package name and fingerprint with those of the Android app. Include the fingerprint for every certificate used to sign the app. The next section shows how to print the fingerprint for the default Expo debug certificate. ## Build the native app Generate the `ios/` and `android/` projects after configuring the app. Expo Prebuild creates these directories and links `react-native-passkey` with the native projects: ```bash theme={null} npx expo prebuild ``` For an Android development build, print the fingerprint of the generated debug certificate and replace `SHA256_FINGERPRINT` in `assetlinks.json` with the result: ```bash theme={null} keytool -list -v \ -keystore android/app/debug.keystore \ -alias androiddebugkey \ -storepass android \ -keypass android ``` Update and deploy `assetlinks.json` with this fingerprint before testing passkeys. Both platform association files must be publicly available over HTTPS, return JSON, and respond without a redirect. Then compile and run the development build. For iOS: ```bash theme={null} npx expo run:ios ``` For Android: ```bash theme={null} npx expo run:android ``` Expo Go cannot run this app because it does not include the native passkey module. ## Create or reuse a passkey React Native does not expose the browser WebAuthn APIs. Pass Mera's `reactNativeWebAuthnClient` to every function that runs a passkey ceremony: ```ts title="src/passkey.ts" theme={null} import { createPasskeyWithPrfOutput, getPasskeyPrfOutput, } from "@category-labs/mera"; import { reactNativeWebAuthnClient } from "@category-labs/mera/react-native-webauthn-client"; const rpId = "accounts.example.com"; export function createAccount() { return createPasskeyWithPrfOutput({ rp: { id: rpId, name: "My Monad App" }, user: { name: "player@example.com", displayName: "Player One" }, webAuthnClient: reactNativeWebAuthnClient, }); } export function signIn() { return getPasskeyPrfOutput({ rpId, webAuthnClient: reactNativeWebAuthnClient, }); } ``` Calling `signIn` without a credential ID lets the platform offer any passkey available for the relying party. If the passkey created on the web has synced to the device, selecting it returns the same PRF output and therefore derives the same account. ## Derive a Monad account Derive the standard EVM BIP-44 key from the returned PRF output: ```ts title="src/wallet.ts" theme={null} import { HDKey } from "@scure/bip32"; import { entropyToMnemonic, mnemonicToSeedSync } from "@scure/bip39"; import { wordlist } from "@scure/bip39/wordlists/english.js"; export function deriveEvmKey( prfOutput: Uint8Array, index = 0, ): Uint8Array { const mnemonic = entropyToMnemonic(prfOutput, wordlist); const seed = mnemonicToSeedSync(mnemonic); try { const node = HDKey.fromMasterSeed(seed).derive(`m/44'/60'/0'/0/${index}`); if (node.privateKey === null) { throw new Error("Derivation produced no private key"); } return node.privateKey; } finally { seed.fill(0); } } ``` The derivation path must match the web app. Increment `index` only when you intend to use another account from the same passkey. ## Send a transaction Create a signing session, adapt it to a viem account, and send a transaction on Monad: ```ts title="src/send-transaction.ts" theme={null} import { createSecp256k1SigningSession } from "@category-labs/mera"; import { toViemAccount } from "@category-labs/mera/viem"; import { createWalletClient, http, parseEther } from "viem"; import { monadTestnet } from "viem/chains"; import { signIn } from "./passkey"; import { deriveEvmKey } from "./wallet"; export async function sendTransaction() { const { prfOutput } = await signIn(); const privateKey = deriveEvmKey(prfOutput); const session = createSecp256k1SigningSession({ privateKey }); try { const client = createWalletClient({ account: toViemAccount(session), chain: monadTestnet, transport: http(), }); const hash = await client.sendTransaction({ to: "0x70997970C51812dc3A010C7d01b50e0d17dc79C8", value: parseEther("0.01"), gas: 21_000n, }); return hash; } finally { privateKey.fill(0); prfOutput.fill(0); session.end(); } } ``` Use `monad` instead of `monadTestnet` for mainnet. See [Gas pricing](/developer-essentials/gas-pricing) before using other transaction types. ## Store and unlock the account The credential ID is metadata and does not contain key material. The PRF output, however, can derive the account's private keys and must be treated as secret key material. For a smoother mobile experience, an app can store the PRF output in platform secure storage protected by biometrics or the device credential. This lets the app unlock an existing account without showing the passkey picker for every transaction. * Do not store the PRF output in `AsyncStorage` or application logs. * End the signing session when the user locks the wallet, signs out, or the app's session expires. * Clear temporary PRF, seed, and private-key buffers after deriving the session. * Request the passkey again before displaying a recovery phrase or performing another sensitive export. The [mobile demo storage implementation](https://github.com/category-labs/mera/blob/main/demos/mobile/src/storage.ts) shows this pattern with Expo SecureStore. ## Troubleshooting * **`PRF_UNAVAILABLE`**: The selected passkey provider or operating system did not return the PRF extension. Check [Mera's authenticator support](https://mera.category.xyz/authenticator-support/). * **`PASSKEY_OPERATION_FAILED` on the first request**: Confirm that the association file is reachable without a redirect and contains the installed app's exact bundle or package identifier and signing certificate. * **The mobile address differs from the web address**: Confirm that both apps use the same `rpId`, the same passkey, and the same BIP-44 account index. ## Further resources * [Create passkey accounts with Mera on the web](/guides/mera) * [Official React Native integration recipe](https://mera.category.xyz/recipes/use-mera-with-react-native/) * [React Native demo](https://github.com/category-labs/mera/tree/main/demos/mobile) * [Mera WebAuthn client reference](https://mera.category.xyz/reference/web-authn-client/) # How to build an MCP server that can interact with Monad Testnet Source: https://docs.monad.xyz/guides/monad-mcp In this guide, you will learn how to build a [Model Context Protocol](https://github.com/modelcontextprotocol) (MCP) server that allows an MCP Client (Claude Desktop) to query Monad Testnet to check the MON balance of an account. ## What is MCP? The [Model Context Protocol](https://github.com/modelcontextprotocol) (MCP) is a standard that allows AI models to interact with external tools and services. ## Prerequisites * Node.js (v16 or later) * `npm` or `yarn` * Claude Desktop ## Getting started 1. Clone the [`monad-mcp-tutorial`](https://github.com/monad-developers/monad-mcp-tutorial) repository. This repository has some code that can help you get started quickly. ```shell theme={null} git clone https://github.com/monad-developers/monad-mcp-tutorial.git ``` 2. Install dependencies: ``` npm install ``` ## Building the MCP server Monad Testnet-related configuration is already added to `index.ts` in the `src` folder. ### Define the server instance ```ts lines title="src/index.ts" theme={null} // Create a new MCP server instance const server = new McpServer({ name: "monad-mcp-tutorial", version: "0.0.1", // Array of supported tool names that clients can call capabilities: ["get-mon-balance"] }); ``` ### Define the MON balance tool Below is the scaffold of the `get-mon-balance` tool: ```ts lines title="src/index.ts" theme={null} server.tool( // Tool ID "get-mon-balance", // Description of what the tool does "Get MON balance for an address on Monad testnet", // Input schema { address: z.string().describe("Monad testnet address to check balance for"), }, // Tool implementation async ({ address }) => { // code to check MON balance } ); ``` Let's add the MON balance check implementation to the tool: ```ts lines title="src/index.ts" theme={null} server.tool( // Tool ID "get-mon-balance", // Description of what the tool does "Get MON balance for an address on Monad testnet", // Input schema { address: z.string().describe("Monad testnet address to check balance for"), }, // Tool implementation async ({ address }) => { try { // Check MON balance for the input address const balance = await publicClient.getBalance({ address: address as `0x${string}`, }); // Return a human friendly message indicating the balance. return { content: [ { type: "text", text: `Balance for ${address}: ${formatUnits(balance, 18)} MON`, }, ], }; } catch (error) { // If the balance check process fails, return a graceful message back to the MCP client indicating a failure. return { content: [ { type: "text", text: `Failed to retrieve balance for address: ${address}. Error: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; } } ); ``` ### Initialize the transport and server from the `main` function ```ts lines title="src/index.ts" theme={null} async function main() { // Create a transport layer using standard input/output const transport = new StdioServerTransport(); // Connect the server to the transport await server.connect(transport); } ``` ### Build the project ```shell theme={null} npm run build ``` The server is now ready to use! ### Add the MCP server to Claude Desktop 1. Open "Claude Desktop" ![claude desktop](https://github.com/monad-developers/monad-mcp-tutorial/blob/main/static/1.png?raw=true) 2. Open Settings Claude > Settings > Developer ![claude settings](https://github.com/monad-developers/monad-mcp-tutorial/blob/main/static/claude_settings.gif?raw=true) 3. Open `claude_desktop_config.json` ![claude config](https://github.com/monad-developers/monad-mcp-tutorial/blob/main/static/config.gif?raw=true) 4. Add details about the MCP server and save the file. ```json lines title="claude_desktop_config.json" theme={null} { "mcpServers": { ... "monad-mcp": { "command": "node", "args": [ "/<path-to-project>/build/index.js" ] } } } ``` 5. Restart "Claude Desktop" ### Use the MCP server You should now be able to see the tools in Claude! ![tools](https://github.com/monad-developers/monad-mcp-tutorial/blob/main/static/tools.gif?raw=true) Here's the final result ![final result](https://github.com/monad-developers/monad-mcp-tutorial/blob/main/static/final_result.gif?raw=true) ## Further resources * [Model Context Protocol Documentation](https://modelcontextprotocol.io/introduction) * [Monad Documentation](https://docs.monad.xyz/) * [Viem Documentation](https://viem.sh/) # How to build a portfolio viewer using Moralis API Source: https://docs.monad.xyz/guides/moralis-api When building a web3 app, it is essentially for the app to show some information about the connected wallet like ERC20 token balances, ERC721 token balances, transaction history etc... Moralis has a wide range of out-of-the-box APIs for the most popular features like wallet native balance, fetch all NFTs and collections, wallet age and activity, and many more... In this guide, you will learn how to build a simple ERC20 token balances view for the connected wallet using Moralis API and NextJS. <Note> If you wish to try the portfolio viewer before building it, you can do so [here](https://portfolio-dashboard-example.vercel.app) </Note> <img alt="portfolio view" /> ## Requirements Before you begin, you'll need: 1. **Moralis API Key**: Get it from [Moralis Dashboard](https://admin.moralis.io/) 2. **Reown Project ID**: Get it from [Reown Cloud](https://cloud.reown.com/) * You can also check out the [Reown AppKit guide for Monad](http://docs.monad.xyz/guides/reown-guide) ## Step 1: Initializing the project ### Create Nextjs app ``` npx create-next-app@latest portfolio-app --typescript --tailwind --eslint --app --src-dir --import-alias "@/*" ``` ### Install ShadCN components ``` npx shadcn@latest add button card tabs ``` ### Install Tanstack React Query (for better async state management) ``` npm install @tanstack/react-query ``` ## Step 2: Environment Setup Add your Moralis API key and Reown Project ID to `.env.local`: ```bash theme={null} MORALIS_API_KEY= NEXT_PUBLIC_PROJECT_ID= ``` ## Step 3: Connect Wallet functionality ### Create the `ContextProvider` component Create a folder named `context` in the `src` directory, inside the folder create a file named `index.ts` ```ts title="src/context/index.ts" theme={null} "use client"; import { wagmiAdapter, projectId } from "@/config"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { createAppKit } from "@reown/appkit/react"; import { monad } from "@reown/appkit/networks"; import React, { type ReactNode } from "react"; import { WagmiProvider, type Config } from "wagmi"; // Set up queryClient const queryClient = new QueryClient(); if (!projectId) { throw new Error("NEXT_PUBLIC_PROJECT_ID is not set"); } // Set up metadata const metadata = { name: "Portfolio Viewer", description: "View your crypto portfolio on Monad blockchain", url: "your-app-url", icons: ["your-app-icon"], }; // Create the modal const modal = createAppKit({ adapters: [wagmiAdapter], projectId, networks: [monad], defaultNetwork: monad, metadata: metadata, features: { analytics: true, }, }); export function ContextProvider({ children }: { children: ReactNode }) { return ( <WagmiProvider config={wagmiAdapter.wagmiConfig as Config}> <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> </WagmiProvider> ); } ``` ### Wrap the app with `ContextProvider` component in `layout.tsx` ```tsx title="src/app/layout.tsx" theme={null} // imports export const metadata: Metadata = { title: "Monad Portfolio App", description: "View your crypto portfolio on Monad blockchain", }; export default function RootLayout({ children, }: Readonly<{ children: React.ReactNode; }>) { return ( <html lang="en" className={GeistSans.variable} style={{ // @ts-ignore '--font-geist-sans': GeistSans.style.fontFamily, '--font-geist-mono': GeistMono.style.fontFamily, }}> <body className="font-sans min-h-screen flex flex-col bg-dot-pattern"> <ContextProvider> <main className="flex-1 pb-6">{children}</main> </ContextProvider> </body> </html> ); } ``` ### Create a custom `ConnectButton` Create a file in `components/wallet` folder named `ConnectButton.tsx` ```tsx title="src/components/wallet/ConnectButton.tsx" theme={null} "use client"; import { useAppKit, useAppKitAccount, useDisconnect } from "@reown/appkit/react"; import { Button } from "@/components/ui/Button"; import { ConnectIcon, type ConnectIconHandle } from "@/components/ui/ConnectIcon"; export function ConnectButton() { const { open } = useAppKit(); const { address, isConnected } = useAppKitAccount(); const { disconnect } = useDisconnect(); if (isConnected && address) { return ( <div className="inline-flex rounded-md shadow-sm"> <a href={`https://monadvision.com/address/${address}`} target="_blank" rel="noopener noreferrer" className="inline-flex items-center px-8 h-10 bg-primary text-primary-foreground rounded-l-md font-semibold text-sm hover:bg-primary/90 transition-colors" > {formatAddress(address)} </a> <Button onClick={() => disconnect()} size="icon" className="h-10 w-10 rounded-l-none border-l border-primary-foreground/20" > <ConnectIcon size={16} /> </Button> </div> ); } return ( <Button onClick={() => open()} size="lg" className="font-semibold shadow-sm gap-2" > <ConnectIcon ref={connectIconRef} size={20} /> Connect Wallet </Button> ); } ``` We are using the `useAppKit` and `useAppKitAccount` hook for creati
프로젝트로 돌아가기