Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Feat/new signatures support #150

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions p256-crypto/src/multi_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_core::{crypto::UncheckedFrom, ecdsa, ed25519, sr25519, RuntimeDebug, H256};
use sp_core::{crypto::UncheckedFrom, ecdsa, ed25519, sr25519, ConstU32, RuntimeDebug, H256};
use sp_runtime::{
traits::{IdentifyAccount, Lazy, Verify},
AccountId32, MultiSignature as SPMultiSignature, MultiSigner as SPMultiSigner,
AccountId32, BoundedVec, MultiSignature as SPMultiSignature, MultiSigner as SPMultiSigner,
};

use crate::application_crypto::p256::{Public, Signature};

pub type AuthenticatorData = BoundedVec<u8, ConstU32<37>>;
pub type MessagePrefix = BoundedVec<u8, ConstU32<100>>;

#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Eq, PartialEq, Clone, Encode, Decode, MaxEncodedLen, RuntimeDebug, TypeInfo)]
pub enum MultiSignature {
Expand All @@ -22,6 +25,10 @@ pub enum MultiSignature {
Ecdsa(ecdsa::Signature),
/// An ECDSA/SECP256r1 signature
P256(Signature),
/// An ECDSA/SECP256r1 signature with additional authenticator data
P256WithAuthData(Signature, AuthenticatorData),
/// An Ed25519 signature with message prefix.
Ed25519WithPrefix(ed25519::Signature, MessagePrefix),
}

impl From<ed25519::Signature> for MultiSignature {
Expand Down Expand Up @@ -252,6 +259,30 @@ impl Verify for MultiSignature {
})
.unwrap_or(false)
}
(Self::P256WithAuthData(sig, auth_data), who) => {
p256::ecdsa::recoverable::Signature::try_from(sig.as_ref())
.and_then(|signature| {
let msg_bytes = msg.get();
let msg = if msg_bytes.len() != 32 {
sp_io::hashing::sha2_256(msg_bytes)
} else {
msg_bytes.try_into().unwrap()
};
let signed_msg =
sp_io::hashing::sha2_256(&[auth_data.as_slice(), &msg].concat());
signature.recover_verifying_key(&signed_msg)
})
.map(|pubkey| {
&sp_io::hashing::blake2_256(pubkey.to_bytes().as_slice())
== <dyn AsRef<[u8; 32]>>::as_ref(who)
})
.unwrap_or(false)
}
(Self::Ed25519WithPrefix(sig, prefix), _) => {
let msig: SPMultiSignature = sig.clone().into();
let message = sp_io::hashing::blake2_256(&[prefix.as_slice(), msg.get()].concat());
msig.verify(&message[..], signer)
}
}
}
}
Loading