@fileverse/api 0.0.15 → 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.
- package/dist/cli/index.js +0 -4
- package/dist/cli/index.js.map +1 -1
- package/dist/cloudflare.js +100 -65
- package/dist/cloudflare.js.map +1 -1
- package/dist/commands/index.js +52 -49
- package/dist/commands/index.js.map +1 -1
- package/dist/index.js +95 -60
- package/dist/index.js.map +1 -1
- package/dist/worker.js +39 -12
- package/dist/worker.js.map +1 -1
- package/package.json +1 -1
package/dist/cloudflare.js
CHANGED
|
@@ -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
|
|
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
|
|
2432
|
-
|
|
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:
|
|
2438
|
-
}
|
|
2395
|
+
chain: NETWORK_NAME
|
|
2396
|
+
},
|
|
2397
|
+
body
|
|
2439
2398
|
});
|
|
2440
|
-
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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,56 @@ 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
|
|
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
|
|
3846
3881
|
async function listFiles(params) {
|
|
3847
3882
|
const { limit, skip, portalAddress } = params;
|
|
3848
3883
|
const effectiveLimit = limit || DEFAULT_LIST_LIMIT;
|
|
@@ -3906,9 +3941,9 @@ var createFile = async (input) => {
|
|
|
3906
3941
|
ddocId,
|
|
3907
3942
|
portalAddress: input.portalAddress,
|
|
3908
3943
|
linkKey: encryptedSecretKey,
|
|
3909
|
-
linkKeyNonce:
|
|
3910
|
-
derivedKey:
|
|
3911
|
-
secretKey:
|
|
3944
|
+
linkKeyNonce: fromUint8Array5(nonce),
|
|
3945
|
+
derivedKey: fromUint8Array5(derivedKey),
|
|
3946
|
+
secretKey: fromUint8Array5(secretKey)
|
|
3912
3947
|
});
|
|
3913
3948
|
await EventsModel.create({ type: "create", fileId: file2._id, portalAddress: file2.portalAddress });
|
|
3914
3949
|
return file2;
|
|
@@ -4050,18 +4085,18 @@ init_esm_shims();
|
|
|
4050
4085
|
// src/cli/fetch-api-key.ts
|
|
4051
4086
|
init_esm_shims();
|
|
4052
4087
|
init_constants();
|
|
4053
|
-
import
|
|
4054
|
-
import { toUint8Array as
|
|
4088
|
+
import axios from "axios";
|
|
4089
|
+
import { toUint8Array as toUint8Array6 } from "js-base64";
|
|
4055
4090
|
import { sha256 } from "viem";
|
|
4056
4091
|
var fetchApiKeyData = async (apiKey) => {
|
|
4057
4092
|
try {
|
|
4058
|
-
const keyHash = sha256(
|
|
4093
|
+
const keyHash = sha256(toUint8Array6(apiKey));
|
|
4059
4094
|
const fullUrl = BASE_CONFIG.API_URL + `api-access/${keyHash}`;
|
|
4060
|
-
const response = await
|
|
4095
|
+
const response = await axios.get(fullUrl);
|
|
4061
4096
|
const { encryptedKeyMaterial, encryptedAppMaterial, id } = response.data;
|
|
4062
4097
|
return { encryptedKeyMaterial, encryptedAppMaterial, id };
|
|
4063
4098
|
} catch (error48) {
|
|
4064
|
-
if (
|
|
4099
|
+
if (axios.isAxiosError(error48)) {
|
|
4065
4100
|
if (error48.response?.status === 401) {
|
|
4066
4101
|
throw new Error("Invalid API key");
|
|
4067
4102
|
}
|
|
@@ -4083,7 +4118,7 @@ init_saveApiKey();
|
|
|
4083
4118
|
init_apikeys_model();
|
|
4084
4119
|
init_infra();
|
|
4085
4120
|
import { deriveHKDFKey as deriveHKDFKey2 } from "@fileverse/crypto/hkdf";
|
|
4086
|
-
import { toUint8Array as
|
|
4121
|
+
import { toUint8Array as toUint8Array7 } from "js-base64";
|
|
4087
4122
|
import { stringToBytes as stringToBytes2 } from "viem";
|
|
4088
4123
|
import { toAESKey as toAESKey2, aesDecrypt } from "@fileverse/crypto/webcrypto";
|
|
4089
4124
|
var SAVED_DATA_ENCRYPTION_KEY_INFO = "SAVED_DATA_ENCRYPTION_KEY";
|
|
@@ -4108,7 +4143,7 @@ async function initializeWithData(data) {
|
|
|
4108
4143
|
}
|
|
4109
4144
|
var getAesKeyFromApiKey = async (apiKey) => {
|
|
4110
4145
|
const rawSecret = deriveHKDFKey2(
|
|
4111
|
-
|
|
4146
|
+
toUint8Array7(apiKey),
|
|
4112
4147
|
new Uint8Array([0]),
|
|
4113
4148
|
stringToBytes2(SAVED_DATA_ENCRYPTION_KEY_INFO)
|
|
4114
4149
|
);
|
|
@@ -4119,7 +4154,7 @@ var bytestToJSON = (bytes) => {
|
|
|
4119
4154
|
};
|
|
4120
4155
|
var decryptSavedData = async (apiKey, encryptedData) => {
|
|
4121
4156
|
const aesKey = await getAesKeyFromApiKey(apiKey);
|
|
4122
|
-
const decryptedBytes = await aesDecrypt(aesKey,
|
|
4157
|
+
const decryptedBytes = await aesDecrypt(aesKey, toUint8Array7(encryptedData));
|
|
4123
4158
|
const data = bytestToJSON(decryptedBytes);
|
|
4124
4159
|
return data;
|
|
4125
4160
|
};
|