@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.
@@ -272,7 +272,9 @@ var init_files_model = __esm({
272
272
  linkKey: fileRaw.linkKey,
273
273
  linkKeyNonce: fileRaw.linkKeyNonce,
274
274
  commentKey: fileRaw.commentKey,
275
- link: fileRaw.link
275
+ link: fileRaw.link,
276
+ derivedKey: fileRaw.derivedKey,
277
+ secretKey: fileRaw.secretKey
276
278
  };
277
279
  }
278
280
  static async findAll(portalAddress, limit, skip) {
@@ -356,10 +358,20 @@ var init_files_model = __esm({
356
358
  const _id = uuidv7();
357
359
  const sql = `
358
360
  INSERT INTO ${this.TABLE}
359
- (_id, title, content, ddocId, portalAddress)
360
- VALUES (?, ?, ?, ?, ?)
361
+ (_id, title, content, ddocId, portalAddress, linkKey, linkKeyNonce, derivedKey, secretKey)
362
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
361
363
  `;
362
- await QueryBuilder.execute(sql, [_id, input.title, input.content, input.ddocId, input.portalAddress]);
364
+ await QueryBuilder.execute(sql, [
365
+ _id,
366
+ input.title,
367
+ input.content,
368
+ input.ddocId,
369
+ input.portalAddress,
370
+ input.linkKey ?? null,
371
+ input.linkKeyNonce ?? null,
372
+ input.derivedKey ?? null,
373
+ input.secretKey ?? null
374
+ ]);
363
375
  const created = await this.findById(_id, input.portalAddress);
364
376
  if (!created) {
365
377
  throw new Error("Failed to create file");
@@ -2180,7 +2192,7 @@ import { fromUint8Array, toUint8Array } from "js-base64";
2180
2192
  import { toAESKey, aesEncrypt } from "@fileverse/crypto/webcrypto";
2181
2193
  import axios from "axios";
2182
2194
  import { encodeFunctionData, parseEventLogs } from "viem";
2183
- var deriveKeyFromAg2Hash, decryptSecretKey, getExistingEncryptionMaterial, getNaclSecretKey, generateLinkKeyMaterial, jsonToFile, appendAuthTagIvToBlob, encryptFile, getNonceAppendedCipherText, jsonToBytes, buildLinklock, encryptTitleWithFileKey, uploadFileToIPFS, getEditFileTrxCalldata, getAddFileTrxCalldata, prepareCallData, prepareDeleteFileCallData, createEncryptedContentFile, buildFileMetadata, parseFileEventLog, uploadAllFilesToIPFS;
2195
+ var deriveKeyFromAg2Hash, getExistingEncryptionMaterial, getNaclSecretKey, generateLinkKeyMaterial, jsonToFile, appendAuthTagIvToBlob, encryptFile, getNonceAppendedCipherText, jsonToBytes, buildLinklock, encryptTitleWithFileKey, uploadFileToIPFS, getEditFileTrxCalldata, getAddFileTrxCalldata, prepareCallData, prepareDeleteFileCallData, createEncryptedContentFile, buildFileMetadata, parseFileEventLog, uploadAllFilesToIPFS;
2184
2196
  var init_file_utils = __esm({
2185
2197
  "src/sdk/file-utils.ts"() {
2186
2198
  "use strict";
@@ -2193,16 +2205,18 @@ var init_file_utils = __esm({
2193
2205
  info: Buffer.from("encryptionKey")
2194
2206
  });
2195
2207
  };
2196
- decryptSecretKey = async (docId, nonce, encryptedSecretKey) => {
2197
- const derivedKey = await deriveKeyFromAg2Hash(docId, toUint8Array(nonce));
2198
- return tweetnacl.secretbox.open(toUint8Array(encryptedSecretKey), toUint8Array(nonce), derivedKey);
2199
- };
2200
2208
  getExistingEncryptionMaterial = async (existingEncryptedSecretKey, existingNonce, docId) => {
2201
- const secretKey = await decryptSecretKey(docId, existingNonce, existingEncryptedSecretKey);
2209
+ const derivedKey = await deriveKeyFromAg2Hash(docId, toUint8Array(existingNonce));
2210
+ const secretKey = tweetnacl.secretbox.open(
2211
+ toUint8Array(existingEncryptedSecretKey),
2212
+ toUint8Array(existingNonce),
2213
+ derivedKey
2214
+ );
2202
2215
  return {
2203
2216
  encryptedSecretKey: existingEncryptedSecretKey,
2204
2217
  nonce: toUint8Array(existingNonce),
2205
- secretKey
2218
+ secretKey,
2219
+ derivedKey: new Uint8Array(derivedKey)
2206
2220
  };
2207
2221
  };
2208
2222
  getNaclSecretKey = async (ddocId) => {
@@ -2210,19 +2224,19 @@ var init_file_utils = __esm({
2210
2224
  const nonce = tweetnacl.randomBytes(tweetnacl.secretbox.nonceLength);
2211
2225
  const derivedKey = await deriveKeyFromAg2Hash(ddocId, nonce);
2212
2226
  const encryptedSecretKey = fromUint8Array(tweetnacl.secretbox(secretKey, nonce, derivedKey), true);
2213
- return { nonce, encryptedSecretKey, secretKey };
2227
+ return { nonce, encryptedSecretKey, secretKey, derivedKey: new Uint8Array(derivedKey) };
2214
2228
  };
2215
2229
  generateLinkKeyMaterial = async (params) => {
2216
2230
  if (params.linkKeyNonce && params.linkKey) {
2217
- const { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2 } = await getExistingEncryptionMaterial(
2231
+ const { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2, derivedKey: derivedKey2 } = await getExistingEncryptionMaterial(
2218
2232
  params.linkKey,
2219
2233
  params.linkKeyNonce,
2220
2234
  params.ddocId
2221
2235
  );
2222
- if (secretKey2) return { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2 };
2236
+ if (secretKey2) return { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2, derivedKey: derivedKey2 };
2223
2237
  }
2224
- const { secretKey, nonce, encryptedSecretKey } = await getNaclSecretKey(params.ddocId);
2225
- return { secretKey, nonce, encryptedSecretKey };
2238
+ const { secretKey, nonce, encryptedSecretKey, derivedKey } = await getNaclSecretKey(params.ddocId);
2239
+ return { secretKey, nonce, encryptedSecretKey, derivedKey };
2226
2240
  };
2227
2241
  jsonToFile = (json2, fileName) => {
2228
2242
  const blob = new Blob([JSON.stringify(json2)], {
@@ -2475,11 +2489,9 @@ var init_file_manager = __esm({
2475
2489
  }
2476
2490
  async submitAddFileTrx(file2) {
2477
2491
  logger.debug(`Preparing to add file ${file2.ddocId}`);
2478
- const { encryptedSecretKey, nonce, secretKey } = await generateLinkKeyMaterial({
2479
- ddocId: file2.ddocId,
2480
- linkKey: file2.linkKey,
2481
- linkKeyNonce: file2.linkKeyNonce
2482
- });
2492
+ const encryptedSecretKey = file2.linkKey;
2493
+ const nonce = toUint8Array2(file2.linkKeyNonce);
2494
+ const secretKey = toUint8Array2(file2.secretKey);
2483
2495
  const yJSContent = markdownToYjs(file2.content);
2484
2496
  const { encryptedFile, key } = await createEncryptedContentFile(yJSContent);
2485
2497
  logger.debug(`Generated encrypted content file for file ${file2.ddocId}`);
@@ -2525,11 +2537,9 @@ var init_file_manager = __esm({
2525
2537
  }
2526
2538
  async submitUpdateFile(file2) {
2527
2539
  logger.debug(`Submitting update for file ${file2.ddocId} with onChainFileId ${file2.onChainFileId}`);
2528
- const { encryptedSecretKey, nonce, secretKey } = await generateLinkKeyMaterial({
2529
- ddocId: file2.ddocId,
2530
- linkKey: file2.linkKey,
2531
- linkKeyNonce: file2.linkKeyNonce
2532
- });
2540
+ const encryptedSecretKey = file2.linkKey;
2541
+ const nonce = toUint8Array2(file2.linkKeyNonce);
2542
+ const secretKey = toUint8Array2(file2.secretKey);
2533
2543
  const yjsContent = markdownToYjs(file2.content);
2534
2544
  const { encryptedFile, key } = await createEncryptedContentFile(yjsContent);
2535
2545
  const commentKey = toUint8Array2(file2.commentKey);
@@ -2575,11 +2585,9 @@ var init_file_manager = __esm({
2575
2585
  }
2576
2586
  async updateFile(file2) {
2577
2587
  logger.debug(`Updating file ${file2.ddocId} with onChainFileId ${file2.onChainFileId}`);
2578
- const { encryptedSecretKey, nonce, secretKey } = await generateLinkKeyMaterial({
2579
- ddocId: file2.ddocId,
2580
- linkKey: file2.linkKey,
2581
- linkKeyNonce: file2.linkKeyNonce
2582
- });
2588
+ const encryptedSecretKey = file2.linkKey;
2589
+ const nonce = toUint8Array2(file2.linkKeyNonce);
2590
+ const secretKey = toUint8Array2(file2.secretKey);
2583
2591
  logger.debug(`Generating encrypted content file for file ${file2.ddocId} with onChainFileId ${file2.onChainFileId}`);
2584
2592
  const yjsContent = markdownToYjs(file2.content);
2585
2593
  const { encryptedFile, key } = await createEncryptedContentFile(yjsContent);
@@ -3630,7 +3638,9 @@ CREATE TABLE IF NOT EXISTS files (
3630
3638
  commentKey TEXT,
3631
3639
  linkKey TEXT,
3632
3640
  linkKeyNonce TEXT,
3633
- link TEXT
3641
+ link TEXT,
3642
+ derivedKey TEXT,
3643
+ secretKey TEXT
3634
3644
  );
3635
3645
  CREATE INDEX IF NOT EXISTS idx_files_createdAt ON files(createdAt);
3636
3646
  CREATE INDEX IF NOT EXISTS idx_files_syncStatus ON files(syncStatus);
@@ -3708,7 +3718,9 @@ init_models();
3708
3718
  init_esm_shims();
3709
3719
  init_models();
3710
3720
  init_constants3();
3721
+ init_file_utils();
3711
3722
  import { generate } from "short-uuid";
3723
+ import { fromUint8Array as fromUint8Array4 } from "js-base64";
3712
3724
  async function listFiles(params) {
3713
3725
  const { limit, skip, portalAddress } = params;
3714
3726
  const effectiveLimit = limit || DEFAULT_LIST_LIMIT;
@@ -3761,11 +3773,20 @@ var createFile = async (input) => {
3761
3773
  throw new Error("title, content, and portalAddress are required");
3762
3774
  }
3763
3775
  const ddocId = generate();
3776
+ const { encryptedSecretKey, nonce, secretKey, derivedKey } = await generateLinkKeyMaterial({
3777
+ ddocId,
3778
+ linkKey: void 0,
3779
+ linkKeyNonce: void 0
3780
+ });
3764
3781
  const file2 = await FilesModel.create({
3765
3782
  title: input.title,
3766
3783
  content: input.content,
3767
3784
  ddocId,
3768
- portalAddress: input.portalAddress
3785
+ portalAddress: input.portalAddress,
3786
+ linkKey: encryptedSecretKey,
3787
+ linkKeyNonce: fromUint8Array4(nonce),
3788
+ derivedKey: fromUint8Array4(derivedKey),
3789
+ secretKey: fromUint8Array4(secretKey)
3769
3790
  });
3770
3791
  await EventsModel.create({ type: "create", fileId: file2._id, portalAddress: file2.portalAddress });
3771
3792
  return file2;