Resolve $SELF Names to EVM Addresses Using ethers.js

This page provides the integration steps to resolve $SELF names to Ethereum Virtual Machine (EVM) addresses using ethers.js.

Prerequisites

Ensure you have Node.js installed in your environment.


Integration Steps

Project Initialization

  1. Initialize a new Node.js project in your desired directory:
    npm init -y
    
  2. Install ethers.js:
    npm i ethers@5.7.2
    
  3. Update package.json to enable ES6 import/export:
    {
      "type": "module"
    }
    
  4. Create a file named index.js. Ensure your project structure is as follows:
    - node_modules/
    - package.json
    - package-lock.json
    - index.js
    

Complete Code Example

  1. Here's the complete script that you can copy and use:

    import { ethers } from "ethers";
    import { keccak256, toUtf8Bytes } from "ethers/lib/utils.js";
    
    const selfNftAbi = [
    {
     "inputs": [{"internalType": "uint256", "name": "tokenId", "type": "uint256"}],
     "name": "ownerOf",
     "outputs": [{"internalType": "address", "name": "", "type": "address"}],
     "stateMutability": "view",
     "type": "function"
    }
    ];
    
    const SELF_NFT_ADDRESS = "0x125Bb13F77f3565d421bD22e92aaFfC795D97a72";
    const BSC_RPC_URL = "https://bsc-dataseed1.binance.org/";
    const NAME_TO_RESOLVE = "ricomaverick";
    
    const provider = new ethers.providers.JsonRpcProvider(BSC_RPC_URL);
    
    async function main() {
      const hashedName = BigInt(keccak256(toUtf8Bytes(NAME_TO_RESOLVE))).toString();
      const contract = new ethers.Contract(SELF_NFT_ADDRESS, selfNftAbi, provider);
      const resolvedName = await contract.ownerOf(hashedName);
    
      console.log("Resolved Name:", resolvedName);
    }
    
    main().catch((error) => {
     console.error(error);
    process.exitCode = 1;
    });
    
    

Running the Script

  1. Execute the script using Node.js:

    node index.js
    

Make sure you have:

  1. Updated the NAME_TO_RESOLVE constant with the name you want to look up
  2. Installed all dependencies (npm install)

Output

  1. The script outputs the resolved EVM address for the provided name:

    Resolved Name: 0x383D8ee00c3Ea31dd4eb6e5eCf649Bc9C08D8e31