Skip to content

Instantly share code, notes, and snippets.

@manan19
Last active November 30, 2023 02:55
Show Gist options
  • Save manan19/367a34980e12b9de13ab4afafb3d05d2 to your computer and use it in GitHub Desktop.
Save manan19/367a34980e12b9de13ab4afafb3d05d2 to your computer and use it in GitHub Desktop.
Generating an ECDSA signature
/****
Note - This snippet will not be maintained / updated as of Nov 29, 2023.
Please check out the to generate signers with Neynar https://github.com/neynarxyz/nodejs-sdk
Or you can also use the utility function signKeyRequest inside this package https://github.com/farcasterxyz/hub-monorepo/blob/main/packages/core/src/signers/viemLocalEip712Signer.ts
****/
import { mnemonicToAccount } from "viem/accounts";
/*** EIP-712 helper code ***/
const SIGNED_KEY_REQUEST_VALIDATOR_EIP_712_DOMAIN = {
name: "Farcaster SignedKeyRequestValidator",
version: "1",
chainId: 10,
verifyingContract: "0x00000000fc700472606ed4fa22623acf62c60553",
} as const;
const SIGNED_KEY_REQUEST_TYPE = [
{ name: "requestFid", type: "uint256" },
{ name: "key", type: "bytes" },
{ name: "deadline", type: "uint256" },
] as const;
const publicKey = ""; // Create and fetch this using the Neynar APIs
/*** Generating a Signed Key Request signature ***/
const appFid = process.env.APP_FID; // Your app's fid
const account = mnemonicToAccount(process.env.APP_MNENOMIC); // Your app's mnemonic
const deadline = Math.floor(Date.now() / 1000) + 86400; // signature is valid for 1 day
const signature = await account.signTypedData({
domain: SIGNED_KEY_REQUEST_VALIDATOR_EIP_712_DOMAIN,
types: {
SignedKeyRequest: SIGNED_KEY_REQUEST_TYPE,
},
primaryType: "SignedKeyRequest",
message: {
requestFid: BigInt(appFid),
key: publicKey,
deadline: BigInt(deadline),
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment