@haex-space/vault-sdk 2.7.2 → 2.9.0

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/dist/index.mjs CHANGED
@@ -248,13 +248,28 @@ __export(userKeypair_exports, {
248
248
  importUserPublicKeyAsync: () => importUserPublicKeyAsync
249
249
  });
250
250
  async function generateUserKeypairAsync() {
251
- const keypair = await crypto.subtle.generateKey(SIGNING_ALGO, true, ["sign", "verify"]);
252
- return { publicKey: keypair.publicKey, privateKey: keypair.privateKey };
251
+ const signing = await crypto.subtle.generateKey(SIGNING_ALGO, true, ["sign", "verify"]);
252
+ const agreement = await crypto.subtle.generateKey(KEY_AGREEMENT_ALGO, true, ["deriveBits"]);
253
+ return {
254
+ signingPublicKey: signing.publicKey,
255
+ signingPrivateKey: signing.privateKey,
256
+ agreementPublicKey: agreement.publicKey,
257
+ agreementPrivateKey: agreement.privateKey
258
+ };
253
259
  }
254
260
  async function exportUserKeypairAsync(keypair) {
255
- const pub = await crypto.subtle.exportKey("spki", keypair.publicKey);
256
- const priv = await crypto.subtle.exportKey("pkcs8", keypair.privateKey);
257
- return { publicKey: arrayBufferToBase64(pub), privateKey: arrayBufferToBase64(priv) };
261
+ const [sigPub, sigPriv, agrPub, agrPriv] = await Promise.all([
262
+ crypto.subtle.exportKey("spki", keypair.signingPublicKey),
263
+ crypto.subtle.exportKey("pkcs8", keypair.signingPrivateKey),
264
+ crypto.subtle.exportKey("spki", keypair.agreementPublicKey),
265
+ crypto.subtle.exportKey("pkcs8", keypair.agreementPrivateKey)
266
+ ]);
267
+ return {
268
+ signingPublicKey: arrayBufferToBase64(sigPub),
269
+ signingPrivateKey: arrayBufferToBase64(sigPriv),
270
+ agreementPublicKey: arrayBufferToBase64(agrPub),
271
+ agreementPrivateKey: arrayBufferToBase64(agrPriv)
272
+ };
258
273
  }
