Skip to content

Commit

Permalink
feat: added initial version of getFrameAccountAddress, which helps …
Browse files Browse the repository at this point in the history
…getting the Account Address from the Farcaster ID using the Frame. (#20)
  • Loading branch information
Zizzamia authored Jan 28, 2024
1 parent cca9ee2 commit 398933b
Show file tree
Hide file tree
Showing 6 changed files with 1,090 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-poems-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@coinbase/onchainkit': patch
---

- **feat**: added initial version of `getFrameAccountAddress`, which helps getting the Account Address from the Farcaster ID using the Frame.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

> OnchainKit is a collection of CSS, React components and Core utilities specifically crafted to enhance your creativity when building onchain applications.

## Getting Started
Add OnchainKit to your project, install the required packages.

Add OnchainKit to your project, install the required packages.

```bash
# Use Yarn
Expand Down Expand Up @@ -53,6 +52,7 @@ export default function Page() {
```

`getFrameMetadata` params
- `buttons`: A list of strings which are the label for the buttons in the frame (max 4 buttons).

- `buttons`: A list of strings which are the label for the buttons in the frame (max 4 buttons).
- `image`: An image which must be smaller than 10MB and should have an aspect ratio of 1.91:1
- `post_url`: A valid POST URL to send the Signature Packet to.
- `post_url`: A valid POST URL to send the Signature Packet to.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"repository": "https://github.com/coinbase/onchainkit.git",
"license": "MIT",
"dependencies": {
"@farcaster/hub-nodejs": "0.10.21",
"react": "^18",
"viem": "2.5.0"
},
Expand Down
53 changes: 53 additions & 0 deletions src/core/getFrameAccountAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { HubRpcClient, Message, getSSLHubRpcClient } from '@farcaster/hub-nodejs';

// URL of the Hub
const HUB_URL = 'nemes.farcaster.xyz:2283';
// Create a Hub RPC client
const client: HubRpcClient = getSSLHubRpcClient(HUB_URL);

type FidResponse = {
verifications: string[];
};

/**
* Get the Account Address from the Farcaster ID using the Frame.
* @param body
* @param param1
* @returns
*/
async function getFrameAccountAddress(
body: { trustedData?: { messageBytes?: string } },
{ NEYNAR_API_KEY = 'NEYNAR_API_DOCS' },
) {
let farcasterID = 0;
let validatedMessage: Message | undefined = undefined;
// Get the message from the request body
const frameMessage: Message = Message.decode(
Buffer.from(body?.trustedData?.messageBytes ?? '', 'hex'),
);
// Validate the message
const result = await client.validateMessage(frameMessage);
if (result.isOk() && result.value.valid && result.value.message) {
validatedMessage = result.value.message;
}
// Get the Farcaster ID from the message
farcasterID = validatedMessage?.data?.fid ?? 0;
// Get the user verifications from the Farcaster Indexer
const options = {
method: 'GET',
url: `https://api.neynar.com/v2/farcaster/user/bulk?fids=${farcasterID}`,
headers: { accept: 'application/json', api_key: NEYNAR_API_KEY },
};
const resp = await fetch(options.url, { headers: options.headers });
const responseBody = await resp.json();
// Get the user verifications from the response
if (responseBody.users) {
const userVerifications = responseBody.users[0] as FidResponse;
if (userVerifications.verifications) {
return userVerifications.verifications[0];
}
}
return '0x00';
}

export { getFrameAccountAddress };
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ☕️
const version = '0.1.2';
const version = '0.1.4';

// TODO remove this line from here
export { version };
export { getFrameAccountAddress } from './core/getFrameAccountAddress';
export { getFrameMetadata } from './core/getFrameMetadata';
Loading

0 comments on commit 398933b

Please sign in to comment.