4

When I try to create a PairSigner, then the Pair traits are not satisfied.

GitHub repo to check the code

With this code:

let pair = sp_core::sr25519::Pair::from_phrase(mnemonic_phrase.as_str(), None).unwrap();

let signer = PairSigner::<PolkadotConfig, sp_core::sr25519::Pair>::new(pair);

I get this error:

error[E0599]: the function or associated item `new` exists for struct `PairSigner<WithExtrinsicParams<SubstrateConfig, BaseExtrinsicParams<SubstrateConfig, PlainTip>>, Pair>`, but its trait bounds were not satisfied
  --> src/main.rs:23:72
   |
23 |     let signer = PairSigner::<PolkadotConfig, sp_core::sr25519::Pair>::new(pair);
   |                                                                        ^^^ function or associated item cannot be called due to unsatisfied trait bounds
   |
  ::: /Users/martin.jensen/.cargo/registry/src/github.com-1ecc6299db9ec823/sp-core-18.0.0/src/sr25519.rs:83:1
   |
83 | pub struct Pair(Keypair);
   | ---------------
   | |
   | doesn't satisfy `_: CryptoType`
   | doesn't satisfy `sp_core::sr25519::Pair: sp_core::crypto::Pair`
   |
   = note: the following trait bounds were not satisfied:
           `sp_core::sr25519::Pair: sp_core::crypto::Pair`
           `sp_core::sr25519::Pair: sp_core::crypto::CryptoType`
           which is required by `sp_core::sr25519::Pair: sp_core::crypto::Pair`

For more information about this error, try `rustc --explain E0599`.

This is my toml dependencies which I followed from the subxt example toml:

[dependencies]
structopt = "0.3.26"
tokio = { version = "1.26", features = ["rt-multi-thread", "macros", "time"] }
sp-keyring = "20.0.0"
sp-core = { version = "18.0.0", default-features = false }
sp-runtime = "20.0.0"
futures = "0.3.27"
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive", "full", "bit-vec"] }
hex = "0.4.3"
tracing-subscriber = "0.3.11"
subxt = "0.27.1"

3 Answers 3

3

This is a simple case of the subxt and sp_* dependencies being out of sync; I think you used Subxt master branch to take the dependencies you used, but you're pointing at an older version of Subxt in your Cargo.toml. In other words, your dependencies are:

sp-keyring = "20.0.0"
sp-core = { version = "18.0.0", default-features = false }
sp-runtime = "20.0.0"
subxt = "0.26.0"

But If you look at the dependencies for Subxt 0.26.0 (https://crates.io/crates/subxt/0.26.0/dependencies) you see it depends on:

sp-keyring = "12.0.0"
sp-core = "11.0.0"
sp-runtime = "12.0.0"

So, some options are:

  • downgrade the versions of the sp_* libraries that you're using to match what Subxt 0.26 is using.
  • upgrade your version of Subxt to be in line with the sp_* libraries you're using (I think you'll still need to downgrade the sp_* ones a bit because we need to do a new release with latest).
  • use subxt::ext::sp_core:: and subxt::ext::sp_runtime:: instead of specifying those deps yourself to get at the same versions that Subxt uses (may only be available in a more recent version of Subxt than the one you're using; can't recall!)
1
3

Add this import:

use sp_core::{Pair};

And then the method from_phrase() returns you a tuple: (Pair, Seed) get only the Pair in your method new:

let pair = sp_core::sr25519::Pair::from_phrase(mnemonic_phrase.as_str(), None).unwrap();

let signer = PairSigner::<PolkadotConfig, sp_core::sr25519::Pair>::new(pair.0);

Or you can use the method from_string():

let pair = sp_core::sr25519::Pair::from_string(mnemonic_phrase.as_str(), None).unwrap();

let signer = PairSigner::<PolkadotConfig, sp_core::sr25519::Pair>::new(pair);
1
  • Thanks for the answer, but I still seem to experience issues with the trait bounds not satisfied for either of them. I have created a GitHub repo if that would help on checking with my code. Appreciate you taking the time with the answer above. github.com/martinloesethjensen/gm-burn-fren-script Apr 1 at 20:06
0

Just wanted to add the solution that worked for me for any future visitors. (Tested with subxt v0.28)

To avoid manually checking and specifying sp_* versions, which did not solve my issues, I used the re-exported structs from subxt itself. I did the following:

use subxt::ext::sp_core::Pair;
use subxt::tx::PairSigner;

...

pub async fn something() -> Result<(), Box<dyn Error>> {

...

    let pair = subxt::ext::sp_core::sr25519::Pair::from_string_with_seed(<'0x'-PREFIXED PRIVATE SEED>, None).unwrap();
    let signer: PairSigner<SubstrateConfig, subxt::ext::sp_core::sr25519::Pair> = PairSigner::<SubstrateConfig, subxt::ext::sp_core::sr25519::Pair>::new(pair.0);

...
}

This will presumably avoid having to check and update the version of each sp_* dependency if the subxt version is changed.

If any subxt maintainer reads this, it could be great with some in-depth explanations/tutorials/guides to set up the tool with a custom Substrate chain. The provided examples and the rust documentation pages did not quite satisfy my needs. That said, the idea of having a tool for interacting with a live chain, which can be validated on compile-time through generated runtime APIs is super cool. I appreciate your hard work.

Your Answer

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

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