259
274
  async function importUserPublicKeyAsync(base64) {
260
275
  return crypto.subtle.importKey("spki", base64ToArrayBuffer(base64), SIGNING_ALGO, true, ["verify"]);
@@ -296,8 +311,8 @@ var SIGNING_ALGO, KEY_AGREEMENT_ALGO;
296
311
  var init_userKeypair = __esm({
297
312
  "src/crypto/userKeypair.ts"() {
298
313
  init_vaultKey();
299
- SIGNING_ALGO = { name: "ECDSA", namedCurve: "P-256" };
300
- KEY_AGREEMENT_ALGO = { name: "ECDH", namedCurve: "P-256" };
314
+ SIGNING_ALGO = { name: "Ed25519" };
315
+ KEY_AGREEMENT_ALGO = { name: "X25519" };
301
316
  }
302
317
  });
303
318
 
@@ -1898,25 +1913,6 @@ var SpacesAPI = class {
1898
1913
  async listSpacesAsync() {
1899
1914
  return this.client.request(SPACE_COMMANDS.list);
1900
1915
  }
1901
- /**
1902
- * Create a new shared space.
1903
- * @param name - Human-readable space name
1904
- * @param serverUrl - The sync server URL to create the space on
1905
- * @returns The created space with decrypted name
1906
- */
1907
- async createSpaceAsync(name, serverUrl) {
1908
- return this.client.request(SPACE_COMMANDS.create, {
1909
- spaceName: name,
1910
- serverUrl
1911
- });
1912
- }
1913
- /**
1914
- * List available sync backends that can host shared spaces.
1915
- * @returns Array of backend info with server URLs
1916
- */
1917
- async listSyncBackendsAsync() {
1918
- return this.client.request(SPACE_COMMANDS.listBackends);
1919
- }
1920
1916
  };
1921
1917
 
1922
1918
  // src/api/shell.ts
@@ -3031,14 +3027,6 @@ function canExternalClientSendRequests(state) {
3031
3027
  return state === "paired" /* PAIRED */;
3032
3028
  }
3033
3029
 
3034
- // src/types/spaces.ts
3035
- var SpaceRoles = {
3036
- OWNER: "owner",
3037
- ADMIN: "admin",
3038
- MEMBER: "member",
3039
- READER: "reader"
3040
- };
3041
-
3042
3030
  // src/crypto/verify.ts
3043
3031
  function sortObjectKeysRecursively(obj) {
3044
3032
  if (typeof obj !== "object" || obj === null) {
@@ -3191,76 +3179,17 @@ function base58btcDecode(str) {
3191
3179
  }
3192
3180
  return result;
3193
3181
  }
3194
- function compressP256Point(uncompressed) {
3195
- if (uncompressed.length !== 65 || uncompressed[0] !== 4) {
3196
- throw new Error("Expected 65-byte uncompressed P-256 point (0x04 prefix)");
3197
- }
3198
- const x = uncompressed.slice(1, 33);
3199
- const yLastByte = uncompressed[64];
3200
- const prefix = (yLastByte & 1) === 0 ? 2 : 3;
3201
- const compressed = new Uint8Array(33);
3202
- compressed[0] = prefix;
3203
- compressed.set(x, 1);
3204
- return compressed;
3205
- }
3206
- function decompressP256Point(compressed) {
3207
- if (compressed.length !== 33 || compressed[0] !== 2 && compressed[0] !== 3) {
3208
- throw new Error("Expected 33-byte compressed P-256 point");
3209
- }
3210
- const isOdd = compressed[0] === 3;
3211
- const x = bytesToBigInt(compressed.slice(1));
3212
- const p = BigInt("0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff");
3213
- const b = BigInt("0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b");
3214
- const a = p - 3n;
3215
- const ySquared = (modPow(x, 3n, p) + a * x + b) % p;
3216
- let y = modSqrt(ySquared, p);
3217
- const yIsOdd = (y & 1n) === 1n;
3218
- if (isOdd !== yIsOdd) {
3219
- y = p - y;
3220
- }
3221
- const uncompressed = new Uint8Array(65);
3222
- uncompressed[0] = 4;
3223
- uncompressed.set(bigIntToBytes(x, 32), 1);
3224
- uncompressed.set(bigIntToBytes(y, 32), 33);
3225
- return uncompressed;
3226
- }
3227
- function bytesToBigInt(bytes) {
3228
- let result = 0n;
3229
- for (const b of bytes) {
3230
- result = result << 8n | BigInt(b);
3231
- }
3232
- return result;
3233
- }
3234
- function bigIntToBytes(n, length) {
3235
- const bytes = new Uint8Array(length);
3236
- let val = n;
3237
- for (let i = length - 1; i >= 0; i--) {
3238
- bytes[i] = Number(val & 0xffn);
3239
- val >>= 8n;
3240
- }
3241
- return bytes;
3242
- }
3243
- function modPow(base, exp, mod) {
3244
- let result = 1n;
3245
- base = base % mod;
3246
- while (exp > 0n) {
3247
- if (exp & 1n) result = result * base % mod;
3248
- exp >>= 1n;
3249
- base = base * base % mod;
3250
- }
3251
- return result;
3252
- }
3253
- function modSqrt(a, p) {
3254
- return modPow(a, (p + 1n) / 4n, p);
3255
- }
3256
- var P256_MULTICODEC_PREFIX = new Uint8Array([128, 36]);
3182
+ var ED25519_MULTICODEC_PREFIX = new Uint8Array([237, 1]);
3183
+ var ED25519_PUBLIC_KEY_LENGTH = 32;
3257
3184
  async function publicKeyToDidKeyAsync(publicKeyBase64) {
3258
3185
  const cryptoKey = await importUserPublicKeyAsync(publicKeyBase64);
3259
3186
  const rawBytes = new Uint8Array(await crypto.subtle.exportKey("raw", cryptoKey));
3260
- const compressed = compressP256Point(rawBytes);
3261
- const multicodecBytes = new Uint8Array(P256_MULTICODEC_PREFIX.length + compressed.length);
3262
- multicodecBytes.set(P256_MULTICODEC_PREFIX);
3263
- multicodecBytes.set(compressed, P256_MULTICODEC_PREFIX.length);
3187
+ if (rawBytes.length !== ED25519_PUBLIC_KEY_LENGTH) {
3188
+ throw new Error(`Expected ${ED25519_PUBLIC_KEY_LENGTH}-byte Ed25519 public key, got ${rawBytes.length}`);
3189
+ }
3190
+ const multicodecBytes = new Uint8Array(ED25519_MULTICODEC_PREFIX.length + rawBytes.length);
3191
+ multicodecBytes.set(ED25519_MULTICODEC_PREFIX);
3192
+ multicodecBytes.set(rawBytes, ED25519_MULTICODEC_PREFIX.length);
3264
3193
  return `did:key:z${base58btcEncode(multicodecBytes)}`;
3265
3194
  }
3266
3195
  async function didKeyToPublicKeyAsync(did) {
@@ -3268,15 +3197,17 @@ async function didKeyToPublicKeyAsync(did) {
3268
3197
  throw new Error("Only did:key with base58-btc multibase (z prefix) is supported");
3269
3198
  }
3270
3199
  const multicodecBytes = base58btcDecode(did.slice("did:key:z".length));
3271
- if (multicodecBytes[0] !== P256_MULTICODEC_PREFIX[0] || multicodecBytes[1] !== P256_MULTICODEC_PREFIX[1]) {
3272
- throw new Error("Unsupported key type in did:key (expected P-256)");
3200
+ if (multicodecBytes[0] !== ED25519_MULTICODEC_PREFIX[0] || multicodecBytes[1] !== ED25519_MULTICODEC_PREFIX[1]) {
3201
+ throw new Error("Unsupported key type in did:key (expected Ed25519)");
3202
+ }
3203
+ const rawKey = multicodecBytes.slice(ED25519_MULTICODEC_PREFIX.length);
3204
+ if (rawKey.length !== ED25519_PUBLIC_KEY_LENGTH) {
3205
+ throw new Error(`Invalid Ed25519 public key length: ${rawKey.length}`);
3273
3206
  }
3274
- const compressed = multicodecBytes.slice(P256_MULTICODEC_PREFIX.length);
3275
- const uncompressed = decompressP256Point(compressed);
3276
3207
  const cryptoKey = await crypto.subtle.importKey(
3277
3208
  "raw",
3278
- uncompressed.buffer,
3279
- { name: "ECDSA", namedCurve: "P-256" },
3209
+ rawKey.buffer,
3210
+ SIGNING_ALGO,
3280
3211
  true,
3281
3212
  ["verify"]
3282
3213
  );
@@ -3287,11 +3218,10 @@ async function generateIdentityAsync() {
3287
3218
  const { generateUserKeypairAsync: generateUserKeypairAsync2, exportUserKeypairAsync: exportUserKeypairAsync2 } = await Promise.resolve().then(() => (init_userKeypair(), userKeypair_exports));
3288
3219
  const keypair = await generateUserKeypairAsync2();
3289
3220
  const exported = await exportUserKeypairAsync2(keypair);
3290
- const did = await publicKeyToDidKeyAsync(exported.publicKey);
3221
+ const did = await publicKeyToDidKeyAsync(exported.signingPublicKey);
3291
3222
  return {
3292
3223
  did,
3293
- publicKeyBase64: exported.publicKey,
3294
- privateKeyBase64: exported.privateKey
3224
+ ...exported
3295
3225
  };
3296
3226
  }
3297
3227
 
@@ -3305,7 +3235,7 @@ async function encryptWithPublicKeyAsync(data, recipientPublicKeyBase64) {
3305
3235
  const ephemeral = await crypto.subtle.generateKey(KEY_AGREEMENT_ALGO, true, ["deriveBits"]);
3306
3236
  const recipientKey = await importPublicKeyForKeyAgreementAsync(recipientPublicKeyBase64);
3307
3237
  const sharedBits = await crypto.subtle.deriveBits(
3308
- { name: "ECDH", public: recipientKey },
3238
+ { ...KEY_AGREEMENT_ALGO, public: recipientKey },
3309
3239
  ephemeral.privateKey,
3310
3240
  256
3311
3241
  );
@@ -3340,7 +3270,7 @@ async function decryptWithPrivateKeyAsync(sealed, ownPrivateKeyBase64) {
3340
3270
  );
3341
3271
  const ownPrivKey = await importPrivateKeyForKeyAgreementAsync(ownPrivateKeyBase64);
3342
3272
  const sharedBits = await crypto.subtle.deriveBits(
3343
- { name: "ECDH", public: ephPubKey },
3273
+ { ...KEY_AGREEMENT_ALGO, public: ephPubKey },
3344
3274
  ownPrivKey,
3345
3275
  256
3346
3276
  );
@@ -3394,7 +3324,7 @@ async function signClaimPresentationAsync(did, publicKeyBase64, claims, privateK
3394
3324
  const privateKey = await importUserPrivateKeyAsync(privateKeyBase64);
3395
3325
  const data = new TextEncoder().encode(canonical);
3396
3326
  const sig = await crypto.subtle.sign(
3397
- { name: "ECDSA", hash: "SHA-256" },
3327
+ SIGNING_ALGO,
3398
3328
  privateKey,
3399
3329
  data
3400
3330
  );
@@ -3414,7 +3344,7 @@ async function verifyClaimPresentationAsync(presentation) {
3414
3344
  const data = new TextEncoder().encode(canonical);
3415
3345
  const sigBytes = Uint8Array.from(atob(signature), (c) => c.charCodeAt(0));
3416
3346
  return crypto.subtle.verify(
3417
- { name: "ECDSA", hash: "SHA-256" },
3347
+ SIGNING_ALGO,
3418
3348
  pubKey,
3419
3349
  sigBytes,
3420
3350
  data
@@ -3436,13 +3366,13 @@ function canonicalize(record) {
3436
3366
  }
3437
3367
  async function signRecordAsync(record, privateKeyBase64) {
3438
3368
  const key = await importUserPrivateKeyAsync(privateKeyBase64);
3439
- const sig = await crypto.subtle.sign({ name: "ECDSA", hash: "SHA-256" }, key, canonicalize(record));
3369
+ const sig = await crypto.subtle.sign(SIGNING_ALGO, key, canonicalize(record));
3440
3370
  return arrayBufferToBase64(sig);
3441
3371
  }
3442
3372
  async function verifyRecordSignatureAsync(record, signatureBase64, publicKeyBase64) {
3443
3373
  const key = await importUserPublicKeyAsync(publicKeyBase64);
3444
3374
  return crypto.subtle.verify(
3445
- { name: "ECDSA", hash: "SHA-256" },
3375
+ SIGNING_ALGO,
3446
3376
  key,
3447
3377
  base64ToArrayBuffer(signatureBase64),
3448
3378
  canonicalize(record)
@@ -3456,7 +3386,7 @@ async function signSpaceChallengeAsync(spaceId, privateKeyBase64) {
3456
3386
  const timestamp = (/* @__PURE__ */ new Date()).toISOString();
3457
3387
  const key = await importUserPrivateKeyAsync(privateKeyBase64);
3458
3388
  const sig = await crypto.subtle.sign(
3459
- { name: "ECDSA", hash: "SHA-256" },
3389
+ SIGNING_ALGO,
3460
3390
  key,
3461
3391
  canonicalizeChallenge(spaceId, timestamp)
3462
3392
  );
@@ -3474,7 +3404,7 @@ async function verifySpaceChallengeAsync(spaceId, timestamp, signatureBase64, pu
3474
3404
  try {
3475
3405
  const key = await importUserPublicKeyAsync(publicKeyBase64);
3476
3406
  const isValid = await crypto.subtle.verify(
3477
- { name: "ECDSA", hash: "SHA-256" },
3407
+ SIGNING_ALGO,
3478
3408
  key,
3479
3409
  base64ToArrayBuffer(signatureBase64),
3480
3410
  canonicalizeChallenge(spaceId, timestamp)
@@ -3656,6 +3586,6 @@ function createHaexVaultSdk(config = {}) {
3656
3586
  return new HaexVaultSdk(config);
3657
3587
  }
3658
3588
 
3659
- export { COSE_ALGORITHM, DEFAULT_TIMEOUT, DatabaseAPI, EXTERNAL_EVENTS, ErrorCode, ExternalConnectionErrorCode, ExternalConnectionState, FilesystemAPI, HAEXSPACE_MESSAGE_TYPES, HAEXTENSION_EVENTS, HaexVaultSdk, HaexVaultSdkError, KEY_AGREEMENT_ALGO, KnownPath, LOCALSEND_EVENTS, LocalSendAPI, PermissionErrorCode, PermissionStatus, PermissionsAPI, RemoteStorageAPI, SHELL_EVENTS, SIGNING_ALGO, SPACE_COMMANDS, ShellAPI, SpaceRoles, SpacesAPI, TABLE_SEPARATOR, TAURI_COMMANDS, WebAPI, arrayBufferToBase64, base58btcDecode, base58btcEncode, base64ToArrayBuffer, canExternalClientSendRequests, createHaexVaultSdk, decryptCrdtData, decryptPrivateKeyAsync, decryptSpaceNameAsync, decryptString, decryptVaultKey, decryptVaultName, decryptWithPrivateKeyAsync, deriveKeyFromPassword, didKeyToPublicKeyAsync, encryptCrdtData, encryptPrivateKeyAsync, encryptSpaceNameAsync, encryptString, encryptVaultKey, encryptWithPublicKeyAsync, exportKeyPairAsync, exportPrivateKeyAsync, exportPublicKeyAsync, exportPublicKeyCoseAsync, exportUserKeypairAsync, generateCredentialId, generateIdentityAsync, generatePasskeyPairAsync, generateSpaceKey, generateUserKeypairAsync, generateVaultKey, getTableName, hexToBytes, importPrivateKeyAsync, importPrivateKeyForKeyAgreementAsync, importPublicKeyAsync, importPublicKeyForKeyAgreementAsync, importUserPrivateKeyAsync, importUserPublicKeyAsync, installBaseTag, installCookiePolyfill, installHistoryPolyfill, installLocalStoragePolyfill, installPolyfills, installSessionStoragePolyfill, isExternalClientConnected, isPermissionDeniedError, isPermissionError, isPermissionPromptError, publicKeyToDidKeyAsync, signClaimPresentationAsync, signRecordAsync, signSpaceChallengeAsync, signWithPasskeyAsync, sortObjectKeysRecursively, unwrapKey, verifyClaimPresentationAsync, verifyExtensionSignature, verifyRecordSignatureAsync, verifySpaceChallengeAsync, verifyWithPasskeyAsync, wrapKey };
3589
+ export { COSE_ALGORITHM, DEFAULT_TIMEOUT, DatabaseAPI, EXTERNAL_EVENTS, ErrorCode, ExternalConnectionErrorCode, ExternalConnectionState, FilesystemAPI, HAEXSPACE_MESSAGE_TYPES, HAEXTENSION_EVENTS, HaexVaultSdk, HaexVaultSdkError, KEY_AGREEMENT_ALGO, KnownPath, LOCALSEND_EVENTS, LocalSendAPI, PermissionErrorCode, PermissionStatus, PermissionsAPI, RemoteStorageAPI, SHELL_EVENTS, SIGNING_ALGO, SPACE_COMMANDS, ShellAPI, SpacesAPI, TABLE_SEPARATOR, TAURI_COMMANDS, WebAPI, arrayBufferToBase64, base58btcDecode, base58btcEncode, base64ToArrayBuffer, canExternalClientSendRequests, createHaexVaultSdk, decryptCrdtData, decryptPrivateKeyAsync, decryptSpaceNameAsync, decryptString, decryptVaultKey, decryptVaultName, decryptWithPrivateKeyAsync, deriveKeyFromPassword, didKeyToPublicKeyAsync, encryptCrdtData, encryptPrivateKeyAsync, encryptSpaceNameAsync, encryptString, encryptVaultKey, encryptWithPublicKeyAsync, exportKeyPairAsync, exportPrivateKeyAsync, exportPublicKeyAsync, exportPublicKeyCoseAsync, exportUserKeypairAsync, generateCredentialId, generateIdentityAsync, generatePasskeyPairAsync, generateSpaceKey, generateUserKeypairAsync, generateVaultKey, getTableName, hexToBytes, importPrivateKeyAsync, importPrivateKeyForKeyAgreementAsync, importPublicKeyAsync, importPublicKeyForKeyAgreementAsync, importUserPrivateKeyAsync, importUserPublicKeyAsync, installBaseTag, installCookiePolyfill, installHistoryPolyfill, installLocalStoragePolyfill, installPolyfills, installSessionStoragePolyfill, isExternalClientConnected, isPermissionDeniedError, isPermissionError, isPermissionPromptError, publicKeyToDidKeyAsync, signClaimPresentationAsync, signRecordAsync, signSpaceChallengeAsync, signWithPasskeyAsync, sortObjectKeysRecursively, unwrapKey, verifyClaimPresentationAsync, verifyExtensionSignature, verifyRecordSignatureAsync, verifySpaceChallengeAsync, verifyWithPasskeyAsync, wrapKey };
3660
3590
  //# sourceMappingURL=index.mjs.map
3661
3591
  //# sourceMappingURL=index.mjs.map