-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathv2.rs
More file actions
369 lines (303 loc) · 12.6 KB
/
v2.rs
File metadata and controls
369 lines (303 loc) · 12.6 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use std::sync::Arc;
use payjoin::persist::SessionPersister;
use payjoin::receive::v2::SessionEvent as ReceiverSessionEvent;
use payjoin::send::v2::SessionEvent as SenderSessionEvent;
use payjoin::HpkePublicKey;
use rusqlite::params;
use super::*;
#[derive(Debug, Clone)]
pub(crate) struct SessionId(i64);
impl core::ops::Deref for SessionId {
type Target = i64;
fn deref(&self) -> &Self::Target { &self.0 }
}
impl std::fmt::Display for SessionId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) }
}
#[derive(Clone, Debug)]
pub(crate) struct SenderPersister {
db: Arc<Database>,
session_id: SessionId,
}
impl SenderPersister {
pub fn new(
db: Arc<Database>,
pj_uri: &str,
receiver_pubkey: &HpkePublicKey,
) -> crate::db::Result<Self> {
let conn = db.get_connection()?;
let receiver_pubkey_bytes = receiver_pubkey.to_compressed_bytes();
let (duplicate_uri, duplicate_rk): (bool, bool) = conn.query_row(
"SELECT \
EXISTS(SELECT 1 FROM send_sessions WHERE pj_uri = ?1), \
EXISTS(SELECT 1 FROM send_sessions WHERE receiver_pubkey = ?2)",
params![pj_uri, &receiver_pubkey_bytes],
|row| Ok((row.get(0)?, row.get(1)?)),
)?;
if duplicate_uri {
return Err(Error::DuplicateSendSession(DuplicateKind::Uri));
}
if duplicate_rk {
return Err(Error::DuplicateSendSession(DuplicateKind::ReceiverPubkey));
}
// Create a new session in send_sessions and get its ID
let session_id: i64 = conn.query_row(
"INSERT INTO send_sessions (pj_uri, receiver_pubkey) VALUES (?1, ?2) RETURNING session_id",
params![pj_uri, &receiver_pubkey_bytes],
|row| row.get(0),
)?;
Ok(Self { db, session_id: SessionId(session_id) })
}
pub fn from_id(db: Arc<Database>, id: SessionId) -> Self { Self { db, session_id: id } }
}
impl SessionPersister for SenderPersister {
type SessionEvent = SenderSessionEvent;
type InternalStorageError = crate::db::error::Error;
fn save_event(
&self,
event: SenderSessionEvent,
) -> std::result::Result<(), Self::InternalStorageError> {
let conn = self.db.get_connection()?;
let event_data = serde_json::to_string(&event).map_err(Error::Serialize)?;
conn.execute(
"INSERT INTO send_session_events (session_id, event_data, created_at) VALUES (?1, ?2, ?3)",
params![*self.session_id, event_data, now()],
)?;
Ok(())
}
fn load(
&self,
) -> std::result::Result<Box<dyn Iterator<Item = SenderSessionEvent>>, Self::InternalStorageError>
{
let conn = self.db.get_connection()?;
let mut stmt = conn.prepare(
"SELECT event_data FROM send_session_events WHERE session_id = ?1 ORDER BY id ASC",
)?;
let event_rows = stmt.query_map(params![*self.session_id], |row| {
let event_data: String = row.get(0)?;
Ok(event_data)
})?;
let events: Vec<SenderSessionEvent> = event_rows
.map(|row| {
let event_data = row.expect("Failed to read event data from database");
serde_json::from_str::<SenderSessionEvent>(&event_data)
.expect("Database corruption: failed to deserialize session event")
})
.collect();
Ok(Box::new(events.into_iter()))
}
fn close(&self) -> std::result::Result<(), Self::InternalStorageError> {
let conn = self.db.get_connection()?;
conn.execute(
"UPDATE send_sessions SET completed_at = ?1 WHERE session_id = ?2",
params![now(), *self.session_id],
)?;
Ok(())
}
}
#[derive(Clone)]
pub(crate) struct ReceiverPersister {
db: Arc<Database>,
session_id: SessionId,
}
impl ReceiverPersister {
pub fn new(db: Arc<Database>) -> crate::db::Result<Self> {
let conn = db.get_connection()?;
// Create a new session in receive_sessions and get its ID
let session_id: i64 = conn.query_row(
"INSERT INTO receive_sessions (session_id) VALUES (NULL) RETURNING session_id",
[],
|row| row.get(0),
)?;
Ok(Self { db, session_id: SessionId(session_id) })
}
pub fn from_id(db: Arc<Database>, id: SessionId) -> Self { Self { db, session_id: id } }
}
impl SessionPersister for ReceiverPersister {
type SessionEvent = ReceiverSessionEvent;
type InternalStorageError = crate::db::error::Error;
fn save_event(
&self,
event: ReceiverSessionEvent,
) -> std::result::Result<(), Self::InternalStorageError> {
let conn = self.db.get_connection()?;
let event_data = serde_json::to_string(&event).map_err(Error::Serialize)?;
conn.execute(
"INSERT INTO receive_session_events (session_id, event_data, created_at) VALUES (?1, ?2, ?3)",
params![*self.session_id, event_data, now()],
)?;
Ok(())
}
fn load(
&self,
) -> std::result::Result<
Box<dyn Iterator<Item = ReceiverSessionEvent>>,
Self::InternalStorageError,
> {
let conn = self.db.get_connection()?;
let mut stmt = conn.prepare(
"SELECT event_data FROM receive_session_events WHERE session_id = ?1 ORDER BY id ASC",
)?;
let event_rows = stmt.query_map(params![*self.session_id], |row| {
let event_data: String = row.get(0)?;
Ok(event_data)
})?;
let events: Vec<ReceiverSessionEvent> = event_rows
.map(|row| {
let event_data = row.expect("Failed to read event data from database");
serde_json::from_str::<ReceiverSessionEvent>(&event_data)
.expect("Database corruption: failed to deserialize session event")
})
.collect();
Ok(Box::new(events.into_iter()))
}
fn close(&self) -> std::result::Result<(), Self::InternalStorageError> {
let conn = self.db.get_connection()?;
conn.execute(
"UPDATE receive_sessions SET completed_at = ?1 WHERE session_id = ?2",
params![now(), *self.session_id],
)?;
Ok(())
}
}
impl Database {
pub(crate) fn get_recv_session_ids(&self) -> Result<Vec<SessionId>> {
let conn = self.get_connection()?;
let mut stmt =
conn.prepare("SELECT session_id FROM receive_sessions WHERE completed_at IS NULL")?;
let session_rows = stmt.query_map([], |row| {
let session_id: i64 = row.get(0)?;
Ok(SessionId(session_id))
})?;
let mut session_ids = Vec::new();
for session_row in session_rows {
let session_id = session_row?;
session_ids.push(session_id);
}
Ok(session_ids)
}
pub(crate) fn get_send_session_ids(&self) -> Result<Vec<SessionId>> {
let conn = self.get_connection()?;
let mut stmt =
conn.prepare("SELECT session_id FROM send_sessions WHERE completed_at IS NULL")?;
let session_rows = stmt.query_map([], |row| {
let session_id: i64 = row.get(0)?;
Ok(SessionId(session_id))
})?;
let mut session_ids = Vec::new();
for session_row in session_rows {
let session_id = session_row?;
session_ids.push(session_id);
}
Ok(session_ids)
}
pub(crate) fn get_send_session_receiver_pk(
&self,
session_id: &SessionId,
) -> Result<HpkePublicKey> {
let conn = self.get_connection()?;
let mut stmt =
conn.prepare("SELECT receiver_pubkey FROM send_sessions WHERE session_id = ?1")?;
let receiver_pubkey: Vec<u8> = stmt.query_row(params![session_id.0], |row| row.get(0))?;
Ok(HpkePublicKey::from_compressed_bytes(&receiver_pubkey).expect("Valid receiver pubkey"))
}
pub(crate) fn get_inactive_send_session_ids(&self) -> Result<Vec<(SessionId, u64)>> {
let conn = self.get_connection()?;
let mut stmt = conn.prepare(
"SELECT session_id, completed_at FROM send_sessions WHERE completed_at IS NOT NULL",
)?;
let session_rows = stmt.query_map([], |row| {
let session_id: i64 = row.get(0)?;
let completed_at: u64 = row.get(1)?;
Ok((SessionId(session_id), completed_at))
})?;
let mut session_ids = Vec::new();
for session_row in session_rows {
let (session_id, completed_at) = session_row?;
session_ids.push((session_id, completed_at));
}
Ok(session_ids)
}
pub(crate) fn get_inactive_recv_session_ids(&self) -> Result<Vec<(SessionId, u64)>> {
let conn = self.get_connection()?;
let mut stmt = conn.prepare(
"SELECT session_id, completed_at FROM receive_sessions WHERE completed_at IS NOT NULL",
)?;
let session_rows = stmt.query_map([], |row| {
let session_id: i64 = row.get(0)?;
let completed_at: u64 = row.get(1)?;
Ok((SessionId(session_id), completed_at))
})?;
let mut session_ids = Vec::new();
for session_row in session_rows {
let (session_id, completed_at) = session_row?;
session_ids.push((session_id, completed_at));
}
Ok(session_ids)
}
}
#[cfg(all(test, feature = "v2"))]
mod tests {
use std::sync::Arc;
use payjoin::HpkeKeyPair;
use super::*;
fn create_test_db() -> Arc<Database> {
// Use an in-memory database for tests
let manager = r2d2_sqlite::SqliteConnectionManager::memory()
.with_init(|conn| conn.execute_batch("PRAGMA locking_mode = EXCLUSIVE;"));
let pool = r2d2::Pool::new(manager).expect("pool creation should succeed");
let conn = pool.get().expect("connection should succeed");
Database::init_schema(&conn).expect("schema init should succeed");
Arc::new(Database(pool))
}
fn make_receiver_pubkey() -> payjoin::HpkePublicKey { HpkeKeyPair::gen_keypair().1 }
// Second call with the same URI (same active session) should return DuplicateSendSession(Uri).
#[test]
fn test_duplicate_uri_returns_error() {
let db = create_test_db();
let rk1 = make_receiver_pubkey();
let rk2 = make_receiver_pubkey();
let uri = "bitcoin:addr1?pj=https://example.com/BBBBBBBB";
SenderPersister::new(db.clone(), uri, &rk1).expect("first session should succeed");
let err = SenderPersister::new(db, uri, &rk2).expect_err("duplicate URI should fail");
assert!(
matches!(err, Error::DuplicateSendSession(DuplicateKind::Uri)),
"expected DuplicateSendSession(Uri), got: {err:?}"
);
}
// Same receiver pubkey under a different URI should return DuplicateSendSession(ReceiverPubkey).
#[test]
fn test_duplicate_rk_returns_error() {
let db = create_test_db();
let rk = make_receiver_pubkey();
let uri1 = "bitcoin:addr1?pj=https://example.com/CCCCCCCC";
let uri2 = "bitcoin:addr1?pj=https://example.com/DDDDDDDD";
SenderPersister::new(db.clone(), uri1, &rk).expect("first session should succeed");
let err = SenderPersister::new(db, uri2, &rk).expect_err("duplicate RK should fail");
assert!(
matches!(err, Error::DuplicateSendSession(DuplicateKind::ReceiverPubkey)),
"expected DuplicateSendSession(ReceiverPubkey), got: {err:?}"
);
}
// After a session is marked completed, a new session with the same URI must still be rejected
// to prevent address reuse, HPKE receiver-key reuse
#[test]
fn test_completed_session_blocks_reuse() {
let db = create_test_db();
let rk1 = make_receiver_pubkey();
let rk2 = make_receiver_pubkey();
let uri = "bitcoin:addr1?pj=https://example.com/EEEEEEEE";
let persister =
SenderPersister::new(db.clone(), uri, &rk1).expect("first session should succeed");
// Mark the session as completed
use payjoin::persist::SessionPersister;
persister.close().expect("close should succeed");
// A new session with the same URI must be rejected even after completion
let err = SenderPersister::new(db, uri, &rk2)
.expect_err("reuse of a completed session URI must be rejected");
assert!(
matches!(err, Error::DuplicateSendSession(DuplicateKind::Uri)),
"expected DuplicateSendSession(Uri), got: {err:?}"
);
}
}