Skip to content

Instantly share code, notes, and snippets.

@i001962
Created May 11, 2025 00:08
Show Gist options
  • Select an option

  • Save i001962/01ece86dbe14f476c5cdc3ef0cd42d09 to your computer and use it in GitHub Desktop.

Select an option

Save i001962/01ece86dbe14f476c5cdc3ef0cd42d09 to your computer and use it in GitHub Desktop.
trying to write to snapchain w privy signer
import { useState } from 'react';
import { ExternalEd25519Signer, HubRestAPIClient } from '@standard-crypto/farcaster-js';
import { useLogin, usePrivy, useFarcasterSigner } from '@privy-io/react-auth';
export default function Cast() {
const [text, setText] = useState('');
const { login } = useLogin();
const { user } = usePrivy();
const { getFarcasterSignerPublicKey, signFarcasterMessage, requestFarcasterSignerFromWarpcast } = useFarcasterSigner();
const farcasterAccount = user?.linkedAccounts.find(
(account) => account.type === 'farcaster'
);
async function sendCast() {
if (!farcasterAccount) {
console.log("No Farcaster account linked.");
return;
}
const signer = new ExternalEd25519Signer(
signFarcasterMessage,
getFarcasterSignerPublicKey
);
const client = new HubRestAPIClient({
// hubUrl: 'https://hub.pinata.cloud'
hubUrl: 'https://snapchain-api.neynar.com',
});
try {
const { hash } = await client.submitCast(
{ text },
farcasterAccount?.fid || 4163,
signer
);
console.log("Cast submitted with hash:", hash);
setText("");
} catch (err) {
console.error("Error sending cast:", err);
}
}
async function fetchSnapchainInfo() {
try {
const response = await fetch('https://snapchain-api.neynar.com/v1/info', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'x-api-key': process.env.NEXT_PUBLIC_NEYNAR_API_KEY!,
},
});
const result = await response.json();
console.log("Snapchain info:", result);
} catch (error) {
console.error("Failed to fetch Snapchain info:", error);
}
}
return (
<>
<textarea value={text} onChange={(e) => setText(e.target.value)} />
<button onClick={sendCast} disabled={!farcasterAccount}>Send Cast</button>
<button
onClick={() => requestFarcasterSignerFromWarpcast()}
//disabled={!farcasterAccount || Boolean(farcasterAccount.signerPublicKey)}
>
Authorize my Farcaster signer from Warpcast
</button>
<button onClick={fetchSnapchainInfo}>Test Snapchain Info</button>
</>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment