@private.me/xbind 3.0.0 → 3.0.2
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/README.md +55 -7
- package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1920 -1
- package/dist-standalone/_deps/shared/cjs/errors.js +729 -1
- package/dist-standalone/_deps/shared/cjs/index.js +463 -1
- package/dist-standalone/_deps/shared/cjs/types.js +315 -1
- package/dist-standalone/_deps/shared/errors.js +244 -1
- package/dist-standalone/_deps/shared/index.js +72 -1
- package/dist-standalone/_deps/shared/types.js +86 -1
- package/dist-standalone/_deps/ux-helpers/cjs/errors.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/index.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/progress.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/search.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/types.js +1 -1
- package/dist-standalone/_deps/ux-helpers/errors.js +1 -1
- package/dist-standalone/_deps/ux-helpers/index.js +1 -1
- package/dist-standalone/_deps/ux-helpers/pagination.js +1 -1
- package/dist-standalone/_deps/ux-helpers/progress.js +1 -1
- package/dist-standalone/_deps/ux-helpers/search.js +1 -1
- package/dist-standalone/_deps/xchange/auto-accept.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/auto-accept.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/errors.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/index.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/invite-client.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/lazy-init.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/trust-integration.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/xchange.js +1 -1
- package/dist-standalone/_deps/xchange/errors.js +1 -1
- package/dist-standalone/_deps/xchange/index.js +1 -1
- package/dist-standalone/_deps/xchange/invite-client.js +1 -1
- package/dist-standalone/_deps/xchange/lazy-init.js +1 -1
- package/dist-standalone/_deps/xchange/trust-integration.js +1 -1
- package/dist-standalone/_deps/xchange/xchange.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/discovery.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/errors.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/index.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/registry.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/schema.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/types.js +1 -1
- package/dist-standalone/_deps/xregistry/discovery.js +1 -1
- package/dist-standalone/_deps/xregistry/errors.js +1 -1
- package/dist-standalone/_deps/xregistry/index.js +1 -1
- package/dist-standalone/_deps/xregistry/registry.js +1 -1
- package/dist-standalone/_deps/xregistry/schema.js +1 -1
- package/dist-standalone/_deps/xregistry/types.js +1 -1
- package/dist-standalone/agent-call.js +659 -1
- package/dist-standalone/agent-sdk.js +328 -1
- package/dist-standalone/agent.js +1800 -1
- package/dist-standalone/approval.js +193 -1
- package/dist-standalone/async-iterators.js +382 -1
- package/dist-standalone/auth.js +219 -1
- package/dist-standalone/auto-accept.js +229 -1
- package/dist-standalone/backup-config.js +201 -1
- package/dist-standalone/backup.js +326 -1
- package/dist-standalone/batch-operations.js +388 -1
- package/dist-standalone/cancellation.js +477 -1
- package/dist-standalone/checkpoint.js +186 -1
- package/dist-standalone/circuit-breaker.js +468 -1
- package/dist-standalone/cjs/agent-call.js +701 -1
- package/dist-standalone/cjs/agent-sdk.js +332 -1
- package/dist-standalone/cjs/agent.js +1837 -1
- package/dist-standalone/cjs/approval.js +199 -1
- package/dist-standalone/cjs/async-iterators.js +392 -1
- package/dist-standalone/cjs/auth.js +225 -1
- package/dist-standalone/cjs/auto-accept.js +233 -1
- package/dist-standalone/cjs/backup-config.js +207 -1
- package/dist-standalone/cjs/backup.js +330 -1
- package/dist-standalone/cjs/batch-operations.js +397 -1
- package/dist-standalone/cjs/cancellation.js +490 -1
- package/dist-standalone/cjs/checkpoint.js +193 -1
- package/dist-standalone/cjs/circuit-breaker.js +476 -1
- package/dist-standalone/cjs/cli/init.js +492 -1
- package/dist-standalone/cjs/config-validation.js +522 -1
- package/dist-standalone/cjs/connect.js +312 -1
- package/dist-standalone/cjs/connection-pool.js +506 -1
- package/dist-standalone/cjs/correlation-id.js +339 -1
- package/dist-standalone/cjs/crypto-utils.js +176 -1
- package/dist-standalone/cjs/debug-mode.js +534 -1
- package/dist-standalone/cjs/did-document.js +101 -1
- package/dist-standalone/cjs/did-privateme.js +130 -1
- package/dist-standalone/cjs/did-web.js +201 -1
- package/dist-standalone/cjs/discovery.js +462 -1
- package/dist-standalone/cjs/dual-mode.js +251 -1
- package/dist-standalone/cjs/email-templates.js +313 -1
- package/dist-standalone/cjs/email-transport.js +239 -1
- package/dist-standalone/cjs/envelope.js +538 -1
- package/dist-standalone/cjs/errors.js +913 -1
- package/dist-standalone/cjs/event-emitter.js +461 -1
- package/dist-standalone/cjs/gateway-state.js +55 -1
- package/dist-standalone/cjs/gateway-transport.js +120 -1
- package/dist-standalone/cjs/graceful-degradation.js +403 -1
- package/dist-standalone/cjs/guardrails.js +223 -1
- package/dist-standalone/cjs/health-check.js +336 -1
- package/dist-standalone/cjs/http-compat.js +272 -1
- package/dist-standalone/cjs/http-status-map.js +571 -1
- package/dist-standalone/cjs/identity.js +645 -1
- package/dist-standalone/cjs/index.js +406 -1
- package/dist-standalone/cjs/invitation.js +421 -1
- package/dist-standalone/cjs/invite.js +328 -1
- package/dist-standalone/cjs/key-agreement.js +335 -1
- package/dist-standalone/cjs/lazy-init.js +300 -1
- package/dist-standalone/cjs/logger.js +291 -1
- package/dist-standalone/cjs/mdns-discovery.js +202 -1
- package/dist-standalone/cjs/nonce-store.js +80 -1
- package/dist-standalone/cjs/pairing-manager.js +223 -1
- package/dist-standalone/cjs/plugin-system.js +264 -1
- package/dist-standalone/cjs/plugins/logging.js +168 -1
- package/dist-standalone/cjs/plugins/metrics.js +181 -1
- package/dist-standalone/cjs/plugins/validation.js +302 -1
- package/dist-standalone/cjs/policy.js +320 -1
- package/dist-standalone/cjs/progress-callbacks.js +583 -1
- package/dist-standalone/cjs/redis-nonce-store.js +76 -1
- package/dist-standalone/cjs/registry-middleware.js +50 -1
- package/dist-standalone/cjs/retry-strategies.js +544 -1
- package/dist-standalone/cjs/retry-transport.js +102 -1
- package/dist-standalone/cjs/runtime/browser.js +533 -1
- package/dist-standalone/cjs/runtime/edge.js +526 -1
- package/dist-standalone/cjs/runtime/react-native.js +394 -1
- package/dist-standalone/cjs/security-policy.js +245 -1
- package/dist-standalone/cjs/serialization.js +1040 -1
- package/dist-standalone/cjs/split-channel.js +225 -1
- package/dist-standalone/cjs/subscription-proof.js +230 -1
- package/dist-standalone/cjs/succession.js +148 -1
- package/dist-standalone/cjs/timeouts.js +412 -1
- package/dist-standalone/cjs/trace-context.js +424 -1
- package/dist-standalone/cjs/trace-spans.js +495 -1
- package/dist-standalone/cjs/transport.js +63 -1
- package/dist-standalone/cjs/trust-registry.js +991 -1
- package/dist-standalone/cjs/types/error-response.js +56 -1
- package/dist-standalone/cjs/vault-auth.js +178 -1
- package/dist-standalone/cjs/vault-store-loader.js +194 -1
- package/dist-standalone/cjs/verify.js +25 -1
- package/dist-standalone/cjs/version-info.js +543 -1
- package/dist-standalone/cjs/xfetch.js +340 -1
- package/dist-standalone/cli/init.js +455 -1
- package/dist-standalone/cli/setup.js +514 -1
- package/dist-standalone/cli/types.js +27 -1
- package/dist-standalone/cli/xbind.js +148 -1
- package/dist-standalone/config-validation.js +513 -1
- package/dist-standalone/connect.js +274 -1
- package/dist-standalone/connection-pool.js +500 -1
- package/dist-standalone/correlation-id.js +326 -1
- package/dist-standalone/crypto-utils.js +157 -1
- package/dist-standalone/debug-mode.js +510 -1
- package/dist-standalone/did-document.js +96 -1
- package/dist-standalone/did-privateme.js +121 -1
- package/dist-standalone/did-web.js +196 -1
- package/dist-standalone/discovery.js +458 -1
- package/dist-standalone/dual-mode.js +247 -1
- package/dist-standalone/email-templates.js +309 -1
- package/dist-standalone/email-transport.js +232 -1
- package/dist-standalone/envelope.js +525 -1
- package/dist-standalone/errors.js +896 -1
- package/dist-standalone/event-emitter.js +456 -1
- package/dist-standalone/gateway-state.js +51 -1
- package/dist-standalone/gateway-transport.js +116 -1
- package/dist-standalone/graceful-degradation.js +396 -1
- package/dist-standalone/guardrails.js +216 -1
- package/dist-standalone/health-check.js +332 -1
- package/dist-standalone/http-compat.js +267 -1
- package/dist-standalone/http-status-map.js +561 -1
- package/dist-standalone/identity.js +619 -1
- package/dist-standalone/index.js +78 -1
- package/dist-standalone/invitation.js +415 -1
- package/dist-standalone/invite.js +324 -1
- package/dist-standalone/key-agreement.js +325 -1
- package/dist-standalone/lazy-init.js +295 -1
- package/dist-standalone/logger.js +285 -1
- package/dist-standalone/mdns-discovery.js +195 -1
- package/dist-standalone/nonce-store.js +76 -1
- package/dist-standalone/pairing-manager.js +219 -1
- package/dist-standalone/plugin-system.js +257 -1
- package/dist-standalone/plugins/logging.d.ts +84 -0
- package/dist-standalone/plugins/logging.js +163 -0
- package/dist-standalone/plugins/metrics.d.ts +111 -0
- package/dist-standalone/plugins/metrics.js +176 -0
- package/dist-standalone/plugins/validation.d.ts +104 -0
- package/dist-standalone/plugins/validation.js +297 -0
- package/dist-standalone/policy.js +315 -1
- package/dist-standalone/progress-callbacks.js +576 -1
- package/dist-standalone/redis-nonce-store.js +72 -1
- package/dist-standalone/registry-middleware.js +47 -1
- package/dist-standalone/retry-strategies.js +534 -1
- package/dist-standalone/retry-transport.js +98 -1
- package/dist-standalone/runtime/browser.d.ts +311 -0
- package/dist-standalone/runtime/browser.js +516 -0
- package/dist-standalone/runtime/edge.d.ts +282 -0
- package/dist-standalone/runtime/edge.js +511 -0
- package/dist-standalone/runtime/react-native.d.ts +157 -0
- package/dist-standalone/runtime/react-native.js +383 -0
- package/dist-standalone/security-policy.js +239 -1
- package/dist-standalone/serialization.js +1031 -1
- package/dist-standalone/split-channel.js +219 -1
- package/dist-standalone/subscription-proof.js +224 -1
- package/dist-standalone/succession.js +142 -1
- package/dist-standalone/timeouts.js +398 -1
- package/dist-standalone/trace-context.js +414 -1
- package/dist-standalone/trace-spans.js +488 -1
- package/dist-standalone/transport.js +59 -1
- package/dist-standalone/trust-registry.js +950 -1
- package/dist-standalone/types/error-response.d.ts +209 -0
- package/dist-standalone/types/error-response.js +52 -0
- package/dist-standalone/vault-auth.js +174 -1
- package/dist-standalone/vault-store-loader.js +187 -1
- package/dist-standalone/verify.js +16 -1
- package/dist-standalone/version-info.js +530 -1
- package/dist-standalone/xfetch.js +335 -1
- package/package.json +4 -10
- package/share1.dat +0 -0
- package/dist-standalone/_deps/mldsa-wasm/LICENSE +0 -24
- package/dist-standalone/_deps/mldsa-wasm/package.json +0 -46
- package/dist-standalone/_deps/shared/cjs/package.json +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/package.json +0 -1
- package/dist-standalone/_deps/xchange/cjs/package.json +0 -1
- package/dist-standalone/_deps/xregistry/cjs/package.json +0 -1
- package/dist-standalone/cjs/package.json +0 -3
- package/dist-standalone/package.json +0 -10
|
@@ -1 +1,513 @@
|
|
|
1
|
-
import{ok,err}from"./_deps/shared/index.js";import{XBindError}from"./errors.js";export class ConfigValidationError extends XBindError{field;expected;actual;fix;constructor(t,e,r,i){super("CONFIG_INVALID",`Configuration validation failed for '${t}': Expected ${e}, got ${"object"==typeof r&&null!==r?JSON.stringify(r):String(r)}. ${i}`,"https://private.me/docs/xbind/api/configuration"),this.name="ConfigValidationError",this.field=t,this.expected=e,this.actual=r,this.fix=i}}export const AGENT_OPTIONS_DEFAULTS={identity:"persistent",identityTTL:36e5,postQuantumSig:!1};export const AGENT_CREATE_OPTIONS_DEFAULTS={timestampWindowMs:3e4,postQuantumSig:!1,xchange:!1,scopes:[]};export function validateAgentOptions(t){if(!t||"object"!=typeof t)return err(new ConfigValidationError("options","object",t,"Provide a configuration object with at least one option."));const e=t,r=[],i=[];if(void 0!==e.identity&&("string"!=typeof e.identity?r.push(new ConfigValidationError("identity","'persistent' | 'ephemeral'",e.identity,"Use 'persistent' for long-lived DIDs or 'ephemeral' for short-lived DIDs.")):"persistent"!==e.identity&&"ephemeral"!==e.identity&&r.push(new ConfigValidationError("identity","'persistent' | 'ephemeral'",e.identity,"Invalid identity mode. Use 'persistent' or 'ephemeral'."))),void 0!==e.identityTTL&&("number"!=typeof e.identityTTL?r.push(new ConfigValidationError("identityTTL","number (milliseconds)",e.identityTTL,"Provide TTL as a number in milliseconds (e.g., 3600000 for 1 hour).")):e.identityTTL<=0?r.push(new ConfigValidationError("identityTTL","positive number",e.identityTTL,"TTL must be greater than 0. Use at least 60000 (1 minute).")):e.identityTTL<6e4?i.push(`identityTTL is very short (${e.identityTTL}ms). Consider using at least 60000ms (1 minute) to avoid frequent re-registrations.`):e.identityTTL>864e5&&i.push(`identityTTL is very long (${e.identityTTL}ms). Consider using 'persistent' identity mode instead for long-lived agents.`),"persistent"===e.identity&&i.push("identityTTL is ignored for 'persistent' identity mode. Remove this option or switch to 'ephemeral' mode.")),void 0!==e.registry){const t="string"==typeof e.registry,i="object"==typeof e.registry&&null!==e.registry;if(t||i){if(t){const t=e.registry;try{const e=new URL(t);"http:"!==e.protocol&&"https:"!==e.protocol&&r.push(new ConfigValidationError("registry","HTTP(S) URL",t,"Registry URL must use HTTP or HTTPS protocol."))}catch{r.push(new ConfigValidationError("registry","valid URL",t,'Provide a valid URL (e.g., "https://private.me/registry").'))}}else if(i){const t=e.registry,i=["register","resolve"].filter(e=>!(e in t)||"function"!=typeof t[e]);i.length>0&&r.push(new ConfigValidationError("registry","TrustRegistry instance","object missing required methods",`TrustRegistry must implement ${i.join(", ")} method(s). Use HttpTrustRegistry or MemoryTrustRegistry.`))}}else r.push(new ConfigValidationError("registry","TrustRegistry instance or URL string",e.registry,'Provide either a registry URL (e.g., "https://private.me/registry") or a TrustRegistry instance.'))}if(void 0!==e.transport){const t=Array.isArray(e.transport);if("object"==typeof e.transport&&null!==e.transport&&!t||t)if(t){const t=e.transport;0===t.length&&r.push(new ConfigValidationError("transport","non-empty array",t,"Provide at least one transport adapter.")),t.forEach((t,e)=>{if("object"!=typeof t||null===t)r.push(new ConfigValidationError(`transport[${e}]`,"XailTransportAdapter",t,"Each transport must be a XailTransportAdapter instance."));else{"send"in t&&"function"==typeof t.send||r.push(new ConfigValidationError(`transport[${e}]`,"XailTransportAdapter with send() method","object missing send() method","Transport must implement send() method. Use HttpsTransportAdapter or custom adapter."))}})}else{const t=e.transport;"send"in t&&"function"==typeof t.send||r.push(new ConfigValidationError("transport","XailTransportAdapter with send() method","object missing send() method","Transport must implement send() method. Use HttpsTransportAdapter or custom adapter."))}else r.push(new ConfigValidationError("transport","XailTransportAdapter or XailTransportAdapter[]",e.transport,"Provide a single transport adapter or an array of adapters for split-channel security."))}if(void 0!==e.securityPolicy)if("object"!=typeof e.securityPolicy||null===e.securityPolicy)r.push(new ConfigValidationError("securityPolicy","SecurityPolicy instance",e.securityPolicy,"Provide a SecurityPolicy instance (e.g., DefaultSecurityPolicy or custom policy)."));else{const t=e.securityPolicy;"classify"in t&&"function"==typeof t.classify||r.push(new ConfigValidationError("securityPolicy","SecurityPolicy with classify() method","object missing classify() method","SecurityPolicy must implement classify() method. Use DefaultSecurityPolicy."))}if(void 0!==e.postQuantumSig&&"boolean"!=typeof e.postQuantumSig&&r.push(new ConfigValidationError("postQuantumSig","boolean",e.postQuantumSig,"Use true to enable ML-DSA-65 post-quantum signatures, or false to disable.")),void 0!==e.backupConfig)if("object"!=typeof e.backupConfig||null===e.backupConfig)r.push(new ConfigValidationError("backupConfig","BackupConfig object",e.backupConfig,"Provide a BackupConfig object with threshold (k) and totalShares (n) properties."));else{const t=e.backupConfig;"number"!=typeof t.threshold?r.push(new ConfigValidationError("backupConfig.threshold","number",t.threshold,"Threshold (k) must be a number representing shares needed for reconstruction.")):t.threshold<2&&r.push(new ConfigValidationError("backupConfig.threshold","number >= 2",t.threshold,"Threshold must be at least 2 for meaningful splitting.")),"number"!=typeof t.totalShares?r.push(new ConfigValidationError("backupConfig.totalShares","number",t.totalShares,"totalShares (n) must be a number representing total shares to create.")):t.totalShares<2&&r.push(new ConfigValidationError("backupConfig.totalShares","number >= 2",t.totalShares,"totalShares must be at least 2 for splitting.")),"number"==typeof t.threshold&&"number"==typeof t.totalShares&&t.threshold>t.totalShares&&r.push(new ConfigValidationError("backupConfig","threshold <= totalShares",`threshold=${t.threshold}, totalShares=${t.totalShares}`,`Threshold (${t.threshold}) cannot exceed totalShares (${t.totalShares}). Use threshold <= totalShares.`))}return r.length>0?err(r[0]):ok(e)}export function validateAgentCreateOptions(t){if(!t||"object"!=typeof t)return err(new ConfigValidationError("options","object",t,"Provide a configuration object with required fields: name, registry, transport."));const e=t,r=[];if(void 0===e.name?r.push(new ConfigValidationError("name","string (required)",void 0,'Provide a display name for the agent (e.g., "my-agent").')):"string"!=typeof e.name?r.push(new ConfigValidationError("name","string",e.name,"Agent name must be a string.")):0===e.name.trim().length?r.push(new ConfigValidationError("name","non-empty string",e.name,"Agent name cannot be empty or whitespace only.")):e.name.length>255&&r.push(new ConfigValidationError("name","string with length <= 255",e.name,`Agent name is too long (${e.name.length} characters). Use 255 characters or less.`)),void 0===e.registry)r.push(new ConfigValidationError("registry","TrustRegistry (required)",void 0,"Provide a TrustRegistry instance (e.g., new MemoryTrustRegistry() or new HttpTrustRegistry())."));else if("object"!=typeof e.registry||null===e.registry)r.push(new ConfigValidationError("registry","TrustRegistry instance",e.registry,"Registry must be a TrustRegistry instance, not a string or primitive value."));else{const t=e.registry,i=["register","resolve"].filter(e=>!(e in t)||"function"!=typeof t[e]);i.length>0&&r.push(new ConfigValidationError("registry","TrustRegistry instance","object missing required methods",`TrustRegistry must implement: ${i.join(", ")}. Use HttpTrustRegistry or MemoryTrustRegistry.`))}if(void 0===e.transport)r.push(new ConfigValidationError("transport","XailTransportAdapter or XailTransportAdapter[] (required)",void 0,"Provide a transport adapter (e.g., new HttpsTransportAdapter()) or array of adapters for split-channel."));else{const t=Array.isArray(e.transport);if("object"==typeof e.transport&&null!==e.transport&&!t||t)if(t){const t=e.transport;0===t.length&&r.push(new ConfigValidationError("transport","non-empty array",t,"Provide at least one transport adapter.")),t.forEach((t,e)=>{if("object"!=typeof t||null===t)r.push(new ConfigValidationError(`transport[${e}]`,"XailTransportAdapter",t,"Each transport must be a XailTransportAdapter instance."));else{"send"in t&&"function"==typeof t.send||r.push(new ConfigValidationError(`transport[${e}]`,"XailTransportAdapter with send() method","object missing send() method","Transport must implement send() method. Use HttpsTransportAdapter or custom adapter."))}})}else{const t=e.transport;"send"in t&&"function"==typeof t.send||r.push(new ConfigValidationError("transport","XailTransportAdapter with send() method","object missing send() method","Transport must implement send() method. Use HttpsTransportAdapter or custom adapter."))}else r.push(new ConfigValidationError("transport","XailTransportAdapter or XailTransportAdapter[]",e.transport,"Provide a single transport adapter or an array of adapters."))}if(void 0!==e.nonceStore)if("object"!=typeof e.nonceStore||null===e.nonceStore)r.push(new ConfigValidationError("nonceStore","NonceStore instance",e.nonceStore,"Provide a NonceStore instance (e.g., new MemoryNonceStore())."));else{const t=e.nonceStore,i=["check","cleanup"].filter(e=>!(e in t)||"function"!=typeof t[e]);i.length>0&&r.push(new ConfigValidationError("nonceStore","NonceStore instance","object missing required methods",`NonceStore must implement: ${i.join(", ")}. Use MemoryNonceStore.`))}if(void 0!==e.scopes)if(Array.isArray(e.scopes)){e.scopes.forEach((t,e)=>{"string"!=typeof t?r.push(new ConfigValidationError(`scopes[${e}]`,"string",t,"Each scope must be a string.")):0===t.trim().length&&r.push(new ConfigValidationError(`scopes[${e}]`,"non-empty string",t,"Scope cannot be empty or whitespace only."))})}else r.push(new ConfigValidationError("scopes","string[]",e.scopes,'Provide an array of scope strings (e.g., ["read", "write"]).'));if(void 0!==e.timestampWindowMs&&("number"!=typeof e.timestampWindowMs?r.push(new ConfigValidationError("timestampWindowMs","number (milliseconds)",e.timestampWindowMs,"Provide timestamp window as a number in milliseconds (e.g., 30000 for 30 seconds).")):e.timestampWindowMs<=0?r.push(new ConfigValidationError("timestampWindowMs","positive number",e.timestampWindowMs,"Timestamp window must be greater than 0. Use at least 1000 (1 second).")):e.timestampWindowMs<5e3?r.push(new ConfigValidationError("timestampWindowMs","number >= 5000",e.timestampWindowMs,"Timestamp window is very short. Use at least 5000ms (5 seconds) to account for clock skew.")):e.timestampWindowMs>3e5&&r.push(new ConfigValidationError("timestampWindowMs","number <= 300000",e.timestampWindowMs,"Timestamp window is very long. Consider using 300000ms (5 minutes) or less for security."))),void 0!==e.postQuantumSig&&"boolean"!=typeof e.postQuantumSig&&r.push(new ConfigValidationError("postQuantumSig","boolean",e.postQuantumSig,"Use true to enable ML-DSA-65 post-quantum signatures, or false to disable.")),void 0!==e.xchange&&"boolean"!=typeof e.xchange&&r.push(new ConfigValidationError("xchange","boolean",e.xchange,"Use true to advertise Xchange mode support, or false to disable.")),void 0!==e.securityPolicy)if("object"!=typeof e.securityPolicy||null===e.securityPolicy)r.push(new ConfigValidationError("securityPolicy","SecurityPolicy instance",e.securityPolicy,"Provide a SecurityPolicy instance (e.g., DefaultSecurityPolicy or custom policy)."));else{const t=e.securityPolicy;"classify"in t&&"function"==typeof t.classify||r.push(new ConfigValidationError("securityPolicy","SecurityPolicy with classify() method","object missing classify() method","SecurityPolicy must implement classify() method. Use DefaultSecurityPolicy."))}return r.length>0?err(r[0]):ok(e)}export function getValidationDetails(t){const e=[],r=[],i=validateAgentOptions(t);if(i.ok||e.push(i.error),t&&"object"==typeof t){const e=t;"number"==typeof e.identityTTL&&(e.identityTTL<6e4&&e.identityTTL>0?r.push(`identityTTL is very short (${e.identityTTL}ms). Consider using at least 60000ms (1 minute).`):e.identityTTL>864e5&&r.push(`identityTTL is very long (${e.identityTTL}ms). Consider using 'persistent' identity mode instead.`),"persistent"===e.identity&&r.push("identityTTL is ignored for 'persistent' identity mode."))}return{valid:0===e.length,errors:e,warnings:r}}export function assertValidConfig(t){const e=validateAgentOptions(t);if(!e.ok)throw e.error}export function assertValidCreateConfig(t){const e=validateAgentCreateOptions(t);if(!e.ok)throw e.error}
|
|
1
|
+
/**
|
|
2
|
+
* @module config-validation
|
|
3
|
+
* Configuration validation for xBind agent initialization.
|
|
4
|
+
*
|
|
5
|
+
* Provides schema validation, type checking, and clear error messages
|
|
6
|
+
* for AgentOptions and AgentCreateOptions to enhance configuration safety
|
|
7
|
+
* and improve developer experience.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { validateAgentOptions, validateAgentCreateOptions } from '@private.me/xbind';
|
|
12
|
+
*
|
|
13
|
+
* // Validate simplified options
|
|
14
|
+
* const simpleResult = validateAgentOptions({
|
|
15
|
+
* identity: 'persistent',
|
|
16
|
+
* registry: 'https://private.me/registry'
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* if (!simpleResult.ok) {
|
|
20
|
+
* console.error(simpleResult.error); // Clear, actionable error message
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* // Validate full options
|
|
24
|
+
* const fullResult = validateAgentCreateOptions({
|
|
25
|
+
* name: 'my-agent',
|
|
26
|
+
* registry: myRegistryInstance,
|
|
27
|
+
* transport: myTransportAdapter
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
import { ok, err } from"./_deps/shared/index.js";
|
|
32
|
+
import { XBindError } from './errors.js';
|
|
33
|
+
/**
|
|
34
|
+
* Configuration validation error with detailed context.
|
|
35
|
+
*/
|
|
36
|
+
export class ConfigValidationError extends XBindError {
|
|
37
|
+
/** Field that failed validation */
|
|
38
|
+
field;
|
|
39
|
+
/** Expected value/type */
|
|
40
|
+
expected;
|
|
41
|
+
/** Actual value received */
|
|
42
|
+
actual;
|
|
43
|
+
/** Suggested fix */
|
|
44
|
+
fix;
|
|
45
|
+
constructor(field, expected, actual, fix) {
|
|
46
|
+
const message = `Configuration validation failed for '${field}': Expected ${expected}, got ${typeof actual === 'object' && actual !== null ? JSON.stringify(actual) : String(actual)}. ${fix}`;
|
|
47
|
+
super('CONFIG_INVALID', message, 'https://private.me/docs/xbind/api/configuration');
|
|
48
|
+
this.name = 'ConfigValidationError';
|
|
49
|
+
this.field = field;
|
|
50
|
+
this.expected = expected;
|
|
51
|
+
this.actual = actual;
|
|
52
|
+
this.fix = fix;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Default values for AgentOptions.
|
|
57
|
+
*/
|
|
58
|
+
export const AGENT_OPTIONS_DEFAULTS = {
|
|
59
|
+
identity: 'persistent',
|
|
60
|
+
identityTTL: 3600000, // 1 hour
|
|
61
|
+
postQuantumSig: false,
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Default values for AgentCreateOptions.
|
|
65
|
+
*/
|
|
66
|
+
export const AGENT_CREATE_OPTIONS_DEFAULTS = {
|
|
67
|
+
timestampWindowMs: 30000, // 30 seconds
|
|
68
|
+
postQuantumSig: false,
|
|
69
|
+
xchange: false,
|
|
70
|
+
scopes: [],
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Validate AgentOptions configuration.
|
|
74
|
+
*
|
|
75
|
+
* Checks all fields for correct types, valid values, and logical consistency.
|
|
76
|
+
* Provides clear, actionable error messages for any validation failures.
|
|
77
|
+
*
|
|
78
|
+
* @param options - AgentOptions to validate
|
|
79
|
+
* @returns Result with validation errors or success
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* const result = validateAgentOptions({
|
|
84
|
+
* identity: 'persistent',
|
|
85
|
+
* registry: 'https://private.me/registry',
|
|
86
|
+
* identityTTL: 7200000 // 2 hours
|
|
87
|
+
* });
|
|
88
|
+
*
|
|
89
|
+
* if (result.ok) {
|
|
90
|
+
* console.log('Configuration valid!');
|
|
91
|
+
* } else {
|
|
92
|
+
* console.error(result.error);
|
|
93
|
+
* }
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export function validateAgentOptions(options) {
|
|
97
|
+
if (!options || typeof options !== 'object') {
|
|
98
|
+
return err(new ConfigValidationError('options', 'object', options, 'Provide a configuration object with at least one option.'));
|
|
99
|
+
}
|
|
100
|
+
const opts = options;
|
|
101
|
+
const errors = [];
|
|
102
|
+
const warnings = [];
|
|
103
|
+
// Validate identity mode
|
|
104
|
+
if (opts.identity !== undefined) {
|
|
105
|
+
if (typeof opts.identity !== 'string') {
|
|
106
|
+
errors.push(new ConfigValidationError('identity', "'persistent' | 'ephemeral'", opts.identity, "Use 'persistent' for long-lived DIDs or 'ephemeral' for short-lived DIDs."));
|
|
107
|
+
}
|
|
108
|
+
else if (opts.identity !== 'persistent' && opts.identity !== 'ephemeral') {
|
|
109
|
+
errors.push(new ConfigValidationError('identity', "'persistent' | 'ephemeral'", opts.identity, `Invalid identity mode. Use 'persistent' or 'ephemeral'.`));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// Validate identityTTL
|
|
113
|
+
if (opts.identityTTL !== undefined) {
|
|
114
|
+
if (typeof opts.identityTTL !== 'number') {
|
|
115
|
+
errors.push(new ConfigValidationError('identityTTL', 'number (milliseconds)', opts.identityTTL, 'Provide TTL as a number in milliseconds (e.g., 3600000 for 1 hour).'));
|
|
116
|
+
}
|
|
117
|
+
else if (opts.identityTTL <= 0) {
|
|
118
|
+
errors.push(new ConfigValidationError('identityTTL', 'positive number', opts.identityTTL, 'TTL must be greater than 0. Use at least 60000 (1 minute).'));
|
|
119
|
+
}
|
|
120
|
+
else if (opts.identityTTL < 60000) {
|
|
121
|
+
warnings.push(`identityTTL is very short (${opts.identityTTL}ms). Consider using at least 60000ms (1 minute) to avoid frequent re-registrations.`);
|
|
122
|
+
}
|
|
123
|
+
else if (opts.identityTTL > 86400000) {
|
|
124
|
+
warnings.push(`identityTTL is very long (${opts.identityTTL}ms). Consider using 'persistent' identity mode instead for long-lived agents.`);
|
|
125
|
+
}
|
|
126
|
+
// Warn if TTL specified but identity is not ephemeral
|
|
127
|
+
if (opts.identity === 'persistent') {
|
|
128
|
+
warnings.push("identityTTL is ignored for 'persistent' identity mode. Remove this option or switch to 'ephemeral' mode.");
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// Validate registry
|
|
132
|
+
if (opts.registry !== undefined) {
|
|
133
|
+
const isString = typeof opts.registry === 'string';
|
|
134
|
+
const isObject = typeof opts.registry === 'object' && opts.registry !== null;
|
|
135
|
+
if (!isString && !isObject) {
|
|
136
|
+
errors.push(new ConfigValidationError('registry', 'TrustRegistry instance or URL string', opts.registry, 'Provide either a registry URL (e.g., "https://private.me/registry") or a TrustRegistry instance.'));
|
|
137
|
+
}
|
|
138
|
+
else if (isString) {
|
|
139
|
+
const url = opts.registry;
|
|
140
|
+
try {
|
|
141
|
+
const parsed = new URL(url);
|
|
142
|
+
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
|
|
143
|
+
errors.push(new ConfigValidationError('registry', 'HTTP(S) URL', url, 'Registry URL must use HTTP or HTTPS protocol.'));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
errors.push(new ConfigValidationError('registry', 'valid URL', url, 'Provide a valid URL (e.g., "https://private.me/registry").'));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
else if (isObject) {
|
|
151
|
+
// Validate TrustRegistry interface
|
|
152
|
+
const registry = opts.registry;
|
|
153
|
+
const requiredMethods = ['register', 'resolve'];
|
|
154
|
+
const missingMethods = requiredMethods.filter((method) => !(method in registry) || typeof registry[method] !== 'function');
|
|
155
|
+
if (missingMethods.length > 0) {
|
|
156
|
+
errors.push(new ConfigValidationError('registry', 'TrustRegistry instance', 'object missing required methods', `TrustRegistry must implement ${missingMethods.join(', ')} method(s). Use HttpTrustRegistry or MemoryTrustRegistry.`));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// Validate transport
|
|
161
|
+
if (opts.transport !== undefined) {
|
|
162
|
+
const isArray = Array.isArray(opts.transport);
|
|
163
|
+
const isSingleTransport = typeof opts.transport === 'object' && opts.transport !== null && !isArray;
|
|
164
|
+
if (!isSingleTransport && !isArray) {
|
|
165
|
+
errors.push(new ConfigValidationError('transport', 'XailTransportAdapter or XailTransportAdapter[]', opts.transport, 'Provide a single transport adapter or an array of adapters for split-channel security.'));
|
|
166
|
+
}
|
|
167
|
+
else if (isArray) {
|
|
168
|
+
const transports = opts.transport;
|
|
169
|
+
if (transports.length === 0) {
|
|
170
|
+
errors.push(new ConfigValidationError('transport', 'non-empty array', transports, 'Provide at least one transport adapter.'));
|
|
171
|
+
}
|
|
172
|
+
// Validate each transport in array
|
|
173
|
+
transports.forEach((transport, index) => {
|
|
174
|
+
if (typeof transport !== 'object' || transport === null) {
|
|
175
|
+
errors.push(new ConfigValidationError(`transport[${index}]`, 'XailTransportAdapter', transport, 'Each transport must be a XailTransportAdapter instance.'));
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
const t = transport;
|
|
179
|
+
if (!('send' in t) || typeof t.send !== 'function') {
|
|
180
|
+
errors.push(new ConfigValidationError(`transport[${index}]`, 'XailTransportAdapter with send() method', 'object missing send() method', 'Transport must implement send() method. Use HttpsTransportAdapter or custom adapter.'));
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
// Validate single transport
|
|
187
|
+
const transport = opts.transport;
|
|
188
|
+
if (!('send' in transport) || typeof transport.send !== 'function') {
|
|
189
|
+
errors.push(new ConfigValidationError('transport', 'XailTransportAdapter with send() method', 'object missing send() method', 'Transport must implement send() method. Use HttpsTransportAdapter or custom adapter.'));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// Validate securityPolicy
|
|
194
|
+
if (opts.securityPolicy !== undefined) {
|
|
195
|
+
if (typeof opts.securityPolicy !== 'object' || opts.securityPolicy === null) {
|
|
196
|
+
errors.push(new ConfigValidationError('securityPolicy', 'SecurityPolicy instance', opts.securityPolicy, 'Provide a SecurityPolicy instance (e.g., DefaultSecurityPolicy or custom policy).'));
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
const policy = opts.securityPolicy;
|
|
200
|
+
if (!('classify' in policy) || typeof policy.classify !== 'function') {
|
|
201
|
+
errors.push(new ConfigValidationError('securityPolicy', 'SecurityPolicy with classify() method', 'object missing classify() method', 'SecurityPolicy must implement classify() method. Use DefaultSecurityPolicy.'));
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// Validate postQuantumSig
|
|
206
|
+
if (opts.postQuantumSig !== undefined) {
|
|
207
|
+
if (typeof opts.postQuantumSig !== 'boolean') {
|
|
208
|
+
errors.push(new ConfigValidationError('postQuantumSig', 'boolean', opts.postQuantumSig, 'Use true to enable ML-DSA-65 post-quantum signatures, or false to disable.'));
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
// Validate backupConfig
|
|
212
|
+
if (opts.backupConfig !== undefined) {
|
|
213
|
+
if (typeof opts.backupConfig !== 'object' || opts.backupConfig === null) {
|
|
214
|
+
errors.push(new ConfigValidationError('backupConfig', 'BackupConfig object', opts.backupConfig, 'Provide a BackupConfig object with threshold (k) and totalShares (n) properties.'));
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
const config = opts.backupConfig;
|
|
218
|
+
if (typeof config.threshold !== 'number') {
|
|
219
|
+
errors.push(new ConfigValidationError('backupConfig.threshold', 'number', config.threshold, 'Threshold (k) must be a number representing shares needed for reconstruction.'));
|
|
220
|
+
}
|
|
221
|
+
else if (config.threshold < 2) {
|
|
222
|
+
errors.push(new ConfigValidationError('backupConfig.threshold', 'number >= 2', config.threshold, 'Threshold must be at least 2 for meaningful splitting.'));
|
|
223
|
+
}
|
|
224
|
+
if (typeof config.totalShares !== 'number') {
|
|
225
|
+
errors.push(new ConfigValidationError('backupConfig.totalShares', 'number', config.totalShares, 'totalShares (n) must be a number representing total shares to create.'));
|
|
226
|
+
}
|
|
227
|
+
else if (config.totalShares < 2) {
|
|
228
|
+
errors.push(new ConfigValidationError('backupConfig.totalShares', 'number >= 2', config.totalShares, 'totalShares must be at least 2 for splitting.'));
|
|
229
|
+
}
|
|
230
|
+
// Validate threshold <= totalShares
|
|
231
|
+
if (typeof config.threshold === 'number' &&
|
|
232
|
+
typeof config.totalShares === 'number' &&
|
|
233
|
+
config.threshold > config.totalShares) {
|
|
234
|
+
errors.push(new ConfigValidationError('backupConfig', 'threshold <= totalShares', `threshold=${config.threshold}, totalShares=${config.totalShares}`, `Threshold (${config.threshold}) cannot exceed totalShares (${config.totalShares}). Use threshold <= totalShares.`));
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
// Return first error or success
|
|
239
|
+
if (errors.length > 0) {
|
|
240
|
+
return err(errors[0]);
|
|
241
|
+
}
|
|
242
|
+
return ok(opts);
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Validate AgentCreateOptions configuration.
|
|
246
|
+
*
|
|
247
|
+
* Checks all fields for correct types, valid values, and logical consistency.
|
|
248
|
+
* Provides clear, actionable error messages for any validation failures.
|
|
249
|
+
*
|
|
250
|
+
* @param options - AgentCreateOptions to validate
|
|
251
|
+
* @returns Result with validation errors or success
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* ```typescript
|
|
255
|
+
* const result = validateAgentCreateOptions({
|
|
256
|
+
* name: 'my-agent',
|
|
257
|
+
* registry: new MemoryTrustRegistry(),
|
|
258
|
+
* transport: new HttpsTransportAdapter(),
|
|
259
|
+
* timestampWindowMs: 60000 // 1 minute
|
|
260
|
+
* });
|
|
261
|
+
*
|
|
262
|
+
* if (result.ok) {
|
|
263
|
+
* console.log('Configuration valid!');
|
|
264
|
+
* } else {
|
|
265
|
+
* console.error(result.error);
|
|
266
|
+
* }
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
export function validateAgentCreateOptions(options) {
|
|
270
|
+
if (!options || typeof options !== 'object') {
|
|
271
|
+
return err(new ConfigValidationError('options', 'object', options, 'Provide a configuration object with required fields: name, registry, transport.'));
|
|
272
|
+
}
|
|
273
|
+
const opts = options;
|
|
274
|
+
const errors = [];
|
|
275
|
+
// Validate required fields
|
|
276
|
+
if (opts.name === undefined) {
|
|
277
|
+
errors.push(new ConfigValidationError('name', 'string (required)', undefined, 'Provide a display name for the agent (e.g., "my-agent").'));
|
|
278
|
+
}
|
|
279
|
+
else if (typeof opts.name !== 'string') {
|
|
280
|
+
errors.push(new ConfigValidationError('name', 'string', opts.name, 'Agent name must be a string.'));
|
|
281
|
+
}
|
|
282
|
+
else if (opts.name.trim().length === 0) {
|
|
283
|
+
errors.push(new ConfigValidationError('name', 'non-empty string', opts.name, 'Agent name cannot be empty or whitespace only.'));
|
|
284
|
+
}
|
|
285
|
+
else if (opts.name.length > 255) {
|
|
286
|
+
errors.push(new ConfigValidationError('name', 'string with length <= 255', opts.name, `Agent name is too long (${opts.name.length} characters). Use 255 characters or less.`));
|
|
287
|
+
}
|
|
288
|
+
// Validate required registry
|
|
289
|
+
if (opts.registry === undefined) {
|
|
290
|
+
errors.push(new ConfigValidationError('registry', 'TrustRegistry (required)', undefined, 'Provide a TrustRegistry instance (e.g., new MemoryTrustRegistry() or new HttpTrustRegistry()).'));
|
|
291
|
+
}
|
|
292
|
+
else if (typeof opts.registry !== 'object' || opts.registry === null) {
|
|
293
|
+
errors.push(new ConfigValidationError('registry', 'TrustRegistry instance', opts.registry, 'Registry must be a TrustRegistry instance, not a string or primitive value.'));
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
// Validate TrustRegistry interface
|
|
297
|
+
const registry = opts.registry;
|
|
298
|
+
const requiredMethods = ['register', 'resolve'];
|
|
299
|
+
const missingMethods = requiredMethods.filter((method) => !(method in registry) || typeof registry[method] !== 'function');
|
|
300
|
+
if (missingMethods.length > 0) {
|
|
301
|
+
errors.push(new ConfigValidationError('registry', 'TrustRegistry instance', 'object missing required methods', `TrustRegistry must implement: ${missingMethods.join(', ')}. Use HttpTrustRegistry or MemoryTrustRegistry.`));
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
// Validate required transport
|
|
305
|
+
if (opts.transport === undefined) {
|
|
306
|
+
errors.push(new ConfigValidationError('transport', 'XailTransportAdapter or XailTransportAdapter[] (required)', undefined, 'Provide a transport adapter (e.g., new HttpsTransportAdapter()) or array of adapters for split-channel.'));
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
const isArray = Array.isArray(opts.transport);
|
|
310
|
+
const isSingleTransport = typeof opts.transport === 'object' && opts.transport !== null && !isArray;
|
|
311
|
+
if (!isSingleTransport && !isArray) {
|
|
312
|
+
errors.push(new ConfigValidationError('transport', 'XailTransportAdapter or XailTransportAdapter[]', opts.transport, 'Provide a single transport adapter or an array of adapters.'));
|
|
313
|
+
}
|
|
314
|
+
else if (isArray) {
|
|
315
|
+
const transports = opts.transport;
|
|
316
|
+
if (transports.length === 0) {
|
|
317
|
+
errors.push(new ConfigValidationError('transport', 'non-empty array', transports, 'Provide at least one transport adapter.'));
|
|
318
|
+
}
|
|
319
|
+
// Validate each transport in array
|
|
320
|
+
transports.forEach((transport, index) => {
|
|
321
|
+
if (typeof transport !== 'object' || transport === null) {
|
|
322
|
+
errors.push(new ConfigValidationError(`transport[${index}]`, 'XailTransportAdapter', transport, 'Each transport must be a XailTransportAdapter instance.'));
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
const t = transport;
|
|
326
|
+
if (!('send' in t) || typeof t.send !== 'function') {
|
|
327
|
+
errors.push(new ConfigValidationError(`transport[${index}]`, 'XailTransportAdapter with send() method', 'object missing send() method', 'Transport must implement send() method. Use HttpsTransportAdapter or custom adapter.'));
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
// Validate single transport
|
|
334
|
+
const transport = opts.transport;
|
|
335
|
+
if (!('send' in transport) || typeof transport.send !== 'function') {
|
|
336
|
+
errors.push(new ConfigValidationError('transport', 'XailTransportAdapter with send() method', 'object missing send() method', 'Transport must implement send() method. Use HttpsTransportAdapter or custom adapter.'));
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
// Validate optional nonceStore
|
|
341
|
+
if (opts.nonceStore !== undefined) {
|
|
342
|
+
if (typeof opts.nonceStore !== 'object' || opts.nonceStore === null) {
|
|
343
|
+
errors.push(new ConfigValidationError('nonceStore', 'NonceStore instance', opts.nonceStore, 'Provide a NonceStore instance (e.g., new MemoryNonceStore()).'));
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
const store = opts.nonceStore;
|
|
347
|
+
const requiredMethods = ['check', 'cleanup'];
|
|
348
|
+
const missingMethods = requiredMethods.filter((method) => !(method in store) || typeof store[method] !== 'function');
|
|
349
|
+
if (missingMethods.length > 0) {
|
|
350
|
+
errors.push(new ConfigValidationError('nonceStore', 'NonceStore instance', 'object missing required methods', `NonceStore must implement: ${missingMethods.join(', ')}. Use MemoryNonceStore.`));
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
// Validate scopes
|
|
355
|
+
if (opts.scopes !== undefined) {
|
|
356
|
+
if (!Array.isArray(opts.scopes)) {
|
|
357
|
+
errors.push(new ConfigValidationError('scopes', 'string[]', opts.scopes, 'Provide an array of scope strings (e.g., ["read", "write"]).'));
|
|
358
|
+
}
|
|
359
|
+
else {
|
|
360
|
+
const scopes = opts.scopes;
|
|
361
|
+
scopes.forEach((scope, index) => {
|
|
362
|
+
if (typeof scope !== 'string') {
|
|
363
|
+
errors.push(new ConfigValidationError(`scopes[${index}]`, 'string', scope, 'Each scope must be a string.'));
|
|
364
|
+
}
|
|
365
|
+
else if (scope.trim().length === 0) {
|
|
366
|
+
errors.push(new ConfigValidationError(`scopes[${index}]`, 'non-empty string', scope, 'Scope cannot be empty or whitespace only.'));
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
// Validate timestampWindowMs
|
|
372
|
+
if (opts.timestampWindowMs !== undefined) {
|
|
373
|
+
if (typeof opts.timestampWindowMs !== 'number') {
|
|
374
|
+
errors.push(new ConfigValidationError('timestampWindowMs', 'number (milliseconds)', opts.timestampWindowMs, 'Provide timestamp window as a number in milliseconds (e.g., 30000 for 30 seconds).'));
|
|
375
|
+
}
|
|
376
|
+
else if (opts.timestampWindowMs <= 0) {
|
|
377
|
+
errors.push(new ConfigValidationError('timestampWindowMs', 'positive number', opts.timestampWindowMs, 'Timestamp window must be greater than 0. Use at least 1000 (1 second).'));
|
|
378
|
+
}
|
|
379
|
+
else if (opts.timestampWindowMs < 5000) {
|
|
380
|
+
errors.push(new ConfigValidationError('timestampWindowMs', 'number >= 5000', opts.timestampWindowMs, 'Timestamp window is very short. Use at least 5000ms (5 seconds) to account for clock skew.'));
|
|
381
|
+
}
|
|
382
|
+
else if (opts.timestampWindowMs > 300000) {
|
|
383
|
+
errors.push(new ConfigValidationError('timestampWindowMs', 'number <= 300000', opts.timestampWindowMs, 'Timestamp window is very long. Consider using 300000ms (5 minutes) or less for security.'));
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
// Validate postQuantumSig
|
|
387
|
+
if (opts.postQuantumSig !== undefined) {
|
|
388
|
+
if (typeof opts.postQuantumSig !== 'boolean') {
|
|
389
|
+
errors.push(new ConfigValidationError('postQuantumSig', 'boolean', opts.postQuantumSig, 'Use true to enable ML-DSA-65 post-quantum signatures, or false to disable.'));
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
// Validate xchange
|
|
393
|
+
if (opts.xchange !== undefined) {
|
|
394
|
+
if (typeof opts.xchange !== 'boolean') {
|
|
395
|
+
errors.push(new ConfigValidationError('xchange', 'boolean', opts.xchange, 'Use true to advertise Xchange mode support, or false to disable.'));
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
// Validate securityPolicy
|
|
399
|
+
if (opts.securityPolicy !== undefined) {
|
|
400
|
+
if (typeof opts.securityPolicy !== 'object' || opts.securityPolicy === null) {
|
|
401
|
+
errors.push(new ConfigValidationError('securityPolicy', 'SecurityPolicy instance', opts.securityPolicy, 'Provide a SecurityPolicy instance (e.g., DefaultSecurityPolicy or custom policy).'));
|
|
402
|
+
}
|
|
403
|
+
else {
|
|
404
|
+
const policy = opts.securityPolicy;
|
|
405
|
+
if (!('classify' in policy) || typeof policy.classify !== 'function') {
|
|
406
|
+
errors.push(new ConfigValidationError('securityPolicy', 'SecurityPolicy with classify() method', 'object missing classify() method', 'SecurityPolicy must implement classify() method. Use DefaultSecurityPolicy.'));
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
// Return first error or success
|
|
411
|
+
if (errors.length > 0) {
|
|
412
|
+
return err(errors[0]);
|
|
413
|
+
}
|
|
414
|
+
return ok(opts);
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Get detailed validation result with all errors and warnings.
|
|
418
|
+
*
|
|
419
|
+
* Unlike the Result-based validators, this function returns all validation
|
|
420
|
+
* issues at once for comprehensive feedback.
|
|
421
|
+
*
|
|
422
|
+
* @param options - AgentOptions to validate
|
|
423
|
+
* @returns ValidationResult with all errors and warnings
|
|
424
|
+
*
|
|
425
|
+
* @example
|
|
426
|
+
* ```typescript
|
|
427
|
+
* const result = getValidationDetails({
|
|
428
|
+
* identity: 'invalid',
|
|
429
|
+
* identityTTL: -100,
|
|
430
|
+
* postQuantumSig: 'yes' // type error
|
|
431
|
+
* });
|
|
432
|
+
*
|
|
433
|
+
* console.log(`Valid: ${result.valid}`);
|
|
434
|
+
* console.log(`Errors: ${result.errors.length}`);
|
|
435
|
+
* console.log(`Warnings: ${result.warnings.length}`);
|
|
436
|
+
*
|
|
437
|
+
* result.errors.forEach(err => {
|
|
438
|
+
* console.error(`- ${err.field}: ${err.message}`);
|
|
439
|
+
* });
|
|
440
|
+
* ```
|
|
441
|
+
*/
|
|
442
|
+
export function getValidationDetails(options) {
|
|
443
|
+
const errors = [];
|
|
444
|
+
const warnings = [];
|
|
445
|
+
// Run validation and collect all errors
|
|
446
|
+
const result = validateAgentOptions(options);
|
|
447
|
+
if (!result.ok) {
|
|
448
|
+
errors.push(result.error);
|
|
449
|
+
}
|
|
450
|
+
// Check for warning conditions (even if validation passed)
|
|
451
|
+
if (options && typeof options === 'object') {
|
|
452
|
+
const opts = options;
|
|
453
|
+
// Check for warning conditions
|
|
454
|
+
if (typeof opts.identityTTL === 'number') {
|
|
455
|
+
if (opts.identityTTL < 60000 && opts.identityTTL > 0) {
|
|
456
|
+
warnings.push(`identityTTL is very short (${opts.identityTTL}ms). Consider using at least 60000ms (1 minute).`);
|
|
457
|
+
}
|
|
458
|
+
else if (opts.identityTTL > 86400000) {
|
|
459
|
+
warnings.push(`identityTTL is very long (${opts.identityTTL}ms). Consider using 'persistent' identity mode instead.`);
|
|
460
|
+
}
|
|
461
|
+
if (opts.identity === 'persistent') {
|
|
462
|
+
warnings.push("identityTTL is ignored for 'persistent' identity mode.");
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
return {
|
|
467
|
+
valid: errors.length === 0,
|
|
468
|
+
errors,
|
|
469
|
+
warnings,
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Validate configuration and throw on error.
|
|
474
|
+
*
|
|
475
|
+
* Convenience function that validates configuration and throws
|
|
476
|
+
* ConfigValidationError if validation fails.
|
|
477
|
+
*
|
|
478
|
+
* @param options - AgentOptions or AgentCreateOptions to validate
|
|
479
|
+
* @throws ConfigValidationError if validation fails
|
|
480
|
+
*
|
|
481
|
+
* @example
|
|
482
|
+
* ```typescript
|
|
483
|
+
* try {
|
|
484
|
+
* assertValidConfig({
|
|
485
|
+
* identity: 'persistent',
|
|
486
|
+
* registry: 'https://private.me/registry'
|
|
487
|
+
* });
|
|
488
|
+
* // Configuration is valid, continue
|
|
489
|
+
* } catch (err) {
|
|
490
|
+
* if (err instanceof ConfigValidationError) {
|
|
491
|
+
* console.error(`Invalid ${err.field}: ${err.fix}`);
|
|
492
|
+
* }
|
|
493
|
+
* }
|
|
494
|
+
* ```
|
|
495
|
+
*/
|
|
496
|
+
export function assertValidConfig(options) {
|
|
497
|
+
const result = validateAgentOptions(options);
|
|
498
|
+
if (!result.ok) {
|
|
499
|
+
throw result.error;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Validate configuration and throw on error (full options).
|
|
504
|
+
*
|
|
505
|
+
* @param options - AgentCreateOptions to validate
|
|
506
|
+
* @throws ConfigValidationError if validation fails
|
|
507
|
+
*/
|
|
508
|
+
export function assertValidCreateConfig(options) {
|
|
509
|
+
const result = validateAgentCreateOptions(options);
|
|
510
|
+
if (!result.ok) {
|
|
511
|
+
throw result.error;
|
|
512
|
+
}
|
|
513
|
+
}
|