@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,56 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Error Response Types
|
|
4
|
+
*
|
|
5
|
+
* Dual-code error response system supporting both AWS standard codes
|
|
6
|
+
* and legacy xBind codes for backward compatibility.
|
|
7
|
+
*
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.isErrorResponse = isErrorResponse;
|
|
12
|
+
exports.createErrorResponse = createErrorResponse;
|
|
13
|
+
/**
|
|
14
|
+
* Type guard to check if a value is an ErrorResponse
|
|
15
|
+
*
|
|
16
|
+
* @param value - Value to check
|
|
17
|
+
* @returns True if value is an ErrorResponse
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* if (isErrorResponse(result)) {
|
|
22
|
+
* console.error(result.message);
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
function isErrorResponse(value) {
|
|
27
|
+
if (!value || typeof value !== 'object') {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
const err = value;
|
|
31
|
+
return (typeof err.code === 'string' &&
|
|
32
|
+
typeof err.httpStatus === 'number' &&
|
|
33
|
+
typeof err.message === 'string');
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Create a standardized error response
|
|
37
|
+
*
|
|
38
|
+
* @param params - Error response parameters
|
|
39
|
+
* @returns Standardized ErrorResponse object
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* const error = createErrorResponse({
|
|
44
|
+
* code: 'UnauthorizedException',
|
|
45
|
+
* legacyCode: 'XBIND_AUTH_FAILED',
|
|
46
|
+
* httpStatus: 401,
|
|
47
|
+
* message: 'Invalid signature'
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
function createErrorResponse(params) {
|
|
52
|
+
return {
|
|
53
|
+
...params,
|
|
54
|
+
timestamp: params.timestamp ?? Date.now()
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -1 +1,178 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module vault-auth
|
|
4
|
+
* DID-based authentication for Vault Store API requests.
|
|
5
|
+
*
|
|
6
|
+
* Implements Ed25519 signature over canonical request representation with:
|
|
7
|
+
* - Timestamp (prevents replay attacks >5min old)
|
|
8
|
+
* - Nonce (prevents duplicate requests)
|
|
9
|
+
* - Request body hash (ensures integrity)
|
|
10
|
+
*
|
|
11
|
+
* Server verifies signature + timestamp + nonce before serving vault content.
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.signVaultRequest = signVaultRequest;
|
|
15
|
+
exports.verifyVaultRequest = verifyVaultRequest;
|
|
16
|
+
const shared_1 = require("../_deps/shared/index.js");
|
|
17
|
+
const crypto_utils_js_1 = require("./crypto-utils.js");
|
|
18
|
+
/**
|
|
19
|
+
* Sign a vault store API request with agent's DID identity.
|
|
20
|
+
*
|
|
21
|
+
* Creates canonical request representation:
|
|
22
|
+
* ```
|
|
23
|
+
* {method}\n{endpoint}\n{timestamp}\n{nonce}\n{bodyHash}
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* Server verifies:
|
|
27
|
+
* 1. Signature matches DID public key
|
|
28
|
+
* 2. Timestamp within ±5min (prevents replay)
|
|
29
|
+
* 3. Nonce not seen before (prevents duplicate)
|
|
30
|
+
* 4. Body hash matches (ensures integrity)
|
|
31
|
+
*
|
|
32
|
+
* @param identity - Agent identity (contains signing key)
|
|
33
|
+
* @param endpoint - API endpoint (e.g., "/api/vault-store/crypto")
|
|
34
|
+
* @param body - Request body (JSON-serializable)
|
|
35
|
+
* @returns Signed request metadata or error
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const sigResult = await signVaultRequest(agent.identity, '/api/vault-store/crypto', {
|
|
40
|
+
* requestedVersion: 'latest',
|
|
41
|
+
* clientVersion: '1.5.0',
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* if (!sigResult.ok) {
|
|
45
|
+
* throw new Error('Failed to sign vault request');
|
|
46
|
+
* }
|
|
47
|
+
*
|
|
48
|
+
* const { signature, timestamp, nonce } = sigResult.value;
|
|
49
|
+
*
|
|
50
|
+
* const response = await fetch('https://private.me/api/vault-store/crypto', {
|
|
51
|
+
* method: 'POST',
|
|
52
|
+
* headers: {
|
|
53
|
+
* 'X-DID': agent.did,
|
|
54
|
+
* 'X-Signature': signature,
|
|
55
|
+
* 'X-Timestamp': timestamp.toString(),
|
|
56
|
+
* 'X-Nonce': nonce,
|
|
57
|
+
* },
|
|
58
|
+
* body: JSON.stringify(body),
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
async function signVaultRequest(identity, endpoint, body) {
|
|
63
|
+
const timestamp = Date.now();
|
|
64
|
+
const nonce = generateNonce();
|
|
65
|
+
// Canonical request representation
|
|
66
|
+
const method = 'POST';
|
|
67
|
+
const bodyJson = JSON.stringify(body);
|
|
68
|
+
const bodyHash = await hashString(bodyJson);
|
|
69
|
+
const canonical = `${method}\n${endpoint}\n${timestamp}\n${nonce}\n${bodyHash}`;
|
|
70
|
+
// Sign with Ed25519
|
|
71
|
+
let signatureBuffer;
|
|
72
|
+
try {
|
|
73
|
+
const encoder = new TextEncoder();
|
|
74
|
+
const message = encoder.encode(canonical);
|
|
75
|
+
signatureBuffer = await crypto.subtle.sign({ name: 'Ed25519' }, identity.privateKey, message);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return (0, shared_1.err)('SIGN_FAILED');
|
|
79
|
+
}
|
|
80
|
+
return (0, shared_1.ok)({
|
|
81
|
+
signature: (0, crypto_utils_js_1.toBase64)(new Uint8Array(signatureBuffer)),
|
|
82
|
+
timestamp,
|
|
83
|
+
nonce,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Generate cryptographically secure nonce (UUID v4 format).
|
|
88
|
+
*
|
|
89
|
+
* @returns Random UUID string
|
|
90
|
+
*/
|
|
91
|
+
function generateNonce() {
|
|
92
|
+
// Use crypto.randomUUID if available (Node.js 16+, modern browsers)
|
|
93
|
+
if (typeof crypto !== 'undefined' && crypto.randomUUID) {
|
|
94
|
+
return crypto.randomUUID();
|
|
95
|
+
}
|
|
96
|
+
// Fallback: manual UUID v4 generation
|
|
97
|
+
const bytes = new Uint8Array(16);
|
|
98
|
+
crypto.getRandomValues(bytes);
|
|
99
|
+
// Set version (4) and variant (RFC 4122)
|
|
100
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
|
101
|
+
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
102
|
+
const hex = Array.from(bytes)
|
|
103
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
104
|
+
.join('');
|
|
105
|
+
return [
|
|
106
|
+
hex.slice(0, 8),
|
|
107
|
+
hex.slice(8, 12),
|
|
108
|
+
hex.slice(12, 16),
|
|
109
|
+
hex.slice(16, 20),
|
|
110
|
+
hex.slice(20, 32),
|
|
111
|
+
].join('-');
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Hash string with SHA-256 (for request body integrity).
|
|
115
|
+
*
|
|
116
|
+
* @param data - String to hash
|
|
117
|
+
* @returns Base64-encoded hash
|
|
118
|
+
*/
|
|
119
|
+
async function hashString(data) {
|
|
120
|
+
const encoder = new TextEncoder();
|
|
121
|
+
const bytes = encoder.encode(data);
|
|
122
|
+
const hashBuffer = await crypto.subtle.digest('SHA-256', bytes);
|
|
123
|
+
return (0, crypto_utils_js_1.toBase64)(new Uint8Array(hashBuffer));
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Verify vault request signature (server-side only).
|
|
127
|
+
*
|
|
128
|
+
* Validates:
|
|
129
|
+
* 1. Signature matches DID public key
|
|
130
|
+
* 2. Timestamp within ±5min window
|
|
131
|
+
* 3. Body hash matches
|
|
132
|
+
*
|
|
133
|
+
* Note: Nonce verification requires server-side nonce store (not implemented here).
|
|
134
|
+
*
|
|
135
|
+
* @param did - Sender DID
|
|
136
|
+
* @param publicKeyBytes - Ed25519 public key (32 bytes)
|
|
137
|
+
* @param endpoint - API endpoint
|
|
138
|
+
* @param body - Request body (JSON string)
|
|
139
|
+
* @param signature - Base64-encoded signature
|
|
140
|
+
* @param timestamp - Request timestamp (Unix ms)
|
|
141
|
+
* @param nonce - Request nonce
|
|
142
|
+
* @returns True if signature is valid
|
|
143
|
+
*
|
|
144
|
+
* @internal Server-side verification only
|
|
145
|
+
*/
|
|
146
|
+
async function verifyVaultRequest(did, publicKeyBytes, endpoint, body, signature, timestamp, nonce) {
|
|
147
|
+
// Timestamp verification (±5min window)
|
|
148
|
+
const now = Date.now();
|
|
149
|
+
const maxAge = 5 * 60 * 1000; // 5 minutes
|
|
150
|
+
if (Math.abs(now - timestamp) > maxAge) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
// Reconstruct canonical request
|
|
154
|
+
const method = 'POST';
|
|
155
|
+
const bodyHash = await hashString(body);
|
|
156
|
+
const canonical = `${method}\n${endpoint}\n${timestamp}\n${nonce}\n${bodyHash}`;
|
|
157
|
+
// Import public key
|
|
158
|
+
let publicKey;
|
|
159
|
+
try {
|
|
160
|
+
// Copy to new ArrayBuffer to avoid SharedArrayBuffer issues
|
|
161
|
+
const keyBuffer = new ArrayBuffer(publicKeyBytes.byteLength);
|
|
162
|
+
new Uint8Array(keyBuffer).set(publicKeyBytes);
|
|
163
|
+
publicKey = await crypto.subtle.importKey('raw', keyBuffer, { name: 'Ed25519' }, false, ['verify']);
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
// Verify signature
|
|
169
|
+
try {
|
|
170
|
+
const encoder = new TextEncoder();
|
|
171
|
+
const message = encoder.encode(canonical);
|
|
172
|
+
const signatureBytes = Uint8Array.from(atob(signature), (c) => c.charCodeAt(0));
|
|
173
|
+
return await crypto.subtle.verify({ name: 'Ed25519' }, publicKey, signatureBytes, message);
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
@@ -1 +1,194 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module vault-store-loader
|
|
4
|
+
* Runtime loader for payment-gated crypto packages (Full Control IP protection).
|
|
5
|
+
*
|
|
6
|
+
* Fetches XorIDA algorithm from EC2 Vault Store with:
|
|
7
|
+
* - DID-based authentication (Ed25519 signatures)
|
|
8
|
+
* - Usage quota verification (Free: 100K/month, Pro: unlimited)
|
|
9
|
+
* - Memory caching (7-day TTL, session-only)
|
|
10
|
+
* - Automatic re-fetch on expiration
|
|
11
|
+
*
|
|
12
|
+
* Security: Crypto package NEVER bundled in npm. Always fetched at runtime
|
|
13
|
+
* with payment gate enforcement. Share 2 (Vault Store) completes algorithm.
|
|
14
|
+
*/
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.loadCryptoPackage = loadCryptoPackage;
|
|
17
|
+
exports.getCrypto = getCrypto;
|
|
18
|
+
exports.isCryptoLoaded = isCryptoLoaded;
|
|
19
|
+
exports.clearCryptoCache = clearCryptoCache;
|
|
20
|
+
exports.getCacheStatus = getCacheStatus;
|
|
21
|
+
const shared_1 = require("../_deps/shared/index.js");
|
|
22
|
+
const vault_auth_js_1 = require("./vault-auth.js");
|
|
23
|
+
/** Vault Store API endpoint (EC2) */
|
|
24
|
+
const VAULT_STORE_URL = process.env.VAULT_STORE_URL || 'https://private.me/api/vault-store';
|
|
25
|
+
/** Cache TTL: 7 days (in milliseconds) */
|
|
26
|
+
const CACHE_TTL_MS = 7 * 24 * 60 * 60 * 1000;
|
|
27
|
+
/** In-memory cache (cleared on process restart) */
|
|
28
|
+
let cryptoCache = null;
|
|
29
|
+
/**
|
|
30
|
+
* Load crypto package from Vault Store with authentication and caching.
|
|
31
|
+
*
|
|
32
|
+
* Flow:
|
|
33
|
+
* 1. Check cache (if valid, return cached)
|
|
34
|
+
* 2. Sign vault request with DID
|
|
35
|
+
* 3. POST to /api/vault-store/crypto
|
|
36
|
+
* 4. Server verifies: signature + quota + payment
|
|
37
|
+
* 5. Receive crypto bundle + share2
|
|
38
|
+
* 6. Evaluate bundle (dynamic import)
|
|
39
|
+
* 7. Cache for 7 days
|
|
40
|
+
*
|
|
41
|
+
* @param identity - Agent identity (for DID signature)
|
|
42
|
+
* @returns Crypto package exports or error
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* const cryptoResult = await loadCryptoPackage(agent.identity);
|
|
47
|
+
* if (!cryptoResult.ok) {
|
|
48
|
+
* if (cryptoResult.error === 'VAULT_QUOTA_EXCEEDED') {
|
|
49
|
+
* console.error('Free tier quota exceeded. Upgrade to Pro for unlimited access.');
|
|
50
|
+
* }
|
|
51
|
+
* throw new Error(cryptoResult.error);
|
|
52
|
+
* }
|
|
53
|
+
* const { splitXorIDA, reconstructXorIDA } = cryptoResult.value;
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
async function loadCryptoPackage(identity) {
|
|
57
|
+
// Check cache first
|
|
58
|
+
if (cryptoCache && Date.now() < cryptoCache.expiresAt) {
|
|
59
|
+
return (0, shared_1.ok)(cryptoCache.module);
|
|
60
|
+
}
|
|
61
|
+
// Sign vault request
|
|
62
|
+
const signatureResult = await (0, vault_auth_js_1.signVaultRequest)(identity, '/api/vault-store/crypto', {
|
|
63
|
+
requestedVersion: 'latest',
|
|
64
|
+
clientVersion: '1.5.0',
|
|
65
|
+
});
|
|
66
|
+
if (!signatureResult.ok) {
|
|
67
|
+
return (0, shared_1.err)('VAULT_AUTH_FAILED');
|
|
68
|
+
}
|
|
69
|
+
const { signature, timestamp, nonce } = signatureResult.value;
|
|
70
|
+
// Fetch from vault store
|
|
71
|
+
let response;
|
|
72
|
+
try {
|
|
73
|
+
response = await fetch(`${VAULT_STORE_URL}/crypto`, {
|
|
74
|
+
method: 'POST',
|
|
75
|
+
headers: {
|
|
76
|
+
'Content-Type': 'application/json',
|
|
77
|
+
'X-DID': identity.did,
|
|
78
|
+
'X-Signature': signature,
|
|
79
|
+
'X-Timestamp': timestamp.toString(),
|
|
80
|
+
'X-Nonce': nonce,
|
|
81
|
+
},
|
|
82
|
+
body: JSON.stringify({
|
|
83
|
+
requestedVersion: 'latest',
|
|
84
|
+
clientVersion: '1.5.0',
|
|
85
|
+
}),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
catch (fetchError) {
|
|
89
|
+
return (0, shared_1.err)('VAULT_FETCH_FAILED');
|
|
90
|
+
}
|
|
91
|
+
// Handle error responses
|
|
92
|
+
if (!response.ok) {
|
|
93
|
+
if (response.status === 402) {
|
|
94
|
+
// Payment Required - quota exceeded
|
|
95
|
+
return (0, shared_1.err)('VAULT_QUOTA_EXCEEDED');
|
|
96
|
+
}
|
|
97
|
+
else if (response.status === 401 || response.status === 403) {
|
|
98
|
+
// Unauthorized / Forbidden
|
|
99
|
+
return (0, shared_1.err)('VAULT_AUTH_FAILED');
|
|
100
|
+
}
|
|
101
|
+
else if (response.status === 451) {
|
|
102
|
+
// Unavailable For Legal Reasons - payment required
|
|
103
|
+
return (0, shared_1.err)('VAULT_PAYMENT_REQUIRED');
|
|
104
|
+
}
|
|
105
|
+
return (0, shared_1.err)('VAULT_FETCH_FAILED');
|
|
106
|
+
}
|
|
107
|
+
// Parse response
|
|
108
|
+
let vaultData;
|
|
109
|
+
try {
|
|
110
|
+
vaultData = (await response.json());
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
return (0, shared_1.err)('VAULT_INVALID_RESPONSE');
|
|
114
|
+
}
|
|
115
|
+
if (!vaultData.cryptoBundle || !vaultData.version) {
|
|
116
|
+
return (0, shared_1.err)('VAULT_INVALID_RESPONSE');
|
|
117
|
+
}
|
|
118
|
+
// Decode and evaluate bundle
|
|
119
|
+
let cryptoModule;
|
|
120
|
+
try {
|
|
121
|
+
const bundleCode = atob(vaultData.cryptoBundle);
|
|
122
|
+
// Dynamic evaluation (isolated scope)
|
|
123
|
+
// eslint-disable-next-line no-eval
|
|
124
|
+
const moduleExports = eval(`(function() {
|
|
125
|
+
const exports = {};
|
|
126
|
+
${bundleCode}
|
|
127
|
+
return exports;
|
|
128
|
+
})()`);
|
|
129
|
+
cryptoModule = moduleExports;
|
|
130
|
+
// Validate required exports
|
|
131
|
+
if (typeof cryptoModule.splitXorIDA !== 'function' ||
|
|
132
|
+
typeof cryptoModule.reconstructXorIDA !== 'function') {
|
|
133
|
+
return (0, shared_1.err)('VAULT_LOAD_FAILED');
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
return (0, shared_1.err)('VAULT_LOAD_FAILED');
|
|
138
|
+
}
|
|
139
|
+
// Cache for 7 days (or server-provided TTL)
|
|
140
|
+
const ttlMs = vaultData.cacheTtl ? vaultData.cacheTtl * 1000 : CACHE_TTL_MS;
|
|
141
|
+
cryptoCache = {
|
|
142
|
+
module: cryptoModule,
|
|
143
|
+
expiresAt: Date.now() + ttlMs,
|
|
144
|
+
version: vaultData.version,
|
|
145
|
+
};
|
|
146
|
+
return (0, shared_1.ok)(cryptoModule);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Get cached crypto package without fetching.
|
|
150
|
+
*
|
|
151
|
+
* Returns null if cache is empty or expired.
|
|
152
|
+
* Use loadCryptoPackage() to fetch and cache.
|
|
153
|
+
*
|
|
154
|
+
* @returns Cached crypto package or null
|
|
155
|
+
*/
|
|
156
|
+
function getCrypto() {
|
|
157
|
+
if (cryptoCache && Date.now() < cryptoCache.expiresAt) {
|
|
158
|
+
return cryptoCache.module;
|
|
159
|
+
}
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Check if crypto package is loaded and valid.
|
|
164
|
+
*
|
|
165
|
+
* @returns True if crypto is cached and not expired
|
|
166
|
+
*/
|
|
167
|
+
function isCryptoLoaded() {
|
|
168
|
+
return cryptoCache !== null && Date.now() < cryptoCache.expiresAt;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Clear crypto cache (force re-fetch on next load).
|
|
172
|
+
*
|
|
173
|
+
* Useful for testing or forcing quota re-verification.
|
|
174
|
+
*/
|
|
175
|
+
function clearCryptoCache() {
|
|
176
|
+
cryptoCache = null;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Get cache status (for debugging).
|
|
180
|
+
*
|
|
181
|
+
* @returns Cache metadata or null if empty
|
|
182
|
+
*/
|
|
183
|
+
function getCacheStatus() {
|
|
184
|
+
if (!cryptoCache) {
|
|
185
|
+
return { loaded: false };
|
|
186
|
+
}
|
|
187
|
+
const now = Date.now();
|
|
188
|
+
return {
|
|
189
|
+
loaded: now < cryptoCache.expiresAt,
|
|
190
|
+
version: cryptoCache.version,
|
|
191
|
+
expiresAt: cryptoCache.expiresAt,
|
|
192
|
+
ttlRemaining: Math.max(0, cryptoCache.expiresAt - now),
|
|
193
|
+
};
|
|
194
|
+
}
|
|
@@ -1 +1,25 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module verify
|
|
4
|
+
* Lightweight sub-path export for verification-only use cases.
|
|
5
|
+
*
|
|
6
|
+
* Import as `@private.me/xbind/verify` for tree-shaking on edge/serverless:
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { verify, importPublicKey, validateEnvelope } from '@private.me/xbind/verify';
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* This module re-exports only the functions needed to verify signatures
|
|
12
|
+
* and validate envelopes — no key generation, no encryption, no transport.
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.openSignedEnvelope = exports.deserializeEnvelope = exports.validateEnvelope = exports.didToPublicKeyBytes = exports.importPublicKey = exports.verify = void 0;
|
|
16
|
+
// Identity — verify + key import only
|
|
17
|
+
var identity_js_1 = require("./identity.js");
|
|
18
|
+
Object.defineProperty(exports, "verify", { enumerable: true, get: function () { return identity_js_1.verify; } });
|
|
19
|
+
Object.defineProperty(exports, "importPublicKey", { enumerable: true, get: function () { return identity_js_1.importPublicKey; } });
|
|
20
|
+
Object.defineProperty(exports, "didToPublicKeyBytes", { enumerable: true, get: function () { return identity_js_1.didToPublicKeyBytes; } });
|
|
21
|
+
// Envelope — validation + signed envelope verification
|
|
22
|
+
var envelope_js_1 = require("./envelope.js");
|
|
23
|
+
Object.defineProperty(exports, "validateEnvelope", { enumerable: true, get: function () { return envelope_js_1.validateEnvelope; } });
|
|
24
|
+
Object.defineProperty(exports, "deserializeEnvelope", { enumerable: true, get: function () { return envelope_js_1.deserializeEnvelope; } });
|
|
25
|
+
Object.defineProperty(exports, "openSignedEnvelope", { enumerable: true, get: function () { return envelope_js_1.openSignedEnvelope; } });
|