Fetch Signers - Backend

This guide demonstrates how to get a list of signers for an account if the developer has the user's mnemonic/account private key (If not check: Frontend (Wallet Integration))

Prerequisites

  1. Node.js Installed
    Ensure you have Nodejs installed on your system. You can download it from Node.js' official website.

  2. API Key and Mnemonic
    Obtain an API key from the dev portal
    Ensure you have a valid Ethereum mnemonic phrase of the account with a signer associated with the above API key.

  3. Dependencies Installed

    Install the required packages:

    yarn add siwe viem @neynar/nodejs-sdk
    

Code Breakdown and Steps

  1. Import Required Libraries
    The code begins by importing the necessary libraries:

    import { SiweMessage } from "siwe";
    import { mnemonicToAccount } from "viem/accounts";
    import { NeynarAPIClient, Configuration } from "@neynar/nodejs-sdk";
    
  2. Define Your Mnemonic
    Replace "YOUR_MNEMONIC_HERE" with your Ethereum mnemonic phrase:

    const mnemonic = "YOUR_MNEMONIC_HERE";
    
  3. Convert Mnemonic to Account
    The mnemonicToAccount function converts your mnemonic into an account object:

    const account = mnemonicToAccount(mnemonic);
    
  4. Configure Neynar API Client
    Replace "YOUR_API_KEY_HERE" with your API key and set the correct base path for the Neynar API:

    const config = new Configuration({
      apiKey: "YOUR_API_KEY_HERE",
    });
    
    const client = new NeynarAPIClient(config);
    
  5. Create the SIWE Message
    The createSiweMessage function generates a SIWE message with details such as domain, address, and nonce:

    async function createSiweMessage(address, statement) {
      const { nonce } = await client.fetchNonce();
    
      const message = new SiweMessage({
        scheme: "http",
        domain: "localhost:8080",
        address,
        statement,
        uri: "http://localhost:8080",
        version: "1",
        chainId: "1",
        nonce: nonce,
      });
    
      return message.prepareMessage();
    }
    
  6. Sign and Verify the Message
    The fetchSigners function handles the signing process and fetches signers:

    async function fetchSigners() {
      const address = account.address;
    
      let message = await createSiweMessage(
        address,
        "Sign in with Ethereum to the app."
      );
    
      const signature = await account.signMessage({ message });
    
      const { signers } = await client.fetchSigners({ message, signature });
    
      return signers;
    }
    
  7. Execute the Function
    Call the fetchSigners function and handle success or errors:

    fetchSigners()
      .then((signers) => {
        console.log("\n\nsigners:", signers, "\n\n");
      })
      .catch((error) => {
        console.error("error:", error);
      });
    

Expected Output

[
  {
    "object": "signer",
    "signer_uuid": "19d0c5fd-9b33-4a48-a0e2-bc7b0555baec",
    "public_key": "0xe4abc135d40f8a6ee216d1a6f2f4e82476dff75f71ea53c5bdebca43f5c415b7",
    "status": "approved",
    "fid": 0
  },
  {
    "object": "signer",
    "signer_uuid": "08c71152-c552-42e7-b094-f510ff44e9cb",
    "public_key": "0xe4cd577123def73295dd9991c589b59b48cdc976b5e83a9ad8d2a13fcfcc0e72",
    "status": "approved",
    "fid": 0
  }
]

For additional help, feel free to contact us.