From 814ac9636e4bcd2b19841bfb216a67a823ac44ae Mon Sep 17 00:00:00 2001 From: Artemka374 Date: Mon, 28 Aug 2023 11:23:58 +0300 Subject: [PATCH] create signature type --- .../src/token/psp22/extensions/permit.rs | 5 ++-- lang/Cargo.toml | 2 +- lang/src/utils.rs | 25 +++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/contracts/src/token/psp22/extensions/permit.rs b/contracts/src/token/psp22/extensions/permit.rs index 7b0f2c737..83324c252 100644 --- a/contracts/src/token/psp22/extensions/permit.rs +++ b/contracts/src/token/psp22/extensions/permit.rs @@ -39,6 +39,7 @@ use openbrush::{ Balance, Storage, }, + utils::Signature, }; pub use psp22::{ Internal as _, @@ -72,7 +73,7 @@ pub trait PSP22PermitImpl: Internal { spender: AccountId, amount: Balance, deadline: u64, - signature: [u8; 64], + signature: Signature, ) -> Result<(), PSP22Error> { self._permit(owner, spender, amount, deadline, signature) } @@ -93,7 +94,7 @@ pub trait Internal { spender: AccountId, amount: Balance, deadline: u64, - signature: [u8; 64], + signature: Signature, ) -> Result<(), PSP22Error>; fn _nonces(&self, owner: AccountId) -> u64; diff --git a/lang/Cargo.toml b/lang/Cargo.toml index ef482d3c1..67ba7f66f 100644 --- a/lang/Cargo.toml +++ b/lang/Cargo.toml @@ -16,7 +16,7 @@ include = ["Cargo.toml", "src/**/*.rs"] [dependencies] openbrush_lang_macro = { version = "~4.0.0-beta", path = "macro", default-features = false } -ink = { version = "4.2.1", default-features = false} +ink = { git = "https://github.com/paritytech/ink", rev = "a71990f", default-features = false} scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } scale-info = { version = "2.6", default-features = false, features = ["derive"] } diff --git a/lang/src/utils.rs b/lang/src/utils.rs index 32974687a..4586d158a 100644 --- a/lang/src/utils.rs +++ b/lang/src/utils.rs @@ -22,6 +22,7 @@ pub use const_format; pub use xxhash_rust; +use crate::traits::AccountId; use xxhash_rust::const_xxh32::xxh32; /// The value 0 is a valid seed. @@ -34,3 +35,27 @@ impl ConstHasher { xxh32(str.as_bytes(), XXH32_SEED) } } + +#[derive(Debug, Default, PartialEq, Eq, Clone, Copy, scale::Encode, scale::Decode)] +pub enum SignatureType { + #[default] + ECDSA, + SR25519, +} + +#[derive(Debug, Default, PartialEq, Eq, Clone, Copy, scale::Encode, scale::Decode)] +pub struct Signature { + pub signature_type: SignatureType, + pub raw_signature: [u8], +} + +impl Signature { + pub fn verify(&self, message: &[u8], pub_key: &AccountId) -> bool { + match self.signature_type { + SignatureType::ECDSA => ink::env::ecdsa_recover(&self.raw_signature.into(), message.into()).is_ok(), + SignatureType::SR25519 => { + ink::env::sr25519_verify(&self.raw_signature.into(), message, pub_key.as_ref()).is_ok() + } + } + } +}