> ## 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.

# 사용 예제

> 다양한 실전 예제를 통해 useSessionKeyManager와 SessionWalletProvider를 사용해 dApp과 상호작용하는 방법을 배웁니다

이제 hooks와 provider 설정이 끝났으니, 제공된 메서드를 사용하는 예제를 살펴보겠습니다.

### 세션 생성하기

세션을 생성하려면 `sessionWallet`의 `createSession` 메서드를 호출합니다. 이 메서드는 세 가지 파라미터를 받습니다.

1. `targetProgramPublicKey`: 상호작용하려는 대상 프로그램을 나타내는 `PublicKey` 인스턴스
2. `topUp`: 세션 keypair에 처음부터 `0.01 SOL`을 충전하고 싶다면 `true`, 원하지 않으면 `false`
3. `expiryInMinutes`: 세션 만료 시간을 분 단위로 나타내는 선택 파라미터입니다. 기본값은 60분입니다.

```typescript theme={null}
const handleCreateSession = async () => {
  const targetProgramPublicKey = new PublicKey(
    "your_target_program_public_key"
  );
  const topUp = true;
  const expiryInMinutes = 60;

  const session = await sessionWallet.createSession(
    targetProgramPublicKey,
    topUp,
    expiryInMinutes
  );

  // session wallet에 충전할 금액을 직접 지정할 수도 있습니다
  // 지정한 금액만큼 충전되며, revoke 시 authority로 다시 반환됩니다
  const session = await sessionWallet.createSession(
    targetProgramPublicKey,
    topUp ? 10000000 : 0, // 0.01 SOL
    expiryInMinutes
  );

  if (session) {
    console.log("Session created:", session);
  } else {
    console.error("Failed to create session");
  }
};
```

`createSession`을 호출하면 새로운 임시 keypair가 생성되어 클라이언트 측에 저장됩니다. 이후 세션 토큰도 생성되어 해당 keypair와 함께 저장됩니다. 이를 통해 사용자는 실제 지갑의 개인 키를 노출하지 않고도 생성된 keypair로 안전하게 트랜잭션에 서명할 수 있습니다.

### 트랜잭션 서명 및 전송

트랜잭션에 서명하고 전송하려면 `signAndSendTransaction` 메서드를 사용합니다. 이 메서드는 먼저 세션 중 생성된 임시 keypair로 트랜잭션에 서명한 뒤, 서명된 트랜잭션을 Solana 네트워크로 전송합니다.

```typescript theme={null}
const handleSendTransaction = async () => {
  const transaction = new Transaction();
  // 트랜잭션에 명령을 추가합니다
  const txids = await sessionWallet.signAndSendTransaction(transaction);

  if (txids && txids.length > 0) {
    console.log("Transaction sent:", txids);
  } else {
    console.error("Failed to send transaction");
  }
};
```

`signAndSendTransaction` 메서드는 실제 지갑의 개인 키가 노출되지 않도록 하여 추가적인 보안 계층을 제공합니다. 클라이언트 측에 저장된 임시 keypair로 트랜잭션에 서명하므로 사용자의 메인 지갑을 안전하게 보호할 수 있습니다.

### 세션 철회하기

세션을 철회하려면 `sessionWallet`의 `revokeSession` 메서드를 호출합니다. 이 메서드는 세 가지 작업을 수행합니다.

1. 클라이언트 측 저장소에서 임시 keypair와 세션 토큰을 제거합니다.
2. 컨트랙트에서 세션을 철회합니다.
3. lamports를 authority에 반환하고 session token PDA를 닫습니다.

```typescript theme={null}
const handleRevokeSession = async () => {
  await sessionWallet.revokeSession();
  console.log("Session revoked");
};
```

세션을 철회하면 임시 keypair는 더 이상 유효하지 않으며 사용할 수도 없게 됩니다。

이 예제들은 앱에서 세션 관리와 지갑 기능을 구현하는 출발점이 될 것입니다.

***Session Token 구현 방식은*** [***Example App***](https://github.com/gumhq/gum-example-app) ***의 "Create Post" 섹션을 참고하세요.***
