rustdependenciescommand-line-interface

How do I call from_mnemonic from the wagyu crate without using the command line?


I would like to use the functionality of the wagyu crate in my code. This crate has command line functionality, so I can run the code from the command line but I can't seem to reference it from my own code.

I've tried 2 options:

  1. Replicate the input 'clap' is expecting when calling the crate (a struct with arguments)
  2. Call a specific function from within the crate

For item 2 I have tried:

use wagyu::cli::ethereum;

fn main() {
    let m: String = String::from("sunny story shrimp absent valid today film floor month measure fatigue pet");
    
    // Returns the address of the corresponding mnemonic.
    let passphrase = "";
    let pathway = "m/44'/60'/0'/0";
    let address = ethereum::from_mnemonic(m, passphrase, pathway);
        
    println!("phrase: {:?}", address);
}

When I try to build this code I get the following compile error:

error[E0425]: cannot find function `from_mnemonic` in module `ethereum`
  --> src\main.rs:37:29
   |
37 |     let address = ethereum::from_mnemonic::<>(s, passphrase, pathway);
   |                             ^^^^^^^^^^^^^ not found in `ethereum`

But I know from checking the code in the ethereum.rs file that there is a public function called 'from_mnemonic' (defined on line 88).

Does anyone know why I can't call this function? Or alternatively, is there an easy way to use a crate that has a clap dependency without using the command line interface?


Solution

  • The from_mnemonic function should be called like so:

    use wagyu::cli::ethereum::EthereumWallet;
    use wagyu_ethereum::network::Mainnet; 
    use wagyu_ethereum::wordlist::English;
    
    fn main() {
        let mnemonic: String = String::from(
            "sunny story shrimp absent valid today. film floor month measure fatigue pet"
        );
        let passphrase = "";
        let path = "m/44'/60'/0'/0";
    
        let wallet = EthereumWallet::from_mnemonic::<Mainnet, English>(
            mnemonic,
            Some(passphrase),
            path
        ).unwrap()
    }
    

    But wagyu::cli::ethereum::EthereumWallet is not pub so you can't just simply do this. You have to download the source of wagyu from github and edit it so wagyu::cli::ethereum::EthereumWallet will be public.

    I think the single change you have to make is replacing this (in ethereum.rs):

    struct EthereumWallet {
    

    With this:

    pub struct EthereumWallet {