-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: stark sig auth * chore: test stark sig auth * feat: propose digest * chore: starknetjs test scripts * feat: verify propose sig * chore: cleaned up stark sig test * chore: removed: dead code * feat: vote and update proposal sig auth * chore: typescript linting + style * chore: update yarn lock * chore: tests for vote and update proposal starknet sig authentication * feat: compute domain hash * chore: account inteface * chore: updated tests * feat: use er165 to check account and sig * chore: updated tests * chore: formatting * chore: type hash comments * refactor: cleanup contracts and test * fix: revert if salt used on propose and update * chore: some comments * chore: cleaned up test * chore: .env example * chore: updated lint settings * chore: formatting --------- Co-authored-by: Orlando <[email protected]>
- Loading branch information
Showing
22 changed files
with
4,098 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
NETWORK_URL= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ethereum/lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"parser": "@typescript-eslint/parser", | ||
"extends": [ | ||
"plugin:@typescript-eslint/recommended" | ||
], | ||
"plugins": [ | ||
"prettier", | ||
"@typescript-eslint" | ||
], | ||
"parserOptions": { | ||
"ecmaVersion": 2018, | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"prettier/prettier": "error", | ||
"@typescript-eslint/no-explicit-any": "off" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"semi": true, | ||
"singleQuote": true, | ||
"printWidth": 100, | ||
"tabWidth": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { JestConfigWithTsJest } from 'ts-jest'; | ||
|
||
const jestConfig: JestConfigWithTsJest = { | ||
// [...] | ||
// Replace `ts-jest` with the preset you want to use | ||
// from the above list | ||
preset: 'ts-jest', | ||
roots: ['<rootDir>/starknet/tests'], | ||
}; | ||
|
||
export default jestConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,6 @@ mod eth_tx; | |
|
||
mod eth_sig; | ||
|
||
mod stark_sig; | ||
|
||
mod stark_tx; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use array::ArrayTrait; | ||
use array::SpanTrait; | ||
use starknet::account::Call; | ||
use starknet::ContractAddress; | ||
|
||
// Interfaces from OZ: https://github.com/OpenZeppelin/cairo-contracts/blob/cairo-2/src/account/interface.cairo | ||
#[starknet::interface] | ||
trait AccountABI<TState> { | ||
fn __execute__(self: @TState, calls: Array<Call>) -> Array<Span<felt252>>; | ||
fn __validate__(self: @TState, calls: Array<Call>) -> felt252; | ||
fn __validate_declare__(self: @TState, class_hash: felt252) -> felt252; | ||
fn __validate_deploy__( | ||
self: @TState, class_hash: felt252, contract_address_salt: felt252, _public_key: felt252 | ||
) -> felt252; | ||
fn set_public_key(ref self: TState, new_public_key: felt252); | ||
fn get_public_key(self: @TState) -> felt252; | ||
fn is_valid_signature(self: @TState, hash: felt252, signature: Array<felt252>) -> felt252; | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.