Skip to content

Commit

Permalink
create signature type
Browse files Browse the repository at this point in the history
  • Loading branch information
Artemka374 committed Aug 28, 2023
1 parent e0d7e25 commit 814ac96
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
5 changes: 3 additions & 2 deletions contracts/src/token/psp22/extensions/permit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use openbrush::{
Balance,
Storage,
},
utils::Signature,
};
pub use psp22::{
Internal as _,
Expand Down Expand Up @@ -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)
}
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lang/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
25 changes: 25 additions & 0 deletions lang/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
}
}
}
}

0 comments on commit 814ac96

Please sign in to comment.