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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2025 Rust Nostr Developers
// Distributed under the MIT software license
//! Nostr Connect signer
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use nostr::nips::nip46::{Message, Request, ResponseResult};
use nostr_relay_pool::prelude::*;
use crate::error::Error;
use crate::util;
/// Nostr Connect Keys
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NostrConnectKeys {
/// The keys used for communication with the client.
///
/// This may be the same as the `user` one.
pub signer: Keys,
/// The keys used to sign events and so on.
pub user: Keys,
}
/// Nostr Connect Signer
///
/// Signer that listen for requests from a client, handle them and send the response.
///
/// <https://github.com/nostr-protocol/nips/blob/master/46.md>
#[derive(Debug, Clone)]
pub struct NostrConnectRemoteSigner {
keys: NostrConnectKeys,
relays: Vec<RelayUrl>,
pool: RelayPool,
opts: RelayOptions,
secret: Option<String>,
nostr_connect_client_public_key: Option<PublicKey>,
bootstrapped: Arc<AtomicBool>,
}
impl NostrConnectRemoteSigner {
/// Construct new remote signer
pub fn new<I, U>(
keys: NostrConnectKeys,
urls: I,
secret: Option<String>,
opts: Option<RelayOptions>,
) -> Result<Self, Error>
where
I: IntoIterator<Item = U>,
U: TryIntoUrl,
pool::Error: From<<U as TryIntoUrl>::Err>,
{
let mut relays = Vec::new();
for relay in urls.into_iter() {
relays.push(
relay
.try_into_url()
.map_err(|e| Error::Pool(pool::Error::from(e)))?,
);
}
Ok(Self {
keys,
relays,
pool: RelayPool::default(),
opts: opts.unwrap_or_default(),
secret,
nostr_connect_client_public_key: None,
bootstrapped: Arc::new(AtomicBool::new(false)),
})
}
/// Construct remote signer from client URI (`nostrconnect://..`)
pub fn from_uri(
uri: NostrConnectURI,
keys: NostrConnectKeys,
secret: Option<String>,
opts: Option<RelayOptions>,
) -> Result<Self, Error> {
match uri {
NostrConnectURI::Client {
public_key, relays, ..
} => {
let mut signer = Self::new(keys, relays, secret, opts)?;
signer.nostr_connect_client_public_key = Some(public_key);
Ok(signer)
}
NostrConnectURI::Bunker { .. } => Err(Error::UnexpectedUri),
}
}
/// Get signer relays
pub fn relays(&self) -> &[RelayUrl] {
&self.relays
}
/// Get `bunker` URI
pub fn bunker_uri(&self) -> NostrConnectURI {
NostrConnectURI::Bunker {
remote_signer_public_key: self.keys.signer.public_key(),
relays: self.relays().to_vec(),
secret: self.secret.clone(),
}
}
async fn send_connect_ack(&self, public_key: PublicKey) -> Result<(), Error> {
let msg = Message::request(Request::Connect {
public_key: self.keys.user.public_key(),
secret: self.secret.clone(),
});
let event = EventBuilder::nostr_connect(&self.keys.signer, public_key, msg)?
.sign_with_keys(&self.keys.signer)?;
self.pool.send_event(&event).await?;
Ok(())
}
async fn bootstrap(&self) -> Result<(), Error> {
// Check if already bootstrapped
if self.bootstrapped.load(Ordering::SeqCst) {
return Ok(());
}
// Add relays to pool
for url in self.relays.iter() {
self.pool.add_relay(url, self.opts.clone()).await?;
}
// Connect
self.pool.connect().await;
let filter = Filter::new()
.pubkey(self.keys.signer.public_key())
.kind(Kind::NostrConnect)
.since(Timestamp::now());
// Subscribe
self.pool
.subscribe(filter, SubscribeOptions::default())
.await?;
// Mark as bootstrapped
self.bootstrapped.store(true, Ordering::SeqCst);
Ok(())
}
fn match_secret(&self, secret: Option<String>) -> bool {
match (&self.secret, secret) {
// Both secrets are set, check if values are equal.
(Some(s1), Some(s2)) => s1 == &s2,
// Only the secret on our side is set, must return `false`.
(Some(..), None) => false,
// Only the secret on their side is set, can continue, return `true`.
(None, Some(..)) => true,
// No secret is set
(None, None) => true,
}
}
/// Serve signer
pub async fn serve<T>(&self, actions: T) -> Result<(), Error>
where
T: NostrConnectSignerActions,
{
self.bootstrap().await?;
// TODO: move into bootstrap method?
if let Some(public_key) = self.nostr_connect_client_public_key {
self.send_connect_ack(public_key).await?;
}
self.pool
.handle_notifications(|notification| async {
if let RelayPoolNotification::Event { event, .. } = notification {
if event.kind == Kind::NostrConnect {
match util::decrypt(self.keys.signer.secret_key(), &event) {
Ok(msg) => {
tracing::debug!("New Nostr Connect message received: {msg}");
let msg: Message = Message::from_json(msg)?;
if let Message::Request { id, req } = msg {
// Generate response
let (result, error) = if actions.approve(&event.pubkey, &req) {
match req {
Request::Connect { secret, .. } => {
if self.match_secret(secret) {
(Some(ResponseResult::Connect), None)
} else {
(None, Some(String::from("Secret not match")))
}
}
Request::GetPublicKey => (
Some(ResponseResult::GetPublicKey(
self.keys.user.public_key(),
)),
None,
),
Request::GetRelays => {
(None, Some(String::from("Not supported yet")))
}
Request::Nip04Encrypt { public_key, text } => {
match nip04::encrypt(
self.keys.user.secret_key(),
&public_key,
text,
) {
Ok(ciphertext) => (
Some(ResponseResult::EncryptionDecryption(
ciphertext,
)),
None,
),
Err(e) => (None, Some(e.to_string())),
}
}
Request::Nip04Decrypt {
public_key,
ciphertext,
} => {
match nip04::decrypt(
self.keys.user.secret_key(),
&public_key,
ciphertext,
) {
Ok(ciphertext) => (
Some(ResponseResult::EncryptionDecryption(
ciphertext,
)),
None,
),
Err(e) => (None, Some(e.to_string())),
}
}
Request::Nip44Encrypt { public_key, text } => {
match nip44::encrypt(
self.keys.user.secret_key(),
&public_key,
text,
nip44::Version::default(),
) {
Ok(ciphertext) => (
Some(ResponseResult::EncryptionDecryption(
ciphertext,
)),
None,
),
Err(e) => (None, Some(e.to_string())),
}
}
Request::Nip44Decrypt {
public_key,
ciphertext,
} => {
match nip44::decrypt(
self.keys.user.secret_key(),
&public_key,
ciphertext,
) {
Ok(ciphertext) => (
Some(ResponseResult::EncryptionDecryption(
ciphertext,
)),
None,
),
Err(e) => (None, Some(e.to_string())),
}
}
Request::SignEvent(unsigned) => {
match unsigned.sign_with_keys(&self.keys.user) {
Ok(event) => (
Some(ResponseResult::SignEvent(Box::new(
event,
))),
None,
),
Err(e) => (None, Some(e.to_string())),
}
}
Request::Ping => (Some(ResponseResult::Pong), None),
}
} else {
(None, Some(String::from("Rejected")))
};
// Compose message
let msg: Message = Message::response(id, result, error);
// Compose and publish event
let event = EventBuilder::nostr_connect(
&self.keys.signer,
event.pubkey,
msg,
)?
.sign_with_keys(&self.keys.signer)?;
self.pool.send_event(&event).await?;
}
}
Err(e) => {
tracing::error!(error = %e, "Impossible to decrypt message.")
}
}
}
}
Ok(false) // Set to true to exit from the loop
})
.await?;
Ok(())
}
}
/// Nostr Connect signer actions
pub trait NostrConnectSignerActions {
/// Approve
fn approve(&self, public_key: &PublicKey, req: &Request) -> bool;
}