You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Before you can build messages, you'll need to construct the following objects:
An ed25519Signer, used to sign most messages.
A eip712Signer, used to sign some messages.
A dataOptions, which contains message metadata.
Ed25519Signer
A Ed25519Signer is an EdDSA key pair which is necessary for signing most messages on behalf of an fid. This example below shows how to construct a new NobleEd25519Signer using the @noble library:
import{NobleEd25519Signer}from'@farcaster/hub-nodejs';import*asedfrom'@noble/ed25519';constprivateKey=ed.utils.randomPrivateKey();// Applications must store this key securely.consted25519Signer=newNobleEd25519Signer(privateKey);
Eip712Signer
An Eip712Signer is an ECDSA key pair which is necessary signing for some messages like SignerAdds and Verifications. This example shows how to construct an EthersEip712Signer from a wallet's recovery phrase:
import{EthersEip712Signer}from'@farcaster/hub-nodejs';import{Wallet}from'ethers';constmnemonic='ordinary long coach bounce thank quit become youth belt pretty diet caught attract melt bargain';constwallet=Wallet.fromPhrase(mnemonic);consteip712Signer=newEthersEip712Signer(wallet);
Note: If you must use Ethers v5 in your application, you'll need to use the EthersV5Eip712Signer.
Data Options
Common message properties that must be passed into to any Builder method to produce a valid message.
Usage
import{FarcasterNetwork,toFarcasterTime}from'@farcaster/hub-nodejs';constunixTimestamp=1679029607159;// Safety: timestamp is known and cannot errorconstfarcasterTimestamp=toFarcasterTime(unixTimestamp)._unsafeUnwrap();constdataOptions={fid: 1,network: FarcasterNetwork.DEVNET,timestamp: farcasterTimestamp,};
(optional) Farcaster epoch message timestamp, defaults to now
Builder Methods
makeSignerAdd
Returns a message which authorizes a new Ed25519 Signer to create messages on behalf of a user.
Usage
import{makeSignerAdd,hexStringToBytes}from'@farcaster/hub-nodejs';constsignerHex='0x027fb58156b2733495acb24248e5e3ddf7ad8be4b85321c1e598e06ed030f51f';// public key of the Ed25519 key-pair to authorizeconstsignerBytes=hexStringToBytes(signerHex)._unsafeUnwrap();// Safety: signerHex is known and cannot errorconstname='foo';// label to help the user identify this signersconstsignerAdd=awaitmakeSignerAdd({signer: signerBytes, name },dataOptions,eip712Signer);
Returns
Type
Description
HubAsyncResult<SignerAddMessage>
A HubAsyncResult that contains the valid SignerAddMessage.
An Eip712Signer generated from the user's custody address.
makeSignerRemove
Returns a message which revokes a previously authorized Ed25519 Signer.
Usage
import{makeSignerRemove,hexStringToBytes}from'@farcaster/hub-nodejs';constsignerHex='0x027fb58156b2733495acb24248e5e3ddf7ad8be4b85321c1e598e06ed030f51f';// public key of the Ed25519 key-pair to revokeconstsignerBytes=hexStringToBytes(signerHex)._unsafeUnwrap();// Safety: signerHex is known and cannot errorconstsignerRemove=awaitmakeSignerRemove({signer: signerBytes},dataOptions,eip712Signer);
Returns
Type
Description
HubAsyncResult<SignerRemoveMessage>
A HubAsyncResult that contains the valid SignerRemoveMessage.
import{hexStringToBytes,makeCastRemove}from'@farcaster/hub-nodejs';consttargetHashHex='006f082f70dfb2de81e7852f3b79f1cdf2aa6b86';// Safety: targetHashHex is known and can't errorconsttargetHashBytes=hexStringToBytes(targetHashHex)._unsafeUnwrap();constcastRemove=awaitmakeCastRemove({targetHash: targetHashBytes,},dataOptions,ed25519Signer);
Returns
Type
Description
HubAsyncResult<CastRemoveMessage>
A HubAsyncResult that contains the valid CastRemoveMessage.
Returns a message that adds a Reaction to an existing Cast.
Usage
import{hexStringToBytes,makeReactionAdd,ReactionType}from'@farcaster/hub-nodejs';consttargetHashHex='006f082f70dfb2de81e7852f3b79f1cdf2aa6b86';// Safety: targetHashHex is known and can't errorconsttargetHashBytes=hexStringToBytes(targetHashHex)._unsafeUnwrap();constreactionLikeBody={type: ReactionType.LIKE,targetCastId: {fid: 1,// Fid of the Cast's author, which is being reacted to.hash: targetHashBytes,// Hash of the CastAdd message being reacted to},};constlike=awaitmakeReactionAdd(reactionLikeBody,dataOptions,ed25519Signer);
Returns
Type
Description
HubAsyncResult<ReactionAddMessage>
A HubAsyncResult that contains the valid ReactionAddMessage.
Returns a message that removes an existing Reaction to an existing Cast.
Usage
import{hexStringToBytes,makeReactionRemove,ReactionType}from'@farcaster/hub-nodejs';consttargetHashHex='006f082f70dfb2de81e7852f3b79f1cdf2aa6b86';// Safety: targetHashHex is known and can't errorconsttargetHashBytes=hexStringToBytes(targetHashHex)._unsafeUnwrap();constreactionLikeBody={type: ReactionType.LIKE,targetCastId: {fid: 1,// Fid of the Cast's author, which is being reacted to.hash: targetHashBytes,// Hash of the CastAdd message being reacted to},};constlike=awaitmakeReactionRemove(reactionLikeBody,dataOptions,ed25519Signer);
Returns
Type
Description
HubAsyncResult<ReactionRemoveMessage>
A HubAsyncResult that contains the valid ReactionRemoveMessage.
Returns a message that proves that a user owns an Ethereum address.
Usage
import{FarcasterNetwork,hexStringToBytes,makeVerificationAddEthAddress,makeVerificationEthAddressClaim,}from'@farcaster/hub-nodejs';// Safety: eip712Signer is known and can't errorconstaddressBytes=(awaiteip712Signer.getSignerKey())._unsafeUnwrap();constfid=1;constblockHashHex='0x1d3b0456c920eb503450c7efdcf9b5cf1f5184bf04e5d8ecbcead188a0d02018';constblockHashBytes=hexStringToBytes(blockHashHex)._unsafeUnwrap();constclaimResult=makeVerificationEthAddressClaim(fid,addressBytes,FarcasterNetwork.DEVNET,blockHashBytes);if(claimResult.isOk()){constclaim=claimResult.value;// Sign the claimconstethSignResult=awaiteip712Signer.signVerificationEthAddressClaim(claim);constethSignature=ethSignResult._unsafeUnwrap();// Safety: claim is known and can't error// Construct a Verification Add Message with the claim signatureconstverificationBody={address: addressBytes,claimSignature: ethSignature,blockHash: blockHashBytes,};constverificationMessage=awaitmakeVerificationAddEthAddress(verificationBody,dataOptions,ed25519Signer);}
Returns
Type
Description
HubAsyncResult<VerificationAddEthAddressMessage>
A HubAsyncResult that contains a signed VerificationAddEthAddressMessage.
Returns a message that removes a previously added Verification.
Usage
import{makeVerificationRemove}from'@farcaster/hub-nodejs';// Safety: eip712Signer is known and can't errorconstaddressBytes=(awaiteip712Signer.getSignerKey())._unsafeUnwrap();constverificationRemoveBody={address: eip712Signer.addressBytes,// Ethereum Address of Verification to remove};constverificationRemoveMessage=awaitmakeVerificationRemove(verificationRemoveBody,dataOptions,ed25519Signer);
Returns
Type
Description
HubAsyncResult<VerificationRemoveMessage>
A HubAsyncResult that contains a signed VerificationRemoveMessage.