@fileverse/api 0.0.14 → 0.0.16

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.
@@ -2282,62 +2282,20 @@ var init_file_encryption = __esm({
2282
2282
  });
2283
2283
 
2284
2284
  // src/sdk/file-utils.ts
2285
- import { getArgon2idHash } from "@fileverse/crypto/argon";
2286
2285
  import { bytesToBase64, generateRandomBytes as generateRandomBytes2 } from "@fileverse/crypto/utils";
2287
2286
  import { derivePBKDF2Key, encryptAesCBC } from "@fileverse/crypto/kdf";
2288
2287
  import { secretBoxEncrypt } from "@fileverse/crypto/nacl";
2289
- import hkdf from "futoin-hkdf";
2290
2288
  import tweetnacl from "tweetnacl";
2291
2289
  import { fromUint8Array, toUint8Array as toUint8Array2 } from "js-base64";
2292
2290
  import { toAESKey, aesEncrypt } from "@fileverse/crypto/webcrypto";
2293
- import axios from "axios";
2294
2291
  import { encodeFunctionData, parseEventLogs } from "viem";
2295
- var deriveKeyFromAg2Hash, getExistingEncryptionMaterial, getNaclSecretKey, generateLinkKeyMaterial, jsonToFile, appendAuthTagIvToBlob, encryptFile, getNonceAppendedCipherText, jsonToBytes, buildLinklock, encryptTitleWithFileKey, uploadFileToIPFS, getEditFileTrxCalldata, getAddFileTrxCalldata, prepareCallData, prepareDeleteFileCallData, createEncryptedContentFile, buildFileMetadata, parseFileEventLog, uploadAllFilesToIPFS;
2292
+ var jsonToFile, appendAuthTagIvToBlob, encryptFile, getNonceAppendedCipherText, jsonToBytes, buildLinklock, encryptTitleWithFileKey, uploadFileToIPFS, getEditFileTrxCalldata, getAddFileTrxCalldata, prepareCallData, prepareDeleteFileCallData, createEncryptedContentFile, buildFileMetadata, parseFileEventLog, uploadAllFilesToIPFS;
2296
2293
  var init_file_utils = __esm({
2297
2294
  "src/sdk/file-utils.ts"() {
2298
2295
  "use strict";
2299
2296
  init_esm_shims();
2300
2297
  init_file_encryption();
2301
2298
  init_constants2();
2302
- deriveKeyFromAg2Hash = async (pass, salt) => {
2303
- const key = await getArgon2idHash(pass, salt);
2304
- return hkdf(Buffer.from(key), tweetnacl.secretbox.keyLength, {
2305
- info: Buffer.from("encryptionKey")
2306
- });
2307
- };
2308
- getExistingEncryptionMaterial = async (existingEncryptedSecretKey, existingNonce, docId) => {
2309
- const derivedKey = await deriveKeyFromAg2Hash(docId, toUint8Array2(existingNonce));
2310
- const secretKey = tweetnacl.secretbox.open(
2311
- toUint8Array2(existingEncryptedSecretKey),
2312
- toUint8Array2(existingNonce),
2313
- derivedKey
2314
- );
2315
- return {
2316
- encryptedSecretKey: existingEncryptedSecretKey,
2317
- nonce: toUint8Array2(existingNonce),
2318
- secretKey,
2319
- derivedKey: new Uint8Array(derivedKey)
2320
- };
2321
- };
2322
- getNaclSecretKey = async (ddocId) => {
2323
- const { secretKey } = tweetnacl.box.keyPair();
2324
- const nonce = tweetnacl.randomBytes(tweetnacl.secretbox.nonceLength);
2325
- const derivedKey = await deriveKeyFromAg2Hash(ddocId, nonce);
2326
- const encryptedSecretKey = fromUint8Array(tweetnacl.secretbox(secretKey, nonce, derivedKey), true);
2327
- return { nonce, encryptedSecretKey, secretKey, derivedKey: new Uint8Array(derivedKey) };
2328
- };
2329
- generateLinkKeyMaterial = async (params) => {
2330
- if (params.linkKeyNonce && params.linkKey) {
2331
- const { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2, derivedKey: derivedKey2 } = await getExistingEncryptionMaterial(
2332
- params.linkKey,
2333
- params.linkKeyNonce,
2334
- params.ddocId
2335
- );
2336
- if (secretKey2) return { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2, derivedKey: derivedKey2 };
2337
- }
2338
- const { secretKey, nonce, encryptedSecretKey, derivedKey } = await getNaclSecretKey(params.ddocId);
2339
- return { secretKey, nonce, encryptedSecretKey, derivedKey };
2340
- };
2341
2299
  jsonToFile = (json2, fileName) => {
2342
2300
  const blob = new Blob([JSON.stringify(json2)], {
2343
2301
  type: "application/json"
@@ -2428,16 +2386,21 @@ var init_file_utils = __esm({
2428
2386
  body.append("ipfsType", ipfsType);
2429
2387
  body.append("appFileId", appFileId);
2430
2388
  body.append("sourceApp", "ddoc");
2431
- const uploadEndpoint = UPLOAD_SERVER_URL + "upload";
2432
- const response = await axios.post(uploadEndpoint, body, {
2389
+ const response = await fetch(UPLOAD_SERVER_URL + "upload", {
2390
+ method: "POST",
2433
2391
  headers: {
2434
2392
  Authorization: `Bearer ${token}`,
2435
2393
  contract: contractAddress,
2436
2394
  invoker,
2437
- chain: process.env.chainId
2438
- }
2395
+ chain: NETWORK_NAME
2396
+ },
2397
+ body
2439
2398
  });
2440
- return response.data.ipfsHash;
2399
+ if (!response.ok) {
2400
+ throw new Error(`Upload failed: ${response.status} ${response.statusText}`);
2401
+ }
2402
+ const data = await response.json();
2403
+ return data.ipfsHash;
2441
2404
  };
2442
2405
  getEditFileTrxCalldata = (args) => {
2443
2406
  return encodeFunctionData({
@@ -2534,7 +2497,6 @@ var init_file_utils = __esm({
2534
2497
  // src/sdk/file-manager.ts
2535
2498
  import { fromUint8Array as fromUint8Array2, toUint8Array as toUint8Array3 } from "js-base64";
2536
2499
  import { generateAESKey, exportAESKey } from "@fileverse/crypto/webcrypto";
2537
- import { markdownToYjs } from "@fileverse/content-processor";
2538
2500
  var FileManager;
2539
2501
  var init_file_manager = __esm({
2540
2502
  "src/sdk/file-manager.ts"() {
@@ -2551,6 +2513,32 @@ var init_file_manager = __esm({
2551
2513
  this.keyStore = keyStore;
2552
2514
  this.agentClient = agentClient;
2553
2515
  }
2516
+ async convertMarkdown(content) {
2517
+ const g = globalThis;
2518
+ const saved = {
2519
+ window: g.window,
2520
+ navigator: g.navigator,
2521
+ document: g.document,
2522
+ Node: g.Node,
2523
+ HTMLElement: g.HTMLElement,
2524
+ Text: g.Text,
2525
+ DOMParser: g.DOMParser,
2526
+ Element: g.Element,
2527
+ DocumentFragment: g.DocumentFragment,
2528
+ getComputedStyle: g.getComputedStyle,
2529
+ MutationObserver: g.MutationObserver,
2530
+ Range: g.Range
2531
+ };
2532
+ try {
2533
+ const { markdownToYjs } = await import("@fileverse/content-processor");
2534
+ return markdownToYjs(content);
2535
+ } finally {
2536
+ for (const [key, value] of Object.entries(saved)) {
2537
+ if (value === void 0) delete g[key];
2538
+ else g[key] = value;
2539
+ }
2540
+ }
2541
+ }
2554
2542
  createLocks(key, encryptedSecretKey, commentKey) {
2555
2543
  const appLock = {
2556
2544
  lockedFileKey: this.keyStore.encryptData(toUint8Array3(key)),
@@ -2594,7 +2582,7 @@ var init_file_manager = __esm({
2594
2582
  const nonce = toUint8Array3(file2.linkKeyNonce);
2595
2583
  const secretKey = toUint8Array3(file2.secretKey);
2596
2584
  console.log("Got encrypted secret key, nonce, and secret key");
2597
- const yJSContent = markdownToYjs(file2.content);
2585
+ const yJSContent = await this.convertMarkdown(file2.content);
2598
2586
  console.log("Generated yjs content");
2599
2587
  const { encryptedFile, key } = await createEncryptedContentFile(yJSContent);
2600
2588
  console.log("Generated encrypted content file");
@@ -2654,7 +2642,7 @@ var init_file_manager = __esm({
2654
2642
  const encryptedSecretKey = file2.linkKey;
2655
2643
  const nonce = toUint8Array3(file2.linkKeyNonce);
2656
2644
  const secretKey = toUint8Array3(file2.secretKey);
2657
- const yjsContent = markdownToYjs(file2.content);
2645
+ const yjsContent = await this.convertMarkdown(file2.content);
2658
2646
  const { encryptedFile, key } = await createEncryptedContentFile(yjsContent);
2659
2647
  const commentKey = toUint8Array3(file2.commentKey);
2660
2648
  const { appLock, ownerLock } = this.createLocks(key, encryptedSecretKey, commentKey);
@@ -2703,7 +2691,7 @@ var init_file_manager = __esm({
2703
2691
  const nonce = toUint8Array3(file2.linkKeyNonce);
2704
2692
  const secretKey = toUint8Array3(file2.secretKey);
2705
2693
  logger.debug(`Generating encrypted content file for file ${file2.ddocId} with onChainFileId ${file2.onChainFileId}`);
2706
- const yjsContent = markdownToYjs(file2.content);
2694
+ const yjsContent = await this.convertMarkdown(file2.content);
2707
2695
  const { encryptedFile, key } = await createEncryptedContentFile(yjsContent);
2708
2696
  const commentKey = toUint8Array3(file2.commentKey);
2709
2697
  const { appLock, ownerLock } = this.createLocks(key, encryptedSecretKey, commentKey);
@@ -2800,11 +2788,17 @@ var init_publish = __esm({
2800
2788
  init_config();
2801
2789
  init_pimlico_utils();
2802
2790
  createFileManager = async (portalSeed, portalAddress, ucanSecret, privateAccountKey) => {
2791
+ console.log("Creating file manager");
2803
2792
  const keyPair = EdKeypair.fromSecretKey(fromUint8Array3(ucanSecret));
2793
+ console.log("Created key pair");
2804
2794
  const authTokenProvider = new AuthTokenProvider(keyPair, portalAddress);
2795
+ console.log("Created auth token provider");
2805
2796
  const keyStore = new KeyStore(toUint8Array4(portalSeed), portalAddress, authTokenProvider);
2797
+ console.log("Created key store");
2806
2798
  const agentClient = new AgentClient(authTokenProvider);
2799
+ console.log("Created agent client");
2807
2800
  await agentClient.initializeAgentClient(privateAccountKey);
2801
+ console.log("Initialized agent client");
2808
2802
  return new FileManager(keyStore, agentClient);
2809
2803
  };
2810
2804
  executeOperation = async (fileManager, file2, operation) => {
@@ -3057,9 +3051,6 @@ var init_eventProcessor = __esm({
3057
3051
  const result = await handleNewFileOp(fileId);
3058
3052
  console.log("New file op submitted");
3059
3053
  await EventsModel.setEventPendingOp(event._id, result.userOpHash, {
3060
- linkKey: result.linkKey,
3061
- linkKeyNonce: result.linkKeyNonce,
3062
- commentKey: result.commentKey,
3063
3054
  metadata: result.metadata
3064
3055
  });
3065
3056
  logger.info(`File ${file2.ddocId} create op submitted (hash: ${result.userOpHash})`);
@@ -3837,9 +3828,56 @@ init_models();
3837
3828
  init_esm_shims();
3838
3829
  init_models();
3839
3830
  init_constants3();
3840
- init_file_utils();
3841
3831
  import { generate } from "short-uuid";
3842
- import { fromUint8Array as fromUint8Array4 } from "js-base64";
3832
+ import { fromUint8Array as fromUint8Array5 } from "js-base64";
3833
+
3834
+ // src/sdk/link-key-utils.ts
3835
+ init_esm_shims();
3836
+ import { getArgon2idHash } from "@fileverse/crypto/argon";
3837
+ import hkdf from "futoin-hkdf";
3838
+ import tweetnacl2 from "tweetnacl";
3839
+ import { fromUint8Array as fromUint8Array4, toUint8Array as toUint8Array5 } from "js-base64";
3840
+ var deriveKeyFromAg2Hash = async (pass, salt) => {
3841
+ const key = await getArgon2idHash(pass, salt);
3842
+ return hkdf(Buffer.from(key), tweetnacl2.secretbox.keyLength, {
3843
+ info: Buffer.from("encryptionKey")
3844
+ });
3845
+ };
3846
+ var getExistingEncryptionMaterial = async (existingEncryptedSecretKey, existingNonce, docId) => {
3847
+ const derivedKey = await deriveKeyFromAg2Hash(docId, toUint8Array5(existingNonce));
3848
+ const secretKey = tweetnacl2.secretbox.open(
3849
+ toUint8Array5(existingEncryptedSecretKey),
3850
+ toUint8Array5(existingNonce),
3851
+ derivedKey
3852
+ );
3853
+ return {
3854
+ encryptedSecretKey: existingEncryptedSecretKey,
3855
+ nonce: toUint8Array5(existingNonce),
3856
+ secretKey,
3857
+ derivedKey: new Uint8Array(derivedKey)
3858
+ };
3859
+ };
3860
+ var getNaclSecretKey = async (ddocId) => {
3861
+ const { secretKey } = tweetnacl2.box.keyPair();
3862
+ const nonce = tweetnacl2.randomBytes(tweetnacl2.secretbox.nonceLength);
3863
+ const derivedKey = await deriveKeyFromAg2Hash(ddocId, nonce);
3864
+ const encryptedSecretKey = fromUint8Array4(tweetnacl2.secretbox(secretKey, nonce, derivedKey), true);
3865
+ return { nonce, encryptedSecretKey, secretKey, derivedKey: new Uint8Array(derivedKey) };
3866
+ };
3867
+ var generateLinkKeyMaterial = async (params) => {
3868
+ if (params.linkKeyNonce && params.linkKey) {
3869
+ const { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2, derivedKey: derivedKey2 } = await getExistingEncryptionMaterial(
3870
+ params.linkKey,
3871
+ params.linkKeyNonce,
3872
+ params.ddocId
3873
+ );
3874
+ if (secretKey2) return { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2, derivedKey: derivedKey2 };
3875
+ }
3876
+ const { secretKey, nonce, encryptedSecretKey, derivedKey } = await getNaclSecretKey(params.ddocId);
3877
+ return { secretKey, nonce, encryptedSecretKey, derivedKey };
3878
+ };
3879
+
3880
+ // src/domain/file/index.ts
3843
3881
  async function listFiles(params) {
3844
3882
  const { limit, skip, portalAddress } = params;
3845
3883
  const effectiveLimit = limit || DEFAULT_LIST_LIMIT;
@@ -3903,9 +3941,9 @@ var createFile = async (input) => {
3903
3941
  ddocId,
3904
3942
  portalAddress: input.portalAddress,
3905
3943
  linkKey: encryptedSecretKey,
3906
- linkKeyNonce: fromUint8Array4(nonce),
3907
- derivedKey: fromUint8Array4(derivedKey),
3908
- secretKey: fromUint8Array4(secretKey)
3944
+ linkKeyNonce: fromUint8Array5(nonce),
3945
+ derivedKey: fromUint8Array5(derivedKey),
3946
+ secretKey: fromUint8Array5(secretKey)
3909
3947
  });
3910
3948
  await EventsModel.create({ type: "create", fileId: file2._id, portalAddress: file2.portalAddress });
3911
3949
  return file2;
@@ -4047,18 +4085,18 @@ init_esm_shims();
4047
4085
  // src/cli/fetch-api-key.ts
4048
4086
  init_esm_shims();
4049
4087
  init_constants();
4050
- import axios2 from "axios";
4051
- import { toUint8Array as toUint8Array5 } from "js-base64";
4088
+ import axios from "axios";
4089
+ import { toUint8Array as toUint8Array6 } from "js-base64";
4052
4090
  import { sha256 } from "viem";
4053
4091
  var fetchApiKeyData = async (apiKey) => {
4054
4092
  try {
4055
- const keyHash = sha256(toUint8Array5(apiKey));
4093
+ const keyHash = sha256(toUint8Array6(apiKey));
4056
4094
  const fullUrl = BASE_CONFIG.API_URL + `api-access/${keyHash}`;
4057
- const response = await axios2.get(fullUrl);
4095
+ const response = await axios.get(fullUrl);
4058
4096
  const { encryptedKeyMaterial, encryptedAppMaterial, id } = response.data;
4059
4097
  return { encryptedKeyMaterial, encryptedAppMaterial, id };
4060
4098
  } catch (error48) {
4061
- if (axios2.isAxiosError(error48)) {
4099
+ if (axios.isAxiosError(error48)) {
4062
4100
  if (error48.response?.status === 401) {
4063
4101
  throw new Error("Invalid API key");
4064
4102
  }
@@ -4080,7 +4118,7 @@ init_saveApiKey();
4080
4118
  init_apikeys_model();
4081
4119
  init_infra();
4082
4120
  import { deriveHKDFKey as deriveHKDFKey2 } from "@fileverse/crypto/hkdf";
4083
- import { toUint8Array as toUint8Array6 } from "js-base64";
4121
+ import { toUint8Array as toUint8Array7 } from "js-base64";
4084
4122
  import { stringToBytes as stringToBytes2 } from "viem";
4085
4123
  import { toAESKey as toAESKey2, aesDecrypt } from "@fileverse/crypto/webcrypto";
4086
4124
  var SAVED_DATA_ENCRYPTION_KEY_INFO = "SAVED_DATA_ENCRYPTION_KEY";
@@ -4105,7 +4143,7 @@ async function initializeWithData(data) {
4105
4143
  }
4106
4144
  var getAesKeyFromApiKey = async (apiKey) => {
4107
4145
  const rawSecret = deriveHKDFKey2(
4108
- toUint8Array6(apiKey),
4146
+ toUint8Array7(apiKey),
4109
4147
  new Uint8Array([0]),
4110
4148
  stringToBytes2(SAVED_DATA_ENCRYPTION_KEY_INFO)
4111
4149
  );
@@ -4116,7 +4154,7 @@ var bytestToJSON = (bytes) => {
4116
4154
  };
4117
4155
  var decryptSavedData = async (apiKey, encryptedData) => {
4118
4156
  const aesKey = await getAesKeyFromApiKey(apiKey);
4119
- const decryptedBytes = await aesDecrypt(aesKey, toUint8Array6(encryptedData));
4157
+ const decryptedBytes = await aesDecrypt(aesKey, toUint8Array7(encryptedData));
4120
4158
  const data = bytestToJSON(decryptedBytes);
4121
4159
  return data;
4122
4160
  };