UNPKG

@solana/spl-name-service

Version:

SPL Name Service JavaScript API

161 lines 8.1 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.reallocNameAccount = exports.deleteNameRegistry = exports.transferNameOwnership = exports.updateNameRegistryData = exports.createNameRegistry = exports.HASH_PREFIX = exports.NAME_PROGRAM_ID = void 0; const web3_js_1 = require("@solana/web3.js"); const instructions_1 = require("./instructions"); const state_1 = require("./state"); const utils_1 = require("./utils"); const utils_2 = require("./utils"); //////////////////////////////////////////////////////////// exports.NAME_PROGRAM_ID = new web3_js_1.PublicKey('namesLPneVptA9Z5rqUDD9tMTWEJwofgaYwp8cawRkX'); exports.HASH_PREFIX = 'SPL Name Service'; //////////////////////////////////////////////////////////// /** * Creates a name account with the given rent budget, allocated space, owner and class. * * @param connection The solana connection object to the RPC node * @param name The name of the new account * @param space The space in bytes allocated to the account * @param payerKey The allocation cost payer * @param nameOwner The pubkey to be set as owner of the new name account * @param lamports The budget to be set for the name account. If not specified, it'll be the minimum for rent exemption * @param nameClass The class of this new name * @param parentName The parent name of the new name. If specified its owner needs to sign * @returns */ function createNameRegistry(connection, name, space, payerKey, nameOwner, lamports, nameClass, parentName) { return __awaiter(this, void 0, void 0, function* () { const hashed_name = yield (0, utils_2.getHashedName)(name); const nameAccountKey = yield (0, utils_2.getNameAccountKey)(hashed_name, nameClass, parentName); const balance = lamports ? lamports : yield connection.getMinimumBalanceForRentExemption(space); let nameParentOwner; if (parentName) { const parentAccount = yield (0, utils_2.getNameOwner)(connection, parentName); nameParentOwner = parentAccount.owner; } const createNameInstr = (0, instructions_1.createInstruction)(exports.NAME_PROGRAM_ID, web3_js_1.SystemProgram.programId, nameAccountKey, nameOwner, payerKey, hashed_name, new utils_1.Numberu64(balance), new utils_2.Numberu32(space), nameClass, parentName, nameParentOwner); return createNameInstr; }); } exports.createNameRegistry = createNameRegistry; /** * Overwrite the data of the given name registry. * * @param connection The solana connection object to the RPC node * @param name The name of the name registry to update * @param offset The offset to which the data should be written into the registry * @param input_data The data to be written * @param nameClass The class of this name, if it exsists * @param nameParent The parent name of this name, if it exists */ function updateNameRegistryData(connection, name, offset, input_data, nameClass, nameParent) { return __awaiter(this, void 0, void 0, function* () { const hashed_name = yield (0, utils_2.getHashedName)(name); const nameAccountKey = yield (0, utils_2.getNameAccountKey)(hashed_name, nameClass, nameParent); let signer; if (nameClass) { signer = nameClass; } else { signer = (yield state_1.NameRegistryState.retrieve(connection, nameAccountKey)) .owner; } const updateInstr = (0, instructions_1.updateInstruction)(exports.NAME_PROGRAM_ID, nameAccountKey, new utils_2.Numberu32(offset), input_data, signer, nameParent); return updateInstr; }); } exports.updateNameRegistryData = updateNameRegistryData; /** * Change the owner of a given name account. * * @param connection The solana connection object to the RPC node * @param name The name of the name account * @param newOwner The new owner to be set * @param curentNameOwner the current name Owner * @param nameClass The class of this name, if it exsists * @param nameParent The parent name of this name, if it exists * @returns */ function transferNameOwnership(connection, name, newOwner, nameClass, nameParent) { return __awaiter(this, void 0, void 0, function* () { const hashed_name = yield (0, utils_2.getHashedName)(name); const nameAccountKey = yield (0, utils_2.getNameAccountKey)(hashed_name, nameClass, nameParent); let curentNameOwner; if (nameClass) { curentNameOwner = nameClass; } else { curentNameOwner = (yield state_1.NameRegistryState.retrieve(connection, nameAccountKey)).owner; } const transferInstr = (0, instructions_1.transferInstruction)(exports.NAME_PROGRAM_ID, nameAccountKey, newOwner, curentNameOwner, nameClass, nameParent); return transferInstr; }); } exports.transferNameOwnership = transferNameOwnership; /** * Delete the name account and transfer the rent to the target. * * @param connection The solana connection object to the RPC node * @param name The name of the name account * @param refundTargetKey The refund destination address * @param nameClass The class of this name, if it exsists * @param nameParent The parent name of this name, if it exists * @returns */ function deleteNameRegistry(connection, name, refundTargetKey, nameClass, nameParent) { return __awaiter(this, void 0, void 0, function* () { const hashed_name = yield (0, utils_2.getHashedName)(name); const nameAccountKey = yield (0, utils_2.getNameAccountKey)(hashed_name, nameClass, nameParent); let nameOwner; if (nameClass) { nameOwner = nameClass; } else { nameOwner = (yield state_1.NameRegistryState.retrieve(connection, nameAccountKey)) .owner; } const changeAuthoritiesInstr = (0, instructions_1.deleteInstruction)(exports.NAME_PROGRAM_ID, nameAccountKey, refundTargetKey, nameOwner); return changeAuthoritiesInstr; }); } exports.deleteNameRegistry = deleteNameRegistry; /** * Realloc the name account space. * * @param connection The solana connection object to the RPC node * @param name The name of the name account * @param space The new space to be allocated * @param payerKey The allocation cost payer if new space is larger than current or the refund destination if smaller * @param nameClass The class of this name, if it exsists * @param nameParent The parent name of this name, if it exists * @returns */ function reallocNameAccount(connection, name, space, payerKey, nameClass, nameParent) { return __awaiter(this, void 0, void 0, function* () { const hashedName = yield (0, utils_2.getHashedName)(name); const nameAccountKey = yield (0, utils_2.getNameAccountKey)(hashedName, nameClass, nameParent); let nameOwner; if (nameClass) { nameOwner = nameClass; } else { nameOwner = (yield state_1.NameRegistryState.retrieve(connection, nameAccountKey)) .owner; } const reallocInstr = (0, instructions_1.reallocInstruction)(exports.NAME_PROGRAM_ID, web3_js_1.SystemProgram.programId, payerKey, nameAccountKey, nameOwner, new utils_2.Numberu32(space)); return reallocInstr; }); } exports.reallocNameAccount = reallocNameAccount; //# sourceMappingURL=bindings.js.map