From ddf3638e229d2fab80ab9efa4814d1f0521d2ba4 Mon Sep 17 00:00:00 2001 From: Dominik | Supercolony <43150707+coreggon11@users.noreply.github.com> Date: Tue, 3 Oct 2023 12:50:17 +0200 Subject: [PATCH] revert approve removal --- contracts/src/token/psp22/psp22.rs | 6 ++++++ contracts/src/traits/psp22/psp22.rs | 15 +++++++++++++++ lang/codegen/src/implementations.rs | 10 ++++++++++ 3 files changed, 31 insertions(+) diff --git a/contracts/src/token/psp22/psp22.rs b/contracts/src/token/psp22/psp22.rs index 08316858c..61e8b48af 100644 --- a/contracts/src/token/psp22/psp22.rs +++ b/contracts/src/token/psp22/psp22.rs @@ -77,6 +77,12 @@ pub trait PSP22Impl: Storage + Internal { Ok(()) } + fn approve(&mut self, spender: AccountId, value: Balance) -> Result<(), PSP22Error> { + let owner = Self::env().caller(); + self._approve_from_to(owner, spender, value)?; + Ok(()) + } + fn increase_allowance(&mut self, spender: AccountId, delta_value: Balance) -> Result<(), PSP22Error> { let owner = Self::env().caller(); self._approve_from_to(owner, spender, self._allowance(&owner, &spender) + delta_value) diff --git a/contracts/src/traits/psp22/psp22.rs b/contracts/src/traits/psp22/psp22.rs index 9e4275d3a..5e375cb70 100644 --- a/contracts/src/traits/psp22/psp22.rs +++ b/contracts/src/traits/psp22/psp22.rs @@ -78,6 +78,21 @@ pub trait PSP22 { data: Vec, ) -> Result<(), PSP22Error>; + /// Allows `spender` to withdraw from the caller's account multiple times, up to + /// the `value` amount. + /// + /// If this function is called again it overwrites the current allowance with `value`. + /// + /// An `Approval` event is emitted. + /// + /// # Important + /// + /// This function is vulnerable to the double spending attack. + /// Based on the OpenZeppelin security review of OpenBrush, we recommend using the + /// `increase_allowance` and `decrease_allowance` functions. + #[ink(message)] + fn approve(&mut self, spender: AccountId, value: Balance) -> Result<(), PSP22Error>; + /// Atomically increases the allowance granted to `spender` by the caller. /// /// An `Approval` event is emitted. diff --git a/lang/codegen/src/implementations.rs b/lang/codegen/src/implementations.rs index 193c66754..4c0b45659 100644 --- a/lang/codegen/src/implementations.rs +++ b/lang/codegen/src/implementations.rs @@ -170,6 +170,11 @@ pub(crate) fn impl_psp22(impl_args: &mut ImplArgs, capped: bool) { PSP22Impl::transfer_from(self, from, to, value, data) } + #[ink(message)] + fn approve(&mut self, spender: AccountId, value: Balance) -> Result<(), PSP22Error> { + PSP22Impl::approve(self, spender, value) + } + #[ink(message)] fn increase_allowance(&mut self, spender: AccountId, delta_value: Balance) -> Result<(), PSP22Error> { PSP22Impl::increase_allowance(self, spender, delta_value) @@ -891,6 +896,11 @@ pub(crate) fn impl_psp22_pallet(impl_args: &mut ImplArgs) { PSP22PalletImpl::transfer_from(self, from, to, value, data) } + #[ink(message)] + fn approve(&mut self, spender: AccountId, value: Balance) -> Result<(), PSP22Error> { + PSP22PalletImpl::approve(self, spender, value) + } + #[ink(message)] fn increase_allowance(&mut self, spender: AccountId, delta_value: Balance) -> Result<(), PSP22Error> { PSP22PalletImpl::increase_allowance(self, spender, delta_value)