Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSP22 Permit #109

Merged
merged 23 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6fbda41
Add traits for PSP22Permit
varex83 Jul 13, 2023
bb821af
Add PSP22 Permit implementation and some tests
varex83 Jul 13, 2023
1a9c86c
Merge branch 'develop' into feature/psp22-permit
Artemka374 Aug 23, 2023
3da6a18
update permit implementation, use specific ink! commit for sr25519
Artemka374 Aug 23, 2023
e0d7e25
Merge remote-tracking branch 'origin/feature/psp22-permit' into featu…
Artemka374 Aug 23, 2023
814ac96
create signature type
Artemka374 Aug 28, 2023
ccd7fcb
use Signature enum for signatures and verifying
Artemka374 Aug 28, 2023
2ad103d
move signature to traits
Artemka374 Aug 28, 2023
4cea9d8
fix hashing compilation
Artemka374 Aug 28, 2023
1ab3a97
Merge remote-tracking branch 'origin/release/4.0.0-beta.1' into featu…
prxgr4mm3r Aug 28, 2023
c06f9db
permit e2e-tests
prxgr4mm3r Aug 28, 2023
ede4037
Merge remote-tracking branch 'origin/release/4.0.0-beta.1' into featu…
prxgr4mm3r Aug 28, 2023
b1dbc9c
e2e-tests
prxgr4mm3r Aug 28, 2023
ef1431c
Merge remote-tracking branch 'origin/develop' into feature/psp22-permit
Artemka374 Aug 28, 2023
829a81a
fix tests
Artemka374 Aug 28, 2023
6e9f928
Merge branch 'develop' into feature/psp22-permit
Artemka374 Aug 28, 2023
78d96dd
apply fmt
Artemka374 Aug 29, 2023
7b75b99
fix warning
Artemka374 Aug 29, 2023
7537268
add some e2e-tests
prxgr4mm3r Aug 29, 2023
d6a2fe5
refactor utils, use nonces and crypto in permit
Artemka374 Aug 29, 2023
4e3db4f
fix e2e tests
Artemka374 Aug 29, 2023
eabd132
fix lending build
Artemka374 Aug 29, 2023
ec84082
fix nonces build
Artemka374 Aug 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ timelock_controller = ["openbrush_contracts/timelock_controller"]
proxy = ["openbrush_contracts/proxy"]
diamond = ["openbrush_contracts/diamond"]
upgradeable = ["openbrush_contracts/upgradeable"]
governance = ["openbrush_contracts/governance"]
utils = ["openbrush_contracts/utils"]
governance = ["openbrush_contracts/governance", "openbrush_contracts/checkpoints"]
crypto = ["openbrush_contracts/crypto"]
nonces = ["openbrush_contracts/nonces"]
checkpoints = ["openbrush_contracts/checkpoints"]

test-all = [
"psp22",
Expand All @@ -85,7 +87,9 @@ test-all = [
"proxy",
"diamond",
"governance",
"utils",
"crypto",
"nonces",
"checkpoints"
]

[profile.release]
Expand Down
15 changes: 9 additions & 6 deletions contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ scale = { package = "parity-scale-codec", version = "3", default-features = fals
scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }

openbrush = { version = "~4.0.0-beta.1", package = "openbrush_lang", path = "../lang", default-features = false }
openbrush = { version = "~4.0.0-beta.1", package = "openbrush_lang", path = "../lang", default-features = false, features = ["crypto", "checkpoints"] }

pallet-assets-chain-extension = { git = "https://github.com/Brushfam/pallet-assets-chain-extension", branch = "polkadot-v0.9.37", default-features = false, features = ["ink-lang"] }

Expand All @@ -40,7 +40,7 @@ std = [
"openbrush/std",
"pallet-assets-chain-extension/ink-std",
]
psp22 = []
psp22 = ["nonces", "crypto"]
psp22_pallet = []
psp34 = []
psp37 = []
Expand All @@ -60,10 +60,14 @@ diamond = [
]
governance = [
"timelock_controller",
"utils"
"crypto",
"nonces",
"checkpoints"
]
utils = []
upgradeable = ["ownable"]
crypto = ["openbrush/crypto"]
nonces = []
checkpoints = ["openbrush/checkpoints"]
test-all = [
"psp22",
"psp34",
Expand All @@ -75,7 +79,6 @@ test-all = [
"pausable",
"timelock_controller",
"proxy",
"utils",
"diamond",
"governance",
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

pub use crate::utils::checkpoint::Checkpoints;
pub use openbrush::utils::checkpoints::Checkpoints;

#[derive(Debug, Default)]
#[openbrush::storage_item]
Expand Down
5 changes: 4 additions & 1 deletion contracts/src/governance/extensions/governor_quorum/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ pub trait QuorumImpl:

/// Returns the current quorum numerator
fn quorum_numerator(&self) -> u128 {
let history = self.data::<Data>().quorum_numerator_history.get_or_default();
let history = self
.data::<governor_quorum::Data>()
.quorum_numerator_history
.get_or_default();

let (exist, _, last_value) = history.latest_checkpoint();

Expand Down
73 changes: 28 additions & 45 deletions contracts/src/governance/governor/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,29 @@
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
use crate::governance::{
extensions::{
governor_settings::{
GovernorSettingsImpl,
GovernorSettingsInternal,
},
governor_votes::GovernorVotesInternal,
},
governor::{
Data,
GovernorEvents,
GovernorInternal,
GovernorStorageGetters,
TimestampProvider,
},
};
pub use crate::{
governance::governor,
traits::{
errors::governance::GovernanceError,
errors::GovernanceError,
governance::*,
types::SignatureType,
},
};
use crate::{
governance::{
extensions::{
governor_settings::{
GovernorSettingsImpl,
GovernorSettingsInternal,
},
governor_votes::GovernorVotesInternal,
},
governor::{
Data,
GovernorEvents,
GovernorInternal,
GovernorStorageGetters,
TimestampProvider,
},
types::Signature,
},
utils::crypto,
};
use ink::{
env::{
Expand Down Expand Up @@ -326,22 +323,15 @@ pub trait GovernorImpl:
proposal_id: ProposalId,
support: VoteType,
reason: String,
signature: SignatureType,
signature: Signature,
) -> Result<Balance, GovernanceError> {
let message = crypto::hash_message(
(proposal_id.clone(), support.clone(), reason.clone(), Vec::<u8>::new())
.encode()
.as_slice(),
)?;

let voter = Self::env().caller();
let valid = crypto::verify_signature(&message, &voter.clone(), &signature)?;
let message = (proposal_id.clone(), support.clone(), reason.clone(), Vec::<u8>::new()).encode();

if !valid {
return Err(GovernanceError::InvalidSignature(voter.clone()))
if !signature.verify(&message, &Self::env().caller()) {
return Err(GovernanceError::InvalidSignature(Self::env().caller()))
}

self._cast_vote(proposal_id, voter, support, reason)
self._cast_vote(proposal_id, Self::env().caller(), support, reason)
}

/// Casts a vote with signature and parameters for a proposal from a message sender. Returns the number of votes already casted for the proposal by the sender
Expand All @@ -350,23 +340,16 @@ pub trait GovernorImpl:
proposal_id: ProposalId,
support: VoteType,
reason: String,
signature: SignatureType,
signature: Signature,
params: Vec<u8>,
) -> Result<Balance, GovernanceError> {
let message = crypto::hash_message(
(proposal_id.clone(), support.clone(), reason.clone(), params.clone())
.encode()
.as_slice(),
)?;
let message = (proposal_id.clone(), support.clone(), reason.clone(), params.clone()).encode();

let voter = Self::env().caller();
let valid = crypto::verify_signature(&message, &voter.clone(), &signature)?;

if !valid {
return Err(GovernanceError::InvalidSignature(voter.clone()))
if !signature.verify(&message, &Self::env().caller()) {
return Err(GovernanceError::InvalidSignature(Self::env().caller()))
}

self._cast_vote_with_params(proposal_id, voter, support, reason, params)
self._cast_vote_with_params(proposal_id, Self::env().caller(), support, reason, params)
}

/// Relays a transaction or function call to an arbitrary target. In cases where the governance executor
Expand Down
22 changes: 12 additions & 10 deletions contracts/src/governance/governor/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::{
},
},
traits::{
errors::governance::GovernanceError,
errors::GovernanceError,
governance::{
CancelationStatus,
ExecutionStatus,
Expand All @@ -46,7 +46,6 @@ use crate::{
ALL_PROPOSAL_STATES,
},
},
utils::crypto,
};
use ink::{
env::{
Expand All @@ -65,12 +64,15 @@ use ink::{
vec::Vec,
},
};
use openbrush::traits::{
AccountId,
Balance,
DefaultEnv,
Storage,
String,
use openbrush::{
traits::{
AccountId,
Balance,
DefaultEnv,
Storage,
String,
},
utils::crypto,
};
use scale::Encode;

Expand All @@ -85,7 +87,7 @@ pub trait GovernorInternal:
) -> Result<HashType, GovernanceError> {
let message = (transactions, description_hash).encode();

crypto::hash_message(message.as_slice()).map_err(|err| err.into())
Ok(crypto::hash_blake2b256(message.as_slice()))
}

/// Current state of a proposal, following Compound's convention
Expand Down Expand Up @@ -326,7 +328,7 @@ pub trait GovernorInternal:

/// Return the hash of the description.
fn _hash_description(&self, description: String) -> Result<HashType, GovernanceError> {
Ok(crypto::hash_message(description.as_bytes())?)
Ok(crypto::hash_blake2b256(description.as_bytes()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/src/governance/utils/votes/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

use crate::utils::checkpoint::Checkpoints;
use openbrush::{
storage::Mapping,
traits::AccountId,
utils::checkpoints::Checkpoints,
};

#[derive(Default, Debug)]
Expand Down
27 changes: 10 additions & 17 deletions contracts/src/governance/utils/votes/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,8 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

use crate::{
governance::utils::votes::{
Data,
VotesEvents,
VotesInternal,
},
utils::{
crypto,
nonces::NoncesImpl,
},
governance::utils::votes::*,
nonces,
};
pub use crate::{
governance::{
Expand All @@ -39,9 +32,8 @@ pub use crate::{
traits::{
errors::GovernanceError,
governance::utils::votes::*,
types::SignatureType,
types::Signature,
},
utils::checkpoint::*,
};
use openbrush::traits::{
AccountId,
Expand All @@ -52,7 +44,7 @@ use openbrush::traits::{
use scale::Encode;

/// Common interface for `PSP22Votes`, and other `Votes`-enabled contracts.
pub trait VotesImpl: Storage<Data> + VotesInternal + NoncesImpl + VotesEvents + TimestampProvider {
pub trait VotesImpl: Storage<Data> + VotesInternal + nonces::NoncesImpl + VotesEvents + TimestampProvider {
/// The amount of votes owned by `account`.
fn get_votes(&self, account: AccountId) -> Balance {
self.data::<Data>()
Expand Down Expand Up @@ -110,16 +102,17 @@ pub trait VotesImpl: Storage<Data> + VotesInternal + NoncesImpl + VotesEvents +
&mut self,
signer: AccountId,
delegatee: AccountId,
nonce: u128,
nonce: u64,
expiry: Timestamp,
signature: SignatureType,
signature: Signature,
) -> Result<(), GovernanceError> {
if TimestampProvider::block_timestamp(self) > expiry {
return Err(GovernanceError::ExpiredSignature(expiry))
}
let message_hash = crypto::hash_message(Encode::encode(&(&delegatee, &nonce, &expiry)).as_slice())?;
let verify_result = crypto::verify_signature(&message_hash, &signer, &signature)?;
if !verify_result {

let message = (&delegatee, &nonce, &expiry).encode();

if !signature.verify(&message, &signer) {
return Err(GovernanceError::InvalidSignature(signer))
} else {
self._use_checked_nonce(&signer, nonce)?;
Expand Down
18 changes: 9 additions & 9 deletions contracts/src/governance/utils/votes/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ use crate::{
VotesEvents,
},
},
traits::errors::{
CheckpointsError,
GovernanceError,
traits::errors::GovernanceError,
};
use openbrush::{
traits::{
AccountId,
Balance,
Storage,
},
utils::checkpoint::{
utils::checkpoints::{
Checkpoint,
Checkpoints,
CheckpointsError,
},
};
use openbrush::traits::{
AccountId,
Balance,
Storage,
};

pub trait VotesInternal: Storage<Data> + VotesEvents + TimestampProvider {
/// Returns the total number of votes.
Expand Down
4 changes: 2 additions & 2 deletions contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ pub use upgradeability::diamond;
pub use upgradeability::proxy;
#[cfg(feature = "upgradeable")]
pub use upgradeability::upgradeable;
#[cfg(feature = "utils")]
pub use utils::*;
#[cfg(feature = "nonces")]
pub use utils::nonces;
Loading
Loading