@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.js CHANGED
@@ -250,13 +250,28 @@ __export(userKeypair_exports, {
250
250
  importUserPublicKeyAsync: () => importUserPublicKeyAsync
251
251
  });
252
252
  async function generateUserKeypairAsync() {
253
- const keypair = await crypto.subtle.generateKey(exports.SIGNING_ALGO, true, ["sign", "verify"]);
254
- return { publicKey: keypair.publicKey, privateKey: keypair.privateKey };
253
+ const signing = await crypto.subtle.generateKey(exports.SIGNING_ALGO, true, ["sign", "verify"]);
254
+ const agreement = await crypto.subtle.generateKey(exports.KEY_AGREEMENT_ALGO, true, ["deriveBits"]);
255
+ return {
256
+ signingPublicKey: signing.publicKey,
257
+ signingPrivateKey: signing.privateKey,
258
+ agreementPublicKey: agreement.publicKey,
259
+ agreementPrivateKey: agreement.privateKey
260
+ };
255
261
  }
256
262
  async function exportUserKeypairAsync(keypair) {
257
- const pub = await crypto.subtle.exportKey("spki", keypair.publicKey);
258
- const priv = await crypto.subtle.exportKey("pkcs8", keypair.privateKey);
259
- return { publicKey: arrayBufferToBase64(pub), privateKey: arrayBufferToBase64(priv) };
263
+ const [sigPub, sigPriv, agrPub, agrPriv] = await Promise.all([
264
+ crypto.subtle.exportKey("spki", keypair.signingPublicKey),
265
+ crypto.subtle.exportKey("pkcs8", keypair.signingPrivateKey),
266
+ crypto.subtle.exportKey("spki", keypair.agreementPublicKey),
267
+ crypto.subtle.exportKey("pkcs8", keypair.agreementPrivateKey)
268
+ ]);
269
+ return {
270
+ signingPublicKey: arrayBufferToBase64(sigPub),
271
+ signingPrivateKey: arrayBufferToBase64(sigPriv),
272
+ agreementPublicKey: arrayBufferToBase64(agrPub),
273
+ agreementPrivateKey: arrayBufferToBase64(agrPriv)
274
+ };
260
275
  }
261
276
  async function importUserPublicKeyAsync(base64) {
262
277
  return crypto.subtle.importKey("spki", base64ToArrayBuffer(base64), exports.SIGNING_ALGO, true, ["verify"]);
@@ -298,8 +313,8 @@ exports.SIGNING_ALGO = void 0; exports.KEY_AGREEMENT_ALGO = void 0;
298
313
  var init_userKeypair = __esm({
299
314
  "src/crypto/userKeypair.ts"() {
300
315
  init_vaultKey();
301
- exports.SIGNING_ALGO = { name: "ECDSA", namedCurve: "P-256" };
302
- exports.KEY_AGREEMENT_ALGO = { name: "ECDH", namedCurve: "P-256" };
316
+ exports.SIGNING_ALGO = { name: "Ed25519" };
317
+ exports.KEY_AGREEMENT_ALGO = { name: "X25519" };
303
318
  }
304
319
  });
305
320
 
@@ -1900,25 +1915,6 @@ var SpacesAPI = class {
1900
1915
  async listSpacesAsync() {
1901
1916
  return this.client.request(SPACE_COMMANDS.list);
1902
1917
  }
1903
- /**
1904
- * Create a new shared space.
1905
- * @param name - Human-readable space name
1906
- * @param serverUrl - The sync server URL to create the space on
1907
- * @returns The created space with decrypted name
1908
- */
1909
- async createSpaceAsync(name, serverUrl) {
1910
- return this.client.request(SPACE_COMMANDS.create, {
1911
- spaceName: name,
1912
- serverUrl
1913
- });
1914
- }
1915
- /**
1916
- * List available sync backends that can host shared spaces.
1917
- * @returns Array of backend info with server URLs
1918
- */
1919
- async listSyncBackendsAsync() {
1920
- return this.client.request(SPACE_COMMANDS.listBackends);
1921
- }
1922
1918
  };
1923
1919
 
1924
1920
  // src/api/shell.ts
@@ -3033,14 +3029,6 @@ function canExternalClientSendRequests(state) {
3033
3029
  return state === "paired" /* PAIRED */;
3034
3030
  }
3035
3031
 
3036
- // src/types/spaces.ts
3037
- var SpaceRoles = {
3038
- OWNER: "owner",
3039
- ADMIN: "admin",
3040
- MEMBER: "member",
3041
- READER: "reader"
3042
- };
3043
-
3044
3032
  // src/crypto/verify.ts
3045
3033
  function sortObjectKeysRecursively(obj) {
3046
3034
  if (typeof obj !== "object" || obj === null) {
@@ -3193,76 +3181,17 @@ function base58btcDecode(str) {
3193
3181
  }
3194
3182
  return result;
3195
3183
  }
3196
- function compressP256Point(uncompressed) {
3197
- if (uncompressed.length !== 65 || uncompressed[0] !== 4) {
3198
- throw new Error("Expected 65-byte uncompressed P-256 point (0x04 prefix)");
3199
- }
3200
- const x = uncompressed.slice(1, 33);
3201
- const yLastByte = uncompressed[64];
3202
- const prefix = (yLastByte & 1) === 0 ? 2 : 3;
3203
- const compressed = new Uint8Array(33);
3204
- compressed[0] = prefix;
3205
- compressed.set(x, 1);
3206
- return compressed;
3207
- }
3208
- function decompressP256Point(compressed) {
3209
- if (compressed.length !== 33 || compressed[0] !== 2 && compressed[0] !== 3) {
3210
- throw new Error("Expected 33-byte compressed P-256 point");
3211
- }
3212
- const isOdd = compressed[0] === 3;
3213
- const x = bytesToBigInt(compressed.slice(1));
3214
- const p = BigInt("0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff");
3215
- const b = BigInt("0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b");
3216
- const a = p - 3n;
3217
- const ySquared = (modPow(x, 3n, p) + a * x + b) % p;
3218
- let y = modSqrt(ySquared, p);
3219
- const yIsOdd = (y & 1n) === 1n;
3220
- if (isOdd !== yIsOdd) {
3221
- y = p - y;
3222
- }
3223
- const uncompressed = new Uint8Array(65);
3224
- uncompressed[0] = 4;
3225
- uncompressed.set(bigIntToBytes(x, 32), 1);
3226
- uncompressed.set(bigIntToBytes(y, 32), 33);
3227
- return uncompressed;
3228
- }
3229
- function bytesToBigInt(bytes) {
3230
- let result = 0n;
3231
- for (const b of bytes) {
3232
- result = result << 8n | BigInt(b);
3233
- }
3234
- return result;
3235
- }
3236
- function bigIntToBytes(n, length) {
3237
- const bytes = new Uint8Array(length);
3238
- let val = n;
3239
- for (let i = length - 1; i >= 0; i--) {
3240
- bytes[i] = Number(val & 0xffn);
3241
- val >>= 8n;
3242
- }
3243
- return bytes;
3244
- }
3245
- function modPow(base, exp, mod) {
3246
- let result = 1n;
3247
- base = base % mod;
3248
- while (exp > 0n) {
3249
- if (exp & 1n) result = result * base % mod;
3250
- exp >>= 1n;
3251
- base = base * base % mod;
3252
- }
3253
- return result;
3254
- }
3255
- function modSqrt(a, p) {
3256
- return modPow(a, (p + 1n) / 4n, p);
3257
- }
3258
- var P256_MULTICODEC_PREFIX = new Uint8Array([128, 36]);
3184
+ var ED25519_MULTICODEC_PREFIX = new Uint8Array([237, 1]);
3185
+ var ED25519_PUBLIC_KEY_LENGTH = 32;
3259
3186
  async function publicKeyToDidKeyAsync(publicKeyBase64) {
3260
3187
  const cryptoKey = await importUserPublicKeyAsync(publicKeyBase64);
3261
3188
  const rawBytes = new Uint8Array(await crypto.subtle.exportKey("raw", cryptoKey));
3262
- const compressed = compressP256Point(rawBytes);
3263
- const multicodecBytes = new Uint8Array(P256_MULTICODEC_PREFIX.length + compressed.length);
3264
- multicodecBytes.set(P256_MULTICODEC_PREFIX);
3265
- multicodecBytes.set(compressed, P256_MULTICODEC_PREFIX.length);
3189
+ if (rawBytes.length !== ED25519_PUBLIC_KEY_LENGTH) {
3190
+ throw new Error(`Expected ${ED25519_PUBLIC_KEY_LENGTH}-byte Ed25519 public key, got ${rawBytes.length}`);
3191
+ }
3192
+ const multicodecBytes = new Uint8Array(ED25519_MULTICODEC_PREFIX.length + rawBytes.length);
3193
+ multicodecBytes.set(ED25519_MULTICODEC_PREFIX);
3194
+ multicodecBytes.set(rawBytes, ED25519_MULTICODEC_PREFIX.length);
3266
3195
  return `did:key:z${base58btcEncode(multicodecBytes)}`;
3267
3196
  }
3268
3197
  async function didKeyToPublicKeyAsync(did) {
@@ -3270,15 +3199,17 @@ async function didKeyToPublicKeyAsync(did) {
3270
3199
  throw new Error("Only did:key with base58-btc multibase (z prefix) is supported");
3271
3200
  }
3272
3201
  const multicodecBytes = base58btcDecode(did.slice("did:key:z".length));
3273
- if (multicodecBytes[0] !== P256_MULTICODEC_PREFIX[0] || multicodecBytes[1] !== P256_MULTICODEC_PREFIX[1]) {
3274
- throw new Error("Unsupported key type in did:key (expected P-256)");
3202
+ if (multicodecBytes[0] !== ED25519_MULTICODEC_PREFIX[0] || multicodecBytes[1] !== ED25519_MULTICODEC_PREFIX[1]) {
3203
+ throw new Error("Unsupported key type in did:key (expected Ed25519)");
3204
+ }
3205
+ const rawKey = multicodecBytes.slice(ED25519_MULTICODEC_PREFIX.length);
3206
+ if (rawKey.length !== ED25519_PUBLIC_KEY_LENGTH) {
3207
+ throw new Error(`Invalid Ed25519 public key length: ${rawKey.length}`);
3275
3208
  }
3276
- const compressed = multicodecBytes.slice(P256_MULTICODEC_PREFIX.length);
3277
- const uncompressed = decompressP256Point(compressed);
3278
3209
  const cryptoKey = await crypto.subtle.importKey(
3279
3210
  "raw",
3280
- uncompressed.buffer,
3281
- { name: "ECDSA", namedCurve: "P-256" },
3211
+ rawKey.buffer,
3212
+ exports.SIGNING_ALGO,
3282
3213
  true,
3283
3214
  ["verify"]
3284
3215
  );
@@ -3289,11 +3220,10 @@ async function generateIdentityAsync() {
3289
3220
  const { generateUserKeypairAsync: generateUserKeypairAsync2, exportUserKeypairAsync: exportUserKeypairAsync2 } = await Promise.resolve().then(() => (init_userKeypair(), userKeypair_exports));
3290
3221
  const keypair = await generateUserKeypairAsync2();
3291
3222
  const exported = await exportUserKeypairAsync2(keypair);
3292
- const did = await publicKeyToDidKeyAsync(exported.publicKey);
3223
+ const did = await publicKeyToDidKeyAsync(exported.signingPublicKey);
3293
3224
  return {
3294
3225
  did,
3295
- publicKeyBase64: exported.publicKey,
3296
- privateKeyBase64: exported.privateKey
3226
+ ...exported
3297
3227
  };
3298
3228
  }
3299
3229
 
@@ -3307,7 +3237,7 @@ async function encryptWithPublicKeyAsync(data, recipientPublicKeyBase64) {
3307
3237
  const ephemeral = await crypto.subtle.generateKey(exports.KEY_AGREEMENT_ALGO, true, ["deriveBits"]);
3308
3238
  const recipientKey = await importPublicKeyForKeyAgreementAsync(recipientPublicKeyBase64);
3309
3239
  const sharedBits = await crypto.subtle.deriveBits(
3310
- { name: "ECDH", public: recipientKey },
3240
+ { ...exports.KEY_AGREEMENT_ALGO, public: recipientKey },
3311
3241
  ephemeral.privateKey,
3312
3242
  256
3313
3243
  );
@@ -3342,7 +3272,7 @@ async function decryptWithPrivateKeyAsync(sealed, ownPrivateKeyBase64) {
3342
3272
  );
3343
3273
  const ownPrivKey = await importPrivateKeyForKeyAgreementAsync(ownPrivateKeyBase64);
3344
3274
  const sharedBits = await crypto.subtle.deriveBits(
3345
- { name: "ECDH", public: ephPubKey },
3275
+ { ...exports.KEY_AGREEMENT_ALGO, public: ephPubKey },
3346
3276
  ownPrivKey,
3347
3277
  256
3348
3278
  );
@@ -3396,7 +3326,7 @@ async function signClaimPresentationAsync(did, publicKeyBase64, claims, privateK
3396
3326
  const privateKey = await importUserPrivateKeyAsync(privateKeyBase64);
3397
3327
  const data = new TextEncoder().encode(canonical);
3398
3328
  const sig = await crypto.subtle.sign(
3399
- { name: "ECDSA", hash: "SHA-256" },
3329
+ exports.SIGNING_ALGO,
3400
3330
  privateKey,
3401
3331
  data
3402
3332
  );
@@ -3416,7 +3346,7 @@ async function verifyClaimPresentationAsync(presentation) {
3416
3346
  const data = new TextEncoder().encode(canonical);
3417
3347
  const sigBytes = Uint8Array.from(atob(signature), (c) => c.charCodeAt(0));
3418
3348
  return crypto.subtle.verify(
3419
- { name: "ECDSA", hash: "SHA-256" },
3349
+ exports.SIGNING_ALGO,
3420
3350
  pubKey,
3421
3351
  sigBytes,
3422
3352
  data
@@ -3438,13 +3368,13 @@ function canonicalize(record) {
3438
3368
  }
3439
3369
  async function signRecordAsync(record, privateKeyBase64) {
3440
3370
  const key = await importUserPrivateKeyAsync(privateKeyBase64);
3441
- const sig = await crypto.subtle.sign({ name: "ECDSA", hash: "SHA-256" }, key, canonicalize(record));
3371
+ const sig = await crypto.subtle.sign(exports.SIGNING_ALGO, key, canonicalize(record));
3442
3372
  return arrayBufferToBase64(sig);
3443
3373
  }
3444
3374
  async function verifyRecordSignatureAsync(record, signatureBase64, publicKeyBase64) {
3445
3375
  const key = await importUserPublicKeyAsync(publicKeyBase64);
3446
3376
  return crypto.subtle.verify(
3447
- { name: "ECDSA", hash: "SHA-256" },
3377
+ exports.SIGNING_ALGO,
3448
3378
  key,
3449
3379
  base64ToArrayBuffer(signatureBase64),
3450
3380
  canonicalize(record)
@@ -3458,7 +3388,7 @@ async function signSpaceChallengeAsync(spaceId, privateKeyBase64) {
3458
3388
  const timestamp = (/* @__PURE__ */ new Date()).toISOString();
3459
3389
  const key = await importUserPrivateKeyAsync(privateKeyBase64);
3460
3390
  const sig = await crypto.subtle.sign(
3461
- { name: "ECDSA", hash: "SHA-256" },
3391
+ exports.SIGNING_ALGO,
3462
3392
  key,
3463
3393
  canonicalizeChallenge(spaceId, timestamp)
3464
3394
  );
@@ -3476,7 +3406,7 @@ async function verifySpaceChallengeAsync(spaceId, timestamp, signatureBase64, pu
3476
3406
  try {
3477
3407
  const key = await importUserPublicKeyAsync(publicKeyBase64);
3478
3408
  const isValid = await crypto.subtle.verify(
3479
- { name: "ECDSA", hash: "SHA-256" },
3409
+ exports.SIGNING_ALGO,
3480
3410
  key,
3481
3411
  base64ToArrayBuffer(signatureBase64),
3482
3412
  canonicalizeChallenge(spaceId, timestamp)
@@ -3680,7 +3610,6 @@ exports.RemoteStorageAPI = RemoteStorageAPI;
3680
3610
  exports.SHELL_EVENTS = SHELL_EVENTS;
3681
3611
  exports.SPACE_COMMANDS = SPACE_COMMANDS;
3682
3612
  exports.ShellAPI = ShellAPI;
3683
- exports.SpaceRoles = SpaceRoles;
3684
3613
  exports.SpacesAPI = SpacesAPI;
3685
3614
  exports.TABLE_SEPARATOR = TABLE_SEPARATOR;
3686
3615
  exports.TAURI_COMMANDS = TAURI_COMMANDS;