forked from payjoin/rust-payjoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
393 lines (346 loc) · 14 KB
/
mod.rs
File metadata and controls
393 lines (346 loc) · 14 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
use std::str::FromStr;
use std::sync::{Arc, RwLock};
use bitcoin_ffi::Psbt;
pub use error::{BuildSenderError, CreateRequestError, EncapsulationError, ResponseError};
use payjoin::persist::SessionPersister;
pub use crate::error::{ImplementationError, SerdeJsonError};
use crate::ohttp::ClientResponse;
use crate::request::Request;
use crate::send::error::{SenderPersistedError, SenderReplayError};
use crate::uri::PjUri;
pub mod error;
#[cfg(feature = "uniffi")]
pub mod uni;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SessionEvent(payjoin::send::v2::SessionEvent);
impl From<SessionEvent> for payjoin::send::v2::SessionEvent {
fn from(value: SessionEvent) -> Self { value.0 }
}
impl From<payjoin::send::v2::SessionEvent> for SessionEvent {
fn from(value: payjoin::send::v2::SessionEvent) -> Self { SessionEvent(value) }
}
#[derive(Debug, Clone)]
pub struct SendSession(pub payjoin::send::v2::SendSession);
impl From<payjoin::send::v2::SendSession> for SendSession {
fn from(value: payjoin::send::v2::SendSession) -> Self { Self(value) }
}
pub fn replay_event_log<P>(
persister: &P,
) -> Result<(SendSession, SessionHistory), SenderReplayError>
where
P: SessionPersister + Clone,
P::SessionEvent: Into<payjoin::send::v2::SessionEvent> + Clone,
{
let (state, history) =
payjoin::send::v2::replay_event_log(persister).map_err(SenderReplayError::from)?;
Ok((state.into(), history.into()))
}
#[derive(Default, Clone)]
pub struct SessionHistory(pub payjoin::send::v2::SessionHistory);
impl From<payjoin::send::v2::SessionHistory> for SessionHistory {
fn from(value: payjoin::send::v2::SessionHistory) -> Self { Self(value) }
}
#[allow(clippy::type_complexity)]
pub struct InitInputsTransition(
Arc<
RwLock<
Option<
payjoin::persist::MaybeBadInitInputsTransition<
payjoin::send::v2::SessionEvent,
payjoin::send::v2::Sender<payjoin::send::v2::WithReplyKey>,
payjoin::send::BuildSenderError,
>,
>,
>,
>,
);
impl InitInputsTransition {
pub fn save<P>(&self, persister: &P) -> Result<WithReplyKey, SenderPersistedError>
where
P: SessionPersister<SessionEvent = payjoin::send::v2::SessionEvent>,
{
let mut inner = self.0.write().map_err(|_| {
SenderPersistedError::Storage(Arc::new(ImplementationError::from(
"Lock poisoned".to_string(),
)))
})?;
let value = inner.take().ok_or_else(|| {
SenderPersistedError::Storage(Arc::new(ImplementationError::from(
"Already saved or moved".to_string(),
)))
})?;
let res = value.save(persister).map_err(SenderPersistedError::from)?;
Ok(res.into())
}
}
///Builder for sender-side payjoin parameters
///
///These parameters define how client wants to handle Payjoin.
#[derive(Clone)]
pub struct SenderBuilder(payjoin::send::v2::SenderBuilder);
impl From<payjoin::send::v2::SenderBuilder> for SenderBuilder {
fn from(value: payjoin::send::v2::SenderBuilder) -> Self { Self(value) }
}
impl SenderBuilder {
/// Prepare an HTTP request and request context to process the response
///
/// Call [`SenderBuilder::build_recommended()`] or other `build` methods
/// to create a [`WithReplyKey`]
pub fn new(psbt: String, uri: PjUri) -> Result<Self, BuildSenderError> {
let psbt = payjoin::bitcoin::psbt::Psbt::from_str(psbt.as_str())?;
Ok(payjoin::send::v2::SenderBuilder::new(psbt, uri.into()).into())
}
/// Disable output substitution even if the receiver didn't.
///
/// This forbids receiver switching output or decreasing amount.
/// It is generally **not** recommended to set this as it may prevent the receiver from
/// doing advanced operations such as opening LN channels and it also guarantees the
/// receiver will **not** reward the sender with a discount.
pub fn always_disable_output_substitution(&self) -> Self {
self.0.clone().always_disable_output_substitution().into()
}
// Calculate the recommended fee contribution for an Original PSBT.
//
// BIP 78 recommends contributing `originalPSBTFeeRate * vsize(sender_input_type)`.
// The minfeerate parameter is set if the contribution is available in change.
//
// This method fails if no recommendation can be made or if the PSBT is malformed.
pub fn build_recommended(&self, min_fee_rate: u64) -> InitInputsTransition {
InitInputsTransition(Arc::new(RwLock::new(Some(
self.0
.clone()
.build_recommended(payjoin::bitcoin::FeeRate::from_sat_per_kwu(min_fee_rate)),
))))
}
/// Offer the receiver contribution to pay for his input.
///
/// These parameters will allow the receiver to take `max_fee_contribution` from given change
/// output to pay for additional inputs. The recommended fee is `size_of_one_input * fee_rate`.
///
/// `change_index` specifies which output can be used to pay fee. If `None` is provided, then
/// the output is auto-detected unless the supplied transaction has more than two outputs.
///
/// `clamp_fee_contribution` decreases fee contribution instead of erroring.
///
/// If this option is true and a transaction with change amount lower than fee
/// contribution is provided then instead of returning error the fee contribution will
/// be just lowered in the request to match the change amount.
pub fn build_with_additional_fee(
&self,
max_fee_contribution: u64,
change_index: Option<u8>,
min_fee_rate: u64,
clamp_fee_contribution: bool,
) -> InitInputsTransition {
InitInputsTransition(Arc::new(RwLock::new(Some(self.0.clone().build_with_additional_fee(
payjoin::bitcoin::Amount::from_sat(max_fee_contribution),
change_index.map(|x| x as usize),
payjoin::bitcoin::FeeRate::from_sat_per_kwu(min_fee_rate),
clamp_fee_contribution,
)))))
}
/// Perform Payjoin without incentivizing the payee to cooperate.
///
/// While it's generally better to offer some contribution some users may wish not to.
/// This function disables contribution.
pub fn build_non_incentivizing(&self, min_fee_rate: u64) -> InitInputsTransition {
InitInputsTransition(Arc::new(RwLock::new(Some(
self.0
.clone()
.build_non_incentivizing(payjoin::bitcoin::FeeRate::from_sat_per_kwu(min_fee_rate)),
))))
}
}
#[derive(Clone)]
pub struct WithReplyKey(payjoin::send::v2::Sender<payjoin::send::v2::WithReplyKey>);
impl From<payjoin::send::v2::Sender<payjoin::send::v2::WithReplyKey>> for WithReplyKey {
fn from(value: payjoin::send::v2::Sender<payjoin::send::v2::WithReplyKey>) -> Self {
Self(value)
}
}
impl From<WithReplyKey> for payjoin::send::v2::Sender<payjoin::send::v2::WithReplyKey> {
fn from(value: WithReplyKey) -> Self { value.0 }
}
#[allow(clippy::type_complexity)]
pub struct WithReplyKeyTransition(
Arc<
RwLock<
Option<
payjoin::persist::MaybeFatalTransition<
payjoin::send::v2::SessionEvent,
payjoin::send::v2::Sender<payjoin::send::v2::V2GetContext>,
payjoin::send::v2::EncapsulationError,
>,
>,
>,
>,
);
impl WithReplyKeyTransition {
pub fn save<P>(&self, persister: &P) -> Result<V2GetContext, SenderPersistedError>
where
P: SessionPersister<SessionEvent = payjoin::send::v2::SessionEvent>,
{
let mut inner = self.0.write().map_err(|_| {
SenderPersistedError::Storage(Arc::new(ImplementationError::from(
"Lock poisoned".to_string(),
)))
})?;
let value = inner.take().ok_or_else(|| {
SenderPersistedError::Storage(Arc::new(ImplementationError::from(
"Already saved or moved".to_string(),
)))
})?;
let res = value.save(persister).map_err(SenderPersistedError::from)?;
Ok(res.into())
}
}
impl WithReplyKey {
pub fn create_v1_post_request(&self) -> (Request, V1Context) {
let (req, ctx) = self.0.clone().create_v1_post_request();
(req.into(), ctx.into())
}
/// Construct serialized Request and Context from a Payjoin Proposal.
///
/// Important: This request must not be retried or reused on failure.
/// Retransmitting the same ciphertext breaks OHTTP privacy properties.
/// The specific concern is that the relay can see that a request is being retried.
pub fn create_v2_post_request(
&self,
ohttp_relay: String,
) -> Result<(Request, V2PostContext), CreateRequestError> {
match self.0.create_v2_post_request(ohttp_relay) {
Ok((req, ctx)) => Ok((req.into(), ctx.into())),
Err(e) => Err(e.into()),
}
}
/// Decodes and validates the response.
/// Call this method with response from receiver to continue BIP-??? flow. A successful response can either be None if the relay has not response yet or Some(Psbt).
/// If the response is some valid PSBT you should sign and broadcast.
pub fn process_response(
&self,
response: &[u8],
post_ctx: V2PostContext,
) -> WithReplyKeyTransition {
WithReplyKeyTransition(Arc::new(RwLock::new(Some(
self.clone().0.process_response(response, post_ctx.into()),
))))
}
}
/// Data required for validation of response.
/// This type is used to process the response. Get it from SenderBuilder's build methods. Then you only need to call .process_response() on it to continue BIP78 flow.
#[derive(Clone)]
pub struct V1Context(Arc<payjoin::send::V1Context>);
impl From<payjoin::send::V1Context> for V1Context {
fn from(value: payjoin::send::V1Context) -> Self { Self(Arc::new(value)) }
}
impl V1Context {
///Decodes and validates the response.
/// Call this method with response from receiver to continue BIP78 flow. If the response is valid you will get appropriate PSBT that you should sign and broadcast.
pub fn process_response(&self, response: &[u8]) -> Result<String, ResponseError> {
<payjoin::send::V1Context as Clone>::clone(&self.0.clone())
.process_response(response)
.map(|e| e.to_string())
.map_err(Into::into)
}
}
pub struct V2PostContext(payjoin::send::v2::V2PostContext);
impl From<V2PostContext> for payjoin::send::v2::V2PostContext {
fn from(value: V2PostContext) -> Self { value.0 }
}
impl From<payjoin::send::v2::V2PostContext> for V2PostContext {
fn from(value: payjoin::send::v2::V2PostContext) -> Self { Self(value) }
}
pub struct V2GetContext(payjoin::send::v2::Sender<payjoin::send::v2::V2GetContext>);
impl From<payjoin::send::v2::Sender<payjoin::send::v2::V2GetContext>> for V2GetContext {
fn from(value: payjoin::send::v2::Sender<payjoin::send::v2::V2GetContext>) -> Self {
Self(value)
}
}
pub struct V2GetContextTransitionOutcome(
payjoin::persist::OptionalTransitionOutcome<
payjoin::bitcoin::Psbt,
payjoin::send::v2::Sender<payjoin::send::v2::V2GetContext>,
>,
);
impl V2GetContextTransitionOutcome {
pub fn is_none(&self) -> bool { self.0.is_none() }
pub fn is_success(&self) -> bool { self.0.is_success() }
pub fn success(&self) -> Option<Psbt> { self.0.success().map(|r| r.clone().into()) }
}
impl
From<
payjoin::persist::OptionalTransitionOutcome<
payjoin::bitcoin::Psbt,
payjoin::send::v2::Sender<payjoin::send::v2::V2GetContext>,
>,
> for V2GetContextTransitionOutcome
{
fn from(
value: payjoin::persist::OptionalTransitionOutcome<
payjoin::bitcoin::Psbt,
payjoin::send::v2::Sender<payjoin::send::v2::V2GetContext>,
>,
) -> Self {
Self(value)
}
}
#[allow(clippy::type_complexity)]
pub struct V2GetContextTransition(
Arc<
RwLock<
Option<
payjoin::persist::MaybeSuccessTransitionWithNoResults<
payjoin::send::v2::SessionEvent,
payjoin::bitcoin::Psbt,
payjoin::send::v2::Sender<payjoin::send::v2::V2GetContext>,
payjoin::send::ResponseError,
>,
>,
>,
>,
);
impl V2GetContextTransition {
pub fn save<P>(
&self,
persister: &P,
) -> Result<V2GetContextTransitionOutcome, SenderPersistedError>
where
P: SessionPersister<SessionEvent = payjoin::send::v2::SessionEvent>,
{
let mut inner = self.0.write().map_err(|_| {
SenderPersistedError::Storage(Arc::new(ImplementationError::from(
"Lock poisoned".to_string(),
)))
})?;
let value = inner.take().ok_or_else(|| {
SenderPersistedError::Storage(Arc::new(ImplementationError::from(
"Already saved or moved".to_string(),
)))
})?;
let res = value.save(persister).map_err(SenderPersistedError::from)?;
Ok(res.into())
}
}
impl V2GetContext {
pub fn create_poll_request(
&self,
ohttp_relay: String,
) -> Result<(Request, ClientResponse), CreateRequestError> {
self.0
.create_poll_request(ohttp_relay)
.map(|(req, ctx)| (req.into(), ctx.into()))
.map_err(|e| e.into())
}
/// Decodes and validates the response.
/// Call this method with response from receiver to continue BIP-??? flow. A successful response can either be None if the relay has not response yet or Some(Psbt).
/// If the response is some valid PSBT you should sign and broadcast.
pub fn process_response(
&self,
response: &[u8],
ohttp_ctx: &ClientResponse,
) -> V2GetContextTransition {
V2GetContextTransition(Arc::new(RwLock::new(Some(
self.0.process_response(response, ohttp_ctx.into()),
))))
}
}