mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-29 22:51:03 +00:00
Update schnorrkel with versioning (#5358)
* use versioning for deprecated api, remove deprecated api from regular verification * Update primitives/core/src/sr25519.rs * add test to transaction pool Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
@@ -529,25 +529,24 @@ impl TraitPair for Pair {
|
||||
self.0.sign(context.bytes(message)).into()
|
||||
}
|
||||
|
||||
/// Verify a signature on a message. Returns true if the signature is good.
|
||||
fn verify<M: AsRef<[u8]>>(sig: &Self::Signature, message: M, pubkey: &Self::Public) -> bool {
|
||||
Self::verify_weak(&sig.0[..], message, pubkey)
|
||||
}
|
||||
|
||||
/// Verify a signature on a message. Returns true if the signature is good.
|
||||
fn verify_weak<P: AsRef<[u8]>, M: AsRef<[u8]>>(sig: &[u8], message: M, pubkey: P) -> bool {
|
||||
// Match both schnorrkel 0.1.1 and 0.8.0+ signatures, supporting both wallets
|
||||
// that have not been upgraded and those that have. To swap to 0.8.0 only,
|
||||
// create `schnorrkel::Signature` and pass that into `verify_simple`
|
||||
match PublicKey::from_bytes(pubkey.as_ref()) {
|
||||
Ok(pk) => pk.verify_simple_preaudit_deprecated(
|
||||
SIGNING_CTX, message.as_ref(), &sig,
|
||||
).is_ok(),
|
||||
Err(_) => false,
|
||||
}
|
||||
let signature = match schnorrkel::Signature::from_bytes(sig) {
|
||||
Ok(signature) => signature,
|
||||
Err(_) => return false,
|
||||
};
|
||||
|
||||
let pub_key = match PublicKey::from_bytes(pubkey.as_ref()) {
|
||||
Ok(pub_key) => pub_key,
|
||||
Err(_) => return false,
|
||||
};
|
||||
|
||||
pub_key.verify_simple(SIGNING_CTX, message.as_ref(), &signature).is_ok()
|
||||
}
|
||||
|
||||
/// Return a vec filled with raw data.
|
||||
fn to_raw_vec(&self) -> Vec<u8> {
|
||||
self.0.secret.to_bytes().to_vec()
|
||||
}
|
||||
@@ -566,6 +565,20 @@ impl Pair {
|
||||
let kp = mini_key.expand_to_keypair(ExpansionMode::Ed25519);
|
||||
(Pair(kp), mini_key.to_bytes())
|
||||
}
|
||||
|
||||
/// Verify a signature on a message. Returns `true` if the signature is good.
|
||||
/// Supports old 0.1.1 deprecated signatures and should be used only for backward
|
||||
/// compatibility.
|
||||
pub fn verify_deprecated<M: AsRef<[u8]>>(sig: &Signature, message: M, pubkey: &Public) -> bool {
|
||||
// Match both schnorrkel 0.1.1 and 0.8.0+ signatures, supporting both wallets
|
||||
// that have not been upgraded and those that have.
|
||||
match PublicKey::from_bytes(pubkey.as_ref()) {
|
||||
Ok(pk) => pk.verify_simple_preaudit_deprecated(
|
||||
SIGNING_CTX, message.as_ref(), &sig.0[..],
|
||||
).is_ok(),
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CryptoType for Public {
|
||||
@@ -609,14 +622,15 @@ mod compatibility_test {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_known_message_should_work() {
|
||||
fn verify_known_old_message_should_work() {
|
||||
let public = Public::from_raw(hex!("b4bfa1f7a5166695eb75299fd1c4c03ea212871c342f2c5dfea0902b2c246918"));
|
||||
// signature generated by the 1.1 version with the same ^^ public key.
|
||||
let signature = Signature::from_raw(hex!(
|
||||
"5a9755f069939f45d96aaf125cf5ce7ba1db998686f87f2fb3cbdea922078741a73891ba265f70c31436e18a9acd14d189d73c12317ab6c313285cd938453202"
|
||||
));
|
||||
let message = b"Verifying that I am the owner of 5G9hQLdsKQswNPgB499DeA5PkFBbgkLPJWkkS6FAM6xGQ8xD. Hash: 221455a3\n";
|
||||
assert!(Pair::verify(&signature, &message[..], &public));
|
||||
assert!(Pair::verify_deprecated(&signature, &message[..], &public));
|
||||
assert!(!Pair::verify(&signature, &message[..], &public));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -776,7 +790,7 @@ mod test {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_from_wasm_works() {
|
||||
fn verify_from_old_wasm_works() {
|
||||
// The values in this test case are compared to the output of `node-test.js` in schnorrkel-js.
|
||||
//
|
||||
// This is to make sure that the wasm library is compatible.
|
||||
@@ -787,7 +801,8 @@ mod test {
|
||||
let js_signature = Signature::from_raw(hex!(
|
||||
"28a854d54903e056f89581c691c1f7d2ff39f8f896c9e9c22475e60902cc2b3547199e0e91fa32902028f2ca2355e8cdd16cfe19ba5e8b658c94aa80f3b81a00"
|
||||
));
|
||||
assert!(Pair::verify(&js_signature, b"SUBSTRATE", &public));
|
||||
assert!(Pair::verify_deprecated(&js_signature, b"SUBSTRATE", &public));
|
||||
assert!(!Pair::verify(&js_signature, b"SUBSTRATE", &public));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user