Now that we've finished writing our gibbername crate, we'll demonstrate actually using it in a project. We will build gibbername-cli, a trivial wrapper around the library that lets you look up and register names on the command line. Using it willl look something like
gibbername-cli lookup tofnal-seh
hello world my dudes this is what's bound to the name lol
You can find a complete example in our GitHub repo.
Project setup
Let's start by creating a new binary crate:
cargonewgibbername-clicdgibbername-cli
We add melprot, melstructs, anyhow for error handling,argh for lightweight argument parsing, and futures-lite for bare-bones async support:
We write a basic scaffold that parses the arguments with argh:
se argh::FromArgs;use futures_lite::future::block_on;use melstructs::{Address, NetID};#[derive(FromArgs, PartialEq, Debug)]/// Look up a name in the Gibbername registry.structCli { #[argh(option, description ="either 'mainnet' or 'testnet'")] network:NetID, #[argh(subcommand)] command:Command,}#[derive(FromArgs, PartialEq, Debug)]#[argh(subcommand)]enumCommand {Lookup(Lookup),Register(Register),}#[derive(FromArgs, PartialEq, Debug)]#[argh(subcommand, name ="lookup")]/// Lookup what is bound to a namestructLookup { #[argh(positional)] name:String,}#[derive(FromArgs, PartialEq, Debug)]#[argh(subcommand, name ="register")]/// Register a namestructRegister { #[argh(option, description ="mel address of the gibbername owner")] owner:Address, #[argh(option, description ="data to be bound to the gibbername")] binding:String, #[argh(option, description ="path to the wallet sending the transaction")] wallet_path:String,}fnmain() -> anyhow::Result<()> { env_logger::init();let args:Cli= argh::from_env();// keep around a clientlet client =block_on(melprot::Client::autoconnect(args.network))?;match args.command.as_ref() {Command::Lookup(lookup) => {todo!() }Command::Register(register) => {todo!() } };Ok(())}
Filling in the functionality
Now that we have a basic scaffold, filling in the functionality is incredibly easy:
use futures_lite::future::block_on;fnmain() -> anyhow::Result<()> {let args:Cli= argh::from_env();// keep around a clientlet client =block_on( melprot::Client::autoconnect(NetID::Testnet) )?;match args.command {Command::Lookup(lookup) => {// we don't need a futures runtime, block_on is finelet gname =block_on(gibbername::lookup(&client, &lookup.name))?;println!("{gname}"); }Command::Register(register) => {// gibbername will prompt the userlet name =block_on(gibbername::register(&client, register.owner, ®ister.binding, ®ister.wallet_name))?;println!("registered {:?}", name); } };Ok(())}
Testing
We now have a complete program! We can test run it with cargo run:
cargo run -- --network mainnet register --owner x7v9tegt6b99xv9t6e56kabap3ych5htw83wa69z0shwa7ms3xbkn7 --binding "hello world" --wallet-path ./alice.json
Send this command with your wallet: melwallet-cli --wallet-path ./alice.json send --to x7v9tegt6b99xv9t6e56kabap3ych5htw83wa69z0shwa7ms3xbkn7,0.000001,"(NEWCUSTOM)","68656c6c6f20776f726c64" --hex-data 6769626265726e616d652d7631
Now, run the melwallet-cli command which will register our gibbername: