-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96374a6
commit e4dc70a
Showing
3 changed files
with
109 additions
and
0 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
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,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(()) | ||
} |
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