@fileverse/api 0.0.13 → 0.0.14

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
@@ -1373,13 +1373,114 @@ var init_key_store = __esm({
1373
1373
  }
1374
1374
  });
1375
1375
 
1376
+ // src/sdk/ucan.ts
1377
+ import { sign, extractPublicKeyFromSecretKey } from "@stablelib/ed25519";
1378
+ import { toUint8Array } from "js-base64";
1379
+ function base58btcEncode(bytes) {
1380
+ const digits = [0];
1381
+ for (const byte of bytes) {
1382
+ let carry = byte;
1383
+ for (let j = 0; j < digits.length; j++) {
1384
+ carry += digits[j] << 8;
1385
+ digits[j] = carry % 58;
1386
+ carry = carry / 58 | 0;
1387
+ }
1388
+ while (carry > 0) {
1389
+ digits.push(carry % 58);
1390
+ carry = carry / 58 | 0;
1391
+ }
1392
+ }
1393
+ let result = "";
1394
+ for (let i = 0; i < bytes.length && bytes[i] === 0; i++) {
1395
+ result += BASE58_ALPHABET[0];
1396
+ }
1397
+ for (let i = digits.length - 1; i >= 0; i--) {
1398
+ result += BASE58_ALPHABET[digits[i]];
1399
+ }
1400
+ return result;
1401
+ }
1402
+ function base64urlEncode(data) {
1403
+ const bytes = typeof data === "string" ? new TextEncoder().encode(data) : data;
1404
+ let binary = "";
1405
+ for (let i = 0; i < bytes.length; i++) {
1406
+ binary += String.fromCharCode(bytes[i]);
1407
+ }
1408
+ return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
1409
+ }
1410
+ async function buildAndEncode(params) {
1411
+ const {
1412
+ issuer,
1413
+ audience,
1414
+ capabilities = [],
1415
+ lifetimeInSeconds = 30,
1416
+ expiration,
1417
+ notBefore,
1418
+ facts,
1419
+ proofs = []
1420
+ } = params;
1421
+ const currentTime = Math.floor(Date.now() / 1e3);
1422
+ const exp = expiration ?? currentTime + lifetimeInSeconds;
1423
+ const header = { alg: issuer.jwtAlg, typ: "JWT", ucv: "0.8.1" };
1424
+ const att = capabilities.map((cap) => ({
1425
+ with: `${cap.with.scheme}:${cap.with.hierPart}`,
1426
+ can: [cap.can.namespace, ...cap.can.segments].join("/")
1427
+ }));
1428
+ const payload = {
1429
+ iss: issuer.did(),
1430
+ aud: audience,
1431
+ exp,
1432
+ att,
1433
+ prf: proofs
1434
+ };
1435
+ if (notBefore !== void 0) payload.nbf = notBefore;
1436
+ if (facts !== void 0) payload.fct = facts;
1437
+ const encodedHeader = base64urlEncode(JSON.stringify(header));
1438
+ const encodedPayload = base64urlEncode(JSON.stringify(payload));
1439
+ const signedData = `${encodedHeader}.${encodedPayload}`;
1440
+ const sig = await issuer.sign(new TextEncoder().encode(signedData));
1441
+ const signature = base64urlEncode(sig);
1442
+ return `${signedData}.${signature}`;
1443
+ }
1444
+ var BASE58_ALPHABET, EDWARDS_DID_PREFIX, EdKeypair;
1445
+ var init_ucan = __esm({
1446
+ "src/sdk/ucan.ts"() {
1447
+ "use strict";
1448
+ init_esm_shims();
1449
+ BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
1450
+ EDWARDS_DID_PREFIX = new Uint8Array([237, 1]);
1451
+ EdKeypair = class _EdKeypair {
1452
+ jwtAlg = "EdDSA";
1453
+ secretKey;
1454
+ publicKey;
1455
+ constructor(secretKey, publicKey) {
1456
+ this.secretKey = secretKey;
1457
+ this.publicKey = publicKey;
1458
+ }
1459
+ static fromSecretKey(key) {
1460
+ const secretKey = toUint8Array(key);
1461
+ const publicKey = extractPublicKeyFromSecretKey(secretKey);
1462
+ return new _EdKeypair(secretKey, publicKey);
1463
+ }
1464
+ did() {
1465
+ const bytes = new Uint8Array(EDWARDS_DID_PREFIX.length + this.publicKey.length);
1466
+ bytes.set(EDWARDS_DID_PREFIX);
1467
+ bytes.set(this.publicKey, EDWARDS_DID_PREFIX.length);
1468
+ return "did:key:z" + base58btcEncode(bytes);
1469
+ }
1470
+ async sign(msg) {
1471
+ return sign(this.secretKey, msg);
1472
+ }
1473
+ };
1474
+ }
1475
+ });
1476
+
1376
1477
  // src/sdk/auth-token-provider.ts
