I received a task which is generating key pair in pallet on chain logic. I know that I should import sp_core crate. I import it like this: "use sp_core::Pair;" This line was written at the top of my pallet source code but below the micro "#![cfg_attr(not(feature = "std"), no_std)]"

When I build the project, an error comes out and shows "no Pair in the root" .I inspect the rust docs which shows that Pair is a trait in sp_core::crypto (sp_core::crypto::Pair),and re-export to the sp_core root. So I don't know why the error jump out.

After thinking it over carefully, I thought it must be related to cfg feature. I mean when compiling the pallet or runtime code, the feature of std should not be set, but the Pair trait has a std feature set on itself just right. So the error said "no Pair in the root"

But why the Pair trait can't be use in pallet and runtime? Maybe the random is the origin of this. As we know, random generating is forbidden on blockchain because of consensus mechanism. So generating key pair is also forbidden due to the use of random.

So how can I generate a key pair? Can I generate key pair on offchain work as an alternative way? But the offchain work is one part of pallet as an implementation of hooks trait. I find that I still can not compile the pallet code with "use sp_core::Pair;".

Oh, who can tell me how to handle this problem? Thanks!

  • You cannot create an on-chain secret key, as it would not remain secret. What are you trying to actually do? Commented Aug 16, 2022 at 11:04
  • I will use key pair in Information disclosure logic, so I must generate key pair first. I also want to figure our if there is a way to keep secret infomation on chain or in offchain work. Is offchain storage an alternative way?@Oliver Tale-Yazdi
    – kugool
    Commented Aug 16, 2022 at 11:40
  • @kugool It's not proper to keep private key on chain. If you want to do some private key relative stuff, why not just do it in an oracle node? Commented Aug 17, 2022 at 3:59
  • @ironman_ch_parami Take a step back, can I generate a random item(number or string or bytes) on chain or offchain work?
    – kugool
    Commented Aug 17, 2022 at 8:27
  • @kugool you can generate random item on chain, but can generate random item offchain, but you need extra consensus on that random item. Commented Aug 17, 2022 at 9:45

1 Answer 1


For "How to generate random offchain", you can refer https://wiki.polkadot.network/docs/learn-randomness


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.