Skip to content

Commit

Permalink
Merge branch 'develop' into chore_eth_sig_auth_test
Browse files Browse the repository at this point in the history
  • Loading branch information
Orlando committed Aug 17, 2023
2 parents 95a3a70 + dda48da commit 1568ceb
Show file tree
Hide file tree
Showing 41 changed files with 1,518 additions and 316 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1 @@
NETWORK_URL
NETWORK_URL=
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license": "MIT",
"scripts": {
"format:ts": "eslint . --ext .ts --fix",
"test:eth-sig-auth": "jest -c jest.config.ts"
"test:sig-auths": "jest -c jest.config.ts --runInBand"
},
"devDependencies": {
"@types/jest": "^29.5.3",
Expand All @@ -23,9 +23,7 @@
"starknet": "^5.14.1",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
},
"dependencies": {
"typescript": "^5.1.6",
"prettier": "^3.0.1"
}
}
4 changes: 4 additions & 0 deletions starknet/src/authenticators.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ mod vanilla;
mod eth_tx;

mod eth_sig;

mod stark_sig;

mod stark_tx;
8 changes: 4 additions & 4 deletions starknet/src/authenticators/eth_tx.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ mod EthTxAuthenticator {
user_proposal_validation_params: Array<felt252>,
metadata_URI: Array<felt252>
) {
let mut payload = ArrayTrait::<felt252>::new();
let mut payload = array![];
target.serialize(ref payload);
PROPOSE_SELECTOR.serialize(ref payload);
author.serialize(ref payload);
Expand Down Expand Up @@ -92,7 +92,7 @@ mod EthTxAuthenticator {
user_voting_strategies: Array<IndexedStrategy>,
metadata_URI: Array<felt252>
) {
let mut payload = ArrayTrait::<felt252>::new();
let mut payload = array![];
target.serialize(ref payload);
VOTE_SELECTOR.serialize(ref payload);
voter.serialize(ref payload);
Expand Down Expand Up @@ -124,7 +124,7 @@ mod EthTxAuthenticator {
execution_strategy: Strategy,
metadata_URI: Array<felt252>
) {
let mut payload = ArrayTrait::<felt252>::new();
let mut payload = array![];
target.serialize(ref payload);
UPDATE_PROPOSAL_SELECTOR.serialize(ref payload);
author.serialize(ref payload);
Expand Down Expand Up @@ -163,7 +163,7 @@ mod EthTxAuthenticator {

fn consume_commit(ref self: ContractState, hash: felt252, sender_address: EthAddress) {
let committer_address = self._commits.read(hash);
assert(committer_address.is_zero(), 'Commit not found');
assert(!committer_address.is_zero(), 'Commit not found');
assert(committer_address == sender_address, 'Invalid sender address');
// Delete the commit to prevent replay attacks.
self._commits.write(hash, Zeroable::zero());
Expand Down
172 changes: 172 additions & 0 deletions starknet/src/authenticators/stark_sig.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
use starknet::ContractAddress;
use sx::types::{Strategy, IndexedStrategy, Choice};

#[starknet::interface]
trait IStarkSigAuthenticator<TContractState> {
fn authenticate_propose(
ref self: TContractState,
signature: Array<felt252>,
target: ContractAddress,
author: ContractAddress,
execution_strategy: Strategy,
user_proposal_validation_params: Array<felt252>,
metadata_URI: Array<felt252>,
salt: felt252,
account_type: felt252
);
fn authenticate_vote(
ref self: TContractState,
signature: Array<felt252>,
target: ContractAddress,
voter: ContractAddress,
proposal_id: u256,
choice: Choice,
user_voting_strategies: Array<IndexedStrategy>,
metadata_URI: Array<felt252>,
account_type: felt252
);
fn authenticate_update_proposal(
ref self: TContractState,
signature: Array<felt252>,
target: ContractAddress,
author: ContractAddress,
proposal_id: u256,
execution_strategy: Strategy,
metadata_URI: Array<felt252>,
salt: felt252,
account_type: felt252
);
}

#[starknet::contract]
mod StarkSigAuthenticator {
use super::IStarkSigAuthenticator;
use starknet::{ContractAddress, info};
use core::array::{ArrayTrait, SpanTrait};
use serde::Serde;
use sx::space::space::{ISpaceDispatcher, ISpaceDispatcherTrait};
use sx::types::{Strategy, IndexedStrategy, UserAddress, Choice};
use sx::utils::stark_eip712;

#[storage]
struct Storage {
_domain_hash: felt252,
_used_salts: LegacyMap::<(ContractAddress, felt252), bool>
}

#[external(v0)]
impl StarkSigAuthenticator of IStarkSigAuthenticator<ContractState> {
fn authenticate_propose(
ref self: ContractState,
signature: Array<felt252>,
target: ContractAddress,
author: ContractAddress,
execution_strategy: Strategy,
user_proposal_validation_params: Array<felt252>,
metadata_URI: Array<felt252>,
salt: felt252,
account_type: felt252
) {
assert(!self._used_salts.read((author, salt)), 'Salt Already Used');

stark_eip712::verify_propose_sig(
self._domain_hash.read(),
signature,
target,
author,
@execution_strategy,
user_proposal_validation_params.span(),
metadata_URI.span(),
salt,
account_type
);

self._used_salts.write((author, salt), true);
ISpaceDispatcher {
contract_address: target
}
.propose(
UserAddress::Starknet(author),
execution_strategy,
user_proposal_validation_params,
metadata_URI
);
}

fn authenticate_vote(
ref self: ContractState,
signature: Array<felt252>,
target: ContractAddress,
voter: ContractAddress,
proposal_id: u256,
choice: Choice,
user_voting_strategies: Array<IndexedStrategy>,
metadata_URI: Array<felt252>,
account_type: felt252
) {
// No need to check salts here, as double voting is prevented by the space itself.

stark_eip712::verify_vote_sig(
self._domain_hash.read(),
signature,
target,
voter,
proposal_id,
choice,
user_voting_strategies.span(),
metadata_URI.span(),
account_type
);

ISpaceDispatcher {
contract_address: target
}
.vote(
UserAddress::Starknet(voter),
proposal_id,
choice,
user_voting_strategies,
metadata_URI
);
}

fn authenticate_update_proposal(
ref self: ContractState,
signature: Array<felt252>,
target: ContractAddress,
author: ContractAddress,
proposal_id: u256,
execution_strategy: Strategy,
metadata_URI: Array<felt252>,
salt: felt252,
account_type: felt252
) {
assert(!self._used_salts.read((author, salt)), 'Salt Already Used');

stark_eip712::verify_update_proposal_sig(
self._domain_hash.read(),
signature,
target,
author,
proposal_id,
@execution_strategy,
metadata_URI.span(),
salt,
account_type
);

self._used_salts.write((author, salt), true);
ISpaceDispatcher {
contract_address: target
}
.update_proposal(
UserAddress::Starknet(author), proposal_id, execution_strategy, metadata_URI
);
}
}
#[constructor]
fn constructor(ref self: ContractState, name: felt252, version: felt252) {
// TODO: store domain hash in stark_eip712 component once syntax is live.
self._domain_hash.write(stark_eip712::get_domain_hash(name, version));
}
}
108 changes: 108 additions & 0 deletions starknet/src/authenticators/stark_tx.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use starknet::{ContractAddress};
use sx::types::{Strategy, IndexedStrategy, Choice};

#[starknet::interface]
trait IStarkTxAuthenticator<TContractState> {
fn authenticate_propose(
ref self: TContractState,
space: ContractAddress,
author: ContractAddress,
execution_strategy: Strategy,
user_proposal_validation_params: Array<felt252>,
metadata_URI: Array<felt252>
);
fn authenticate_vote(
ref self: TContractState,
space: ContractAddress,
voter: ContractAddress,
proposal_id: u256,
choice: Choice,
user_voting_strategies: Array<IndexedStrategy>,
metadata_URI: Array<felt252>
);
fn authenticate_update_proposal(
ref self: TContractState,
space: ContractAddress,
author: ContractAddress,
proposal_id: u256,
execution_strategy: Strategy,
metadata_URI: Array<felt252>
);
}

#[starknet::contract]
mod StarkTxAuthenticator {
use super::IStarkTxAuthenticator;
use starknet::{ContractAddress, info};
use core::array::ArrayTrait;
use sx::space::space::{ISpaceDispatcher, ISpaceDispatcherTrait};
use sx::types::{UserAddress, Strategy, IndexedStrategy, Choice};

#[storage]
struct Storage {}

#[external(v0)]
impl StarkTxAuthenticator of IStarkTxAuthenticator<ContractState> {
fn authenticate_propose(
ref self: ContractState,
space: ContractAddress,
author: ContractAddress,
execution_strategy: Strategy,
user_proposal_validation_params: Array<felt252>,
metadata_URI: Array<felt252>
) {
assert(info::get_caller_address() == author, 'Invalid Caller');

ISpaceDispatcher {
contract_address: space
}
.propose(
UserAddress::Starknet(author),
execution_strategy,
user_proposal_validation_params,
metadata_URI
);
}

fn authenticate_vote(
ref self: ContractState,
space: ContractAddress,
voter: ContractAddress,
proposal_id: u256,
choice: Choice,
user_voting_strategies: Array<IndexedStrategy>,
metadata_URI: Array<felt252>
) {
assert(info::get_caller_address() == voter, 'Invalid Caller');

ISpaceDispatcher {
contract_address: space
}
.vote(
UserAddress::Starknet(voter),
proposal_id,
choice,
user_voting_strategies,
metadata_URI
);
}

fn authenticate_update_proposal(
ref self: ContractState,
space: ContractAddress,
author: ContractAddress,
proposal_id: u256,
execution_strategy: Strategy,
metadata_URI: Array<felt252>
) {
assert(info::get_caller_address() == author, 'Invalid Caller');

ISpaceDispatcher {
contract_address: space
}
.update_proposal(
UserAddress::Starknet(author), proposal_id, execution_strategy, metadata_URI
);
}
}
}
2 changes: 1 addition & 1 deletion starknet/src/execution_strategies/eth_relayer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod EthRelayerExecutionStrategy {
// keccak hash of the proposal execution payload
let execution_hash = u256 { low: payload[2], high: payload[1] };

let mut message_payload = ArrayTrait::<felt252>::new();
let mut message_payload = array![];
space.serialize(mut message_payload);
proposal.serialize(mut message_payload);
votes_for.serialize(mut message_payload);
Expand Down
8 changes: 4 additions & 4 deletions starknet/src/execution_strategies/simple_quorum.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ mod SimpleQuorumExecutionStrategy {
let accepted = _quorum_reached(self._quorum.read(), votes_for, votes_against, votes_abstain)
& _supported(votes_for, votes_against);

let block_number = info::get_block_number().try_into().unwrap();
let timestamp = info::get_block_timestamp().try_into().unwrap();
if *proposal.finalization_status == FinalizationStatus::Cancelled(()) {
ProposalStatus::Cancelled(())
} else if *proposal.finalization_status == FinalizationStatus::Executed(()) {
ProposalStatus::Executed(())
} else if block_number < *proposal.start_block_number {
} else if timestamp < *proposal.start_timestamp {
ProposalStatus::VotingDelay(())
} else if block_number < *proposal.min_end_block_number {
} else if timestamp < *proposal.min_end_timestamp {
ProposalStatus::VotingPeriod(())
} else if block_number < *proposal.max_end_block_number {
} else if timestamp < *proposal.max_end_timestamp {
if accepted {
ProposalStatus::VotingPeriodAccepted(())
} else {
Expand Down
5 changes: 5 additions & 0 deletions starknet/src/interfaces.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod i_voting_strategy;
mod i_execution_strategy;
mod i_proposal_validation_strategy;
mod i_account;

use i_voting_strategy::{IVotingStrategy, IVotingStrategyDispatcher, IVotingStrategyDispatcherTrait};
use i_execution_strategy::{
Expand All @@ -10,3 +11,7 @@ use i_proposal_validation_strategy::{
IProposalValidationStrategy, IProposalValidationStrategyDispatcher,
IProposalValidationStrategyDispatcherTrait
};
use i_account::{
AccountABI, AccountABIDispatcher, AccountABIDispatcherTrait, AccountCamelABI,
AccountCamelABIDispatcher, AccountCamelABIDispatcherTrait
};
Loading

0 comments on commit 1568ceb

Please sign in to comment.