use frame_support::sp_runtime::app_crypto::TryFrom;
use frame_support::sp_runtime::traits::{IdentifyAccount, Verify};
use frame_support::sp_runtime::MultiSignature;
use frame_support::sp_runtime::MultiSigner;
use sp_application_crypto::sr25519;
use sp_application_crypto::sr25519::Public;
use sp_application_crypto::sr25519::Signature;
impl<T: Config> Pallet<T> {
fn check_signed_valid(public_id: Public, signature: &[u8], msg: &[u8]) -> bool {
let signature = Signature::try_from(signature);
let signature = signature.unwrap();
let multi_sig = MultiSignature::from(signature); // OK
let multi_signer = MultiSigner::from(public_id);
multi_sig.verify(msg, &multi_signer.into_account())
}
}
函數(shù)的使用
這里是使用的代碼片段惠桃,展示了參數(shù)部分和調(diào)用的部分浦夷。
#[pallet::weight(1234)]
pub fn reveal_puzzle(
origin: OriginFor<T>,
answer_signed: Vec<u8>,
answer_hash:Vec<u8>
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
// 提取公鑰
let encode_data: Vec<u8> = who.encode();
// 公鑰應(yīng)該是32位的
assert!(32 == encode_data.len());
let raw_data = encode_data.try_into();
let raw_data = raw_data.unwrap();
let public = sp_core::sr25519::Public::from_raw(raw_data);
Self::check_signed_valid(
public,
// 這邊要注意先要吧簽名的16進制decode成&[u8]
&hex::decode(answer_signed.as_slice()).expect("Hex invalid")[..],
answer_hash.as_slice()
),