-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathreceive.rs
217 lines (186 loc) · 6.35 KB
/
receive.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use std::collections::HashSet;
use std::str::FromStr;
use std::sync::Arc;
use anyhow::{anyhow, Result};
use cdk::cdk_database::{self, WalletDatabase};
use cdk::nuts::{SecretKey, Token};
use cdk::util::unix_time;
use cdk::wallet::multi_mint_wallet::MultiMintWallet;
use cdk::wallet::types::WalletKey;
use cdk::wallet::Wallet;
use cdk::Amount;
use clap::Args;
use nostr_sdk::nips::nip04;
use nostr_sdk::{Filter, Keys, Kind, Timestamp};
#[derive(Args)]
pub struct ReceiveSubCommand {
/// Cashu Token
token: Option<String>,
/// Signing Key
#[arg(short, long, action = clap::ArgAction::Append)]
signing_key: Vec<String>,
/// Nostr key
#[arg(short, long)]
nostr_key: Option<String>,
/// Nostr relay
#[arg(short, long, action = clap::ArgAction::Append)]
relay: Vec<String>,
/// Unix time to to query nostr from
#[arg(long)]
since: Option<u64>,
/// Preimage
#[arg(short, long, action = clap::ArgAction::Append)]
preimage: Vec<String>,
}
pub async fn receive(
multi_mint_wallet: &MultiMintWallet,
localstore: Arc<dyn WalletDatabase<Err = cdk_database::Error> + Send + Sync>,
seed: &[u8],
sub_command_args: &ReceiveSubCommand,
) -> Result<()> {
let mut signing_keys = Vec::new();
if !sub_command_args.signing_key.is_empty() {
let mut s_keys: Vec<SecretKey> = sub_command_args
.signing_key
.iter()
.map(|s| {
if s.starts_with("nsec") {
let nostr_key = nostr_sdk::SecretKey::from_str(s).expect("Invalid secret key");
SecretKey::from_str(&nostr_key.to_secret_hex())
} else {
SecretKey::from_str(s)
}
})
.collect::<Result<Vec<SecretKey>, _>>()?;
signing_keys.append(&mut s_keys);
}
let amount = match &sub_command_args.token {
Some(token_str) => {
receive_token(
multi_mint_wallet,
localstore,
seed,
token_str,
&signing_keys,
&sub_command_args.preimage,
)
.await?
}
None => {
//wallet.add_p2pk_signing_key(nostr_signing_key).await;
let nostr_key = match sub_command_args.nostr_key.as_ref() {
Some(nostr_key) => {
let secret_key = nostr_sdk::SecretKey::from_str(nostr_key)?;
let secret_key = SecretKey::from_str(&secret_key.to_secret_hex())?;
Some(secret_key)
}
None => None,
};
let nostr_key =
nostr_key.ok_or(anyhow!("Nostr key required if token is not provided"))?;
signing_keys.push(nostr_key.clone());
let relays = sub_command_args.relay.clone();
let since = localstore
.get_nostr_last_checked(&nostr_key.public_key())
.await?;
let tokens = nostr_receive(relays, nostr_key.clone(), since).await?;
let mut total_amount = Amount::ZERO;
for token_str in &tokens {
match receive_token(
multi_mint_wallet,
localstore.clone(),
seed,
token_str,
&signing_keys,
&sub_command_args.preimage,
)
.await
{
Ok(amount) => {
total_amount += amount;
}
Err(err) => {
println!("{}", err);
}
}
}
localstore
.add_nostr_last_checked(nostr_key.public_key(), unix_time() as u32)
.await?;
total_amount
}
};
println!("Received: {}", amount);
Ok(())
}
async fn receive_token(
multi_mint_wallet: &MultiMintWallet,
localstore: Arc<dyn WalletDatabase<Err = cdk_database::Error> + Send + Sync>,
seed: &[u8],
token_str: &str,
signing_keys: &[SecretKey],
preimage: &[String],
) -> Result<Amount> {
let token: Token = Token::from_str(token_str)?;
let mint_url = token.mint_url()?;
let wallet_key = WalletKey::new(mint_url.clone(), token.unit().unwrap_or_default());
if multi_mint_wallet.get_wallet(&wallet_key).await.is_none() {
let wallet = Wallet::new(
&mint_url.to_string(),
token.unit().unwrap_or_default(),
localstore,
seed,
None,
)?;
multi_mint_wallet.add_wallet(wallet).await;
}
let amount = multi_mint_wallet
.receive(token_str, signing_keys, preimage)
.await?;
Ok(amount)
}
/// Receive tokens sent to nostr pubkey via dm
async fn nostr_receive(
relays: Vec<String>,
nostr_signing_key: SecretKey,
since: Option<u32>,
) -> Result<HashSet<String>> {
let verifying_key = nostr_signing_key.public_key();
let x_only_pubkey = verifying_key.x_only_public_key();
let nostr_pubkey = nostr_sdk::PublicKey::from_hex(x_only_pubkey.to_string())?;
let since = since.map(|s| Timestamp::from(s as u64));
let filter = match since {
Some(since) => Filter::new()
.pubkey(nostr_pubkey)
.kind(Kind::EncryptedDirectMessage)
.since(since),
None => Filter::new()
.pubkey(nostr_pubkey)
.kind(Kind::EncryptedDirectMessage),
};
let client = nostr_sdk::Client::default();
client.connect().await;
let events = client
.get_events_of(
vec![filter],
nostr_sdk::EventSource::Relays {
timeout: None,
specific_relays: Some(relays),
},
)
.await?;
let mut tokens: HashSet<String> = HashSet::new();
let keys = Keys::from_str(&(nostr_signing_key).to_secret_hex())?;
for event in events {
if event.kind == Kind::EncryptedDirectMessage {
if let Ok(msg) = nip04::decrypt(keys.secret_key(), &event.pubkey, event.content) {
if let Some(token) = cdk::wallet::util::token_from_text(&msg) {
tokens.insert(token.to_string());
}
} else {
tracing::error!("Impossible to decrypt direct message");
}
}
}
Ok(tokens)
}