Skip to content

Instantly share code, notes, and snippets.

@danromero
Last active September 26, 2024 00:25
Show Gist options
  • Save danromero/87be7035aab27bf6a603b2c956022370 to your computer and use it in GitHub Desktop.
Save danromero/87be7035aab27bf6a603b2c956022370 to your computer and use it in GitHub Desktop.
Generate an Application Bearer Token from a Farcaster custody address mnemonic for Merkle v2 API
const ethers = require('ethers');
const canonicalize = require('canonicalize');
const https = require('https');
const myMnemonic = 'a b c d e f g h i j k';
async function run() {
// WARNING: Example only -- do not ever hard-code your real mnemonic!
wallet = ethers.Wallet.fromMnemonic(myMnemonic);
const currentTimestamp = Date.now(); // get the current timestamp
const payload = canonicalize({
method: 'generateToken',
params: {
timestamp: currentTimestamp,
},
});
const signedPayload = await(wallet.signMessage(payload));
const signature = Buffer.from(ethers.utils.arrayify(signedPayload)).toString('base64');
const bearerToken = `eip191:${signature}`;
// console.log(`Custody bearer token: ${bearerToken}`);
// make an HTTP request with the bearer token
const options = {
hostname: 'api.farcaster.xyz',
path: '/v2/auth',
method: 'PUT',
headers: {
'Authorization': `Bearer ${bearerToken}`,
'Content-Type': 'application/json',
}
};
const req = https.request(options, (res) => {
let body = '';
res.on('data', chunk => {
body += chunk;
});
res.on('end', () => {
// Parse the response body as JSON
const json = JSON.parse(body);
const appBearerToken = `${json.result.token.secret}`;
// Log the parsed JSON
console.log(`Application bearer token: ${appBearerToken}`);
});
});
req.on('error', (error) => {
// log any errors that occur when making the request
console.error(`Request error: ${error.message}`);
});
req.write(JSON.stringify({
method: 'generateToken',
params: {
timestamp: currentTimestamp,
}
}));
req.end();
}
run();
@volkyeth
Copy link

Update: On line 30, replace the hostname api.farcaster.xyz by the new one api.warpcast.com

@Meliodasho
Copy link

Is this code to solve EIP-191 Timestamp Bearer token error at Warpcast. How to use this code? Can you guide me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment