@haex-space/vault-sdk 2.7.1 → 2.8.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
@@ -3191,76 +3187,17 @@ function base58btcDecode(str) {
3191
3187
  }
3192
3188
  return result;
3193
3189
  }
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]);
3190
+ var ED25519_MULTICODEC_PREFIX = new Uint8Array([237, 1]);
3191
+ var ED25519_PUBLIC_KEY_LENGTH = 32;
3257
3192
  async function publicKeyToDidKeyAsync(publicKeyBase64) {
3258
3193
  const cryptoKey = await importUserPublicKeyAsync(publicKeyBase64);
3259
3194
  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);
3195
+ if (rawBytes.length !== ED25519_PUBLIC_KEY_LENGTH) {
3196
+ throw new Error(`Expected ${ED25519_PUBLIC_KEY_LENGTH}-byte Ed25519 public key, got ${rawBytes.length}`);
3197
+ }
3198
+ const multicodecBytes = new Uint8Array(ED25519_MULTICODEC_PREFIX.length + rawBytes.length);
3199
+ multicodecBytes.set(ED25519_MULTICODEC_PREFIX);
3200
+ multicodecBytes.set(rawBytes, ED25519_MULTICODEC_PREFIX.length);
3264
3201
  return `did:key:z${base58btcEncode(multicodecBytes)}`;
3265
3202
  }
3266
3203
  async function didKeyToPublicKeyAsync(did) {
@@ -3268,15 +3205,17 @@ async function didKeyToPublicKeyAsync(did) {
3268
3205
  throw new Error("Only did:key with base58-btc multibase (z prefix) is supported");
3269
3206
  }
3270
3207
  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)");
3208
+ if (multicodecBytes[0] !== ED25519_MULTICODEC_PREFIX[0] || multicodecBytes[1] !== ED25519_MULTICODEC_PREFIX[1]) {
3209
+ throw new Error("Unsupported key type in did:key (expected Ed25519)");
3210
+ }
3211
+ const rawKey = multicodecBytes.slice(ED25519_MULTICODEC_PREFIX.length);
3212
+ if (rawKey.length !== ED25519_PUBLIC_KEY_LENGTH) {
3213
+ throw new Error(`Invalid Ed25519 public key length: ${rawKey.length}`);
3273
3214
  }
3274
- const compressed = multicodecBytes.slice(P256_MULTICODEC_PREFIX.length);
3275
- const uncompressed = decompressP256Point(compressed);
3276
3215
  const cryptoKey = await crypto.subtle.importKey(
3277
3216
  "raw",
3278
- uncompressed.buffer,
3279
- { name: "ECDSA", namedCurve: "P-256" },
3217
+ rawKey.buffer,
3218
+ SIGNING_ALGO,
3280
3219
  true,
3281
3220
  ["verify"]
3282
3221
  );
@@ -3287,11 +3226,10 @@ async function generateIdentityAsync() {
3287
3226
  const { generateUserKeypairAsync: generateUserKeypairAsync2, exportUserKeypairAsync: exportUserKeypairAsync2 } = await Promise.resolve().then(() => (init_userKeypair(), userKeypair_exports));
3288
3227
  const keypair = await generateUserKeypairAsync2();
3289
3228
  const exported = await exportUserKeypairAsync2(keypair);
3290
- const did = await publicKeyToDidKeyAsync(exported.publicKey);
3229
+ const did = await publicKeyToDidKeyAsync(exported.signingPublicKey);
3291
3230
  return {
3292
3231
  did,
3293
- publicKeyBase64: exported.publicKey,
3294
- privateKeyBase64: exported.privateKey
3232
+ ...exported
3295
3233
  };
3296
3234
  }
3297
3235
 