1377
- import * as ucans from "@ucans/ucans";
1378
1478
  var AuthTokenProvider;
1379
1479
  var init_auth_token_provider = __esm({
1380
1480
  "src/sdk/auth-token-provider.ts"() {
1381
1481
  "use strict";
1382
1482
  init_esm_shims();
1483
+ init_ucan();
1383
1484
  AuthTokenProvider = class {
1384
1485
  DEFAULT_OPTIONS = {
1385
1486
  namespace: "file",
@@ -1393,7 +1494,7 @@ var init_auth_token_provider = __esm({
1393
1494
  this.portalAddress = portalAddress;
1394
1495
  }
1395
1496
  async getAuthToken(audienceDid, options = this.DEFAULT_OPTIONS) {
1396
- const ucan = await ucans.build({
1497
+ return buildAndEncode({
1397
1498
  audience: audienceDid,
1398
1499
  issuer: this.keyPair,
1399
1500
  lifetimeInSeconds: 7 * 86400,
@@ -1407,7 +1508,6 @@ var init_auth_token_provider = __esm({
1407
1508
  }
1408
1509
  ]
1409
1510
  });
1410
- return ucans.encode(ucan);
1411
1511
  }
1412
1512
  };
1413
1513
  }
@@ -2581,7 +2681,7 @@ import { derivePBKDF2Key, encryptAesCBC } from "@fileverse/crypto/kdf";
2581
2681
  import { secretBoxEncrypt } from "@fileverse/crypto/nacl";
2582
2682
  import hkdf from "futoin-hkdf";
2583
2683
  import tweetnacl from "tweetnacl";
2584
- import { fromUint8Array, toUint8Array } from "js-base64";
2684
+ import { fromUint8Array, toUint8Array as toUint8Array2 } from "js-base64";
2585
2685
  import { toAESKey, aesEncrypt } from "@fileverse/crypto/webcrypto";
2586
2686
  import axios from "axios";
2587
2687
  import { encodeFunctionData, parseEventLogs } from "viem";
@@ -2599,15 +2699,15 @@ var init_file_utils = __esm({
2599
2699
  });
2600
2700
  };
2601
2701
  getExistingEncryptionMaterial = async (existingEncryptedSecretKey, existingNonce, docId) => {
2602
- const derivedKey = await deriveKeyFromAg2Hash(docId, toUint8Array(existingNonce));
2702
+ const derivedKey = await deriveKeyFromAg2Hash(docId, toUint8Array2(existingNonce));
2603
2703
  const secretKey = tweetnacl.secretbox.open(
2604
- toUint8Array(existingEncryptedSecretKey),
2605
- toUint8Array(existingNonce),
2704
+ toUint8Array2(existingEncryptedSecretKey),
2705
+ toUint8Array2(existingNonce),
2606
2706
  derivedKey
2607
2707
  );
2608
2708
  return {
2609
2709
  encryptedSecretKey: existingEncryptedSecretKey,
2610
- nonce: toUint8Array(existingNonce),
2710
+ nonce: toUint8Array2(existingNonce),
2611
2711
  secretKey,
2612
2712
  derivedKey: new Uint8Array(derivedKey)
2613
2713
  };
@@ -2660,8 +2760,8 @@ var init_file_utils = __esm({
2660
2760
  const encryptedBlob = new Blob([ciphertext], { type: file2.type });
2661
2761
  const encryptedBlobWithAuthTagIv = await appendAuthTagIvToBlob(
2662
2762
  encryptedBlob,
2663
- toUint8Array(authTag),
2664
- toUint8Array(iv)
2763
+ toUint8Array2(authTag),
2764
+ toUint8Array2(iv)
2665
2765
  );
2666
2766
  return {
2667
2767
  encryptedFile: new File([encryptedBlobWithAuthTagIv], file2.name),
@@ -2707,7 +2807,7 @@ var init_file_utils = __esm({
2707
2807
  };
2708
2808
  };
2709
2809
  encryptTitleWithFileKey = async (args) => {
2710
- const key = await toAESKey(toUint8Array(args.key));
2810
+ const key = await toAESKey(toUint8Array2(args.key));
2711
2811
  if (!key) throw new Error("Key is undefined");
2712
2812
  const titleBytes = new TextEncoder().encode(args.title);
2713
2813
  const encryptedTitle = await aesEncrypt(key, titleBytes, "base64");
@@ -2825,7 +2925,7 @@ var init_file_utils = __esm({
2825
2925
  });
2826
2926
 
2827
2927
  // src/sdk/file-manager.ts
2828
- import { fromUint8Array as fromUint8Array2, toUint8Array as toUint8Array2 } from "js-base64";
2928
+ import { fromUint8Array as fromUint8Array2, toUint8Array as toUint8Array3 } from "js-base64";
2829
2929
  import { generateAESKey, exportAESKey } from "@fileverse/crypto/webcrypto";
2830
2930
  import { markdownToYjs } from "@fileverse/content-processor";
2831
2931
  var FileManager;
@@ -2846,8 +2946,8 @@ var init_file_manager = __esm({
2846
2946
  }
2847
2947
  createLocks(key, encryptedSecretKey, commentKey) {
2848
2948
  const appLock = {
2849
- lockedFileKey: this.keyStore.encryptData(toUint8Array2(key)),
2850
- lockedLinkKey: this.keyStore.encryptData(toUint8Array2(encryptedSecretKey)),
2949
+ lockedFileKey: this.keyStore.encryptData(toUint8Array3(key)),
2950
+ lockedLinkKey: this.keyStore.encryptData(toUint8Array3(encryptedSecretKey)),
2851
2951
  lockedChatKey: this.keyStore.encryptData(commentKey)
2852
2952
  };
2853
2953
  return { appLock, ownerLock: { ...appLock } };
@@ -2884,8 +2984,8 @@ var init_file_manager = __esm({
2884
2984
  console.log("Submitting add file trx");
2885
2985
  logger.debug(`Preparing to add file ${file2.ddocId}`);
2886
2986
  const encryptedSecretKey = file2.linkKey;
2887
- const nonce = toUint8Array2(file2.linkKeyNonce);
2888
- const secretKey = toUint8Array2(file2.secretKey);
2987
+ const nonce = toUint8Array3(file2.linkKeyNonce);
2988
+ const secretKey = toUint8Array3(file2.secretKey);
2889
2989
  console.log("Got encrypted secret key, nonce, and secret key");
2890
2990
  const yJSContent = markdownToYjs(file2.content);
2891
2991
  console.log("Generated yjs content");
@@ -2896,7 +2996,7 @@ var init_file_manager = __esm({
2896
2996
  console.log("Generated comment key");
2897
2997
  const { appLock, ownerLock } = this.createLocks(key, encryptedSecretKey, commentKey);
2898
2998
  console.log("Built app lock and owner lock");
2899
- const linkLock = buildLinklock(secretKey, toUint8Array2(key), commentKey);
2999
+ const linkLock = buildLinklock(secretKey, toUint8Array3(key), commentKey);
2900
3000
  console.log("Built link lock");
2901
3001
  const encryptedTitle = await encryptTitleWithFileKey({
2902
3002
  title: file2.title || "Untitled",
@@ -2945,13 +3045,13 @@ var init_file_manager = __esm({
2945
3045
  async submitUpdateFile(file2) {
2946
3046
  logger.debug(`Submitting update for file ${file2.ddocId} with onChainFileId ${file2.onChainFileId}`);
2947
3047
  const encryptedSecretKey = file2.linkKey;
2948
- const nonce = toUint8Array2(file2.linkKeyNonce);
2949
- const secretKey = toUint8Array2(file2.secretKey);
3048
+ const nonce = toUint8Array3(file2.linkKeyNonce);
3049
+ const secretKey = toUint8Array3(file2.secretKey);
2950
3050
  const yjsContent = markdownToYjs(file2.content);
2951
3051
  const { encryptedFile, key } = await createEncryptedContentFile(yjsContent);
2952
- const commentKey = toUint8Array2(file2.commentKey);
3052
+ const commentKey = toUint8Array3(file2.commentKey);
2953
3053
  const { appLock, ownerLock } = this.createLocks(key, encryptedSecretKey, commentKey);
2954
- const linkLock = buildLinklock(secretKey, toUint8Array2(key), commentKey);
3054
+ const linkLock = buildLinklock(secretKey, toUint8Array3(key), commentKey);
2955
3055
  const encryptedTitle = await encryptTitleWithFileKey({
2956
3056
  title: file2.title || "Untitled",
2957
3057
  key
@@ -2993,14 +3093,14 @@ var init_file_manager = __esm({
2993
3093
  async updateFile(file2) {
2994
3094
  logger.debug(`Updating file ${file2.ddocId} with onChainFileId ${file2.onChainFileId}`);
2995
3095
  const encryptedSecretKey = file2.linkKey;
2996
- const nonce = toUint8Array2(file2.linkKeyNonce);
2997
- const secretKey = toUint8Array2(file2.secretKey);
3096
+ const nonce = toUint8Array3(file2.linkKeyNonce);
3097
+ const secretKey = toUint8Array3(file2.secretKey);
2998
3098
  logger.debug(`Generating encrypted content file for file ${file2.ddocId} with onChainFileId ${file2.onChainFileId}`);
2999
3099
  const yjsContent = markdownToYjs(file2.content);
3000
3100
  const { encryptedFile, key } = await createEncryptedContentFile(yjsContent);
3001
- const commentKey = toUint8Array2(file2.commentKey);
3101
+ const commentKey = toUint8Array3(file2.commentKey);
3002
3102
  const { appLock, ownerLock } = this.createLocks(key, encryptedSecretKey, commentKey);
3003
- const linkLock = buildLinklock(secretKey, toUint8Array2(key), commentKey);
3103
+ const linkLock = buildLinklock(secretKey, toUint8Array3(key), commentKey);
3004
3104
  const encryptedTitle = await encryptTitleWithFileKey({
3005
3105
  title: file2.title || "Untitled",
3006
3106
  key
@@ -3052,11 +3152,10 @@ var init_file_manager = __esm({
3052
3152
  });
3053
3153
 
3054
3154
  // src/domain/portal/publish.ts
3055
- import { fromUint8Array as fromUint8Array3, toUint8Array as toUint8Array3 } from "js-base64";
3155
+ import { fromUint8Array as fromUint8Array3, toUint8Array as toUint8Array4 } from "js-base64";
3056
3156
  import { stringToBytes } from "viem";
3057
3157
  import { deriveHKDFKey } from "@fileverse/crypto/kdf";
3058
3158
  import { generateKeyPairFromSeed } from "@stablelib/ed25519";
3059
- import * as ucans2 from "@ucans/ucans";
3060
3159
  async function getPortalData(fileId) {
3061
3160
  const file2 = await FilesModel.findByIdIncludingDeleted(fileId);
3062
3161
  if (!file2) {
@@ -3088,16 +3187,15 @@ var init_publish = __esm({
3088
3187
  init_infra();
3089
3188
  init_key_store();
3090
3189
  init_auth_token_provider();
3190
+ init_ucan();
3091
3191
  init_smart_agent();
3092
3192
  init_file_manager();
3093
3193
  init_config();
3094
3194
  init_pimlico_utils();
3095
3195
  createFileManager = async (portalSeed, portalAddress, ucanSecret, privateAccountKey) => {
3096
- const keyPair = ucans2.EdKeypair.fromSecretKey(fromUint8Array3(ucanSecret), {
3097
- exportable: true
3098
- });
3196
+ const keyPair = EdKeypair.fromSecretKey(fromUint8Array3(ucanSecret));
3099
3197
  const authTokenProvider = new AuthTokenProvider(keyPair, portalAddress);
3100
- const keyStore = new KeyStore(toUint8Array3(portalSeed), portalAddress, authTokenProvider);
3198
+ const keyStore = new KeyStore(toUint8Array4(portalSeed), portalAddress, authTokenProvider);
3101
3199
  const agentClient = new AgentClient(authTokenProvider);
3102
3200
  await agentClient.initializeAgentClient(privateAccountKey);
3103
3201
  return new FileManager(keyStore, agentClient);
@@ -3116,7 +3214,7 @@ var init_publish = __esm({
3116
3214
  handleExistingFileOp = async (fileId, operation) => {
3117
3215
  try {
3118
3216
  const { file: file2, portalDetails, apiKey } = await getPortalData(fileId);
3119
- const apiKeySeed = toUint8Array3(apiKey);
3217
+ const apiKeySeed = toUint8Array4(apiKey);
3120
3218
  const { privateAccountKey, ucanSecret } = deriveCollaboratorKeys(apiKeySeed);
3121
3219
  const fileManager = await createFileManager(
3122
3220
  portalDetails.portalSeed,
@@ -3133,7 +3231,7 @@ var init_publish = __esm({
3133
3231
  handleNewFileOp = async (fileId) => {
3134
3232
  const { file: file2, portalDetails, apiKey } = await getPortalData(fileId);
3135
3233
  console.log("Got portal data");
3136
- const apiKeySeed = toUint8Array3(apiKey);
3234
+ const apiKeySeed = toUint8Array4(apiKey);
3137
3235
  const { privateAccountKey, ucanSecret } = deriveCollaboratorKeys(apiKeySeed);
3138
3236
  console.log("Derived collaborator keys");
3139
3237
  const fileManager = await createFileManager(
@@ -3147,7 +3245,7 @@ var init_publish = __esm({
3147
3245
  };
3148
3246
  getProxyAuthParams = async (fileId) => {
3149
3247
  const { portalDetails, apiKey } = await getPortalData(fileId);
3150
- const apiKeySeed = toUint8Array3(apiKey);
3248
+ const apiKeySeed = toUint8Array4(apiKey);
3151
3249
  const { privateAccountKey, ucanSecret } = deriveCollaboratorKeys(apiKeySeed);
3152
3250
  const fileManager = await createFileManager(
3153
3251
  portalDetails.portalSeed,
@@ -3753,11 +3851,11 @@ init_esm_shims();
3753
3851
  init_esm_shims();
3754
3852
  init_constants();
3755
3853
  import axios2 from "axios";
3756
- import { toUint8Array as toUint8Array4 } from "js-base64";
3854
+ import { toUint8Array as toUint8Array5 } from "js-base64";
3757
3855
  import { sha256 } from "viem";
3758
3856
  var fetchApiKeyData = async (apiKey) => {
3759
3857
  try {
3760
- const keyHash = sha256(toUint8Array4(apiKey));
3858
+ const keyHash = sha256(toUint8Array5(apiKey));
3761
3859
  const fullUrl = BASE_CONFIG.API_URL + `api-access/${keyHash}`;
3762
3860
  const response = await axios2.get(fullUrl);
3763
3861
  const { encryptedKeyMaterial, encryptedAppMaterial, id } = response.data;
@@ -3785,7 +3883,7 @@ init_saveApiKey();
3785
3883
  init_apikeys_model();
3786
3884
  init_infra();
3787
3885
  import { deriveHKDFKey as deriveHKDFKey2 } from "@fileverse/crypto/hkdf";
3788
- import { toUint8Array as toUint8Array5 } from "js-base64";
3886
+ import { toUint8Array as toUint8Array6 } from "js-base64";
3789
3887
  import { stringToBytes as stringToBytes2 } from "viem";
3790
3888
  import { toAESKey as toAESKey2, aesDecrypt } from "@fileverse/crypto/webcrypto";
3791
3889
  var SAVED_DATA_ENCRYPTION_KEY_INFO = "SAVED_DATA_ENCRYPTION_KEY";
@@ -3810,7 +3908,7 @@ async function initializeWithData(data) {
3810
3908
  }
3811
3909
  var getAesKeyFromApiKey = async (apiKey) => {
3812
3910
  const rawSecret = deriveHKDFKey2(
3813
- toUint8Array5(apiKey),
3911
+ toUint8Array6(apiKey),
3814
3912
  new Uint8Array([0]),
3815
3913
  stringToBytes2(SAVED_DATA_ENCRYPTION_KEY_INFO)
3816
3914
  );
@@ -3821,7 +3919,7 @@ var bytestToJSON = (bytes) => {
3821
3919
  };
3822
3920
  var decryptSavedData = async (apiKey, encryptedData) => {
3823
3921
  const aesKey = await getAesKeyFromApiKey(apiKey);
3824
- const decryptedBytes = await aesDecrypt(aesKey, toUint8Array5(encryptedData));
3922
+ const decryptedBytes = await aesDecrypt(aesKey, toUint8Array6(encryptedData));
3825
3923
  const data = bytestToJSON(decryptedBytes);
3826
3924
  return data;
3827
3925
  };
@@ -4745,7 +4843,7 @@ __export(external_exports, {
4745
4843
  e164: () => e1642,
4746
4844
  email: () => email2,
4747
4845
  emoji: () => emoji2,
4748
- encode: () => encode3,
4846
+ encode: () => encode2,
4749
4847
  encodeAsync: () => encodeAsync2,
4750
4848
  endsWith: () => _endsWith,
4751
4849
  enum: () => _enum2,
@@ -5119,7 +5217,7 @@ __export(core_exports2, {
5119
5217
  decode: () => decode,
5120
5218
  decodeAsync: () => decodeAsync,
5121
5219
  describe: () => describe,
5122
- encode: () => encode2,
5220
+ encode: () => encode,
5123
5221
  encodeAsync: () => encodeAsync,
5124
5222
  extractDefs: () => extractDefs,
5125
5223
  finalize: () => finalize,
@@ -6106,7 +6204,7 @@ var _encode = (_Err) => (schema, value, _ctx) => {
6106
6204
  const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
6107
6205
  return _parse(_Err)(schema, value, ctx);
6108
6206
  };
6109
- var encode2 = /* @__PURE__ */ _encode($ZodRealError);
6207
+ var encode = /* @__PURE__ */ _encode($ZodRealError);
6110
6208
  var _decode = (_Err) => (schema, value, _ctx) => {
6111
6209
  return _parse(_Err)(schema, value, _ctx);
6112
6210
  };
@@ -16870,7 +16968,7 @@ var parse2 = /* @__PURE__ */ _parse(ZodRealError);
16870
16968
  var parseAsync2 = /* @__PURE__ */ _parseAsync(ZodRealError);
16871
16969
  var safeParse2 = /* @__PURE__ */ _safeParse(ZodRealError);
16872
16970
  var safeParseAsync2 = /* @__PURE__ */ _safeParseAsync(ZodRealError);
16873
- var encode3 = /* @__PURE__ */ _encode(ZodRealError);
16971
+ var encode2 = /* @__PURE__ */ _encode(ZodRealError);
16874
16972
  var decode2 = /* @__PURE__ */ _decode(ZodRealError);
16875
16973
  var encodeAsync2 = /* @__PURE__ */ _encodeAsync(ZodRealError);
16876
16974
  var decodeAsync2 = /* @__PURE__ */ _decodeAsync(ZodRealError);
@@ -16914,7 +17012,7 @@ var ZodType = /* @__PURE__ */ $constructor("ZodType", (inst, def) => {
16914
17012
  inst.parseAsync = async (data, params) => parseAsync2(inst, data, params, { callee: inst.parseAsync });
16915
17013
  inst.safeParseAsync = async (data, params) => safeParseAsync2(inst, data, params);
16916
17014
  inst.spa = inst.safeParseAsync;
16917
- inst.encode = (data, params) => encode3(inst, data, params);
17015
+ inst.encode = (data, params) => encode2(inst, data, params);
16918
17016
  inst.decode = (data, params) => decode2(inst, data, params);
16919
17017
  inst.encodeAsync = async (data, params) => encodeAsync2(inst, data, params);
16920
17018
  inst.decodeAsync = async (data, params) => decodeAsync2(inst, data, params);