# unWallet ユーザーに対して電子署名をリクエストする

## 手順

### 1. unWallet 認証を終わらせる

unWallet ユーザーに対して電子署名をリクエストするためには、該当ユーザーがリクエスト元となる認証アプリケーションとの接続を認可している必要があります。この認可は [unWallet 認証](https://developers.ent.unwallet.world/02_usecase/01_connect-to-unwallet) の中で行われますので、まずはこちらを終わらせてください。

### 2. 電子署名チケットトークンを発行する

[POST /issueSignatureTicket](https://developers.ent.unwallet.world/01_spec/01_http-endpoint/01_interface/01_blockchain#issuesignatureticket) を利用して、電子署名チケットトークンを発行してください。なお、この処理はアプリケーションのバックエンドで行なってください。

### 3. 電子署名をリクエストする

クライアントサイド SDK の `sign` を実行し、unWallet ユーザーに対して電子署名をリクエストしてください。なお、`sign` の引数である `ticketToken` には、前段で発行した電子署名チケットトークンを指定してください。それ以外の引数には、前段で電子署名チケットトークンを発行した際に指定した値と同じ値を指定してください。

{% hint style="info" %}
`sign` の詳細な仕様については [こちら](https://developers.ent.unwallet.world/01_spec/02_client-side-sdk/02_interface#sign) を参照してください。
{% endhint %}

リクエストが不正とみなされてしまう場合は、前段で登録した認証アプリケーションの `allowedCallerOrigins` が正しく設定されているかを確認してください。

### 4. 電子署名の正当性を検証する

unWallet はコントラクトウォレットであるため、[ERC-1271](https://eips.ethereum.org/EIPS/eip-1271) に準拠した方法で電子署名の正当性を検証してください。

{% tabs %}
{% tab title="JavaScript" %}

```js
import { ethers } from "ethers";

(async () => {
  const contract = new ethers.Contract(
    "<USER_ADDRESS>",
    [
      {
        inputs: [
          {
            internalType: "bytes32",
            name: "hash",
            type: "bytes32",
          },
          {
            internalType: "bytes",
            name: "signature",
            type: "bytes",
          },
        ],
        name: "isValidSignature",
        outputs: [
          {
            internalType: "bytes4",
            name: "",
            type: "bytes4",
          },
        ],
        stateMutability: "view",
        type: "function",
      },
    ],
    new ethers.JsonRpcProvider("<YOUR_RPC_URL>")
  );

  try {
    await contract.isValidSignature("<DIGEST>", "<SIGNATURE>");
  } catch (e) {
    console.log("invalid");
    return;
  }

  console.log("valid");
})();
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.ent.unwallet.world/02_usecase/03_request-to-sign.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
