Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Merge pull request #45 from solana-labs/solana-wallet-names
Browse files Browse the repository at this point in the history
Add wallet name lookup
  • Loading branch information
ngundotra authored Aug 4, 2023
2 parents 3d2ae00 + 7b6ff9f commit 52394b6
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions chatgpt-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@hellomoon/api": "^1.16.7",
"@heroicons/react": "^2.0.18",
"@metaplex-foundation/js": "^0.19.3",
"@portal-payments/solana-wallet-names": "^2.1.0",
"@solana/pay": "^0.2.5",
"@solana/spl-token": "^0.3.8",
"@solana/web3.js": "^1.77.3",
Expand Down
97 changes: 97 additions & 0 deletions chatgpt-plugin/src/pages/api/handlers/walletName/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { NextApiRequest, NextApiResponse } from "next";
import { PublicKey, Connection } from "@solana/web3.js";
import {
walletNameToAddressAndProfilePicture,
walletAddressToDotAnything,
getProfilePictureUsingSolanaPFPStandard,
walletAddressToDotGlow,
walletAddressToDotBackpack,
} from "@portal-payments/solana-wallet-names";
import configConstants, { CONNECTION } from "../../constants";
configConstants();

interface WalletNames {
walletNames: string[];
}

const walletAddressToDotSolCustom = async (
connection: Connection,
wallet: PublicKey,
): Promise<WalletNames> => {
try {
const result = await fetch(
// See https://github.com/Bonfida/sns-sdk#sdk-proxy
// There's a 'favorite-domain' endpoint butmost SNS users haven't set up a
// favorite domain, as the UI to do so is complex
// `https://sns-sdk-proxy.bonfida.workers.dev/favorite-domain/${wallet.toBase58()}`
`https://sns-sdk-proxy.bonfida.workers.dev/domains/${wallet.toBase58()}`,
{
method: "GET",
},
);

const body = await result.json();
let walletNames: string[] = body.result.map((info: any) => `${info.domain}.sol`);
return {
walletNames,
};
} catch (thrownObject) {
const error = thrownObject as Error;
if (error.message === "Invalid wallet account provided") {
return {
walletNames: [],
};
}
throw error;
}
};

const walletAddressToNameAndProfilePictureCustom = async (
connection: Connection,
wallet: PublicKey,
backpackJWT: string | null = null,
): Promise<WalletNames> => {
let walletNames: string[] = [];
const dotAnything = await walletAddressToDotAnything(connection, wallet);
if (dotAnything.walletName) {
walletNames.push(dotAnything.walletName);
}

const dotSol = await walletAddressToDotSolCustom(connection, wallet);
walletNames = walletNames.concat(dotSol.walletNames);

const dotGlow = await walletAddressToDotGlow(wallet);
if (dotGlow?.walletName && dotGlow.walletName !== "null.glow") {
walletNames.push(dotGlow.walletName);
}

if (backpackJWT) {
const dotBackpack = await walletAddressToDotBackpack(wallet, backpackJWT);
if (dotBackpack?.walletName) {
walletNames.push(dotBackpack.walletName);
}
}

return {
walletNames,
};
};

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method != "POST") {
res.status(405).send({ message: "Only POST requests allowed" });
return;
}

const { walletName } = req.body;
try {
const dotAnything = await walletAddressToNameAndProfilePictureCustom(
CONNECTION,
new PublicKey(walletName),
);
res.status(200).send(dotAnything);
} catch (error) {
const walletInfo = await walletNameToAddressAndProfilePicture(CONNECTION, walletName);
res.status(200).send(walletInfo);
}
}

1 comment on commit 52394b6

@vercel
Copy link

@vercel vercel bot commented on 52394b6 Aug 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.