1

I'm pretty new with Substrate and Rust, but I was trying to generate a key pair using the from_string function of sp_core::crypto::Pair . I haven't had any luck and kept getting either InvalidHexString or InvalidPhrase errors when I try to pass valid key items generated from the subkey tool.

For instance, I generated these key items from subkey tool by running the cmd subkey generate:

Secret phrase:       endorse doctor arch helmet master dragon wild favorite property mercy vault maze
  Network ID:        substrate
  Secret seed:       0x78b8fbbbea218509c00d3531e72128d4eae54089f15deb24d721e30b351733ad
  Public key (hex):  0x14e121f6e6cc2891cbbd5f6692e3724672d13e93a3562e3905d4310c2ba1c510
  Account ID:        0x14e121f6e6cc2891cbbd5f6692e3724672d13e93a3562e3905d4310c2ba1c510
  Public key (SS58): 5CY5hAGkTB7RZrouimGJ3S7zTDtnE6yg7z41DHrQeBcoc1PN
  SS58 Address:      5CY5hAGkTB7RZrouimGJ3S7zTDtnE6yg7z41DHrQeBcoc1PN

When I pass in the secret phrase I got from subkey tool to the from_string function, I get an InvalidPhrase error when submitting the composed extrinsic, even tho it was generated from subkey. This is sample code below:

  let priv_key: String = "endorse/doctor/arch/helmet/master/dragon/wild/favorite/property/mercy/vault/maze".to_owned();
  let from = Pair::from_string(&priv_key[..], None).unwrap();
  //let from = AccountKeyring::Bob.pair();
  let api = Api::new(client).map(|api| api.set_signer(from)).unwrap();
  ....
  extrinsic_submitters::submit_to_save_item(api, incoming_item, now, datetime);

This is the error I get upon submission of extrinsic:

thread 'tokio-runtime-worker' panicked at 'called `Result::unwrap()` on an `Err` value: InvalidPhrase', src/services/route_actions/mod.rs:76:53

I get similar error when using the secret seed as argument for from_string, only difference is I get InvalidHexString error when submitting the composed extrinsic. I was trying to follow the documentation of from_string usage in this link: https://docs.substrate.io/rustdocs/latest/sp_core/crypto/trait.Pair.html

Any tips on this on how to make this work? I was thinking the items I generated were valid. My goal was to be able to pass in a valid secret phrase or secret seed in this code to generate a key pair. I would then like to be able to sign an extrinsic using the generated key pair before passing the extrinsic to the chain. Thanks, all!

EDIT: I just want to note that the secret phrases and secret seeds I added here aren't used in production...

2
  • Any reason for using a forward slash in your string? Apr 29, 2022 at 5:55
  • Oh yeah I get an InvalidFormat error if I don't put forward slash on the secret phrase. With / characters, I get InvalidPhrase, so I think thats kinda better. Also, my understanding from the Pair documentation was that I need to separate the words for the mnemonics with / character. I may have understood the Pair docs wrong tho Apr 29, 2022 at 6:13

1 Answer 1

2

You have to use a concrete Pair implementation, since the Pair from sp_core::crypto is just a trait.
sr25519 is the default of subkey. By using that it produces the same address that you saw in the console.
No need for / or anything, just the from_string method and None as password.

#[test]
fn test_from_string() {
    use sp_core::{sr25519, Pair};
    let pair = sr25519::Pair::from_string("endorse doctor arch helmet master dragon wild favorite property mercy vault maze", None).unwrap();
    
    let want = "14e121f6e6cc2891cbbd5f6692e3724672d13e93a3562e3905d4310c2ba1c510 (5CY5hAGk...)";
    let got = format!("{:?}", pair.public());
    assert_eq!(want, got);
}

3
  • I get InvalidHexString(InvalidStringLength) when I submit the extrinsic I have signed with the key pair generated from this approach.. Any clues why this might be happening? I'm pretty sure the key pair generated is of the same type as AccountKeyring::Bob.pair(). AccountKeyring::Bob.pair() works, but the one I generated doesn't... Your code works tho Apr 29, 2022 at 15:41
  • Perhaps my actual issue is different from what I was thinking when I opened this question.. Apr 29, 2022 at 19:01
  • Maybe you encode the extrinsic incorrectly? You can look at the line that the error appears in your code. Apr 30, 2022 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.