0

newbie here, so apologies if this is a stupid question.

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?

Many thanks.

Isambard_FA
  • 97
  • 13
  • 1
    That function is inside `impl EthereumWallet`, so you'd first need to create an instance of that struct, which you can't, since the struct is private. – Sven Marnach Feb 22 '21 at 09:26
  • 1
    I'm wondering whether declaring associated functions of a private type as public makes any difference. I currently can't think of any. – Sven Marnach Feb 22 '21 at 09:38
  • Thanks Sven, that was what I feared. Is there then someway to mimic the input of the command line interface? For example, running the crate from the command line with: ```cargo run ethereum import-hd "sunny story shrimp absent valid today film floor month measure fatigue pet"``` will return the right output. Should it be possible to mimic the input struct of clap with my own parameters from code? – Isambard_FA Feb 22 '21 at 09:47
  • 1
    The easiest solution I can think of is to simply build the binary and call it as a subprocess. – Sven Marnach Feb 22 '21 at 09:49
  • 1
    See [`std::process:Command`](https://doc.rust-lang.org/std/process/struct.Command.html`) for examples. – Sven Marnach Feb 22 '21 at 09:51
  • Thank you. I will now look into how to do that! – Isambard_FA Feb 22 '21 at 09:51

1 Answers1

0

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 {
Hadus
  • 1,551
  • 11
  • 22
  • 1
    Many thanks Hadus for taking the time to provide your extended answer. I have actually started to move away from using the `Wagyu` crate functionality as I am not sure it is giving me the correct address. When I use the same mnemonic on Ian Coleman's fantastic [website](https://allprivatekeys.com/mnemonic-code-converter#english) I get a different address (and the correct one that can be verified via MetaMask). I'm going to post a separate question related to the use of the keccak-256 function, which I am struggling to make work. Thanks again. – Isambard_FA Feb 26 '21 at 08:29
  • New query on use of keccak-256 algorithm [here](https://stackoverflow.com/questions/66383584/generating-ethereum-wallet-in-rust-problem-with-my-public-key-or-address) – Isambard_FA Feb 26 '21 at 10:05