mpp 0.12.0

Rust SDK for the Machine Payments Protocol (MPP)
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Store-independent TIP-1034 channel recovery.

use alloy::{
    primitives::{Address, B256},
    providers::Provider,
    sol_types::SolCall,
};
use tempo_alloy::{
    contracts::precompiles::{ITIP20ChannelReserve, TIP20_CHANNEL_RESERVE_ADDRESS},
    rpc::TempoTransactionRequest,
    TempoNetwork,
};

use super::{
    channel_ops::{
        compute_precompile_channel_id_from_descriptor_with_escrow,
        encode_precompile_get_channel_state_call,
    },
    store::StoredChannelEntry,
};
use crate::{
    error::{MppError, ResultExt},
    protocol::methods::tempo::{
        precompile_voucher::verify_precompile_voucher_signature,
        session::{ChannelDescriptor, SessionSnapshot},
    },
};

/// Current mutable state read from the TIP-1034 precompile.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OnChainChannelState {
    /// Cumulative value already settled on-chain.
    pub settled: u128,
    /// Total channel deposit.
    pub deposit: u128,
    /// Close-request timestamp, or zero while reusable.
    pub close_requested_at: u64,
}

/// Read mutable TIP-1034 channel state by channel ID.
pub async fn read_on_chain_channel_state<P: Provider<TempoNetwork>>(
    provider: &P,
    channel_id: B256,
) -> Result<OnChainChannelState, MppError> {
    let mut request = TempoTransactionRequest::default();
    request.inner = request.inner.to(TIP20_CHANNEL_RESERVE_ADDRESS).input(
        alloy::rpc::types::TransactionInput::new(encode_precompile_get_channel_state_call(
            channel_id,
        )),
    );
    let result = provider
        .call(request)
        .await
        .mpp_http("failed to read TIP-1034 channel state")?;
    let decoded = ITIP20ChannelReserve::getChannelStateCall::abi_decode_returns(&result)
        .mpp_http("failed to decode TIP-1034 channel state")?;
    Ok(OnChainChannelState {
        settled: decoded.settled.to::<u128>(),
        deposit: decoded.deposit.to::<u128>(),
        close_requested_at: u64::from(decoded.closeRequestedAt),
    })
}

/// Expected identity for a reusable payment scope.
#[derive(Debug, Clone, Copy)]
pub struct RecoveryScope {
    /// Root payer account.
    pub payer: Address,
    /// Voucher authority controlled by the client.
    pub authorized_signer: Address,
    /// Protected service payee.
    pub payee: Address,
    /// Settlement recipient/operator.
    pub operator: Address,
    /// TIP-20 token.
    pub token: Address,
    /// TIP-1034 escrow/precompile.
    pub escrow: Address,
    /// EVM chain ID.
    pub chain_id: u64,
}

/// Return whether this client controls the voucher authority bound into a descriptor.
///
/// A different authority is expected after access-key rotation and makes the old
/// channel non-reusable; it is not a malformed snapshot.
pub fn can_sign_descriptor(
    descriptor: &ChannelDescriptor,
    payer: Address,
    authorized_signer: Address,
) -> Result<bool, MppError> {
    Ok(
        parse_address("descriptor payer", &descriptor.payer)? == payer
            && descriptor_authority(descriptor)? == authorized_signer,
    )
}

/// Reconcile a locally stored entry with its descriptor and on-chain state.
pub fn recover_stored_channel(
    mut entry: StoredChannelEntry,
    scope: RecoveryScope,
    state: OnChainChannelState,
) -> Result<StoredChannelEntry, MppError> {
    validate_descriptor(&entry.descriptor, entry.channel_id, scope)?;
    if entry.escrow != scope.escrow || entry.chain_id != scope.chain_id {
        return Err(invalid("stored channel scope does not match the challenge"));
    }
    if state.deposit == 0 || state.close_requested_at != 0 {
        return Err(invalid("stored channel is absent or closing on-chain"));
    }
    let cumulative = entry.cumulative_amount.max(state.settled);
    if cumulative > state.deposit {
        return Err(invalid(
            "stored cumulative amount exceeds the on-chain channel deposit",
        ));
    }
    entry.cumulative_amount = cumulative;
    entry.deposit = state.deposit;
    entry.opened = true;
    Ok(entry)
}

/// Hydrate a fresh client store from a server snapshot plus on-chain state.
pub fn hydrate_session_snapshot(
    snapshot: &SessionSnapshot,
    scope: RecoveryScope,
    state: OnChainChannelState,
) -> Result<StoredChannelEntry, MppError> {
    let channel_id = parse_b256("snapshot channelId", &snapshot.channel_id)?;
    let escrow = parse_address("snapshot escrow", &snapshot.escrow)?;
    if snapshot.chain_id != scope.chain_id || escrow != scope.escrow {
        return Err(invalid(
            "snapshot chain or escrow does not match the challenge",
        ));
    }
    validate_descriptor(&snapshot.descriptor, channel_id, scope)?;

    let signed = snapshot
        .highest_voucher
        .as_ref()
        .ok_or_else(|| invalid("session snapshot is missing its highest signed voucher"))?;
    let voucher_channel_id = parse_b256("snapshot voucher channelId", &signed.channel_id)?;
    if voucher_channel_id != channel_id {
        return Err(invalid(
            "snapshot voucher channelId does not match snapshot channelId",
        ));
    }

    let accepted = parse_amount("snapshot acceptedCumulative", &snapshot.accepted_cumulative)?;
    let voucher_cumulative = parse_amount(
        "snapshot voucher cumulativeAmount",
        &signed.cumulative_amount,
    )?;
    let required = parse_amount("snapshot requiredCumulative", &snapshot.required_cumulative)?;
    let snapshot_deposit = parse_amount("snapshot deposit", &snapshot.deposit)?;
    let snapshot_settled = parse_amount("snapshot settled", &snapshot.settled)?;
    let spent = parse_amount("snapshot spent", &snapshot.spent)?;
    if voucher_cumulative != accepted {
        return Err(invalid(
            "snapshot voucher amount does not match acceptedCumulative",
        ));
    }
    if spent > accepted || snapshot_settled > snapshot_deposit {
        return Err(invalid("snapshot amounts are inconsistent"));
    }
    if state.deposit == 0 || state.close_requested_at != 0 {
        return Err(invalid("snapshot channel is absent or closing on-chain"));
    }

    let signature = alloy::hex::decode(signed.signature.trim_start_matches("0x"))
        .map_err(|e| invalid(format!("invalid snapshot voucher signature: {e}")))?;
    let authority = descriptor_authority(&snapshot.descriptor)?;
    if !verify_precompile_voucher_signature(
        &signature,
        authority,
        channel_id,
        voucher_cumulative,
        escrow,
        snapshot.chain_id,
    )? {
        return Err(invalid("snapshot highest voucher signature is invalid"));
    }

    let cumulative_amount = accepted.max(required).max(state.settled);
    if cumulative_amount > state.deposit {
        return Err(invalid(
            "recovered cumulative amount exceeds the on-chain channel deposit",
        ));
    }
    Ok(StoredChannelEntry {
        channel_id,
        cumulative_amount,
        deposit: state.deposit,
        descriptor: snapshot.descriptor.clone(),
        settlement_route: snapshot.settlement_route.clone(),
        escrow,
        chain_id: snapshot.chain_id,
        opened: true,
    })
}

fn validate_descriptor(
    descriptor: &ChannelDescriptor,
    channel_id: B256,
    scope: RecoveryScope,
) -> Result<(), MppError> {
    let payer = parse_address("descriptor payer", &descriptor.payer)?;
    let payee = parse_address("descriptor payee", &descriptor.payee)?;
    let operator = parse_address("descriptor operator", &descriptor.operator)?;
    let token = parse_address("descriptor token", &descriptor.token)?;
    let authority = descriptor_authority(descriptor)?;
    if payer != scope.payer
        || payee != scope.payee
        || operator != scope.operator
        || token != scope.token
        || authority != scope.authorized_signer
    {
        return Err(invalid(
            "channel descriptor identity does not match the challenge",
        ));
    }
    let expected = compute_precompile_channel_id_from_descriptor_with_escrow(
        descriptor,
        scope.escrow,
        scope.chain_id,
    )?;
    if expected != channel_id {
        return Err(invalid("channelId does not match its descriptor"));
    }
    Ok(())
}

fn descriptor_authority(descriptor: &ChannelDescriptor) -> Result<Address, MppError> {
    let authority = parse_address("descriptor authorizedSigner", &descriptor.authorized_signer)?;
    if authority == Address::ZERO {
        parse_address("descriptor payer", &descriptor.payer)
    } else {
        Ok(authority)
    }
}

fn parse_address(label: &str, value: &str) -> Result<Address, MppError> {
    value
        .parse()
        .map_err(|e| invalid(format!("invalid {label}: {e}")))
}

fn parse_b256(label: &str, value: &str) -> Result<B256, MppError> {
    value
        .parse()
        .map_err(|e| invalid(format!("invalid {label}: {e}")))
}

fn parse_amount(label: &str, value: &str) -> Result<u128, MppError> {
    value
        .parse()
        .map_err(|e| invalid(format!("invalid {label}: {e}")))
}

fn invalid(message: impl Into<String>) -> MppError {
    MppError::InvalidConfig(message.into())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        client::tempo::{
            session::channel_ops::build_channel_descriptor,
            signing::{P256Jwk, TempoP256Signer},
        },
        protocol::methods::tempo::{
            precompile_voucher::sign_precompile_voucher_primitive, session::SnapshotVoucher,
        },
    };
    use alloy::signers::Signer;
    use tempo_alloy::contracts::precompiles::TIP20_CHANNEL_RESERVE_ADDRESS;

    #[tokio::test]
    async fn reads_tip1034_channel_state_via_alloy_provider() {
        use alloy::{
            primitives::{Bytes, Uint},
            providers::{mock::Asserter, ProviderBuilder},
            sol_types::SolCall,
        };

        let state = ITIP20ChannelReserve::ChannelState {
            settled: Uint::<96, 2>::from(123u64),
            deposit: Uint::<96, 2>::from(1_000u64),
            closeRequestedAt: 0,
        };
        let response =
            Bytes::from(ITIP20ChannelReserve::getChannelStateCall::abi_encode_returns(&state));
        let asserter = Asserter::new();
        asserter.push_success(&response);
        let provider =
            ProviderBuilder::new_with_network::<TempoNetwork>().connect_mocked_client(asserter);

        let actual = read_on_chain_channel_state(&provider, B256::repeat_byte(0x11))
            .await
            .unwrap();
        assert_eq!(
            actual,
            OnChainChannelState {
                settled: 123,
                deposit: 1_000,
                close_requested_at: 0,
            }
        );
    }

    fn signer() -> TempoP256Signer {
        TempoP256Signer::from_webcrypto_jwk(&P256Jwk {
            kty: "EC".into(),
            crv: "P-256".into(),
            x: "OtOGGpViE5JRa7WT7wVYPtLlhm9ctiYKMBcjf9ibkK8".into(),
            y: "0JYcfjcHWmeRo5xh9WKVsCttJlZ7YV5gqkHuHI6DOI0".into(),
            d: "QkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkI".into(),
        })
        .unwrap()
    }

    fn descriptor(authority: Address) -> ChannelDescriptor {
        build_channel_descriptor(
            Address::repeat_byte(0x10),
            Address::repeat_byte(0x20),
            Address::ZERO,
            Address::repeat_byte(0x30),
            B256::repeat_byte(0x40),
            authority,
            B256::repeat_byte(0x50),
        )
    }

    fn scope(authority: Address) -> RecoveryScope {
        RecoveryScope {
            payer: Address::repeat_byte(0x10),
            authorized_signer: authority,
            payee: Address::repeat_byte(0x20),
            operator: Address::ZERO,
            token: Address::repeat_byte(0x30),
            escrow: TIP20_CHANNEL_RESERVE_ADDRESS,
            chain_id: 4217,
        }
    }

    #[test]
    fn stored_recovery_uses_highest_local_or_settled_boundary() {
        let authority = signer().address();
        let descriptor = descriptor(authority);
        let channel_id = compute_precompile_channel_id_from_descriptor_with_escrow(
            &descriptor,
            TIP20_CHANNEL_RESERVE_ADDRESS,
            4217,
        )
        .unwrap();
        let recovered = recover_stored_channel(
            StoredChannelEntry {
                channel_id,
                cumulative_amount: 200,
                deposit: 900,
                descriptor,
                settlement_route: None,
                escrow: TIP20_CHANNEL_RESERVE_ADDRESS,
                chain_id: 4217,
                opened: true,
            },
            scope(authority),
            OnChainChannelState {
                settled: 300,
                deposit: 1_000,
                close_requested_at: 0,
            },
        )
        .unwrap();
        assert_eq!(recovered.cumulative_amount, 300);
        assert_eq!(recovered.deposit, 1_000);
    }

    #[test]
    fn rotated_access_key_cannot_reuse_old_descriptor() {
        let old_authority = Address::repeat_byte(0x60);
        let new_authority = Address::repeat_byte(0x70);
        assert!(!can_sign_descriptor(
            &descriptor(old_authority),
            Address::repeat_byte(0x10),
            new_authority,
        )
        .unwrap());
    }

    #[tokio::test]
    async fn snapshot_hydration_verifies_p256_voucher() {
        let signer = signer();
        let descriptor = descriptor(signer.address());
        let channel_id = compute_precompile_channel_id_from_descriptor_with_escrow(
            &descriptor,
            TIP20_CHANNEL_RESERVE_ADDRESS,
            4217,
        )
        .unwrap();
        let signature = sign_precompile_voucher_primitive(&signer, channel_id, 200, 4217)
            .await
            .unwrap();
        let snapshot = SessionSnapshot {
            accepted_cumulative: "200".into(),
            chain_id: 4217,
            channel_id: format!("{channel_id:#x}"),
            close_requested_at: None,
            deposit: "1000".into(),
            descriptor,
            settlement_route: None,
            escrow: format!("{TIP20_CHANNEL_RESERVE_ADDRESS:#x}"),
            highest_voucher: Some(SnapshotVoucher {
                channel_id: format!("{channel_id:#x}"),
                cumulative_amount: "200".into(),
                signature: alloy::hex::encode_prefixed(signature),
            }),
            required_cumulative: "250".into(),
            settled: "0".into(),
            spent: "200".into(),
            units: None,
        };
        let entry = hydrate_session_snapshot(
            &snapshot,
            scope(signer.address()),
            OnChainChannelState {
                settled: 0,
                deposit: 1_000,
                close_requested_at: 0,
            },
        )
        .unwrap();
        assert_eq!(entry.cumulative_amount, 250);
    }
}