我已使用 ed25519 签署了该消息
const msg = Buffer.from("hi");
const signerKeyPair = await keyStore.getKey(config.networkId, signerAccount);
const { signature } = signerKeyPair.sign(msg);
Run Code Online (Sandbox Code Playgroud)
如果我有签名和签名者公钥,如何验证智能合约中的签名?
我发现近核心使用近加密箱来做到这一点,但我不确定我是否可以将它用于智能合约。
使用近核心的示例
use near_crypto::{PublicKey, Signature, KeyType};
...
pub fn verify(&self, data: &[u8], public_key: String) -> bool {
let public_key = PublicKey::from_str(&public_key).unwrap();
let signature = Signature::empty(KeyType::SECP256K1);
signature.verify(data, &public_key)
}
...
Run Code Online (Sandbox Code Playgroud)
这是一个解决方案
使用 ed25519-dalek = "1.0.1" 箱
pub fn gimme_my_present(&mut self, signature: Vec<u8>) -> Promise {
let signature = ed25519_dalek::Signature::try_from(signature.as_ref())
.expect("Signature should be a valid array of 64 bytes [13, 254, 123, ...]");
let account_id = near_sdk::env::signer_account_id();
// Someone will give you the corresponding private key...
let public_key = ed25519_dalek::PublicKey::from_bytes(
&bs58::decode(
"H5ANpdUoXVwhYBgAgEi1ieMQZKJbwxjPJtHX4vkVcSnF",
)
.into_vec()
.unwrap(),
)
.unwrap();
near_sdk::env::log(
format!(
"Verifiying validity of signature ('{:?}') for string '{}'...",
signature, account_id
)
.as_bytes(),
);
if let Ok(_) = public_key.verify(account_id.as_bytes(), &signature) {
return Promise::new(account_id).transfer(16 * 16 * ONE_NEAR);
}
panic!("Ima no gonna give-ya the present without a signature! :-P");
}
Run Code Online (Sandbox Code Playgroud)