Skip to content

Commit

Permalink
feat:social account initialize with nostr public key (#20)
Browse files Browse the repository at this point in the history
* 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
mubarak23 authored May 16, 2024
1 parent 8142079 commit 0fdfd95
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 2 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/starknet-contracts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v3
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: "nightly"
scarb-version: 'nightly'
- name: Check cairo format
run: scarb fmt --check
working-directory: onchain
Expand All @@ -24,7 +24,8 @@ jobs:
- uses: actions/checkout@v3
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: "nightly"
scarb-version: 'nightly'
- name: Run cairo tests
uses: foundry-rs/setup-snfoundry@v3
run: scarb test
working-directory: onchain
Empty file.
8 changes: 8 additions & 0 deletions onchain/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@ version = 1
[[package]]
name = "joyboy"
version = "0.1.0"
dependencies = [
"snforge_std",
]

[[package]]
name = "snforge_std"
version = "0.21.0"
source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.21.0#2996b8c1dd66b2715fc67e69578089f278a46790"
13 changes: 13 additions & 0 deletions onchain/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,16 @@ edition = "2023_11"
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

[dependencies]
starknet = "2.6.3"

[dev-dependencies]
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.21.0" }

#[lib]

[scripts]
test = "snforge test"

[[target.starknet-contract]]
casm = true
sierra = true
3 changes: 3 additions & 0 deletions onchain/src/lib.cairo
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;

101 changes: 101 additions & 0 deletions onchain/src/social_account.cairo
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");
}
}

0 comments on commit 0fdfd95

Please sign in to comment.