Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
init getConnectedAddresses
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelLHuber committed Mar 6, 2024
1 parent 5b19f4f commit 2cda7dd
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 4 deletions.
89 changes: 89 additions & 0 deletions src/functions/getConnectedAddresses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { getSSLHubRpcClient, Message } from "@farcaster/hub-nodejs";

import { Provider, HubProvider, NeynarProvider } from "../providers";
import { ConnectedAddresses } from "../types";

export default function getConnectedAddresses(provider: Provider, fid: number, ethereum?: boolean, solana?: boolean): ConnectedAddresses {
let addresses: ConnectedAddresses;

if(provider instanceof HubProvider) {
if(provider.psqlUrl)
addresses = getConnectedAddressesFromReplicator(provider.psqlUrl, fid, ethereum, solana);
else
addresses = getConnectedAddressesFromHub(provider.hubUrl, fid, ethereum, solana);
}
else if( provider instanceof NeynarProvider) {
addresses = getConnectedAddressesFromNeynar(provider, fid, ethereum, solana);
}
else {
throw new Error("Provider not supported");
}

return addresses;
}

function getConnectedAddressesFromReplicator(psqlUrl: string, fid: number, ethereum?: boolean, solana?: boolean): ConnectedAddresses {
let addresses: ConnectedAddresses = {
all: [],
ethereum: [],
solana: [],
};
// ...
throw new Error("Not implemented");
}

/**
* Get connected addresses for a given FID from a hub
* @param hubUrl the hubUrl to be queried
* @param fid the fid for which the connected addresses are to be queried
* @param ethereum is ignored as hubs return all addresses anyway
* @param solana is ignored as hubs return all addresses anyway
* @returns ConnectedAddresses for the qiven fid
*/
function getConnectedAddressesFromHub(hubUrl: string, fid: number, ethereum?: boolean, solana?: boolean): ConnectedAddresses {
let addresses: ConnectedAddresses = {
all: [],
ethereum: [],
solana: [],
};

const client = getSSLHubRpcClient(hubUrl);

try {
const verificationResponse = client.getVerificationsByFid({fid: fid});

if (verificationResponse.isOk() && verificationResponse.value) {
verificationResponse.value.messages.forEach((verification) => {

if(verification.data?.verificationAddAddressBody?.protocol === 0){ // protocol === 0 guarantees only ETH addresses
const addressBytes = verification.data?.verificationAddAddressBody.address;
const address = `0x${Buffer.from(addressBytes).toString('hex')}`;
addresses.all.push(address);
addresses.ethereum.push(address);
}

if(verification.data?.verificationAddAddressBody?.protocol === 1){ // protocol === 1 guarantees only SOL addresses
const addressBytes = verification.data?.verificationAddAddressBody.address;
const address = `${Buffer.from(addressBytes).toString('hex')}`;
addresses.all.push(address);
addresses.solana.push(address);
}

});
}
} catch (e) {
console.error(e);
throw new Error("Error getting verifications from hub", e);
}
return addresses;
}

function getConnectedAddressesFromNeynar(provider: NeynarProvider, fid: number, ethereum?: boolean, solana?: boolean): ConnectedAddresses {
let addresses: ConnectedAddresses = {
all: [],
ethereum: [],
solana: [],
};
// ...
throw new Error("Not implemented");
}
10 changes: 6 additions & 4 deletions types/types.d.ts → src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ export type FarcasterUser = {
fid?: number;
}

export type ConnectedAddresses = {
all: string[],
ethereum: string[],
solana: string[]
}

export type Channel = {
name: string;
parent_url: string;
image: string;
channel_id: string;
lead_fid?: number;
}



// Path: types/types.d.ts

0 comments on commit 2cda7dd

Please sign in to comment.