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

feat:social account initialize with nostr public key #20

Merged
merged 24 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 21 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
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.
14 changes: 14 additions & 0 deletions onchain/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,17 @@ version = 1
[[package]]
name = "joyboy"
version = "0.1.0"
dependencies = [
"openzeppelin",
"snforge_std",
]

[[package]]
name = "openzeppelin"
version = "0.11.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.11.0#a83f36b23f1af6e160288962be4a2701c3ecbcda"

[[package]]
name = "snforge_std"
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
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;

98 changes: 98 additions & 0 deletions onchain/src/social_account.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use starknet::{ContractAddress, get_caller_address, get_contract_address, contract_address_const};
// use traits::TryInto;
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
// use traits::Into;

#[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,
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
}

#[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);
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
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};


const public_key: u256 = 45;

fn deploy_social_account() -> ContractAddress {
let contract = declare("SocialPayAccount");

mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
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');
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved

let (_, event) = spy.events.at(0);
assert(event.keys.at(0) == @selector!("AccountCreated"), 'Wrong event name');

let key_felt252 = *event.keys.at(1);
let key: u256 = key_felt252.into();
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
assert(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 == public_key, "Public key is not the same");
}
}
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved