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.js
CHANGED
|
@@ -188,24 +188,27 @@ var FileMetaEncryptedSchema = import_zod.z.object({
|
|
|
188
188
|
// Encrypted filename
|
|
189
189
|
path: import_zod.z.string(),
|
|
190
190
|
// Encrypted path
|
|
191
|
-
key:
|
|
192
|
-
// Encrypted AES key
|
|
193
|
-
iv:
|
|
194
|
-
// Encrypted IV
|
|
195
|
-
store_hash:
|
|
196
|
-
// Aleph STORE hash
|
|
191
|
+
key: import_zod.z.string(),
|
|
192
|
+
// Encrypted AES key (ECIES encrypted)
|
|
193
|
+
iv: import_zod.z.string(),
|
|
194
|
+
// Encrypted IV (ECIES encrypted)
|
|
195
|
+
store_hash: import_zod.z.string(),
|
|
196
|
+
// Encrypted Aleph STORE hash
|
|
197
197
|
size: import_zod.z.string(),
|
|
198
198
|
// Encrypted size
|
|
199
199
|
created_at: import_zod.z.string(),
|
|
200
200
|
// Encrypted datetime
|
|
201
201
|
deleted_at: import_zod.z.string().nullable(),
|
|
202
202
|
// Encrypted datetime or null
|
|
203
|
-
shared_keys: import_zod.z.record(
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
203
|
+
shared_keys: import_zod.z.record(
|
|
204
|
+
import_zod.z.string(),
|
|
205
|
+
import_zod.z.object({
|
|
206
|
+
key: import_zod.z.string(),
|
|
207
|
+
// Encrypted key for recipient
|
|
208
|
+
iv: import_zod.z.string()
|
|
209
|
+
// Encrypted IV for recipient
|
|
210
|
+
})
|
|
211
|
+
).default({})
|
|
209
212
|
});
|
|
210
213
|
var FileMetaSchema = import_zod.z.object({
|
|
211
214
|
name: import_zod.z.string(),
|
|
@@ -216,10 +219,13 @@ var FileMetaSchema = import_zod.z.object({
|
|
|
216
219
|
size: import_zod.z.number(),
|
|
217
220
|
created_at: DatetimeSchema,
|
|
218
221
|
deleted_at: DatetimeSchema.nullable(),
|
|
219
|
-
shared_keys: import_zod.z.record(
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
222
|
+
shared_keys: import_zod.z.record(
|
|
223
|
+
import_zod.z.string(),
|
|
224
|
+
import_zod.z.object({
|
|
225
|
+
key: HexString64Schema,
|
|
226
|
+
iv: HexString32Schema
|
|
227
|
+
})
|
|
228
|
+
).default({})
|
|
223
229
|
});
|
|
224
230
|
var PublicFileMetaSchema = import_zod.z.object({
|
|
225
231
|
name: import_zod.z.string(),
|
|
@@ -264,13 +270,15 @@ var FileEntriesAggregateSchema = import_zod.z.object({
|
|
|
264
270
|
files: import_zod.z.array(FileEntrySchema).default([])
|
|
265
271
|
});
|
|
266
272
|
var SecurityAggregateSchema = import_zod.z.object({
|
|
267
|
-
authorizations: import_zod.z.array(
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
273
|
+
authorizations: import_zod.z.array(
|
|
274
|
+
import_zod.z.object({
|
|
275
|
+
address: AddressSchema,
|
|
276
|
+
chain: import_zod.z.string(),
|
|
277
|
+
channels: import_zod.z.array(import_zod.z.string()).optional(),
|
|
278
|
+
post_types: import_zod.z.array(import_zod.z.string()).optional(),
|
|
279
|
+
aggregate_keys: import_zod.z.array(import_zod.z.string()).optional()
|
|
280
|
+
})
|
|
281
|
+
)
|
|
274
282
|
});
|
|
275
283
|
|
|
276
284
|
// src/client/aleph-service.ts
|
|
@@ -883,68 +891,28 @@ var EncryptionService = class {
|
|
|
883
891
|
// ============================================================================
|
|
884
892
|
static async encryptBrowser(data, key, iv) {
|
|
885
893
|
const crypto = CryptoUtils.getCrypto();
|
|
886
|
-
const cryptoKey = await crypto.subtle.importKey(
|
|
887
|
-
"raw",
|
|
888
|
-
key,
|
|
889
|
-
{ name: "AES-CBC" },
|
|
890
|
-
false,
|
|
891
|
-
["encrypt"]
|
|
892
|
-
);
|
|
894
|
+
const cryptoKey = await crypto.subtle.importKey("raw", key, { name: "AES-CBC" }, false, ["encrypt"]);
|
|
893
895
|
const dataBuffer = Buffer.from(data, "utf-8");
|
|
894
|
-
const encrypted = await crypto.subtle.encrypt(
|
|
895
|
-
{ name: "AES-CBC", iv },
|
|
896
|
-
cryptoKey,
|
|
897
|
-
dataBuffer
|
|
898
|
-
);
|
|
896
|
+
const encrypted = await crypto.subtle.encrypt({ name: "AES-CBC", iv }, cryptoKey, dataBuffer);
|
|
899
897
|
return CryptoUtils.bufferToHex(Buffer.from(encrypted));
|
|
900
898
|
}
|
|
901
899
|
static async decryptBrowser(encryptedData, key, iv) {
|
|
902
900
|
const crypto = CryptoUtils.getCrypto();
|
|
903
|
-
const cryptoKey = await crypto.subtle.importKey(
|
|
904
|
-
"raw",
|
|
905
|
-
key,
|
|
906
|
-
{ name: "AES-CBC" },
|
|
907
|
-
false,
|
|
908
|
-
["decrypt"]
|
|
909
|
-
);
|
|
901
|
+
const cryptoKey = await crypto.subtle.importKey("raw", key, { name: "AES-CBC" }, false, ["decrypt"]);
|
|
910
902
|
const encryptedBuffer = CryptoUtils.hexToBuffer(encryptedData);
|
|
911
|
-
const decrypted = await crypto.subtle.decrypt(
|
|
912
|
-
{ name: "AES-CBC", iv },
|
|
913
|
-
cryptoKey,
|
|
914
|
-
encryptedBuffer
|
|
915
|
-
);
|
|
903
|
+
const decrypted = await crypto.subtle.decrypt({ name: "AES-CBC", iv }, cryptoKey, encryptedBuffer);
|
|
916
904
|
return Buffer.from(decrypted).toString("utf-8");
|
|
917
905
|
}
|
|
918
906
|
static async encryptFileBrowser(buffer, key, iv) {
|
|
919
907
|
const crypto = CryptoUtils.getCrypto();
|
|
920
|
-
const cryptoKey = await crypto.subtle.importKey(
|
|
921
|
-
|
|
922
|
-
key,
|
|
923
|
-
{ name: "AES-CBC" },
|
|
924
|
-
false,
|
|
925
|
-
["encrypt"]
|
|
926
|
-
);
|
|
927
|
-
const encrypted = await crypto.subtle.encrypt(
|
|
928
|
-
{ name: "AES-CBC", iv },
|
|
929
|
-
cryptoKey,
|
|
930
|
-
buffer
|
|
931
|
-
);
|
|
908
|
+
const cryptoKey = await crypto.subtle.importKey("raw", key, { name: "AES-CBC" }, false, ["encrypt"]);
|
|
909
|
+
const encrypted = await crypto.subtle.encrypt({ name: "AES-CBC", iv }, cryptoKey, buffer);
|
|
932
910
|
return Buffer.from(encrypted);
|
|
933
911
|
}
|
|
934
912
|
static async decryptFileBrowser(buffer, key, iv) {
|
|
935
913
|
const crypto = CryptoUtils.getCrypto();
|
|
936
|
-
const cryptoKey = await crypto.subtle.importKey(
|
|
937
|
-
|
|
938
|
-
key,
|
|
939
|
-
{ name: "AES-CBC" },
|
|
940
|
-
false,
|
|
941
|
-
["decrypt"]
|
|
942
|
-
);
|
|
943
|
-
const decrypted = await crypto.subtle.decrypt(
|
|
944
|
-
{ name: "AES-CBC", iv },
|
|
945
|
-
cryptoKey,
|
|
946
|
-
buffer
|
|
947
|
-
);
|
|
914
|
+
const cryptoKey = await crypto.subtle.importKey("raw", key, { name: "AES-CBC" }, false, ["decrypt"]);
|
|
915
|
+
const decrypted = await crypto.subtle.decrypt({ name: "AES-CBC", iv }, cryptoKey, buffer);
|
|
948
916
|
return Buffer.from(decrypted);
|
|
949
917
|
}
|
|
950
918
|
};
|
|
@@ -960,10 +928,7 @@ var FileService = class {
|
|
|
960
928
|
async setup() {
|
|
961
929
|
const aleph = this.core.getAlephService();
|
|
962
930
|
try {
|
|
963
|
-
await aleph.fetchAggregate(
|
|
964
|
-
AGGREGATE_KEYS.FILE_ENTRIES,
|
|
965
|
-
FileEntriesAggregateSchema
|
|
966
|
-
);
|
|
931
|
+
await aleph.fetchAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema);
|
|
967
932
|
} catch {
|
|
968
933
|
await aleph.createAggregate(AGGREGATE_KEYS.FILE_ENTRIES, { files: [] });
|
|
969
934
|
}
|
|
@@ -1044,10 +1009,7 @@ var FileService = class {
|
|
|
1044
1009
|
const aleph = this.core.getAlephService();
|
|
1045
1010
|
const privateKey = this.core.getSubAccountPrivateKey();
|
|
1046
1011
|
try {
|
|
1047
|
-
const aggregate = await aleph.fetchAggregate(
|
|
1048
|
-
AGGREGATE_KEYS.FILE_ENTRIES,
|
|
1049
|
-
FileEntriesAggregateSchema
|
|
1050
|
-
);
|
|
1012
|
+
const aggregate = await aleph.fetchAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema);
|
|
1051
1013
|
return aggregate.files.map(({ post_hash, path, shared_with }) => ({
|
|
1052
1014
|
post_hash,
|
|
1053
1015
|
path: EncryptionService.decryptEcies(path, privateKey),
|
|
@@ -1125,10 +1087,10 @@ var FileService = class {
|
|
|
1125
1087
|
try {
|
|
1126
1088
|
for (const path of filePaths) {
|
|
1127
1089
|
const file = await this.getFile(path);
|
|
1128
|
-
await aleph.updatePost(
|
|
1090
|
+
const updatedPost = await aleph.updatePost(
|
|
1129
1091
|
POST_TYPES.FILE,
|
|
1130
1092
|
file.post_hash,
|
|
1131
|
-
[
|
|
1093
|
+
[aleph.getAddress()],
|
|
1132
1094
|
FileMetaEncryptedSchema,
|
|
1133
1095
|
async (encryptedMeta) => {
|
|
1134
1096
|
const decryptedMeta = await this.decryptFileMeta(encryptedMeta);
|
|
@@ -1136,6 +1098,11 @@ var FileService = class {
|
|
|
1136
1098
|
return await this.encryptFileMeta(decryptedMeta);
|
|
1137
1099
|
}
|
|
1138
1100
|
);
|
|
1101
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => ({
|
|
1102
|
+
files: aggregate.files.map(
|
|
1103
|
+
(entry) => entry.post_hash === file.post_hash ? { ...entry, post_hash: updatedPost.item_hash } : entry
|
|
1104
|
+
)
|
|
1105
|
+
}));
|
|
1139
1106
|
}
|
|
1140
1107
|
} catch (error) {
|
|
1141
1108
|
throw new FileError(`Failed to soft delete files: ${error.message}`);
|
|
@@ -1150,10 +1117,10 @@ var FileService = class {
|
|
|
1150
1117
|
try {
|
|
1151
1118
|
for (const path of filePaths) {
|
|
1152
1119
|
const file = await this.getFile(path);
|
|
1153
|
-
await aleph.updatePost(
|
|
1120
|
+
const updatedPost = await aleph.updatePost(
|
|
1154
1121
|
POST_TYPES.FILE,
|
|
1155
1122
|
file.post_hash,
|
|
1156
|
-
[
|
|
1123
|
+
[aleph.getAddress()],
|
|
1157
1124
|
FileMetaEncryptedSchema,
|
|
1158
1125
|
async (encryptedMeta) => {
|
|
1159
1126
|
const decryptedMeta = await this.decryptFileMeta(encryptedMeta);
|
|
@@ -1161,6 +1128,11 @@ var FileService = class {
|
|
|
1161
1128
|
return await this.encryptFileMeta(decryptedMeta);
|
|
1162
1129
|
}
|
|
1163
1130
|
);
|
|
1131
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => ({
|
|
1132
|
+
files: aggregate.files.map(
|
|
1133
|
+
(entry) => entry.post_hash === file.post_hash ? { ...entry, post_hash: updatedPost.item_hash } : entry
|
|
1134
|
+
)
|
|
1135
|
+
}));
|
|
1164
1136
|
}
|
|
1165
1137
|
} catch (error) {
|
|
1166
1138
|
throw new FileError(`Failed to restore files: ${error.message}`);
|
|
@@ -1174,15 +1146,9 @@ var FileService = class {
|
|
|
1174
1146
|
const aleph = this.core.getAlephService();
|
|
1175
1147
|
try {
|
|
1176
1148
|
const files = await Promise.all(filePaths.map((path) => this.getFile(path)));
|
|
1177
|
-
await aleph.updateAggregate(
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
async (aggregate) => ({
|
|
1181
|
-
files: aggregate.files.filter(
|
|
1182
|
-
(entry) => !files.some((f) => f.post_hash === entry.post_hash)
|
|
1183
|
-
)
|
|
1184
|
-
})
|
|
1185
|
-
);
|
|
1149
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => ({
|
|
1150
|
+
files: aggregate.files.filter((entry) => !files.some((f) => f.post_hash === entry.post_hash))
|
|
1151
|
+
}));
|
|
1186
1152
|
const hashesToForget = files.flatMap((f) => [f.store_hash, f.post_hash]);
|
|
1187
1153
|
await aleph.deleteFiles(hashesToForget);
|
|
1188
1154
|
} catch (error) {
|
|
@@ -1199,10 +1165,10 @@ var FileService = class {
|
|
|
1199
1165
|
try {
|
|
1200
1166
|
for (const { oldPath, newPath } of moves) {
|
|
1201
1167
|
const file = await this.getFile(oldPath);
|
|
1202
|
-
await aleph.updatePost(
|
|
1168
|
+
const updatedPost = await aleph.updatePost(
|
|
1203
1169
|
POST_TYPES.FILE,
|
|
1204
1170
|
file.post_hash,
|
|
1205
|
-
[
|
|
1171
|
+
[aleph.getAddress()],
|
|
1206
1172
|
FileMetaEncryptedSchema,
|
|
1207
1173
|
async (encryptedMeta) => {
|
|
1208
1174
|
const decryptedMeta = await this.decryptFileMeta(encryptedMeta);
|
|
@@ -1212,15 +1178,11 @@ var FileService = class {
|
|
|
1212
1178
|
}
|
|
1213
1179
|
);
|
|
1214
1180
|
const newEncryptedPath = EncryptionService.encryptEcies(newPath, publicKey);
|
|
1215
|
-
await aleph.updateAggregate(
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
(entry) => entry.post_hash === file.post_hash ? { ...entry, path: newEncryptedPath } : entry
|
|
1221
|
-
)
|
|
1222
|
-
})
|
|
1223
|
-
);
|
|
1181
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => ({
|
|
1182
|
+
files: aggregate.files.map(
|
|
1183
|
+
(entry) => entry.post_hash === file.post_hash ? { ...entry, path: newEncryptedPath, post_hash: updatedPost.item_hash } : entry
|
|
1184
|
+
)
|
|
1185
|
+
}));
|
|
1224
1186
|
}
|
|
1225
1187
|
} catch (error) {
|
|
1226
1188
|
throw new FileError(`Failed to move files: ${error.message}`);
|
|
@@ -1235,11 +1197,13 @@ var FileService = class {
|
|
|
1235
1197
|
try {
|
|
1236
1198
|
const sourceFile = await this.getFile(sourcePath);
|
|
1237
1199
|
const content = await this.downloadFile(sourceFile);
|
|
1238
|
-
const [newFile] = await this.uploadFiles([
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1200
|
+
const [newFile] = await this.uploadFiles([
|
|
1201
|
+
{
|
|
1202
|
+
name: newPath.split("/").pop() || newPath,
|
|
1203
|
+
path: newPath,
|
|
1204
|
+
content
|
|
1205
|
+
}
|
|
1206
|
+
]);
|
|
1243
1207
|
return newFile;
|
|
1244
1208
|
} catch (error) {
|
|
1245
1209
|
throw new FileError(`Failed to duplicate file: ${error.message}`);
|
|
@@ -1256,10 +1220,10 @@ var FileService = class {
|
|
|
1256
1220
|
const file = await this.getFile(filePath);
|
|
1257
1221
|
const encryptedKey = EncryptionService.encryptEcies(file.key, contactPublicKey);
|
|
1258
1222
|
const encryptedIv = EncryptionService.encryptEcies(file.iv, contactPublicKey);
|
|
1259
|
-
await aleph.updatePost(
|
|
1223
|
+
const updatedPost = await aleph.updatePost(
|
|
1260
1224
|
POST_TYPES.FILE,
|
|
1261
1225
|
file.post_hash,
|
|
1262
|
-
[
|
|
1226
|
+
[aleph.getAddress()],
|
|
1263
1227
|
FileMetaEncryptedSchema,
|
|
1264
1228
|
async (encryptedMeta) => {
|
|
1265
1229
|
const decryptedMeta = await this.decryptFileMeta(encryptedMeta);
|
|
@@ -1270,15 +1234,15 @@ var FileService = class {
|
|
|
1270
1234
|
return await this.encryptFileMeta(decryptedMeta);
|
|
1271
1235
|
}
|
|
1272
1236
|
);
|
|
1273
|
-
await aleph.updateAggregate(
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
);
|
|
1237
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => ({
|
|
1238
|
+
files: aggregate.files.map(
|
|
1239
|
+
(entry) => entry.post_hash === file.post_hash ? {
|
|
1240
|
+
...entry,
|
|
1241
|
+
post_hash: updatedPost.item_hash,
|
|
1242
|
+
shared_with: [.../* @__PURE__ */ new Set([...entry.shared_with, contactPublicKey])]
|
|
1243
|
+
} : entry
|
|
1244
|
+
)
|
|
1245
|
+
}));
|
|
1282
1246
|
} catch (error) {
|
|
1283
1247
|
throw new FileError(`Failed to share file: ${error.message}`);
|
|
1284
1248
|
}
|
|
@@ -1292,10 +1256,10 @@ var FileService = class {
|
|
|
1292
1256
|
const aleph = this.core.getAlephService();
|
|
1293
1257
|
try {
|
|
1294
1258
|
const file = await this.getFile(filePath);
|
|
1295
|
-
await aleph.updatePost(
|
|
1259
|
+
const updatedPost = await aleph.updatePost(
|
|
1296
1260
|
POST_TYPES.FILE,
|
|
1297
1261
|
file.post_hash,
|
|
1298
|
-
[
|
|
1262
|
+
[aleph.getAddress()],
|
|
1299
1263
|
FileMetaEncryptedSchema,
|
|
1300
1264
|
async (encryptedMeta) => {
|
|
1301
1265
|
const decryptedMeta = await this.decryptFileMeta(encryptedMeta);
|
|
@@ -1303,15 +1267,15 @@ var FileService = class {
|
|
|
1303
1267
|
return await this.encryptFileMeta(decryptedMeta);
|
|
1304
1268
|
}
|
|
1305
1269
|
);
|
|
1306
|
-
await aleph.updateAggregate(
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
);
|
|
1270
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => ({
|
|
1271
|
+
files: aggregate.files.map(
|
|
1272
|
+
(entry) => entry.post_hash === file.post_hash ? {
|
|
1273
|
+
...entry,
|
|
1274
|
+
post_hash: updatedPost.item_hash,
|
|
1275
|
+
shared_with: entry.shared_with.filter((pk) => pk !== contactPublicKey)
|
|
1276
|
+
} : entry
|
|
1277
|
+
)
|
|
1278
|
+
}));
|
|
1315
1279
|
} catch (error) {
|
|
1316
1280
|
throw new FileError(`Failed to unshare file: ${error.message}`);
|
|
1317
1281
|
}
|
|
@@ -1377,40 +1341,32 @@ var FileService = class {
|
|
|
1377
1341
|
async saveFileEntries(files) {
|
|
1378
1342
|
const aleph = this.core.getAlephService();
|
|
1379
1343
|
const publicKey = this.core.getPublicKey();
|
|
1380
|
-
await aleph.updateAggregate(
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
}));
|
|
1389
|
-
return { files: [...aggregate.files, ...newEntries] };
|
|
1390
|
-
}
|
|
1391
|
-
);
|
|
1344
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => {
|
|
1345
|
+
const newEntries = files.map((f) => ({
|
|
1346
|
+
path: EncryptionService.encryptEcies(f.path, publicKey),
|
|
1347
|
+
post_hash: f.post_hash,
|
|
1348
|
+
shared_with: f.shared_with || []
|
|
1349
|
+
}));
|
|
1350
|
+
return { files: [...aggregate.files, ...newEntries] };
|
|
1351
|
+
});
|
|
1392
1352
|
}
|
|
1393
1353
|
async encryptFileMeta(meta) {
|
|
1394
|
-
const key = this.core.getEncryptionKey();
|
|
1395
|
-
const iv = EncryptionService.generateIv();
|
|
1396
1354
|
const publicKey = this.core.getPublicKey();
|
|
1397
1355
|
const fileKey = Buffer.from(meta.key, "hex");
|
|
1398
1356
|
const fileIv = Buffer.from(meta.iv, "hex");
|
|
1399
1357
|
return {
|
|
1400
|
-
name: await EncryptionService.encrypt(meta.name,
|
|
1358
|
+
name: await EncryptionService.encrypt(meta.name, fileKey, fileIv),
|
|
1401
1359
|
path: await EncryptionService.encrypt(meta.path, fileKey, fileIv),
|
|
1402
1360
|
key: EncryptionService.encryptEcies(meta.key, publicKey),
|
|
1403
1361
|
iv: EncryptionService.encryptEcies(meta.iv, publicKey),
|
|
1404
1362
|
store_hash: await EncryptionService.encrypt(meta.store_hash, fileKey, fileIv),
|
|
1405
|
-
size: await EncryptionService.encrypt(meta.size.toString(),
|
|
1406
|
-
created_at: await EncryptionService.encrypt(meta.created_at,
|
|
1363
|
+
size: await EncryptionService.encrypt(meta.size.toString(), fileKey, fileIv),
|
|
1364
|
+
created_at: await EncryptionService.encrypt(meta.created_at, fileKey, fileIv),
|
|
1407
1365
|
deleted_at: await EncryptionService.encrypt(meta.deleted_at ?? "null", fileKey, fileIv),
|
|
1408
1366
|
shared_keys: meta.shared_keys
|
|
1409
1367
|
};
|
|
1410
1368
|
}
|
|
1411
1369
|
async decryptFileMeta(encryptedMeta, privateKey) {
|
|
1412
|
-
const key = this.core.getEncryptionKey();
|
|
1413
|
-
const iv = EncryptionService.generateIv();
|
|
1414
1370
|
const privKey = privateKey || this.core.getSubAccountPrivateKey();
|
|
1415
1371
|
if (!privKey) {
|
|
1416
1372
|
throw new EncryptionError("Private key not available");
|
|
@@ -1421,13 +1377,13 @@ var FileService = class {
|
|
|
1421
1377
|
const fileIv = Buffer.from(decryptedIv, "hex");
|
|
1422
1378
|
const decryptedDeletedAt = await EncryptionService.decrypt(encryptedMeta.deleted_at, fileKey, fileIv);
|
|
1423
1379
|
return {
|
|
1424
|
-
name: await EncryptionService.decrypt(encryptedMeta.name,
|
|
1380
|
+
name: await EncryptionService.decrypt(encryptedMeta.name, fileKey, fileIv),
|
|
1425
1381
|
path: await EncryptionService.decrypt(encryptedMeta.path, fileKey, fileIv),
|
|
1426
1382
|
key: decryptedKey,
|
|
1427
1383
|
iv: decryptedIv,
|
|
1428
1384
|
store_hash: await EncryptionService.decrypt(encryptedMeta.store_hash, fileKey, fileIv),
|
|
1429
|
-
size: parseInt(await EncryptionService.decrypt(encryptedMeta.size,
|
|
1430
|
-
created_at: await EncryptionService.decrypt(encryptedMeta.created_at,
|
|
1385
|
+
size: Number.parseInt(await EncryptionService.decrypt(encryptedMeta.size, fileKey, fileIv)),
|
|
1386
|
+
created_at: await EncryptionService.decrypt(encryptedMeta.created_at, fileKey, fileIv),
|
|
1431
1387
|
deleted_at: decryptedDeletedAt === "null" ? null : decryptedDeletedAt,
|
|
1432
1388
|
shared_keys: encryptedMeta.shared_keys || {}
|
|
1433
1389
|
};
|
|
@@ -1446,10 +1402,7 @@ var ContactService = class {
|
|
|
1446
1402
|
async setup() {
|
|
1447
1403
|
const aleph = this.core.getAlephService();
|
|
1448
1404
|
try {
|
|
1449
|
-
await aleph.fetchAggregate(
|
|
1450
|
-
AGGREGATE_KEYS.CONTACTS,
|
|
1451
|
-
ContactsAggregateSchema
|
|
1452
|
-
);
|
|
1405
|
+
await aleph.fetchAggregate(AGGREGATE_KEYS.CONTACTS, ContactsAggregateSchema);
|
|
1453
1406
|
} catch {
|
|
1454
1407
|
await aleph.createAggregate(AGGREGATE_KEYS.CONTACTS, { contacts: [] });
|
|
1455
1408
|
}
|
|
@@ -1460,10 +1413,7 @@ var ContactService = class {
|
|
|
1460
1413
|
async listContacts() {
|
|
1461
1414
|
const aleph = this.core.getAlephService();
|
|
1462
1415
|
try {
|
|
1463
|
-
const aggregate = await aleph.fetchAggregate(
|
|
1464
|
-
AGGREGATE_KEYS.CONTACTS,
|
|
1465
|
-
ContactsAggregateSchema
|
|
1466
|
-
);
|
|
1416
|
+
const aggregate = await aleph.fetchAggregate(AGGREGATE_KEYS.CONTACTS, ContactsAggregateSchema);
|
|
1467
1417
|
return aggregate.contacts;
|
|
1468
1418
|
} catch (error) {
|
|
1469
1419
|
throw new ContactError(`Failed to fetch contacts: ${error.message}`);
|
|
@@ -1514,13 +1464,9 @@ var ContactService = class {
|
|
|
1514
1464
|
address,
|
|
1515
1465
|
public_key: publicKey
|
|
1516
1466
|
};
|
|
1517
|
-
await aleph.updateAggregate(
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
async (aggregate) => ({
|
|
1521
|
-
contacts: [...aggregate.contacts, newContact]
|
|
1522
|
-
})
|
|
1523
|
-
);
|
|
1467
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.CONTACTS, ContactsAggregateSchema, async (aggregate) => ({
|
|
1468
|
+
contacts: [...aggregate.contacts, newContact]
|
|
1469
|
+
}));
|
|
1524
1470
|
return newContact;
|
|
1525
1471
|
} catch (error) {
|
|
1526
1472
|
if (error instanceof ContactError) {
|
|
@@ -1537,13 +1483,9 @@ var ContactService = class {
|
|
|
1537
1483
|
const aleph = this.core.getAlephService();
|
|
1538
1484
|
try {
|
|
1539
1485
|
await this.getContact(publicKey);
|
|
1540
|
-
await aleph.updateAggregate(
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
async (aggregate) => ({
|
|
1544
|
-
contacts: aggregate.contacts.filter((c) => c.public_key !== publicKey)
|
|
1545
|
-
})
|
|
1546
|
-
);
|
|
1486
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.CONTACTS, ContactsAggregateSchema, async (aggregate) => ({
|
|
1487
|
+
contacts: aggregate.contacts.filter((c) => c.public_key !== publicKey)
|
|
1488
|
+
}));
|
|
1547
1489
|
} catch (error) {
|
|
1548
1490
|
if (error instanceof ContactError) {
|
|
1549
1491
|
throw error;
|
|
@@ -1564,15 +1506,9 @@ var ContactService = class {
|
|
|
1564
1506
|
...existingContact,
|
|
1565
1507
|
name: newName
|
|
1566
1508
|
};
|
|
1567
|
-
await aleph.updateAggregate(
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
async (aggregate) => ({
|
|
1571
|
-
contacts: aggregate.contacts.map(
|
|
1572
|
-
(c) => c.public_key === publicKey ? updatedContact : c
|
|
1573
|
-
)
|
|
1574
|
-
})
|
|
1575
|
-
);
|
|
1509
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.CONTACTS, ContactsAggregateSchema, async (aggregate) => ({
|
|
1510
|
+
contacts: aggregate.contacts.map((c) => c.public_key === publicKey ? updatedContact : c)
|
|
1511
|
+
}));
|
|
1576
1512
|
return updatedContact;
|
|
1577
1513
|
} catch (error) {
|
|
1578
1514
|
if (error instanceof ContactError) {
|
|
@@ -1589,9 +1525,7 @@ var ContactService = class {
|
|
|
1589
1525
|
try {
|
|
1590
1526
|
await this.getContact(publicKey);
|
|
1591
1527
|
const entries = await this.fileService.fetchFileEntries();
|
|
1592
|
-
const sharedEntries = entries.filter(
|
|
1593
|
-
(entry) => entry.shared_with.includes(publicKey)
|
|
1594
|
-
);
|
|
1528
|
+
const sharedEntries = entries.filter((entry) => entry.shared_with.includes(publicKey));
|
|
1595
1529
|
const files = await this.fileService.fetchFilesMetaFromEntries(sharedEntries);
|
|
1596
1530
|
return files;
|
|
1597
1531
|
} catch (error) {
|
|
@@ -1614,9 +1548,7 @@ var ContactService = class {
|
|
|
1614
1548
|
contact.address
|
|
1615
1549
|
// Important: contact's address, not current user's
|
|
1616
1550
|
);
|
|
1617
|
-
const sharedEntries = contactEntries.files.filter(
|
|
1618
|
-
(entry) => entry.shared_with.includes(currentUserPublicKey)
|
|
1619
|
-
);
|
|
1551
|
+
const sharedEntries = contactEntries.files.filter((entry) => entry.shared_with.includes(currentUserPublicKey));
|
|
1620
1552
|
const files = await this.fileService.fetchFilesMetaFromEntries(
|
|
1621
1553
|
sharedEntries,
|
|
1622
1554
|
contact.address
|
|
@@ -1666,10 +1598,7 @@ var KnowledgeBaseService = class {
|
|
|
1666
1598
|
async setup() {
|
|
1667
1599
|
const aleph = this.core.getAlephService();
|
|
1668
1600
|
try {
|
|
1669
|
-
await aleph.fetchAggregate(
|
|
1670
|
-
AGGREGATE_KEYS.KNOWLEDGE_BASES,
|
|
1671
|
-
KnowledgeBasesAggregateSchema
|
|
1672
|
-
);
|
|
1601
|
+
await aleph.fetchAggregate(AGGREGATE_KEYS.KNOWLEDGE_BASES, KnowledgeBasesAggregateSchema);
|
|
1673
1602
|
} catch {
|
|
1674
1603
|
await aleph.createAggregate(AGGREGATE_KEYS.KNOWLEDGE_BASES, { knowledge_bases: [] });
|
|
1675
1604
|
}
|
|
@@ -1680,10 +1609,7 @@ var KnowledgeBaseService = class {
|
|
|
1680
1609
|
async listKnowledgeBases() {
|
|
1681
1610
|
const aleph = this.core.getAlephService();
|
|
1682
1611
|
try {
|
|
1683
|
-
const aggregate = await aleph.fetchAggregate(
|
|
1684
|
-
AGGREGATE_KEYS.KNOWLEDGE_BASES,
|
|
1685
|
-
KnowledgeBasesAggregateSchema
|
|
1686
|
-
);
|
|
1612
|
+
const aggregate = await aleph.fetchAggregate(AGGREGATE_KEYS.KNOWLEDGE_BASES, KnowledgeBasesAggregateSchema);
|
|
1687
1613
|
return aggregate.knowledge_bases;
|
|
1688
1614
|
} catch (error) {
|
|
1689
1615
|
throw new KnowledgeBaseError(`Failed to fetch knowledge bases: ${error.message}`);
|
|
@@ -1721,13 +1647,9 @@ var KnowledgeBaseService = class {
|
|
|
1721
1647
|
created_at: now,
|
|
1722
1648
|
updated_at: now
|
|
1723
1649
|
};
|
|
1724
|
-
await aleph.updateAggregate(
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
async (aggregate) => ({
|
|
1728
|
-
knowledge_bases: [...aggregate.knowledge_bases, newKb]
|
|
1729
|
-
})
|
|
1730
|
-
);
|
|
1650
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.KNOWLEDGE_BASES, KnowledgeBasesAggregateSchema, async (aggregate) => ({
|
|
1651
|
+
knowledge_bases: [...aggregate.knowledge_bases, newKb]
|
|
1652
|
+
}));
|
|
1731
1653
|
return newKb;
|
|
1732
1654
|
} catch (error) {
|
|
1733
1655
|
if (error instanceof KnowledgeBaseError) {
|
|
@@ -1744,13 +1666,9 @@ var KnowledgeBaseService = class {
|
|
|
1744
1666
|
const aleph = this.core.getAlephService();
|
|
1745
1667
|
try {
|
|
1746
1668
|
await this.getKnowledgeBase(name);
|
|
1747
|
-
await aleph.updateAggregate(
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
async (aggregate) => ({
|
|
1751
|
-
knowledge_bases: aggregate.knowledge_bases.filter((k) => k.name !== name)
|
|
1752
|
-
})
|
|
1753
|
-
);
|
|
1669
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.KNOWLEDGE_BASES, KnowledgeBasesAggregateSchema, async (aggregate) => ({
|
|
1670
|
+
knowledge_bases: aggregate.knowledge_bases.filter((k) => k.name !== name)
|
|
1671
|
+
}));
|
|
1754
1672
|
} catch (error) {
|
|
1755
1673
|
if (error instanceof KnowledgeBaseError) {
|
|
1756
1674
|
throw error;
|
|
@@ -1776,15 +1694,9 @@ var KnowledgeBaseService = class {
|
|
|
1776
1694
|
name: newName,
|
|
1777
1695
|
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
1778
1696
|
};
|
|
1779
|
-
await aleph.updateAggregate(
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
async (aggregate) => ({
|
|
1783
|
-
knowledge_bases: aggregate.knowledge_bases.map(
|
|
1784
|
-
(k) => k.name === oldName ? updatedKb : k
|
|
1785
|
-
)
|
|
1786
|
-
})
|
|
1787
|
-
);
|
|
1697
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.KNOWLEDGE_BASES, KnowledgeBasesAggregateSchema, async (aggregate) => ({
|
|
1698
|
+
knowledge_bases: aggregate.knowledge_bases.map((k) => k.name === oldName ? updatedKb : k)
|
|
1699
|
+
}));
|
|
1788
1700
|
return updatedKb;
|
|
1789
1701
|
} catch (error) {
|
|
1790
1702
|
if (error instanceof KnowledgeBaseError) {
|
|
@@ -1807,15 +1719,9 @@ var KnowledgeBaseService = class {
|
|
|
1807
1719
|
file_paths: filePaths,
|
|
1808
1720
|
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
1809
1721
|
};
|
|
1810
|
-
await aleph.updateAggregate(
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
async (aggregate) => ({
|
|
1814
|
-
knowledge_bases: aggregate.knowledge_bases.map(
|
|
1815
|
-
(k) => k.name === name ? updatedKb : k
|
|
1816
|
-
)
|
|
1817
|
-
})
|
|
1818
|
-
);
|
|
1722
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.KNOWLEDGE_BASES, KnowledgeBasesAggregateSchema, async (aggregate) => ({
|
|
1723
|
+
knowledge_bases: aggregate.knowledge_bases.map((k) => k.name === name ? updatedKb : k)
|
|
1724
|
+
}));
|
|
1819
1725
|
return updatedKb;
|
|
1820
1726
|
} catch (error) {
|
|
1821
1727
|
if (error instanceof KnowledgeBaseError) {
|
|
@@ -1839,15 +1745,9 @@ var KnowledgeBaseService = class {
|
|
|
1839
1745
|
file_paths: updatedFilePaths,
|
|
1840
1746
|
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
1841
1747
|
};
|
|
1842
|
-
await aleph.updateAggregate(
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
async (aggregate) => ({
|
|
1846
|
-
knowledge_bases: aggregate.knowledge_bases.map(
|
|
1847
|
-
(k) => k.name === name ? updatedKb : k
|
|
1848
|
-
)
|
|
1849
|
-
})
|
|
1850
|
-
);
|
|
1748
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.KNOWLEDGE_BASES, KnowledgeBasesAggregateSchema, async (aggregate) => ({
|
|
1749
|
+
knowledge_bases: aggregate.knowledge_bases.map((k) => k.name === name ? updatedKb : k)
|
|
1750
|
+
}));
|
|
1851
1751
|
return updatedKb;
|
|
1852
1752
|
} catch (error) {
|
|
1853
1753
|
if (error instanceof KnowledgeBaseError) {
|
|
@@ -1865,23 +1765,15 @@ var KnowledgeBaseService = class {
|
|
|
1865
1765
|
const aleph = this.core.getAlephService();
|
|
1866
1766
|
try {
|
|
1867
1767
|
const existingKb = await this.getKnowledgeBase(name);
|
|
1868
|
-
const updatedFilePaths = existingKb.file_paths.filter(
|
|
1869
|
-
(path) => !filePaths.includes(path)
|
|
1870
|
-
);
|
|
1768
|
+
const updatedFilePaths = existingKb.file_paths.filter((path) => !filePaths.includes(path));
|
|
1871
1769
|
const updatedKb = {
|
|
1872
1770
|
...existingKb,
|
|
1873
1771
|
file_paths: updatedFilePaths,
|
|
1874
1772
|
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
1875
1773
|
};
|
|
1876
|
-
await aleph.updateAggregate(
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
async (aggregate) => ({
|
|
1880
|
-
knowledge_bases: aggregate.knowledge_bases.map(
|
|
1881
|
-
(k) => k.name === name ? updatedKb : k
|
|
1882
|
-
)
|
|
1883
|
-
})
|
|
1884
|
-
);
|
|
1774
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.KNOWLEDGE_BASES, KnowledgeBasesAggregateSchema, async (aggregate) => ({
|
|
1775
|
+
knowledge_bases: aggregate.knowledge_bases.map((k) => k.name === name ? updatedKb : k)
|
|
1776
|
+
}));
|
|
1885
1777
|
return updatedKb;
|
|
1886
1778
|
} catch (error) {
|
|
1887
1779
|
if (error instanceof KnowledgeBaseError) {
|
|
@@ -2020,11 +1912,7 @@ var BedrockClient = class _BedrockClient {
|
|
|
2020
1912
|
* WARNING: This will delete all user data from Aleph
|
|
2021
1913
|
*/
|
|
2022
1914
|
async resetAllData() {
|
|
2023
|
-
await Promise.all([
|
|
2024
|
-
this.resetFiles(),
|
|
2025
|
-
this.resetContacts(),
|
|
2026
|
-
this.resetKnowledgeBases()
|
|
2027
|
-
]);
|
|
1915
|
+
await Promise.all([this.resetFiles(), this.resetContacts(), this.resetKnowledgeBases()]);
|
|
2028
1916
|
}
|
|
2029
1917
|
/**
|
|
2030
1918
|
* Reset all files
|
|
@@ -2057,11 +1945,7 @@ var BedrockClient = class _BedrockClient {
|
|
|
2057
1945
|
* Setup all services (create aggregates if they don't exist)
|
|
2058
1946
|
*/
|
|
2059
1947
|
async setup() {
|
|
2060
|
-
await Promise.all([
|
|
2061
|
-
this.files.setup(),
|
|
2062
|
-
this.contacts.setup(),
|
|
2063
|
-
this.knowledgeBases.setup()
|
|
2064
|
-
]);
|
|
1948
|
+
await Promise.all([this.files.setup(), this.contacts.setup(), this.knowledgeBases.setup()]);
|
|
2065
1949
|
}
|
|
2066
1950
|
};
|
|
2067
1951
|
|