receive.rs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. use std::collections::HashSet;
  2. use std::path::Path;
  3. use std::str::FromStr;
  4. use std::time::Duration;
  5. use anyhow::{anyhow, Result};
  6. use cdk::nuts::{SecretKey, Token};
  7. use cdk::util::unix_time;
  8. use cdk::wallet::multi_mint_wallet::MultiMintWallet;
  9. use cdk::wallet::types::WalletKey;
  10. use cdk::wallet::ReceiveOptions;
  11. use cdk::Amount;
  12. use clap::Args;
  13. use nostr_sdk::nips::nip04;
  14. use nostr_sdk::{Filter, Keys, Kind, Timestamp};
  15. use crate::nostr_storage;
  16. use crate::utils::get_or_create_wallet;
  17. #[derive(Args)]
  18. pub struct ReceiveSubCommand {
  19. /// Cashu Token
  20. token: Option<String>,
  21. /// Signing Key
  22. #[arg(short, long, action = clap::ArgAction::Append)]
  23. signing_key: Vec<String>,
  24. /// Nostr key
  25. #[arg(short, long)]
  26. nostr_key: Option<String>,
  27. /// Nostr relay
  28. #[arg(short, long, action = clap::ArgAction::Append)]
  29. relay: Vec<String>,
  30. /// Unix time to query nostr from
  31. #[arg(long)]
  32. since: Option<u64>,
  33. /// Preimage
  34. #[arg(short, long, action = clap::ArgAction::Append)]
  35. preimage: Vec<String>,
  36. }
  37. pub async fn receive(
  38. multi_mint_wallet: &MultiMintWallet,
  39. sub_command_args: &ReceiveSubCommand,
  40. work_dir: &Path,
  41. ) -> Result<()> {
  42. let mut signing_keys = Vec::new();
  43. if !sub_command_args.signing_key.is_empty() {
  44. let mut s_keys: Vec<SecretKey> = sub_command_args
  45. .signing_key
  46. .iter()
  47. .map(|s| {
  48. if s.starts_with("nsec") {
  49. let nostr_key = nostr_sdk::SecretKey::from_str(s).expect("Invalid secret key");
  50. SecretKey::from_str(&nostr_key.to_secret_hex())
  51. } else {
  52. SecretKey::from_str(s)
  53. }
  54. })
  55. .collect::<Result<Vec<SecretKey>, _>>()?;
  56. signing_keys.append(&mut s_keys);
  57. }
  58. let amount = match &sub_command_args.token {
  59. Some(token_str) => {
  60. receive_token(
  61. multi_mint_wallet,
  62. token_str,
  63. &signing_keys,
  64. &sub_command_args.preimage,
  65. )
  66. .await?
  67. }
  68. None => {
  69. //wallet.add_p2pk_signing_key(nostr_signing_key).await;
  70. let nostr_key = match sub_command_args.nostr_key.as_ref() {
  71. Some(nostr_key) => {
  72. let secret_key = nostr_sdk::SecretKey::from_str(nostr_key)?;
  73. let secret_key = SecretKey::from_str(&secret_key.to_secret_hex())?;
  74. Some(secret_key)
  75. }
  76. None => None,
  77. };
  78. let nostr_key =
  79. nostr_key.ok_or(anyhow!("Nostr key required if token is not provided"))?;
  80. signing_keys.push(nostr_key.clone());
  81. let relays = sub_command_args.relay.clone();
  82. let since =
  83. nostr_storage::get_nostr_last_checked(work_dir, &nostr_key.public_key()).await?;
  84. let tokens = nostr_receive(relays, nostr_key.clone(), since).await?;
  85. // Store the current time as last checked
  86. nostr_storage::store_nostr_last_checked(
  87. work_dir,
  88. &nostr_key.public_key(),
  89. unix_time() as u32,
  90. )
  91. .await?;
  92. let mut total_amount = Amount::ZERO;
  93. for token_str in &tokens {
  94. match receive_token(
  95. multi_mint_wallet,
  96. token_str,
  97. &signing_keys,
  98. &sub_command_args.preimage,
  99. )
  100. .await
  101. {
  102. Ok(amount) => {
  103. total_amount += amount;
  104. }
  105. Err(err) => {
  106. println!("{err}");
  107. }
  108. }
  109. }
  110. total_amount
  111. }
  112. };
  113. println!("Received: {amount}");
  114. Ok(())
  115. }
  116. async fn receive_token(
  117. multi_mint_wallet: &MultiMintWallet,
  118. token_str: &str,
  119. signing_keys: &[SecretKey],
  120. preimage: &[String],
  121. ) -> Result<Amount> {
  122. let token: Token = Token::from_str(token_str)?;
  123. let mint_url = token.mint_url()?;
  124. let unit = token.unit().unwrap_or_default();
  125. if multi_mint_wallet
  126. .get_wallet(&WalletKey::new(mint_url.clone(), unit.clone()))
  127. .await
  128. .is_none()
  129. {
  130. get_or_create_wallet(multi_mint_wallet, &mint_url, unit).await?;
  131. }
  132. let amount = multi_mint_wallet
  133. .receive(
  134. token_str,
  135. ReceiveOptions {
  136. p2pk_signing_keys: signing_keys.to_vec(),
  137. preimages: preimage.to_vec(),
  138. ..Default::default()
  139. },
  140. )
  141. .await?;
  142. Ok(amount)
  143. }
  144. /// Receive tokens sent to nostr pubkey via dm
  145. async fn nostr_receive(
  146. relays: Vec<String>,
  147. nostr_signing_key: SecretKey,
  148. since: Option<u32>,
  149. ) -> Result<HashSet<String>> {
  150. let verifying_key = nostr_signing_key.public_key();
  151. let x_only_pubkey = verifying_key.x_only_public_key();
  152. let nostr_pubkey = nostr_sdk::PublicKey::from_hex(&x_only_pubkey.to_string())?;
  153. let since = since.map(|s| Timestamp::from(s as u64));
  154. let filter = match since {
  155. Some(since) => Filter::new()
  156. .pubkey(nostr_pubkey)
  157. .kind(Kind::EncryptedDirectMessage)
  158. .since(since),
  159. None => Filter::new()
  160. .pubkey(nostr_pubkey)
  161. .kind(Kind::EncryptedDirectMessage),
  162. };
  163. let client = nostr_sdk::Client::default();
  164. client.connect().await;
  165. let events = client
  166. .fetch_events_from(relays, filter, Duration::from_secs(30))
  167. .await?;
  168. let mut tokens: HashSet<String> = HashSet::new();
  169. let keys = Keys::from_str(&(nostr_signing_key).to_secret_hex())?;
  170. for event in events {
  171. if event.kind == Kind::EncryptedDirectMessage {
  172. if let Ok(msg) = nip04::decrypt(keys.secret_key(), &event.pubkey, event.content) {
  173. if let Some(token) = cdk::wallet::util::token_from_text(&msg) {
  174. tokens.insert(token.to_string());
  175. }
  176. } else {
  177. tracing::error!("Impossible to decrypt direct message");
  178. }
  179. }
  180. }
  181. Ok(tokens)
  182. }