r402-extensions 0.21.0

Protocol extension implementations for the x402 payment protocol.
Documentation
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
394
395
396
397
398
399
400
401
402
403
404
//! Resource-server declare, issuer, and 402/200 enrich.

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use r402_protocol::extension::{AdvertiseContext, Extension, SettleContext};
use r402_protocol::payment::{ExtensionEntry, PaymentRequirements, SettleResponse};
use serde_json::{Value, json};

use super::create::{
    create_offer_eip712, create_offer_jws, create_receipt_eip712, create_receipt_jws,
};
use super::eip712::Eip712DigestSigner;
use super::jws::JwsSigner;
use super::{
    OFFER_RECEIPT, OfferInput, OfferReceiptError, ReceiptInput, SignatureFormat, SignedOffer,
    SignedReceipt,
};

/// Boxed future used by [`DynOfferReceiptIssuer`].
pub(super) type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

/// High-level issuer that signs offers and receipts.
pub trait OfferReceiptIssuer: Send + Sync {
    /// Key identifier DID.
    fn kid(&self) -> &str;
    /// Signature format.
    fn format(&self) -> SignatureFormat;
    /// Sign an offer for `resource_url` / `input`.
    fn issue_offer(
        &self,
        resource_url: &str,
        input: OfferInput,
    ) -> impl Future<Output = Result<SignedOffer, OfferReceiptError>> + Send;
    /// Sign a receipt.
    fn issue_receipt(
        &self,
        input: ReceiptInput,
    ) -> impl Future<Output = Result<SignedReceipt, OfferReceiptError>> + Send;
}

/// Object-safe issuer.
pub trait DynOfferReceiptIssuer: Send + Sync {
    /// See [`OfferReceiptIssuer::kid`].
    fn kid(&self) -> &str;
    /// See [`OfferReceiptIssuer::format`].
    fn format(&self) -> SignatureFormat;
    /// See [`OfferReceiptIssuer::issue_offer`].
    fn issue_offer<'a>(
        &'a self,
        resource_url: &'a str,
        input: OfferInput,
    ) -> BoxFuture<'a, Result<SignedOffer, OfferReceiptError>>;
    /// See [`OfferReceiptIssuer::issue_receipt`].
    fn issue_receipt(
        &self,
        input: ReceiptInput,
    ) -> BoxFuture<'_, Result<SignedReceipt, OfferReceiptError>>;
}

impl<T: OfferReceiptIssuer + ?Sized> DynOfferReceiptIssuer for T {
    fn kid(&self) -> &str {
        <Self as OfferReceiptIssuer>::kid(self)
    }

    fn format(&self) -> SignatureFormat {
        <Self as OfferReceiptIssuer>::format(self)
    }

    fn issue_offer<'a>(
        &'a self,
        resource_url: &'a str,
        input: OfferInput,
    ) -> BoxFuture<'a, Result<SignedOffer, OfferReceiptError>> {
        Box::pin(<Self as OfferReceiptIssuer>::issue_offer(
            self,
            resource_url,
            input,
        ))
    }

    fn issue_receipt(
        &self,
        input: ReceiptInput,
    ) -> BoxFuture<'_, Result<SignedReceipt, OfferReceiptError>> {
        Box::pin(<Self as OfferReceiptIssuer>::issue_receipt(self, input))
    }
}

/// JWS issuer.
#[derive(Clone)]
pub struct JwsOfferReceiptIssuer<S> {
    signer: S,
}

impl<S> std::fmt::Debug for JwsOfferReceiptIssuer<S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("JwsOfferReceiptIssuer")
            .finish_non_exhaustive()
    }
}

impl<S: JwsSigner> JwsOfferReceiptIssuer<S> {
    /// Wrap a JWS signer.
    #[must_use]
    pub const fn new(signer: S) -> Self {
        Self { signer }
    }
}

impl<S: JwsSigner> OfferReceiptIssuer for JwsOfferReceiptIssuer<S> {
    fn kid(&self) -> &str {
        self.signer.kid()
    }

    fn format(&self) -> SignatureFormat {
        SignatureFormat::Jws
    }

    fn issue_offer(
        &self,
        resource_url: &str,
        input: OfferInput,
    ) -> impl Future<Output = Result<SignedOffer, OfferReceiptError>> + Send {
        std::future::ready(create_offer_jws(resource_url, &input, &self.signer))
    }

    fn issue_receipt(
        &self,
        input: ReceiptInput,
    ) -> impl Future<Output = Result<SignedReceipt, OfferReceiptError>> + Send {
        std::future::ready(create_receipt_jws(&input, &self.signer))
    }
}

/// EIP-712 issuer.
#[derive(Clone)]
pub struct Eip712OfferReceiptIssuer<S> {
    kid: String,
    signer: S,
}

impl<S> std::fmt::Debug for Eip712OfferReceiptIssuer<S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Eip712OfferReceiptIssuer")
            .field("kid", &self.kid)
            .finish_non_exhaustive()
    }
}

impl<S: Eip712DigestSigner> Eip712OfferReceiptIssuer<S> {
    /// Wrap an EIP-712 digest signer.
    #[must_use]
    pub fn new(kid: impl Into<String>, signer: S) -> Self {
        Self {
            kid: kid.into(),
            signer,
        }
    }
}

impl<S: Eip712DigestSigner> OfferReceiptIssuer for Eip712OfferReceiptIssuer<S> {
    fn kid(&self) -> &str {
        &self.kid
    }

    fn format(&self) -> SignatureFormat {
        SignatureFormat::Eip712
    }

    fn issue_offer(
        &self,
        resource_url: &str,
        input: OfferInput,
    ) -> impl Future<Output = Result<SignedOffer, OfferReceiptError>> + Send {
        std::future::ready(create_offer_eip712(resource_url, &input, &self.signer))
    }

    fn issue_receipt(
        &self,
        input: ReceiptInput,
    ) -> impl Future<Output = Result<SignedReceipt, OfferReceiptError>> + Send {
        std::future::ready(create_receipt_eip712(&input, &self.signer))
    }
}

/// `createJWSOfferReceiptIssuer` analogue.
#[must_use]
pub const fn create_jws_offer_receipt_issuer<S: JwsSigner>(signer: S) -> JwsOfferReceiptIssuer<S> {
    JwsOfferReceiptIssuer::new(signer)
}

/// `createEIP712OfferReceiptIssuer` analogue.
#[must_use]
pub fn create_eip712_offer_receipt_issuer<S: Eip712DigestSigner>(
    kid: impl Into<String>,
    signer: S,
) -> Eip712OfferReceiptIssuer<S> {
    Eip712OfferReceiptIssuer::new(kid, signer)
}

/// Route declaration `{ includeTxHash, offerValiditySeconds }`.
#[must_use]
pub fn declare_offer_receipt_extension(
    include_tx_hash: Option<bool>,
    offer_validity_seconds: Option<u64>,
) -> ExtensionEntry {
    let mut obj = serde_json::Map::new();
    if let Some(flag) = include_tx_hash {
        let _ = obj.insert("includeTxHash".into(), Value::Bool(flag));
    }
    if let Some(secs) = offer_validity_seconds {
        let _ = obj.insert("offerValiditySeconds".into(), json!(secs));
    }
    ExtensionEntry::raw(Value::Object(obj))
}

fn offer_schema() -> Value {
    json!({
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "type": "object",
        "properties": {
            "offers": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "format": { "type": "string" },
                        "acceptIndex": { "type": "integer" },
                        "payload": { "type": "object" },
                        "signature": { "type": "string" },
                    },
                    "required": ["format", "signature"],
                },
            },
        },
        "required": ["offers"],
    })
}

fn receipt_schema() -> Value {
    json!({
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "type": "object",
        "properties": {
            "receipt": {
                "type": "object",
                "properties": {
                    "format": { "type": "string" },
                    "payload": { "type": "object" },
                    "signature": { "type": "string" },
                },
                "required": ["format", "signature"],
            },
        },
        "required": ["receipt"],
    })
}

/// Resource-server offer-receipt extension (signed offers on 402, receipts on 200).
#[derive(Clone)]
pub struct OfferReceiptExtension {
    issuer: Arc<dyn DynOfferReceiptIssuer>,
    include_tx_hash: bool,
    offer_validity_seconds: Option<u64>,
}

impl std::fmt::Debug for OfferReceiptExtension {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("OfferReceiptExtension")
            .field("kid", &self.issuer.kid())
            .field("format", &self.issuer.format())
            .field("include_tx_hash", &self.include_tx_hash)
            .finish_non_exhaustive()
    }
}

impl OfferReceiptExtension {
    /// Bind an issuer. Receipts omit `transaction` by default.
    #[must_use]
    pub fn new(issuer: Arc<dyn DynOfferReceiptIssuer>) -> Self {
        Self {
            issuer,
            include_tx_hash: false,
            offer_validity_seconds: None,
        }
    }

    /// Include the settlement transaction hash on receipts.
    #[must_use]
    pub const fn with_include_tx_hash(mut self, include: bool) -> Self {
        self.include_tx_hash = include;
        self
    }

    /// Override offer validity (seconds). Default 300 / accept `maxTimeoutSeconds`.
    #[must_use]
    pub const fn with_offer_validity_seconds(mut self, seconds: u64) -> Self {
        self.offer_validity_seconds = Some(seconds);
        self
    }

    async fn sign_offers(
        &self,
        resource_url: &str,
        ctx: &AdvertiseContext<'_>,
    ) -> Vec<SignedOffer> {
        let (_, declared_validity) = declaration_config(ctx.existing);
        let validity = declared_validity.or(self.offer_validity_seconds);
        let mut offers = Vec::new();
        for (i, requirement) in ctx.accepts.iter().enumerate() {
            let index = u32::try_from(i).unwrap_or(u32::MAX);
            let input = requirements_to_offer_input(requirement, index, validity);
            if let Ok(signed) = self.issuer.issue_offer(resource_url, input).await {
                offers.push(signed);
            }
        }
        offers
    }
}

fn declaration_config(existing: Option<&ExtensionEntry>) -> (Option<bool>, Option<u64>) {
    let Some(existing) = existing else {
        return (None, None);
    };
    let value = existing.to_value();
    let obj = value.get("info").unwrap_or(&value);
    let include = obj.get("includeTxHash").and_then(Value::as_bool);
    let validity = obj.get("offerValiditySeconds").and_then(Value::as_u64);
    (include, validity)
}

fn requirements_to_offer_input(
    requirements: &PaymentRequirements,
    accept_index: u32,
    offer_validity_seconds: Option<u64>,
) -> OfferInput {
    let validity = offer_validity_seconds.or(Some(requirements.max_timeout_seconds));
    OfferInput {
        accept_index,
        scheme: requirements.scheme.to_string(),
        network: requirements.network.to_string(),
        asset: requirements.asset.to_string(),
        pay_to: requirements.pay_to.to_string(),
        amount: requirements.amount.to_string(),
        offer_validity_seconds: validity,
    }
}

impl Extension for OfferReceiptExtension {
    fn id(&self) -> &'static str {
        OFFER_RECEIPT
    }

    fn dynamic_info_fields(&self) -> &'static [&'static str] {
        &["offers"]
    }

    async fn enrich_payment_required(&self, ctx: &AdvertiseContext<'_>) -> Option<ExtensionEntry> {
        let resource_url = ctx.resource.map(|r| r.url.as_str())?;
        let offers = self.sign_offers(resource_url, ctx).await;
        if offers.is_empty() {
            return None;
        }
        serde_json::to_value(&offers)
            .ok()
            .map(|offers| ExtensionEntry::with_schema(json!({ "offers": offers }), offer_schema()))
    }

    async fn on_settle(&self, ctx: &SettleContext<'_>) -> Option<ExtensionEntry> {
        let SettleResponse::Success {
            payer,
            network,
            transaction,
            ..
        } = ctx.response
        else {
            return None;
        };
        let payer = payer.as_deref()?;
        let resource_url = ctx
            .resource_url
            .or_else(|| ctx.payload.resource.as_ref().map(|r| r.url.as_str()))?;
        let (declared_include, _) =
            declaration_config(ctx.advertised.and_then(|ext| ext.get(OFFER_RECEIPT)));
        let include_tx = declared_include.unwrap_or(self.include_tx_hash);
        let tx = if include_tx && !transaction.is_empty() {
            Some(transaction.to_string())
        } else {
            None
        };
        let input = ReceiptInput {
            resource_url: resource_url.to_owned(),
            payer: payer.to_owned(),
            network: network.to_string(),
            transaction: tx,
        };
        let signed = self.issuer.issue_receipt(input).await.ok()?;
        serde_json::to_value(&signed).ok().map(|receipt| {
            ExtensionEntry::with_schema(json!({ "receipt": receipt }), receipt_schema())
        })
    }
}