-
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.
* draft of SocialPayRequest * fix fmt
- Loading branch information
Showing
5 changed files
with
129 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//! bech32 encoding implementation | ||
//! Spec: https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki | ||
//! Sample implementations: | ||
//! https://github.com/sipa/bech32/blob/master/ref/javascript/bech32.js#L86 | ||
//! https://github.com/paulmillr/scure-base/blob/main/index.ts#L479 | ||
|
||
pub fn encode(hrp: ByteArray, data: ByteArray) -> ByteArray { | ||
// TODO: provide full implementation of bech32 encoding | ||
if (@"NostrProfile", @"alice") == (@hrp, @data) { | ||
return "nprofile1alice"; | ||
} | ||
|
||
if (@"NostrProfile", @"bob") == (@hrp, @data) { | ||
return "nprofile1bob"; | ||
} | ||
|
||
panic!("bench32 encoding not implemented yet for: ({}, {})", hrp, data) | ||
} |
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 +1,4 @@ | ||
pub mod bech32; | ||
pub mod bip340; | ||
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,22 @@ | ||
//! Representation of Nostr profiles | ||
|
||
#[derive(Drop, Debug)] | ||
pub struct NostrProfile { | ||
pub public_key: u256, | ||
pub relays: Array<ByteArray> | ||
} | ||
|
||
/// 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(n_profile: @NostrProfile) -> ByteArray { | ||
// TODO: implement full bech32 profile encoding | ||
"nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p" | ||
} |
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,75 @@ | ||
use starknet::ContractAddress; | ||
use core::to_byte_array::{FormatAsByteArray, AppendFormattedToByteArray}; | ||
use core::fmt::{Display, Formatter, Error}; | ||
|
||
use joyboy::nostr_profile::{NostrProfile, encode}; | ||
|
||
type NostrKey = u256; | ||
|
||
#[derive(Copy, Drop, Debug)] | ||
enum Token { | ||
DAI, | ||
ETH, | ||
STRK, | ||
USDC, | ||
} | ||
|
||
#[derive(Drop)] | ||
struct SocialPayRequest { | ||
amount: u256, | ||
token: Token, | ||
joyboy: NostrProfile, | ||
recipient: NostrProfile | ||
} | ||
|
||
impl DisplayToken of Display<Token> { | ||
fn fmt(self: @Token, ref f: Formatter) -> Result<(), Error> { | ||
f | ||
.buffer | ||
.append( | ||
@match *self { | ||
Token::DAI => "DAI", | ||
Token::ETH => "ETH", | ||
Token::STRK => "STRK", | ||
Token::USDC => "USDC", | ||
} | ||
); | ||
Result::Ok(()) | ||
} | ||
} | ||
|
||
impl DisplaySocialPayRequest of Display<SocialPayRequest> { | ||
fn fmt(self: @SocialPayRequest, ref f: Formatter) -> Result<(), Error> { | ||
f | ||
.buffer | ||
.append( | ||
@format!( | ||
"{} send {} {} to {}", | ||
encode(self.joyboy), | ||
self.amount, | ||
self.token, | ||
encode(self.recipient) | ||
) | ||
); | ||
Result::Ok(()) | ||
} | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use core::option::OptionTrait; | ||
use super::{SocialPayRequest, Token}; | ||
use joyboy::nostr_profile::NostrProfile; | ||
|
||
#[test] | ||
fn test_to_byte_array() { | ||
let joyboy = NostrProfile { public_key: 123_u256, relays: array![] }; | ||
let recipient = NostrProfile { public_key: 345_u256, relays: array![] }; | ||
let r = SocialPayRequest { amount: 1, token: Token::USDC, joyboy, recipient }; | ||
assert_eq!( | ||
format!("{r}"), | ||
"nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p send 1 USDC to nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p" | ||
); | ||
} | ||
} |