mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-18 02:31:03 +00:00
[NFTs] Improve offchain signature validation (#13960)
* Improve signature validation * Rework
This commit is contained in:
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user