Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve findsubdomain #46

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions js/package-lock.json

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

2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bonfida/spl-name-service",
"version": "2.3.1",
"version": "2.3.2",
"license": "MIT",
"files": [
"dist"
Expand Down
39 changes: 30 additions & 9 deletions js/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const findSubdomains = async (
parentKey: PublicKey,
): Promise<string[]> => {
// Fetch reverse accounts
const filtersReverse: MemcmpFilter[] = [
const filtersRevs: MemcmpFilter[] = [
{
memcmp: {
offset: 0,
Expand All @@ -126,19 +126,40 @@ export const findSubdomains = async (
},
},
];
const reverse = await connection.getProgramAccounts(NAME_PROGRAM_ID, {
filters: filtersReverse,
const reverses = await connection.getProgramAccounts(NAME_PROGRAM_ID, {
filters: filtersRevs,
});

const parent = await reverseLookup(connection, parentKey);
const subs = reverse.map(
(e) => e.account.data.slice(97).toString("utf-8")?.split("\0").join(""),
const filtersSubs: MemcmpFilter[] = [
{
memcmp: {
offset: 0,
bytes: parentKey.toBase58(),
},
},
];
const subs = await connection.getProgramAccounts(NAME_PROGRAM_ID, {
filters: filtersSubs,
dataSlice: { offset: 0, length: 0 },
});

const map = new Map<string, string | undefined>(
reverses.map((e) => [
e.pubkey.toBase58(),
deserializeReverse(e.account.data.slice(96)),
]),
);

const keys = subs.map((e) => getDomainKeySync(e + "." + parent).pubkey);
const subsAcc = await connection.getMultipleAccountsInfo(keys);
const result: string[] = [];
subs.forEach((e) => {
const revKey = getReverseKeyFromDomainKey(e.pubkey, parentKey).toBase58();
const rev = map.get(revKey);
if (!!rev) {
result.push(rev.replace("\0", ""));
}
});

return subs.filter((_, idx) => !!subsAcc[idx]);
return result;
};

const _deriveSync = (
Expand Down
18 changes: 14 additions & 4 deletions js/tests/sub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Connection, PublicKey, Transaction } from "@solana/web3.js";
import { createSubdomain, transferSubdomain } from "../src/bindings";
import { randomBytes } from "crypto";
import { VAULT_OWNER } from "../src/constants";
import { findSubdomains, getDomainKeySync } from "../src/utils";

jest.setTimeout(20_000);

Expand All @@ -15,7 +16,7 @@ test("Create sub", async () => {
connection,
randomBytes(10).toString("hex") + ".bonfida",
new PublicKey("HKKp49qGWXd639QsuH7JiLijfVW5UtCVY4s1n2HANwEA"),
2_000
2_000,
);
tx.add(...ix);
const { blockhash } = await connection.getLatestBlockhash();
Expand All @@ -29,13 +30,13 @@ test("Transfer sub", async () => {
let tx = new Transaction();
const owner = new PublicKey("J6QDztZCegYTWnGUYtjqVS9d7AZoS43UbEQmMcdGeP5s");
const parentOwner = new PublicKey(
"J6QDztZCegYTWnGUYtjqVS9d7AZoS43UbEQmMcdGeP5s"
"J6QDztZCegYTWnGUYtjqVS9d7AZoS43UbEQmMcdGeP5s",
);
let ix = await transferSubdomain(
connection,
"test.0x33.sol",
PublicKey.default,
false
false,
);
tx.add(ix);
let blockhash = (await connection.getLatestBlockhash()).blockhash;
Expand All @@ -49,7 +50,7 @@ test("Transfer sub", async () => {
connection,
"test.0x33.sol",
PublicKey.default,
true
true,
);
tx.add(ix);
blockhash = (await connection.getLatestBlockhash()).blockhash;
Expand All @@ -58,3 +59,12 @@ test("Transfer sub", async () => {
res = await connection.simulateTransaction(tx);
expect(res.value.err).toBe(null);
});

test("Find sub domain", async () => {
const subs = await findSubdomains(
connection,
getDomainKeySync("bonfida").pubkey,
);
const expectedSub = ["dex", "naming", "test"];
subs.sort().forEach((e, idx) => expect(e).toBe(expectedSub[idx]));
});
Loading