-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:social account initialize with nostr public key (#20)
* social account initialize with nostr public key * format code with scarb fmt * reflect changes base on review requested * update scarb.toml with dependencies * scarb fmt * fix fail test * fix scarb.toml file * fix scarb toml file * make changes base on the code review * add snforge command * remove former test action * revert changes back * fix dependencies for snforge * scarb format * add check for event emission * log precalculated address * fix check event number emitted * scarb format * check for event name emitted * check for the right public key emitted * fix ugly code * remove type decleration * update scarb lock
- Loading branch information
Showing
6 changed files
with
128 additions
and
2 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
Empty file.
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
pub mod bech32; | ||
pub mod bip340; | ||
pub mod social_account; | ||
|
||
pub mod nostr_profile; | ||
pub mod social_pay_request; | ||
|
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,101 @@ | ||
use starknet::{ContractAddress, get_caller_address, get_contract_address, contract_address_const}; | ||
|
||
#[starknet::interface] | ||
pub trait ISocialPayAccount<TContractState> { | ||
fn get_public_key(self: @TContractState) -> u256; | ||
} | ||
|
||
|
||
#[starknet::contract] | ||
pub mod SocialPayAccount { | ||
#[storage] | ||
struct Storage { | ||
#[key] | ||
public_key: u256 | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
AccountCreated: AccountCreated, | ||
} | ||
|
||
#[derive(Drop, starknet::Event)] | ||
struct AccountCreated { | ||
#[key] | ||
public_key: u256 | ||
} | ||
|
||
#[constructor] | ||
fn constructor(ref self: ContractState, public_key: u256) { | ||
self.public_key.write(public_key); | ||
self.emit(AccountCreated { public_key: public_key }); | ||
} | ||
|
||
#[abi(embed_v0)] | ||
impl SocialPayAccount of super::ISocialPayAccount<ContractState> { | ||
fn get_public_key(self: @ContractState) -> u256 { | ||
self.public_key.read() | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use core::traits::Into; | ||
use core::array::ArrayTrait; | ||
use starknet::{ | ||
ContractAddress, get_caller_address, get_contract_address, contract_address_const | ||
}; | ||
use snforge_std as snf; | ||
use snforge_std::{ | ||
declare, ContractClassTrait, start_prank, stop_prank, CheatTarget, spy_events, SpyOn, | ||
EventSpy, EventFetcher, Event, EventAssertions | ||
}; | ||
use super::{ | ||
ISocialPayAccountDispatcher, ISocialPayAccountDispatcherTrait, | ||
ISocialPayAccountSafeDispatcher, ISocialPayAccountSafeDispatcherTrait | ||
}; | ||
|
||
|
||
const public_key: u256 = 45; | ||
|
||
|
||
fn deploy_social_account() -> ContractAddress { | ||
let contract = declare("SocialPayAccount"); | ||
|
||
let mut social_account_calldata = array![]; | ||
public_key.serialize(ref social_account_calldata); | ||
|
||
let address = contract.precalculate_address(@social_account_calldata); | ||
|
||
let mut spy = spy_events(SpyOn::One(address)); | ||
|
||
let deployed_contract_address = contract.deploy(@social_account_calldata).unwrap(); | ||
|
||
spy.fetch_events(); | ||
|
||
assert(spy.events.len() == 1, 'there should be one event'); | ||
|
||
let (_, event) = spy.events.at(0); | ||
assert(event.keys.at(0) == @selector!("AccountCreated"), 'Wrong event name'); | ||
|
||
let event_key = (*event.keys.at(1)).into(); | ||
|
||
assert(event_key == public_key, 'Wrong Public Key'); | ||
|
||
deployed_contract_address | ||
} | ||
|
||
|
||
#[test] | ||
fn test_get_public_key() { | ||
let contract_address = deploy_social_account(); | ||
let dispatcher = ISocialPayAccountDispatcher { contract_address }; | ||
|
||
let get_public_key = dispatcher.get_public_key(); | ||
|
||
assert!(get_public_key == 45, "Public key is not the same"); | ||
} | ||
} | ||
|