Skip to content

Commit

Permalink
Draft of SocialPayRequest (#24)
Browse files Browse the repository at this point in the history
* draft of SocialPayRequest

* fix fmt
  • Loading branch information
maciejka authored May 13, 2024
1 parent 0f98601 commit c70fa70
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 9 deletions.
18 changes: 18 additions & 0 deletions onchain/src/bech32.cairo
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)
}
20 changes: 11 additions & 9 deletions onchain/src/bip340.cairo
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
//! bip340 implementation
//! references:
//! Schnorr signatures explained:
//! https://www.youtube.com/watch?v=wjACBRJDfxc&ab_channel=Bitcoinology
//! NIP-01:
//! https://github.com/nostr-protocol/nips/blob/master/01.md
//! BIP-340:
//! https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
//! reference implementation:
//! https://github.com/bitcoin/bips/blob/master/bip-0340/reference.py

use core::byte_array::ByteArrayTrait;
use core::option::OptionTrait;
Expand All @@ -26,6 +17,17 @@ const p: u256 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC

/// Computes BIP0340/challenge tagged hash.
///
/// References:
/// Schnorr signatures explained:
/// https://www.youtube.com/watch?v=wjACBRJDfxc&ab_channel=Bitcoinology
/// NIP-01:
/// https://github.com/nostr-protocol/nips/blob/master/01.md
/// BIP-340:
/// https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
/// reference implementation:
/// https://github.com/bitcoin/bips/blob/master/bip-0340/reference.py
///
///
/// # Parameters:
/// - `rx`: `u256` - The x-coordinate of the R point from the signature.
/// - `px`: `u256` - The x-coordinate of the public key.
Expand Down
3 changes: 3 additions & 0 deletions onchain/src/lib.cairo
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;
22 changes: 22 additions & 0 deletions onchain/src/nostr_profile.cairo
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"
}
75 changes: 75 additions & 0 deletions onchain/src/social_pay_request.cairo
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"
);
}
}

0 comments on commit c70fa70

Please sign in to comment.