@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,231 @@
|
|
|
1
|
+
// IMD (https://imd.fun) paid actions: the two signatures one payment needs.
|
|
2
|
+
//
|
|
3
|
+
// IMD charges 0.5 IMD per action through x402 v2 `exact` with Permit2, plus an
|
|
4
|
+
// IMD-specific EIP-712 QuoteApproval of that exact payment. Both typed-data
|
|
5
|
+
// shapes are pinned here and never taken from the caller: the caller only
|
|
6
|
+
// supplies the values, and each value is checked against the IMD allowlist
|
|
7
|
+
// before anything is signed.
|
|
8
|
+
|
|
9
|
+
export const IMD_CHAIN_ID = 1;
|
|
10
|
+
export const IMD_TOKEN_ADDRESS = "0xd34a99bc0f67ae1bbd63c660e6d0b0dd03e263b7";
|
|
11
|
+
export const IMD_PAY_TO_ALLOWLIST = Object.freeze(["0x4e0fa57bde726079356537e2f34d671e9f41adbc"]);
|
|
12
|
+
export const IMD_API_ORIGIN = "https://api.imd.fun";
|
|
13
|
+
export const IMD_ACTIONS = Object.freeze([
|
|
14
|
+
"job.open",
|
|
15
|
+
"job.continue",
|
|
16
|
+
"launch.open",
|
|
17
|
+
"workflow.open",
|
|
18
|
+
"oracle.request",
|
|
19
|
+
"schedule.create",
|
|
20
|
+
"schedule.topup",
|
|
21
|
+
]);
|
|
22
|
+
// Hard ceiling for one signed payment (10 actions' worth, or a schedule's runs).
|
|
23
|
+
export const IMD_MAX_PER_PAYMENT_RAW = 5_000_000_000_000_000_000n;
|
|
24
|
+
// IMD quotes live 600s; allow a little clock skew on top.
|
|
25
|
+
export const IMD_MAX_WINDOW_SECONDS = 900;
|
|
26
|
+
|
|
27
|
+
export const PERMIT2_ADDRESS = "0x000000000022D473030F116dDEE9F6B43aC78BA3";
|
|
28
|
+
export const X402_EXACT_PERMIT2_PROXY_ADDRESS = "0x402085c248EeA27D92E8b30b2C58ed07f9E20001";
|
|
29
|
+
|
|
30
|
+
export const PERMIT2_WITNESS_TYPES = Object.freeze({
|
|
31
|
+
PermitWitnessTransferFrom: [
|
|
32
|
+
{ name: "permitted", type: "TokenPermissions" },
|
|
33
|
+
{ name: "spender", type: "address" },
|
|
34
|
+
{ name: "nonce", type: "uint256" },
|
|
35
|
+
{ name: "deadline", type: "uint256" },
|
|
36
|
+
{ name: "witness", type: "Witness" },
|
|
37
|
+
],
|
|
38
|
+
TokenPermissions: [
|
|
39
|
+
{ name: "token", type: "address" },
|
|
40
|
+
{ name: "amount", type: "uint256" },
|
|
41
|
+
],
|
|
42
|
+
Witness: [
|
|
43
|
+
{ name: "to", type: "address" },
|
|
44
|
+
{ name: "validAfter", type: "uint256" },
|
|
45
|
+
],
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
export const IMD_QUOTE_APPROVAL_TYPES = Object.freeze({
|
|
49
|
+
QuoteApproval: [
|
|
50
|
+
{ name: "resource", type: "string" },
|
|
51
|
+
{ name: "requesterScopeHash", type: "bytes32" },
|
|
52
|
+
{ name: "quoteId", type: "string" },
|
|
53
|
+
{ name: "quoteHash", type: "bytes32" },
|
|
54
|
+
{ name: "paymentHash", type: "bytes32" },
|
|
55
|
+
{ name: "action", type: "string" },
|
|
56
|
+
{ name: "asset", type: "address" },
|
|
57
|
+
{ name: "amount", type: "uint256" },
|
|
58
|
+
{ name: "payTo", type: "address" },
|
|
59
|
+
{ name: "expiresAt", type: "uint256" },
|
|
60
|
+
],
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
function imdError(message) {
|
|
64
|
+
const error = new Error(message);
|
|
65
|
+
error.errorCode = "imd_signature_refused";
|
|
66
|
+
return error;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function plainObject(value, fieldName) {
|
|
70
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
71
|
+
throw imdError(`${fieldName} must be an object.`);
|
|
72
|
+
}
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function address(value, fieldName) {
|
|
77
|
+
const text = String(value ?? "").trim();
|
|
78
|
+
if (!/^0x[a-fA-F0-9]{40}$/.test(text)) {
|
|
79
|
+
throw imdError(`${fieldName} must be a 20-byte hex address.`);
|
|
80
|
+
}
|
|
81
|
+
return text;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function sameAddress(left, right) {
|
|
85
|
+
return String(left).toLowerCase() === String(right).toLowerCase();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function uint(value, fieldName) {
|
|
89
|
+
const text = String(value ?? "").trim();
|
|
90
|
+
if (!/^[0-9]+$/.test(text)) {
|
|
91
|
+
throw imdError(`${fieldName} must be a base-10 integer string.`);
|
|
92
|
+
}
|
|
93
|
+
return BigInt(text);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function bytes32(value, fieldName) {
|
|
97
|
+
const text = String(value ?? "").trim();
|
|
98
|
+
if (!/^0x[a-fA-F0-9]{64}$/.test(text)) {
|
|
99
|
+
throw imdError(`${fieldName} must be 0x + 32 bytes of hex.`);
|
|
100
|
+
}
|
|
101
|
+
return text.toLowerCase();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function boundedString(value, fieldName, maxLength) {
|
|
105
|
+
if (typeof value !== "string" || !value.trim() || value.length > maxLength) {
|
|
106
|
+
throw imdError(`${fieldName} must be a non-empty string of at most ${maxLength} characters.`);
|
|
107
|
+
}
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function assertImdChain(runtimeConfig) {
|
|
112
|
+
if (Number(runtimeConfig?.chainId) !== IMD_CHAIN_ID) {
|
|
113
|
+
throw imdError("IMD payments are signed on Ethereum mainnet only (network=ethereum).");
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function assertAmount(amount, fieldName) {
|
|
118
|
+
if (amount <= 0n) {
|
|
119
|
+
throw imdError(`${fieldName} must be greater than zero.`);
|
|
120
|
+
}
|
|
121
|
+
if (amount > IMD_MAX_PER_PAYMENT_RAW) {
|
|
122
|
+
throw imdError(`${fieldName} exceeds the 5 IMD per-payment ceiling.`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function assertPayTo(payTo, fieldName) {
|
|
127
|
+
if (!IMD_PAY_TO_ALLOWLIST.some((allowed) => sameAddress(allowed, payTo))) {
|
|
128
|
+
throw imdError(`${fieldName} is not a known IMD treasury address.`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function assertWindowEnd(value, fieldName, nowSeconds) {
|
|
133
|
+
if (value <= BigInt(nowSeconds)) {
|
|
134
|
+
throw imdError(`${fieldName} is already in the past.`);
|
|
135
|
+
}
|
|
136
|
+
if (value > BigInt(nowSeconds + IMD_MAX_WINDOW_SECONDS)) {
|
|
137
|
+
throw imdError(`${fieldName} is more than ${IMD_MAX_WINDOW_SECONDS}s in the future.`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function buildImdPermit2TypedData(permit, runtimeConfig, nowSeconds = Math.floor(Date.now() / 1000)) {
|
|
142
|
+
assertImdChain(runtimeConfig);
|
|
143
|
+
const source = plainObject(permit, "permit");
|
|
144
|
+
const permitted = plainObject(source.permitted, "permit.permitted");
|
|
145
|
+
const witness = plainObject(source.witness, "permit.witness");
|
|
146
|
+
|
|
147
|
+
const token = address(permitted.token, "permit.permitted.token");
|
|
148
|
+
if (!sameAddress(token, IMD_TOKEN_ADDRESS)) {
|
|
149
|
+
throw imdError("permit.permitted.token must be the IMD token.");
|
|
150
|
+
}
|
|
151
|
+
const amount = uint(permitted.amount, "permit.permitted.amount");
|
|
152
|
+
assertAmount(amount, "permit.permitted.amount");
|
|
153
|
+
const spender = address(source.spender, "permit.spender");
|
|
154
|
+
if (!sameAddress(spender, X402_EXACT_PERMIT2_PROXY_ADDRESS)) {
|
|
155
|
+
throw imdError("permit.spender must be the x402 exact Permit2 proxy.");
|
|
156
|
+
}
|
|
157
|
+
const nonce = uint(source.nonce, "permit.nonce");
|
|
158
|
+
const deadline = uint(source.deadline, "permit.deadline");
|
|
159
|
+
assertWindowEnd(deadline, "permit.deadline", nowSeconds);
|
|
160
|
+
const to = address(witness.to, "permit.witness.to");
|
|
161
|
+
assertPayTo(to, "permit.witness.to");
|
|
162
|
+
const validAfter = uint(witness.validAfter, "permit.witness.validAfter");
|
|
163
|
+
if (validAfter >= deadline) {
|
|
164
|
+
throw imdError("permit.witness.validAfter must be before permit.deadline.");
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return {
|
|
168
|
+
domain: { name: "Permit2", chainId: IMD_CHAIN_ID, verifyingContract: PERMIT2_ADDRESS },
|
|
169
|
+
types: PERMIT2_WITNESS_TYPES,
|
|
170
|
+
primaryType: "PermitWitnessTransferFrom",
|
|
171
|
+
message: {
|
|
172
|
+
permitted: { token, amount: amount.toString() },
|
|
173
|
+
spender,
|
|
174
|
+
nonce: nonce.toString(),
|
|
175
|
+
deadline: deadline.toString(),
|
|
176
|
+
witness: { to, validAfter: validAfter.toString() },
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function buildImdQuoteApprovalTypedData(
|
|
182
|
+
quoteApproval,
|
|
183
|
+
runtimeConfig,
|
|
184
|
+
nowSeconds = Math.floor(Date.now() / 1000)
|
|
185
|
+
) {
|
|
186
|
+
assertImdChain(runtimeConfig);
|
|
187
|
+
const source = plainObject(quoteApproval, "quoteApproval");
|
|
188
|
+
|
|
189
|
+
const resource = boundedString(source.resource, "quoteApproval.resource", 512);
|
|
190
|
+
if (!resource.startsWith(`${IMD_API_ORIGIN}/`)) {
|
|
191
|
+
throw imdError(`quoteApproval.resource must be an ${IMD_API_ORIGIN} URL.`);
|
|
192
|
+
}
|
|
193
|
+
const action = boundedString(source.action, "quoteApproval.action", 64);
|
|
194
|
+
if (!IMD_ACTIONS.includes(action)) {
|
|
195
|
+
throw imdError(`quoteApproval.action '${action}' is not an IMD paid action.`);
|
|
196
|
+
}
|
|
197
|
+
const asset = address(source.asset, "quoteApproval.asset");
|
|
198
|
+
if (!sameAddress(asset, IMD_TOKEN_ADDRESS)) {
|
|
199
|
+
throw imdError("quoteApproval.asset must be the IMD token.");
|
|
200
|
+
}
|
|
201
|
+
const amount = uint(source.amount, "quoteApproval.amount");
|
|
202
|
+
assertAmount(amount, "quoteApproval.amount");
|
|
203
|
+
const payTo = address(source.payTo, "quoteApproval.payTo");
|
|
204
|
+
assertPayTo(payTo, "quoteApproval.payTo");
|
|
205
|
+
const expiresAt = uint(source.expiresAt, "quoteApproval.expiresAt");
|
|
206
|
+
assertWindowEnd(expiresAt, "quoteApproval.expiresAt", nowSeconds);
|
|
207
|
+
|
|
208
|
+
return {
|
|
209
|
+
domain: { name: "IdentityMD Paid Action", version: "1", chainId: IMD_CHAIN_ID },
|
|
210
|
+
types: IMD_QUOTE_APPROVAL_TYPES,
|
|
211
|
+
primaryType: "QuoteApproval",
|
|
212
|
+
message: {
|
|
213
|
+
resource,
|
|
214
|
+
requesterScopeHash: bytes32(source.requesterScopeHash, "quoteApproval.requesterScopeHash"),
|
|
215
|
+
quoteId: boundedString(source.quoteId, "quoteApproval.quoteId", 128),
|
|
216
|
+
quoteHash: bytes32(source.quoteHash, "quoteApproval.quoteHash"),
|
|
217
|
+
paymentHash: bytes32(source.paymentHash, "quoteApproval.paymentHash"),
|
|
218
|
+
action,
|
|
219
|
+
asset,
|
|
220
|
+
amount: amount.toString(),
|
|
221
|
+
payTo,
|
|
222
|
+
expiresAt: expiresAt.toString(),
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function normalizeImdAllowanceAmount(value) {
|
|
228
|
+
const amount = uint(value, "amount");
|
|
229
|
+
assertAmount(amount, "amount");
|
|
230
|
+
return amount;
|
|
231
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export function jsonSafe(value) {
|
|
2
|
+
if (typeof value === "bigint") {
|
|
3
|
+
return value.toString();
|
|
4
|
+
}
|
|
5
|
+
if (Array.isArray(value)) {
|
|
6
|
+
return value.map((item) => jsonSafe(item));
|
|
7
|
+
}
|
|
8
|
+
if (value && typeof value === "object") {
|
|
9
|
+
return Object.fromEntries(
|
|
10
|
+
Object.entries(value).map(([key, item]) => [key, jsonSafe(item)])
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function sendJson(response, statusCode, payload) {
|
|
17
|
+
const body = JSON.stringify(jsonSafe(payload));
|
|
18
|
+
response.writeHead(statusCode, {
|
|
19
|
+
"Content-Type": "application/json",
|
|
20
|
+
"Content-Length": Buffer.byteLength(body),
|
|
21
|
+
});
|
|
22
|
+
response.end(body);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function readJsonBody(request) {
|
|
26
|
+
const chunks = [];
|
|
27
|
+
for await (const chunk of request) {
|
|
28
|
+
chunks.push(chunk);
|
|
29
|
+
}
|
|
30
|
+
const raw = Buffer.concat(chunks).toString("utf8").trim();
|
|
31
|
+
if (!raw) {
|
|
32
|
+
return {};
|
|
33
|
+
}
|
|
34
|
+
return JSON.parse(raw);
|
|
35
|
+
}
|
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
|
|
6
|
+
import WDK from "@tetherto/wdk";
|
|
7
|
+
|
|
8
|
+
const scryptAsync = promisify(crypto.scrypt);
|
|
9
|
+
|
|
10
|
+
const REGISTRY_FILE = "registry.json";
|
|
11
|
+
const WALLETS_DIR = "wallets";
|
|
12
|
+
const VAULT_VERSION = 1;
|
|
13
|
+
|
|
14
|
+
function assertNonEmptyString(value, fieldName) {
|
|
15
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
16
|
+
throw new Error(`${fieldName} is required.`);
|
|
17
|
+
}
|
|
18
|
+
return value.trim();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function assertPositiveInteger(value, fieldName) {
|
|
22
|
+
const parsed = Number(value);
|
|
23
|
+
if (!Number.isInteger(parsed) || parsed <= 0) {
|
|
24
|
+
throw new Error(`${fieldName} must be a positive integer.`);
|
|
25
|
+
}
|
|
26
|
+
return parsed;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function sanitizeLabel(label) {
|
|
30
|
+
const normalized = String(label ?? "").trim();
|
|
31
|
+
return normalized || "EVM Wallet";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function assertValidNetwork(network, fieldName = "network") {
|
|
35
|
+
const normalized = assertNonEmptyString(network, fieldName).toLowerCase();
|
|
36
|
+
if (!["ethereum", "sepolia", "base", "base-sepolia"].includes(normalized)) {
|
|
37
|
+
throw new Error(`${fieldName} must be one of: ethereum, sepolia, base, base-sepolia.`);
|
|
38
|
+
}
|
|
39
|
+
return normalized;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function deriveKey(password, salt) {
|
|
43
|
+
return scryptAsync(password, salt, 32, {
|
|
44
|
+
N: 1 << 15,
|
|
45
|
+
r: 8,
|
|
46
|
+
p: 1,
|
|
47
|
+
maxmem: 64 * 1024 * 1024,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function encryptSeedPhrase({ seedPhrase, password, walletId }) {
|
|
52
|
+
const salt = crypto.randomBytes(16);
|
|
53
|
+
const iv = crypto.randomBytes(12);
|
|
54
|
+
const key = await deriveKey(password, salt);
|
|
55
|
+
const cipher = crypto.createCipheriv("aes-256-gcm", key, iv);
|
|
56
|
+
cipher.setAAD(Buffer.from(`wdk-evm-wallet:${walletId}:v${VAULT_VERSION}`, "utf8"));
|
|
57
|
+
const ciphertext = Buffer.concat([
|
|
58
|
+
cipher.update(Buffer.from(seedPhrase, "utf8")),
|
|
59
|
+
cipher.final(),
|
|
60
|
+
]);
|
|
61
|
+
const tag = cipher.getAuthTag();
|
|
62
|
+
key.fill(0);
|
|
63
|
+
return {
|
|
64
|
+
version: VAULT_VERSION,
|
|
65
|
+
kdf: {
|
|
66
|
+
name: "scrypt",
|
|
67
|
+
salt: salt.toString("base64"),
|
|
68
|
+
N: 1 << 15,
|
|
69
|
+
r: 8,
|
|
70
|
+
p: 1,
|
|
71
|
+
},
|
|
72
|
+
cipher: {
|
|
73
|
+
name: "aes-256-gcm",
|
|
74
|
+
iv: iv.toString("base64"),
|
|
75
|
+
tag: tag.toString("base64"),
|
|
76
|
+
},
|
|
77
|
+
ciphertext: ciphertext.toString("base64"),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function decryptSeedPhrase({ encrypted, password, walletId }) {
|
|
82
|
+
const salt = Buffer.from(encrypted.kdf.salt, "base64");
|
|
83
|
+
const iv = Buffer.from(encrypted.cipher.iv, "base64");
|
|
84
|
+
const tag = Buffer.from(encrypted.cipher.tag, "base64");
|
|
85
|
+
const ciphertext = Buffer.from(encrypted.ciphertext, "base64");
|
|
86
|
+
const key = await deriveKey(password, salt);
|
|
87
|
+
const decipher = crypto.createDecipheriv("aes-256-gcm", key, iv);
|
|
88
|
+
decipher.setAAD(Buffer.from(`wdk-evm-wallet:${walletId}:v${VAULT_VERSION}`, "utf8"));
|
|
89
|
+
decipher.setAuthTag(tag);
|
|
90
|
+
const plaintext = Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
91
|
+
key.fill(0);
|
|
92
|
+
// The returned string is an unavoidable transient (V8 strings are immutable);
|
|
93
|
+
// the derived key and plaintext buffers are zeroized so no zeroizable secret lingers.
|
|
94
|
+
const seedPhrase = plaintext.toString("utf8");
|
|
95
|
+
plaintext.fill(0);
|
|
96
|
+
return seedPhrase;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function decryptSeedPhraseWithPasswordCheck(args) {
|
|
100
|
+
try {
|
|
101
|
+
return await decryptSeedPhrase(args);
|
|
102
|
+
} catch (error) {
|
|
103
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
104
|
+
if (
|
|
105
|
+
message.includes("authenticate data") ||
|
|
106
|
+
message.includes("unable to authenticate") ||
|
|
107
|
+
message.includes("Unsupported state")
|
|
108
|
+
) {
|
|
109
|
+
throw new Error("Invalid password.");
|
|
110
|
+
}
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export class LocalEvmVault {
|
|
116
|
+
constructor(config) {
|
|
117
|
+
this.config = config;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async createWallet({
|
|
121
|
+
label = "",
|
|
122
|
+
password,
|
|
123
|
+
words = 12,
|
|
124
|
+
revealSeedPhrase = false,
|
|
125
|
+
network,
|
|
126
|
+
}) {
|
|
127
|
+
const count = assertPositiveInteger(words, "words");
|
|
128
|
+
if (count !== 12) {
|
|
129
|
+
throw new Error("Only 12-word wallet creation is currently supported.");
|
|
130
|
+
}
|
|
131
|
+
const seedPhrase = WDK.getRandomSeedPhrase();
|
|
132
|
+
const wallet = await this.#storeWallet({
|
|
133
|
+
label,
|
|
134
|
+
password,
|
|
135
|
+
seedPhrase,
|
|
136
|
+
source: "created",
|
|
137
|
+
network,
|
|
138
|
+
});
|
|
139
|
+
return {
|
|
140
|
+
...wallet,
|
|
141
|
+
unlocked: false,
|
|
142
|
+
unlockExpiresAt: null,
|
|
143
|
+
...(revealSeedPhrase ? { seedPhrase } : {}),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async importWallet({ label = "", password, seedPhrase, network }) {
|
|
148
|
+
const mnemonic = assertNonEmptyString(seedPhrase, "seedPhrase");
|
|
149
|
+
if (!WDK.isValidSeed(mnemonic)) {
|
|
150
|
+
throw new Error("seedPhrase must be a valid BIP-39 seed phrase.");
|
|
151
|
+
}
|
|
152
|
+
const wallet = await this.#storeWallet({
|
|
153
|
+
label,
|
|
154
|
+
password,
|
|
155
|
+
seedPhrase: mnemonic,
|
|
156
|
+
source: "imported",
|
|
157
|
+
network,
|
|
158
|
+
});
|
|
159
|
+
return {
|
|
160
|
+
...wallet,
|
|
161
|
+
unlocked: false,
|
|
162
|
+
unlockExpiresAt: null,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async listWallets() {
|
|
167
|
+
const registry = await this.#loadRegistry();
|
|
168
|
+
return registry.wallets.map((wallet) => ({
|
|
169
|
+
...wallet,
|
|
170
|
+
unlocked: false,
|
|
171
|
+
unlockExpiresAt: null,
|
|
172
|
+
}));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async getWallet({ walletId }) {
|
|
176
|
+
const wallet = await this.#getWalletMetadata(assertNonEmptyString(walletId, "walletId"));
|
|
177
|
+
return {
|
|
178
|
+
...wallet,
|
|
179
|
+
unlocked: false,
|
|
180
|
+
unlockExpiresAt: null,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Deprecated: the wallet now uses a decrypt-on-demand model and never holds a
|
|
185
|
+
// plaintext seed in memory between requests. This endpoint only verifies the
|
|
186
|
+
// password so callers get feedback; it does not persist any unlocked state.
|
|
187
|
+
async unlockWallet({ walletId, password }) {
|
|
188
|
+
const metadata = await this.#getWalletMetadata(assertNonEmptyString(walletId, "walletId"));
|
|
189
|
+
const encrypted = await this.#loadEncryptedWallet(walletId);
|
|
190
|
+
const secret = await decryptSeedPhraseWithPasswordCheck({
|
|
191
|
+
encrypted,
|
|
192
|
+
password: assertNonEmptyString(password, "password"),
|
|
193
|
+
walletId,
|
|
194
|
+
});
|
|
195
|
+
if (!WDK.isValidSeed(secret)) {
|
|
196
|
+
throw new Error("Decrypted wallet seed phrase is invalid.");
|
|
197
|
+
}
|
|
198
|
+
return {
|
|
199
|
+
walletId,
|
|
200
|
+
label: metadata.label,
|
|
201
|
+
unlocked: false,
|
|
202
|
+
unlockExpiresAt: null,
|
|
203
|
+
deprecated: true,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Deprecated no-op: the wallet is always sealed at rest in the decrypt-on-demand model.
|
|
208
|
+
async lockWallet({ walletId }) {
|
|
209
|
+
return {
|
|
210
|
+
walletId: assertNonEmptyString(walletId, "walletId"),
|
|
211
|
+
unlocked: false,
|
|
212
|
+
deprecated: true,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async revealSeedPhrase({ walletId, password }) {
|
|
217
|
+
const id = assertNonEmptyString(walletId, "walletId");
|
|
218
|
+
const metadata = await this.#getWalletMetadata(id);
|
|
219
|
+
const encrypted = await this.#loadEncryptedWallet(id);
|
|
220
|
+
const seedPhrase = await decryptSeedPhraseWithPasswordCheck({
|
|
221
|
+
encrypted,
|
|
222
|
+
password: assertNonEmptyString(password, "password"),
|
|
223
|
+
walletId: id,
|
|
224
|
+
});
|
|
225
|
+
if (!WDK.isValidSeed(seedPhrase)) {
|
|
226
|
+
throw new Error("Decrypted wallet seed phrase is invalid.");
|
|
227
|
+
}
|
|
228
|
+
return {
|
|
229
|
+
walletId: id,
|
|
230
|
+
label: metadata.label,
|
|
231
|
+
seedPhrase,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async changePassword({ walletId, currentPassword, newPassword }) {
|
|
236
|
+
const id = assertNonEmptyString(walletId, "walletId");
|
|
237
|
+
const safeCurrentPassword = assertNonEmptyString(currentPassword, "currentPassword");
|
|
238
|
+
const safeNewPassword = assertNonEmptyString(newPassword, "newPassword");
|
|
239
|
+
const metadata = await this.#getWalletMetadata(id);
|
|
240
|
+
const encrypted = await this.#loadEncryptedWallet(id);
|
|
241
|
+
const seedPhrase = await decryptSeedPhraseWithPasswordCheck({
|
|
242
|
+
encrypted,
|
|
243
|
+
password: safeCurrentPassword,
|
|
244
|
+
walletId: id,
|
|
245
|
+
});
|
|
246
|
+
if (!WDK.isValidSeed(seedPhrase)) {
|
|
247
|
+
throw new Error("Decrypted wallet seed phrase is invalid.");
|
|
248
|
+
}
|
|
249
|
+
const reencrypted = await encryptSeedPhrase({
|
|
250
|
+
seedPhrase,
|
|
251
|
+
password: safeNewPassword,
|
|
252
|
+
walletId: id,
|
|
253
|
+
});
|
|
254
|
+
await fs.writeFile(this.#walletFilePath(id), JSON.stringify(reencrypted, null, 2), {
|
|
255
|
+
encoding: "utf8",
|
|
256
|
+
mode: 0o600,
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
const registry = await this.#loadRegistry();
|
|
260
|
+
const index = registry.wallets.findIndex((wallet) => wallet.walletId === id);
|
|
261
|
+
if (index === -1) {
|
|
262
|
+
throw new Error(`Unknown walletId: ${id}`);
|
|
263
|
+
}
|
|
264
|
+
const updatedAt = new Date().toISOString();
|
|
265
|
+
registry.wallets[index] = {
|
|
266
|
+
...registry.wallets[index],
|
|
267
|
+
updatedAt,
|
|
268
|
+
};
|
|
269
|
+
await this.#saveRegistry(registry);
|
|
270
|
+
|
|
271
|
+
return {
|
|
272
|
+
walletId: id,
|
|
273
|
+
label: metadata.label,
|
|
274
|
+
passwordChanged: true,
|
|
275
|
+
updatedAt,
|
|
276
|
+
unlocked: false,
|
|
277
|
+
unlockExpiresAt: null,
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Decrypt-on-demand: the seed is decrypted just-in-time for a single signing
|
|
282
|
+
// request from the supplied password, never persisted between requests.
|
|
283
|
+
async resolveSeedPhrase({ walletId, seedPhrase, password }) {
|
|
284
|
+
if (typeof seedPhrase === "string" && seedPhrase.trim()) {
|
|
285
|
+
if (!WDK.isValidSeed(seedPhrase.trim())) {
|
|
286
|
+
throw new Error("seedPhrase must be a valid BIP-39 seed phrase.");
|
|
287
|
+
}
|
|
288
|
+
return {
|
|
289
|
+
seedPhrase: seedPhrase.trim(),
|
|
290
|
+
source: "request",
|
|
291
|
+
walletId: null,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
const id = assertNonEmptyString(walletId, "walletId");
|
|
295
|
+
if (typeof password !== "string" || !password.trim()) {
|
|
296
|
+
throw new Error("Wallet is locked. Provide password or seedPhrase explicitly.");
|
|
297
|
+
}
|
|
298
|
+
await this.#getWalletMetadata(id);
|
|
299
|
+
const encrypted = await this.#loadEncryptedWallet(id);
|
|
300
|
+
const secret = await decryptSeedPhraseWithPasswordCheck({
|
|
301
|
+
encrypted,
|
|
302
|
+
password: password.trim(),
|
|
303
|
+
walletId: id,
|
|
304
|
+
});
|
|
305
|
+
if (!WDK.isValidSeed(secret)) {
|
|
306
|
+
throw new Error("Decrypted wallet seed phrase is invalid.");
|
|
307
|
+
}
|
|
308
|
+
return {
|
|
309
|
+
seedPhrase: secret,
|
|
310
|
+
source: "local-vault-jit",
|
|
311
|
+
walletId: id,
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
async #storeWallet({ label, password, seedPhrase, source, network }) {
|
|
316
|
+
const safePassword = assertNonEmptyString(password, "password");
|
|
317
|
+
await this.#ensureLayout();
|
|
318
|
+
|
|
319
|
+
const walletId = crypto.randomUUID();
|
|
320
|
+
const now = new Date().toISOString();
|
|
321
|
+
const encrypted = await encryptSeedPhrase({
|
|
322
|
+
seedPhrase,
|
|
323
|
+
password: safePassword,
|
|
324
|
+
walletId,
|
|
325
|
+
});
|
|
326
|
+
const entry = {
|
|
327
|
+
walletId,
|
|
328
|
+
label: sanitizeLabel(label),
|
|
329
|
+
createdAt: now,
|
|
330
|
+
updatedAt: now,
|
|
331
|
+
network: assertValidNetwork(network ?? this.config.network, "network"),
|
|
332
|
+
source,
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
await fs.writeFile(this.#walletFilePath(walletId), JSON.stringify(encrypted, null, 2), {
|
|
336
|
+
encoding: "utf8",
|
|
337
|
+
mode: 0o600,
|
|
338
|
+
});
|
|
339
|
+
const registry = await this.#loadRegistry();
|
|
340
|
+
registry.wallets.push(entry);
|
|
341
|
+
await this.#saveRegistry(registry);
|
|
342
|
+
|
|
343
|
+
return {
|
|
344
|
+
walletId,
|
|
345
|
+
label: entry.label,
|
|
346
|
+
createdAt: entry.createdAt,
|
|
347
|
+
updatedAt: entry.updatedAt,
|
|
348
|
+
network: entry.network,
|
|
349
|
+
source: entry.source,
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
async #getWalletMetadata(walletId) {
|
|
354
|
+
const registry = await this.#loadRegistry();
|
|
355
|
+
const wallet = registry.wallets.find((item) => item.walletId === walletId);
|
|
356
|
+
if (!wallet) {
|
|
357
|
+
throw new Error(`Unknown walletId: ${walletId}`);
|
|
358
|
+
}
|
|
359
|
+
return wallet;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
async #loadEncryptedWallet(walletId) {
|
|
363
|
+
const raw = await fs.readFile(this.#walletFilePath(walletId), "utf8");
|
|
364
|
+
return JSON.parse(raw);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
async #ensureLayout() {
|
|
368
|
+
await fs.mkdir(this.config.dataDir, { recursive: true, mode: 0o700 });
|
|
369
|
+
await fs.mkdir(path.join(this.config.dataDir, WALLETS_DIR), {
|
|
370
|
+
recursive: true,
|
|
371
|
+
mode: 0o700,
|
|
372
|
+
});
|
|
373
|
+
try {
|
|
374
|
+
await fs.access(this.#registryPath());
|
|
375
|
+
} catch {
|
|
376
|
+
await this.#saveRegistry({
|
|
377
|
+
version: VAULT_VERSION,
|
|
378
|
+
wallets: [],
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
async #loadRegistry() {
|
|
384
|
+
await this.#ensureLayout();
|
|
385
|
+
const raw = await fs.readFile(this.#registryPath(), "utf8");
|
|
386
|
+
const parsed = JSON.parse(raw);
|
|
387
|
+
if (!Array.isArray(parsed.wallets)) {
|
|
388
|
+
throw new Error("Vault registry is invalid.");
|
|
389
|
+
}
|
|
390
|
+
return parsed;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
async #saveRegistry(registry) {
|
|
394
|
+
await fs.writeFile(this.#registryPath(), JSON.stringify(registry, null, 2), {
|
|
395
|
+
encoding: "utf8",
|
|
396
|
+
mode: 0o600,
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
#walletFilePath(walletId) {
|
|
401
|
+
return path.join(this.config.dataDir, WALLETS_DIR, `${walletId}.json`);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
#registryPath() {
|
|
405
|
+
return path.join(this.config.dataDir, REGISTRY_FILE);
|
|
406
|
+
}
|
|
407
|
+
}
|