bedrock-ts-sdk 0.0.3 → 0.0.5
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/README.md +4 -4
- package/dist/index.d.mts +11 -11
- package/dist/index.d.ts +11 -11
- package/dist/index.js +144 -260
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +144 -260
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -1
package/dist/index.mjs
CHANGED
|
@@ -116,24 +116,27 @@ var FileMetaEncryptedSchema = z.object({
|
|
|
116
116
|
// Encrypted filename
|
|
117
117
|
path: z.string(),
|
|
118
118
|
// Encrypted path
|
|
119
|
-
key:
|
|
120
|
-
// Encrypted AES key
|
|
121
|
-
iv:
|
|
122
|
-
// Encrypted IV
|
|
123
|
-
store_hash:
|
|
124
|
-
// Aleph STORE hash
|
|
119
|
+
key: z.string(),
|
|
120
|
+
// Encrypted AES key (ECIES encrypted)
|
|
121
|
+
iv: z.string(),
|
|
122
|
+
// Encrypted IV (ECIES encrypted)
|
|
123
|
+
store_hash: z.string(),
|
|
124
|
+
// Encrypted Aleph STORE hash
|
|
125
125
|
size: z.string(),
|
|
126
126
|
// Encrypted size
|
|
127
127
|
created_at: z.string(),
|
|
128
128
|
// Encrypted datetime
|
|
129
129
|
deleted_at: z.string().nullable(),
|
|
130
130
|
// Encrypted datetime or null
|
|
131
|
-
shared_keys: z.record(
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
131
|
+
shared_keys: z.record(
|
|
132
|
+
z.string(),
|
|
133
|
+
z.object({
|
|
134
|
+
key: z.string(),
|
|
135
|
+
// Encrypted key for recipient
|
|
136
|
+
iv: z.string()
|
|
137
|
+
// Encrypted IV for recipient
|
|
138
|
+
})
|
|
139
|
+
).default({})
|
|
137
140
|
});
|
|
138
141
|
var FileMetaSchema = z.object({
|
|
139
142
|
name: z.string(),
|
|
@@ -144,10 +147,13 @@ var FileMetaSchema = z.object({
|
|
|
144
147
|
size: z.number(),
|
|
145
148
|
created_at: DatetimeSchema,
|
|
146
149
|
deleted_at: DatetimeSchema.nullable(),
|
|
147
|
-
shared_keys: z.record(
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
150
|
+
shared_keys: z.record(
|
|
151
|
+
z.string(),
|
|
152
|
+
z.object({
|
|
153
|
+
key: HexString64Schema,
|
|
154
|
+
iv: HexString32Schema
|
|
155
|
+
})
|
|
156
|
+
).default({})
|
|
151
157
|
});
|
|
152
158
|
var PublicFileMetaSchema = z.object({
|
|
153
159
|
name: z.string(),
|
|
@@ -192,13 +198,15 @@ var FileEntriesAggregateSchema = z.object({
|
|
|
192
198
|
files: z.array(FileEntrySchema).default([])
|
|
193
199
|
});
|
|
194
200
|
var SecurityAggregateSchema = z.object({
|
|
195
|
-
authorizations: z.array(
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
201
|
+
authorizations: z.array(
|
|
202
|
+
z.object({
|
|
203
|
+
address: AddressSchema,
|
|
204
|
+
chain: z.string(),
|
|
205
|
+
channels: z.array(z.string()).optional(),
|
|
206
|
+
post_types: z.array(z.string()).optional(),
|
|
207
|
+
aggregate_keys: z.array(z.string()).optional()
|
|
208
|
+
})
|
|
209
|
+
)
|
|
202
210
|
});
|
|
203
211
|
|
|
204
212
|
// src/client/aleph-service.ts
|
|
@@ -811,68 +819,28 @@ var EncryptionService = class {
|
|
|
811
819
|
// ============================================================================
|
|
812
820
|
static async encryptBrowser(data, key, iv) {
|
|
813
821
|
const crypto = CryptoUtils.getCrypto();
|
|
814
|
-
const cryptoKey = await crypto.subtle.importKey(
|
|
815
|
-
"raw",
|
|
816
|
-
key,
|
|
817
|
-
{ name: "AES-CBC" },
|
|
818
|
-
false,
|
|
819
|
-
["encrypt"]
|
|
820
|
-
);
|
|
822
|
+
const cryptoKey = await crypto.subtle.importKey("raw", key, { name: "AES-CBC" }, false, ["encrypt"]);
|
|
821
823
|
const dataBuffer = Buffer.from(data, "utf-8");
|
|
822
|
-
const encrypted = await crypto.subtle.encrypt(
|
|
823
|
-
{ name: "AES-CBC", iv },
|
|
824
|
-
cryptoKey,
|
|
825
|
-
dataBuffer
|
|
826
|
-
);
|
|
824
|
+
const encrypted = await crypto.subtle.encrypt({ name: "AES-CBC", iv }, cryptoKey, dataBuffer);
|
|
827
825
|
return CryptoUtils.bufferToHex(Buffer.from(encrypted));
|
|
828
826
|
}
|
|
829
827
|
static async decryptBrowser(encryptedData, key, iv) {
|
|
830
828
|
const crypto = CryptoUtils.getCrypto();
|
|
831
|
-
const cryptoKey = await crypto.subtle.importKey(
|
|
832
|
-
"raw",
|
|
833
|
-
key,
|
|
834
|
-
{ name: "AES-CBC" },
|
|
835
|
-
false,
|
|
836
|
-
["decrypt"]
|
|
837
|
-
);
|
|
829
|
+
const cryptoKey = await crypto.subtle.importKey("raw", key, { name: "AES-CBC" }, false, ["decrypt"]);
|
|
838
830
|
const encryptedBuffer = CryptoUtils.hexToBuffer(encryptedData);
|
|
839
|
-
const decrypted = await crypto.subtle.decrypt(
|
|
840
|
-
{ name: "AES-CBC", iv },
|
|
841
|
-
cryptoKey,
|
|
842
|
-
encryptedBuffer
|
|
843
|
-
);
|
|
831
|
+
const decrypted = await crypto.subtle.decrypt({ name: "AES-CBC", iv }, cryptoKey, encryptedBuffer);
|
|
844
832
|
return Buffer.from(decrypted).toString("utf-8");
|
|
845
833
|
}
|
|
846
834
|
static async encryptFileBrowser(buffer, key, iv) {
|
|
847
835
|
const crypto = CryptoUtils.getCrypto();
|
|
848
|
-
const cryptoKey = await crypto.subtle.importKey(
|
|
849
|
-
|
|
850
|
-
key,
|
|
851
|
-
{ name: "AES-CBC" },
|
|
852
|
-
false,
|
|
853
|
-
["encrypt"]
|
|
854
|
-
);
|
|
855
|
-
const encrypted = await crypto.subtle.encrypt(
|
|
856
|
-
{ name: "AES-CBC", iv },
|
|
857
|
-
cryptoKey,
|
|
858
|
-
buffer
|
|
859
|
-
);
|
|
836
|
+
const cryptoKey = await crypto.subtle.importKey("raw", key, { name: "AES-CBC" }, false, ["encrypt"]);
|
|
837
|
+
const encrypted = await crypto.subtle.encrypt({ name: "AES-CBC", iv }, cryptoKey, buffer);
|
|
860
838
|
return Buffer.from(encrypted);
|
|
861
839
|
}
|
|
862
840
|
static async decryptFileBrowser(buffer, key, iv) {
|
|
863
841
|
const crypto = CryptoUtils.getCrypto();
|
|
864
|
-
const cryptoKey = await crypto.subtle.importKey(
|
|
865
|
-
|
|
866
|
-
key,
|
|
867
|
-
{ name: "AES-CBC" },
|
|
868
|
-
false,
|
|
869
|
-
["decrypt"]
|
|
870
|
-
);
|
|
871
|
-
const decrypted = await crypto.subtle.decrypt(
|
|
872
|
-
{ name: "AES-CBC", iv },
|
|
873
|
-
cryptoKey,
|
|
874
|
-
buffer
|
|
875
|
-
);
|
|
842
|
+
const cryptoKey = await crypto.subtle.importKey("raw", key, { name: "AES-CBC" }, false, ["decrypt"]);
|
|
843
|
+
const decrypted = await crypto.subtle.decrypt({ name: "AES-CBC", iv }, cryptoKey, buffer);
|
|
876
844
|
return Buffer.from(decrypted);
|
|
877
845
|
}
|
|
878
846
|
};
|
|
@@ -888,10 +856,7 @@ var FileService = class {
|
|
|
888
856
|
async setup() {
|
|
889
857
|
const aleph = this.core.getAlephService();
|
|
890
858
|
try {
|
|
891
|
-
await aleph.fetchAggregate(
|
|
892
|
-
AGGREGATE_KEYS.FILE_ENTRIES,
|
|
893
|
-
FileEntriesAggregateSchema
|
|
894
|
-
);
|
|
859
|
+
await aleph.fetchAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema);
|
|
895
860
|
} catch {
|
|
896
861
|
await aleph.createAggregate(AGGREGATE_KEYS.FILE_ENTRIES, { files: [] });
|
|
897
862
|
}
|
|
@@ -972,10 +937,7 @@ var FileService = class {
|
|
|
972
937
|
const aleph = this.core.getAlephService();
|
|
973
938
|
const privateKey = this.core.getSubAccountPrivateKey();
|
|
974
939
|
try {
|
|
975
|
-
const aggregate = await aleph.fetchAggregate(
|
|
976
|
-
AGGREGATE_KEYS.FILE_ENTRIES,
|
|
977
|
-
FileEntriesAggregateSchema
|
|
978
|
-
);
|
|
940
|
+
const aggregate = await aleph.fetchAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema);
|
|
979
941
|
return aggregate.files.map(({ post_hash, path, shared_with }) => ({
|
|
980
942
|
post_hash,
|
|
981
943
|
path: EncryptionService.decryptEcies(path, privateKey),
|
|
@@ -1053,10 +1015,10 @@ var FileService = class {
|
|
|
1053
1015
|
try {
|
|
1054
1016
|
for (const path of filePaths) {
|
|
1055
1017
|
const file = await this.getFile(path);
|
|
1056
|
-
await aleph.updatePost(
|
|
1018
|
+
const updatedPost = await aleph.updatePost(
|
|
1057
1019
|
POST_TYPES.FILE,
|
|
1058
1020
|
file.post_hash,
|
|
1059
|
-
[
|
|
1021
|
+
[aleph.getAddress()],
|
|
1060
1022
|
FileMetaEncryptedSchema,
|
|
1061
1023
|
async (encryptedMeta) => {
|
|
1062
1024
|
const decryptedMeta = await this.decryptFileMeta(encryptedMeta);
|
|
@@ -1064,6 +1026,11 @@ var FileService = class {
|
|
|
1064
1026
|
return await this.encryptFileMeta(decryptedMeta);
|
|
1065
1027
|
}
|
|
1066
1028
|
);
|
|
1029
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => ({
|
|
1030
|
+
files: aggregate.files.map(
|
|
1031
|
+
(entry) => entry.post_hash === file.post_hash ? { ...entry, post_hash: updatedPost.item_hash } : entry
|
|
1032
|
+
)
|
|
1033
|
+
}));
|
|
1067
1034
|
}
|
|
1068
1035
|
} catch (error) {
|
|
1069
1036
|
throw new FileError(`Failed to soft delete files: ${error.message}`);
|
|
@@ -1078,10 +1045,10 @@ var FileService = class {
|
|
|
1078
1045
|
try {
|
|
1079
1046
|
for (const path of filePaths) {
|
|
1080
1047
|
const file = await this.getFile(path);
|
|
1081
|
-
await aleph.updatePost(
|
|
1048
|
+
const updatedPost = await aleph.updatePost(
|
|
1082
1049
|
POST_TYPES.FILE,
|
|
1083
1050
|
file.post_hash,
|
|
1084
|
-
[
|
|
1051
|
+
[aleph.getAddress()],
|
|
1085
1052
|
FileMetaEncryptedSchema,
|
|
1086
1053
|
async (encryptedMeta) => {
|
|
1087
1054
|
const decryptedMeta = await this.decryptFileMeta(encryptedMeta);
|
|
@@ -1089,6 +1056,11 @@ var FileService = class {
|
|
|
1089
1056
|
return await this.encryptFileMeta(decryptedMeta);
|
|
1090
1057
|
}
|
|
1091
1058
|
);
|
|
1059
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => ({
|
|
1060
|
+
files: aggregate.files.map(
|
|
1061
|
+
(entry) => entry.post_hash === file.post_hash ? { ...entry, post_hash: updatedPost.item_hash } : entry
|
|
1062
|
+
)
|
|
1063
|
+
}));
|
|
1092
1064
|
}
|
|
1093
1065
|
} catch (error) {
|
|
1094
1066
|
throw new FileError(`Failed to restore files: ${error.message}`);
|
|
@@ -1102,15 +1074,9 @@ var FileService = class {
|
|
|
1102
1074
|
const aleph = this.core.getAlephService();
|
|
1103
1075
|
try {
|
|
1104
1076
|
const files = await Promise.all(filePaths.map((path) => this.getFile(path)));
|
|
1105
|
-
await aleph.updateAggregate(
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
async (aggregate) => ({
|
|
1109
|
-
files: aggregate.files.filter(
|
|
1110
|
-
(entry) => !files.some((f) => f.post_hash === entry.post_hash)
|
|
1111
|
-
)
|
|
1112
|
-
})
|
|
1113
|
-
);
|
|
1077
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => ({
|
|
1078
|
+
files: aggregate.files.filter((entry) => !files.some((f) => f.post_hash === entry.post_hash))
|
|
1079
|
+
}));
|
|
1114
1080
|
const hashesToForget = files.flatMap((f) => [f.store_hash, f.post_hash]);
|
|
1115
1081
|
await aleph.deleteFiles(hashesToForget);
|
|
1116
1082
|
} catch (error) {
|
|
@@ -1127,10 +1093,10 @@ var FileService = class {
|
|
|
1127
1093
|
try {
|
|
1128
1094
|
for (const { oldPath, newPath } of moves) {
|
|
1129
1095
|
const file = await this.getFile(oldPath);
|
|
1130
|
-
await aleph.updatePost(
|
|
1096
|
+
const updatedPost = await aleph.updatePost(
|
|
1131
1097
|
POST_TYPES.FILE,
|
|
1132
1098
|
file.post_hash,
|
|
1133
|
-
[
|
|
1099
|
+
[aleph.getAddress()],
|
|
1134
1100
|
FileMetaEncryptedSchema,
|
|
1135
1101
|
async (encryptedMeta) => {
|
|
1136
1102
|
const decryptedMeta = await this.decryptFileMeta(encryptedMeta);
|
|
@@ -1140,15 +1106,11 @@ var FileService = class {
|
|
|
1140
1106
|
}
|
|
1141
1107
|
);
|
|
1142
1108
|
const newEncryptedPath = EncryptionService.encryptEcies(newPath, publicKey);
|
|
1143
|
-
await aleph.updateAggregate(
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
(entry) => entry.post_hash === file.post_hash ? { ...entry, path: newEncryptedPath } : entry
|
|
1149
|
-
)
|
|
1150
|
-
})
|
|
1151
|
-
);
|
|
1109
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => ({
|
|
1110
|
+
files: aggregate.files.map(
|
|
1111
|
+
(entry) => entry.post_hash === file.post_hash ? { ...entry, path: newEncryptedPath, post_hash: updatedPost.item_hash } : entry
|
|
1112
|
+
)
|
|
1113
|
+
}));
|
|
1152
1114
|
}
|
|
1153
1115
|
} catch (error) {
|
|
1154
1116
|
throw new FileError(`Failed to move files: ${error.message}`);
|
|
@@ -1163,11 +1125,13 @@ var FileService = class {
|
|
|
1163
1125
|
try {
|
|
1164
1126
|
const sourceFile = await this.getFile(sourcePath);
|
|
1165
1127
|
const content = await this.downloadFile(sourceFile);
|
|
1166
|
-
const [newFile] = await this.uploadFiles([
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1128
|
+
const [newFile] = await this.uploadFiles([
|
|
1129
|
+
{
|
|
1130
|
+
name: newPath.split("/").pop() || newPath,
|
|
1131
|
+
path: newPath,
|
|
1132
|
+
content
|
|
1133
|
+
}
|
|
1134
|
+
]);
|
|
1171
1135
|
return newFile;
|
|
1172
1136
|
} catch (error) {
|
|
1173
1137
|
throw new FileError(`Failed to duplicate file: ${error.message}`);
|
|
@@ -1184,10 +1148,10 @@ var FileService = class {
|
|
|
1184
1148
|
const file = await this.getFile(filePath);
|
|
1185
1149
|
const encryptedKey = EncryptionService.encryptEcies(file.key, contactPublicKey);
|
|
1186
1150
|
const encryptedIv = EncryptionService.encryptEcies(file.iv, contactPublicKey);
|
|
1187
|
-
await aleph.updatePost(
|
|
1151
|
+
const updatedPost = await aleph.updatePost(
|
|
1188
1152
|
POST_TYPES.FILE,
|
|
1189
1153
|
file.post_hash,
|
|
1190
|
-
[
|
|
1154
|
+
[aleph.getAddress()],
|
|
1191
1155
|
FileMetaEncryptedSchema,
|
|
1192
1156
|
async (encryptedMeta) => {
|
|
1193
1157
|
const decryptedMeta = await this.decryptFileMeta(encryptedMeta);
|
|
@@ -1198,15 +1162,15 @@ var FileService = class {
|
|
|
1198
1162
|
return await this.encryptFileMeta(decryptedMeta);
|
|
1199
1163
|
}
|
|
1200
1164
|
);
|
|
1201
|
-
await aleph.updateAggregate(
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
);
|
|
1165
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => ({
|
|
1166
|
+
files: aggregate.files.map(
|
|
1167
|
+
(entry) => entry.post_hash === file.post_hash ? {
|
|
1168
|
+
...entry,
|
|
1169
|
+
post_hash: updatedPost.item_hash,
|
|
1170
|
+
shared_with: [.../* @__PURE__ */ new Set([...entry.shared_with, contactPublicKey])]
|
|
1171
|
+
} : entry
|
|
1172
|
+
)
|
|
1173
|
+
}));
|
|
1210
1174
|
} catch (error) {
|
|
1211
1175
|
throw new FileError(`Failed to share file: ${error.message}`);
|
|
1212
1176
|
}
|
|
@@ -1220,10 +1184,10 @@ var FileService = class {
|
|
|
1220
1184
|
const aleph = this.core.getAlephService();
|
|
1221
1185
|
try {
|
|
1222
1186
|
const file = await this.getFile(filePath);
|
|
1223
|
-
await aleph.updatePost(
|
|
1187
|
+
const updatedPost = await aleph.updatePost(
|
|
1224
1188
|
POST_TYPES.FILE,
|
|
1225
1189
|
file.post_hash,
|
|
1226
|
-
[
|
|
1190
|
+
[aleph.getAddress()],
|
|
1227
1191
|
FileMetaEncryptedSchema,
|
|
1228
1192
|
async (encryptedMeta) => {
|
|
1229
1193
|
const decryptedMeta = await this.decryptFileMeta(encryptedMeta);
|
|
@@ -1231,15 +1195,15 @@ var FileService = class {
|
|
|
1231
1195
|
return await this.encryptFileMeta(decryptedMeta);
|
|
1232
1196
|
}
|
|
1233
1197
|
);
|
|
1234
|
-
await aleph.updateAggregate(
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
);
|
|
1198
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => ({
|
|
1199
|
+
files: aggregate.files.map(
|
|
1200
|
+
(entry) => entry.post_hash === file.post_hash ? {
|
|
1201
|
+
...entry,
|
|
1202
|
+
post_hash: updatedPost.item_hash,
|
|
1203
|
+
shared_with: entry.shared_with.filter((pk) => pk !== contactPublicKey)
|
|
1204
|
+
} : entry
|
|
1205
|
+
)
|
|
1206
|
+
}));
|
|
1243
1207
|
} catch (error) {
|
|
1244
1208
|
throw new FileError(`Failed to unshare file: ${error.message}`);
|
|
1245
1209
|
}
|
|
@@ -1305,40 +1269,32 @@ var FileService = class {
|
|
|
1305
1269
|
async saveFileEntries(files) {
|
|
1306
1270
|
const aleph = this.core.getAlephService();
|
|
1307
1271
|
const publicKey = this.core.getPublicKey();
|
|
1308
|
-
await aleph.updateAggregate(
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
}));
|
|
1317
|
-
return { files: [...aggregate.files, ...newEntries] };
|
|
1318
|
-
}
|
|
1319
|
-
);
|
|
1272
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => {
|
|
1273
|
+
const newEntries = files.map((f) => ({
|
|
1274
|
+
path: EncryptionService.encryptEcies(f.path, publicKey),
|
|
1275
|
+
post_hash: f.post_hash,
|
|
1276
|
+
shared_with: f.shared_with || []
|
|
1277
|
+
}));
|
|
1278
|
+
return { files: [...aggregate.files, ...newEntries] };
|
|
1279
|
+
});
|
|
1320
1280
|
}
|
|
1321
1281
|
async encryptFileMeta(meta) {
|
|
1322
|
-
const key = this.core.getEncryptionKey();
|
|
1323
|
-
const iv = EncryptionService.generateIv();
|
|
1324
1282
|
const publicKey = this.core.getPublicKey();
|
|
1325
1283
|
const fileKey = Buffer.from(meta.key, "hex");
|
|
1326
1284
|
const fileIv = Buffer.from(meta.iv, "hex");
|
|
1327
1285
|
return {
|
|
1328
|
-
name: await EncryptionService.encrypt(meta.name,
|
|
1286
|
+
name: await EncryptionService.encrypt(meta.name, fileKey, fileIv),
|
|
1329
1287
|
path: await EncryptionService.encrypt(meta.path, fileKey, fileIv),
|
|
1330
1288
|
key: EncryptionService.encryptEcies(meta.key, publicKey),
|
|
1331
1289
|
iv: EncryptionService.encryptEcies(meta.iv, publicKey),
|
|
1332
1290
|
store_hash: await EncryptionService.encrypt(meta.store_hash, fileKey, fileIv),
|
|
1333
|
-
size: await EncryptionService.encrypt(meta.size.toString(),
|
|
1334
|
-
created_at: await EncryptionService.encrypt(meta.created_at,
|
|
1291
|
+
size: await EncryptionService.encrypt(meta.size.toString(), fileKey, fileIv),
|
|
1292
|
+
created_at: await EncryptionService.encrypt(meta.created_at, fileKey, fileIv),
|
|
1335
1293
|
deleted_at: await EncryptionService.encrypt(meta.deleted_at ?? "null", fileKey, fileIv),
|
|
1336
1294
|
shared_keys: meta.shared_keys
|
|
1337
1295
|
};
|
|
1338
1296
|
}
|
|
1339
1297
|
async decryptFileMeta(encryptedMeta, privateKey) {
|
|
1340
|
-
const key = this.core.getEncryptionKey();
|
|
1341
|
-
const iv = EncryptionService.generateIv();
|
|
1342
1298
|
const privKey = privateKey || this.core.getSubAccountPrivateKey();
|
|
1343
1299
|
if (!privKey) {
|
|
1344
1300
|
throw new EncryptionError("Private key not available");
|
|
@@ -1349,13 +1305,13 @@ var FileService = class {
|
|
|
1349
1305
|
const fileIv = Buffer.from(decryptedIv, "hex");
|
|
1350
1306
|
const decryptedDeletedAt = await EncryptionService.decrypt(encryptedMeta.deleted_at, fileKey, fileIv);
|
|
1351
1307
|
return {
|
|
1352
|
-
name: await EncryptionService.decrypt(encryptedMeta.name,
|
|
1308
|
+
name: await EncryptionService.decrypt(encryptedMeta.name, fileKey, fileIv),
|
|
1353
1309
|
path: await EncryptionService.decrypt(encryptedMeta.path, fileKey, fileIv),
|
|
1354
1310
|
key: decryptedKey,
|
|
1355
1311
|
iv: decryptedIv,
|
|
1356
1312
|
store_hash: await EncryptionService.decrypt(encryptedMeta.store_hash, fileKey, fileIv),
|
|
1357
|
-
size: parseInt(await EncryptionService.decrypt(encryptedMeta.size,
|
|
1358
|
-
created_at: await EncryptionService.decrypt(encryptedMeta.created_at,
|
|
1313
|
+
size: Number.parseInt(await EncryptionService.decrypt(encryptedMeta.size, fileKey, fileIv)),
|
|
1314
|
+
created_at: await EncryptionService.decrypt(encryptedMeta.created_at, fileKey, fileIv),
|
|
1359
1315
|
deleted_at: decryptedDeletedAt === "null" ? null : decryptedDeletedAt,
|
|
1360
1316
|
shared_keys: encryptedMeta.shared_keys || {}
|
|
1361
1317
|
};
|
|
@@ -1374,10 +1330,7 @@ var ContactService = class {
|
|
|
1374
1330
|
async setup() {
|
|
1375
1331
|
const aleph = this.core.getAlephService();
|
|
1376
1332
|
try {
|
|
1377
|
-
await aleph.fetchAggregate(
|
|
1378
|
-
AGGREGATE_KEYS.CONTACTS,
|
|
1379
|
-
ContactsAggregateSchema
|
|
1380
|
-
);
|
|
1333
|
+
await aleph.fetchAggregate(AGGREGATE_KEYS.CONTACTS, ContactsAggregateSchema);
|
|
1381
1334
|
} catch {
|
|
1382
1335
|
await aleph.createAggregate(AGGREGATE_KEYS.CONTACTS, { contacts: [] });
|
|
1383
1336
|
}
|
|
@@ -1388,10 +1341,7 @@ var ContactService = class {
|
|
|
1388
1341
|
async listContacts() {
|
|
1389
1342
|
const aleph = this.core.getAlephService();
|
|
1390
1343
|
try {
|
|
1391
|
-
const aggregate = await aleph.fetchAggregate(
|
|
1392
|
-
AGGREGATE_KEYS.CONTACTS,
|
|
1393
|
-
ContactsAggregateSchema
|
|
1394
|
-
);
|
|
1344
|
+
const aggregate = await aleph.fetchAggregate(AGGREGATE_KEYS.CONTACTS, ContactsAggregateSchema);
|
|
1395
1345
|
return aggregate.contacts;
|
|
1396
1346
|
} catch (error) {
|
|
1397
1347
|
throw new ContactError(`Failed to fetch contacts: ${error.message}`);
|
|
@@ -1442,13 +1392,9 @@ var ContactService = class {
|
|
|
1442
1392
|
address,
|
|
1443
1393
|
public_key: publicKey
|
|
1444
1394
|
};
|
|
1445
|
-
await aleph.updateAggregate(
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
async (aggregate) => ({
|
|
1449
|
-
contacts: [...aggregate.contacts, newContact]
|
|
1450
|
-
})
|
|
1451
|
-
);
|
|
1395
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.CONTACTS, ContactsAggregateSchema, async (aggregate) => ({
|
|
1396
|
+
contacts: [...aggregate.contacts, newContact]
|
|
1397
|
+
}));
|
|
1452
1398
|
return newContact;
|
|
1453
1399
|
} catch (error) {
|
|
1454
1400
|
if (error instanceof ContactError) {
|
|
@@ -1465,13 +1411,9 @@ var ContactService = class {
|
|
|
1465
1411
|
const aleph = this.core.getAlephService();
|
|
1466
1412
|
try {
|
|
1467
1413
|
await this.getContact(publicKey);
|
|
1468
|
-
await aleph.updateAggregate(
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
async (aggregate) => ({
|
|
1472
|
-
contacts: aggregate.contacts.filter((c) => c.public_key !== publicKey)
|
|
1473
|
-
})
|
|
1474
|
-
);
|
|
1414
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.CONTACTS, ContactsAggregateSchema, async (aggregate) => ({
|
|
1415
|
+
contacts: aggregate.contacts.filter((c) => c.public_key !== publicKey)
|
|
1416
|
+
}));
|
|
1475
1417
|
} catch (error) {
|
|
1476
1418
|
if (error instanceof ContactError) {
|
|
1477
1419
|
throw error;
|
|
@@ -1492,15 +1434,9 @@ var ContactService = class {
|
|
|
1492
1434
|
...existingContact,
|
|
1493
1435
|
name: newName
|
|
1494
1436
|
};
|
|
1495
|
-
await aleph.updateAggregate(
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
async (aggregate) => ({
|
|
1499
|
-
contacts: aggregate.contacts.map(
|
|
1500
|
-
(c) => c.public_key === publicKey ? updatedContact : c
|
|
1501
|
-
)
|
|
1502
|
-
})
|
|
1503
|
-
);
|
|
1437
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.CONTACTS, ContactsAggregateSchema, async (aggregate) => ({
|
|
1438
|
+
contacts: aggregate.contacts.map((c) => c.public_key === publicKey ? updatedContact : c)
|
|
1439
|
+
}));
|
|
1504
1440
|
return updatedContact;
|
|
1505
1441
|
} catch (error) {
|
|
1506
1442
|
if (error instanceof ContactError) {
|
|
@@ -1517,9 +1453,7 @@ var ContactService = class {
|
|
|
1517
1453
|
try {
|
|
1518
1454
|
await this.getContact(publicKey);
|
|
1519
1455
|
const entries = await this.fileService.fetchFileEntries();
|
|
1520
|
-
const sharedEntries = entries.filter(
|
|
1521
|
-
(entry) => entry.shared_with.includes(publicKey)
|
|
1522
|
-
);
|
|
1456
|
+
const sharedEntries = entries.filter((entry) => entry.shared_with.includes(publicKey));
|
|
1523
1457
|
const files = await this.fileService.fetchFilesMetaFromEntries(sharedEntries);
|
|
1524
1458
|
return files;
|
|
1525
1459
|
} catch (error) {
|
|
@@ -1542,9 +1476,7 @@ var ContactService = class {
|
|
|
1542
1476
|
contact.address
|
|
1543
1477
|
// Important: contact's address, not current user's
|
|
1544
1478
|
);
|
|
1545
|
-
const sharedEntries = contactEntries.files.filter(
|
|
1546
|
-
(entry) => entry.shared_with.includes(currentUserPublicKey)
|
|
1547
|
-
);
|
|
1479
|
+
const sharedEntries = contactEntries.files.filter((entry) => entry.shared_with.includes(currentUserPublicKey));
|
|
1548
1480
|
const files = await this.fileService.fetchFilesMetaFromEntries(
|
|
1549
1481
|
sharedEntries,
|
|
1550
1482
|
contact.address
|
|
@@ -1594,10 +1526,7 @@ var KnowledgeBaseService = class {
|
|
|
1594
1526
|
async setup() {
|
|
1595
1527
|
const aleph = this.core.getAlephService();
|
|
1596
1528
|
try {
|
|
1597
|
-
await aleph.fetchAggregate(
|
|
1598
|
-
AGGREGATE_KEYS.KNOWLEDGE_BASES,
|
|
1599
|
-
KnowledgeBasesAggregateSchema
|
|
1600
|
-
);
|
|
1529
|
+
await aleph.fetchAggregate(AGGREGATE_KEYS.KNOWLEDGE_BASES, KnowledgeBasesAggregateSchema);
|
|
1601
1530
|
} catch {
|
|
1602
1531
|
await aleph.createAggregate(AGGREGATE_KEYS.KNOWLEDGE_BASES, { knowledge_bases: [] });
|
|
1603
1532
|
}
|
|
@@ -1608,10 +1537,7 @@ var KnowledgeBaseService = class {
|
|
|
1608
1537
|
async listKnowledgeBases() {
|
|
1609
1538
|
const aleph = this.core.getAlephService();
|
|
1610
1539
|
try {
|
|
1611
|
-
const aggregate = await aleph.fetchAggregate(
|
|
1612
|
-
AGGREGATE_KEYS.KNOWLEDGE_BASES,
|
|
1613
|
-
KnowledgeBasesAggregateSchema
|
|
1614
|
-
);
|
|
1540
|
+
const aggregate = await aleph.fetchAggregate(AGGREGATE_KEYS.KNOWLEDGE_BASES, KnowledgeBasesAggregateSchema);
|
|
1615
1541
|
return aggregate.knowledge_bases;
|
|
1616
1542
|
} catch (error) {
|
|
1617
1543
|
throw new KnowledgeBaseError(`Failed to fetch knowledge bases: ${error.message}`);
|
|
@@ -1649,13 +1575,9 @@ var KnowledgeBaseService = class {
|
|
|
1649
1575
|
created_at: now,
|
|
1650
1576
|
updated_at: now
|
|
1651
1577
|
};
|
|
1652
|
-
await aleph.updateAggregate(
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
async (aggregate) => ({
|
|
1656
|
-
knowledge_bases: [...aggregate.knowledge_bases, newKb]
|
|
1657
|
-
})
|
|
1658
|
-
);
|
|
1578
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.KNOWLEDGE_BASES, KnowledgeBasesAggregateSchema, async (aggregate) => ({
|
|
1579
|
+
knowledge_bases: [...aggregate.knowledge_bases, newKb]
|
|
1580
|
+
}));
|
|
1659
1581
|
return newKb;
|
|
1660
1582
|
} catch (error) {
|
|
1661
1583
|
if (error instanceof KnowledgeBaseError) {
|
|
@@ -1672,13 +1594,9 @@ var KnowledgeBaseService = class {
|
|
|
1672
1594
|
const aleph = this.core.getAlephService();
|
|
1673
1595
|
try {
|
|
1674
1596
|
await this.getKnowledgeBase(name);
|
|
1675
|
-
await aleph.updateAggregate(
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
async (aggregate) => ({
|
|
1679
|
-
knowledge_bases: aggregate.knowledge_bases.filter((k) => k.name !== name)
|
|
1680
|
-
})
|
|
1681
|
-
);
|
|
1597
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.KNOWLEDGE_BASES, KnowledgeBasesAggregateSchema, async (aggregate) => ({
|
|
1598
|
+
knowledge_bases: aggregate.knowledge_bases.filter((k) => k.name !== name)
|
|
1599
|
+
}));
|
|
1682
1600
|
} catch (error) {
|
|
1683
1601
|
if (error instanceof KnowledgeBaseError) {
|
|
1684
1602
|
throw error;
|
|
@@ -1704,15 +1622,9 @@ var KnowledgeBaseService = class {
|
|
|
1704
1622
|
name: newName,
|
|
1705
1623
|
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
1706
1624
|
};
|
|
1707
|
-
await aleph.updateAggregate(
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
async (aggregate) => ({
|
|
1711
|
-
knowledge_bases: aggregate.knowledge_bases.map(
|
|
1712
|
-
(k) => k.name === oldName ? updatedKb : k
|
|
1713
|
-
)
|
|
1714
|
-
})
|
|
1715
|
-
);
|
|
1625
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.KNOWLEDGE_BASES, KnowledgeBasesAggregateSchema, async (aggregate) => ({
|
|
1626
|
+
knowledge_bases: aggregate.knowledge_bases.map((k) => k.name === oldName ? updatedKb : k)
|
|
1627
|
+
}));
|
|
1716
1628
|
return updatedKb;
|
|
1717
1629
|
} catch (error) {
|
|
1718
1630
|
if (error instanceof KnowledgeBaseError) {
|
|
@@ -1735,15 +1647,9 @@ var KnowledgeBaseService = class {
|
|
|
1735
1647
|
file_paths: filePaths,
|
|
1736
1648
|
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
1737
1649
|
};
|
|
1738
|
-
await aleph.updateAggregate(
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
async (aggregate) => ({
|
|
1742
|
-
knowledge_bases: aggregate.knowledge_bases.map(
|
|
1743
|
-
(k) => k.name === name ? updatedKb : k
|
|
1744
|
-
)
|
|
1745
|
-
})
|
|
1746
|
-
);
|
|
1650
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.KNOWLEDGE_BASES, KnowledgeBasesAggregateSchema, async (aggregate) => ({
|
|
1651
|
+
knowledge_bases: aggregate.knowledge_bases.map((k) => k.name === name ? updatedKb : k)
|
|
1652
|
+
}));
|
|
1747
1653
|
return updatedKb;
|
|
1748
1654
|
} catch (error) {
|
|
1749
1655
|
if (error instanceof KnowledgeBaseError) {
|
|
@@ -1767,15 +1673,9 @@ var KnowledgeBaseService = class {
|
|
|
1767
1673
|
file_paths: updatedFilePaths,
|
|
1768
1674
|
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
1769
1675
|
};
|
|
1770
|
-
await aleph.updateAggregate(
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
async (aggregate) => ({
|
|
1774
|
-
knowledge_bases: aggregate.knowledge_bases.map(
|
|
1775
|
-
(k) => k.name === name ? updatedKb : k
|
|
1776
|
-
)
|
|
1777
|
-
})
|
|
1778
|
-
);
|
|
1676
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.KNOWLEDGE_BASES, KnowledgeBasesAggregateSchema, async (aggregate) => ({
|
|
1677
|
+
knowledge_bases: aggregate.knowledge_bases.map((k) => k.name === name ? updatedKb : k)
|
|
1678
|
+
}));
|
|
1779
1679
|
return updatedKb;
|
|
1780
1680
|
} catch (error) {
|
|
1781
1681
|
if (error instanceof KnowledgeBaseError) {
|
|
@@ -1793,23 +1693,15 @@ var KnowledgeBaseService = class {
|
|
|
1793
1693
|
const aleph = this.core.getAlephService();
|
|
1794
1694
|
try {
|
|
1795
1695
|
const existingKb = await this.getKnowledgeBase(name);
|
|
1796
|
-
const updatedFilePaths = existingKb.file_paths.filter(
|
|
1797
|
-
(path) => !filePaths.includes(path)
|
|
1798
|
-
);
|
|
1696
|
+
const updatedFilePaths = existingKb.file_paths.filter((path) => !filePaths.includes(path));
|
|
1799
1697
|
const updatedKb = {
|
|
1800
1698
|
...existingKb,
|
|
1801
1699
|
file_paths: updatedFilePaths,
|
|
1802
1700
|
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
1803
1701
|
};
|
|
1804
|
-
await aleph.updateAggregate(
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
async (aggregate) => ({
|
|
1808
|
-
knowledge_bases: aggregate.knowledge_bases.map(
|
|
1809
|
-
(k) => k.name === name ? updatedKb : k
|
|
1810
|
-
)
|
|
1811
|
-
})
|
|
1812
|
-
);
|
|
1702
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.KNOWLEDGE_BASES, KnowledgeBasesAggregateSchema, async (aggregate) => ({
|
|
1703
|
+
knowledge_bases: aggregate.knowledge_bases.map((k) => k.name === name ? updatedKb : k)
|
|
1704
|
+
}));
|
|
1813
1705
|
return updatedKb;
|
|
1814
1706
|
} catch (error) {
|
|
1815
1707
|
if (error instanceof KnowledgeBaseError) {
|
|
@@ -1948,11 +1840,7 @@ var BedrockClient = class _BedrockClient {
|
|
|
1948
1840
|
* WARNING: This will delete all user data from Aleph
|
|
1949
1841
|
*/
|
|
1950
1842
|
async resetAllData() {
|
|
1951
|
-
await Promise.all([
|
|
1952
|
-
this.resetFiles(),
|
|
1953
|
-
this.resetContacts(),
|
|
1954
|
-
this.resetKnowledgeBases()
|
|
1955
|
-
]);
|
|
1843
|
+
await Promise.all([this.resetFiles(), this.resetContacts(), this.resetKnowledgeBases()]);
|
|
1956
1844
|
}
|
|
1957
1845
|
/**
|
|
1958
1846
|
* Reset all files
|
|
@@ -1985,11 +1873,7 @@ var BedrockClient = class _BedrockClient {
|
|
|
1985
1873
|
* Setup all services (create aggregates if they don't exist)
|
|
1986
1874
|
*/
|
|
1987
1875
|
async setup() {
|
|
1988
|
-
await Promise.all([
|
|
1989
|
-
this.files.setup(),
|
|
1990
|
-
this.contacts.setup(),
|
|
1991
|
-
this.knowledgeBases.setup()
|
|
1992
|
-
]);
|
|
1876
|
+
await Promise.all([this.files.setup(), this.contacts.setup(), this.knowledgeBases.setup()]);
|
|
1993
1877
|
}
|
|
1994
1878
|
};
|
|
1995
1879
|
|