@@ -3305,7 +3243,7 @@ async function encryptWithPublicKeyAsync(data, recipientPublicKeyBase64) {
3305
3243
  const ephemeral = await crypto.subtle.generateKey(KEY_AGREEMENT_ALGO, true, ["deriveBits"]);
3306
3244
  const recipientKey = await importPublicKeyForKeyAgreementAsync(recipientPublicKeyBase64);
3307
3245
  const sharedBits = await crypto.subtle.deriveBits(
3308
- { name: "ECDH", public: recipientKey },
3246
+ { ...KEY_AGREEMENT_ALGO, public: recipientKey },
3309
3247
  ephemeral.privateKey,
3310
3248
  256
3311
3249
  );
@@ -3340,7 +3278,7 @@ async function decryptWithPrivateKeyAsync(sealed, ownPrivateKeyBase64) {
3340
3278
  );
3341
3279
  const ownPrivKey = await importPrivateKeyForKeyAgreementAsync(ownPrivateKeyBase64);
3342
3280
  const sharedBits = await crypto.subtle.deriveBits(
3343
- { name: "ECDH", public: ephPubKey },
3281
+ { ...KEY_AGREEMENT_ALGO, public: ephPubKey },
3344
3282
  ownPrivKey,
3345
3283
  256
3346
3284
  );
@@ -3394,7 +3332,7 @@ async function signClaimPresentationAsync(did, publicKeyBase64, claims, privateK
3394
3332
  const privateKey = await importUserPrivateKeyAsync(privateKeyBase64);
3395
3333
  const data = new TextEncoder().encode(canonical);
3396
3334
  const sig = await crypto.subtle.sign(
3397
- { name: "ECDSA", hash: "SHA-256" },
3335
+ SIGNING_ALGO,
3398
3336
  privateKey,
3399
3337
  data
3400
3338
  );
@@ -3414,7 +3352,7 @@ async function verifyClaimPresentationAsync(presentation) {
3414
3352
  const data = new TextEncoder().encode(canonical);
3415
3353
  const sigBytes = Uint8Array.from(atob(signature), (c) => c.charCodeAt(0));
3416
3354
  return crypto.subtle.verify(
3417
- { name: "ECDSA", hash: "SHA-256" },
3355
+ SIGNING_ALGO,
3418
3356
  pubKey,
3419
3357
  sigBytes,
3420
3358
  data
@@ -3436,13 +3374,13 @@ function canonicalize(record) {
3436
3374
  }
3437
3375
  async function signRecordAsync(record, privateKeyBase64) {
3438
3376
  const key = await importUserPrivateKeyAsync(privateKeyBase64);
3439
- const sig = await crypto.subtle.sign({ name: "ECDSA", hash: "SHA-256" }, key, canonicalize(record));
3377
+ const sig = await crypto.subtle.sign(SIGNING_ALGO, key, canonicalize(record));
3440
3378
  return arrayBufferToBase64(sig);
3441
3379
  }
3442
3380
  async function verifyRecordSignatureAsync(record, signatureBase64, publicKeyBase64) {
3443
3381
  const key = await importUserPublicKeyAsync(publicKeyBase64);
3444
3382
  return crypto.subtle.verify(
3445
- { name: "ECDSA", hash: "SHA-256" },
3383
+ SIGNING_ALGO,
3446
3384
  key,
3447
3385
  base64ToArrayBuffer(signatureBase64),
3448
3386
  canonicalize(record)
@@ -3456,7 +3394,7 @@ async function signSpaceChallengeAsync(spaceId, privateKeyBase64) {
3456
3394
  const timestamp = (/* @__PURE__ */ new Date()).toISOString();
3457
3395
  const key = await importUserPrivateKeyAsync(privateKeyBase64);
3458
3396
  const sig = await crypto.subtle.sign(
3459
- { name: "ECDSA", hash: "SHA-256" },
3397
+ SIGNING_ALGO,
3460
3398
  key,
3461
3399
  canonicalizeChallenge(spaceId, timestamp)
3462
3400
  );
@@ -3474,7 +3412,7 @@ async function verifySpaceChallengeAsync(spaceId, timestamp, signatureBase64, pu
3474
3412
  try {
3475
3413
  const key = await importUserPublicKeyAsync(publicKeyBase64);
3476
3414
  const isValid = await crypto.subtle.verify(
3477
- { name: "ECDSA", hash: "SHA-256" },
3415
+ SIGNING_ALGO,
3478
3416
  key,
3479
3417
  base64ToArrayBuffer(signatureBase64),
3480
3418
  canonicalizeChallenge(spaceId, timestamp)