@onecraft/wallet 0.0.0-stage → 0.1.145
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/.claude-plugin/marketplace.json +20 -0
- package/.openclaw/AGENTS.md +100 -0
- package/.openclaw/extensions/agent-wallet/README.md +195 -0
- package/.openclaw/extensions/agent-wallet/dist/index.js +2198 -0
- package/.openclaw/extensions/agent-wallet/index.ts +2198 -0
- package/.openclaw/extensions/agent-wallet/openclaw.plugin.json +251 -0
- package/.openclaw/extensions/agent-wallet/package.json +49 -0
- package/.openclaw/extensions/agent-wallet/skills/cards/SKILL.md +211 -0
- package/.openclaw/extensions/agent-wallet/skills/wallet-operator/SKILL.md +30 -0
- package/CHANGELOG.md +1232 -0
- package/LICENSE +104 -0
- package/README.md +193 -2
- package/VERSION +1 -0
- package/agent-wallet/.env.example +63 -0
- package/agent-wallet/AGENTS.md +130 -0
- package/agent-wallet/CARD_VAULT.md +640 -0
- package/agent-wallet/README.md +615 -0
- package/agent-wallet/UPGRADE_COMPATIBILITY.md +66 -0
- package/agent-wallet/agent_wallet/__init__.py +16 -0
- package/agent-wallet/agent_wallet/approval.py +165 -0
- package/agent-wallet/agent_wallet/autonomous_permissions.py +281 -0
- package/agent-wallet/agent_wallet/autonomous_policy.py +305 -0
- package/agent-wallet/agent_wallet/autonomous_session.py +236 -0
- package/agent-wallet/agent_wallet/boot_key_migration.py +155 -0
- package/agent-wallet/agent_wallet/boot_key_recovery.py +57 -0
- package/agent-wallet/agent_wallet/bootstrap.py +194 -0
- package/agent-wallet/agent_wallet/card_catalog.py +243 -0
- package/agent-wallet/agent_wallet/card_runtime_secrets.py +108 -0
- package/agent-wallet/agent_wallet/card_vault.py +331 -0
- package/agent-wallet/agent_wallet/card_vault_cli.py +174 -0
- package/agent-wallet/agent_wallet/card_vault_import.py +65 -0
- package/agent-wallet/agent_wallet/card_vault_keychain.py +239 -0
- package/agent-wallet/agent_wallet/config.py +719 -0
- package/agent-wallet/agent_wallet/connector_cli.py +198 -0
- package/agent-wallet/agent_wallet/connectors/__init__.py +28 -0
- package/agent-wallet/agent_wallet/connectors/catalog.py +68 -0
- package/agent-wallet/agent_wallet/connectors/client.py +308 -0
- package/agent-wallet/agent_wallet/connectors/intent_policy.py +243 -0
- package/agent-wallet/agent_wallet/connectors/manifest.py +192 -0
- package/agent-wallet/agent_wallet/connectors/registry.py +341 -0
- package/agent-wallet/agent_wallet/encrypted_storage.py +314 -0
- package/agent-wallet/agent_wallet/evm_user_wallets.py +910 -0
- package/agent-wallet/agent_wallet/exceptions.py +10 -0
- package/agent-wallet/agent_wallet/file_ops.py +34 -0
- package/agent-wallet/agent_wallet/http_client.py +25 -0
- package/agent-wallet/agent_wallet/kernel_browser.py +479 -0
- package/agent-wallet/agent_wallet/kernel_cli.py +57 -0
- package/agent-wallet/agent_wallet/kernel_gateway.py +95 -0
- package/agent-wallet/agent_wallet/keystore.py +503 -0
- package/agent-wallet/agent_wallet/laso_card_vault.py +246 -0
- package/agent-wallet/agent_wallet/models.py +66 -0
- package/agent-wallet/agent_wallet/morpho_monitor.py +298 -0
- package/agent-wallet/agent_wallet/nonce_registry.py +59 -0
- package/agent-wallet/agent_wallet/openclaw_adapter.py +9129 -0
- package/agent-wallet/agent_wallet/openclaw_cli.py +658 -0
- package/agent-wallet/agent_wallet/openclaw_runtime.py +194 -0
- package/agent-wallet/agent_wallet/plugin_bundle.py +44 -0
- package/agent-wallet/agent_wallet/providers/__init__.py +1 -0
- package/agent-wallet/agent_wallet/providers/bags.py +202 -0
- package/agent-wallet/agent_wallet/providers/dexscreener.py +147 -0
- package/agent-wallet/agent_wallet/providers/evm_activity.py +257 -0
- package/agent-wallet/agent_wallet/providers/evm_portfolio.py +459 -0
- package/agent-wallet/agent_wallet/providers/imd.py +720 -0
- package/agent-wallet/agent_wallet/providers/jupiter.py +519 -0
- package/agent-wallet/agent_wallet/providers/kamino.py +404 -0
- package/agent-wallet/agent_wallet/providers/lifi.py +302 -0
- package/agent-wallet/agent_wallet/providers/solana_rpc.py +447 -0
- package/agent-wallet/agent_wallet/providers/tokenized_stocks.py +189 -0
- package/agent-wallet/agent_wallet/providers/wdk_evm_local.py +346 -0
- package/agent-wallet/agent_wallet/providers/x402.py +1972 -0
- package/agent-wallet/agent_wallet/sealed_keys.py +118 -0
- package/agent-wallet/agent_wallet/solana_stake.py +103 -0
- package/agent-wallet/agent_wallet/solana_tx.py +93 -0
- package/agent-wallet/agent_wallet/spending_limits.py +119 -0
- package/agent-wallet/agent_wallet/telemetry.py +481 -0
- package/agent-wallet/agent_wallet/transaction_policy.py +573 -0
- package/agent-wallet/agent_wallet/update_check.py +190 -0
- package/agent-wallet/agent_wallet/user_wallets.py +573 -0
- package/agent-wallet/agent_wallet/validation.py +31 -0
- package/agent-wallet/agent_wallet/vault_executor.py +456 -0
- package/agent-wallet/agent_wallet/wallet_layer/__init__.py +1 -0
- package/agent-wallet/agent_wallet/wallet_layer/base.py +1204 -0
- package/agent-wallet/agent_wallet/wallet_layer/base58.py +44 -0
- package/agent-wallet/agent_wallet/wallet_layer/factory.py +117 -0
- package/agent-wallet/agent_wallet/wallet_layer/solana.py +5246 -0
- package/agent-wallet/agent_wallet/wallet_layer/wdk_evm.py +2705 -0
- package/agent-wallet/examples/bootstrap_wallet.py +21 -0
- package/agent-wallet/examples/openclaw_runtime_onboarding.py +28 -0
- package/agent-wallet/examples/openclaw_user_wallet_example.py +31 -0
- package/agent-wallet/examples/openclaw_wallet_adapter_example.py +33 -0
- package/agent-wallet/openclaw.plugin.json +118 -0
- package/agent-wallet/pyproject.toml +36 -0
- package/agent-wallet/scripts/bootstrap_openclaw_evm.py +312 -0
- package/agent-wallet/scripts/build_release_bundle.py +210 -0
- package/agent-wallet/scripts/finalize_openclaw_local_wallet_config.py +121 -0
- package/agent-wallet/scripts/install_agent_wallet.py +1357 -0
- package/agent-wallet/scripts/install_openclaw_local_config.py +421 -0
- package/agent-wallet/scripts/install_openclaw_sealed_keys.py +117 -0
- package/agent-wallet/scripts/manage_openclaw_evm_wallet.py +333 -0
- package/agent-wallet/scripts/security_utils.py +37 -0
- package/agent-wallet/scripts/setup_evm_wallet.sh +140 -0
- package/agent-wallet/scripts/switch_openclaw_wallet_network.py +109 -0
- package/agent-wallet/skills/wallet-operator/SKILL.md +157 -0
- package/bin/lib/boot-key.mjs +184 -0
- package/bin/lib/evm-daemon.mjs +389 -0
- package/bin/lib/host-detection.mjs +259 -0
- package/bin/lib/integrations.mjs +578 -0
- package/bin/lib/release-python.mjs +9 -0
- package/bin/lib/update-transaction.mjs +235 -0
- package/bin/openclaw-agent-wallet.mjs +3354 -0
- package/claude-code/plugins/agent-wallet/.claude-plugin/plugin.json +19 -0
- package/claude-code/plugins/agent-wallet/.mcp.json +14 -0
- package/claude-code/plugins/agent-wallet/AGENTLAYER_AGENT_GUIDE.md +282 -0
- package/claude-code/plugins/agent-wallet/README.md +134 -0
- package/claude-code/plugins/agent-wallet/commands/agentlayer-autonomous-approve.md +39 -0
- package/claude-code/plugins/agent-wallet/commands/agentlayer-autonomous-revoke.md +17 -0
- package/claude-code/plugins/agent-wallet/commands/cards.md +129 -0
- package/claude-code/plugins/agent-wallet/commands/guide.md +40 -0
- package/claude-code/plugins/agent-wallet/commands/imd.md +125 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-base.md +60 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-ethereum.md +37 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-evm.md +39 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-setup.md +25 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-sol.md +51 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-statusline.md +34 -0
- package/claude-code/plugins/agent-wallet/commands/x402.md +121 -0
- package/claude-code/plugins/agent-wallet/commands/yield.md +62 -0
- package/claude-code/plugins/agent-wallet/hooks/hooks.json +16 -0
- package/claude-code/plugins/agent-wallet/scripts/bootstrap_backend.sh +136 -0
- package/claude-code/plugins/agent-wallet/scripts/enable_statusline.py +52 -0
- package/claude-code/plugins/agent-wallet/scripts/resolve_wallet_addresses.py +69 -0
- package/claude-code/plugins/agent-wallet/scripts/run_mcp.sh +66 -0
- package/claude-code/plugins/agent-wallet/scripts/wallet_statusline.py +130 -0
- package/claude-code/plugins/agent-wallet/scripts/wallet_statusline_setup.sh +40 -0
- package/claude-code/plugins/agent-wallet/skills/wallet-operator/SKILL.md +21 -0
- package/codex/plugins/agent-wallet/.codex-plugin/plugin.json +37 -0
- package/codex/plugins/agent-wallet/.mcp.json +15 -0
- package/codex/plugins/agent-wallet/README.md +158 -0
- package/codex/plugins/agent-wallet/scripts/run_mcp.sh +47 -0
- package/codex/plugins/agent-wallet/server.py +1797 -0
- package/codex/plugins/agent-wallet/skills/cards/SKILL.md +225 -0
- package/codex/plugins/agent-wallet/skills/imd/SKILL.md +145 -0
- package/codex/plugins/agent-wallet/skills/wallet-base/SKILL.md +56 -0
- package/codex/plugins/agent-wallet/skills/wallet-operator/SKILL.md +85 -0
- package/codex/plugins/agent-wallet/skills/wallet-sol/SKILL.md +29 -0
- package/codex/plugins/agent-wallet/skills/x402/SKILL.md +159 -0
- package/codex/plugins/agent-wallet/skills/yield/SKILL.md +65 -0
- package/connectors/AGENTS.md +37 -0
- package/connectors/DEVELOPER_GUIDE.md +131 -0
- package/connectors/README.md +139 -0
- package/connectors/READ_ONLY_BETA.md +54 -0
- package/connectors/RELEASING.md +48 -0
- package/connectors/conformance/README.md +31 -0
- package/connectors/conformance/package.json +54 -0
- package/connectors/conformance/scripts/sync-license.mjs +3 -0
- package/connectors/conformance/scripts/sync-spec.mjs +16 -0
- package/connectors/conformance/src/cli.ts +43 -0
- package/connectors/conformance/src/index.ts +7 -0
- package/connectors/conformance/src/runner.ts +314 -0
- package/connectors/conformance/src/types.ts +27 -0
- package/connectors/conformance/test/fixtures/invalid-response-identity.json +14 -0
- package/connectors/conformance/test/fixtures/missing-tool-fixture.json +4 -0
- package/connectors/conformance/test/runner.test.mjs +151 -0
- package/connectors/conformance/tsconfig.json +17 -0
- package/connectors/examples/reference-crypto-data/Dockerfile +21 -0
- package/connectors/examples/reference-crypto-data/README.md +51 -0
- package/connectors/examples/reference-crypto-data/conformance.json +11 -0
- package/connectors/examples/reference-crypto-data/connector.json +95 -0
- package/connectors/examples/reference-crypto-data/package.json +21 -0
- package/connectors/examples/reference-crypto-data/railway.toml +9 -0
- package/connectors/examples/reference-crypto-data/src/connector.ts +137 -0
- package/connectors/examples/reference-crypto-data/src/server.ts +13 -0
- package/connectors/examples/reference-crypto-data/test/connector.test.mjs +97 -0
- package/connectors/examples/reference-crypto-data/tsconfig.json +17 -0
- package/connectors/package-lock.json +561 -0
- package/connectors/package.json +19 -0
- package/connectors/scripts/smoke-packed-packages.mjs +121 -0
- package/connectors/sdk-typescript/README.md +47 -0
- package/connectors/sdk-typescript/package.json +49 -0
- package/connectors/sdk-typescript/scripts/sync-license.mjs +3 -0
- package/connectors/sdk-typescript/src/connector.ts +204 -0
- package/connectors/sdk-typescript/src/errors.ts +11 -0
- package/connectors/sdk-typescript/src/http.ts +116 -0
- package/connectors/sdk-typescript/src/index.ts +26 -0
- package/connectors/sdk-typescript/src/types.ts +101 -0
- package/connectors/sdk-typescript/test/sdk.test.mjs +181 -0
- package/connectors/sdk-typescript/tsconfig.json +21 -0
- package/connectors/spec/connector-manifest.schema.json +340 -0
- package/connectors/spec/connector-protocol.md +159 -0
- package/connectors/spec/transaction-intent.schema.json +281 -0
- package/connectors/templates/read-only/.env.example +1 -0
- package/connectors/templates/read-only/Dockerfile +11 -0
- package/connectors/templates/read-only/README.md +33 -0
- package/connectors/templates/read-only/conformance.json +11 -0
- package/connectors/templates/read-only/connector.json +64 -0
- package/connectors/templates/read-only/package.json +21 -0
- package/connectors/templates/read-only/railway.toml +9 -0
- package/connectors/templates/read-only/src/connector.ts +61 -0
- package/connectors/templates/read-only/src/server.ts +14 -0
- package/connectors/templates/read-only/test/connector.test.mjs +62 -0
- package/connectors/templates/read-only/tsconfig.json +17 -0
- package/cursor/plugins/agent-wallet/.cursor-plugin/plugin.json +14 -0
- package/cursor/plugins/agent-wallet/README.md +62 -0
- package/cursor/plugins/agent-wallet/mcp.json +16 -0
- package/cursor/plugins/agent-wallet/scripts/run_mcp.sh +33 -0
- package/cursor/plugins/agent-wallet/server.py +33 -0
- package/cursor/plugins/agent-wallet/skills/cards/SKILL.md +210 -0
- package/cursor/plugins/agent-wallet/skills/wallet-base/SKILL.md +56 -0
- package/cursor/plugins/agent-wallet/skills/wallet-operator/SKILL.md +23 -0
- package/cursor/plugins/agent-wallet/skills/wallet-sol/SKILL.md +29 -0
- package/cursor/plugins/agent-wallet/skills/x402/SKILL.md +130 -0
- package/hermes/plugins/agent_wallet/README.md +54 -0
- package/hermes/plugins/agent_wallet/__init__.py +55 -0
- package/hermes/plugins/agent_wallet/plugin.yaml +9 -0
- package/hermes/plugins/agent_wallet/schemas.py +211 -0
- package/hermes/plugins/agent_wallet/tools.py +622 -0
- package/install-from-github.sh +139 -0
- package/integrations/hedera/treasury-guard/README.md +92 -0
- package/integrations/hedera/treasury-guard/examples/treasury-policy.example.json +9 -0
- package/integrations/hedera/treasury-guard/package-lock.json +880 -0
- package/integrations/hedera/treasury-guard/package.json +8 -0
- package/integrations/hedera/treasury-guard/skills/onecraft-hedera-treasury/SKILL.md +22 -0
- package/integrations/hedera/treasury-guard/src/hedera-mirror-client.mjs +106 -0
- package/integrations/hedera/treasury-guard/src/hedera-signer.mjs +120 -0
- package/integrations/hedera/treasury-guard/src/treasury-cli.mjs +177 -0
- package/integrations/hedera/treasury-guard/src/treasury-policy.mjs +155 -0
- package/mcp/README.md +39 -0
- package/mcp/scripts/run_mcp.sh +37 -0
- package/mcp/server.py +33 -0
- package/package.json +106 -4
- package/scripts/check_release_version.mjs +72 -0
- package/scripts/version_targets.mjs +60 -0
- package/setup.sh +87 -0
- package/wdk-evm-wallet/.env.example +38 -0
- package/wdk-evm-wallet/README.md +254 -0
- package/wdk-evm-wallet/bootstrap.sh +8 -0
- package/wdk-evm-wallet/package-lock.json +2359 -0
- package/wdk-evm-wallet/package.json +43 -0
- package/wdk-evm-wallet/run-local.sh +12 -0
- package/wdk-evm-wallet/src/config.js +461 -0
- package/wdk-evm-wallet/src/imd.js +231 -0
- package/wdk-evm-wallet/src/json.js +35 -0
- package/wdk-evm-wallet/src/local_vault.js +407 -0
- package/wdk-evm-wallet/src/network_state.js +96 -0
- package/wdk-evm-wallet/src/pending_transactions.js +148 -0
- package/wdk-evm-wallet/src/server.js +849 -0
- package/wdk-evm-wallet/src/shutdown.js +63 -0
- package/wdk-evm-wallet/src/wdk_evm_wallet.js +10036 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"""Sealed key storage backed by one boot key and an encrypted file on disk."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from agent_wallet.encrypted_storage import (
|
|
9
|
+
decrypt_secret_material,
|
|
10
|
+
encrypt_secret_material,
|
|
11
|
+
remove_envelope_migration_backup,
|
|
12
|
+
)
|
|
13
|
+
from agent_wallet.file_ops import atomic_write_text
|
|
14
|
+
from agent_wallet.wallet_layer.base import WalletBackendError
|
|
15
|
+
|
|
16
|
+
SEALED_KEYS_FILENAME = "sealed_keys.json"
|
|
17
|
+
|
|
18
|
+
# Single-entry cache: (boot_key, path, mtime_ns, size) -> secrets. Unsealing
|
|
19
|
+
# runs a KDF, so repeated resolutions in one process must pay it only once.
|
|
20
|
+
# File identity in the key makes rotation (re-seal) self-invalidating.
|
|
21
|
+
_unseal_cache: dict[tuple[str, str, int, int], dict[str, str]] = {}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def clear_unseal_cache() -> None:
|
|
25
|
+
"""Drop cached unsealed secrets (wired into config.clear_secret_caches)."""
|
|
26
|
+
_unseal_cache.clear()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def resolve_sealed_keys_path() -> Path:
|
|
30
|
+
"""Resolve the encrypted secret bundle path under the OpenClaw home directory."""
|
|
31
|
+
from agent_wallet.config import resolve_openclaw_home
|
|
32
|
+
|
|
33
|
+
return resolve_openclaw_home() / SEALED_KEYS_FILENAME
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def seal_keys(boot_key: str, secrets: dict[str, str]) -> Path:
|
|
37
|
+
"""Encrypt all secrets into a single sealed file."""
|
|
38
|
+
if not boot_key.strip():
|
|
39
|
+
raise WalletBackendError("AGENT_WALLET_BOOT_KEY is required to seal secrets.")
|
|
40
|
+
normalized: dict[str, str] = {}
|
|
41
|
+
for key, value in secrets.items():
|
|
42
|
+
if not isinstance(key, str) or not key.strip():
|
|
43
|
+
raise WalletBackendError("Sealed secret names must be non-empty strings.")
|
|
44
|
+
if not isinstance(value, str):
|
|
45
|
+
raise WalletBackendError(f"Sealed secret '{key}' must be a string.")
|
|
46
|
+
normalized[key.strip()] = value
|
|
47
|
+
|
|
48
|
+
payload = json.dumps(normalized, indent=2)
|
|
49
|
+
encrypted = encrypt_secret_material(payload, master_key=boot_key)
|
|
50
|
+
path = resolve_sealed_keys_path()
|
|
51
|
+
atomic_write_text(path, encrypted, mode=0o600)
|
|
52
|
+
remove_envelope_migration_backup(path)
|
|
53
|
+
clear_unseal_cache()
|
|
54
|
+
return path
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def unseal_keys(boot_key: str) -> dict[str, str]:
|
|
58
|
+
"""Decrypt all secrets from the sealed file. Memoized by file identity."""
|
|
59
|
+
if not boot_key.strip():
|
|
60
|
+
return {}
|
|
61
|
+
|
|
62
|
+
path = resolve_sealed_keys_path()
|
|
63
|
+
if not path.exists():
|
|
64
|
+
return {}
|
|
65
|
+
|
|
66
|
+
try:
|
|
67
|
+
stat = path.stat()
|
|
68
|
+
except OSError:
|
|
69
|
+
return {}
|
|
70
|
+
cache_key = (boot_key, str(path), stat.st_mtime_ns, stat.st_size)
|
|
71
|
+
cached = _unseal_cache.get(cache_key)
|
|
72
|
+
if cached is not None:
|
|
73
|
+
return dict(cached)
|
|
74
|
+
|
|
75
|
+
raw_text = path.read_text(encoding="utf-8")
|
|
76
|
+
plaintext = decrypt_secret_material(raw_text, master_key=boot_key)
|
|
77
|
+
try:
|
|
78
|
+
payload = json.loads(plaintext)
|
|
79
|
+
except json.JSONDecodeError as exc:
|
|
80
|
+
raise WalletBackendError("Sealed secret file is malformed.") from exc
|
|
81
|
+
if not isinstance(payload, dict):
|
|
82
|
+
raise WalletBackendError("Sealed secret file is malformed.")
|
|
83
|
+
secrets: dict[str, str] = {}
|
|
84
|
+
for key, value in payload.items():
|
|
85
|
+
if isinstance(key, str) and isinstance(value, str):
|
|
86
|
+
secrets[key] = value
|
|
87
|
+
_maybe_migrate_envelope_kdf(boot_key, plaintext, raw_text)
|
|
88
|
+
_unseal_cache.clear()
|
|
89
|
+
_unseal_cache[cache_key] = dict(secrets)
|
|
90
|
+
return secrets
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _maybe_migrate_envelope_kdf(boot_key: str, plaintext: str, raw_text: str) -> None:
|
|
94
|
+
"""Opt-in re-seal of argon2id as HKDF with a verified rollback backup.
|
|
95
|
+
|
|
96
|
+
Normal reads never migrate. Set AGENT_WALLET_ENVELOPE_KDF_MIGRATION=1 to
|
|
97
|
+
opt in; failures leave the readable original in place.
|
|
98
|
+
"""
|
|
99
|
+
from agent_wallet.config import envelope_kdf_migration_enabled
|
|
100
|
+
from agent_wallet.encrypted_storage import KDF_ARGON2ID, _default_write_kdf, envelope_kdf
|
|
101
|
+
|
|
102
|
+
try:
|
|
103
|
+
if not envelope_kdf_migration_enabled():
|
|
104
|
+
return
|
|
105
|
+
if _default_write_kdf() == KDF_ARGON2ID:
|
|
106
|
+
return # forced-argon2id installs must not rewrite in place forever
|
|
107
|
+
if envelope_kdf(raw_text) != KDF_ARGON2ID:
|
|
108
|
+
return
|
|
109
|
+
from agent_wallet.encrypted_storage import migrate_envelope_to_hkdf
|
|
110
|
+
|
|
111
|
+
migrate_envelope_to_hkdf(
|
|
112
|
+
resolve_sealed_keys_path(),
|
|
113
|
+
plaintext,
|
|
114
|
+
master_key=boot_key,
|
|
115
|
+
)
|
|
116
|
+
clear_unseal_cache()
|
|
117
|
+
except Exception:
|
|
118
|
+
pass
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""Helpers for native Solana stake program instructions."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import struct
|
|
6
|
+
|
|
7
|
+
from solders.instruction import AccountMeta, Instruction
|
|
8
|
+
from solders.pubkey import Pubkey
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
STAKE_PROGRAM_ID = Pubkey.from_string("Stake11111111111111111111111111111111111111")
|
|
12
|
+
STAKE_CONFIG_ID = Pubkey.from_string("StakeConfig11111111111111111111111111111111")
|
|
13
|
+
SYSVAR_CLOCK_ID = Pubkey.from_string("SysvarC1ock11111111111111111111111111111111")
|
|
14
|
+
SYSVAR_STAKE_HISTORY_ID = Pubkey.from_string("SysvarStakeHistory1111111111111111111111111")
|
|
15
|
+
SYSVAR_RENT_ID = Pubkey.from_string("SysvarRent111111111111111111111111111111111")
|
|
16
|
+
STAKE_STATE_V2_SIZE = 200
|
|
17
|
+
|
|
18
|
+
STAKE_INSTR_DELEGATE = 2
|
|
19
|
+
STAKE_INSTR_WITHDRAW = 4
|
|
20
|
+
STAKE_INSTR_DEACTIVATE = 5
|
|
21
|
+
STAKE_INSTR_INITIALIZE_CHECKED = 9
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _stake_variant(index: int) -> bytes:
|
|
25
|
+
return struct.pack("<I", index)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def initialize_checked(
|
|
29
|
+
*,
|
|
30
|
+
stake_account: Pubkey,
|
|
31
|
+
staker: Pubkey,
|
|
32
|
+
withdrawer: Pubkey,
|
|
33
|
+
) -> Instruction:
|
|
34
|
+
"""Build InitializeChecked for a new stake account."""
|
|
35
|
+
return Instruction(
|
|
36
|
+
STAKE_PROGRAM_ID,
|
|
37
|
+
_stake_variant(STAKE_INSTR_INITIALIZE_CHECKED),
|
|
38
|
+
[
|
|
39
|
+
AccountMeta(stake_account, is_signer=False, is_writable=True),
|
|
40
|
+
AccountMeta(SYSVAR_RENT_ID, is_signer=False, is_writable=False),
|
|
41
|
+
AccountMeta(staker, is_signer=False, is_writable=False),
|
|
42
|
+
AccountMeta(withdrawer, is_signer=False, is_writable=False),
|
|
43
|
+
],
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def delegate_stake(
|
|
48
|
+
*,
|
|
49
|
+
stake_account: Pubkey,
|
|
50
|
+
vote_account: Pubkey,
|
|
51
|
+
authority: Pubkey,
|
|
52
|
+
) -> Instruction:
|
|
53
|
+
"""Build DelegateStake for an initialized stake account."""
|
|
54
|
+
return Instruction(
|
|
55
|
+
STAKE_PROGRAM_ID,
|
|
56
|
+
_stake_variant(STAKE_INSTR_DELEGATE),
|
|
57
|
+
[
|
|
58
|
+
AccountMeta(stake_account, is_signer=False, is_writable=True),
|
|
59
|
+
AccountMeta(vote_account, is_signer=False, is_writable=False),
|
|
60
|
+
AccountMeta(SYSVAR_CLOCK_ID, is_signer=False, is_writable=False),
|
|
61
|
+
AccountMeta(SYSVAR_STAKE_HISTORY_ID, is_signer=False, is_writable=False),
|
|
62
|
+
AccountMeta(STAKE_CONFIG_ID, is_signer=False, is_writable=False),
|
|
63
|
+
AccountMeta(authority, is_signer=True, is_writable=False),
|
|
64
|
+
],
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def deactivate_stake(
|
|
69
|
+
*,
|
|
70
|
+
stake_account: Pubkey,
|
|
71
|
+
authority: Pubkey,
|
|
72
|
+
) -> Instruction:
|
|
73
|
+
"""Build Deactivate for a delegated stake account."""
|
|
74
|
+
return Instruction(
|
|
75
|
+
STAKE_PROGRAM_ID,
|
|
76
|
+
_stake_variant(STAKE_INSTR_DEACTIVATE),
|
|
77
|
+
[
|
|
78
|
+
AccountMeta(stake_account, is_signer=False, is_writable=True),
|
|
79
|
+
AccountMeta(SYSVAR_CLOCK_ID, is_signer=False, is_writable=False),
|
|
80
|
+
AccountMeta(authority, is_signer=True, is_writable=False),
|
|
81
|
+
],
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def withdraw_stake(
|
|
86
|
+
*,
|
|
87
|
+
stake_account: Pubkey,
|
|
88
|
+
recipient: Pubkey,
|
|
89
|
+
authority: Pubkey,
|
|
90
|
+
lamports: int,
|
|
91
|
+
) -> Instruction:
|
|
92
|
+
"""Build Withdraw for an inactive or partially withdrawable stake account."""
|
|
93
|
+
return Instruction(
|
|
94
|
+
STAKE_PROGRAM_ID,
|
|
95
|
+
struct.pack("<IQ", STAKE_INSTR_WITHDRAW, lamports),
|
|
96
|
+
[
|
|
97
|
+
AccountMeta(stake_account, is_signer=False, is_writable=True),
|
|
98
|
+
AccountMeta(recipient, is_signer=False, is_writable=True),
|
|
99
|
+
AccountMeta(SYSVAR_CLOCK_ID, is_signer=False, is_writable=False),
|
|
100
|
+
AccountMeta(SYSVAR_STAKE_HISTORY_ID, is_signer=False, is_writable=False),
|
|
101
|
+
AccountMeta(authority, is_signer=True, is_writable=False),
|
|
102
|
+
],
|
|
103
|
+
)
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"""Minimal legacy Solana transaction builder for native SOL transfers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import base64
|
|
6
|
+
import struct
|
|
7
|
+
|
|
8
|
+
from agent_wallet.wallet_layer.base58 import b58decode
|
|
9
|
+
|
|
10
|
+
SYSTEM_PROGRAM_ID = "11111111111111111111111111111111"
|
|
11
|
+
SYSTEM_TRANSFER_INSTRUCTION = 2
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def encode_shortvec(value: int) -> bytes:
|
|
15
|
+
"""Encode an integer using Solana's compact-u16/shortvec format."""
|
|
16
|
+
if value < 0:
|
|
17
|
+
raise ValueError("shortvec cannot encode negative values")
|
|
18
|
+
|
|
19
|
+
encoded = bytearray()
|
|
20
|
+
remaining = value
|
|
21
|
+
while True:
|
|
22
|
+
element = remaining & 0x7F
|
|
23
|
+
remaining >>= 7
|
|
24
|
+
if remaining:
|
|
25
|
+
encoded.append(element | 0x80)
|
|
26
|
+
else:
|
|
27
|
+
encoded.append(element)
|
|
28
|
+
break
|
|
29
|
+
return bytes(encoded)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def build_legacy_sol_transfer_message(
|
|
33
|
+
sender: str,
|
|
34
|
+
recipient: str,
|
|
35
|
+
recent_blockhash: str,
|
|
36
|
+
lamports: int,
|
|
37
|
+
) -> bytes:
|
|
38
|
+
"""Build a legacy transaction message for a native SOL transfer."""
|
|
39
|
+
if lamports <= 0:
|
|
40
|
+
raise ValueError("lamports must be greater than zero")
|
|
41
|
+
|
|
42
|
+
sender_key = b58decode(sender)
|
|
43
|
+
recipient_key = b58decode(recipient)
|
|
44
|
+
system_program_key = b58decode(SYSTEM_PROGRAM_ID)
|
|
45
|
+
blockhash_bytes = b58decode(recent_blockhash)
|
|
46
|
+
|
|
47
|
+
if len(sender_key) != 32 or len(recipient_key) != 32 or len(system_program_key) != 32:
|
|
48
|
+
raise ValueError("All account keys must decode to 32 bytes")
|
|
49
|
+
if len(blockhash_bytes) != 32:
|
|
50
|
+
raise ValueError("Recent blockhash must decode to 32 bytes")
|
|
51
|
+
|
|
52
|
+
header = bytes(
|
|
53
|
+
[
|
|
54
|
+
1, # num_required_signatures
|
|
55
|
+
0, # num_readonly_signed_accounts
|
|
56
|
+
1, # num_readonly_unsigned_accounts (system program)
|
|
57
|
+
]
|
|
58
|
+
)
|
|
59
|
+
account_keys = [sender_key, recipient_key, system_program_key]
|
|
60
|
+
message = bytearray()
|
|
61
|
+
message.extend(header)
|
|
62
|
+
message.extend(encode_shortvec(len(account_keys)))
|
|
63
|
+
for account_key in account_keys:
|
|
64
|
+
message.extend(account_key)
|
|
65
|
+
message.extend(blockhash_bytes)
|
|
66
|
+
|
|
67
|
+
instruction_data = struct.pack("<IQ", SYSTEM_TRANSFER_INSTRUCTION, lamports)
|
|
68
|
+
compiled_instruction = bytearray()
|
|
69
|
+
compiled_instruction.append(2) # system program index
|
|
70
|
+
compiled_instruction.extend(encode_shortvec(2))
|
|
71
|
+
compiled_instruction.extend(bytes([0, 1])) # sender, recipient
|
|
72
|
+
compiled_instruction.extend(encode_shortvec(len(instruction_data)))
|
|
73
|
+
compiled_instruction.extend(instruction_data)
|
|
74
|
+
|
|
75
|
+
message.extend(encode_shortvec(1))
|
|
76
|
+
message.extend(compiled_instruction)
|
|
77
|
+
return bytes(message)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def serialize_legacy_transaction(signature: bytes, message: bytes) -> bytes:
|
|
81
|
+
"""Serialize a signed legacy transaction."""
|
|
82
|
+
if len(signature) != 64:
|
|
83
|
+
raise ValueError("Solana signatures must be 64 bytes")
|
|
84
|
+
payload = bytearray()
|
|
85
|
+
payload.extend(encode_shortvec(1))
|
|
86
|
+
payload.extend(signature)
|
|
87
|
+
payload.extend(message)
|
|
88
|
+
return bytes(payload)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def encode_transaction_base64(transaction_bytes: bytes) -> str:
|
|
92
|
+
"""Encode serialized transaction bytes for sendTransaction RPC."""
|
|
93
|
+
return base64.b64encode(transaction_bytes).decode("ascii")
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""In-process spending ledger with configurable per-transaction, hourly, and daily limits.
|
|
2
|
+
|
|
3
|
+
All limits default to ``0`` (unlimited). When a limit is set, every
|
|
4
|
+
``check_and_record`` call verifies that the requested spend would not
|
|
5
|
+
exceed the configured thresholds. If a limit would be breached, a
|
|
6
|
+
``WalletBackendError`` is raised *before* the on-chain transaction is
|
|
7
|
+
submitted.
|
|
8
|
+
|
|
9
|
+
Thread-safe: uses ``threading.Lock`` for concurrent access. For
|
|
10
|
+
multi-instance deployments, replace ``SpendingLedger`` with a shared
|
|
11
|
+
store (Redis / SQLite) behind the same interface.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import threading
|
|
17
|
+
import time
|
|
18
|
+
from collections.abc import Callable
|
|
19
|
+
from dataclasses import dataclass
|
|
20
|
+
|
|
21
|
+
from agent_wallet.wallet_layer.base import WalletBackendError
|
|
22
|
+
|
|
23
|
+
# ---------------------------------------------------------------------------
|
|
24
|
+
# 1 SOL = 1_000_000_000 lamports
|
|
25
|
+
# ---------------------------------------------------------------------------
|
|
26
|
+
LAMPORTS_PER_SOL = 1_000_000_000
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(frozen=True)
|
|
30
|
+
class SpendingConfig:
|
|
31
|
+
"""Configurable spending limits (all values in lamports, 0 = unlimited)."""
|
|
32
|
+
|
|
33
|
+
max_per_tx_lamports: int = 0
|
|
34
|
+
max_hourly_lamports: int = 0
|
|
35
|
+
max_daily_lamports: int = 0
|
|
36
|
+
max_txs_per_minute: int = 0
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class SpendingLedger:
|
|
40
|
+
"""Records executed spends and rejects operations that exceed limits."""
|
|
41
|
+
|
|
42
|
+
def __init__(
|
|
43
|
+
self,
|
|
44
|
+
config: SpendingConfig,
|
|
45
|
+
*,
|
|
46
|
+
clock: Callable[[], float] = time.monotonic,
|
|
47
|
+
entries: list[tuple[float, int]] | None = None,
|
|
48
|
+
) -> None:
|
|
49
|
+
self.config = config
|
|
50
|
+
# (timestamp, lamports) pairs in the unit returned by ``clock``.
|
|
51
|
+
# Defaults to ``time.monotonic`` for in-process use; pass
|
|
52
|
+
# ``time.time`` (wall-clock) when entries must be persisted and
|
|
53
|
+
# reloaded across processes.
|
|
54
|
+
self._clock = clock
|
|
55
|
+
self._entries: list[tuple[float, int]] = list(entries or [])
|
|
56
|
+
self._lock = threading.Lock()
|
|
57
|
+
|
|
58
|
+
# -- public API ----------------------------------------------------------
|
|
59
|
+
|
|
60
|
+
def check_and_record(self, lamports: int) -> None:
|
|
61
|
+
"""Validate *lamports* against limits and record the spend.
|
|
62
|
+
|
|
63
|
+
Raises ``WalletBackendError`` if any limit would be exceeded.
|
|
64
|
+
"""
|
|
65
|
+
with self._lock:
|
|
66
|
+
now = self._clock()
|
|
67
|
+
self._cleanup(now)
|
|
68
|
+
|
|
69
|
+
# Per-transaction cap
|
|
70
|
+
if self.config.max_per_tx_lamports > 0:
|
|
71
|
+
if lamports > self.config.max_per_tx_lamports:
|
|
72
|
+
raise WalletBackendError(
|
|
73
|
+
f"Transaction exceeds per-tx limit: "
|
|
74
|
+
f"{lamports} > {self.config.max_per_tx_lamports} lamports "
|
|
75
|
+
f"({self.config.max_per_tx_lamports / LAMPORTS_PER_SOL:.4f} SOL)."
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
# Transactions-per-minute rate limit
|
|
79
|
+
if self.config.max_txs_per_minute > 0:
|
|
80
|
+
recent_count = sum(1 for ts, _ in self._entries if now - ts < 60)
|
|
81
|
+
if recent_count >= self.config.max_txs_per_minute:
|
|
82
|
+
raise WalletBackendError(
|
|
83
|
+
f"Transaction rate limit exceeded: "
|
|
84
|
+
f"max {self.config.max_txs_per_minute} txs/minute."
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
# Hourly cumulative cap
|
|
88
|
+
if self.config.max_hourly_lamports > 0:
|
|
89
|
+
hourly_total = sum(lam for ts, lam in self._entries if now - ts < 3600)
|
|
90
|
+
if hourly_total + lamports > self.config.max_hourly_lamports:
|
|
91
|
+
raise WalletBackendError(
|
|
92
|
+
f"Hourly spending limit exceeded: "
|
|
93
|
+
f"would reach {(hourly_total + lamports) / LAMPORTS_PER_SOL:.4f} SOL, "
|
|
94
|
+
f"limit is {self.config.max_hourly_lamports / LAMPORTS_PER_SOL:.4f} SOL."
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
# Daily cumulative cap
|
|
98
|
+
if self.config.max_daily_lamports > 0:
|
|
99
|
+
daily_total = sum(lam for ts, lam in self._entries if now - ts < 86400)
|
|
100
|
+
if daily_total + lamports > self.config.max_daily_lamports:
|
|
101
|
+
raise WalletBackendError(
|
|
102
|
+
f"Daily spending limit exceeded: "
|
|
103
|
+
f"would reach {(daily_total + lamports) / LAMPORTS_PER_SOL:.4f} SOL, "
|
|
104
|
+
f"limit is {self.config.max_daily_lamports / LAMPORTS_PER_SOL:.4f} SOL."
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
self._entries.append((now, lamports))
|
|
108
|
+
|
|
109
|
+
def export(self) -> list[tuple[float, int]]:
|
|
110
|
+
"""Return a copy of the (timestamp, lamports) entries for persistence."""
|
|
111
|
+
with self._lock:
|
|
112
|
+
self._cleanup(self._clock())
|
|
113
|
+
return list(self._entries)
|
|
114
|
+
|
|
115
|
+
# -- internal ------------------------------------------------------------
|
|
116
|
+
|
|
117
|
+
def _cleanup(self, now: float) -> None:
|
|
118
|
+
"""Evict entries older than 24 h to bound memory usage."""
|
|
119
|
+
self._entries = [(ts, lam) for ts, lam in self._entries if now - ts < 86400]
|