@fileverse/api 0.0.10 → 0.0.12

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
@@ -669,7 +669,9 @@ var init_files_model = __esm({
669
669
  linkKey: fileRaw.linkKey,
670
670
  linkKeyNonce: fileRaw.linkKeyNonce,
671
671
  commentKey: fileRaw.commentKey,
672
- link: fileRaw.link
672
+ link: fileRaw.link,
673
+ derivedKey: fileRaw.derivedKey,
674
+ secretKey: fileRaw.secretKey
673
675
  };
674
676
  }
675
677
  static async findAll(portalAddress, limit, skip) {
@@ -753,10 +755,20 @@ var init_files_model = __esm({
753
755
  const _id = uuidv7();
754
756
  const sql = `
755
757
  INSERT INTO ${this.TABLE}
756
- (_id, title, content, ddocId, portalAddress)
757
- VALUES (?, ?, ?, ?, ?)
758
+ (_id, title, content, ddocId, portalAddress, linkKey, linkKeyNonce, derivedKey, secretKey)
759
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
758
760
  `;
759
- await QueryBuilder.execute(sql, [_id, input.title, input.content, input.ddocId, input.portalAddress]);
761
+ await QueryBuilder.execute(sql, [
762
+ _id,
763
+ input.title,
764
+ input.content,
765
+ input.ddocId,
766
+ input.portalAddress,
767
+ input.linkKey ?? null,
768
+ input.linkKeyNonce ?? null,
769
+ input.derivedKey ?? null,
770
+ input.secretKey ?? null
771
+ ]);
760
772
  const created = await this.findById(_id, input.portalAddress);
761
773
  if (!created) {
762
774
  throw new Error("Failed to create file");
@@ -2573,7 +2585,7 @@ import { fromUint8Array, toUint8Array } from "js-base64";
2573
2585
  import { toAESKey, aesEncrypt } from "@fileverse/crypto/webcrypto";
2574
2586
  import axios from "axios";
2575
2587
  import { encodeFunctionData, parseEventLogs } from "viem";
2576
- var deriveKeyFromAg2Hash, decryptSecretKey, getExistingEncryptionMaterial, getNaclSecretKey, generateLinkKeyMaterial, jsonToFile, appendAuthTagIvToBlob, encryptFile, getNonceAppendedCipherText, jsonToBytes, buildLinklock, encryptTitleWithFileKey, uploadFileToIPFS, getEditFileTrxCalldata, getAddFileTrxCalldata, prepareCallData, prepareDeleteFileCallData, createEncryptedContentFile, buildFileMetadata, parseFileEventLog, uploadAllFilesToIPFS;
2588
+ var deriveKeyFromAg2Hash, getExistingEncryptionMaterial, getNaclSecretKey, generateLinkKeyMaterial, jsonToFile, appendAuthTagIvToBlob, encryptFile, getNonceAppendedCipherText, jsonToBytes, buildLinklock, encryptTitleWithFileKey, uploadFileToIPFS, getEditFileTrxCalldata, getAddFileTrxCalldata, prepareCallData, prepareDeleteFileCallData, createEncryptedContentFile, buildFileMetadata, parseFileEventLog, uploadAllFilesToIPFS;
2577
2589
  var init_file_utils = __esm({
2578
2590
  "src/sdk/file-utils.ts"() {
2579
2591
  "use strict";
@@ -2586,16 +2598,18 @@ var init_file_utils = __esm({
2586
2598
  info: Buffer.from("encryptionKey")
2587
2599
  });
2588
2600
  };
2589
- decryptSecretKey = async (docId, nonce, encryptedSecretKey) => {
2590
- const derivedKey = await deriveKeyFromAg2Hash(docId, toUint8Array(nonce));
2591
- return tweetnacl.secretbox.open(toUint8Array(encryptedSecretKey), toUint8Array(nonce), derivedKey);
2592
- };
2593
2601
  getExistingEncryptionMaterial = async (existingEncryptedSecretKey, existingNonce, docId) => {
2594
- const secretKey = await decryptSecretKey(docId, existingNonce, existingEncryptedSecretKey);
2602
+ const derivedKey = await deriveKeyFromAg2Hash(docId, toUint8Array(existingNonce));
2603
+ const secretKey = tweetnacl.secretbox.open(
2604
+ toUint8Array(existingEncryptedSecretKey),
2605
+ toUint8Array(existingNonce),
2606
+ derivedKey
2607
+ );
2595
2608
  return {
2596
2609
  encryptedSecretKey: existingEncryptedSecretKey,
2597
2610
  nonce: toUint8Array(existingNonce),
2598
- secretKey
2611
+ secretKey,
2612
+ derivedKey: new Uint8Array(derivedKey)
2599
2613
  };
2600
2614
  };
2601
2615
  getNaclSecretKey = async (ddocId) => {
@@ -2603,19 +2617,19 @@ var init_file_utils = __esm({
2603
2617
  const nonce = tweetnacl.randomBytes(tweetnacl.secretbox.nonceLength);
2604
2618
  const derivedKey = await deriveKeyFromAg2Hash(ddocId, nonce);
2605
2619
  const encryptedSecretKey = fromUint8Array(tweetnacl.secretbox(secretKey, nonce, derivedKey), true);
2606
- return { nonce, encryptedSecretKey, secretKey };
2620
+ return { nonce, encryptedSecretKey, secretKey, derivedKey: new Uint8Array(derivedKey) };
2607
2621
  };
2608
2622
  generateLinkKeyMaterial = async (params) => {
2609
2623
  if (params.linkKeyNonce && params.linkKey) {
2610
- const { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2 } = await getExistingEncryptionMaterial(
2624
+ const { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2, derivedKey: derivedKey2 } = await getExistingEncryptionMaterial(
2611
2625
  params.linkKey,
2612
2626
  params.linkKeyNonce,
2613
2627
  params.ddocId
2614
2628
  );
2615
- if (secretKey2) return { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2 };
2629
+ if (secretKey2) return { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2, derivedKey: derivedKey2 };
2616
2630
  }
2617
- const { secretKey, nonce, encryptedSecretKey } = await getNaclSecretKey(params.ddocId);
2618
- return { secretKey, nonce, encryptedSecretKey };
2631
+ const { secretKey, nonce, encryptedSecretKey, derivedKey } = await getNaclSecretKey(params.ddocId);
2632
+ return { secretKey, nonce, encryptedSecretKey, derivedKey };
2619
2633
  };
2620
2634
  jsonToFile = (json2, fileName) => {
2621
2635
  const blob = new Blob([JSON.stringify(json2)], {
@@ -2868,11 +2882,9 @@ var init_file_manager = __esm({
2868
2882
  }
2869
2883
  async submitAddFileTrx(file2) {
2870
2884
  logger.debug(`Preparing to add file ${file2.ddocId}`);
2871
- const { encryptedSecretKey, nonce, secretKey } = await generateLinkKeyMaterial({
2872
- ddocId: file2.ddocId,
2873
- linkKey: file2.linkKey,
2874
- linkKeyNonce: file2.linkKeyNonce
2875
- });
2885
+ const encryptedSecretKey = file2.linkKey;
2886
+ const nonce = toUint8Array2(file2.linkKeyNonce);
2887
+ const secretKey = toUint8Array2(file2.secretKey);
2876
2888
  const yJSContent = markdownToYjs(file2.content);
2877
2889
  const { encryptedFile, key } = await createEncryptedContentFile(yJSContent);
2878
2890
  logger.debug(`Generated encrypted content file for file ${file2.ddocId}`);
@@ -2918,11 +2930,9 @@ var init_file_manager = __esm({
2918
2930
  }
2919
2931
  async submitUpdateFile(file2) {
2920
2932
  logger.debug(`Submitting update for file ${file2.ddocId} with onChainFileId ${file2.onChainFileId}`);
2921
- const { encryptedSecretKey, nonce, secretKey } = await generateLinkKeyMaterial({
2922
- ddocId: file2.ddocId,
2923
- linkKey: file2.linkKey,
2924
- linkKeyNonce: file2.linkKeyNonce
2925
- });
2933
+ const encryptedSecretKey = file2.linkKey;
2934
+ const nonce = toUint8Array2(file2.linkKeyNonce);
2935
+ const secretKey = toUint8Array2(file2.secretKey);
2926
2936
  const yjsContent = markdownToYjs(file2.content);
2927
2937
  const { encryptedFile, key } = await createEncryptedContentFile(yjsContent);
2928
2938
  const commentKey = toUint8Array2(file2.commentKey);
@@ -2968,11 +2978,9 @@ var init_file_manager = __esm({
2968
2978
  }
2969
2979
  async updateFile(file2) {
2970
2980
  logger.debug(`Updating file ${file2.ddocId} with onChainFileId ${file2.onChainFileId}`);
2971
- const { encryptedSecretKey, nonce, secretKey } = await generateLinkKeyMaterial({
2972
- ddocId: file2.ddocId,
2973
- linkKey: file2.linkKey,
2974
- linkKeyNonce: file2.linkKeyNonce
2975
- });
2981
+ const encryptedSecretKey = file2.linkKey;
2982
+ const nonce = toUint8Array2(file2.linkKeyNonce);
2983
+ const secretKey = toUint8Array2(file2.secretKey);
2976
2984
  logger.debug(`Generating encrypted content file for file ${file2.ddocId} with onChainFileId ${file2.onChainFileId}`);
2977
2985
  const yjsContent = markdownToYjs(file2.content);
2978
2986
  const { encryptedFile, key } = await createEncryptedContentFile(yjsContent);
@@ -3644,7 +3652,9 @@ CREATE TABLE IF NOT EXISTS files (
3644
3652
  commentKey TEXT,
3645
3653
  linkKey TEXT,
3646
3654
  linkKeyNonce TEXT,
3647
- link TEXT
3655
+ link TEXT,
3656
+ derivedKey TEXT,
3657
+ secretKey TEXT
3648
3658
  );
3649
3659
  CREATE INDEX IF NOT EXISTS idx_files_createdAt ON files(createdAt);
3650
3660
  CREATE INDEX IF NOT EXISTS idx_files_syncStatus ON files(syncStatus);
@@ -3838,7 +3848,9 @@ init_esm_shims();
3838
3848
  init_esm_shims();
3839
3849
  init_models();
3840
3850
  init_constants2();
3851
+ init_file_utils();
3841
3852
  import { generate } from "short-uuid";
3853
+ import { fromUint8Array as fromUint8Array4 } from "js-base64";
3842
3854
  async function listFiles(params) {
3843
3855
  const { limit, skip, portalAddress } = params;
3844
3856
  const effectiveLimit = limit || DEFAULT_LIST_LIMIT;
@@ -3891,11 +3903,20 @@ var createFile = async (input) => {
3891
3903
  throw new Error("title, content, and portalAddress are required");
3892
3904
  }
3893
3905
  const ddocId = generate();
3906
+ const { encryptedSecretKey, nonce, secretKey, derivedKey } = await generateLinkKeyMaterial({
3907
+ ddocId,
3908
+ linkKey: void 0,
3909
+ linkKeyNonce: void 0
3910
+ });
3894
3911
  const file2 = await FilesModel.create({
3895
3912
  title: input.title,
3896
3913
  content: input.content,
3897
3914
  ddocId,
3898
- portalAddress: input.portalAddress
3915
+ portalAddress: input.portalAddress,
3916
+ linkKey: encryptedSecretKey,
3917
+ linkKeyNonce: fromUint8Array4(nonce),
3918
+ derivedKey: fromUint8Array4(derivedKey),
3919
+ secretKey: fromUint8Array4(secretKey)
3899
3920
  });
3900
3921
  await EventsModel.create({ type: "create", fileId: file2._id, portalAddress: file2.portalAddress });
3901
3922
  return file2;