Skip to content

Commit

Permalink
feat: create payment request
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Oct 24, 2024
1 parent 96374a6 commit e4dc70a
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
5 changes: 5 additions & 0 deletions crates/cdk-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ enum Commands {
DecodeRequest(sub_commands::decode_request::DecodePaymentRequestSubCommand),
/// Pay a payment request
PayRequest(sub_commands::pay_request::PayRequestSubCommand),
/// Create Payment request
CreateRequest(sub_commands::create_request::CreateRequestSubCommand),
}

#[tokio::main]
Expand Down Expand Up @@ -214,5 +216,8 @@ async fn main() -> Result<()> {
Commands::PayRequest(sub_command_args) => {
sub_commands::pay_request::pay_request(&multi_mint_wallet, sub_command_args).await
}
Commands::CreateRequest(sub_command_args) => {
sub_commands::create_request::create_request(&multi_mint_wallet, sub_command_args).await
}
}
}
103 changes: 103 additions & 0 deletions crates/cdk-cli/src/sub_commands/create_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use anyhow::Result;
use cdk::{
nuts::{
nut18::TransportType, CurrencyUnit, PaymentRequest, PaymentRequestPayload, Token, Transport,
},
wallet::MultiMintWallet,
};
use clap::Args;
use nostr_sdk::prelude::*;
use nostr_sdk::{nips::nip19::Nip19Profile, Client as NostrClient, Filter, Keys, ToBech32};

#[derive(Args)]
pub struct CreateRequestSubCommand {
amount: Option<u64>,
/// Currency unit e.g. sat
#[arg(default_value = "sat")]
unit: String,
/// Quote description
description: Option<String>,
}

pub async fn create_request(
multi_mint_wallet: &MultiMintWallet,
sub_command_args: &CreateRequestSubCommand,
) -> Result<()> {
let keys = Keys::generate();
let relays = vec!["wss://relay.nos.social", "wss://relay.damus.io"];

let nprofile = Nip19Profile::new(keys.public_key, relays.clone())?;

let nostr_transport = Transport {
_type: TransportType::Nostr,
target: nprofile.to_bech32()?,
tags: Some(vec![vec!["n".to_string(), "17".to_string()]]),
};

let mints: Vec<cdk::mint_url::MintUrl> = multi_mint_wallet
.get_balances(&CurrencyUnit::Sat)
.await?
.keys()
.cloned()
.collect();

let req = PaymentRequest {
payment_id: None,
amount: sub_command_args.amount.map(|a| a.into()),
unit: None,
single_use: Some(true),
mints: Some(mints),
description: sub_command_args.description.clone(),
transports: vec![nostr_transport],
};

println!("{}", req.to_string());

let client = NostrClient::new(keys);

let filter = Filter::new().pubkey(nprofile.public_key);

for relay in relays {
client.add_read_relay(relay).await?;
}

client.connect().await;

client.subscribe(vec![filter], None).await?;

// Handle subscription notifications with `handle_notifications` method
client
.handle_notifications(|notification| async {
let mut exit = false;
if let RelayPoolNotification::Event {
subscription_id: _,
event,
..
} = notification
{
let unwrapped = client.unwrap_gift_wrap(&event).await?;

let rumor = unwrapped.rumor;

let payload: PaymentRequestPayload = serde_json::from_str(&rumor.content)?;

let token = Token::new(
payload.mint,
payload.proofs,
payload.memo,
Some(payload.unit),
);

let amount = multi_mint_wallet
.receive(&token.to_string(), &[], &[])
.await?;

println!("Received {}", amount);
exit = true;
}
Ok(exit) // Set to true to exit from the loop
})
.await?;

Ok(())
}
1 change: 1 addition & 0 deletions crates/cdk-cli/src/sub_commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod balance;
pub mod burn;
pub mod check_spent;
pub mod create_request;
pub mod decode_request;
pub mod decode_token;
pub mod list_mint_proofs;
Expand Down

0 comments on commit e4dc70a

Please sign in to comment.