Skip to content

ERC2612 permit署名生成hook実装 #33

@yu23ki14

Description

@yu23ki14

概要

ERC2612のpermit署名をオフチェーンで生成するhookを実装します。

詳細

  • EIP-712署名データの構築
  • ウォレットでの署名リクエスト
  • deadline(有効期限)の管理(デフォルト5分、変更可能)
  • nonce取得
  • v, r, s署名パラメータの取得

受け入れ基準

  • app/hooks/usePermitSignature.tsの作成
  • EIP-712署名データ構築
  • deadline設定機能(デフォルト5分)
  • nonce自動取得
  • 署名実行機能
  • 署名結果(v, r, s, deadline)の返却
  • エラーハンドリング

実装例

// app/hooks/usePermitSignature.ts
import { useCallback } from 'react';
import { signTypedData } from 'viem/actions';

export function usePermitSignature() {
  const generatePermitSignature = async (
    owner: string,
    spender: string,
    value: bigint,
    deadlineMinutes: number = 5
  ) => {
    const deadline = Math.floor(Date.now() / 1000) + deadlineMinutes * 60;
    
    // EIP-712署名データ構築
    const domain = {
      name: 'FoR Token',
      version: '1',
      chainId: CHAIN_ID,
      verifyingContract: FOR_TOKEN_ADDRESS,
    };
    
    const types = {
      Permit: [
        { name: 'owner', type: 'address' },
        { name: 'spender', type: 'address' },
        { name: 'value', type: 'uint256' },
        { name: 'nonce', type: 'uint256' },
        { name: 'deadline', type: 'uint256' },
      ],
    };
    
    // nonce取得とメッセージ構築
    // 署名実行
    
    return { v, r, s, deadline };
  };
  
  return { generatePermitSignature };
}

技術スタック

  • Viem
  • EIP-712
  • React 19

依存関係

推定工数

2日

参考

Metadata

Metadata

Assignees

No one assigned

    Labels

    frontendFrontend development

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions