-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_evm.py
More file actions
73 lines (60 loc) · 2.55 KB
/
Copy pathtest_evm.py
File metadata and controls
73 lines (60 loc) · 2.55 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
"""Tests for the EVM signing primitives."""
from __future__ import annotations
from eth_account import Account
from acedatacloud_x402 import EVMAccountSigner, sign_evm_payment
# Test vector — well-known throwaway key from ethers.js docs. NEVER use in production.
TEST_PRIVATE_KEY = "0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318"
def _fake_requirement() -> dict:
return {
"scheme": "exact",
"network": "eip155:8453",
"amount": "95215",
"maxTimeoutSeconds": 120,
"resource": "https://x402.acedata.cloud/openai/chat/completions",
"description": "chat",
"payTo": "0x4d2f00Dac0aCb02C7211cBDe2DbE9d86D7B7b2F2",
"asset": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
"extra": {
"name": "USD Coin",
"version": "2",
"chainId": 8453,
"verifyingContract": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
},
}
def test_signer_derives_correct_address():
signer = EVMAccountSigner.from_private_key(TEST_PRIVATE_KEY)
expected = Account.from_key(TEST_PRIVATE_KEY).address
assert signer.address == expected
def test_envelope_shape():
signer = EVMAccountSigner.from_private_key(TEST_PRIVATE_KEY)
envelope = sign_evm_payment(_fake_requirement(), signer)
assert envelope["x402Version"] == 2
assert envelope["accepted"]["scheme"] == "exact"
assert envelope["accepted"]["network"] == "eip155:8453"
payload = envelope["payload"]
assert set(payload.keys()) == {"authorization", "signature"}
assert payload["signature"].startswith("0x")
auth = payload["authorization"]
assert auth["from"].lower() == signer.address.lower()
assert auth["to"] == "0x4d2f00Dac0aCb02C7211cBDe2DbE9d86D7B7b2F2"
assert auth["value"] == "95215"
assert auth["validAfter"] == "0"
# nonce is 32 random bytes, hex-encoded
assert auth["nonce"].startswith("0x")
assert len(auth["nonce"]) == 66 # "0x" + 64 hex chars
def test_handler_produces_payment_signature_header():
from acedatacloud_x402 import create_x402_payment_handler
handler = create_x402_payment_handler(
network="base",
evm_signer=EVMAccountSigner.from_private_key(TEST_PRIVATE_KEY),
)
result = handler(
{
"url": "https://x402.acedata.cloud/openai/chat/completions",
"method": "POST",
"accepts": [_fake_requirement()],
}
)
assert "headers" in result
assert "PAYMENT-SIGNATURE" in result["headers"]
assert len(result["headers"]["PAYMENT-SIGNATURE"]) > 20