> ## Documentation Index
> Fetch the complete documentation index at: https://docs.neynar.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Auth Address Signature Generation

> Generate a Signed Key Request using viem for registering auth addresses in Farcaster with Neynar

This guide walks you through generating a Signed Key Request using [viem](https://viem.sh/)
that need to be passed in while [registering auth address](https://docs.neynar.com/reference/register-signed-key-for-developer-managed-auth-address)

## System & Installation Requirements

### Prerequisites

* Node.js >= **18.x** (LTS recommended)
* npm >= **9.x** OR yarn >= **1.22.x**

[Download and install node (if not installed)](https://nodejs.org/en/download)

### Initialize project (optional)

```bash theme={"system"}
mkdir signed-key-request
cd signed-key-request
npm init -y
```

### Install `viem`

```bash theme={"system"}
npm install viem
```

OR with yarn:

```bash theme={"system"}
yarn add viem
```

***

## Code Breakdown and Steps

**You can find full code at the [end of this guide](#full-final-code).**

<Steps>
  <Step title="Import the required functions">
    The code starts by importing the necessary libraries:

    <CodeGroup>
      ```javascript Javascript theme={"system"}
      import { encodeAbiParameters } from "viem";
      import { mnemonicToAccount, generateMnemonic, english } from "viem/accounts";
      ```
    </CodeGroup>
  </Step>

  <Step title="Generate a random mnemonic and derive the auth address">
    Generates a mnemonic and converts it to an Ethereum address (`auth_address`)

    <CodeGroup>
      ```javascript Javascript theme={"system"}
      const mnemonic = generateMnemonic(english);
      const auth_address_acc = mnemonicToAccount(mnemonic);
      const auth_address = auth_address_acc.address;
      ```
    </CodeGroup>
  </Step>

  <Step title="Define EIP-712 domain">
    Describes the EIP-712 domain (context of signature).

    <CodeGroup>
      ```javascript Javascript theme={"system"}
      const SIGNED_KEY_REQUEST_VALIDATOR_EIP_712_DOMAIN = {
        name: "Farcaster SignedKeyRequestValidator",
        version: "1",
        chainId: 10,
        verifyingContract: "0x00000000fc700472606ed4fa22623acf62c60553",
      };
      ```
    </CodeGroup>
  </Step>

  <Step title="Define the EIP-712 message structure">
    Defines the structure of the message to be signed.

    <CodeGroup>
      ```javascript Javascript theme={"system"}
      const SIGNED_KEY_REQUEST_TYPE = [
        { name: "requestFid", type: "uint256" },
        { name: "key", type: "bytes" },
        { name: "deadline", type: "uint256" },
      ];
      ```
    </CodeGroup>
  </Step>

  <Step title="Encode the auth_address">
    Encodes `auth_address` as **32 bytes**.

    <CodeGroup>
      ```javascript Javascript theme={"system"}
      const key = encodeAbiParameters(
        [{ name: "auth_address", type: "address" }],
        [auth_address]
      );
      ```
    </CodeGroup>
  </Step>

  <Step title="App details">
    Replace `"MNEMONIC_HERE"` with your app mnemonic phrase and fid with your app's fid.

    <CodeGroup>
      ```javascript Javascript theme={"system"}
      const fid = 0;
      const account = mnemonicToAccount("MNEMONIC_HERE");
      ```
    </CodeGroup>
  </Step>

  <Step title="Define a deadline">
    Sets a 24-hour expiration time.

    <CodeGroup>
      ```javascript Javascript theme={"system"}
      const deadline = Math.floor(Date.now() / 1000) + 86400;
      ```
    </CodeGroup>
  </Step>

  <Step title="Sign the EIP-712 message">
    Signs the message per EIP-712 standard.

    <CodeGroup>
      ```javascript Javascript theme={"system"}
      const signature = await account.signTypedData({
        domain: SIGNED_KEY_REQUEST_VALIDATOR_EIP_712_DOMAIN,
        types: {
          SignedKeyRequest: SIGNED_KEY_REQUEST_TYPE,
        },
        primaryType: "SignedKeyRequest",
        message: {
          requestFid: BigInt(fid),
          key,
          deadline: BigInt(deadline),
        },
      });
      ```
    </CodeGroup>
  </Step>

  <Step title="Create a sponsor signature">
    If you want to sponsor the auth address, you can sign the EIP-712 signature again with a basic Ethereum signature.
    [This route](https://docs.neynar.com/reference/register-signed-key-for-developer-managed-auth-address) needs to be sponsored if not provided then neynar will sponsor it for you and you will be charged in credits.

    <CodeGroup>
      ```javascript Javascript theme={"system"}
      const sponsorSignature = await account.signMessage({
        message: { raw: signature },
      });
      ```
    </CodeGroup>
  </Step>

  <Step title="Print output">
    Prints useful values for further use.

    <CodeGroup>
      ```javascript Javascript theme={"system"}
      console.log("auth_address", auth_address);
      console.log("app_fid", fid);
      console.log("signature", signature);
      console.log("deadline", deadline);
      console.log("sponsor.signature", sponsorSignature);
      console.log("sponsor.fid", fid);
      ```
    </CodeGroup>
  </Step>

  <Step title="Run the code">
    Save the code in a file, e.g., `generateSignedKeyRequest.js`, and run it using Node.js:

    <CodeGroup>
      ```bash Bash theme={"system"}
      node generateSignedKeyRequest.js
      ```
    </CodeGroup>
  </Step>

  <Step title="cURL">
    Use the generated values to make a cURL request to register the auth address.
    Replace `<api-key>` with your actual API key and `<string>` with your redirect URL(if needed).

    <CodeGroup>
      ```bash Bash theme={"system"}
      curl --request POST \
         --url https://api.neynar.com/v2/farcaster/auth_address/developer_managed/signed_key/ \
         --header 'Content-Type: application/json' \
         --header 'x-api-key: <api-key>' \
         --data '{
         "address": "0x5a927ac639636e534b678e81768ca19e2c6280b7",
         "app_fid": 3,
         "deadline": 123,
         "signature": "0x16161933625ac90b7201625bfea0d816de0449ea1802d97a38c53eef3c9c0c424fefbc5c6fb5eabe3d4f161a36d18cda585cff7e77c677c5d34a9c87e68ede011c",
         "redirect_url": "<string>",
         "sponsor": {
           "fid": 3,
           "signature": "<string>",
           "sponsored_by_neynar": true
         }
       }'
      ```
    </CodeGroup>
  </Step>
</Steps>

***

## Full Final Code

<CodeGroup>
  ```javascript Javascript theme={"system"}

    import { encodeAbiParameters } from "viem";
    import { mnemonicToAccount, generateMnemonic, english } from "viem/accounts";

    (async () => {
      const mnemonic = generateMnemonic(english);
      const auth_address_acc = mnemonicToAccount(mnemonic);
      const auth_address = auth_address_acc.address;

      const SIGNED_KEY_REQUEST_VALIDATOR_EIP_712_DOMAIN = {
        name: "Farcaster SignedKeyRequestValidator",
        version: "1",
        chainId: 10,
        verifyingContract: "0x00000000fc700472606ed4fa22623acf62c60553",
      };

      const SIGNED_KEY_REQUEST_TYPE = [
        { name: "requestFid", type: "uint256" },
        { name: "key", type: "bytes" },
        { name: "deadline", type: "uint256" },
      ];

      const key = encodeAbiParameters(
        [{ name: "auth_address", type: "address" }],
        [auth_address]
      );

      const fid = 0;
      const account = mnemonicToAccount("MNEMONIC_HERE");

      const deadline = Math.floor(Date.now() / 1000) + 86400;

      const signature = await account.signTypedData({
        domain: SIGNED_KEY_REQUEST_VALIDATOR_EIP_712_DOMAIN,
        types: {
          SignedKeyRequest: SIGNED_KEY_REQUEST_TYPE,
        },
        primaryType: "SignedKeyRequest",
        message: {
          requestFid: BigInt(fid),
          key,
          deadline: BigInt(deadline),
        },
      });

      const sponsorSignature = await account.signMessage({
        message: { raw: signature },
      });

      console.log("auth_address", auth_address);
      console.log("app_fid", fid);
      console.log("signature", signature);
      console.log("deadline", deadline);
      console.log("sponsor.signature", sponsorSignature);
      console.log("sponsor.fid", fid);
    })();

  ```
</CodeGroup>

***

Enjoy building! 🚀

For additional help, [feel free to contact us](https://neynar.com/slack).
