Documentation Index
Fetch the complete documentation index at: https://docs.magicblock.gg/llms.txt
Use this file to discover all available pages before exploring further.
SessionWalletProvider は、アプリ内のコンポーネントをラップして、アプリケーション全体に sessionWallet context を提供する高階コンポーネントです。
以下は SessionWalletProvider の使用例です。
components/SessionProvider.tsx という新しいファイルを作成します
// components/SessionProvider.tsx
// The SessionProvider component initializes the SessionKeyManager and provides it to its children via context.
// Wrap any component that needs access to the SessionKeyManager with this provider.
import {
SessionWalletProvider,
useSessionKeyManager,
} from "@magicblock-labs/gum-react-sdk";
import {
AnchorWallet,
useAnchorWallet,
useConnection,
} from "@solana/wallet-adapter-react";
interface SessionProviderProps {
children: React.ReactNode;
}
const SessionProvider: React.FC<SessionProviderProps> = ({ children }) => {
const { connection } = useConnection();
const anchorWallet = useAnchorWallet() as AnchorWallet;
const cluster = "devnet"; // or "mainnet-beta", "testnet", "localnet"
//here the useSessionKeyManager takes in 3 properties
const sessionWallet = useSessionKeyManager(anchorWallet, connection, cluster);
return (
<SessionWalletProvider sessionWallet={sessionWallet}>
{children}
</SessionWalletProvider>
);
};
export default SessionProvider;
- In your
_app.tsx file, wrap the SessionProvider around the entire app to ensure it’s accessible within every component:
// pages/_app.tsx
import SessionProvider from "@/components/SessionProvider";
import { WalletContextProvider } from "@/contexts/WalletContextProvider";
import "@/styles/globals.css";
import { WalletAdapterNetwork } from "@solana/wallet-adapter-base";
import {
PhantomWalletAdapter,
SolflareWalletAdapter,
} from "@solana/wallet-adapter-wallets";
import { clusterApiUrl } from "@solana/web3.js";
import type { AppProps } from "next/app";
import React, { useMemo } from "react";
import dotenv from "dotenv";
// Use require instead of import since order matters
require("@solana/wallet-adapter-react-ui/styles.css");
dotenv.config();
export default function App({ Component, pageProps }: AppProps) {
const network = WalletAdapterNetwork.Devnet;
const endpoint =
process.env.NEXT_PUBLIC_SOLANA_ENDPOINT || clusterApiUrl(network);
const wallets = useMemo(
() => [
new PhantomWalletAdapter({ network }),
new SolflareWalletAdapter({ network }),
],
[network]
);
return (
<WalletContextProvider
endpoint={endpoint}
network={network}
wallets={wallets}
>
<SessionProvider>
<Component {...pageProps} />
</SessionProvider>
</WalletContextProvider>
);
}
注意: すべての Solana wallet adapter context が SessionProvider の親になっていることを確認してください。
- SessionWalletProvider の設定ができたら、コンポーネント内で
useSessionWallet hook を使えるようになります。
コンポーネントで useSessionWallet を使う
useSessionWallet は、session wallet context の値へアクセスするためのカスタム hook です。SessionWalletProvider でラップされた任意のコンポーネントで使用できます。
useSessionWallet hook が何を提供するのか正しく理解するには、SessionWalletInterface を見る必要があります。これは useSessionKeyManager が返すもので、session keys の利用や、トランザクションの署名・送信に必要なメソッドを含んでいます。
interface SessionWalletInterface {
publicKey: PublicKey | null; // Public key associated with the session wallet
ownerPublicKey: PublicKey | null; // The Publickey of the session token authority(The creator of the session token)
isLoading: boolean; // Indicates whether the session wallet is loading
error: string | null; // An error message, if any
sessionToken: string | null; // Session token for the current session
signTransaction:
| (<T extends Transaction>(
transaction: T,
connection?: Connection,
sendOptions?: SendTransactionOptions
) => Promise<T>)
| undefined; // Sign a single transaction
signAllTransactions:
| (<T extends Transaction>(
transactions: T[],
connection?: Connection,
sendOptions?: SendTransactionOptions
) => Promise<T[]>)
| undefined; // Sign multiple transactions
signMessage: ((message: Uint8Array) => Promise<Uint8Array>) | undefined; // Sign a message
sendTransaction:
| (<T extends Transaction>(
transaction: T,
connection?: Connection,
options?: SendTransactionOptions
) => Promise<string>)
| undefined; // Send a signed transaction
signAndSendTransaction:
| (<T extends Transaction>(
transactions: T | T[],
connection?: Connection,
options?: SendTransactionOptions
) => Promise<string[]>)
| undefined; // Sign and send transactions
createSession: (
targetProgram: PublicKey, // Target Solana program
topUpLamports?: number, // Top up session wallet with lamports or not
validUntil?: number, // Duration of session token before expiration
sessionCreatedCallback?: (sessionInfo: {
sessionToken: string;
publicKey: string;
}) => void
) => Promise<SessionWalletInterface | undefined>; // Create a new session
revokeSession: () => Promise<void>; // Revoke the current session
getSessionToken: () => Promise<string | null>; // Retrieve the current session token
}
これにより、コード内で sessionWallet を使って session token を作成したり、不要になったときに revoke したりする方法が理解しやすくなります。
import { useSessionWallet } from "@magicblock-labs/gum-react-sdk";
function YourComponent() {
const sessionWallet = useSessionWallet();
//create session token
const session = await sessionWallet.createSession(
// pass needed params here
);
//Revoke Session Wallet
const result = await sessionWallet.revokeSession();
//Access the session signer Publickey
sessionWallet.publicKey,
//To access the session token
sessionWallet.sessionToken
return (
// Your component JSX
);
}