a16z领投3000万美元的去中心化社交协议Farcaster教程

使用 replit ,它是一个基于 IDE 进行编程的浏览器。

1.在 repli 上注册一个免费帐户并登录;

2.点击左上角的create创建;

post image

3.选择nods.js

post image

4.npm install ethers got@11.8.2

这将安装 ethers ,一个用于与 Ethereum 一起工作的库, got ,一个用于发出 HTTP 请求的库。 您可能会看到一些关于缺少 package.json 的警告,您可以忽略这些警告。

post image

5.这样就算安装成功.

post image

6.还需要一个以太坊节点来与 Facaster Registry 合约对话。 建议使用 Alchemy 。 如果您是第一次注册,以下步骤可能会略有不同:

7.注册 Alchemy.com ,登录

8.选择以太坊作为区块链,点击get started

post image

9.team name和app name随便取,网络选择Rinkeby,点击Create APP。

post image

10.选择第一个免费的,点击continue。

post image

11.点击跳过。

post image

12.继续点击跳过。

post image

13.点击continue。

post image

14.随便输入什么,点击let‘s go。

post image

15.点击view details。

post image

16.点击view key。

post image

17.复制代码,把箭头标记的xxxxxxxxxxxxxxxxxx处换成你alchemy的api key:

post image

18.把下面代码复制粘贴到index.js这个文件下,run运行。

const { providers, Contract, utils } = require("ethers"); const got = require("got");

const doStuff = async () => { const ALCHEMY_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; const provider = new providers.AlchemyProvider('rinkeby', ALCHEMY_SECRET);

const block = await provider.getBlockNumber();

console.log("The latest Ethereum block is:", block);

const REGISTRY_CONTRACT_ADDRESS = '0xe3Be01D99bAa8dB9905b33a3cA391238234B79D1' const REGISTRY_ABI = [ { name: 'getDirectoryUrl', inputs: [{ internalType: 'bytes32', name: 'username', type: 'bytes32' }], outputs: [{ internalType: 'string', name: '', type: 'string'}], stateMutability: 'view', type: 'function', }, { inputs: [{ internalType: 'address', name: '', type: 'address' }], name: 'addressToUsername', outputs: [{ internalType: 'bytes32', name: '', type: 'bytes32' }], stateMutability: 'view', type: 'function', }, ];

const registryContract = new Contract(REGISTRY_CONTRACT_ADDRESS, REGISTRY_ABI, provider);

const username = 'v'; const byte32Name = utils.formatBytes32String(username); const directoryUrl = await registryContract.getDirectoryUrl(byte32Name); console.log(`${username}'s Host is located at: ${directoryUrl} \n`);

const directoryResponse = await got(directoryUrl); const directory = JSON.parse(directoryResponse.body); console.log(`${username}'s Directory is: `); console.log(directory, '\n');

const addressActivityUrl = directory.body.addressActivityUrl; const addressActivityResponse = await got(addressActivityUrl); const addressActivity = JSON.parse(addressActivityResponse.body); const cast = addressActivity[0]; console.log(`${username}'s most recent Cast was: `) console.log(cast, '\n')

const stringifiedCastBody = JSON.stringify(cast.body); const calculatedHash = utils.keccak256(utils.toUtf8Bytes(stringifiedCastBody)); const expectedHash = cast.merkleRoot;

if (calculatedHash !== expectedHash) { console.log(`FAILED: the calculated hash ${calculatedHash} does not match the one in the cast: ${expectedHash}`); } else { console.log(`PASSED: the calculated hash ${calculatedHash} matches the one in the cast`); }

const recoveredAddress = utils.verifyMessage(cast.merkleRoot, cast.signature); const expectedAddress = cast.body.address;

if (recoveredAddress !== expectedAddress) { console.log( `Failed: the recovered address ${recoveredAddress} does not match the address provided in the cast ${expectedAddress}` ); } else { console.log(`PASSED: the recovered address ${recoveredAddress} matches the one in the cast`); }

const encodedUsername = await registryContract.addressToUsername(expectedAddress); const expectedUsername = utils.parseBytes32String(encodedUsername); const castUsername = cast.body.username;

if (expectedUsername !== castUsername) { console.log(`FAILED: ${expectedAddress} does not own ${castUsername}, it owns ${expectedUsername}`); } else { console.log(`PASSED: ${expectedAddress} owns ${castUsername}`); } }

doStuff();

19.运行得到以下结果,祝贺你成功了。

post image

官网教程:

https://www.farcaster.xyz/docs/fetch-casts

如果有什么不懂得。可以推特私信@LuobuH