@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,849 @@
|
|
|
1
|
+
import "dotenv/config";
|
|
2
|
+
|
|
3
|
+
import crypto from "node:crypto";
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
import { createServer } from "node:http";
|
|
6
|
+
import net from "node:net";
|
|
7
|
+
|
|
8
|
+
import { loadConfig } from "./config.js";
|
|
9
|
+
import { readJsonBody, sendJson } from "./json.js";
|
|
10
|
+
import { LocalEvmVault } from "./local_vault.js";
|
|
11
|
+
import { EvmNetworkState } from "./network_state.js";
|
|
12
|
+
import { createShutdownCoordinator, withTrackedRequest } from "./shutdown.js";
|
|
13
|
+
import { WdkEvmWalletService } from "./wdk_evm_wallet.js";
|
|
14
|
+
|
|
15
|
+
export async function startServer(config = loadConfig()) {
|
|
16
|
+
const service = new WdkEvmWalletService(config);
|
|
17
|
+
const vault = new LocalEvmVault(config);
|
|
18
|
+
const networkState = new EvmNetworkState(config);
|
|
19
|
+
|
|
20
|
+
function normalizeErrorCode(errorCode, pathname, message) {
|
|
21
|
+
const code = String(errorCode || "").trim().toLowerCase();
|
|
22
|
+
const lower = String(message || "").toLowerCase();
|
|
23
|
+
const isTokenPath = pathname.includes("/token");
|
|
24
|
+
const isTokenReadPath =
|
|
25
|
+
pathname.includes("/token-balance/") || pathname.includes("/token-metadata/");
|
|
26
|
+
|
|
27
|
+
if (code === "insufficient_funds") {
|
|
28
|
+
return "insufficient_funds";
|
|
29
|
+
}
|
|
30
|
+
if (code === "network_unavailable") {
|
|
31
|
+
return "network_unavailable";
|
|
32
|
+
}
|
|
33
|
+
if (
|
|
34
|
+
code === "swap_quote_changed" ||
|
|
35
|
+
code === "swap_simulation_failed" ||
|
|
36
|
+
code === "swap_approval_required" ||
|
|
37
|
+
code === "swap_approval_failed" ||
|
|
38
|
+
code === "swap_cleanup_failed" ||
|
|
39
|
+
code === "aave_quote_changed" ||
|
|
40
|
+
code === "aave_approval_required" ||
|
|
41
|
+
code === "aave_fee_unavailable" ||
|
|
42
|
+
code === "aave_operation_reverted" ||
|
|
43
|
+
code === "aave_cleanup_failed" ||
|
|
44
|
+
code === "morpho_api_failed" ||
|
|
45
|
+
code === "morpho_quote_changed" ||
|
|
46
|
+
code === "morpho_requirements_unresolved" ||
|
|
47
|
+
code === "morpho_fee_unavailable" ||
|
|
48
|
+
code === "morpho_cleanup_failed" ||
|
|
49
|
+
code === "defi_gas_estimate_unavailable" ||
|
|
50
|
+
code === "lido_operation_reverted" ||
|
|
51
|
+
code === "lido_withdrawal_reverted" ||
|
|
52
|
+
code === "swap_reverted" ||
|
|
53
|
+
code === "morpho_operation_reverted" ||
|
|
54
|
+
code === "uniswap_liquidity_reverted" ||
|
|
55
|
+
code === "uniswap_liquidity_approval_reverted" ||
|
|
56
|
+
code === "transaction_reverted" ||
|
|
57
|
+
code === "native_transfer_reverted" ||
|
|
58
|
+
code === "token_transfer_reverted" ||
|
|
59
|
+
code === "token_transfer_failed" ||
|
|
60
|
+
code === "fee_limit_exceeded" ||
|
|
61
|
+
code === "token_read_failed" ||
|
|
62
|
+
code === "uniswap_api_key_missing" ||
|
|
63
|
+
code === "uniswap_unsupported_route" ||
|
|
64
|
+
code === "uniswap_unexpected_router"
|
|
65
|
+
) {
|
|
66
|
+
return code;
|
|
67
|
+
}
|
|
68
|
+
if (
|
|
69
|
+
code === "call_exception" ||
|
|
70
|
+
code === "bad_data" ||
|
|
71
|
+
code === "execution_reverted" ||
|
|
72
|
+
code === "contract_not_found"
|
|
73
|
+
) {
|
|
74
|
+
if (isTokenReadPath) {
|
|
75
|
+
return "token_not_found";
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (
|
|
79
|
+
code === "econnrefused" ||
|
|
80
|
+
code === "enotfound" ||
|
|
81
|
+
code === "etimedout" ||
|
|
82
|
+
code === "fetch_failed"
|
|
83
|
+
) {
|
|
84
|
+
return "network_unavailable";
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (lower.includes("wallet is locked")) {
|
|
88
|
+
return "wallet_locked";
|
|
89
|
+
}
|
|
90
|
+
if (lower.includes("insufficient funds")) {
|
|
91
|
+
return "insufficient_funds";
|
|
92
|
+
}
|
|
93
|
+
if (
|
|
94
|
+
lower.includes("recipient must be a valid") ||
|
|
95
|
+
lower.includes("recipient must not be the zero address") ||
|
|
96
|
+
lower.includes("to must be a valid") ||
|
|
97
|
+
lower.includes("to must not be the zero address") ||
|
|
98
|
+
lower.includes("invalid address") ||
|
|
99
|
+
lower.includes("bad address checksum")
|
|
100
|
+
) {
|
|
101
|
+
return "recipient_invalid";
|
|
102
|
+
}
|
|
103
|
+
if (
|
|
104
|
+
isTokenReadPath &&
|
|
105
|
+
(lower.includes("missing revert data") ||
|
|
106
|
+
lower.includes("call exception") ||
|
|
107
|
+
lower.includes("could not decode result data") ||
|
|
108
|
+
lower.includes("no contract code") ||
|
|
109
|
+
lower.includes("execution reverted"))
|
|
110
|
+
) {
|
|
111
|
+
return "token_not_found";
|
|
112
|
+
}
|
|
113
|
+
if (
|
|
114
|
+
lower.includes("rpc network unavailable") ||
|
|
115
|
+
lower.includes("rpc request failed") ||
|
|
116
|
+
lower.includes("rpc returned invalid json") ||
|
|
117
|
+
lower.includes("fetch failed") ||
|
|
118
|
+
lower.includes("network unavailable") ||
|
|
119
|
+
lower.includes("timeout")
|
|
120
|
+
) {
|
|
121
|
+
return "network_unavailable";
|
|
122
|
+
}
|
|
123
|
+
if (lower.includes("unknown walletid")) {
|
|
124
|
+
return "wallet_not_found";
|
|
125
|
+
}
|
|
126
|
+
if (lower.includes("invalid password")) {
|
|
127
|
+
return "invalid_password";
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function errorStatusCode(errorCode, fallback = 400) {
|
|
134
|
+
if (errorCode === "wallet_locked" || errorCode === "insufficient_funds") {
|
|
135
|
+
return 409;
|
|
136
|
+
}
|
|
137
|
+
if (errorCode === "swap_quote_changed") {
|
|
138
|
+
return 409;
|
|
139
|
+
}
|
|
140
|
+
if (errorCode === "aave_quote_changed") {
|
|
141
|
+
return 409;
|
|
142
|
+
}
|
|
143
|
+
if (errorCode === "morpho_quote_changed") {
|
|
144
|
+
return 409;
|
|
145
|
+
}
|
|
146
|
+
if (
|
|
147
|
+
errorCode === "swap_simulation_failed" ||
|
|
148
|
+
errorCode === "swap_approval_required" ||
|
|
149
|
+
errorCode === "swap_approval_failed" ||
|
|
150
|
+
errorCode === "swap_cleanup_failed" ||
|
|
151
|
+
errorCode === "aave_approval_required" ||
|
|
152
|
+
errorCode === "aave_fee_unavailable" ||
|
|
153
|
+
errorCode === "aave_operation_reverted" ||
|
|
154
|
+
errorCode === "aave_cleanup_failed" ||
|
|
155
|
+
errorCode === "morpho_api_failed" ||
|
|
156
|
+
errorCode === "morpho_requirements_unresolved" ||
|
|
157
|
+
errorCode === "morpho_fee_unavailable" ||
|
|
158
|
+
errorCode === "morpho_cleanup_failed" ||
|
|
159
|
+
errorCode === "defi_gas_estimate_unavailable" ||
|
|
160
|
+
errorCode === "lido_operation_reverted" ||
|
|
161
|
+
errorCode === "lido_withdrawal_reverted" ||
|
|
162
|
+
errorCode === "swap_reverted" ||
|
|
163
|
+
errorCode === "token_transfer_failed" ||
|
|
164
|
+
errorCode === "fee_limit_exceeded" ||
|
|
165
|
+
errorCode === "uniswap_api_key_missing"
|
|
166
|
+
) {
|
|
167
|
+
return 400;
|
|
168
|
+
}
|
|
169
|
+
if (errorCode === "uniswap_unsupported_route") {
|
|
170
|
+
return 422;
|
|
171
|
+
}
|
|
172
|
+
if (errorCode === "uniswap_unexpected_router") {
|
|
173
|
+
return 502;
|
|
174
|
+
}
|
|
175
|
+
if (errorCode === "token_read_failed") {
|
|
176
|
+
return 502;
|
|
177
|
+
}
|
|
178
|
+
if (errorCode === "network_unavailable") {
|
|
179
|
+
return 503;
|
|
180
|
+
}
|
|
181
|
+
if (errorCode === "wallet_not_found" || errorCode === "token_not_found") {
|
|
182
|
+
return 404;
|
|
183
|
+
}
|
|
184
|
+
return fallback;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function sanitizeProviderUrl(value) {
|
|
188
|
+
const raw = String(value || "").trim();
|
|
189
|
+
if (!raw) {
|
|
190
|
+
return raw;
|
|
191
|
+
}
|
|
192
|
+
try {
|
|
193
|
+
const url = new URL(raw);
|
|
194
|
+
if (url.searchParams.has("token")) {
|
|
195
|
+
url.searchParams.set("token", "***");
|
|
196
|
+
}
|
|
197
|
+
return url.toString();
|
|
198
|
+
} catch {
|
|
199
|
+
return raw;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function sanitizeProfiles(profiles = {}) {
|
|
204
|
+
return Object.fromEntries(
|
|
205
|
+
Object.entries(profiles).map(([network, profile]) => [
|
|
206
|
+
network,
|
|
207
|
+
{
|
|
208
|
+
...profile,
|
|
209
|
+
providerUrl: sanitizeProviderUrl(profile?.providerUrl),
|
|
210
|
+
},
|
|
211
|
+
])
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function toErrorResponse(error, pathname, fallbackStatus = 400) {
|
|
216
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
217
|
+
const explicitCode =
|
|
218
|
+
(typeof error?.errorCode === "string" && error.errorCode.trim()) ||
|
|
219
|
+
(typeof error?.code === "string" && error.code.trim()) ||
|
|
220
|
+
"";
|
|
221
|
+
const errorCode = normalizeErrorCode(explicitCode, pathname, message);
|
|
222
|
+
const details =
|
|
223
|
+
error && typeof error === "object" && error.errorDetails && typeof error.errorDetails === "object"
|
|
224
|
+
? { ...error.errorDetails }
|
|
225
|
+
: {};
|
|
226
|
+
details.source = "wdk-evm-wallet";
|
|
227
|
+
details.path = pathname;
|
|
228
|
+
return {
|
|
229
|
+
statusCode: errorStatusCode(errorCode, fallbackStatus),
|
|
230
|
+
payload: {
|
|
231
|
+
ok: false,
|
|
232
|
+
error: message,
|
|
233
|
+
...(errorCode ? { error_code: errorCode } : {}),
|
|
234
|
+
error_details: details,
|
|
235
|
+
},
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function notFound(response) {
|
|
240
|
+
sendJson(response, 404, { ok: false, error: "Not Found" });
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function unauthorized(response) {
|
|
244
|
+
response.setHeader("WWW-Authenticate", 'Bearer realm="wdk-evm-wallet"');
|
|
245
|
+
sendJson(response, 401, { ok: false, error: "Unauthorized." });
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function pathRequiresAuth(pathname) {
|
|
249
|
+
return pathname !== "/health";
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function isAuthorized(request) {
|
|
253
|
+
const header = String(request.headers.authorization || "").trim();
|
|
254
|
+
if (!header.startsWith("Bearer ")) {
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
const provided = Buffer.from(header.slice("Bearer ".length).trim(), "utf8");
|
|
258
|
+
const expected = Buffer.from(String(config.authToken || ""), "utf8");
|
|
259
|
+
if (provided.length === 0 || provided.length !== expected.length) {
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
return crypto.timingSafeEqual(provided, expected);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
async function withResolvedSeed(body = {}) {
|
|
266
|
+
const resolved = await vault.resolveSeedPhrase({
|
|
267
|
+
walletId: body.walletId,
|
|
268
|
+
seedPhrase: body.seedPhrase,
|
|
269
|
+
password: body.password,
|
|
270
|
+
});
|
|
271
|
+
return {
|
|
272
|
+
...body,
|
|
273
|
+
seedPhrase: resolved.seedPhrase,
|
|
274
|
+
walletId: resolved.walletId ?? body.walletId ?? null,
|
|
275
|
+
credentialSource: resolved.source,
|
|
276
|
+
unlockExpiresAt: resolved.unlockExpiresAt ?? null,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
async function withResolvedSeedOrAddress(body = {}) {
|
|
281
|
+
const address = typeof body.address === "string" ? body.address.trim() : "";
|
|
282
|
+
if (address) {
|
|
283
|
+
return {
|
|
284
|
+
...body,
|
|
285
|
+
address,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
return withResolvedSeed(body);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
async function withResolvedNetwork(body = {}) {
|
|
292
|
+
const runtimeConfig = await networkState.resolveRuntimeConfig(body.network);
|
|
293
|
+
return {
|
|
294
|
+
...body,
|
|
295
|
+
network: runtimeConfig.network,
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
async function handleRequest(request, response) {
|
|
300
|
+
try {
|
|
301
|
+
const url = new URL(request.url || "/", "http://localhost");
|
|
302
|
+
const { method = "GET" } = request;
|
|
303
|
+
|
|
304
|
+
if (pathRequiresAuth(url.pathname) && !isAuthorized(request)) {
|
|
305
|
+
return unauthorized(response);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (method === "GET" && url.pathname === "/health") {
|
|
309
|
+
const runtimeConfig = await networkState.resolveRuntimeConfig();
|
|
310
|
+
const networkInfo = await networkState.getNetworkInfo();
|
|
311
|
+
return sendJson(response, 200, {
|
|
312
|
+
ok: true,
|
|
313
|
+
service: "wdk-evm-wallet",
|
|
314
|
+
version: config.version,
|
|
315
|
+
wallet: "evm",
|
|
316
|
+
network: runtimeConfig.network,
|
|
317
|
+
chainId: runtimeConfig.chainId,
|
|
318
|
+
host: config.host,
|
|
319
|
+
dataDir: config.dataDir,
|
|
320
|
+
instanceId: config.instanceId,
|
|
321
|
+
pid: process.pid,
|
|
322
|
+
authRequired: config.authRequired,
|
|
323
|
+
unlockTimeoutSeconds: config.unlockTimeoutSeconds,
|
|
324
|
+
availableNetworks: Object.keys(config.networkProfiles),
|
|
325
|
+
provider: sanitizeProviderUrl(runtimeConfig.providerUrl),
|
|
326
|
+
networkProfiles: sanitizeProfiles(networkInfo.profiles),
|
|
327
|
+
source: "wdk",
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (method === "POST" && url.pathname === "/v1/evm/seed-phrase/generate") {
|
|
332
|
+
const body = await readJsonBody(request);
|
|
333
|
+
return sendJson(response, 200, {
|
|
334
|
+
ok: true,
|
|
335
|
+
data: service.generateSeedPhrase(body.words ?? 12),
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (method === "GET" && url.pathname === "/v1/evm/wallets") {
|
|
340
|
+
const activeNetwork = await networkState.getActiveNetwork();
|
|
341
|
+
return sendJson(response, 200, {
|
|
342
|
+
ok: true,
|
|
343
|
+
data: (await vault.listWallets()).map((wallet) => ({
|
|
344
|
+
...wallet,
|
|
345
|
+
activeNetwork,
|
|
346
|
+
})),
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (method === "GET" && url.pathname === "/v1/evm/network") {
|
|
351
|
+
return sendJson(response, 200, {
|
|
352
|
+
ok: true,
|
|
353
|
+
data: await networkState.getNetworkInfo(),
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if (method === "POST" && url.pathname === "/v1/evm/network/set") {
|
|
358
|
+
const body = await readJsonBody(request);
|
|
359
|
+
return sendJson(response, 200, {
|
|
360
|
+
ok: true,
|
|
361
|
+
data: await networkState.setActiveNetwork(body),
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (method === "POST" && url.pathname === "/v1/evm/wallets/get") {
|
|
366
|
+
const body = await readJsonBody(request);
|
|
367
|
+
const activeNetwork = await networkState.getActiveNetwork();
|
|
368
|
+
return sendJson(response, 200, {
|
|
369
|
+
ok: true,
|
|
370
|
+
data: {
|
|
371
|
+
...(await vault.getWallet(body)),
|
|
372
|
+
activeNetwork,
|
|
373
|
+
},
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (method === "POST" && url.pathname === "/v1/evm/wallets/create") {
|
|
378
|
+
const body = await withResolvedNetwork(await readJsonBody(request));
|
|
379
|
+
return sendJson(response, 200, {
|
|
380
|
+
ok: true,
|
|
381
|
+
data: await vault.createWallet(body),
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (method === "POST" && url.pathname === "/v1/evm/wallets/import") {
|
|
386
|
+
const body = await withResolvedNetwork(await readJsonBody(request));
|
|
387
|
+
return sendJson(response, 200, {
|
|
388
|
+
ok: true,
|
|
389
|
+
data: await vault.importWallet(body),
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (method === "POST" && url.pathname === "/v1/evm/wallets/unlock") {
|
|
394
|
+
const body = await readJsonBody(request);
|
|
395
|
+
return sendJson(response, 200, {
|
|
396
|
+
ok: true,
|
|
397
|
+
data: await vault.unlockWallet(body),
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
if (method === "POST" && url.pathname === "/v1/evm/wallets/lock") {
|
|
402
|
+
const body = await readJsonBody(request);
|
|
403
|
+
return sendJson(response, 200, {
|
|
404
|
+
ok: true,
|
|
405
|
+
data: await vault.lockWallet(body),
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
if (method === "POST" && url.pathname === "/v1/evm/wallets/reveal-seed") {
|
|
410
|
+
const body = await readJsonBody(request);
|
|
411
|
+
return sendJson(response, 200, {
|
|
412
|
+
ok: true,
|
|
413
|
+
data: await vault.revealSeedPhrase(body),
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (method === "POST" && url.pathname === "/v1/evm/wallets/change-password") {
|
|
418
|
+
const body = await readJsonBody(request);
|
|
419
|
+
return sendJson(response, 200, {
|
|
420
|
+
ok: true,
|
|
421
|
+
data: await vault.changePassword(body),
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
if (method === "POST" && url.pathname === "/v1/evm/address/resolve") {
|
|
426
|
+
const body = await withResolvedNetwork(await withResolvedSeed(await readJsonBody(request)));
|
|
427
|
+
const data = await service.resolveAddress(body);
|
|
428
|
+
return sendJson(response, 200, { ok: true, data });
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
if (method === "POST" && url.pathname === "/v1/evm/balance/get") {
|
|
432
|
+
const body = await withResolvedNetwork(await withResolvedSeedOrAddress(await readJsonBody(request)));
|
|
433
|
+
const data = await service.getBalance(body);
|
|
434
|
+
return sendJson(response, 200, { ok: true, data });
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
if (method === "POST" && url.pathname === "/v1/evm/token-balance/get") {
|
|
438
|
+
const body = await withResolvedNetwork(await withResolvedSeedOrAddress(await readJsonBody(request)));
|
|
439
|
+
const data = await service.getTokenBalance(body);
|
|
440
|
+
return sendJson(response, 200, { ok: true, data });
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (method === "POST" && url.pathname === "/v1/evm/token-metadata/get") {
|
|
444
|
+
const body = await withResolvedNetwork(await readJsonBody(request));
|
|
445
|
+
const data = await service.getTokenMetadata(body);
|
|
446
|
+
return sendJson(response, 200, { ok: true, data });
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if (method === "POST" && url.pathname === "/v1/evm/fee-rates/get") {
|
|
450
|
+
const body = await withResolvedNetwork(await readJsonBody(request));
|
|
451
|
+
const data = await service.getFeeRates(body);
|
|
452
|
+
return sendJson(response, 200, { ok: true, data });
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
if (method === "POST" && url.pathname === "/v1/evm/transaction/receipt/get") {
|
|
456
|
+
const body = await withResolvedNetwork(await readJsonBody(request));
|
|
457
|
+
const data = await service.getTransactionReceipt(body);
|
|
458
|
+
return sendJson(response, 200, { ok: true, data });
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
if (method === "POST" && url.pathname === "/v1/evm/aave/account/get") {
|
|
462
|
+
const body = await withResolvedNetwork(await withResolvedSeedOrAddress(await readJsonBody(request)));
|
|
463
|
+
const data = await service.getAaveAccountData(body);
|
|
464
|
+
return sendJson(response, 200, { ok: true, data });
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
if (method === "POST" && url.pathname === "/v1/evm/aave/reserves/get") {
|
|
468
|
+
const body = await withResolvedNetwork(await withResolvedSeedOrAddress(await readJsonBody(request)));
|
|
469
|
+
const data = await service.getAaveReserves(body);
|
|
470
|
+
return sendJson(response, 200, { ok: true, data });
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
if (method === "POST" && url.pathname === "/v1/evm/aave/positions/get") {
|
|
474
|
+
const body = await withResolvedNetwork(await withResolvedSeedOrAddress(await readJsonBody(request)));
|
|
475
|
+
const data = await service.getAavePositions(body);
|
|
476
|
+
return sendJson(response, 200, { ok: true, data });
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
if (method === "POST" && url.pathname === "/v1/evm/morpho/vaults/get") {
|
|
480
|
+
const body = await withResolvedNetwork(await readJsonBody(request));
|
|
481
|
+
const data = await service.getMorphoVaults(body);
|
|
482
|
+
return sendJson(response, 200, { ok: true, data });
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
if (method === "POST" && url.pathname === "/v1/evm/morpho/markets/get") {
|
|
486
|
+
const body = await withResolvedNetwork(await readJsonBody(request));
|
|
487
|
+
const data = await service.getMorphoMarkets(body);
|
|
488
|
+
return sendJson(response, 200, { ok: true, data });
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
if (method === "POST" && url.pathname === "/v1/evm/morpho/positions/get") {
|
|
492
|
+
const body = await withResolvedNetwork(await withResolvedSeedOrAddress(await readJsonBody(request)));
|
|
493
|
+
const data = await service.getMorphoPositions(body);
|
|
494
|
+
return sendJson(response, 200, { ok: true, data });
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
if (method === "POST" && url.pathname === "/v1/evm/morpho/rebalance-report/get") {
|
|
498
|
+
const body = await withResolvedSeedOrAddress(await readJsonBody(request));
|
|
499
|
+
const data = await service.getMorphoRebalanceReport(body);
|
|
500
|
+
return sendJson(response, 200, { ok: true, data });
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const morphoVaultOperationMatch = url.pathname.match(
|
|
504
|
+
/^\/v1\/evm\/morpho\/vault\/(supply|withdraw)\/(quote|send)$/
|
|
505
|
+
);
|
|
506
|
+
if (method === "POST" && morphoVaultOperationMatch) {
|
|
507
|
+
const operation = morphoVaultOperationMatch[1];
|
|
508
|
+
const action = morphoVaultOperationMatch[2];
|
|
509
|
+
const rawBody = await readJsonBody(request);
|
|
510
|
+
const body =
|
|
511
|
+
action === "quote"
|
|
512
|
+
? await withResolvedNetwork(await withResolvedSeedOrAddress(rawBody))
|
|
513
|
+
: await withResolvedNetwork(await withResolvedSeed(rawBody));
|
|
514
|
+
const data =
|
|
515
|
+
action === "quote"
|
|
516
|
+
? await service.quoteMorphoVaultOperation({ ...body, operation })
|
|
517
|
+
: await service.sendMorphoVaultOperation({ ...body, operation });
|
|
518
|
+
return sendJson(response, 200, { ok: true, data });
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
const morphoMarketOperationMatch = url.pathname.match(
|
|
522
|
+
/^\/v1\/evm\/morpho\/market\/(supply_collateral|borrow|repay|withdraw_collateral)\/(quote|send)$/
|
|
523
|
+
);
|
|
524
|
+
if (method === "POST" && morphoMarketOperationMatch) {
|
|
525
|
+
const operation = morphoMarketOperationMatch[1];
|
|
526
|
+
const action = morphoMarketOperationMatch[2];
|
|
527
|
+
const rawBody = await readJsonBody(request);
|
|
528
|
+
const body =
|
|
529
|
+
action === "quote"
|
|
530
|
+
? await withResolvedNetwork(await withResolvedSeedOrAddress(rawBody))
|
|
531
|
+
: await withResolvedNetwork(await withResolvedSeed(rawBody));
|
|
532
|
+
const data =
|
|
533
|
+
action === "quote"
|
|
534
|
+
? await service.quoteMorphoMarketOperation({ ...body, operation })
|
|
535
|
+
: await service.sendMorphoMarketOperation({ ...body, operation });
|
|
536
|
+
return sendJson(response, 200, { ok: true, data });
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
if (method === "POST" && url.pathname === "/v1/evm/lido/overview/get") {
|
|
540
|
+
const body = await withResolvedNetwork(await withResolvedSeedOrAddress(await readJsonBody(request)));
|
|
541
|
+
const data = await service.getLidoOverview(body);
|
|
542
|
+
return sendJson(response, 200, { ok: true, data });
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
if (method === "POST" && url.pathname === "/v1/evm/lido/positions/get") {
|
|
546
|
+
const body = await withResolvedNetwork(await withResolvedSeedOrAddress(await readJsonBody(request)));
|
|
547
|
+
const data = await service.getLidoPositions(body);
|
|
548
|
+
return sendJson(response, 200, { ok: true, data });
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
if (method === "POST" && url.pathname === "/v1/evm/lido/withdrawals/get") {
|
|
552
|
+
const body = await withResolvedNetwork(await withResolvedSeedOrAddress(await readJsonBody(request)));
|
|
553
|
+
const data = await service.getLidoWithdrawalRequests(body);
|
|
554
|
+
return sendJson(response, 200, { ok: true, data });
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
const aaveOperationMatch = url.pathname.match(
|
|
558
|
+
/^\/v1\/evm\/aave\/(supply|withdraw|borrow|repay)\/(quote|send)$/
|
|
559
|
+
);
|
|
560
|
+
if (method === "POST" && aaveOperationMatch) {
|
|
561
|
+
const operation = aaveOperationMatch[1];
|
|
562
|
+
const action = aaveOperationMatch[2];
|
|
563
|
+
const rawBody = await readJsonBody(request);
|
|
564
|
+
const body =
|
|
565
|
+
action === "quote"
|
|
566
|
+
? await withResolvedNetwork(await withResolvedSeedOrAddress(rawBody))
|
|
567
|
+
: await withResolvedNetwork(await withResolvedSeed(rawBody));
|
|
568
|
+
const data =
|
|
569
|
+
action === "quote"
|
|
570
|
+
? await service.quoteAaveOperation({ ...body, operation })
|
|
571
|
+
: await service.sendAaveOperation({ ...body, operation });
|
|
572
|
+
return sendJson(response, 200, { ok: true, data });
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
const lidoOperationMatch = url.pathname.match(
|
|
576
|
+
/^\/v1\/evm\/lido\/(stake_eth_for_wsteth|wrap_steth|unwrap_wsteth)\/(quote|send)$/
|
|
577
|
+
);
|
|
578
|
+
if (method === "POST" && lidoOperationMatch) {
|
|
579
|
+
const operation = lidoOperationMatch[1];
|
|
580
|
+
const action = lidoOperationMatch[2];
|
|
581
|
+
const rawBody = await readJsonBody(request);
|
|
582
|
+
const body =
|
|
583
|
+
action === "quote"
|
|
584
|
+
? await withResolvedNetwork(await withResolvedSeedOrAddress(rawBody))
|
|
585
|
+
: await withResolvedNetwork(await withResolvedSeed(rawBody));
|
|
586
|
+
const data =
|
|
587
|
+
action === "quote"
|
|
588
|
+
? await service.quoteLidoOperation({ ...body, operation })
|
|
589
|
+
: await service.sendLidoOperation({ ...body, operation });
|
|
590
|
+
return sendJson(response, 200, { ok: true, data });
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
const lidoWithdrawalOperationMatch = url.pathname.match(
|
|
594
|
+
/^\/v1\/evm\/lido\/(request_withdrawal_steth|request_withdrawal_wsteth|claim_withdrawal)\/(quote|send)$/
|
|
595
|
+
);
|
|
596
|
+
if (method === "POST" && lidoWithdrawalOperationMatch) {
|
|
597
|
+
const operation = lidoWithdrawalOperationMatch[1];
|
|
598
|
+
const action = lidoWithdrawalOperationMatch[2];
|
|
599
|
+
const rawBody = await readJsonBody(request);
|
|
600
|
+
const body =
|
|
601
|
+
action === "quote"
|
|
602
|
+
? await withResolvedNetwork(await withResolvedSeedOrAddress(rawBody))
|
|
603
|
+
: await withResolvedNetwork(await withResolvedSeed(rawBody));
|
|
604
|
+
const data =
|
|
605
|
+
action === "quote"
|
|
606
|
+
? await service.quoteLidoWithdrawalOperation({ ...body, operation })
|
|
607
|
+
: await service.sendLidoWithdrawalOperation({ ...body, operation });
|
|
608
|
+
return sendJson(response, 200, { ok: true, data });
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
if (method === "POST" && url.pathname === "/v1/evm/swap/quote") {
|
|
612
|
+
const body = await withResolvedNetwork(await withResolvedSeedOrAddress(await readJsonBody(request)));
|
|
613
|
+
const data = await service.quoteSwap(body);
|
|
614
|
+
return sendJson(response, 200, { ok: true, data });
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
if (method === "POST" && url.pathname === "/v1/evm/swap/send") {
|
|
618
|
+
const body = await withResolvedNetwork(await withResolvedSeed(await readJsonBody(request)));
|
|
619
|
+
const data = await service.swap(body);
|
|
620
|
+
return sendJson(response, 200, { ok: true, data });
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
if (method === "POST" && url.pathname === "/v1/evm/lifi/quote") {
|
|
624
|
+
const body = await withResolvedNetwork(await withResolvedSeedOrAddress(await readJsonBody(request)));
|
|
625
|
+
const data = await service.quoteLifiSwap(body);
|
|
626
|
+
return sendJson(response, 200, { ok: true, data });
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
if (method === "POST" && url.pathname === "/v1/evm/lifi/send") {
|
|
630
|
+
const body = await withResolvedNetwork(await withResolvedSeed(await readJsonBody(request)));
|
|
631
|
+
const data = await service.sendLifiSwap(body);
|
|
632
|
+
return sendJson(response, 200, { ok: true, data });
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
if (method === "POST" && url.pathname === "/v1/evm/uniswap/swap/quote") {
|
|
636
|
+
const body = await withResolvedNetwork(await withResolvedSeedOrAddress(await readJsonBody(request)));
|
|
637
|
+
const data = await service.quoteUniswapSwap(body);
|
|
638
|
+
return sendJson(response, 200, { ok: true, data });
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
if (method === "POST" && url.pathname === "/v1/evm/uniswap/swap/send") {
|
|
642
|
+
const body = await withResolvedNetwork(await withResolvedSeed(await readJsonBody(request)));
|
|
643
|
+
const data = await service.sendUniswapSwap(body);
|
|
644
|
+
return sendJson(response, 200, { ok: true, data });
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
if (method === "POST" && url.pathname === "/v1/evm/uniswap/liquidity/quote") {
|
|
648
|
+
const body = await withResolvedNetwork(await withResolvedSeedOrAddress(await readJsonBody(request)));
|
|
649
|
+
const data = await service.quoteUniswapLiquidity(body);
|
|
650
|
+
return sendJson(response, 200, { ok: true, data });
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
if (method === "POST" && url.pathname === "/v1/evm/uniswap/liquidity/pools") {
|
|
654
|
+
const body = await withResolvedNetwork(await readJsonBody(request));
|
|
655
|
+
const data = await service.getUniswapLiquidityPools(body);
|
|
656
|
+
return sendJson(response, 200, { ok: true, data });
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
if (method === "POST" && url.pathname === "/v1/evm/uniswap/liquidity/positions") {
|
|
660
|
+
const body = await withResolvedNetwork(await withResolvedSeedOrAddress(await readJsonBody(request)));
|
|
661
|
+
const data = await service.getUniswapLiquidityPositions(body);
|
|
662
|
+
return sendJson(response, 200, { ok: true, data });
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
if (method === "POST" && url.pathname === "/v1/evm/uniswap/liquidity/send") {
|
|
666
|
+
const body = await withResolvedNetwork(await withResolvedSeed(await readJsonBody(request)));
|
|
667
|
+
const data = await service.sendUniswapLiquidity(body);
|
|
668
|
+
return sendJson(response, 200, { ok: true, data });
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
if (method === "POST" && url.pathname === "/v1/evm/transfer/quote") {
|
|
672
|
+
const body = await withResolvedNetwork(await withResolvedSeed(await readJsonBody(request)));
|
|
673
|
+
const data = await service.quoteNativeTransfer(body);
|
|
674
|
+
return sendJson(response, 200, { ok: true, data });
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
if (method === "POST" && url.pathname === "/v1/evm/transfer/send") {
|
|
678
|
+
const body = await withResolvedNetwork(await withResolvedSeed(await readJsonBody(request)));
|
|
679
|
+
const data = await service.sendNativeTransfer(body);
|
|
680
|
+
return sendJson(response, 200, { ok: true, data });
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
if (method === "POST" && url.pathname === "/v1/evm/token-transfer/quote") {
|
|
684
|
+
const body = await withResolvedNetwork(await withResolvedSeed(await readJsonBody(request)));
|
|
685
|
+
const data = await service.quoteTokenTransfer(body);
|
|
686
|
+
return sendJson(response, 200, { ok: true, data });
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
if (method === "POST" && url.pathname === "/v1/evm/token-transfer/send") {
|
|
690
|
+
const body = await withResolvedNetwork(await withResolvedSeed(await readJsonBody(request)));
|
|
691
|
+
const data = await service.sendTokenTransfer(body);
|
|
692
|
+
return sendJson(response, 200, { ok: true, data });
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
if (method === "POST" && url.pathname === "/v1/evm/x402/exact/sign") {
|
|
696
|
+
const body = await withResolvedNetwork(await withResolvedSeed(await readJsonBody(request)));
|
|
697
|
+
const data = await service.signX402ExactTypedData(body);
|
|
698
|
+
return sendJson(response, 200, { ok: true, data });
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
if (method === "POST" && url.pathname === "/v1/evm/imd/permit2/sign") {
|
|
702
|
+
const body = await withResolvedNetwork(await withResolvedSeed(await readJsonBody(request)));
|
|
703
|
+
const data = await service.signImdPermit2(body);
|
|
704
|
+
return sendJson(response, 200, { ok: true, data });
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
if (method === "POST" && url.pathname === "/v1/evm/imd/quote-approval/sign") {
|
|
708
|
+
const body = await withResolvedNetwork(await withResolvedSeed(await readJsonBody(request)));
|
|
709
|
+
const data = await service.signImdQuoteApproval(body);
|
|
710
|
+
return sendJson(response, 200, { ok: true, data });
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
if (method === "POST" && url.pathname === "/v1/evm/imd/state") {
|
|
714
|
+
const body = await withResolvedNetwork(await withResolvedSeed(await readJsonBody(request)));
|
|
715
|
+
const data = await service.getImdAccountState(body);
|
|
716
|
+
return sendJson(response, 200, { ok: true, data });
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
if (method === "POST" && url.pathname === "/v1/evm/imd/approve/quote") {
|
|
720
|
+
const body = await withResolvedNetwork(await withResolvedSeed(await readJsonBody(request)));
|
|
721
|
+
const data = await service.quoteImdPermit2Approval(body);
|
|
722
|
+
return sendJson(response, 200, { ok: true, data });
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
if (method === "POST" && url.pathname === "/v1/evm/imd/approve/send") {
|
|
726
|
+
const body = await withResolvedNetwork(await withResolvedSeed(await readJsonBody(request)));
|
|
727
|
+
const data = await service.approveImdForPermit2(body);
|
|
728
|
+
return sendJson(response, 200, { ok: true, data });
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
if (method === "POST" && url.pathname === "/v1/evm/message/sign") {
|
|
732
|
+
const body = await withResolvedNetwork(await withResolvedSeed(await readJsonBody(request)));
|
|
733
|
+
const data = await service.signPersonalMessage(body);
|
|
734
|
+
return sendJson(response, 200, { ok: true, data });
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
return notFound(response);
|
|
738
|
+
} catch (error) {
|
|
739
|
+
const shaped = toErrorResponse(error, new URL(request.url || "/", "http://localhost").pathname, 400);
|
|
740
|
+
return sendJson(response, shaped.statusCode, shaped.payload);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
const server = createServer((request, response) => {
|
|
745
|
+
void withTrackedRequest(shutdown, async () => {
|
|
746
|
+
try {
|
|
747
|
+
await handleRequest(request, response);
|
|
748
|
+
} catch (error) {
|
|
749
|
+
const shaped = toErrorResponse(
|
|
750
|
+
error,
|
|
751
|
+
new URL(request.url || "/", "http://localhost").pathname,
|
|
752
|
+
500,
|
|
753
|
+
);
|
|
754
|
+
sendJson(response, shaped.statusCode, shaped.payload);
|
|
755
|
+
}
|
|
756
|
+
});
|
|
757
|
+
});
|
|
758
|
+
|
|
759
|
+
// 8s stays strictly inside the 10s SIGTERM->SIGKILL window the Python client
|
|
760
|
+
// allows (agent_wallet/evm_user_wallets.py), so the orderly path always wins.
|
|
761
|
+
const shutdown = createShutdownCoordinator({
|
|
762
|
+
closeServer: () => {
|
|
763
|
+
server.close();
|
|
764
|
+
if (config.transport === "socket") {
|
|
765
|
+
try {
|
|
766
|
+
fs.unlinkSync(config.socketPath);
|
|
767
|
+
} catch {
|
|
768
|
+
// already gone — nothing to clean up
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
},
|
|
772
|
+
exit: (code) => process.exit(code),
|
|
773
|
+
graceMs: 8000,
|
|
774
|
+
log: (message) => console.log(message),
|
|
775
|
+
});
|
|
776
|
+
|
|
777
|
+
// Named so a test's close() can remove exactly these listeners — startServer()
|
|
778
|
+
// runs multiple times in one process across tests, and anonymous listeners
|
|
779
|
+
// would accumulate on `process` for the life of the test run.
|
|
780
|
+
const onSigterm = () => shutdown.begin("SIGTERM");
|
|
781
|
+
const onSigint = () => shutdown.begin("SIGINT");
|
|
782
|
+
process.on("SIGTERM", onSigterm);
|
|
783
|
+
process.on("SIGINT", onSigint);
|
|
784
|
+
|
|
785
|
+
if (config.transport === "socket" && fs.existsSync(config.socketPath)) {
|
|
786
|
+
// A leftover socket file from an unclean shutdown makes bind() fail even
|
|
787
|
+
// though nothing is listening. Probe it first: a connection error means
|
|
788
|
+
// it's stale and safe to remove; a live response means something else is
|
|
789
|
+
// genuinely already serving this exact dataDir, which the caller's own
|
|
790
|
+
// health check should have caught before ever spawning us.
|
|
791
|
+
const alive = await new Promise((resolve) => {
|
|
792
|
+
const probe = net.connect(config.socketPath);
|
|
793
|
+
probe.once("connect", () => {
|
|
794
|
+
probe.destroy();
|
|
795
|
+
resolve(true);
|
|
796
|
+
});
|
|
797
|
+
probe.once("error", () => resolve(false));
|
|
798
|
+
});
|
|
799
|
+
if (!alive) {
|
|
800
|
+
fs.unlinkSync(config.socketPath);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
return new Promise((resolve) => {
|
|
805
|
+
const onListening = () => {
|
|
806
|
+
if (config.transport === "socket") {
|
|
807
|
+
fs.chmodSync(config.socketPath, 0o600);
|
|
808
|
+
}
|
|
809
|
+
console.log(
|
|
810
|
+
config.transport === "socket"
|
|
811
|
+
? `wdk-evm-wallet listening on ${config.socketPath} (${config.network})`
|
|
812
|
+
: `wdk-evm-wallet listening on ${config.host}:${config.port} (${config.network})`,
|
|
813
|
+
);
|
|
814
|
+
resolve({
|
|
815
|
+
server,
|
|
816
|
+
// Deliberately does NOT go through `shutdown.begin` — that path ends
|
|
817
|
+
// in `exit(code)` (`process.exit`), which is correct for the real
|
|
818
|
+
// SIGTERM/SIGINT handlers below but would kill the whole test
|
|
819
|
+
// process the first time a test calls `close()`. This is a plain
|
|
820
|
+
// close: stop the server, clean up the socket file, done.
|
|
821
|
+
close: () =>
|
|
822
|
+
new Promise((closeResolve) => {
|
|
823
|
+
process.off("SIGTERM", onSigterm);
|
|
824
|
+
process.off("SIGINT", onSigint);
|
|
825
|
+
server.close(() => {
|
|
826
|
+
if (config.transport === "socket") {
|
|
827
|
+
try {
|
|
828
|
+
fs.unlinkSync(config.socketPath);
|
|
829
|
+
} catch {
|
|
830
|
+
// already gone — nothing to clean up
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
closeResolve();
|
|
834
|
+
});
|
|
835
|
+
}),
|
|
836
|
+
});
|
|
837
|
+
};
|
|
838
|
+
server.once("listening", onListening);
|
|
839
|
+
if (config.transport === "tcp") {
|
|
840
|
+
server.listen(config.port, config.host);
|
|
841
|
+
} else {
|
|
842
|
+
server.listen(config.socketPath);
|
|
843
|
+
}
|
|
844
|
+
});
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
848
|
+
startServer();
|
|
849
|
+
}
|