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

fix: stark sig auth account interaction #574

Merged
merged 7 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ env:
STARKNET_SIERRA_COMPILE_PATH: ./cairo/bin/starknet-sierra-compile
OBJC_DISABLE_INITIALIZE_FORK_SAFETY: YES
ADDRESS: "0x347be35996a21f6bf0623e75dbce52baba918ad5ae8d83b6f416045ab22961a"
PUBLIC_KEY: "0x674efe292c3c1125108916d6128bd6d1db4528db07322a84177551067aa2bef"
PK: "0xbdd640fb06671ad11c80317fa3b1799d"

on:
Expand Down
1 change: 1 addition & 0 deletions starknet/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ allowed-libfuncs-list.name = "audited"
sierra = true
casm = true
casm-add-pythonic-hints = true
build-external-contracts = ["openzeppelin::account::account::Account"]

[dependencies]
openzeppelin = { git = "https://github.com/ericnordelo/cairo-contracts", branch = "feat/erc20votes-#631" }
Expand Down
36 changes: 13 additions & 23 deletions starknet/src/authenticators/stark_sig.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use sx::types::{Strategy, IndexedStrategy, Choice};
#[starknet::interface]
trait IStarkSigAuthenticator<TContractState> {
/// Authenticates a propose transaction using the starknet EIP712-equivalent signature scheme.
/// Note: Only SNIP-6 compliant accounts are supported.
///
/// # Arguments
///
Expand All @@ -14,7 +15,6 @@ trait IStarkSigAuthenticator<TContractState> {
/// * `execution_strategy` - The execution strategy of the proposal.
/// * `user_proposal_validation_params` - The user proposal validation params of the proposal.
/// * `salt` - The salt, used for replay protection.
/// * `account_type` - The account type of the author ('snake' or 'camel').
fn authenticate_propose(
ref self: TContractState,
signature: Array<felt252>,
Expand All @@ -23,13 +23,13 @@ trait IStarkSigAuthenticator<TContractState> {
metadata_uri: Array<felt252>,
execution_strategy: Strategy,
user_proposal_validation_params: Array<felt252>,
salt: felt252,
account_type: felt252
salt: felt252
);


/// Authenticates a vote transaction using the starknet EIP712-equivalent signature scheme.
/// Salt is not needed because double voting is prevented by the space itself.
/// Note: Salt is not needed because double voting is prevented by the space itself.
/// Note: Only SNIP-6 compliant accounts are supported.
///
/// # Arguments
///
Expand All @@ -40,8 +40,6 @@ trait IStarkSigAuthenticator<TContractState> {
/// * `choice` - The choice of the voter.
/// * `user_voting_strategies` - The user voting strategies of the voter.
/// * `metadata_uri` - The URI of the proposal metadata.
/// * `account_type` - The account type of the voter ('snake' or 'camel').
///
fn authenticate_vote(
ref self: TContractState,
signature: Array<felt252>,
Expand All @@ -50,11 +48,11 @@ trait IStarkSigAuthenticator<TContractState> {
proposal_id: u256,
choice: Choice,
user_voting_strategies: Array<IndexedStrategy>,
metadata_uri: Array<felt252>,
account_type: felt252
metadata_uri: Array<felt252>
);

/// Authenticates an update proposal transaction using the starknet EIP712-equivalent signature scheme.
/// Note: Only SNIP-6 compliant accounts are supported.
///
/// # Arguments
///
Expand All @@ -65,7 +63,6 @@ trait IStarkSigAuthenticator<TContractState> {
/// * `execution_strategy` - The execution strategy of the proposal.
/// * `metadata_uri` - The URI of the proposal metadata.
/// * `salt` - The salt, used for replay protection.
/// * `account_type` - The account type of the author ('snake' or 'camel').
fn authenticate_update_proposal(
ref self: TContractState,
signature: Array<felt252>,
Expand All @@ -74,8 +71,7 @@ trait IStarkSigAuthenticator<TContractState> {
proposal_id: u256,
execution_strategy: Strategy,
metadata_uri: Array<felt252>,
salt: felt252,
account_type: felt252
salt: felt252
);
}

Expand Down Expand Up @@ -103,8 +99,7 @@ mod StarkSigAuthenticator {
metadata_uri: Array<felt252>,
execution_strategy: Strategy,
user_proposal_validation_params: Array<felt252>,
salt: felt252,
account_type: felt252
salt: felt252
) {
assert(!self._used_salts.read((author, salt)), 'Salt Already Used');

Expand All @@ -117,8 +112,7 @@ mod StarkSigAuthenticator {
metadata_uri.span(),
@execution_strategy,
user_proposal_validation_params.span(),
salt,
account_type
salt
);

self._used_salts.write((author, salt), true);
Expand All @@ -139,8 +133,7 @@ mod StarkSigAuthenticator {
proposal_id: u256,
choice: Choice,
user_voting_strategies: Array<IndexedStrategy>,
metadata_uri: Array<felt252>,
account_type: felt252
metadata_uri: Array<felt252>
) {
// No need to check salts here, as double voting is prevented by the space itself.

Expand All @@ -153,8 +146,7 @@ mod StarkSigAuthenticator {
proposal_id,
choice,
user_voting_strategies.span(),
metadata_uri.span(),
account_type
metadata_uri.span()
);

ISpaceDispatcher { contract_address: target }
Expand All @@ -175,8 +167,7 @@ mod StarkSigAuthenticator {
proposal_id: u256,
execution_strategy: Strategy,
metadata_uri: Array<felt252>,
salt: felt252,
account_type: felt252
salt: felt252
) {
assert(!self._used_salts.read((author, salt)), 'Salt Already Used');

Expand All @@ -189,8 +180,7 @@ mod StarkSigAuthenticator {
proposal_id,
@execution_strategy,
metadata_uri.span(),
salt,
account_type
salt
);

self._used_salts.write((author, salt), true);
Expand Down
15 changes: 0 additions & 15 deletions starknet/src/interfaces/i_account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,3 @@ trait AccountABI<TState> {
fn is_valid_signature(self: @TState, hash: felt252, signature: Array<felt252>) -> felt252;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we use the OZ library for the account interface? Or maybe there's a SNIP library somewhere we can use?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep imported from oz

fn supports_interface(self: @TState, interface_id: felt252) -> bool;
}

// Entry points case-convention is enforced by the protocol
#[starknet::interface]
trait AccountCamelABI<TState> {
fn __execute__(self: @TState, calls: Array<Call>) -> Array<Span<felt252>>;
fn __validate__(self: @TState, calls: Array<Call>) -> felt252;
fn __validate_declare__(self: @TState, classHash: felt252) -> felt252;
fn __validate_deploy__(
self: @TState, classHash: felt252, contractAddressSalt: felt252, _publicKey: felt252
) -> felt252;
fn setPublicKey(ref self: TState, newPublicKey: felt252);
fn getPublicKey(self: @TState) -> felt252;
fn isValidSignature(self: @TState, hash: felt252, signature: Array<felt252>) -> felt252;
fn supportsInterface(self: @TState, interfaceId: felt252) -> bool;
}
5 changes: 1 addition & 4 deletions starknet/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ mod factory {

mod interfaces {
mod i_account;
use i_account::{
AccountABI, AccountABIDispatcher, AccountABIDispatcherTrait, AccountCamelABI,
AccountCamelABIDispatcher, AccountCamelABIDispatcherTrait
};
use i_account::{AccountABI, AccountABIDispatcher, AccountABIDispatcherTrait};

mod i_execution_strategy;
use i_execution_strategy::{
Expand Down
4 changes: 2 additions & 2 deletions starknet/src/utils/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ const U256_TYPEHASH: felt252 = 0x1094260a770342332e6a73e9256b901d484a43892531620
// ------ ERC165 Interface Ids ------
// For more information, refer to: https://github.com/starknet-io/SNIPs/blob/main/SNIPS/snip-5.md

const ERC165_ACCOUNT_INTERFACE_ID: felt252 = 0xa66bd575; // snake
const ERC165_OLD_ACCOUNT_INTERFACE_ID: felt252 = 0x3943f10f; // camel
const ERC165_ACCOUNT_INTERFACE_ID: felt252 =
0x2ceccef7f994940b3962a6c67e0ba4fcd37df7d131417c604f91e03caecc1cd; // SNIP-6 compliant account ID, functions are snake case
58 changes: 20 additions & 38 deletions starknet/src/utils/stark_eip712.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ mod StarkEIP712 {
use sx::utils::StructHash;
use sx::utils::constants::{
STARKNET_MESSAGE, DOMAIN_TYPEHASH, PROPOSE_TYPEHASH, VOTE_TYPEHASH,
UPDATE_PROPOSAL_TYPEHASH, ERC165_ACCOUNT_INTERFACE_ID, ERC165_OLD_ACCOUNT_INTERFACE_ID
};
use sx::interfaces::{
AccountABIDispatcher, AccountABIDispatcherTrait, AccountCamelABIDispatcher,
AccountCamelABIDispatcherTrait
UPDATE_PROPOSAL_TYPEHASH, ERC165_ACCOUNT_INTERFACE_ID
};
use sx::interfaces::{AccountABIDispatcher, AccountABIDispatcherTrait,};

#[storage]
struct Storage {
Expand All @@ -34,8 +31,7 @@ mod StarkEIP712 {
metadata_uri: Span<felt252>,
execution_strategy: @Strategy,
user_proposal_validation_params: Span<felt252>,
salt: felt252,
account_type: felt252,
salt: felt252
) {
let digest: felt252 = self
.get_propose_digest(
Expand All @@ -47,7 +43,7 @@ mod StarkEIP712 {
salt
);

InternalImpl::verify_signature(digest, signature, author, account_type);
InternalImpl::verify_signature(digest, signature, author);
}

/// Verifies the signature of the vote calldata.
Expand All @@ -59,14 +55,13 @@ mod StarkEIP712 {
proposal_id: u256,
choice: Choice,
user_voting_strategies: Span<IndexedStrategy>,
metadata_uri: Span<felt252>,
account_type: felt252,
metadata_uri: Span<felt252>
) {
let digest: felt252 = self
.get_vote_digest(
target, voter, proposal_id, choice, user_voting_strategies, metadata_uri
);
InternalImpl::verify_signature(digest, signature, voter, account_type);
InternalImpl::verify_signature(digest, signature, voter);
}

/// Verifies the signature of the update proposal calldata.
Expand All @@ -78,14 +73,13 @@ mod StarkEIP712 {
proposal_id: u256,
execution_strategy: @Strategy,
metadata_uri: Span<felt252>,
salt: felt252,
account_type: felt252,
salt: felt252
) {
let digest: felt252 = self
.get_update_proposal_digest(
target, author, proposal_id, execution_strategy, metadata_uri, salt
);
InternalImpl::verify_signature(digest, signature, author, account_type);
InternalImpl::verify_signature(digest, signature, author);
}

/// Returns the digest of the propose calldata.
Expand Down Expand Up @@ -177,31 +171,19 @@ mod StarkEIP712 {
}

/// Verifies the signature of a message by calling the account contract.
fn verify_signature(
digest: felt252,
signature: Array<felt252>,
account: ContractAddress,
account_type: felt252
) {
if account_type == 'snake' {
assert(
AccountCamelABIDispatcher { contract_address: account }
.supportsInterface(ERC165_ACCOUNT_INTERFACE_ID) == true,
'Invalid Account'
);
AccountCamelABIDispatcher { contract_address: account }
.isValidSignature(digest, signature);
} else if account_type == 'camel' {
assert(
AccountABIDispatcher { contract_address: account }
.supports_interface(ERC165_OLD_ACCOUNT_INTERFACE_ID) == true,
'Invalid Account'
);
fn verify_signature(digest: felt252, signature: Array<felt252>, account: ContractAddress) {
// Only SNIP-6 compliant accounts are supported.
assert(
AccountABIDispatcher { contract_address: account }
.supports_interface(ERC165_ACCOUNT_INTERFACE_ID) == true,
'Invalid Account'
);

assert(
AccountABIDispatcher { contract_address: account }
.is_valid_signature(digest, signature);
} else {
panic_with_felt252('Invalid Account Type');
}
.is_valid_signature(digest, signature) == 'VALID',
'Invalid Signature'
);
}
}
}
Loading
Loading