Converting Between Usernames and Addresses Overview
Initia Usernames
are mapped to wallet addresses using a specific domain format (e.g., xxx.init). This section provides guidelines on how to interact with Move modules that manage these usernames. The functionality includes fetching addresses from usernames and vice versa.
Module Address
The usernames
module is not part of the standard library and is deployed through a multisig wallet. The current module address for the usernames
module is:
initiation-1: 0x42cd8467b1c86e59bf319e5664a09b6b5840bb3fac64f5ce690b5041c530565a
Fetching Address from Username
To retrieve an address from a username, the get_address_from_name
view function is used. The function interface is as follows:
Copy #[view]
public fun get_address_from_name (name : String ) : Option <address>
curl initiad initia.js
Copy curl -X POST "[LCD_URI]/initia/move/v1/accounts/[MODULE_ADDR]/modules/usernames/view_functions/get_address_from_name" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d "{ \"args\": [ \"[BCS_ENCODED_NAME]\" ]}"
# BCS_ENCODED_NAME is the bcs encoded string without .init
#{
# "data": "\"0x...\"",
# "events": [],
# "gas_used": "5699"
#}
Copy > initiad query move view [MODULE_ADDR] usernames get_address_from_name \
--args "string:username" \ # without .init
--node [rpc-url]:[rpc-port]
# data: '"0x..."'
# events: []
# gas_used: "5699"
Copy import { LCDClient , bcs } from '@initia/initia.js' ;
const moduleAddress = '0x...' ;
const lcdUri = 'https://...' ;
const name = 'abc' ;
const lcd = new LCDClient (lcdUri);
lcd .move
.view (
moduleAddress ,
'usernames' ,
'get_address_from_name' ,
[] ,
[ bcs .string () .serialize (name) .toBase64 ()]
)
.then ( console .log);
// {
// data: '"0x.."',
// events: [],
// gas_used: '5699'
// }
Fetching Username from Address
To retrieve a username from an address, the get_name_from_address
view function is utilized:
Copy #[view]
public fun get_name_from_address (addr : address) : Option < String >
curl initiad initia.js
Copy curl -X POST "[LCD_URI]/initia/move/v1/accounts/[MODULE_ADDR]/modules/usernames/view_functions/get_name_from_address" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d "{ \"args\": [ \"[BCS_ENCODED_ADDRESS]\" ]}"
#{
# "data": "\"abc...\"",
# "events": [],
# "gas_used": "5699"
#}
Copy > initiad query move view [MODULE_ADDR] usernames get_name_from_address \
--args "address:init1..." \
--node [rpc-url]:[rpc-port]
# data: '"abc..."'
# events: []
# gas_used: "5699"
Copy import { LCDClient , bcs } from '@initia/initia.js' ;
const moduleAddress = '0x...' ;
const lcdUri = 'https://...' ;
const name = 'abc' ;
const lcd = new LCDClient (lcdUri);
lcd .move
.view (
'0x42cd8467b1c86e59bf319e5664a09b6b5840bb3fac64f5ce690b5041c530565a' ,
'usernames' ,
'get_name_from_address' ,
[] ,
[
bcs
.address ()
.serialize ( 'init1ha6psc7wnz0t5fm5fec98q0qf9zr960candrew' )
.toBase64 () ,
]
)
.then ( console .log);
// {
// data: '"abc..."',
// events: [],
// gas_used: '5699'
// }
Conclusion
Overall, the ability to link wallet addresses with human-readable names is invaluable, significantly improving the user experience by replacing cumbersome hexadecimal addresses with easily recognizable names. This functionality is crucial for the adoption and ongoing use of blockchain technology in everyday applications, making it more approachable and practical for a broader audience.
Last updated 4 months ago