Skip to content

Commit

Permalink
handle_transfer_request refactor (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejka authored May 23, 2024
1 parent 1a09aee commit 3987968
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 139 deletions.
2 changes: 1 addition & 1 deletion onchain/src/social.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pub mod account;
pub mod bech32;
pub mod profile;
pub mod request;
pub mod transfer_request;
pub mod transfer;
35 changes: 5 additions & 30 deletions onchain/src/social/account.cairo
Original file line number Diff line number Diff line change
@@ -1,43 +1,18 @@
use starknet::{ContractAddress, get_caller_address, get_contract_address, contract_address_const};
use super::profile::NostrProfile;

// request types added temporarily for the OD Hack
#[derive(Copy, Drop, Debug, Serde)]
pub struct Signature {
r: u256,
s: u256
}

#[derive(Drop, Serde)]
pub struct TransferRequest {
amount: u256,
token: felt252,
joyboy: NostrProfile,
recipient: NostrProfile
}

#[derive(Drop, Serde)]
pub struct SocialRequest {
pubkey: u256,
created_at: u64,
kind: u16,
tags: ByteArray, // we don't need to look inside the tags(at least for now)
content: TransferRequest,
sig: Signature
}

use super::request::SocialRequest;
use super::transfer::{Transfer};

#[starknet::interface]
pub trait ISocialAccount<TContractState> {
fn get_public_key(self: @TContractState) -> u256;
fn handle_transfer_request(ref self: TContractState, request: SocialRequest);
fn handle_transfer_request(ref self: TContractState, request: SocialRequest<Transfer>);
}


#[starknet::contract]
pub mod SocialAccount {
use super::SocialRequest;
use super::TransferRequest;
use super::Transfer;

#[storage]
struct Storage {
Expand Down Expand Up @@ -69,7 +44,7 @@ pub mod SocialAccount {
self.public_key.read()
}
fn handle_transfer_request(
ref self: ContractState, request: SocialRequest
ref self: ContractState, request: SocialRequest<Transfer>
) { // TODO: implement handle transfer logic
}
}
Expand Down
75 changes: 39 additions & 36 deletions onchain/src/social/profile.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,46 @@ pub struct NostrProfile {
pub relays: Array<ByteArray> //UTF-8 encoded
}

/// nip19 bech32 encoding of NostrProfile
///
/// Spec: https://github.com/nostr-protocol/nips/blob/master/19.md
/// Sample implementation:
/// https://github.com/nbd-wtf/nostr-tools/blob/master/nip19.ts#L182
///
/// # Parameters:
/// - `n_profile` - profile to be encoded
/// # Returns:
// bech32 encoding of NostrProfile
pub fn encode(profile: @NostrProfile) -> ByteArray {
let mut data: ByteArray = Default::default();

let mut relays = profile.relays.span();
loop {
match relays.pop_front() {
Option::Some(relay) => {
data.append_byte(1);
data.append_byte(relay.len().try_into().unwrap());
data.append(relay);
},
Option::None => { break (); },
}
};

data.append_byte(0);
data.append_byte(32);
data.append_word(profile.public_key.high.clone().into(), 16);
data.append_word(profile.public_key.low.clone().into(), 16);

let len = data.len();
bech32::encode(@"nprofile", @data, len)
#[generate_trait]
pub impl NostrProfileImpl of NostrProfileTrait {
/// nip19 bech32 encoding of NostrProfile
///
/// Spec: https://github.com/nostr-protocol/nips/blob/master/19.md
/// Sample implementation:
/// https://github.com/nbd-wtf/nostr-tools/blob/master/nip19.ts#L182
///
/// # Parameters:
/// - `self` - profile to be encoded
/// # Returns:
// bech32 encoding of NostrProfile
fn encode(self: @NostrProfile) -> ByteArray {
let mut data: ByteArray = Default::default();

let mut relays = self.relays.span();
loop {
match relays.pop_front() {
Option::Some(relay) => {
data.append_byte(1);
data.append_byte(relay.len().try_into().unwrap());
data.append(relay);
},
Option::None => { break (); },
}
};

data.append_byte(0);
data.append_byte(32);
data.append_word(self.public_key.high.clone().into(), 16);
data.append_word(self.public_key.low.clone().into(), 16);

let len = data.len();
bech32::encode(@"nprofile", @data, len)
}
}

#[cfg(test)]
mod tests {
use super::{NostrProfile, encode};
use super::{NostrProfile, NostrProfileTrait};

// test data generated with: https://replit.com/@maciejka/WanIndolentKilobyte

Expand All @@ -67,7 +70,7 @@ mod tests {
let expected =
"nprofile1qyh8wumn8ghj7un9d3shjtnwdaehgu3wv4uxzmtsd3jjumtev3hk6ctfdchx27rpd4cxcefwvdhk6qgkwaehxw309ahx7um5wghxyctwv9hxztnrdaksqgyyvqa5uvqggqpke2xvsy47lnywysxqndeczf3e6hxa3m886m46gqlwp5er";

assert_eq!(encode(@profile), expected);
assert_eq!(profile.encode(), expected);
}

#[test]
Expand All @@ -79,7 +82,7 @@ mod tests {

let expected = "nprofile1qqs2sa3zk4a49umxg4lgvlsaenrqaf33ejkffd78f2cgy4xy38h393s2w22mm";

assert_eq!(encode(@profile), expected);
assert_eq!(profile.encode(), expected);
}

#[test]
Expand All @@ -92,7 +95,7 @@ mod tests {
let expected =
"nprofile1qyt8wumn8ghj7mn0wd68ytnzv9hxzmnp9e3k7mgqyz08pf5l3unu38v5phxaupze0xxdd8hjjxpsutl6gn4lmt28muy5cevgzmu";

assert_eq!(encode(@profile), expected);
assert_eq!(profile.encode(), expected);
}
}

Loading

0 comments on commit 3987968

Please sign in to comment.