@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,326 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Encrypted Backup and Restore (RCV-3)
|
|
3
|
+
*
|
|
4
|
+
* Exports and imports encrypted identity and cryptographic state.
|
|
5
|
+
* Uses PBKDF2 (NIST SP 800-132) for key derivation and AES-256-GCM
|
|
6
|
+
* for authenticated encryption.
|
|
7
|
+
*
|
|
8
|
+
* @module backup
|
|
9
|
+
*/
|
|
10
|
+
import { ok, err } from"./_deps/shared/index.js";
|
|
11
|
+
import { toBase64, fromBase64 } from './crypto-utils.js';
|
|
12
|
+
import { exportPKCS8, exportX25519PKCS8, importIdentity, exportMlKemSecretKey, exportMlKemPublicKey, exportMlDsaSecretKey, exportMlDsaPublicKey, } from './identity.js';
|
|
13
|
+
/* ── Constants ── */
|
|
14
|
+
/** PBKDF2 iterations for key derivation (NIST SP 800-132 minimum: 100,000). */
|
|
15
|
+
const PBKDF2_ITERATIONS = 310_000;
|
|
16
|
+
/** PBKDF2 salt length in bytes. */
|
|
17
|
+
const SALT_LENGTH = 16;
|
|
18
|
+
/** AES-256-GCM IV length in bytes. */
|
|
19
|
+
const IV_LENGTH = 12;
|
|
20
|
+
/** AES-256-GCM key length in bytes. */
|
|
21
|
+
const KEY_LENGTH = 32;
|
|
22
|
+
/* ── Utility Functions ── */
|
|
23
|
+
/**
|
|
24
|
+
* Copy Uint8Array to fresh ArrayBuffer (avoids SharedArrayBuffer type issues).
|
|
25
|
+
*/
|
|
26
|
+
function toArrayBuffer(data) {
|
|
27
|
+
const buf = new ArrayBuffer(data.byteLength);
|
|
28
|
+
new Uint8Array(buf).set(data);
|
|
29
|
+
return buf;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Derive a 256-bit AES key from password using PBKDF2-SHA256.
|
|
33
|
+
*
|
|
34
|
+
* Uses NIST SP 800-132 recommended parameters:
|
|
35
|
+
* - 310,000 iterations (as of 2025, recommended for 100ms delay on modern hardware)
|
|
36
|
+
* - SHA-256 as PRF
|
|
37
|
+
* - 16-byte salt
|
|
38
|
+
*
|
|
39
|
+
* @param password - User password (UTF-8 string).
|
|
40
|
+
* @param salt - 16-byte random salt.
|
|
41
|
+
* @returns AES-256 key or error.
|
|
42
|
+
*/
|
|
43
|
+
async function deriveKey(password, salt) {
|
|
44
|
+
try {
|
|
45
|
+
if (salt.length !== SALT_LENGTH) {
|
|
46
|
+
return err('INVALID_BACKUP');
|
|
47
|
+
}
|
|
48
|
+
// Import password as PBKDF2 base key
|
|
49
|
+
const baseKey = await crypto.subtle.importKey('raw', new TextEncoder().encode(password), 'PBKDF2', false, ['deriveBits']);
|
|
50
|
+
// Derive 256 bits (32 bytes) for AES-256
|
|
51
|
+
const derivedBits = new Uint8Array(await crypto.subtle.deriveBits({
|
|
52
|
+
name: 'PBKDF2',
|
|
53
|
+
hash: 'SHA-256',
|
|
54
|
+
salt: toArrayBuffer(salt),
|
|
55
|
+
iterations: PBKDF2_ITERATIONS,
|
|
56
|
+
}, baseKey, 256));
|
|
57
|
+
// Import derived key as AES-GCM key
|
|
58
|
+
const aesKey = await crypto.subtle.importKey('raw', toArrayBuffer(derivedBits), { name: 'AES-GCM' }, false, ['encrypt', 'decrypt']);
|
|
59
|
+
return ok(aesKey);
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return err('PBKDF2_FAILED');
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Serialize an AgentIdentity to a plaintext backup payload.
|
|
67
|
+
* (The payload is then encrypted before storage.)
|
|
68
|
+
*/
|
|
69
|
+
async function serializeIdentity(identity) {
|
|
70
|
+
try {
|
|
71
|
+
// Export Ed25519 and X25519 private keys as PKCS8
|
|
72
|
+
const ed25519PkResult = await exportPKCS8(identity.privateKey);
|
|
73
|
+
if (!ed25519PkResult.ok)
|
|
74
|
+
return err('EXPORT_FAILED');
|
|
75
|
+
const x25519PkResult = await exportX25519PKCS8(identity.x25519PrivateKey);
|
|
76
|
+
if (!x25519PkResult.ok)
|
|
77
|
+
return err('EXPORT_FAILED');
|
|
78
|
+
// Collect ML-KEM and ML-DSA keys if present
|
|
79
|
+
const mlKemSecretKey = exportMlKemSecretKey(identity);
|
|
80
|
+
const mlKemPublicKey = exportMlKemPublicKey(identity);
|
|
81
|
+
const mlDsaSecretKey = exportMlDsaSecretKey(identity);
|
|
82
|
+
const mlDsaPublicKey = exportMlDsaPublicKey(identity);
|
|
83
|
+
// Serialize rotated keys
|
|
84
|
+
const rotatedKeys = identity.rotatedKeys
|
|
85
|
+
? await Promise.all(identity.rotatedKeys.map(async (rotated) => {
|
|
86
|
+
const x25519Pk = await exportX25519PKCS8(rotated.x25519PrivateKey);
|
|
87
|
+
if (!x25519Pk.ok)
|
|
88
|
+
throw new Error('Failed to export rotated X25519 key');
|
|
89
|
+
return {
|
|
90
|
+
rotatedAt: rotated.rotatedAt,
|
|
91
|
+
x25519Pkcs8: toBase64(x25519Pk.value),
|
|
92
|
+
...(rotated.mlKemSecretKey ? { mlKemSecretKey: toBase64(rotated.mlKemSecretKey) } : {}),
|
|
93
|
+
};
|
|
94
|
+
}))
|
|
95
|
+
: undefined;
|
|
96
|
+
return ok({
|
|
97
|
+
did: identity.did,
|
|
98
|
+
rawPublicKey: toBase64(identity.rawPublicKey),
|
|
99
|
+
ed25519Pkcs8: toBase64(ed25519PkResult.value),
|
|
100
|
+
x25519Pkcs8: toBase64(x25519PkResult.value),
|
|
101
|
+
...(mlKemSecretKey ? { mlKemSecretKey: toBase64(mlKemSecretKey) } : {}),
|
|
102
|
+
...(mlKemPublicKey ? { mlKemPublicKey: toBase64(mlKemPublicKey) } : {}),
|
|
103
|
+
...(mlDsaSecretKey ? { mlDsaSecretKey: toBase64(mlDsaSecretKey) } : {}),
|
|
104
|
+
...(mlDsaPublicKey ? { mlDsaPublicKey: toBase64(mlDsaPublicKey) } : {}),
|
|
105
|
+
...(rotatedKeys ? { rotatedKeys } : {}),
|
|
106
|
+
exportedAt: Date.now(),
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
return err('EXPORT_FAILED');
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/* ── Export: Encrypt Identity to Backup ── */
|
|
114
|
+
/**
|
|
115
|
+
* Export an identity as an encrypted backup blob.
|
|
116
|
+
*
|
|
117
|
+
* Encrypts the complete identity (Ed25519, X25519, ML-KEM, ML-DSA keys,
|
|
118
|
+
* rotated keys) with a password using PBKDF2 + AES-256-GCM.
|
|
119
|
+
*
|
|
120
|
+
* The backup can be stored safely in cloud storage, version control, or
|
|
121
|
+
* transmitted over untrusted channels. Only the password holder can decrypt.
|
|
122
|
+
*
|
|
123
|
+
* Security:
|
|
124
|
+
* - PBKDF2-SHA256 with 310,000 iterations (≈100ms on modern hardware)
|
|
125
|
+
* - AES-256-GCM for authenticated encryption
|
|
126
|
+
* - Random salt + IV for each backup (no two backups are identical)
|
|
127
|
+
* - 16-byte GCM tag prevents tampering
|
|
128
|
+
*
|
|
129
|
+
* @param identity - Agent identity to backup.
|
|
130
|
+
* @param password - User password (plaintext). Will be PBKDF2 derived.
|
|
131
|
+
* @returns Encrypted backup blob or error.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* import { generateIdentity } from '@private.me/xbind';
|
|
136
|
+
* import { exportBackup } from '@private.me/xbind';
|
|
137
|
+
*
|
|
138
|
+
* const identity = await generateIdentity();
|
|
139
|
+
* if (!identity.ok) throw identity.error;
|
|
140
|
+
*
|
|
141
|
+
* const backup = await exportBackup(identity.value, 'user-password');
|
|
142
|
+
* if (!backup.ok) throw backup.error;
|
|
143
|
+
*
|
|
144
|
+
* // Store backup securely
|
|
145
|
+
* const json = JSON.stringify(backup.value);
|
|
146
|
+
* localStorage.setItem('backup', json);
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
export async function exportBackup(identity, password) {
|
|
150
|
+
try {
|
|
151
|
+
// Serialize identity to plaintext payload
|
|
152
|
+
const payloadResult = await serializeIdentity(identity);
|
|
153
|
+
if (!payloadResult.ok)
|
|
154
|
+
return payloadResult;
|
|
155
|
+
// Generate random salt and IV
|
|
156
|
+
const salt = crypto.getRandomValues(new Uint8Array(SALT_LENGTH));
|
|
157
|
+
const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
|
|
158
|
+
// Derive AES key from password
|
|
159
|
+
const keyResult = await deriveKey(password, salt);
|
|
160
|
+
if (!keyResult.ok)
|
|
161
|
+
return keyResult;
|
|
162
|
+
// Encrypt payload as JSON
|
|
163
|
+
const payloadJson = JSON.stringify(payloadResult.value);
|
|
164
|
+
const plaintext = new TextEncoder().encode(payloadJson);
|
|
165
|
+
// Encrypt with AES-256-GCM
|
|
166
|
+
const encryptResult = await crypto.subtle.encrypt({ name: 'AES-GCM', iv: toArrayBuffer(iv) }, keyResult.value, plaintext);
|
|
167
|
+
const ciphertext = new Uint8Array(encryptResult);
|
|
168
|
+
// GCM includes authentication tag in the ciphertext.
|
|
169
|
+
// Split: ciphertext is everything except last 16 bytes, tag is last 16 bytes.
|
|
170
|
+
// (Web Crypto API appends the tag to the ciphertext)
|
|
171
|
+
if (ciphertext.length < 16) {
|
|
172
|
+
return err('ENCRYPTION_FAILED');
|
|
173
|
+
}
|
|
174
|
+
const ctLen = ciphertext.length - 16;
|
|
175
|
+
const ct = ciphertext.slice(0, ctLen);
|
|
176
|
+
const tag = ciphertext.slice(ctLen);
|
|
177
|
+
return ok({
|
|
178
|
+
version: 1,
|
|
179
|
+
salt: toBase64(salt),
|
|
180
|
+
iv: toBase64(iv),
|
|
181
|
+
ciphertext: toBase64(ct),
|
|
182
|
+
tag: toBase64(tag),
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
return err('ENCRYPTION_FAILED');
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/* ── Import: Decrypt Backup to Identity ── */
|
|
190
|
+
/**
|
|
191
|
+
* Restore an identity from an encrypted backup blob.
|
|
192
|
+
*
|
|
193
|
+
* Decrypts the backup using PBKDF2-derived AES key. Verifies authenticity
|
|
194
|
+
* with GCM tag before returning plaintext keys.
|
|
195
|
+
*
|
|
196
|
+
* @param backup - Encrypted backup blob.
|
|
197
|
+
* @param password - User password (plaintext). Will be PBKDF2 derived.
|
|
198
|
+
* @returns Restored AgentIdentity or error.
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```typescript
|
|
202
|
+
* import { importBackup } from '@private.me/xbind';
|
|
203
|
+
*
|
|
204
|
+
* const json = localStorage.getItem('backup');
|
|
205
|
+
* const backup = JSON.parse(json);
|
|
206
|
+
*
|
|
207
|
+
* const identity = await importBackup(backup, 'user-password');
|
|
208
|
+
* if (!identity.ok) {
|
|
209
|
+
* if (identity.error === 'INVALID_PASSWORD') {
|
|
210
|
+
* console.error('Wrong password!');
|
|
211
|
+
* } else {
|
|
212
|
+
* throw identity.error;
|
|
213
|
+
* }
|
|
214
|
+
* }
|
|
215
|
+
*
|
|
216
|
+
* // Now use restored identity
|
|
217
|
+
* const agent = new Agent(identity.value);
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
export async function importBackup(backup, password) {
|
|
221
|
+
try {
|
|
222
|
+
// Validate version
|
|
223
|
+
if (backup.version !== 1) {
|
|
224
|
+
return err('INVALID_BACKUP');
|
|
225
|
+
}
|
|
226
|
+
// Decode base64 fields
|
|
227
|
+
let salt;
|
|
228
|
+
let iv;
|
|
229
|
+
let ciphertext;
|
|
230
|
+
let tag;
|
|
231
|
+
try {
|
|
232
|
+
salt = fromBase64(backup.salt);
|
|
233
|
+
iv = fromBase64(backup.iv);
|
|
234
|
+
ciphertext = fromBase64(backup.ciphertext);
|
|
235
|
+
tag = fromBase64(backup.tag);
|
|
236
|
+
}
|
|
237
|
+
catch {
|
|
238
|
+
return err('INVALID_BACKUP');
|
|
239
|
+
}
|
|
240
|
+
// Validate lengths
|
|
241
|
+
if (salt.length !== SALT_LENGTH || iv.length !== IV_LENGTH || tag.length !== 16) {
|
|
242
|
+
return err('INVALID_BACKUP');
|
|
243
|
+
}
|
|
244
|
+
// Derive AES key from password
|
|
245
|
+
const keyResult = await deriveKey(password, salt);
|
|
246
|
+
if (!keyResult.ok)
|
|
247
|
+
return keyResult;
|
|
248
|
+
// Reconstruct full GCM input (ciphertext + tag)
|
|
249
|
+
const gcmInput = new Uint8Array(ciphertext.length + tag.length);
|
|
250
|
+
gcmInput.set(ciphertext);
|
|
251
|
+
gcmInput.set(tag, ciphertext.length);
|
|
252
|
+
// Decrypt with AES-256-GCM
|
|
253
|
+
let plaintext;
|
|
254
|
+
try {
|
|
255
|
+
plaintext = await crypto.subtle.decrypt({ name: 'AES-GCM', iv: toArrayBuffer(iv) }, keyResult.value, toArrayBuffer(gcmInput));
|
|
256
|
+
}
|
|
257
|
+
catch (decryptErr) {
|
|
258
|
+
// GCM authentication failure (wrong password or tampered backup)
|
|
259
|
+
console.warn('[xBind] GCM verification failed:', decryptErr);
|
|
260
|
+
return err('INVALID_PASSWORD');
|
|
261
|
+
}
|
|
262
|
+
// Parse JSON payload
|
|
263
|
+
let payload;
|
|
264
|
+
try {
|
|
265
|
+
const jsonStr = new TextDecoder().decode(plaintext);
|
|
266
|
+
payload = JSON.parse(jsonStr);
|
|
267
|
+
}
|
|
268
|
+
catch {
|
|
269
|
+
return err('INVALID_BACKUP');
|
|
270
|
+
}
|
|
271
|
+
// Validate payload structure
|
|
272
|
+
if (!payload.did || !payload.ed25519Pkcs8 || !payload.x25519Pkcs8) {
|
|
273
|
+
return err('INVALID_BACKUP');
|
|
274
|
+
}
|
|
275
|
+
// Decode base64 keys
|
|
276
|
+
let ed25519Pkcs8;
|
|
277
|
+
let x25519Pkcs8;
|
|
278
|
+
let mlKemSecretKey;
|
|
279
|
+
let mlKemPublicKey;
|
|
280
|
+
let mlDsaSecretKey;
|
|
281
|
+
let mlDsaPublicKey;
|
|
282
|
+
try {
|
|
283
|
+
ed25519Pkcs8 = fromBase64(payload.ed25519Pkcs8);
|
|
284
|
+
x25519Pkcs8 = fromBase64(payload.x25519Pkcs8);
|
|
285
|
+
if (payload.mlKemSecretKey)
|
|
286
|
+
mlKemSecretKey = fromBase64(payload.mlKemSecretKey);
|
|
287
|
+
if (payload.mlKemPublicKey)
|
|
288
|
+
mlKemPublicKey = fromBase64(payload.mlKemPublicKey);
|
|
289
|
+
if (payload.mlDsaSecretKey)
|
|
290
|
+
mlDsaSecretKey = fromBase64(payload.mlDsaSecretKey);
|
|
291
|
+
if (payload.mlDsaPublicKey)
|
|
292
|
+
mlDsaPublicKey = fromBase64(payload.mlDsaPublicKey);
|
|
293
|
+
}
|
|
294
|
+
catch {
|
|
295
|
+
return err('INVALID_BACKUP');
|
|
296
|
+
}
|
|
297
|
+
// Import identity from decoded keys
|
|
298
|
+
const identity = await importIdentity(ed25519Pkcs8, x25519Pkcs8, mlKemSecretKey, mlKemPublicKey, mlDsaSecretKey, mlDsaPublicKey);
|
|
299
|
+
if (!identity.ok)
|
|
300
|
+
return err('IMPORT_FAILED');
|
|
301
|
+
// Restore rotated keys if present
|
|
302
|
+
if (payload.rotatedKeys && payload.rotatedKeys.length > 0) {
|
|
303
|
+
const restored = identity.value;
|
|
304
|
+
const rotatedKeys = await Promise.all(payload.rotatedKeys.map(async (rk) => {
|
|
305
|
+
const x25519Pk = fromBase64(rk.x25519Pkcs8);
|
|
306
|
+
const x25519PrivateKey = await crypto.subtle.importKey('pkcs8', toArrayBuffer(x25519Pk), { name: 'X25519' }, true, ['deriveBits']);
|
|
307
|
+
const mlKemSecretKeyRk = rk.mlKemSecretKey ? fromBase64(rk.mlKemSecretKey) : undefined;
|
|
308
|
+
return {
|
|
309
|
+
rotatedAt: rk.rotatedAt,
|
|
310
|
+
x25519PrivateKey,
|
|
311
|
+
...(mlKemSecretKeyRk ? { mlKemSecretKey: mlKemSecretKeyRk } : {}),
|
|
312
|
+
};
|
|
313
|
+
}));
|
|
314
|
+
// Return identity with restored rotated keys
|
|
315
|
+
return ok({
|
|
316
|
+
...restored,
|
|
317
|
+
rotatedKeys: rotatedKeys,
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
return identity;
|
|
321
|
+
}
|
|
322
|
+
catch (importErr) {
|
|
323
|
+
console.warn('[xBind] Import backup failed:', importErr);
|
|
324
|
+
return err('DECRYPTION_FAILED');
|
|
325
|
+
}
|
|
326
|
+
}
|