Skip to content

Instantly share code, notes, and snippets.

@alongcamewayne
Created April 11, 2025 17:17
Show Gist options
  • Select an option

  • Save alongcamewayne/1fd9493815cbfc06f1d15cdbbe2d211a to your computer and use it in GitHub Desktop.

Select an option

Save alongcamewayne/1fd9493815cbfc06f1d15cdbbe2d211a to your computer and use it in GitHub Desktop.
Script to set the username for your Farcaster account. https://dololand.com/set-farcaster-username
/**
* Set a Farcaster username by submitting a signed message to a hub.
*
* This script:
* - Loads an authorized signer key
* - Creates a UserDataAdd message to set the username
* - Signs and submits the message to a Farcaster hub
*
* Requirements:
* - A reserved fname via the Fname Registry
* - An authorized signer key added to the FID
*
* .env:
* WALLET_FID=1234
* WALLET_SIGNER_PRIVATE_KEY="0x..." (hex-encoded Ed25519 private key)
*/
import { hexToBytes, type Hex } from 'viem';
import {
FarcasterNetwork,
makeUserDataAdd,
Message,
NobleEd25519Signer,
UserDataType,
} from '@farcaster/core';
const HUB_ENDPOINT = 'https://hub.pinata.cloud'; // Farcaster hub endpoint
const fid = Number(process.env.WALLET_FID);
// Load the authorized signer (must be registered to this FID)
const privateKeyBytes = hexToBytes(process.env.WALLET_SIGNER_PRIVATE_KEY! as Hex);
const signer = new NobleEd25519Signer(privateKeyBytes);
// Define the username to link (must already be reserved via the Fname Registry)
const username = 'dooloo';
// Create a signed UserDataAdd message to link the username to the FID
const message = await makeUserDataAdd(
{ type: UserDataType.USERNAME, value: username },
{ fid, network: FarcasterNetwork.MAINNET },
signer
);
console.log('Signed message:', message);
// Encode the message to binary format for submission
const data = Message.encode(message.value).finish();
// Submit the message to the hub
const response = await fetch(`${HUB_ENDPOINT}/v1/submitMessage`, {
method: 'post',
headers: { 'Content-Type': 'application/octet-stream' },
body: data,
});
// Print the hub's response
console.log('Hub response:', await response.json());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment