mpp 0.15.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
use super::*;

/// Build a co-signed `0x76` transaction the same way `broadcast_transaction`
/// does, for exercising `simulate_before_broadcast` against a mocked node.
async fn make_cosigned_fee_payer_tx() -> Vec<u8> {
    use super::super::super::FeePayerEnvelope78;
    use alloy::signers::SignerSync;

    let client_signer = alloy::signers::local::PrivateKeySigner::random();
    let fee_payer_signer = alloy::signers::local::PrivateKeySigner::random();
    let fee_token = KnownTempoNetwork::Mainnet
        .default_currency()
        .parse::<Address>()
        .unwrap();

    let tx = make_fee_payer_tx(60);
    let sig_hash = tx.signature_hash();
    let sig = client_signer.sign_hash_sync(&sig_hash).unwrap();
    let signature: tempo_alloy::primitives::transaction::TempoSignature = sig.into();
    let envelope = FeePayerEnvelope78::from_signing_tx(tx, client_signer.address(), signature)
        .encoded_envelope();

    let provider =
        alloy::providers::ProviderBuilder::new_with_network::<tempo_alloy::TempoNetwork>()
            .connect_http("http://127.0.0.1:1".parse().unwrap());
    let method = ChargeMethod::new(provider).with_fee_payer(fee_payer_signer);
    method
        .cosign_fee_payer_transaction(
            &envelope,
            method.fee_payer_signer.as_deref().unwrap(),
            fee_token,
        )
        .await
        .expect("cosign should succeed")
}

/// Co-signed `0x76` tx whose client signature is a keychain (access-key)
/// signature. Returns `(cosigned bytes, wallet, access key)`.
async fn make_keychain_cosigned_fee_payer_tx() -> (Vec<u8>, Address, Address) {
    use super::super::super::FeePayerEnvelope78;
    use alloy::signers::SignerSync;
    use tempo_alloy::primitives::transaction::{
        KeychainSignature, PrimitiveSignature, TempoSignature,
    };

    let wallet = Address::repeat_byte(0xab);
    let access_key_signer = alloy::signers::local::PrivateKeySigner::random();
    let fee_payer_signer = alloy::signers::local::PrivateKeySigner::random();
    let fee_token = KnownTempoNetwork::Mainnet
        .default_currency()
        .parse::<Address>()
        .unwrap();

    let tx = make_fee_payer_tx(60);
    let sig_hash = tx.signature_hash();
    // V1 keychain signs sig_hash directly; inner signer is the access key.
    let inner = access_key_signer.sign_hash_sync(&sig_hash).unwrap();
    let keychain_sig = KeychainSignature::new_v1(wallet, PrimitiveSignature::Secp256k1(inner));
    let signature = TempoSignature::Keychain(keychain_sig);
    let envelope = FeePayerEnvelope78::from_signing_tx(tx, wallet, signature).encoded_envelope();

    let provider =
        alloy::providers::ProviderBuilder::new_with_network::<tempo_alloy::TempoNetwork>()
            .connect_http("http://127.0.0.1:1".parse().unwrap());
    let method = ChargeMethod::new(provider).with_fee_payer(fee_payer_signer);
    let cosigned = method
        .cosign_fee_payer_transaction(
            &envelope,
            method.fee_payer_signer.as_deref().unwrap(),
            fee_token,
        )
        .await
        .expect("cosign should succeed");

    (cosigned, wallet, access_key_signer.address())
}

/// A keychain tx's request sets `from` to the wallet and carries the access
/// key as `keyId`/`keyType`, serialized onto the wire.
#[tokio::test]
async fn test_build_simulate_payload_includes_keychain_fields() {
    let (cosigned, wallet, access_key) = make_keychain_cosigned_fee_payer_tx().await;

    let payload =
        ChargeMethod::<alloy::providers::RootProvider<tempo_alloy::TempoNetwork>>::build_simulate_payload(
            &cosigned,
        )
        .expect("payload must build");

    let call = &payload.block_state_calls[0].calls[0];

    assert_eq!(call.inner.from, Some(wallet), "from must be the wallet");
    assert_eq!(
        call.key_id,
        Some(access_key),
        "keyId must be the access key, not the wallet"
    );
    assert_eq!(
        call.key_type,
        Some(tempo_alloy::primitives::SignatureType::Secp256k1)
    );
    // Otherwise the assertions above would be vacuous.
    assert_ne!(wallet, access_key);

    // Confirm the keychain fields serialize onto the wire.
    let p = serde_json::to_value(&payload).unwrap();
    let wire_call = &p["blockStateCalls"][0]["calls"][0];
    assert_eq!(
        wire_call["keyId"].as_str().unwrap().to_lowercase(),
        format!("{:#x}", access_key),
    );
    assert!(wire_call["keyType"].is_string() || wire_call["keyType"].is_number());
}

/// A plain EOA tx carries no `keyId` but still advertises its `keyType`
/// so the node sizes signature gas correctly.
#[tokio::test]
async fn test_build_simulate_payload_omits_keychain_for_primitive_sig() {
    let cosigned = make_cosigned_fee_payer_tx().await;

    let payload =
        ChargeMethod::<alloy::providers::RootProvider<tempo_alloy::TempoNetwork>>::build_simulate_payload(
            &cosigned,
        )
        .expect("payload must build");

    let call = &payload.block_state_calls[0].calls[0];
    assert!(call.key_id.is_none(), "plain EOA tx must not set keyId");
    assert_eq!(
        call.key_type,
        Some(tempo_alloy::primitives::SignatureType::Secp256k1),
        "primitive tx must advertise its keyType for gas sizing"
    );
    assert!(
        call.key_data.is_none(),
        "secp256k1 tx has no WebAuthn auth data"
    );
}

/// The `tempo_simulateV1` request must carry the full sponsor/cosign ABI:
/// `from` (recovered sender), `feeToken`, `feePayerSignature`, `nonceKey`,
/// the validity window, and `validation: false`. The single payment call is
/// folded into `to`/`input` (not left in `calls`) so the node does not read
/// the empty `to` as a CREATE.
#[tokio::test]
async fn test_build_simulate_payload_request_abi() {
    let cosigned = make_cosigned_fee_payer_tx().await;

    // The from we expect is the client sender recovered from the cosigned tx.
    let signed = tempo_alloy::primitives::AASigned::decode_2718(&mut cosigned.as_slice()).unwrap();
    let expected_from = signed.recover_signer().unwrap();
    let expected_calls = signed.tx().calls.clone();

    let payload =
        ChargeMethod::<alloy::providers::RootProvider<tempo_alloy::TempoNetwork>>::build_simulate_payload(
            &cosigned,
        )
        .expect("payload must build");

    // `build_aa()` must reproduce the original call list exactly, so the
    // fee-payer signature still recovers.
    let rebuilt = payload.block_state_calls[0].calls[0]
        .clone()
        .build_aa()
        .expect("request must rebuild into an AA tx");
    assert_eq!(
        rebuilt.calls, expected_calls,
        "rebuilt batch must match the signed tx's calls"
    );

    // Serialize exactly as it goes over the wire (single-element param array).
    let params = serde_json::to_value((payload,)).unwrap();
    let arr = params.as_array().expect("params serialize to a JSON array");
    assert_eq!(
        arr.len(),
        1,
        "tempo_simulateV1 takes a single payload param"
    );

    let p = &arr[0];
    assert_eq!(p["validation"], serde_json::json!(false));

    let call = &p["blockStateCalls"][0]["calls"][0];
    assert_eq!(
        call["from"].as_str().unwrap().to_lowercase(),
        format!("{:#x}", expected_from),
        "request must set the recovered sender as `from`"
    );
    // The single payment call must be folded into `to` (so the node does
    // not treat the empty `to` as a CREATE) — not left in `calls`.
    assert!(
        call["to"].is_string(),
        "payment call must be folded into `to`: {call}"
    );
    assert!(
        call["calls"]
            .as_array()
            .map(|c| c.is_empty())
            .unwrap_or(true),
        "single-call request must leave `calls` empty: {call}"
    );
    // Fee-sponsor fields the node needs to model gas affordability/execution.
    assert!(call["feeToken"].is_string(), "feeToken must be present");
    assert!(
        call["feePayerSignature"].is_string() || call["feePayerSignature"].is_object(),
        "feePayerSignature must be present: {call}"
    );
    assert!(call["nonceKey"].is_string(), "nonceKey must be present");
    assert!(
        call["validBefore"].is_string(),
        "validBefore must be present"
    );
}

/// A multi-call AA batch must round-trip with its order preserved: the
/// last call is folded into `to`, the rest stay in `calls`, and the node's
/// `calls ++ [inner.to call]` reconstruction reproduces the original order.
#[test]
fn test_build_simulate_payload_preserves_multi_call_order() {
    use alloy::eips::Encodable2718;
    use alloy::signers::SignerSync;

    let signer = alloy::signers::local::PrivateKeySigner::random();

    // Three distinct calls so a reordering bug (e.g. moving the first call)
    // would be observable.
    let calls = vec![
        tempo_alloy::primitives::transaction::Call {
            to: TxKind::Call(Address::repeat_byte(0x11)),
            value: U256::ZERO,
            input: Bytes::from(vec![0xaa]),
        },
        tempo_alloy::primitives::transaction::Call {
            to: TxKind::Call(Address::repeat_byte(0x22)),
            value: U256::from(7u64),
            input: Bytes::from(vec![0xbb, 0xbb]),
        },
        tempo_alloy::primitives::transaction::Call {
            to: TxKind::Call(Address::repeat_byte(0x33)),
            value: U256::ZERO,
            input: Bytes::from(vec![0xcc, 0xcc, 0xcc]),
        },
    ];

    let mut tx = make_fee_payer_tx(60);
    tx.calls = calls.clone();
    let signature: tempo_alloy::primitives::transaction::TempoSignature =
        signer.sign_hash_sync(&tx.signature_hash()).unwrap().into();
    let signed_bytes = tx.into_signed(signature).encoded_2718();

    let payload =
        ChargeMethod::<alloy::providers::RootProvider<tempo_alloy::TempoNetwork>>::build_simulate_payload(
            &signed_bytes,
        )
        .expect("payload must build");

    let req = &payload.block_state_calls[0].calls[0];
    // N-1 calls stay in `calls`, the last is folded into `to`.
    assert_eq!(req.calls, calls[..2], "first N-1 calls stay in `calls`");
    assert_eq!(req.inner.to, Some(calls[2].to), "last call folds into `to`");
    assert_eq!(req.inner.value, Some(calls[2].value));

    // The node reconstructs `calls ++ [inner.to call]`; verify it matches.
    let rebuilt = req.clone().build_aa().expect("must rebuild");
    assert_eq!(
        rebuilt.calls, calls,
        "reconstructed batch must preserve the original order"
    );
}

/// A reverting simulation must block the broadcast so the sponsor never
/// pays gas for a failing transaction.
#[tokio::test]
async fn test_simulate_before_broadcast_rejects_revert() {
    use alloy::providers::mock::Asserter;

    let cosigned = make_cosigned_fee_payer_tx().await;

    let asserter = Asserter::new();
    asserter.push_success(&serde_json::json!({
        "blocks": [{
            "calls": [{
                "returnData": "0x",
                "gasUsed": "0x5208",
                "status": "0x0",
                "error": { "code": 3, "message": "execution reverted" }
            }]
        }]
    }));

    let provider =
        alloy::providers::ProviderBuilder::new_with_network::<tempo_alloy::TempoNetwork>()
            .connect_mocked_client(asserter);
    let method = ChargeMethod::new(provider);

    let err = method
        .simulate_before_broadcast(&cosigned)
        .await
        .expect_err("reverting simulation must be rejected");
    assert!(
        err.to_string().contains("would revert"),
        "unexpected error: {err}"
    );
    assert!(err.to_string().contains("execution reverted"));
}

/// A successful simulation must allow the broadcast to proceed.
#[tokio::test]
async fn test_simulate_before_broadcast_accepts_success() {
    use alloy::providers::mock::Asserter;

    let cosigned = make_cosigned_fee_payer_tx().await;

    let asserter = Asserter::new();
    asserter.push_success(&serde_json::json!({
        "blocks": [{
            "calls": [{
                "returnData": "0x",
                "gasUsed": "0x5208",
                "status": "0x1"
            }]
        }]
    }));

    let provider =
        alloy::providers::ProviderBuilder::new_with_network::<tempo_alloy::TempoNetwork>()
            .connect_mocked_client(asserter);
    let method = ChargeMethod::new(provider);

    method
        .simulate_before_broadcast(&cosigned)
        .await
        .expect("successful simulation must pass");
}

/// If the simulation RPC itself errors, fail closed: the sponsor must not
/// broadcast a transaction it could not simulate.
#[tokio::test]
async fn test_simulate_before_broadcast_fails_closed_on_rpc_error() {
    use alloy::providers::mock::Asserter;

    let cosigned = make_cosigned_fee_payer_tx().await;

    let asserter = Asserter::new();
    asserter.push_failure_msg("tempo_simulateV1 unavailable");

    let provider =
        alloy::providers::ProviderBuilder::new_with_network::<tempo_alloy::TempoNetwork>()
            .connect_mocked_client(asserter);
    let method = ChargeMethod::new(provider);

    let err = method
        .simulate_before_broadcast(&cosigned)
        .await
        .expect_err("RPC failure must fail closed");
    assert!(
        err.to_string().contains("Pre-broadcast simulation failed"),
        "unexpected error: {err}"
    );
}

/// A node that doesn't implement `tempo_simulateV1` (JSON-RPC "method not
/// found", -32601) is asked to `eth_call` the calls from the sender, so a
/// reverting transaction is still caught before the sponsor pays for it.
#[tokio::test]
async fn test_simulate_before_broadcast_falls_back_to_eth_call() {
    use alloy::providers::mock::Asserter;

    let cosigned = make_cosigned_fee_payer_tx().await;

    let method_with = |eth_call: Result<Bytes, alloy_json_rpc::ErrorPayload>| {
        let asserter = Asserter::new();
        asserter.push_failure(alloy_json_rpc::ErrorPayload {
            code: -32601,
            message: "the method tempo_simulateV1 does not exist/is not available".into(),
            data: None,
        });
        match eth_call {
            Ok(output) => asserter.push_success(&output),
            Err(error) => asserter.push_failure(error),
        }
        let provider =
            alloy::providers::ProviderBuilder::new_with_network::<tempo_alloy::TempoNetwork>()
                .connect_mocked_client(asserter.clone());
        (ChargeMethod::new(provider), asserter)
    };

    let (method, asserter) = method_with(Ok(Bytes::from(U256::from(1).to_be_bytes::<32>())));
    method
        .simulate_before_broadcast(&cosigned)
        .await
        .expect("successful eth_call must pass");
    assert!(asserter.read_q().is_empty(), "eth_call must be issued");

    let (method, _) = method_with(Err(alloy_json_rpc::ErrorPayload {
        code: 3,
        message: "execution reverted: InsufficientBalance".into(),
        data: None,
    }));
    let err = method
        .simulate_before_broadcast(&cosigned)
        .await
        .expect_err("reverting eth_call must be rejected");
    assert!(err.to_string().contains("would revert"), "got: {err}");
    assert!(err.to_string().contains("InsufficientBalance"));

    let (method, _) = method_with(Err(alloy_json_rpc::ErrorPayload {
        code: -32601,
        message: "the method eth_call does not exist/is not available".into(),
        data: None,
    }));
    let err = method
        .simulate_before_broadcast(&cosigned)
        .await
        .expect_err("a node that cannot simulate at all must fail closed");
    assert!(
        err.to_string().contains("Pre-broadcast simulation failed"),
        "got: {err}"
    );
}

/// The `eth_call` fallback runs the calls from the sender without fee
/// fields, so it does not depend on the sender holding a fee token.
#[tokio::test]
async fn test_sender_call_request_keeps_only_sender_and_calls() {
    let cosigned = make_cosigned_fee_payer_tx().await;
    let signed = tempo_alloy::primitives::AASigned::decode_2718(&mut cosigned.as_slice()).unwrap();
    let sender = signed.recover_signer().unwrap();

    let mut payload =
        ChargeMethod::<alloy::providers::RootProvider<tempo_alloy::TempoNetwork>>::build_simulate_payload(
            &cosigned,
        )
        .unwrap();
    let request =
        ChargeMethod::<alloy::providers::RootProvider<tempo_alloy::TempoNetwork>>::sender_call_request(
            payload.block_state_calls.remove(0).calls.remove(0),
        );

    let wire = serde_json::to_value(&request).unwrap();
    assert_eq!(
        wire["from"].as_str().unwrap().to_lowercase(),
        format!("{sender:#x}")
    );
    assert!(wire["to"].is_string() && wire["input"].is_string());
    for field in [
        "feePayerSignature",
        "nonceKey",
        "validBefore",
        "gas",
        "maxFeePerGas",
        "maxPriorityFeePerGas",
    ] {
        assert!(wire.get(field).is_none(), "{field} must be omitted: {wire}");
    }
    assert!(wire["feeToken"].is_null());
}