Name Service

Name registry

Name registry는 도메인 이름에 대한 정보를 저장합니다. 이것은 두 가지로 구성되어 있습니다:

  • The header
  • The data

도메인 이름을 위한 데이터는 항상 header의 앞에 붙습니다. 아래는 JS코드로 header 구조체를 보여줍니다:

Press </> button to view full source
import { PublicKey } from "@solana/web3.js";
import { Schema } from "borsh";

export class NameRegistryState {
  parentName: PublicKey;
  owner: PublicKey;
  class: PublicKey;
  data: Buffer | undefined;

  static HEADER_LEN = 96;

  static schema: Schema = new Map([
    [
      NameRegistryState,
      {
        kind: "struct",
        fields: [
          ["parentName", [32]],
          ["owner", [32]],
          ["class", [32]],
        ],
      },
    ],
  ]);
  constructor(obj: {
    parentName: Uint8Array;
    owner: Uint8Array;
    class: Uint8Array;
  }) {
    this.parentName = new PublicKey(obj.parentName);
    this.owner = new PublicKey(obj.owner);
    this.class = new PublicKey(obj.class);
  }
}

Resolving SOL domains

.SOL 도메인들은 유니크한 publicKey들로 전환되는 인간 친화적인 도메인 네임들입니다. 많은 지갑들이 이것들을 Token과 SOL을 보내기 위한 또 다른 옵션으로 사용합니다. 당신은 .SOL 도메인들을 아래와 같이 publicKey로 변환할 수 있습니다.

Press </> button to view full source
import {
  getHashedName,
  getNameAccountKey,
  NameRegistryState,
} from "@bonfida/spl-name-service";

import { Connection, clusterApiUrl, PublicKey } from "@solana/web3.js";

(async () => {
  const domain = "levi.sol";
  const hashedName = await getHashedName(domain.replace(".sol", ""));
  const nameAccountKey = await getNameAccountKey(
    hashedName,
    undefined,
    new PublicKey("58PwtjSDuFHuUkYjH9BYnnQKHfwo9reZhC2zMJv9JPkx") // SOL TLD Authority
  );
  const owner = await NameRegistryState.retrieve(
    new Connection(clusterApiUrl("mainnet-beta")),
    nameAccountKey
  );
  console.log(owner.registry.owner.toBase58());
  // JUskoxS2PTiaBpxfGaAPgf3cUNhdeYFGMKdL6mZKKfR
})();

Reverse look up

아래와 같이 public key로부터 도메인 이름을 알아내기 위해 사용될 수 있습니다.

Press </> button to view full source
import { performReverseLookup } from "@bonfida/spl-name-service";
import { PublicKey, Connection, clusterApiUrl } from "@solana/web3.js";

async () => {
  const connection = new Connection(clusterApiUrl("mainnet-beta"));
  // Public key of bonfida.sol
  const domainKey = new PublicKey(
    "Crf8hzfthWGbGbLTVCiqRqV5MVnbpHB1L9KQMd6gsinb"
  );

  const domainName = await performReverseLookup(connection, domainKey); // bonfida
};

Subdomain look up

서브 도메인을 알아내기 위해 당신이 필요한 것들:

  1. 부모 도메인의 Key를 얻으세요.
  2. 서브 도메인의 Key를 얻으세요.
  3. Account 정보를 조회하세요.
Press </> button to view full source
import {
  getHashedName,
  getNameAccountKey,
  NameRegistryState,
  getDNSRecordAddress,
} from "@bonfida/spl-name-service";
import { Connection, clusterApiUrl, PublicKey } from "@solana/web3.js";

async () => {
  const SOL_TLD_AUTHORITY = new PublicKey(
    "58PwtjSDuFHuUkYjH9BYnnQKHfwo9reZhC2zMJv9JPkx"
  );
  const connection = new Connection(clusterApiUrl("mainnet-beta"));

  // Resolution of demo.bonfida.sol

  const parentDomain = "bonfida";
  const subDomain = "demo";

  // Step 1
  const hashedParentDomain = await getHashedName(parentDomain);
  const parentDomainKey = await getNameAccountKey(
    hashedParentDomain,
    undefined,
    SOL_TLD_AUTHORITY
  );

  // Step 2
  const subDomainKey = await getDNSRecordAddress(parentDomainKey, subDomain);

  // Step 3
  const registry = await NameRegistryState.retrieve(connection, subDomainKey);
};

Find all the domain names owned by a public key

당신은 memcmp 필터와 함께 getProgramAccount를 요청해서 지갑의 모든 도메인 네임들을 조회할 수 있습니다.

Press </> button to view full source
import { Connection, PublicKey } from "@solana/web3.js";
import { NAME_PROGRAM_ID } from "@bonfida/spl-name-service";

export async function findOwnedNameAccountsForUser(
  connection: Connection,
  userAccount: PublicKey
): Promise<PublicKey[]> {
  const filters = [
    {
      memcmp: {
        offset: 32,
        bytes: userAccount.toBase58(),
      },
    },
  ];
  const accounts = await connection.getProgramAccounts(NAME_PROGRAM_ID, {
    filters,
  });
  return accounts.map((a) => a.publicKey);
}

Resolve a Twitter handle

Twitter handles는 Solana name service에 등록open in new window될 수 있고 .SOL 도메인 네임들처럼 사용될 수 있습니다.

Press </> button to view full source
import { PublicKey, clusterApiUrl, Connection } from "@solana/web3.js";
import { getHandleAndRegistryKey } from "@bonfida/spl-name-service";

async () => {
  const connection = new Connection(clusterApiUrl("mainnet-beta"));
  // Pubkey of the wallet you want to retrieve the Twitter handle
  const pubkey = new PublicKey("FidaeBkZkvDqi1GXNEwB8uWmj9Ngx2HXSS5nyGRuVFcZ");

  const [handle, registryKey] = await getHandleAndRegistryKey(
    connection,
    pubkey
  );
};

Reverse look up of a Twitter handle

한 Twitter handle과 연관된 SOL address를 찾기 위해서 당신은 reverse look up을 수행할 수 있습니다.

Press </> button to view full source
import { getTwitterRegistry } from "@bonfida/spl-name-service";
import { Connection, clusterApiUrl } from "@solana/web3.js";

async () => {
  const handle = "bonfida";
  const connection = new Connection(clusterApiUrl("mainnet-beta"));

  const registry = await getTwitterRegistry(connection, handle);
};
Last Updated:
Contributors: TaeGit