@hkns/hknsjs v0.0.7
Use hknsjs SDK to interact with HID contracts
HID.js
hknsjs integrates the HID contract and ENS and supports all the ENSjs APIs, you will only need one unified SDK to integrate all domains across multiple chains. hknsjs will hide all the complicated cross-chain detail from the partners, making the integration very easy.
Overview of the API
Installation
Install @hkns/hknsjs, alongside web3.
npm install @hkns/hknsjs web3
or
yarn add @hkns/hknsjs
yarn add web3
Getting Started
All that's needed to get started is a web3 provider instance, you should pass it and select network id when creating a new HID instance.
// Mumbai Testnet domain example
const HID = require("@hkns/hknsjs").default;
const RPC = require("@hkns/hknsjs/dist/constants/rpc");
const HIDfunctions = require("@hkns/hknsjs/dist/index");
const Web3 = require("web3");
let hid;
async function main(name) {
const provider = new Web3.providers.HttpProvider(RPC.apis.mig_mainnet);
hid = new HID({ provider, getHidAddress: HIDfunctions.getHidAddress("80001") }); //chainId
const address = await hid.name(name).getAddress("60"); // coidId
console.log("name: %s, address: %s", name, address);
}
main("xuran12.hk");
// Mumbai Testnet domain example
async function mainName(address) {
const provider = new Web3.providers.HttpProvider(RPC.apis.mig_mainnet);
hid = new HID({ provider, getResolverAddress: HIDfunctions.getResolverAddress("80001") });
const name = await hid.getName(address); // 0x123
console.log("address: %s, name: %s", address, name);
}
mainName("0xd530ec9517c20de518345a7210338dfb6279f454");
Registering Steps
Step 1
Check if the normalized and validated domain name is available for registering through the ENSRegistrarController contract.
async function getvalidne(name, from) {
const provider = new Web3.providers.HttpProvider(RPC.apis.mig_mainnet);
hid = new HID({ provider, from, getENSControllerAddr: HIDfunctions.getENSControllerAddr("80001") });
const namevalue = await hid.getValidName(name); // 0x123
console.log("name: %s", namevalue);
}
getvalidne("xuranhhh", "0x661699Cf19990B5ed7E27a12e9F6051A7D587Bb0");
Parameter | Meaning | Value | Required |
---|---|---|---|
name | a validated domain name | xuranhhh | true |
from | the address of the user | 0x661699Cf19990B5ed7E27a12e9F6051A7D587Bb0 | false |
Step 2
Call the makeCommitmentWithConfig method in ENSRegistrarController with a return of encoded commitment of type bytes32.
async function makeCommitment(name, randomString, from) {
const provider = new Web3.providers.HttpProvider(RPC.apis.mig_mainnet);
hid = new HID({ provider, from, getENSControllerAddr: HIDfunctions.getENSControllerAddr("80001"), getHidAddress: HIDfunctions.getHidAddress("80001") });
const namevalue = await hid.makeCommitmentWithConfig(name, randomString); // 0x123
console.log("name: %s", namevalue);
}
makeCommitment("xuranhhh", "sdfadfasdfa1243sd", "0x661699Cf19990B5ed7E27a12e9F6051A7D587Bb0");
Parameter | Meaning | Value | Required |
---|---|---|---|
name | a validated domain name | xuranhhh | true |
randomString | random string | sdfadfasdfa1243sd | true |
from | the address of the user | 0x661699Cf19990B5ed7E27a12e9F6051A7D587Bb0 | true |
Step 3
Call commit method in ENSRegistrarController. The input is step 3’s output.
async function commit(commitment, from) {
const provider = new Web3.providers.HttpProvider(RPC.apis.mig_mainnet);
hid = new HID({ provider, from, getENSControllerAddr: HIDfunctions.getENSControllerAddr("80001") });
const value = await hid.makeCommit(commitment); // 0x123
console.log("name: %s", value);
}
commit("0xda3120f5993a30904214d0edd2581b27be1a630484908a186b3b5a7b986bd67b");
//send the result to the wallet and send the transaction
Parameter | Meaning | Value | Required |
---|---|---|---|
commitment | the input is step 3’s output. | 0xda3120f5993a30904214d0edd2581b27be1a630484908a186b3b5a7b986bd67b | true |
from | the address of the user | 0x661699Cf19990B5ed7E27a12e9F6051A7D587Bb0 | true |
Step 4
Retrieve the real-time price of the domain by calling the rentPrice Method in ENSController contract.
async function rentPrice(name, number, from) {
const provider = new Web3.providers.HttpProvider(RPC.apis.mig_mainnet);
hid = new HID({ provider, from, getENSControllerAddr: HIDfunctions.getENSControllerAddr("80001") });
const value = await hid.makeRentPrice(name, number); // 0x123
console.log("price: %s", value);
}
rentPrice("xuranhhh", 1);
Parameter | Meaning | Value | Required |
---|---|---|---|
name | a validated domain name | xuranhhh | true |
number | unit: year. | 1 | true |
from | the address of the user | 0x661699Cf19990B5ed7E27a12e9F6051A7D587Bb0 | true |
Step 5
Wait for minCommitmentAge, retrieving from ENSController contract and call registerWithConfig method in ENSController.
async function regiseterConfirm(name, randomString, from, expiration, inviteAddr, rentPrice) {
const provider = new Web3.providers.HttpProvider(RPC.apis.mig_mainnet);
hid = new HID({ provider, from, getENSControllerAddr: HIDfunctions.getENSControllerAddr("80001"), getHidAddress: HIDfunctions.getHidAddress("80001") });
const resultValue = await hid.makeRegisterWithConfig(name, randomString, expiration, inviteAddr, rentPrice); // 0x123
console.log("name: %s", resultValue);
}
regiseterConfirm("xuranhhh", "sdfadfasdfa1243sd", "0x661699Cf19990B5ed7E27a12e9F6051A7D587Bb0", 1, "0x0000000000000000000000000000000000000000", "62500000000");
//send the result to the wallet and send the transaction
Parameter | Meaning | Value | Required |
---|---|---|---|
name | a validated domain name | xuranhhh | true |
randomString | need to be consistent with the random string in the step 2. | sdfadfasdfa1243sd | true |
from | the address of the user | 0x661699Cf19990B5ed7E27a12e9F6051A7D587Bb0 | true |
expiration | this domain validity period | 1 | true |
inviteAddr | inviter's address,please use empty address when empty | 0x0000000000000000000000000000000000000000 | false:empty address |
rentPrice | step 4’s output. | 62500000000 | true |
exports
default - HID
getHidAddress
getResolverContract
getHIDContract
namehash
labelhash
Build SDK and test on your test machine
yarn install
yarn run build
node main.js