@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.
- package/dist/cli/index.js +0 -4
- package/dist/cli/index.js.map +1 -1
- package/dist/cloudflare.js +105 -65
- package/dist/cloudflare.js.map +1 -1
- package/dist/commands/index.js +57 -49
- package/dist/commands/index.js.map +1 -1
- package/dist/index.js +100 -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/index.js
CHANGED
|
@@ -2675,62 +2675,20 @@ var init_file_encryption = __esm({
|
|
|
2675
2675
|
});
|
|
2676
2676
|
|
|
2677
2677
|
// src/sdk/file-utils.ts
|
|
2678
|
-
import { getArgon2idHash } from "@fileverse/crypto/argon";
|
|
2679
2678
|
import { bytesToBase64, generateRandomBytes as generateRandomBytes2 } from "@fileverse/crypto/utils";
|
|
2680
2679
|
import { derivePBKDF2Key, encryptAesCBC } from "@fileverse/crypto/kdf";
|
|
2681
2680
|
import { secretBoxEncrypt } from "@fileverse/crypto/nacl";
|
|
2682
|
-
import hkdf from "futoin-hkdf";
|
|
2683
2681
|
import tweetnacl from "tweetnacl";
|
|
2684
2682
|
import { fromUint8Array, toUint8Array as toUint8Array2 } from "js-base64";
|
|
2685
2683
|
import { toAESKey, aesEncrypt } from "@fileverse/crypto/webcrypto";
|
|
2686
|
-
import axios from "axios";
|
|
2687
2684
|
import { encodeFunctionData, parseEventLogs } from "viem";
|
|
2688
|
-
var
|
|
2685
|
+
var jsonToFile, appendAuthTagIvToBlob, encryptFile, getNonceAppendedCipherText, jsonToBytes, buildLinklock, encryptTitleWithFileKey, uploadFileToIPFS, getEditFileTrxCalldata, getAddFileTrxCalldata, prepareCallData, prepareDeleteFileCallData, createEncryptedContentFile, buildFileMetadata, parseFileEventLog, uploadAllFilesToIPFS;
|
|
2689
2686
|
var init_file_utils = __esm({
|
|
2690
2687
|
"src/sdk/file-utils.ts"() {
|
|
2691
2688
|
"use strict";
|
|
2692
2689
|
init_esm_shims();
|
|
2693
2690
|
init_file_encryption();
|
|
2694
2691
|
init_constants3();
|
|
2695
|
-
deriveKeyFromAg2Hash = async (pass, salt) => {
|
|
2696
|
-
const key = await getArgon2idHash(pass, salt);
|
|
2697
|
-
return hkdf(Buffer.from(key), tweetnacl.secretbox.keyLength, {
|
|
2698
|
-
info: Buffer.from("encryptionKey")
|
|
2699
|
-
});
|
|
2700
|
-
};
|
|
2701
|
-
getExistingEncryptionMaterial = async (existingEncryptedSecretKey, existingNonce, docId) => {
|
|
2702
|
-
const derivedKey = await deriveKeyFromAg2Hash(docId, toUint8Array2(existingNonce));
|
|
2703
|
-
const secretKey = tweetnacl.secretbox.open(
|
|
2704
|
-
toUint8Array2(existingEncryptedSecretKey),
|
|
2705
|
-
toUint8Array2(existingNonce),
|
|
2706
|
-
derivedKey
|
|
2707
|
-
);
|
|
2708
|
-
return {
|
|
2709
|
-
encryptedSecretKey: existingEncryptedSecretKey,
|
|
2710
|
-
nonce: toUint8Array2(existingNonce),
|
|
2711
|
-
secretKey,
|
|
2712
|
-
derivedKey: new Uint8Array(derivedKey)
|
|
2713
|
-
};
|
|
2714
|
-
};
|
|
2715
|
-
getNaclSecretKey = async (ddocId) => {
|
|
2716
|
-
const { secretKey } = tweetnacl.box.keyPair();
|
|
2717
|
-
const nonce = tweetnacl.randomBytes(tweetnacl.secretbox.nonceLength);
|
|
2718
|
-
const derivedKey = await deriveKeyFromAg2Hash(ddocId, nonce);
|
|
2719
|
-
const encryptedSecretKey = fromUint8Array(tweetnacl.secretbox(secretKey, nonce, derivedKey), true);
|
|
2720
|
-
return { nonce, encryptedSecretKey, secretKey, derivedKey: new Uint8Array(derivedKey) };
|
|
2721
|
-
};
|
|
2722
|
-
generateLinkKeyMaterial = async (params) => {
|
|
2723
|
-
if (params.linkKeyNonce && params.linkKey) {
|
|
2724
|
-
const { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2, derivedKey: derivedKey2 } = await getExistingEncryptionMaterial(
|
|
2725
|
-
params.linkKey,
|
|
2726
|
-
params.linkKeyNonce,
|
|
2727
|
-
params.ddocId
|
|
2728
|
-
);
|
|
2729
|
-
if (secretKey2) return { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2, derivedKey: derivedKey2 };
|
|
2730
|
-
}
|
|
2731
|
-
const { secretKey, nonce, encryptedSecretKey, derivedKey } = await getNaclSecretKey(params.ddocId);
|
|
2732
|
-
return { secretKey, nonce, encryptedSecretKey, derivedKey };
|
|
2733
|
-
};
|
|
2734
2692
|
jsonToFile = (json2, fileName) => {
|
|
2735
2693
|
const blob = new Blob([JSON.stringify(json2)], {
|
|
2736
2694
|
type: "application/json"
|
|
@@ -2821,16 +2779,21 @@ var init_file_utils = __esm({
|
|
|
2821
2779
|
body.append("ipfsType", ipfsType);
|
|
2822
2780
|
body.append("appFileId", appFileId);
|
|
2823
2781
|
body.append("sourceApp", "ddoc");
|
|
2824
|
-
const
|
|
2825
|
-
|
|
2782
|
+
const response = await fetch(UPLOAD_SERVER_URL + "upload", {
|
|
2783
|
+
method: "POST",
|
|
2826
2784
|
headers: {
|
|
2827
2785
|
Authorization: `Bearer ${token}`,
|
|
2828
2786
|
contract: contractAddress,
|
|
2829
2787
|
invoker,
|
|
2830
|
-
chain:
|
|
2831
|
-
}
|
|
2788
|
+
chain: NETWORK_NAME
|
|
2789
|
+
},
|
|
2790
|
+
body
|
|
2832
2791
|
});
|
|
2833
|
-
|
|
2792
|
+
if (!response.ok) {
|
|
2793
|
+
throw new Error(`Upload failed: ${response.status} ${response.statusText}`);
|
|
2794
|
+
}
|
|
2795
|
+
const data = await response.json();
|
|
2796
|
+
return data.ipfsHash;
|
|
2834
2797
|
};
|
|
2835
2798
|
getEditFileTrxCalldata = (args) => {
|
|
2836
2799
|
return encodeFunctionData({
|
|
@@ -2927,7 +2890,6 @@ var init_file_utils = __esm({
|
|
|
2927
2890
|
// src/sdk/file-manager.ts
|
|
2928
2891
|
import { fromUint8Array as fromUint8Array2, toUint8Array as toUint8Array3 } from "js-base64";
|
|
2929
2892
|
import { generateAESKey, exportAESKey } from "@fileverse/crypto/webcrypto";
|
|
2930
|
-
import { markdownToYjs } from "@fileverse/content-processor";
|
|
2931
2893
|
var FileManager;
|
|
2932
2894
|
var init_file_manager = __esm({
|
|
2933
2895
|
"src/sdk/file-manager.ts"() {
|
|
@@ -2944,6 +2906,32 @@ var init_file_manager = __esm({
|
|
|
2944
2906
|
this.keyStore = keyStore;
|
|
2945
2907
|
this.agentClient = agentClient;
|
|
2946
2908
|
}
|
|
2909
|
+
async convertMarkdown(content) {
|
|
2910
|
+
const g = globalThis;
|
|
2911
|
+
const saved = {
|
|
2912
|
+
window: g.window,
|
|
2913
|
+
navigator: g.navigator,
|
|
2914
|
+
document: g.document,
|
|
2915
|
+
Node: g.Node,
|
|
2916
|
+
HTMLElement: g.HTMLElement,
|
|
2917
|
+
Text: g.Text,
|
|
2918
|
+
DOMParser: g.DOMParser,
|
|
2919
|
+
Element: g.Element,
|
|
2920
|
+
DocumentFragment: g.DocumentFragment,
|
|
2921
|
+
getComputedStyle: g.getComputedStyle,
|
|
2922
|
+
MutationObserver: g.MutationObserver,
|
|
2923
|
+
Range: g.Range
|
|
2924
|
+
};
|
|
2925
|
+
try {
|
|
2926
|
+
const { markdownToYjs } = await import("@fileverse/content-processor");
|
|
2927
|
+
return markdownToYjs(content);
|
|
2928
|
+
} finally {
|
|
2929
|
+
for (const [key, value] of Object.entries(saved)) {
|
|
2930
|
+
if (value === void 0) delete g[key];
|
|
2931
|
+
else g[key] = value;
|
|
2932
|
+
}
|
|
2933
|
+
}
|
|
2934
|
+
}
|
|
2947
2935
|
createLocks(key, encryptedSecretKey, commentKey) {
|
|
2948
2936
|
const appLock = {
|
|
2949
2937
|
lockedFileKey: this.keyStore.encryptData(toUint8Array3(key)),
|
|
@@ -2987,7 +2975,7 @@ var init_file_manager = __esm({
|
|
|
2987
2975
|
const nonce = toUint8Array3(file2.linkKeyNonce);
|
|
2988
2976
|
const secretKey = toUint8Array3(file2.secretKey);
|
|
2989
2977
|
console.log("Got encrypted secret key, nonce, and secret key");
|
|
2990
|
-
const yJSContent =
|
|
2978
|
+
const yJSContent = await this.convertMarkdown(file2.content);
|
|
2991
2979
|
console.log("Generated yjs content");
|
|
2992
2980
|
const { encryptedFile, key } = await createEncryptedContentFile(yJSContent);
|
|
2993
2981
|
console.log("Generated encrypted content file");
|
|
@@ -3047,7 +3035,7 @@ var init_file_manager = __esm({
|
|
|
3047
3035
|
const encryptedSecretKey = file2.linkKey;
|
|
3048
3036
|
const nonce = toUint8Array3(file2.linkKeyNonce);
|
|
3049
3037
|
const secretKey = toUint8Array3(file2.secretKey);
|
|
3050
|
-
const yjsContent =
|
|
3038
|
+
const yjsContent = await this.convertMarkdown(file2.content);
|
|
3051
3039
|
const { encryptedFile, key } = await createEncryptedContentFile(yjsContent);
|
|
3052
3040
|
const commentKey = toUint8Array3(file2.commentKey);
|
|
3053
3041
|
const { appLock, ownerLock } = this.createLocks(key, encryptedSecretKey, commentKey);
|
|
@@ -3096,7 +3084,7 @@ var init_file_manager = __esm({
|
|
|
3096
3084
|
const nonce = toUint8Array3(file2.linkKeyNonce);
|
|
3097
3085
|
const secretKey = toUint8Array3(file2.secretKey);
|
|
3098
3086
|
logger.debug(`Generating encrypted content file for file ${file2.ddocId} with onChainFileId ${file2.onChainFileId}`);
|
|
3099
|
-
const yjsContent =
|
|
3087
|
+
const yjsContent = await this.convertMarkdown(file2.content);
|
|
3100
3088
|
const { encryptedFile, key } = await createEncryptedContentFile(yjsContent);
|
|
3101
3089
|
const commentKey = toUint8Array3(file2.commentKey);
|
|
3102
3090
|
const { appLock, ownerLock } = this.createLocks(key, encryptedSecretKey, commentKey);
|
|
@@ -3856,18 +3844,18 @@ init_esm_shims();
|
|
|
3856
3844
|
// src/cli/fetch-api-key.ts
|
|
3857
3845
|
init_esm_shims();
|
|
3858
3846
|
init_constants();
|
|
3859
|
-
import
|
|
3847
|
+
import axios from "axios";
|
|
3860
3848
|
import { toUint8Array as toUint8Array5 } from "js-base64";
|
|
3861
3849
|
import { sha256 } from "viem";
|
|
3862
3850
|
var fetchApiKeyData = async (apiKey) => {
|
|
3863
3851
|
try {
|
|
3864
3852
|
const keyHash = sha256(toUint8Array5(apiKey));
|
|
3865
3853
|
const fullUrl = BASE_CONFIG.API_URL + `api-access/${keyHash}`;
|
|
3866
|
-
const response = await
|
|
3854
|
+
const response = await axios.get(fullUrl);
|
|
3867
3855
|
const { encryptedKeyMaterial, encryptedAppMaterial, id } = response.data;
|
|
3868
3856
|
return { encryptedKeyMaterial, encryptedAppMaterial, id };
|
|
3869
3857
|
} catch (error48) {
|
|
3870
|
-
if (
|
|
3858
|
+
if (axios.isAxiosError(error48)) {
|
|
3871
3859
|
if (error48.response?.status === 401) {
|
|
3872
3860
|
throw new Error("Invalid API key");
|
|
3873
3861
|
}
|
|
@@ -3969,9 +3957,61 @@ init_esm_shims();
|
|
|
3969
3957
|
init_esm_shims();
|
|
3970
3958
|
init_models();
|
|
3971
3959
|
init_constants2();
|
|
3972
|
-
init_file_utils();
|
|
3973
3960
|
import { generate } from "short-uuid";
|
|
3974
|
-
import { fromUint8Array as
|
|
3961
|
+
import { fromUint8Array as fromUint8Array5 } from "js-base64";
|
|
3962
|
+
|
|
3963
|
+
// src/sdk/link-key-utils.ts
|
|
3964
|
+
init_esm_shims();
|
|
3965
|
+
import { getArgon2idHash } from "@fileverse/crypto/argon";
|
|
3966
|
+
import hkdf from "futoin-hkdf";
|
|
3967
|
+
import tweetnacl2 from "tweetnacl";
|
|
3968
|
+
import { fromUint8Array as fromUint8Array4, toUint8Array as toUint8Array7 } from "js-base64";
|
|
3969
|
+
var deriveKeyFromAg2Hash = async (pass, salt) => {
|
|
3970
|
+
const key = await getArgon2idHash(pass, salt, void 0, {
|
|
3971
|
+
t: 2,
|
|
3972
|
+
m: 4096,
|
|
3973
|
+
p: 8,
|
|
3974
|
+
dkLen: 32
|
|
3975
|
+
});
|
|
3976
|
+
return hkdf(Buffer.from(key), tweetnacl2.secretbox.keyLength, {
|
|
3977
|
+
info: Buffer.from("encryptionKey")
|
|
3978
|
+
});
|
|
3979
|
+
};
|
|
3980
|
+
var getExistingEncryptionMaterial = async (existingEncryptedSecretKey, existingNonce, docId) => {
|
|
3981
|
+
const derivedKey = await deriveKeyFromAg2Hash(docId, toUint8Array7(existingNonce));
|
|
3982
|
+
const secretKey = tweetnacl2.secretbox.open(
|
|
3983
|
+
toUint8Array7(existingEncryptedSecretKey),
|
|
3984
|
+
toUint8Array7(existingNonce),
|
|
3985
|
+
derivedKey
|
|
3986
|
+
);
|
|
3987
|
+
return {
|
|
3988
|
+
encryptedSecretKey: existingEncryptedSecretKey,
|
|
3989
|
+
nonce: toUint8Array7(existingNonce),
|
|
3990
|
+
secretKey,
|
|
3991
|
+
derivedKey: new Uint8Array(derivedKey)
|
|
3992
|
+
};
|
|
3993
|
+
};
|
|
3994
|
+
var getNaclSecretKey = async (ddocId) => {
|
|
3995
|
+
const { secretKey } = tweetnacl2.box.keyPair();
|
|
3996
|
+
const nonce = tweetnacl2.randomBytes(tweetnacl2.secretbox.nonceLength);
|
|
3997
|
+
const derivedKey = await deriveKeyFromAg2Hash(ddocId, nonce);
|
|
3998
|
+
const encryptedSecretKey = fromUint8Array4(tweetnacl2.secretbox(secretKey, nonce, derivedKey), true);
|
|
3999
|
+
return { nonce, encryptedSecretKey, secretKey, derivedKey: new Uint8Array(derivedKey) };
|
|
4000
|
+
};
|
|
4001
|
+
var generateLinkKeyMaterial = async (params) => {
|
|
4002
|
+
if (params.linkKeyNonce && params.linkKey) {
|
|
4003
|
+
const { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2, derivedKey: derivedKey2 } = await getExistingEncryptionMaterial(
|
|
4004
|
+
params.linkKey,
|
|
4005
|
+
params.linkKeyNonce,
|
|
4006
|
+
params.ddocId
|
|
4007
|
+
);
|
|
4008
|
+
if (secretKey2) return { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2, derivedKey: derivedKey2 };
|
|
4009
|
+
}
|
|
4010
|
+
const { secretKey, nonce, encryptedSecretKey, derivedKey } = await getNaclSecretKey(params.ddocId);
|
|
4011
|
+
return { secretKey, nonce, encryptedSecretKey, derivedKey };
|
|
4012
|
+
};
|
|
4013
|
+
|
|
4014
|
+
// src/domain/file/index.ts
|
|
3975
4015
|
async function listFiles(params) {
|
|
3976
4016
|
const { limit, skip, portalAddress } = params;
|
|
3977
4017
|
const effectiveLimit = limit || DEFAULT_LIST_LIMIT;
|
|
@@ -4035,9 +4075,9 @@ var createFile = async (input) => {
|
|
|
4035
4075
|
ddocId,
|
|
4036
4076
|
portalAddress: input.portalAddress,
|
|
4037
4077
|
linkKey: encryptedSecretKey,
|
|
4038
|
-
linkKeyNonce:
|
|
4039
|
-
derivedKey:
|
|
4040
|
-
secretKey:
|
|
4078
|
+
linkKeyNonce: fromUint8Array5(nonce),
|
|
4079
|
+
derivedKey: fromUint8Array5(derivedKey),
|
|
4080
|
+
secretKey: fromUint8Array5(secretKey)
|
|
4041
4081
|
});
|
|
4042
4082
|
await EventsModel.create({ type: "create", fileId: file2._id, portalAddress: file2.portalAddress });
|
|
4043
4083
|
return file2;
|