@fileverse/api 0.0.15 → 0.0.17

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);
@@ -3840,9 +3828,61 @@ init_models();
3840
3828
  init_esm_shims();
3841
3829
  init_models();
3842
3830
  init_constants3();
3843
- init_file_utils();
3844
3831
  import { generate } from "short-uuid";
3845
- 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, void 0, {
3842
+ t: 2,
3843
+ m: 4096,
3844
+ p: 8,
3845
+ dkLen: 32
3846
+ });
3847
+ return hkdf(Buffer.from(key), tweetnacl2.secretbox.keyLength, {
3848
+ info: Buffer.from("encryptionKey")
3849
+ });
3850
+ };
3851
+ var getExistingEncryptionMaterial = async (existingEncryptedSecretKey, existingNonce, docId) => {
3852
+ const derivedKey = await deriveKeyFromAg2Hash(docId, toUint8Array5(existingNonce));
3853
+ const secretKey = tweetnacl2.secretbox.open(
3854
+ toUint8Array5(existingEncryptedSecretKey),
3855
+ toUint8Array5(existingNonce),
3856
+ derivedKey
3857
+ );
3858
+ return {
3859
+ encryptedSecretKey: existingEncryptedSecretKey,
3860
+ nonce: toUint8Array5(existingNonce),
3861
+ secretKey,
3862
+ derivedKey: new Uint8Array(derivedKey)
3863
+ };
3864
+ };
3865
+ var getNaclSecretKey = async (ddocId) => {
3866
+ const { secretKey } = tweetnacl2.box.keyPair();
3867
+ const nonce = tweetnacl2.randomBytes(tweetnacl2.secretbox.nonceLength);
3868
+ const derivedKey = await deriveKeyFromAg2Hash(ddocId, nonce);
3869
+ const encryptedSecretKey = fromUint8Array4(tweetnacl2.secretbox(secretKey, nonce, derivedKey), true);
3870
+ return { nonce, encryptedSecretKey, secretKey, derivedKey: new Uint8Array(derivedKey) };
3871
+ };
3872
+ var generateLinkKeyMaterial = async (params) => {
3873
+ if (params.linkKeyNonce && params.linkKey) {
3874
+ const { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2, derivedKey: derivedKey2 } = await getExistingEncryptionMaterial(
3875
+ params.linkKey,
3876
+ params.linkKeyNonce,
3877
+ params.ddocId
3878
+ );
3879
+ if (secretKey2) return { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2, derivedKey: derivedKey2 };
3880
+ }
3881
+ const { secretKey, nonce, encryptedSecretKey, derivedKey } = await getNaclSecretKey(params.ddocId);
3882
+ return { secretKey, nonce, encryptedSecretKey, derivedKey };
3883
+ };
3884
+
3885
+ // src/domain/file/index.ts
3846
3886
  async function listFiles(params) {
3847
3887
  const { limit, skip, portalAddress } = params;
3848
3888
  const effectiveLimit = limit || DEFAULT_LIST_LIMIT;
@@ -3906,9 +3946,9 @@ var createFile = async (input) => {
3906
3946
  ddocId,
3907
3947
  portalAddress: input.portalAddress,
3908
3948
  linkKey: encryptedSecretKey,
3909
- linkKeyNonce: fromUint8Array4(nonce),
3910
- derivedKey: fromUint8Array4(derivedKey),
3911
- secretKey: fromUint8Array4(secretKey)
3949
+ linkKeyNonce: fromUint8Array5(nonce),
3950
+ derivedKey: fromUint8Array5(derivedKey),
3951
+ secretKey: fromUint8Array5(secretKey)
3912
3952
  });
3913
3953
  await EventsModel.create({ type: "create", fileId: file2._id, portalAddress: file2.portalAddress });
3914
3954
  return file2;
@@ -4050,18 +4090,18 @@ init_esm_shims();
4050
4090
  // src/cli/fetch-api-key.ts
4051
4091
  init_esm_shims();
4052
4092
  init_constants();
4053
- import axios2 from "axios";
4054
- import { toUint8Array as toUint8Array5 } from "js-base64";
4093
+ import axios from "axios";
4094
+ import { toUint8Array as toUint8Array6 } from "js-base64";
4055
4095
  import { sha256 } from "viem";
4056
4096
  var fetchApiKeyData = async (apiKey) => {
4057
4097
  try {
4058
- const keyHash = sha256(toUint8Array5(apiKey));
4098
+ const keyHash = sha256(toUint8Array6(apiKey));
4059
4099
  const fullUrl = BASE_CONFIG.API_URL + `api-access/${keyHash}`;
4060
- const response = await axios2.get(fullUrl);
4100
+ const response = await axios.get(fullUrl);
4061
4101
  const { encryptedKeyMaterial, encryptedAppMaterial, id } = response.data;
4062
4102
  return { encryptedKeyMaterial, encryptedAppMaterial, id };
4063
4103
  } catch (error48) {
4064
- if (axios2.isAxiosError(error48)) {
4104
+ if (axios.isAxiosError(error48)) {
4065
4105
  if (error48.response?.status === 401) {
4066
4106
  throw new Error("Invalid API key");
4067
4107
  }
@@ -4083,7 +4123,7 @@ init_saveApiKey();
4083
4123
  init_apikeys_model();
4084
4124
  init_infra();
4085
4125
  import { deriveHKDFKey as deriveHKDFKey2 } from "@fileverse/crypto/hkdf";
4086
- import { toUint8Array as toUint8Array6 } from "js-base64";
4126
+ import { toUint8Array as toUint8Array7 } from "js-base64";
4087
4127
  import { stringToBytes as stringToBytes2 } from "viem";
4088
4128
  import { toAESKey as toAESKey2, aesDecrypt } from "@fileverse/crypto/webcrypto";
4089
4129
  var SAVED_DATA_ENCRYPTION_KEY_INFO = "SAVED_DATA_ENCRYPTION_KEY";
@@ -4108,7 +4148,7 @@ async function initializeWithData(data) {
4108
4148
  }
4109
4149
  var getAesKeyFromApiKey = async (apiKey) => {
4110
4150
  const rawSecret = deriveHKDFKey2(
4111
- toUint8Array6(apiKey),
4151
+ toUint8Array7(apiKey),
4112
4152
  new Uint8Array([0]),
4113
4153
  stringToBytes2(SAVED_DATA_ENCRYPTION_KEY_INFO)
4114
4154
  );
@@ -4119,7 +4159,7 @@ var bytestToJSON = (bytes) => {
4119
4159
  };
4120
4160
  var decryptSavedData = async (apiKey, encryptedData) => {
4121
4161
  const aesKey = await getAesKeyFromApiKey(apiKey);
4122
- const decryptedBytes = await aesDecrypt(aesKey, toUint8Array6(encryptedData));
4162
+ const decryptedBytes = await aesDecrypt(aesKey, toUint8Array7(encryptedData));
4123
4163
  const data = bytestToJSON(decryptedBytes);
4124
4164
  return data;
4125
4165
  };