-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from argentlabs/feature/contract_guardian
Contract guardian
- Loading branch information
Showing
6 changed files
with
485 additions
and
53 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
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,94 @@ | ||
%lang starknet | ||
%builtins pedersen range_check ecdsa | ||
|
||
from starkware.cairo.common.cairo_builtins import HashBuiltin, SignatureBuiltin | ||
from starkware.cairo.common.signature import verify_ecdsa_signature | ||
from starkware.cairo.common.hash import hash2 | ||
from starkware.cairo.common.math import assert_not_zero, assert_nn | ||
from starkware.starknet.common.syscalls import get_tx_signature | ||
|
||
###################################### | ||
# Single Common Stark Key Guardian | ||
###################################### | ||
|
||
@storage_var | ||
func _signing_key() -> (res: felt): | ||
end | ||
@constructor | ||
func constructor{ | ||
syscall_ptr: felt*, | ||
pedersen_ptr: HashBuiltin*, | ||
range_check_ptr | ||
} ( | ||
signing_key: felt | ||
): | ||
assert_not_zero(signing_key) | ||
_signing_key.write(signing_key) | ||
return () | ||
end | ||
|
||
@external | ||
func set_signing_key{ | ||
syscall_ptr: felt*, | ||
pedersen_ptr: HashBuiltin*, | ||
ecdsa_ptr: SignatureBuiltin*, | ||
range_check_ptr | ||
} ( | ||
new_signing_key: felt | ||
) -> (): | ||
|
||
# get the signature | ||
let (sig_len : felt, sig : felt*) = get_tx_signature() | ||
# Verify the signature length. | ||
assert_nn(sig_len - 2) | ||
# Compute the hash of the message. | ||
let (hash) = hash2{hash_ptr=pedersen_ptr}(new_signing_key, 0) | ||
# get the existing signing key | ||
let (signing_key) = _signing_key.read() | ||
# verify the signature | ||
verify_ecdsa_signature( | ||
message=hash, | ||
public_key=signing_key, | ||
signature_r=sig[0], | ||
signature_s=sig[1]) | ||
# set the new key | ||
_signing_key.write(new_signing_key) | ||
return() | ||
end | ||
|
||
@view | ||
func is_valid_signature{ | ||
syscall_ptr: felt*, | ||
pedersen_ptr: HashBuiltin*, | ||
ecdsa_ptr: SignatureBuiltin*, | ||
range_check_ptr | ||
} ( | ||
hash: felt, | ||
sig_len: felt, | ||
sig: felt* | ||
) -> (): | ||
assert_nn(sig_len - 2) | ||
let (signing_key) = _signing_key.read() | ||
verify_ecdsa_signature( | ||
message=hash, | ||
public_key=signing_key, | ||
signature_r=sig[0], | ||
signature_s=sig[1]) | ||
return() | ||
end | ||
|
||
@view | ||
func weight() -> (weight: felt): | ||
return (weight=1) | ||
end | ||
@view | ||
func get_signing_key{ | ||
syscall_ptr: felt*, | ||
pedersen_ptr: HashBuiltin*, | ||
range_check_ptr | ||
} () -> (signing_key: felt): | ||
let (signing_key) = _signing_key.read() | ||
return (signing_key=signing_key) | ||
end |
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,107 @@ | ||
%lang starknet | ||
%builtins pedersen range_check ecdsa | ||
|
||
from starkware.cairo.common.cairo_builtins import HashBuiltin, SignatureBuiltin | ||
from starkware.cairo.common.signature import verify_ecdsa_signature | ||
from starkware.cairo.common.math import assert_not_zero | ||
from starkware.starknet.common.syscalls import get_caller_address | ||
|
||
############################################## | ||
# User selected different single keys Guardian | ||
############################################## | ||
|
||
@storage_var | ||
func _signing_key(account : felt) -> (res: felt): | ||
end | ||
@storage_var | ||
func _escape_key(account : felt) -> (res: felt): | ||
end | ||
@view | ||
func is_valid_signature{ | ||
syscall_ptr: felt*, | ||
pedersen_ptr: HashBuiltin*, | ||
ecdsa_ptr: SignatureBuiltin*, | ||
range_check_ptr | ||
} ( | ||
hash: felt, | ||
sig_len: felt, | ||
sig: felt* | ||
) -> (): | ||
|
||
let (account) = get_caller_address() | ||
if sig_len == 3: | ||
assert [sig + 2] = 'escape' | ||
let (key) = _escape_key.read(account) | ||
verify_ecdsa_signature( | ||
message=hash, | ||
public_key=key, | ||
signature_r=sig[0], | ||
signature_s=sig[1]) | ||
return() | ||
end | ||
|
||
if sig_len == 2: | ||
let (key) = _signing_key.read(account) | ||
verify_ecdsa_signature( | ||
message=hash, | ||
public_key=key, | ||
signature_r=sig[0], | ||
signature_s=sig[1]) | ||
return() | ||
end | ||
assert_not_zero(0) | ||
return() | ||
end | ||
|
||
@view | ||
func weight() -> (weight: felt): | ||
return (weight=1) | ||
end | ||
@external | ||
func set_signing_key{ | ||
syscall_ptr: felt*, | ||
pedersen_ptr: HashBuiltin*, | ||
range_check_ptr | ||
} ( | ||
key: felt | ||
) -> (): | ||
let (account) = get_caller_address() | ||
_signing_key.write(account, key) | ||
return() | ||
end | ||
|
||
@external | ||
func set_escape_key{ | ||
syscall_ptr: felt*, | ||
pedersen_ptr: HashBuiltin*, | ||
range_check_ptr | ||
} ( | ||
key: felt | ||
) -> (): | ||
let (account) = get_caller_address() | ||
_escape_key.write(account, key) | ||
return() | ||
end | ||
|
||
@view | ||
func get_signing_key{ | ||
syscall_ptr: felt*, | ||
pedersen_ptr: HashBuiltin*, | ||
range_check_ptr | ||
} (account: felt) -> (signing_key: felt): | ||
let (signing_key) = _signing_key.read(account) | ||
return (signing_key=signing_key) | ||
end | ||
|
||
@view | ||
func get_escape_key{ | ||
syscall_ptr: felt*, | ||
pedersen_ptr: HashBuiltin*, | ||
range_check_ptr | ||
} (account: felt) -> (escape_key: felt): | ||
let (escape_key) = _escape_key.read(account) | ||
return (escape_key=escape_key) | ||
end |
Oops, something went wrong.