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!