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

Verify signer control on setPublicKey #922

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
11 changes: 8 additions & 3 deletions src/account/account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#[starknet::component]
mod AccountComponent {
use openzeppelin::account::interface;
use openzeppelin::account::utils::add_owner::AddOwner;
use openzeppelin::account::utils::{MIN_TRANSACTION_VERSION, QUERY_VERSION, QUERY_OFFSET};
use openzeppelin::account::utils::{execute_calls, is_valid_stark_signature};
use openzeppelin::introspection::src5::SRC5Component::InternalTrait as SRC5InternalTrait;
Expand Down Expand Up @@ -156,8 +157,12 @@ mod AccountComponent {
/// - The caller must be the contract itself.
///
/// Emits an `OwnerRemoved` event.
fn set_public_key(ref self: ComponentState<TContractState>, new_public_key: felt252) {
fn set_public_key(ref self: ComponentState<TContractState>, new_public_key: felt252, signature: Span<felt252>) {
self.assert_only_self();

let hash = AddOwner::build_hash(new_public_key);
assert(self._is_valid_signature(hash, signature), Errors::INVALID_SIGNATURE);

self.emit(OwnerRemoved { removed_owner_guid: self.Account_public_key.read() });
self._set_public_key(new_public_key);
}
Expand Down Expand Up @@ -190,8 +195,8 @@ mod AccountComponent {
self.Account_public_key.read()
}

fn setPublicKey(ref self: ComponentState<TContractState>, newPublicKey: felt252) {
self.set_public_key(newPublicKey);
fn setPublicKey(ref self: ComponentState<TContractState>, newPublicKey: felt252, signature: Span<felt252>) {
self.set_public_key(newPublicKey, signature);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/account/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ trait IDeployable<TState> {
#[starknet::interface]
trait IPublicKey<TState> {
fn get_public_key(self: @TState) -> felt252;
fn set_public_key(ref self: TState, new_public_key: felt252);
fn set_public_key(ref self: TState, new_public_key: felt252, signature: Span<felt252>);
}

#[starknet::interface]
Expand All @@ -46,7 +46,7 @@ trait ISRC6CamelOnly<TState> {
#[starknet::interface]
trait IPublicKeyCamel<TState> {
fn getPublicKey(self: @TState) -> felt252;
fn setPublicKey(ref self: TState, newPublicKey: felt252);
fn setPublicKey(ref self: TState, newPublicKey: felt252, signature: Span<felt252>);
}

//
Expand All @@ -73,7 +73,7 @@ trait AccountABI<TState> {

// IPublicKey
fn get_public_key(self: @TState) -> felt252;
fn set_public_key(ref self: TState, new_public_key: felt252);
fn set_public_key(ref self: TState, new_public_key: felt252, signature: Span<felt252>);

// ISRC6CamelOnly
fn isValidSignature(self: @TState, hash: felt252, signature: Array<felt252>) -> felt252;
Expand All @@ -83,7 +83,7 @@ trait AccountABI<TState> {

// IPublicKeyCamel
fn getPublicKey(self: @TState) -> felt252;
fn setPublicKey(ref self: TState, newPublicKey: felt252);
fn setPublicKey(ref self: TState, newPublicKey: felt252, signature: Span<felt252>);
}

//
Expand Down
1 change: 1 addition & 0 deletions src/account/utils.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts for Cairo v0.9.0 (account/utils.cairo)

mod add_owner;
mod secp256k1;
mod signature;

Expand Down
52 changes: 52 additions & 0 deletions src/account/utils/add_owner.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

mod AddOwner {
use hash::{HashStateTrait, HashStateExTrait};
use pedersen::PedersenTrait;
use starknet::ContractAddress;
use starknet::get_contract_address;
use starknet::get_tx_info;

#[derive(Copy, Drop, Hash)]
struct StarkNetDomain {
name: felt252,
version: felt252,
chain_id: felt252
}

#[derive(Copy, Drop, Serde)]
struct AddOwner {
account: ContractAddress,
owner: felt252
}

fn build_hash(new_owner: felt252) -> felt252 {
let domain = StarkNetDomain {
name: 'Account.add_owner', version: 1, chain_id: get_tx_info().unbox().chain_id,
};

PedersenTrait::new(0)
.update('StarkNet Message')
.update(hash_domain(@domain))
.update(get_contract_address().into())
.update(hash_add_owner_message(new_owner))
.update(4)
.finalize()
}

fn hash_domain(domain: @StarkNetDomain) -> felt252 {
PedersenTrait::new(0)
.update(selector!("StarkNetDomain(name:felt,version:felt,chainId:felt)"))
.update_with(*domain)
.update(4)
.finalize()
}

fn hash_add_owner_message(new_owner: felt252) -> felt252 {
PedersenTrait::new(0)
.update(selector!("AddOwner(account:felt,owner:felt)"))
.update(get_contract_address().into())
.update(new_owner)
.update(3)
.finalize()
}
}
Loading