A TypeScript library for interacting with Nad Name Service (NNS) protocol using Viem. This library provides a convenient wrapper around the NNS smart contract, enabling seamless operations related to name resolution, address mapping, and managing name attributes.
Nad Name Service (NNS) is a web3 name service built natively on Monad. NNS maps human-readable names like 'salmo.nad' to machine-readable identifiers such as cryptocurrency addresses, metadata, and more.
More information:
- Name Resolution: Resolve
.naddomain names to Ethereum addresses. - Address Mapping: Retrieve primary names associated with wallet addresses.
- Batch queries: Retrieve resolved addresses, primary names, profiles, ...
- Attribute Management: Get and set custom attributes for names.
- Avatar Integration: Fetch avatar URLs associated with names.
- And many more...
- Node.js: Ensure you have Node.js installed (v12 or higher recommended).
- TypeScript
- Viem: This library depends on Viem for blockchain interactions.
Install the library via npm:
npm install @nadnameservice/nns-viem-sdkOr using yarn:
yarn add @nadnameservice/nns-viem-sdkTo initialize the NNS library:
Create an instance of the NNS class by providing a PublicClient if you only want to read infos:
import { NNS } from '@nadnameservice/nns-viem-sdk'
import { createPublicClient, http } from 'viem'
const viemClient = createPublicClient({
chain: monad,
transport: http(),
})
const nns = new NNS(viemClient)Or if you want to modify name attributes, provide PublicCliient and WalletClient to the NNS class:
import { NNS } from '@nadnameservice/nns-viem-sdk'
import {
createPublicClient,
createWalletClient,
http,
} from 'viem'
const viemClient = createPublicClient({
chain: monad,
transport: http(),
})
const viemWallet = privateKeyToAccount('...')
const viemWalletClient = createWalletClient({
account: viemWallet,
chain: monnad,
transport: http(),
})
const nns = new NNS(viemClient, viemWalletClient)const profile = await nns.getProfile(
'0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'
)
// {
// primaryName: 'test.nad',
// avatar: 'https://...
// }const profiles = await nns.getProfiles([
'0x771CdA7e3786979d8fDed8d4c22Cd42F7B576dD4',
'0x88eecAcDfaC29ab77aa5E185f34CF8481127FB0E',
'0xf020798E2D29C0e26d400EE12A1b100Da46bfB3C',
])
/*[
{
primaryName: 'nnsfaucet.nad',
avatar: undefined,
addr: '0x771CdA7e3786979d8fDed8d4c22Cd42F7B576dD4'
},
{
primaryName: 'tungtungtungsahur69.nad',
avatar: 'https://play-lh.googleusercontent.com/QrCjJRpiwcFZcwey7VYGtTLPM-iohNDa6ktQfRJNvr7meX3eeTSnyvW6Fe7fHh7Kjwc=w240-h480-rw',
addr: '0x88eecAcDfaC29ab77aa5E185f34CF8481127FB0E'
},
{
primaryName: 'hihihaha1234.nad',
avatar: 'https://cdn.discordapp.com/avatars/789141581155795004/a43f791724b7a61334c2f2b01ea82ddd.png',
addr: '0xf020798E2D29C0e26d400EE12A1b100Da46bfB3C'
}
]*/const resolveAddress = await nns.getResolvedAddress(
'test.nad'
)
// 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266const resolveAddresses = await nns.getResolvedAddresses([
'mo.nad',
'keone.nad',
'joe69.nad',
'0xchine.nad',
'π.nad',
'0x771CdA7e3786979d8fDed8d4c22Cd42F7B576dD4',
'123%^&',
])
/*[
{
name: 'joe69.nad',
resolvedAddress: '0x88eecAcDfaC29ab77aa5E185f34CF8481127FB0E'
},
{
name: '0xchine.nad',
resolvedAddress: '0x76C5851cE22e476F3513eD31DD77840B4d9DB34C'
},
{
name: 'π.nad',
resolvedAddress: '0x0A28c04667686A7F2182a9A2488998c61029289E'
},
{
name: '0x771CdA7e3786979d8fDed8d4c22Cd42F7B576dD4',
resolvedAddress: undefined
},
{ name: '123%^&', resolvedAddress: undefined }
]*/const primaryName = await nns.getPrimaryNameForAddress(
'0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'
)
// test.nadconst primaryNames = await nns.getPrimaryNameForAddresses([
'0x771CdA7e3786979d8fDed8d4c22Cd42F7B576dD4',
'0x88eecAcDfaC29ab77aa5E185f34CF8481127FB0E',
zeroAddress,
])
/*[
{
addr: '0x771CdA7e3786979d8fDed8d4c22Cd42F7B576dD4',
primaryName: 'nnsfaucet.nad'
},
{
addr: '0x88eecAcDfaC29ab77aa5E185f34CF8481127FB0E',
primaryName: 'tungtungtungsahur69.nad'
},
{
addr: '0x0000000000000000000000000000000000000000',
primaryName: undefined
}
]*/const avatar = await nns.getAvatarUrl('test.nad')
// https://www.example.com/image.png// Get an attribute
const avatarStr = await nns.getNameAttribute(
'test.nad',
'avatar'
)
// Get a list of attribute of a name
const customAttrs = await nns.getNameAttributes(
'test.nad',
['key1', 'key2', 'key3']
)// Set value for an attribute
const tx = await nns.setNameAttribute(
'test.nad',
'key1',
'value1'
)
// Set value for a list of attributes
const tx1 = await nns.setNameAttributes('test.nad', [
{
key: 'key2',
value: 'value2',
},
{
key: 'key3',
value: 'value3',
},
])Get profile including primary name, avatar for an address
- Parameters:
address: The wallet address.
- Returns: an
Profileobject includesprimaryName: The primaryName of the accountavatar: The avatar record of the account
Get profile including primary name, avatar for multiple address
- Parameters:
addrs: The list of wallet address.
- Returns: an
Profile[]array
Resolves a given name to an address.
- Parameters:
name: The name to resolve, with the.naddomain.
- Returns: Resolved address or zero address if unregistered.
Resolves a list of names to a list of addresses.
- Parameters:
names: The name array to resolve, with the.naddomain.
- Returns: A list of
ResolvedAddressItemincludes:name: The .nad nameresolvedAddress: The resolved address of the name
Retrieves the primary name associated with an address.
- Parameters:
address: The wallet address.
- Returns: Primary name or empty string if not set.
Retrieves the primary names associated with a list of address.
- Parameters:
addrs: The list of address
- Returns: A list of
PrimaryNameItemsincludes:addr: The wallet addressprimaryName: The primary name associated with the address
Retrieves the value of a specific attribute for a given name.
- Parameters:
name: The name to retrieve the attribute from, with the.naddomain.key: The key of the attribute.
- Returns: Value of the attribute.
Retrieves a list of name attributes.
- Parameters:
name: The name to retrieve attributes from, with the.naddomain.keys: The keys of the attributes to retrieve.
- Returns: List of name attributes.
Retrieves the names associated with a given address.
- Parameters:
address: The wallet address.
- Returns: An array of names associated with the address.
Retrieves the avatar URL associated with a given name.
- Parameters:
name: The name to retrieve the avatar URL from, with the.naddomain.
- Returns: Avatar URL.
Sets a value for a specific attribute for a given name.
- Parameters:
name: The name to set the attribute for, with the.naddomain.key: The key of the attribute to set.value: The value of the attribute to set.
- Returns: Transaction data.
Sets multiple attributes for a given name.
- Parameters:
name: The name to set the attributes for, with the.naddomain.attributes: The attributes to set.
- Returns: Transaction data.
Contributions are welcome! If you would like to contribute to this project, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and commit them with descriptive messages.
- Push your changes to your forked repository.
- Open a pull request detailing your changes and why they should be merged.
This project is licensed under the MIT License. See the LICENSE file for details.