Skip to main content

Query the web3name of an Account

For accounts that have been linked to DIDs that have claimed a web3name, the linking feature opens the way to a host of possibilities, e.g., showing the web3name of a collator's account on the KILT Stakeboard.

This section shows how to perform the account -> web3name querying both with and without the support of the KILT SDK.

Query an Account's web3name with the KILT SDK

import * as Kilt from '@kiltprotocol/sdk-js'

export async function queryAccountWeb3Name(
lookupAccountAddress: Kilt.KiltAddress
): Promise<Kilt.Did.Web3Name | null> {
const api = Kilt.ConfigService.get('api')

const encodedLinkedDetails = await api.call.did.queryByAccount(
Kilt.Did.accountToChain(lookupAccountAddress)
)
const { web3Name } = Kilt.Did.linkedInfoFromChain(encodedLinkedDetails)
if (web3Name) {
console.log(
`web3name for account "${lookupAccountAddress}" -> "${web3Name}"`
)
} else {
console.log(
`Account "${lookupAccountAddress}" does not have a linked web3name.`
)
}

return web3Name
}

Query an Account's web3name without the KILT SDK

import type { KeyringPair } from '@polkadot/keyring/types'

import { ApiPromise, WsProvider } from '@polkadot/api'

// Import needed to provide KILT Typescript support to the api object.
import '@kiltprotocol/augment-api'
import { typesBundle } from '@kiltprotocol/type-definitions'

export async function queryAccountWeb3Name(
endpoint: string,
lookupAccountAddress: KeyringPair['address']
): Promise<string | null> {
const api = await ApiPromise.create({
provider: new WsProvider(endpoint),
typesBundle
})
// Call to the KILT runtime API `did.queryByAccount`
const didDetails = await api.call.did.queryByAccount({
AccountId32: lookupAccountAddress
})
if (didDetails.isNone) {
throw new Error(`No DID for the KILT account "${lookupAccountAddress}".`)
}

const { w3n } = didDetails.unwrap()
if (w3n.isNone) {
throw new Error(
`No web3name for the KILT account "${lookupAccountAddress}".`
)
}

const web3Name = w3n.unwrap().toHuman()
console.log(
`The provided account is identifiable by the following web3name: "w3n:${web3Name}"`
)

return web3Name
}