@fileverse/api 0.0.14 → 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 +106 -68
- 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 +101 -60
- package/dist/index.js.map +1 -1
- package/dist/worker.js +45 -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);
|
|
@@ -3193,11 +3181,17 @@ var init_publish = __esm({
|
|
|
3193
3181
|
init_config();
|
|
3194
3182
|
init_pimlico_utils();
|
|
3195
3183
|
createFileManager = async (portalSeed, portalAddress, ucanSecret, privateAccountKey) => {
|
|
3184
|
+
console.log("Creating file manager");
|
|
3196
3185
|
const keyPair = EdKeypair.fromSecretKey(fromUint8Array3(ucanSecret));
|
|
3186
|
+
console.log("Created key pair");
|
|
3197
3187
|
const authTokenProvider = new AuthTokenProvider(keyPair, portalAddress);
|
|
3188
|
+
console.log("Created auth token provider");
|
|
3198
3189
|
const keyStore = new KeyStore(toUint8Array4(portalSeed), portalAddress, authTokenProvider);
|
|
3190
|
+
console.log("Created key store");
|
|
3199
3191
|
const agentClient = new AgentClient(authTokenProvider);
|
|
3192
|
+
console.log("Created agent client");
|
|
3200
3193
|
await agentClient.initializeAgentClient(privateAccountKey);
|
|
3194
|
+
console.log("Initialized agent client");
|
|
3201
3195
|
return new FileManager(keyStore, agentClient);
|
|
3202
3196
|
};
|
|
3203
3197
|
executeOperation = async (fileManager, file2, operation) => {
|
|
@@ -3850,18 +3844,18 @@ init_esm_shims();
|
|
|
3850
3844
|
// src/cli/fetch-api-key.ts
|
|
3851
3845
|
init_esm_shims();
|
|
3852
3846
|
init_constants();
|
|
3853
|
-
import
|
|
3847
|
+
import axios from "axios";
|
|
3854
3848
|
import { toUint8Array as toUint8Array5 } from "js-base64";
|
|
3855
3849
|
import { sha256 } from "viem";
|
|
3856
3850
|
var fetchApiKeyData = async (apiKey) => {
|
|
3857
3851
|
try {
|
|
3858
3852
|
const keyHash = sha256(toUint8Array5(apiKey));
|
|
3859
3853
|
const fullUrl = BASE_CONFIG.API_URL + `api-access/${keyHash}`;
|
|
3860
|
-
const response = await
|
|
3854
|
+
const response = await axios.get(fullUrl);
|
|
3861
3855
|
const { encryptedKeyMaterial, encryptedAppMaterial, id } = response.data;
|
|
3862
3856
|
return { encryptedKeyMaterial, encryptedAppMaterial, id };
|
|
3863
3857
|
} catch (error48) {
|
|
3864
|
-
if (
|
|
3858
|
+
if (axios.isAxiosError(error48)) {
|
|
3865
3859
|
if (error48.response?.status === 401) {
|
|
3866
3860
|
throw new Error("Invalid API key");
|
|
3867
3861
|
}
|
|
@@ -3963,9 +3957,56 @@ init_esm_shims();
|
|
|
3963
3957
|
init_esm_shims();
|
|
3964
3958
|
init_models();
|
|
3965
3959
|
init_constants2();
|
|
3966
|
-
init_file_utils();
|
|
3967
3960
|
import { generate } from "short-uuid";
|
|
3968
|
-
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);
|
|
3971
|
+
return hkdf(Buffer.from(key), tweetnacl2.secretbox.keyLength, {
|
|
3972
|
+
info: Buffer.from("encryptionKey")
|
|
3973
|
+
});
|
|
3974
|
+
};
|
|
3975
|
+
var getExistingEncryptionMaterial = async (existingEncryptedSecretKey, existingNonce, docId) => {
|
|
3976
|
+
const derivedKey = await deriveKeyFromAg2Hash(docId, toUint8Array7(existingNonce));
|
|
3977
|
+
const secretKey = tweetnacl2.secretbox.open(
|
|
3978
|
+
toUint8Array7(existingEncryptedSecretKey),
|
|
3979
|
+
toUint8Array7(existingNonce),
|
|
3980
|
+
derivedKey
|
|
3981
|
+
);
|
|
3982
|
+
return {
|
|
3983
|
+
encryptedSecretKey: existingEncryptedSecretKey,
|
|
3984
|
+
nonce: toUint8Array7(existingNonce),
|
|
3985
|
+
secretKey,
|
|
3986
|
+
derivedKey: new Uint8Array(derivedKey)
|
|
3987
|
+
};
|
|
3988
|
+
};
|
|
3989
|
+
var getNaclSecretKey = async (ddocId) => {
|
|
3990
|
+
const { secretKey } = tweetnacl2.box.keyPair();
|
|
3991
|
+
const nonce = tweetnacl2.randomBytes(tweetnacl2.secretbox.nonceLength);
|
|
3992
|
+
const derivedKey = await deriveKeyFromAg2Hash(ddocId, nonce);
|
|
3993
|
+
const encryptedSecretKey = fromUint8Array4(tweetnacl2.secretbox(secretKey, nonce, derivedKey), true);
|
|
3994
|
+
return { nonce, encryptedSecretKey, secretKey, derivedKey: new Uint8Array(derivedKey) };
|
|
3995
|
+
};
|
|
3996
|
+
var generateLinkKeyMaterial = async (params) => {
|
|
3997
|
+
if (params.linkKeyNonce && params.linkKey) {
|
|
3998
|
+
const { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2, derivedKey: derivedKey2 } = await getExistingEncryptionMaterial(
|
|
3999
|
+
params.linkKey,
|
|
4000
|
+
params.linkKeyNonce,
|
|
4001
|
+
params.ddocId
|
|
4002
|
+
);
|
|
4003
|
+
if (secretKey2) return { encryptedSecretKey: encryptedSecretKey2, nonce: nonce2, secretKey: secretKey2, derivedKey: derivedKey2 };
|
|
4004
|
+
}
|
|
4005
|
+
const { secretKey, nonce, encryptedSecretKey, derivedKey } = await getNaclSecretKey(params.ddocId);
|
|
4006
|
+
return { secretKey, nonce, encryptedSecretKey, derivedKey };
|
|
4007
|
+
};
|
|
4008
|
+
|
|
4009
|
+
// src/domain/file/index.ts
|
|
3969
4010
|
async function listFiles(params) {
|
|
3970
4011
|
const { limit, skip, portalAddress } = params;
|
|
3971
4012
|
const effectiveLimit = limit || DEFAULT_LIST_LIMIT;
|
|
@@ -4029,9 +4070,9 @@ var createFile = async (input) => {
|
|
|
4029
4070
|
ddocId,
|
|
4030
4071
|
portalAddress: input.portalAddress,
|
|
4031
4072
|
linkKey: encryptedSecretKey,
|
|
4032
|
-
linkKeyNonce:
|
|
4033
|
-
derivedKey:
|
|
4034
|
-
secretKey:
|
|
4073
|
+
linkKeyNonce: fromUint8Array5(nonce),
|
|
4074
|
+
derivedKey: fromUint8Array5(derivedKey),
|
|
4075
|
+
secretKey: fromUint8Array5(secretKey)
|
|
4035
4076
|
});
|
|
4036
4077
|
await EventsModel.create({ type: "create", fileId: file2._id, portalAddress: file2.portalAddress });
|
|
4037
4078
|
return file2;
|