Skip to content

Commit

Permalink
cf-worker: add record v2 + multiple fav domains endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dr497 committed Jan 24, 2024
1 parent c3b9c75 commit b7da7c0
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ The SDK proxy is a Cloudflare worker that proxies the JS SDK via REST calls. It'
- `GET /register?buyer={buyer}&domain={domain}&space={space}&serialize={serialize}`: This endpoint can be used to register `domain` for `buyer`. Additionaly, the `buyer` dans specify the `space` it wants to allocate for the `domain` account. In the case where `serialize` is `true` the endpoint will return the transaction serialized in the wire format base64 encoded. Otherwise it will return the instruction in the following format: `{ programId: string, keys: {isWritable: boolean, isSigner: boolean, pubkey: string}[], data: string }` where data is base64 encoded. This endpoint also supports the optional `mint` parameter to change the mint of the token used for registration (currently supports USDC, USDT, FIDA and wSOL), if `mint` is omitted it defaults to USDC.
- `GET /twitter/get-handle-by-key/:key`: This endpoint can be used to fetch the Twitter handle of a given public key
- `GET /twitter/get-key-by-handle/:handle`: This endpoint can be used to fetch the public key of a given Twitter handle
- `GET /multiple-favorite-domains/:owners`: Returns the favorite domains for a list of owners that are comma separated
- `GET /record-v2/:domain/:record`: Returns the content of the `record` (v2) of `domain`. The result is made of the deserialized value, staleness boolean (`stale`), right of association (`roa`) if applicable, and the record object made of its`header` and `data` (base64 encoded).

NOTE: All endpoints capable of performing RPC calls currently support an optional `rpc` query parameter for specifying a custom RPC URL. In the future, this parameter will become mandatory, and the Cloudflare worker will exclusively proxy calls to a specified custom RPC URL.

Expand Down
1 change: 1 addition & 0 deletions cf-worker/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cf-worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"deploy": "wrangler publish"
},
"dependencies": {
"@bonfida/sns-records": "^0.0.1",
"@bonfida/spl-name-service": "^2.3.3",
"@solana/spl-token": "^0.3.7",
"@solana/web3.js": "^1.73.3",
Expand Down
98 changes: 98 additions & 0 deletions cf-worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ import {
RecordVersion,
getTwitterRegistry,
getHandleAndRegistryKey,
getRecordV2,
getMultipleFavoriteDomains,
NameRegistryState,
GUARDIANS,
} from "@bonfida/spl-name-service";
import { Connection, PublicKey, Transaction } from "@solana/web3.js";
import { getAssociatedTokenAddress } from "@solana/spl-token";
import { cors } from "hono/cors";
import { cache } from "hono/cache";
import { logger } from "hono/logger";
import { z } from "zod";
import { Validation } from "@bonfida/sns-records";

const USDC_MINT = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";

Expand Down Expand Up @@ -199,6 +204,78 @@ app.get("/record/:domain/:record", async (c) => {
}
});

/**
* Returns the base64 encoded content of a record for the specified domain
*/
app.get("/record-v2/:domain/:record", async (c) => {
try {
const Query = z.object({
domain: z.string(),
record: z.nativeEnum(Record),
rpc: z.string().optional(),
});
const { domain, record, rpc } = Query.parse(c.req.param());
const connection = getConnection(c, rpc);
const { registry } = await NameRegistryState.retrieve(
connection,
getDomainKeySync(domain).pubkey
);
const owner = registry.owner;
const res = await getRecordV2(connection, domain, record, {
deserialize: true,
});

const stale = !res.retrievedRecord
.getStalenessId()
.equals(owner.toBuffer());

let roa = undefined;

if (Record.SOL === record) {
roa =
res.retrievedRecord
.getRoAId()
.equals(res.retrievedRecord.getContent()) &&
res.retrievedRecord.header.rightOfAssociationValidation ===
Validation.Solana;
} else if ([Record.ETH, Record.BSC, Record.Injective].includes(record)) {
roa =
res.retrievedRecord
.getRoAId()
.equals(res.retrievedRecord.getContent()) &&
res.retrievedRecord.header.rightOfAssociationValidation ===
Validation.Ethereum;
} else if ([Record.Url]) {
const guardian = GUARDIANS.get(record);
if (guardian) {
roa =
res.retrievedRecord.getRoAId().equals(guardian.toBuffer()) &&
res.retrievedRecord.header.rightOfAssociationValidation ===
Validation.Solana;
}
}

return c.json(
response(true, {
deserialized: res.deserializedContent,
stale,
roa,
record: {
header: res.retrievedRecord.header,
data: res.retrievedRecord.data.toString("base64"),
},
})
);
} catch (err) {
console.log(err);
if (err instanceof z.ZodError) {
return c.json(response(false, "Invalid input"), 400);
} else {
return c.json(response(false, "Internal error"), 500);
}
}
});

/**
* Returns the favorite domain for the specified owner and null if it does not exist
*/
Expand All @@ -224,6 +301,27 @@ app.get("/favorite-domain/:owner", async (c) => {
}
});

/**
* Returns the favorite domain for the specified owners (comma separated) and undefined if it does not exist
*/
app.get("/multiple-favorite-domains/:owners", async (c) => {
try {
const { owners } = c.req.param();
const rpc = c.req.query("rpc");
const parsed = owners.split(",").map((e) => new PublicKey(e));
const res = await getMultipleFavoriteDomains(getConnection(c, rpc), parsed);
return c.json(response(true, res));
} catch (err) {
console.log(err);
if (err instanceof Error) {
if (err.message.includes("Favourite domain not found")) {
return c.json(response(true, null));
}
}
return c.json(response(false, "Invalid domain input"));
}
});

/**
* Returns the list of supported records
*/
Expand Down

0 comments on commit b7da7c0

Please sign in to comment.