@fileverse/api 0.0.10 → 0.0.11

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.
@@ -340,7 +340,9 @@ var init_files_model = __esm({
340
340
  linkKey: fileRaw.linkKey,
341
341
  linkKeyNonce: fileRaw.linkKeyNonce,
342
342
  commentKey: fileRaw.commentKey,
343
- link: fileRaw.link
343
+ link: fileRaw.link,
344
+ derivedKey: fileRaw.derivedKey,
345
+ secretKey: fileRaw.secretKey
344
346
  };
345
347
  }
346
348
  static async findAll(portalAddress, limit, skip) {
@@ -424,10 +426,20 @@ var init_files_model = __esm({
424
426
  const _id = uuidv7();
425
427
  const sql = `
426
428
  INSERT INTO ${this.TABLE}
427
- (_id, title, content, ddocId, portalAddress)
428
- VALUES (?, ?, ?, ?, ?)
429
+ (_id, title, content, ddocId, portalAddress, linkKey, linkKeyNonce, derivedKey, secretKey)
430
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
429
431
  `;
430
- await QueryBuilder.execute(sql, [_id, input.title, input.content, input.ddocId, input.portalAddress]);
432
+ await QueryBuilder.execute(sql, [
433
+ _id,
434
+ input.title,
435
+ input.content,
436
+ input.ddocId,
437
+ input.portalAddress,
438
+ input.linkKey ?? null,
439
+ input.linkKeyNonce ?? null,
440
+ input.derivedKey ?? null,
441
+ input.secretKey ?? null
442
+ ]);
431
443
  const created = await this.findById(_id, input.portalAddress);
432
444
  if (!created) {
433
445
  throw new Error("Failed to create file");
@@ -938,12 +950,52 @@ import { fromUint8Array, toUint8Array } from "js-base64";
938
950
  import { toAESKey, aesEncrypt } from "@fileverse/crypto/webcrypto";
939
951
  import axios from "axios";
940
952
  import { encodeFunctionData, parseEventLogs } from "viem";
953
+ var deriveKeyFromAg2Hash, getExistingEncryptionMaterial, getNaclSecretKey, generateLinkKeyMaterial;
941
954
  var init_file_utils = __esm({
942
955
  "src/sdk/file-utils.ts"() {
943
956
  "use strict";
944
957
  init_esm_shims();
945
958
  init_file_encryption();
946
959
  init_constants3();
960
+ deriveKeyFromAg2Hash = async (pass, salt) => {
961
+ const key = await getArgon2idHash(pass, salt);
962
+ return hkdf(Buffer.from(key), tweetnacl.secretbox.keyLength, {
963
+ info: Buffer.from("encryptionKey")
964
+ });
965
+ };
966
+ getExistingEncryptionMaterial = async (existingEncryptedSecretKey, existingNonce, docId) => {
967
+ const derivedKey = await deriveKeyFromAg2Hash(docId, toUint8Array(existingNonce));
968
+ const secretKey = tweetnacl.secretbox.open(
969
+ toUint8Array(existingEncryptedSecretKey),
970
+ toUint8Array(existingNonce),
971
+ derivedKey
972
+ );
973
+ return {
974
+ encryptedSecretKey: existingEncryptedSecretKey,
975
+ nonce: toUint8Array(existingNonce),
976
+ secretKey,
977
+ derivedKey: new Uint8Array(derivedKey)
978
+ };
979
+ };
980
+ getNaclSecretKey = async (ddocId) => {
981
+ const { secretKey } = tweetnacl.box.keyPair();
982
+ const nonce = tweetnacl.randomBytes(tweetnacl.secretbox.nonceLength);
983
+ const derivedKey = await deriveKeyFromAg2Hash(ddocId, nonce);
984
+ const encryptedSecretKey = fromUint8Array(tweetnacl.secretbox(secretKey, nonce, derivedKey), true);
985
+ return { nonce, encryptedSecretKey, secretKey, derivedKey: new Uint8Array(derivedKey) };
986
+ };
987
+ generateLinkKeyMaterial = async (params) => {
988
+ if (params.linkKeyNonce && params.linkKey) {
989
+ const { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2, derivedKey: derivedKey2 } = await getExistingEncryptionMaterial(
990
+ params.linkKey,
991
+ params.linkKeyNonce,
992
+ params.ddocId
993
+ );
994
+ if (secretKey2) return { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2, derivedKey: derivedKey2 };
995
+ }
996
+ const { secretKey, nonce, encryptedSecretKey, derivedKey } = await getNaclSecretKey(params.ddocId);
997
+ return { secretKey, nonce, encryptedSecretKey, derivedKey };
998
+ };
947
999
  }
948
1000
  });
949
1001
 
@@ -1449,7 +1501,9 @@ CREATE TABLE IF NOT EXISTS files (
1449
1501
  commentKey TEXT,
1450
1502
  linkKey TEXT,
1451
1503
  linkKeyNonce TEXT,
1452
- link TEXT
1504
+ link TEXT,
1505
+ derivedKey TEXT,
1506
+ secretKey TEXT
1453
1507
  );
1454
1508
  CREATE INDEX IF NOT EXISTS idx_files_createdAt ON files(createdAt);
1455
1509
  CREATE INDEX IF NOT EXISTS idx_files_syncStatus ON files(syncStatus);
@@ -1529,7 +1583,9 @@ import Table from "cli-table3";
1529
1583
  init_esm_shims();
1530
1584
  init_models();
1531
1585
  init_constants2();
1586
+ init_file_utils();
1532
1587
  import { generate } from "short-uuid";
1588
+ import { fromUint8Array as fromUint8Array4 } from "js-base64";
1533
1589
  async function listFiles(params) {
1534
1590
  const { limit, skip, portalAddress } = params;
1535
1591
  const effectiveLimit = limit || DEFAULT_LIST_LIMIT;
@@ -1582,11 +1638,20 @@ var createFile = async (input) => {
1582
1638
  throw new Error("title, content, and portalAddress are required");
1583
1639
  }
1584
1640
  const ddocId = generate();
1641
+ const { encryptedSecretKey, nonce, secretKey, derivedKey } = await generateLinkKeyMaterial({
1642
+ ddocId,
1643
+ linkKey: void 0,
1644
+ linkKeyNonce: void 0
1645
+ });
1585
1646
  const file = await FilesModel.create({
1586
1647
  title: input.title,
1587
1648
  content: input.content,
1588
1649
  ddocId,
1589
- portalAddress: input.portalAddress
1650
+ portalAddress: input.portalAddress,
1651
+ linkKey: encryptedSecretKey,
1652
+ linkKeyNonce: fromUint8Array4(nonce),
1653
+ derivedKey: fromUint8Array4(derivedKey),
1654
+ secretKey: fromUint8Array4(secretKey)
1590
1655
  });
1591
1656
  await EventsModel.create({ type: "create", fileId: file._id, portalAddress: file.portalAddress });
1592
1657
  return file;