@agentlayer.tech/wallet 0.1.64 → 0.1.66
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.openclaw/extensions/agent-wallet/openclaw.plugin.json +1 -1
- package/.openclaw/extensions/agent-wallet/package.json +1 -1
- package/VERSION +1 -1
- package/agent-wallet/agent_wallet/__init__.py +1 -1
- package/agent-wallet/agent_wallet/config.py +44 -2
- package/agent-wallet/agent_wallet/encrypted_storage.py +77 -9
- package/agent-wallet/agent_wallet/keystore.py +25 -0
- package/agent-wallet/agent_wallet/openclaw_adapter.py +4 -0
- package/agent-wallet/agent_wallet/openclaw_cli.py +63 -4
- package/agent-wallet/agent_wallet/providers/x402.py +229 -148
- package/agent-wallet/agent_wallet/sealed_keys.py +58 -2
- package/agent-wallet/agent_wallet/user_wallets.py +93 -5
- package/agent-wallet/openclaw.plugin.json +1 -1
- package/agent-wallet/pyproject.toml +1 -1
- package/agent-wallet/scripts/build_release_bundle.py +10 -2
- package/agent-wallet/skills/wallet-operator/SKILL.md +1 -1
- package/claude-code/plugins/agent-wallet/.claude-plugin/plugin.json +1 -1
- package/claude-code/plugins/agent-wallet/skills/wallet-operator/SKILL.md +1 -1
- package/codex/plugins/agent-wallet/.codex-plugin/plugin.json +1 -1
- package/codex/plugins/agent-wallet/server.py +116 -47
- package/codex/plugins/agent-wallet/skills/wallet-operator/SKILL.md +1 -1
- package/hermes/plugins/agent_wallet/plugin.yaml +1 -1
- package/install-from-github.sh +6 -0
- package/package.json +1 -1
- package/wdk-btc-wallet/package.json +1 -1
- package/wdk-evm-wallet/package.json +1 -1
|
@@ -6,6 +6,7 @@ import base64
|
|
|
6
6
|
import hashlib
|
|
7
7
|
import json
|
|
8
8
|
import logging
|
|
9
|
+
import time
|
|
9
10
|
from typing import Any
|
|
10
11
|
from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit
|
|
11
12
|
|
|
@@ -17,6 +18,11 @@ from agent_wallet.wallet_layer.base import AgentWalletBackend
|
|
|
17
18
|
CDP_BAZAAR_DISCOVERY_BASE_URL = "https://api.cdp.coinbase.com/platform/v2/x402/discovery"
|
|
18
19
|
AGENTIC_MARKET_API_BASE_URL = "https://api.agentic.market/v1"
|
|
19
20
|
X402_EXECUTE_TIMEOUT_SECONDS = 45.0
|
|
21
|
+
DISCOVERY_CACHE_TTL_SECONDS = 300.0
|
|
22
|
+
_DISCOVERY_CACHE_MAX_ENTRIES = 64
|
|
23
|
+
# Discovery responses cached as JSON text so hits hand out independent copies.
|
|
24
|
+
# Long-lived only inside the resident read worker; cold CLI runs start empty.
|
|
25
|
+
_discovery_cache: dict[str, tuple[float, str]] = {}
|
|
20
26
|
SOLANA_CAIP_BY_NETWORK = {
|
|
21
27
|
"mainnet": "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
|
|
22
28
|
}
|
|
@@ -24,6 +30,12 @@ EVM_CAIP_BY_NETWORK = {
|
|
|
24
30
|
"ethereum": "eip155:1",
|
|
25
31
|
"base": "eip155:8453",
|
|
26
32
|
}
|
|
33
|
+
# x402 v1 requirements carry legacy network names instead of CAIP-2 ids.
|
|
34
|
+
LEGACY_NETWORK_TO_CAIP = {
|
|
35
|
+
"ethereum": "eip155:1",
|
|
36
|
+
"base": "eip155:8453",
|
|
37
|
+
"solana": "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
|
|
38
|
+
}
|
|
27
39
|
_USDC_IDENTIFIERS = {
|
|
28
40
|
"usdc",
|
|
29
41
|
"usd coin",
|
|
@@ -200,6 +212,38 @@ def _decode_payment_required(header_value: str) -> dict[str, Any]:
|
|
|
200
212
|
}
|
|
201
213
|
|
|
202
214
|
|
|
215
|
+
def _caip_network(value: Any) -> str:
|
|
216
|
+
"""Map a requirement network to CAIP-2 form, passing CAIP ids through."""
|
|
217
|
+
network = _trim(value)
|
|
218
|
+
if ":" in network:
|
|
219
|
+
return network
|
|
220
|
+
return LEGACY_NETWORK_TO_CAIP.get(network.lower(), network)
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def _decode_payment_required_body(response: Any) -> dict[str, Any] | None:
|
|
224
|
+
"""Parse a legacy x402 v1 402 response whose requirements live in the JSON body.
|
|
225
|
+
|
|
226
|
+
Returns the same shape as _decode_payment_required, with `encoded` set to a
|
|
227
|
+
base64 form the x402 SDK can decode (it detects the version from the JSON).
|
|
228
|
+
"""
|
|
229
|
+
try:
|
|
230
|
+
payload = response.json()
|
|
231
|
+
except Exception:
|
|
232
|
+
return None
|
|
233
|
+
if not isinstance(payload, dict) or payload.get("x402Version") != 1:
|
|
234
|
+
return None
|
|
235
|
+
accepts = payload.get("accepts")
|
|
236
|
+
if not isinstance(accepts, list) or not accepts:
|
|
237
|
+
return None
|
|
238
|
+
encoded = base64.b64encode(json.dumps(payload).encode("utf-8")).decode("ascii")
|
|
239
|
+
return {
|
|
240
|
+
"x402_version": 1,
|
|
241
|
+
"accepts": accepts,
|
|
242
|
+
"raw": payload,
|
|
243
|
+
"encoded": encoded,
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
|
|
203
247
|
def _extract_requirement_extra(requirement: dict[str, Any]) -> dict[str, Any]:
|
|
204
248
|
extra = requirement.get("extra")
|
|
205
249
|
return dict(extra) if isinstance(extra, dict) else {}
|
|
@@ -210,12 +254,24 @@ def _requirement_field(requirement: Any, field_name: str) -> Any:
|
|
|
210
254
|
aliases = {
|
|
211
255
|
"pay_to": ("pay_to", "payTo"),
|
|
212
256
|
"max_timeout_seconds": ("max_timeout_seconds", "maxTimeoutSeconds"),
|
|
257
|
+
"amount": ("amount", "maxAmountRequired"),
|
|
213
258
|
}
|
|
214
259
|
for candidate in aliases.get(field_name, (field_name,)):
|
|
215
260
|
if candidate in requirement:
|
|
216
261
|
return requirement.get(candidate)
|
|
217
262
|
return None
|
|
218
|
-
|
|
263
|
+
value = getattr(requirement, field_name, None)
|
|
264
|
+
if value is None and field_name == "amount":
|
|
265
|
+
# v1 SDK models expose the amount as max_amount_required / get_amount().
|
|
266
|
+
getter = getattr(requirement, "get_amount", None)
|
|
267
|
+
if callable(getter):
|
|
268
|
+
return getter()
|
|
269
|
+
return value
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def _requirement_amount(requirement: dict[str, Any]) -> str:
|
|
273
|
+
# x402 v2 uses "amount"; v1 uses "maxAmountRequired".
|
|
274
|
+
return _trim(requirement.get("amount") or requirement.get("maxAmountRequired"))
|
|
219
275
|
|
|
220
276
|
|
|
221
277
|
def _looks_like_usdc(requirement: dict[str, Any]) -> bool:
|
|
@@ -227,7 +283,7 @@ def _looks_like_usdc(requirement: dict[str, Any]) -> bool:
|
|
|
227
283
|
|
|
228
284
|
|
|
229
285
|
def _normalize_amount_hint(requirement: dict[str, Any]) -> str | None:
|
|
230
|
-
amount =
|
|
286
|
+
amount = _requirement_amount(requirement)
|
|
231
287
|
if not amount.isdigit():
|
|
232
288
|
return None
|
|
233
289
|
if _looks_like_usdc(requirement):
|
|
@@ -247,7 +303,7 @@ def normalize_payment_requirement(
|
|
|
247
303
|
resource_url: str | None = None,
|
|
248
304
|
) -> dict[str, Any]:
|
|
249
305
|
extra = _extract_requirement_extra(requirement)
|
|
250
|
-
amount =
|
|
306
|
+
amount = _requirement_amount(requirement)
|
|
251
307
|
return {
|
|
252
308
|
"scheme": _trim(requirement.get("scheme")).lower() or None,
|
|
253
309
|
"network": _trim(requirement.get("network")) or None,
|
|
@@ -405,7 +461,7 @@ def _wallet_x402_support_summary(backend: AgentWalletBackend) -> dict[str, Any]:
|
|
|
405
461
|
|
|
406
462
|
def _requirement_compatibility(requirement: dict[str, Any], backend: AgentWalletBackend) -> dict[str, Any]:
|
|
407
463
|
wallet_summary = _wallet_x402_support_summary(backend)
|
|
408
|
-
network =
|
|
464
|
+
network = _caip_network(requirement.get("network"))
|
|
409
465
|
scheme = _trim(requirement.get("scheme")).lower()
|
|
410
466
|
wallet_network_matches = network in wallet_summary["supported_caip_networks"]
|
|
411
467
|
chain = _backend_chain(backend)
|
|
@@ -458,23 +514,24 @@ def _select_preferred_requirement(
|
|
|
458
514
|
requirements: list[dict[str, Any]],
|
|
459
515
|
backend: AgentWalletBackend,
|
|
460
516
|
) -> dict[str, Any] | None:
|
|
461
|
-
compatible = [
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
requirement
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
]
|
|
517
|
+
compatible: list[dict[str, Any]] = []
|
|
518
|
+
exact_match: list[dict[str, Any]] = []
|
|
519
|
+
for requirement in requirements:
|
|
520
|
+
compatibility = _requirement_compatibility(requirement, backend)
|
|
521
|
+
if not compatibility["planned_execution_supported"]:
|
|
522
|
+
continue
|
|
523
|
+
compatible.append(requirement)
|
|
524
|
+
if compatibility["wallet_network_matches"]:
|
|
525
|
+
exact_match.append(requirement)
|
|
471
526
|
candidates = exact_match or compatible
|
|
472
527
|
if not candidates:
|
|
473
528
|
return None
|
|
474
529
|
|
|
475
|
-
def sort_key(item: dict[str, Any]) -> tuple[int,
|
|
530
|
+
def sort_key(item: dict[str, Any]) -> tuple[int, int]:
|
|
476
531
|
amount = _trim(item.get("amount"))
|
|
477
|
-
|
|
532
|
+
if amount.isdigit():
|
|
533
|
+
return (0, int(amount))
|
|
534
|
+
return (1, 0)
|
|
478
535
|
|
|
479
536
|
return sorted(candidates, key=sort_key)[0]
|
|
480
537
|
|
|
@@ -605,14 +662,16 @@ def _parse_payment_required_response(
|
|
|
605
662
|
wallet_summary: dict[str, Any],
|
|
606
663
|
) -> dict[str, Any]:
|
|
607
664
|
payment_required = response.headers.get("PAYMENT-REQUIRED")
|
|
608
|
-
if
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
665
|
+
if payment_required:
|
|
666
|
+
decoded = _decode_payment_required(payment_required)
|
|
667
|
+
else:
|
|
668
|
+
decoded = _decode_payment_required_body(response)
|
|
669
|
+
if decoded is None:
|
|
670
|
+
raise ProviderError(
|
|
671
|
+
"x402-http",
|
|
672
|
+
"Server returned HTTP 402 without a PAYMENT-REQUIRED header or an x402 v1 JSON body.",
|
|
673
|
+
details={"status_code": response.status_code, "url": request["url"]},
|
|
674
|
+
)
|
|
616
675
|
normalized_accepts = [
|
|
617
676
|
normalize_payment_requirement(requirement, source="payment_required", resource_url=request["url"])
|
|
618
677
|
for requirement in decoded["accepts"]
|
|
@@ -651,34 +710,6 @@ def _parse_payment_required_response(
|
|
|
651
710
|
return preview
|
|
652
711
|
|
|
653
712
|
|
|
654
|
-
def _require_executable_payment(
|
|
655
|
-
*,
|
|
656
|
-
preview: dict[str, Any],
|
|
657
|
-
backend: AgentWalletBackend,
|
|
658
|
-
) -> dict[str, Any]:
|
|
659
|
-
selected = preview.get("selected_payment")
|
|
660
|
-
if not isinstance(selected, dict):
|
|
661
|
-
raise ProviderError(
|
|
662
|
-
"x402-http",
|
|
663
|
-
"No compatible x402 payment requirement was selected for this wallet.",
|
|
664
|
-
details={
|
|
665
|
-
"request_url": preview.get("request_url"),
|
|
666
|
-
"accepted_payments": preview.get("accepted_payments"),
|
|
667
|
-
},
|
|
668
|
-
)
|
|
669
|
-
compatibility = _requirement_compatibility(selected, backend)
|
|
670
|
-
if not compatibility["currently_executable"]:
|
|
671
|
-
raise ProviderError(
|
|
672
|
-
"x402-http",
|
|
673
|
-
str(compatibility["reason"]),
|
|
674
|
-
details={
|
|
675
|
-
"selected_payment": selected,
|
|
676
|
-
"compatibility": compatibility,
|
|
677
|
-
},
|
|
678
|
-
)
|
|
679
|
-
return selected
|
|
680
|
-
|
|
681
|
-
|
|
682
713
|
def _validate_payment_requirement(
|
|
683
714
|
selected: dict[str, Any] | None,
|
|
684
715
|
*,
|
|
@@ -717,7 +748,7 @@ def _validate_payment_requirement(
|
|
|
717
748
|
|
|
718
749
|
chain = _backend_chain(backend) or "unknown"
|
|
719
750
|
network = _backend_network(backend) or "unknown"
|
|
720
|
-
requirement_network =
|
|
751
|
+
requirement_network = _caip_network(selected.get("network")) or "unknown"
|
|
721
752
|
if chain == "solana" and requirement_network not in SOLANA_CAIP_BY_NETWORK.values():
|
|
722
753
|
message = (
|
|
723
754
|
f"This endpoint requires payment on {requirement_network}, but the active wallet is Solana ({network})."
|
|
@@ -1091,6 +1122,30 @@ def _log_x402_execute(
|
|
|
1091
1122
|
)
|
|
1092
1123
|
|
|
1093
1124
|
|
|
1125
|
+
def _discovery_cache_get(cache_key: str) -> dict[str, Any] | None:
|
|
1126
|
+
entry = _discovery_cache.get(cache_key)
|
|
1127
|
+
if entry is None:
|
|
1128
|
+
return None
|
|
1129
|
+
expires_at, payload_text = entry
|
|
1130
|
+
if expires_at <= time.monotonic():
|
|
1131
|
+
_discovery_cache.pop(cache_key, None)
|
|
1132
|
+
return None
|
|
1133
|
+
return json.loads(payload_text)
|
|
1134
|
+
|
|
1135
|
+
|
|
1136
|
+
def _discovery_cache_set(cache_key: str, payload: dict[str, Any]) -> None:
|
|
1137
|
+
try:
|
|
1138
|
+
payload_text = json.dumps(payload)
|
|
1139
|
+
except (TypeError, ValueError):
|
|
1140
|
+
return
|
|
1141
|
+
if len(_discovery_cache) >= _DISCOVERY_CACHE_MAX_ENTRIES:
|
|
1142
|
+
for stale_key in sorted(_discovery_cache, key=lambda key: _discovery_cache[key][0])[
|
|
1143
|
+
: len(_discovery_cache) - _DISCOVERY_CACHE_MAX_ENTRIES + 1
|
|
1144
|
+
]:
|
|
1145
|
+
_discovery_cache.pop(stale_key, None)
|
|
1146
|
+
_discovery_cache[cache_key] = (time.monotonic() + DISCOVERY_CACHE_TTL_SECONDS, payload_text)
|
|
1147
|
+
|
|
1148
|
+
|
|
1094
1149
|
async def search_services(
|
|
1095
1150
|
*,
|
|
1096
1151
|
query: str | None = None,
|
|
@@ -1106,20 +1161,62 @@ async def search_services(
|
|
|
1106
1161
|
provider = "cdp_bazaar"
|
|
1107
1162
|
if limit <= 0:
|
|
1108
1163
|
raise ProviderError("x402-discovery", "limit must be greater than zero.")
|
|
1164
|
+
cache_key = _canonical_json_text(
|
|
1165
|
+
{
|
|
1166
|
+
"op": "search_services",
|
|
1167
|
+
"provider": provider,
|
|
1168
|
+
"query": _trim(query),
|
|
1169
|
+
"network": _trim(network),
|
|
1170
|
+
"asset": _trim(asset),
|
|
1171
|
+
"scheme": _trim(scheme),
|
|
1172
|
+
"max_usd_price": _trim(max_usd_price),
|
|
1173
|
+
"limit": limit,
|
|
1174
|
+
}
|
|
1175
|
+
)
|
|
1176
|
+
cached = _discovery_cache_get(cache_key)
|
|
1177
|
+
if cached is not None:
|
|
1178
|
+
return cached
|
|
1179
|
+
result = await _search_services_uncached(
|
|
1180
|
+
query=query,
|
|
1181
|
+
provider=provider,
|
|
1182
|
+
network=network,
|
|
1183
|
+
asset=asset,
|
|
1184
|
+
scheme=scheme,
|
|
1185
|
+
max_usd_price=max_usd_price,
|
|
1186
|
+
limit=limit,
|
|
1187
|
+
)
|
|
1188
|
+
_discovery_cache_set(cache_key, result)
|
|
1189
|
+
return result
|
|
1190
|
+
|
|
1191
|
+
|
|
1192
|
+
async def _search_services_uncached(
|
|
1193
|
+
*,
|
|
1194
|
+
query: str | None,
|
|
1195
|
+
provider: str,
|
|
1196
|
+
network: str | None,
|
|
1197
|
+
asset: str | None,
|
|
1198
|
+
scheme: str | None,
|
|
1199
|
+
max_usd_price: str | None,
|
|
1200
|
+
limit: int,
|
|
1201
|
+
) -> dict[str, Any]:
|
|
1109
1202
|
client = get_client()
|
|
1110
1203
|
|
|
1111
1204
|
if provider == "cdp_bazaar":
|
|
1112
1205
|
if query and _trim(query):
|
|
1206
|
+
# CDP rejects empty filter values with HTTP 400, so omit unset params
|
|
1207
|
+
# entirely instead of sending them as blank strings.
|
|
1208
|
+
search_params: dict[str, Any] = {"query": _trim(query), "limit": min(limit, 20)}
|
|
1209
|
+
for name, value in (
|
|
1210
|
+
("network", network),
|
|
1211
|
+
("asset", asset),
|
|
1212
|
+
("scheme", scheme),
|
|
1213
|
+
("maxUsdPrice", max_usd_price),
|
|
1214
|
+
):
|
|
1215
|
+
if _trim(value):
|
|
1216
|
+
search_params[name] = _trim(value)
|
|
1113
1217
|
response = await client.get(
|
|
1114
1218
|
f"{CDP_BAZAAR_DISCOVERY_BASE_URL}/search",
|
|
1115
|
-
params=
|
|
1116
|
-
"query": _trim(query),
|
|
1117
|
-
"network": _trim(network) or None,
|
|
1118
|
-
"asset": _trim(asset) or None,
|
|
1119
|
-
"scheme": _trim(scheme) or None,
|
|
1120
|
-
"maxUsdPrice": _trim(max_usd_price) or None,
|
|
1121
|
-
"limit": min(limit, 20),
|
|
1122
|
-
},
|
|
1219
|
+
params=search_params,
|
|
1123
1220
|
)
|
|
1124
1221
|
payload = _parse_json_response(
|
|
1125
1222
|
response, provider="x402-cdp-bazaar", operation="CDP Bazaar search"
|
|
@@ -1214,19 +1311,40 @@ async def get_service_details(
|
|
|
1214
1311
|
)
|
|
1215
1312
|
|
|
1216
1313
|
if provider == "cdp_bazaar":
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1314
|
+
|
|
1315
|
+
def _match(items: list[dict[str, Any]]) -> dict[str, Any] | None:
|
|
1316
|
+
exact = next((item for item in items if item.get("resource") == ref), None)
|
|
1317
|
+
if exact is not None:
|
|
1318
|
+
return exact
|
|
1220
1319
|
needle = ref.lower()
|
|
1221
|
-
|
|
1320
|
+
return next(
|
|
1222
1321
|
(
|
|
1223
1322
|
item
|
|
1224
|
-
for item in
|
|
1323
|
+
for item in items
|
|
1225
1324
|
if needle in _trim(item.get("resource")).lower()
|
|
1226
1325
|
or needle in _trim(item.get("description")).lower()
|
|
1227
1326
|
),
|
|
1228
1327
|
None,
|
|
1229
1328
|
)
|
|
1329
|
+
|
|
1330
|
+
# Server-side search covers the full Bazaar catalog; a plain resource
|
|
1331
|
+
# listing only ever returns the first page, so use it as a last resort.
|
|
1332
|
+
queries = [ref]
|
|
1333
|
+
if ref.startswith(("http://", "https://")):
|
|
1334
|
+
netloc = _trim(urlsplit(ref).netloc)
|
|
1335
|
+
if netloc and netloc != ref:
|
|
1336
|
+
queries.append(netloc)
|
|
1337
|
+
exact: dict[str, Any] | None = None
|
|
1338
|
+
for candidate_query in queries:
|
|
1339
|
+
results = await search_services(
|
|
1340
|
+
query=candidate_query, discovery_provider="cdp_bazaar", limit=20
|
|
1341
|
+
)
|
|
1342
|
+
exact = _match(results["items"])
|
|
1343
|
+
if exact is not None:
|
|
1344
|
+
break
|
|
1345
|
+
if exact is None:
|
|
1346
|
+
resources = await search_services(discovery_provider="cdp_bazaar", limit=200)
|
|
1347
|
+
exact = _match(resources["items"])
|
|
1230
1348
|
if exact is None:
|
|
1231
1349
|
raise ProviderError("x402-cdp-bazaar", f"No Bazaar resource matched: {ref}")
|
|
1232
1350
|
return {"discovery_provider": provider, "service": exact}
|
|
@@ -1330,78 +1448,30 @@ async def preview_request(
|
|
|
1330
1448
|
return payment_preview
|
|
1331
1449
|
|
|
1332
1450
|
|
|
1333
|
-
|
|
1451
|
+
def _reusable_approved_preview(
|
|
1452
|
+
approved_preview: Any,
|
|
1334
1453
|
*,
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
prepared["prepare_note"] = "The endpoint did not require x402 payment for this request."
|
|
1357
|
-
return prepared
|
|
1358
|
-
|
|
1359
|
-
selected_payment = _require_executable_payment(preview=preview, backend=backend)
|
|
1360
|
-
payment_required_header = (
|
|
1361
|
-
dict(preview.get("response_headers") or {}).get("payment-required")
|
|
1362
|
-
)
|
|
1363
|
-
if not isinstance(payment_required_header, str) or not payment_required_header.strip():
|
|
1364
|
-
raise ProviderError("x402-http", "Missing PAYMENT-REQUIRED header in preview state.")
|
|
1365
|
-
# Create the payload once during prepare to validate that the active wallet can sign it.
|
|
1366
|
-
await _create_payment_headers(
|
|
1367
|
-
backend=backend,
|
|
1368
|
-
payment_required_header=payment_required_header,
|
|
1369
|
-
selected_payment=selected_payment,
|
|
1370
|
-
)
|
|
1371
|
-
prepared = dict(preview)
|
|
1372
|
-
prepared["mode"] = "prepare"
|
|
1373
|
-
prepared["prepared"] = True
|
|
1374
|
-
prepared["signed"] = False
|
|
1375
|
-
prepared["broadcasted"] = False
|
|
1376
|
-
prepared["confirmed"] = False
|
|
1377
|
-
prepared["payment_payload_withheld"] = True
|
|
1378
|
-
prepared["prepare_note"] = (
|
|
1379
|
-
"x402 payment authorization was validated locally, but the PAYMENT-SIGNATURE header is withheld until execute."
|
|
1380
|
-
)
|
|
1381
|
-
return prepared
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
async def execute_request(
|
|
1385
|
-
*,
|
|
1386
|
-
backend: AgentWalletBackend,
|
|
1387
|
-
url: str,
|
|
1388
|
-
method: str = "GET",
|
|
1389
|
-
headers: dict[str, Any] | None = None,
|
|
1390
|
-
query: dict[str, Any] | None = None,
|
|
1391
|
-
json_body: Any | None = None,
|
|
1392
|
-
text_body: str | None = None,
|
|
1393
|
-
) -> dict[str, Any]:
|
|
1394
|
-
executed = await pay_and_fetch(
|
|
1395
|
-
backend=backend,
|
|
1396
|
-
url=url,
|
|
1397
|
-
method=method,
|
|
1398
|
-
headers=headers,
|
|
1399
|
-
query=query,
|
|
1400
|
-
json_body=json_body,
|
|
1401
|
-
text_body=text_body,
|
|
1402
|
-
)
|
|
1403
|
-
executed["mode"] = "execute"
|
|
1404
|
-
return executed
|
|
1454
|
+
request: dict[str, Any],
|
|
1455
|
+
) -> dict[str, Any] | None:
|
|
1456
|
+
"""Return an approval-time preview usable instead of a fresh unpaid probe.
|
|
1457
|
+
|
|
1458
|
+
Reuse is only safe when the preview describes exactly this request
|
|
1459
|
+
(fingerprint covers method, final URL, and body hash) and still carries the
|
|
1460
|
+
encoded PAYMENT-REQUIRED challenge plus a selected payment. Anything else
|
|
1461
|
+
falls back to a fresh probe.
|
|
1462
|
+
"""
|
|
1463
|
+
if not isinstance(approved_preview, dict):
|
|
1464
|
+
return None
|
|
1465
|
+
if not approved_preview.get("payment_required"):
|
|
1466
|
+
return None
|
|
1467
|
+
if _trim(approved_preview.get("request_fingerprint")) != request["request_fingerprint"]:
|
|
1468
|
+
return None
|
|
1469
|
+
if not isinstance(approved_preview.get("selected_payment"), dict):
|
|
1470
|
+
return None
|
|
1471
|
+
header = dict(approved_preview.get("response_headers") or {}).get("payment-required")
|
|
1472
|
+
if not isinstance(header, str) or not header.strip():
|
|
1473
|
+
return None
|
|
1474
|
+
return dict(approved_preview)
|
|
1405
1475
|
|
|
1406
1476
|
|
|
1407
1477
|
async def pay_and_fetch(
|
|
@@ -1413,9 +1483,9 @@ async def pay_and_fetch(
|
|
|
1413
1483
|
query: dict[str, Any] | None = None,
|
|
1414
1484
|
json_body: Any | None = None,
|
|
1415
1485
|
text_body: str | None = None,
|
|
1486
|
+
approved_preview: dict[str, Any] | None = None,
|
|
1416
1487
|
) -> dict[str, Any]:
|
|
1417
|
-
|
|
1418
|
-
backend=backend,
|
|
1488
|
+
request = _build_request_metadata(
|
|
1419
1489
|
url=url,
|
|
1420
1490
|
method=method,
|
|
1421
1491
|
headers=headers,
|
|
@@ -1423,6 +1493,18 @@ async def pay_and_fetch(
|
|
|
1423
1493
|
json_body=json_body,
|
|
1424
1494
|
text_body=text_body,
|
|
1425
1495
|
)
|
|
1496
|
+
preview = _reusable_approved_preview(approved_preview, request=request)
|
|
1497
|
+
reused_approved_preview = preview is not None
|
|
1498
|
+
if preview is None:
|
|
1499
|
+
preview = await preview_request(
|
|
1500
|
+
backend=backend,
|
|
1501
|
+
url=url,
|
|
1502
|
+
method=method,
|
|
1503
|
+
headers=headers,
|
|
1504
|
+
query=query,
|
|
1505
|
+
json_body=json_body,
|
|
1506
|
+
text_body=text_body,
|
|
1507
|
+
)
|
|
1426
1508
|
if not preview.get("payment_required"):
|
|
1427
1509
|
executed = dict(preview)
|
|
1428
1510
|
executed["mode"] = "execute"
|
|
@@ -1444,14 +1526,6 @@ async def pay_and_fetch(
|
|
|
1444
1526
|
if not isinstance(payment_required_header, str) or not payment_required_header.strip():
|
|
1445
1527
|
raise ProviderError("x402-http", "Missing PAYMENT-REQUIRED header in preview state.")
|
|
1446
1528
|
|
|
1447
|
-
request = _build_request_metadata(
|
|
1448
|
-
url=url,
|
|
1449
|
-
method=method,
|
|
1450
|
-
headers=headers,
|
|
1451
|
-
query=query,
|
|
1452
|
-
json_body=json_body,
|
|
1453
|
-
text_body=text_body,
|
|
1454
|
-
)
|
|
1455
1529
|
_validate_request_execution_policy(request=request, backend=backend)
|
|
1456
1530
|
payment_headers = await _create_payment_headers(
|
|
1457
1531
|
backend=backend,
|
|
@@ -1478,6 +1552,7 @@ async def pay_and_fetch(
|
|
|
1478
1552
|
{
|
|
1479
1553
|
"mode": "execute",
|
|
1480
1554
|
"paid": True,
|
|
1555
|
+
"reused_approved_preview": reused_approved_preview,
|
|
1481
1556
|
"broadcasted": bool(settlement and settlement.get("transaction")),
|
|
1482
1557
|
"confirmed": bool(settlement and settlement.get("success")),
|
|
1483
1558
|
"payment_settlement": settlement,
|
|
@@ -1491,9 +1566,15 @@ async def pay_and_fetch(
|
|
|
1491
1566
|
}
|
|
1492
1567
|
)
|
|
1493
1568
|
if response.status_code == 402:
|
|
1569
|
+
message = "The paid x402 retry still returned HTTP 402."
|
|
1570
|
+
if reused_approved_preview:
|
|
1571
|
+
message += (
|
|
1572
|
+
" The payment was signed from the approved preview quote; the server may have"
|
|
1573
|
+
" re-priced the endpoint since then. Run x402_preview_request again and retry."
|
|
1574
|
+
)
|
|
1494
1575
|
raise ProviderError(
|
|
1495
1576
|
"x402-http",
|
|
1496
|
-
|
|
1577
|
+
message,
|
|
1497
1578
|
details={
|
|
1498
1579
|
"request_url": request["url"],
|
|
1499
1580
|
"selected_payment": selected_payment,
|
|
@@ -11,6 +11,16 @@ from agent_wallet.wallet_layer.base import WalletBackendError
|
|
|
11
11
|
|
|
12
12
|
SEALED_KEYS_FILENAME = "sealed_keys.json"
|
|
13
13
|
|
|
14
|
+
# Single-entry cache: (boot_key, path, mtime_ns, size) -> secrets. Unsealing
|
|
15
|
+
# runs a KDF, so repeated resolutions in one process must pay it only once.
|
|
16
|
+
# File identity in the key makes rotation (re-seal) self-invalidating.
|
|
17
|
+
_unseal_cache: dict[tuple[str, str, int, int], dict[str, str]] = {}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def clear_unseal_cache() -> None:
|
|
21
|
+
"""Drop cached unsealed secrets (wired into config.clear_secret_caches)."""
|
|
22
|
+
_unseal_cache.clear()
|
|
23
|
+
|
|
14
24
|
|
|
15
25
|
def resolve_sealed_keys_path() -> Path:
|
|
16
26
|
"""Resolve the encrypted secret bundle path under the OpenClaw home directory."""
|
|
@@ -35,11 +45,12 @@ def seal_keys(boot_key: str, secrets: dict[str, str]) -> Path:
|
|
|
35
45
|
encrypted = encrypt_secret_material(payload, master_key=boot_key)
|
|
36
46
|
path = resolve_sealed_keys_path()
|
|
37
47
|
atomic_write_text(path, encrypted, mode=0o600)
|
|
48
|
+
clear_unseal_cache()
|
|
38
49
|
return path
|
|
39
50
|
|
|
40
51
|
|
|
41
52
|
def unseal_keys(boot_key: str) -> dict[str, str]:
|
|
42
|
-
"""Decrypt all secrets from the sealed file."""
|
|
53
|
+
"""Decrypt all secrets from the sealed file. Memoized by file identity."""
|
|
43
54
|
if not boot_key.strip():
|
|
44
55
|
return {}
|
|
45
56
|
|
|
@@ -47,7 +58,17 @@ def unseal_keys(boot_key: str) -> dict[str, str]:
|
|
|
47
58
|
if not path.exists():
|
|
48
59
|
return {}
|
|
49
60
|
|
|
50
|
-
|
|
61
|
+
try:
|
|
62
|
+
stat = path.stat()
|
|
63
|
+
except OSError:
|
|
64
|
+
return {}
|
|
65
|
+
cache_key = (boot_key, str(path), stat.st_mtime_ns, stat.st_size)
|
|
66
|
+
cached = _unseal_cache.get(cache_key)
|
|
67
|
+
if cached is not None:
|
|
68
|
+
return dict(cached)
|
|
69
|
+
|
|
70
|
+
raw_text = path.read_text(encoding="utf-8")
|
|
71
|
+
plaintext = decrypt_secret_material(raw_text, master_key=boot_key)
|
|
51
72
|
try:
|
|
52
73
|
payload = json.loads(plaintext)
|
|
53
74
|
except json.JSONDecodeError as exc:
|
|
@@ -58,4 +79,39 @@ def unseal_keys(boot_key: str) -> dict[str, str]:
|
|
|
58
79
|
for key, value in payload.items():
|
|
59
80
|
if isinstance(key, str) and isinstance(value, str):
|
|
60
81
|
secrets[key] = value
|
|
82
|
+
_maybe_migrate_envelope_kdf(boot_key, secrets, raw_text)
|
|
83
|
+
_unseal_cache.clear()
|
|
84
|
+
_unseal_cache[cache_key] = dict(secrets)
|
|
61
85
|
return secrets
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _maybe_migrate_envelope_kdf(boot_key: str, secrets: dict[str, str], raw_text: str) -> None:
|
|
89
|
+
"""Lazily re-seal an argon2id file as hkdf-sha256 after a successful unseal.
|
|
90
|
+
|
|
91
|
+
Best-effort: any failure leaves the (still readable) argon2id file in
|
|
92
|
+
place. The boot key is machine-generated high entropy, so hkdf loses no
|
|
93
|
+
security while dropping ~1s of KDF per cold process. Rollback caveat:
|
|
94
|
+
pre-hkdf runtimes cannot read the rewritten file — kill switch is
|
|
95
|
+
AGENT_WALLET_ENVELOPE_KDF_MIGRATION=0.
|
|
96
|
+
"""
|
|
97
|
+
from agent_wallet.config import envelope_kdf_migration_enabled
|
|
98
|
+
from agent_wallet.encrypted_storage import (
|
|
99
|
+
KDF_ARGON2ID,
|
|
100
|
+
KDF_HKDF_SHA256,
|
|
101
|
+
_default_write_kdf,
|
|
102
|
+
envelope_kdf,
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
try:
|
|
106
|
+
if not envelope_kdf_migration_enabled():
|
|
107
|
+
return
|
|
108
|
+
if _default_write_kdf() == KDF_ARGON2ID:
|
|
109
|
+
return # forced-argon2id installs must not rewrite in place forever
|
|
110
|
+
if envelope_kdf(raw_text) != KDF_ARGON2ID:
|
|
111
|
+
return
|
|
112
|
+
payload = json.dumps(secrets, indent=2)
|
|
113
|
+
encrypted = encrypt_secret_material(payload, master_key=boot_key, kdf=KDF_HKDF_SHA256)
|
|
114
|
+
atomic_write_text(resolve_sealed_keys_path(), encrypted, mode=0o600)
|
|
115
|
+
clear_unseal_cache()
|
|
116
|
+
except Exception:
|
|
117
|
+
pass
|