[NFTs] Improve offchain signature validation (#13960)

* Improve signature validation

* Rework
This commit is contained in:
Jegor Sidorenko
2023-04-20 15:38:45 +03:00
committed by GitHub
parent 9544ec765d
commit 7a1fc72a4a
3 changed files with 56 additions and 6 deletions
@@ -18,6 +18,7 @@
//! Various pieces of common functionality.
use crate::*;
use frame_support::pallet_prelude::*;
impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Get the owner of the item, if the item exists.
@@ -30,6 +31,30 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
Collection::<T, I>::get(collection).map(|i| i.owner)
}
/// Validate the `data` was signed by `signer` and the `signature` is correct.
pub fn validate_signature(
data: &Vec<u8>,
signature: &T::OffchainSignature,
signer: &T::AccountId,
) -> DispatchResult {
if signature.verify(&**data, &signer) {
return Ok(())
}
// NOTE: for security reasons modern UIs implicitly wrap the data requested to sign into
// <Bytes></Bytes>, that's why we support both wrapped and raw versions.
let prefix = b"<Bytes>";
let suffix = b"</Bytes>";
let mut wrapped: Vec<u8> = Vec::with_capacity(data.len() + prefix.len() + suffix.len());
wrapped.extend(prefix);
wrapped.extend(data);
wrapped.extend(suffix);
ensure!(signature.verify(&*wrapped, &signer), Error::<T, I>::WrongSignature);
Ok(())
}
#[cfg(any(test, feature = "runtime-benchmarks"))]
pub fn set_next_id(id: T::CollectionId) {
NextCollectionId::<T, I>::set(Some(id));