@brightchain/brightchain-lib 0.29.26 → 0.30.1

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.
Files changed (290) hide show
  1. package/package.json +10 -4
  2. package/src/index.d.ts +1 -1
  3. package/src/index.d.ts.map +1 -1
  4. package/src/index.js +2 -1
  5. package/src/index.js.map +1 -1
  6. package/src/lib/constants.d.ts +3 -26
  7. package/src/lib/constants.d.ts.map +1 -1
  8. package/src/lib/constants.js +2 -1
  9. package/src/lib/constants.js.map +1 -1
  10. package/src/lib/db/collection.d.ts +1 -1
  11. package/src/lib/db/collection.d.ts.map +1 -1
  12. package/src/lib/db/collection.js +2 -1
  13. package/src/lib/db/collection.js.map +1 -1
  14. package/src/lib/enumeration-translations/index.d.ts +2 -0
  15. package/src/lib/enumeration-translations/index.d.ts.map +1 -1
  16. package/src/lib/enumeration-translations/index.js +1 -0
  17. package/src/lib/enumeration-translations/index.js.map +1 -1
  18. package/src/lib/enumeration-translations/memberStatusType.d.ts +5 -0
  19. package/src/lib/enumeration-translations/memberStatusType.d.ts.map +1 -0
  20. package/src/lib/enumeration-translations/memberStatusType.js +58 -0
  21. package/src/lib/enumeration-translations/memberStatusType.js.map +1 -0
  22. package/src/lib/enumerations/brightChainStrings.d.ts +61 -0
  23. package/src/lib/enumerations/brightChainStrings.d.ts.map +1 -1
  24. package/src/lib/enumerations/brightChainStrings.js +64 -0
  25. package/src/lib/enumerations/brightChainStrings.js.map +1 -1
  26. package/src/lib/enumerations/brightchainFeatures.d.ts +1 -0
  27. package/src/lib/enumerations/brightchainFeatures.d.ts.map +1 -1
  28. package/src/lib/enumerations/brightchainFeatures.js +1 -0
  29. package/src/lib/enumerations/brightchainFeatures.js.map +1 -1
  30. package/src/lib/enumerations/communication.d.ts +7 -1
  31. package/src/lib/enumerations/communication.d.ts.map +1 -1
  32. package/src/lib/enumerations/communication.js +6 -0
  33. package/src/lib/enumerations/communication.js.map +1 -1
  34. package/src/lib/enumerations/friendRequestStatus.d.ts +7 -0
  35. package/src/lib/enumerations/friendRequestStatus.d.ts.map +1 -0
  36. package/src/lib/enumerations/friendRequestStatus.js +11 -0
  37. package/src/lib/enumerations/friendRequestStatus.js.map +1 -0
  38. package/src/lib/enumerations/friendsErrorCode.d.ts +10 -0
  39. package/src/lib/enumerations/friendsErrorCode.d.ts.map +1 -0
  40. package/src/lib/enumerations/friendsErrorCode.js +14 -0
  41. package/src/lib/enumerations/friendsErrorCode.js.map +1 -0
  42. package/src/lib/enumerations/friendshipStatus.d.ts +7 -0
  43. package/src/lib/enumerations/friendshipStatus.d.ts.map +1 -0
  44. package/src/lib/enumerations/friendshipStatus.js +11 -0
  45. package/src/lib/enumerations/friendshipStatus.js.map +1 -0
  46. package/src/lib/enumerations/index.d.ts +3 -0
  47. package/src/lib/enumerations/index.d.ts.map +1 -1
  48. package/src/lib/enumerations/index.js +4 -0
  49. package/src/lib/enumerations/index.js.map +1 -1
  50. package/src/lib/enumerations/messaging/emailErrorType.d.ts +15 -2
  51. package/src/lib/enumerations/messaging/emailErrorType.d.ts.map +1 -1
  52. package/src/lib/enumerations/messaging/emailErrorType.js +17 -1
  53. package/src/lib/enumerations/messaging/emailErrorType.js.map +1 -1
  54. package/src/lib/enumerations/messaging/messageEncryptionScheme.d.ts +4 -2
  55. package/src/lib/enumerations/messaging/messageEncryptionScheme.d.ts.map +1 -1
  56. package/src/lib/enumerations/messaging/messageEncryptionScheme.js +3 -1
  57. package/src/lib/enumerations/messaging/messageEncryptionScheme.js.map +1 -1
  58. package/src/lib/errors/encryptionErrors.d.ts +71 -0
  59. package/src/lib/errors/encryptionErrors.d.ts.map +1 -0
  60. package/src/lib/errors/encryptionErrors.js +112 -0
  61. package/src/lib/errors/encryptionErrors.js.map +1 -0
  62. package/src/lib/errors/friendsServiceError.d.ts +20 -0
  63. package/src/lib/errors/friendsServiceError.d.ts.map +1 -0
  64. package/src/lib/errors/friendsServiceError.js +48 -0
  65. package/src/lib/errors/friendsServiceError.js.map +1 -0
  66. package/src/lib/errors/index.d.ts +15 -0
  67. package/src/lib/errors/index.d.ts.map +1 -1
  68. package/src/lib/errors/index.js +21 -0
  69. package/src/lib/errors/index.js.map +1 -1
  70. package/src/lib/i18n/i18n-setup.d.ts +2 -2
  71. package/src/lib/i18n/i18n-setup.d.ts.map +1 -1
  72. package/src/lib/i18n/strings/englishUK.d.ts +2 -2
  73. package/src/lib/i18n/strings/englishUK.d.ts.map +1 -1
  74. package/src/lib/i18n/strings/englishUK.js.map +1 -1
  75. package/src/lib/i18n/strings/englishUs.d.ts +2 -2
  76. package/src/lib/i18n/strings/englishUs.d.ts.map +1 -1
  77. package/src/lib/i18n/strings/englishUs.js +65 -1
  78. package/src/lib/i18n/strings/englishUs.js.map +1 -1
  79. package/src/lib/i18n/strings/french.d.ts +2 -2
  80. package/src/lib/i18n/strings/french.d.ts.map +1 -1
  81. package/src/lib/i18n/strings/french.js +78 -13
  82. package/src/lib/i18n/strings/french.js.map +1 -1
  83. package/src/lib/i18n/strings/german.d.ts +2 -2
  84. package/src/lib/i18n/strings/german.d.ts.map +1 -1
  85. package/src/lib/i18n/strings/german.js +77 -12
  86. package/src/lib/i18n/strings/german.js.map +1 -1
  87. package/src/lib/i18n/strings/japanese.d.ts +2 -2
  88. package/src/lib/i18n/strings/japanese.d.ts.map +1 -1
  89. package/src/lib/i18n/strings/japanese.js +77 -12
  90. package/src/lib/i18n/strings/japanese.js.map +1 -1
  91. package/src/lib/i18n/strings/mandarin.d.ts +2 -2
  92. package/src/lib/i18n/strings/mandarin.d.ts.map +1 -1
  93. package/src/lib/i18n/strings/mandarin.js +77 -12
  94. package/src/lib/i18n/strings/mandarin.js.map +1 -1
  95. package/src/lib/i18n/strings/spanish.d.ts +2 -2
  96. package/src/lib/i18n/strings/spanish.d.ts.map +1 -1
  97. package/src/lib/i18n/strings/spanish.js +77 -12
  98. package/src/lib/i18n/strings/spanish.js.map +1 -1
  99. package/src/lib/i18n/strings/ukrainian.d.ts +2 -2
  100. package/src/lib/i18n/strings/ukrainian.d.ts.map +1 -1
  101. package/src/lib/i18n/strings/ukrainian.js +77 -12
  102. package/src/lib/i18n/strings/ukrainian.js.map +1 -1
  103. package/src/lib/index.d.ts +31 -0
  104. package/src/lib/index.d.ts.map +1 -1
  105. package/src/lib/index.js +29 -1
  106. package/src/lib/index.js.map +1 -1
  107. package/src/lib/interfaces/appSubsystemPlugin.d.ts +69 -0
  108. package/src/lib/interfaces/appSubsystemPlugin.d.ts.map +1 -0
  109. package/src/lib/interfaces/appSubsystemPlugin.js +3 -0
  110. package/src/lib/interfaces/appSubsystemPlugin.js.map +1 -0
  111. package/src/lib/interfaces/auth/writeProof.d.ts +2 -0
  112. package/src/lib/interfaces/auth/writeProof.d.ts.map +1 -1
  113. package/src/lib/interfaces/auth/writeProofUtils.d.ts +1 -1
  114. package/src/lib/interfaces/auth/writeProofUtils.d.ts.map +1 -1
  115. package/src/lib/interfaces/auth/writeProofUtils.js +2 -2
  116. package/src/lib/interfaces/auth/writeProofUtils.js.map +1 -1
  117. package/src/lib/interfaces/availability/gossipService.d.ts +99 -1
  118. package/src/lib/interfaces/availability/gossipService.d.ts.map +1 -1
  119. package/src/lib/interfaces/availability/gossipService.js +4 -0
  120. package/src/lib/interfaces/availability/gossipService.js.map +1 -1
  121. package/src/lib/interfaces/communication/blockContentStore.d.ts +57 -0
  122. package/src/lib/interfaces/communication/blockContentStore.d.ts.map +1 -0
  123. package/src/lib/interfaces/communication/blockContentStore.js +21 -0
  124. package/src/lib/interfaces/communication/blockContentStore.js.map +1 -0
  125. package/src/lib/interfaces/communication/chatStorageProvider.d.ts +77 -0
  126. package/src/lib/interfaces/communication/chatStorageProvider.d.ts.map +1 -0
  127. package/src/lib/interfaces/communication/chatStorageProvider.js +25 -0
  128. package/src/lib/interfaces/communication/chatStorageProvider.js.map +1 -0
  129. package/src/lib/interfaces/communication/index.d.ts +4 -0
  130. package/src/lib/interfaces/communication/index.d.ts.map +1 -0
  131. package/src/lib/interfaces/communication/index.js +3 -0
  132. package/src/lib/interfaces/communication/index.js.map +1 -0
  133. package/src/lib/interfaces/communication/server.d.ts +57 -0
  134. package/src/lib/interfaces/communication/server.d.ts.map +1 -0
  135. package/src/lib/interfaces/communication/server.js +14 -0
  136. package/src/lib/interfaces/communication/server.js.map +1 -0
  137. package/src/lib/interfaces/communication.d.ts +62 -5
  138. package/src/lib/interfaces/communication.d.ts.map +1 -1
  139. package/src/lib/interfaces/communication.js +8 -0
  140. package/src/lib/interfaces/communication.js.map +1 -1
  141. package/src/lib/interfaces/communicationEvents.d.ts +59 -1
  142. package/src/lib/interfaces/communicationEvents.d.ts.map +1 -1
  143. package/src/lib/interfaces/constants.d.ts +2 -0
  144. package/src/lib/interfaces/constants.d.ts.map +1 -1
  145. package/src/lib/interfaces/events/communicationEventEmitter.d.ts +9 -0
  146. package/src/lib/interfaces/events/communicationEventEmitter.d.ts.map +1 -1
  147. package/src/lib/interfaces/events/communicationEventEmitter.js +9 -0
  148. package/src/lib/interfaces/events/communicationEventEmitter.js.map +1 -1
  149. package/src/lib/interfaces/friends/baseFriendRequest.d.ts +13 -0
  150. package/src/lib/interfaces/friends/baseFriendRequest.d.ts.map +1 -0
  151. package/src/lib/interfaces/friends/baseFriendRequest.js +3 -0
  152. package/src/lib/interfaces/friends/baseFriendRequest.js.map +1 -0
  153. package/src/lib/interfaces/friends/baseFriendship.d.ts +11 -0
  154. package/src/lib/interfaces/friends/baseFriendship.d.ts.map +1 -0
  155. package/src/lib/interfaces/friends/baseFriendship.js +3 -0
  156. package/src/lib/interfaces/friends/baseFriendship.js.map +1 -0
  157. package/src/lib/interfaces/friends/friendsService.d.ts +32 -0
  158. package/src/lib/interfaces/friends/friendsService.d.ts.map +1 -0
  159. package/src/lib/interfaces/friends/friendsService.js +3 -0
  160. package/src/lib/interfaces/friends/friendsService.js.map +1 -0
  161. package/src/lib/interfaces/friends/friendsSuggestionProvider.d.ts +17 -0
  162. package/src/lib/interfaces/friends/friendsSuggestionProvider.d.ts.map +1 -0
  163. package/src/lib/interfaces/friends/friendsSuggestionProvider.js +3 -0
  164. package/src/lib/interfaces/friends/friendsSuggestionProvider.js.map +1 -0
  165. package/src/lib/interfaces/friends/index.d.ts +6 -0
  166. package/src/lib/interfaces/friends/index.d.ts.map +1 -0
  167. package/src/lib/interfaces/friends/index.js +3 -0
  168. package/src/lib/interfaces/friends/index.js.map +1 -0
  169. package/src/lib/interfaces/friends/pagination.d.ts +11 -0
  170. package/src/lib/interfaces/friends/pagination.d.ts.map +1 -0
  171. package/src/lib/interfaces/friends/pagination.js +3 -0
  172. package/src/lib/interfaces/friends/pagination.js.map +1 -0
  173. package/src/lib/interfaces/index.d.ts +6 -1
  174. package/src/lib/interfaces/index.d.ts.map +1 -1
  175. package/src/lib/interfaces/index.js +3 -0
  176. package/src/lib/interfaces/index.js.map +1 -1
  177. package/src/lib/interfaces/messaging/gpgKey.d.ts +93 -0
  178. package/src/lib/interfaces/messaging/gpgKey.d.ts.map +1 -0
  179. package/src/lib/interfaces/messaging/gpgKey.js +12 -0
  180. package/src/lib/interfaces/messaging/gpgKey.js.map +1 -0
  181. package/src/lib/interfaces/messaging/index.d.ts +4 -0
  182. package/src/lib/interfaces/messaging/index.d.ts.map +1 -1
  183. package/src/lib/interfaces/messaging/index.js +4 -0
  184. package/src/lib/interfaces/messaging/index.js.map +1 -1
  185. package/src/lib/interfaces/messaging/keyStore.d.ts +100 -0
  186. package/src/lib/interfaces/messaging/keyStore.d.ts.map +1 -0
  187. package/src/lib/interfaces/messaging/keyStore.js +13 -0
  188. package/src/lib/interfaces/messaging/keyStore.js.map +1 -0
  189. package/src/lib/interfaces/messaging/recipientKeyResolver.d.ts +92 -0
  190. package/src/lib/interfaces/messaging/recipientKeyResolver.d.ts.map +1 -0
  191. package/src/lib/interfaces/messaging/recipientKeyResolver.js +13 -0
  192. package/src/lib/interfaces/messaging/recipientKeyResolver.js.map +1 -0
  193. package/src/lib/interfaces/messaging/smimeCertificate.d.ts +99 -0
  194. package/src/lib/interfaces/messaging/smimeCertificate.d.ts.map +1 -0
  195. package/src/lib/interfaces/messaging/smimeCertificate.js +12 -0
  196. package/src/lib/interfaces/messaging/smimeCertificate.js.map +1 -0
  197. package/src/lib/interfaces/responses/adminDashboardResponse.d.ts +8 -0
  198. package/src/lib/interfaces/responses/adminDashboardResponse.d.ts.map +1 -1
  199. package/src/lib/interfaces/responses/communicationResponses.d.ts +25 -0
  200. package/src/lib/interfaces/responses/communicationResponses.d.ts.map +1 -1
  201. package/src/lib/interfaces/storage/documentTypes.d.ts +4 -0
  202. package/src/lib/interfaces/storage/documentTypes.d.ts.map +1 -1
  203. package/src/lib/services/blockService.d.ts +11 -3
  204. package/src/lib/services/blockService.d.ts.map +1 -1
  205. package/src/lib/services/blockService.js +22 -2
  206. package/src/lib/services/blockService.js.map +1 -1
  207. package/src/lib/services/communication/__tests__/mockChatStorageProvider.d.ts +108 -0
  208. package/src/lib/services/communication/__tests__/mockChatStorageProvider.d.ts.map +1 -0
  209. package/src/lib/services/communication/__tests__/mockChatStorageProvider.js +284 -0
  210. package/src/lib/services/communication/__tests__/mockChatStorageProvider.js.map +1 -0
  211. package/src/lib/services/communication/attachmentUtils.d.ts +20 -0
  212. package/src/lib/services/communication/attachmentUtils.d.ts.map +1 -0
  213. package/src/lib/services/communication/attachmentUtils.js +43 -0
  214. package/src/lib/services/communication/attachmentUtils.js.map +1 -0
  215. package/src/lib/services/communication/channelService.d.ts +143 -14
  216. package/src/lib/services/communication/channelService.d.ts.map +1 -1
  217. package/src/lib/services/communication/channelService.js +562 -41
  218. package/src/lib/services/communication/channelService.js.map +1 -1
  219. package/src/lib/services/communication/conversationService.d.ts +91 -4
  220. package/src/lib/services/communication/conversationService.d.ts.map +1 -1
  221. package/src/lib/services/communication/conversationService.js +269 -7
  222. package/src/lib/services/communication/conversationService.js.map +1 -1
  223. package/src/lib/services/communication/eciesKeyEncryptionHandler.d.ts +36 -0
  224. package/src/lib/services/communication/eciesKeyEncryptionHandler.d.ts.map +1 -0
  225. package/src/lib/services/communication/eciesKeyEncryptionHandler.js +30 -0
  226. package/src/lib/services/communication/eciesKeyEncryptionHandler.js.map +1 -0
  227. package/src/lib/services/communication/groupService.d.ts +99 -21
  228. package/src/lib/services/communication/groupService.d.ts.map +1 -1
  229. package/src/lib/services/communication/groupService.js +387 -41
  230. package/src/lib/services/communication/groupService.js.map +1 -1
  231. package/src/lib/services/communication/index.d.ts +6 -1
  232. package/src/lib/services/communication/index.d.ts.map +1 -1
  233. package/src/lib/services/communication/index.js +20 -1
  234. package/src/lib/services/communication/index.js.map +1 -1
  235. package/src/lib/services/communication/keyEpochManager.d.ts +41 -0
  236. package/src/lib/services/communication/keyEpochManager.d.ts.map +1 -0
  237. package/src/lib/services/communication/keyEpochManager.js +59 -0
  238. package/src/lib/services/communication/keyEpochManager.js.map +1 -0
  239. package/src/lib/services/communication/rehydrationHelpers.d.ts +32 -0
  240. package/src/lib/services/communication/rehydrationHelpers.d.ts.map +1 -0
  241. package/src/lib/services/communication/rehydrationHelpers.js +58 -0
  242. package/src/lib/services/communication/rehydrationHelpers.js.map +1 -0
  243. package/src/lib/services/communication/serverService.d.ts +193 -0
  244. package/src/lib/services/communication/serverService.d.ts.map +1 -0
  245. package/src/lib/services/communication/serverService.js +495 -0
  246. package/src/lib/services/communication/serverService.js.map +1 -0
  247. package/src/lib/services/copyOnWrite.service.d.ts +110 -0
  248. package/src/lib/services/copyOnWrite.service.d.ts.map +1 -0
  249. package/src/lib/services/copyOnWrite.service.js +256 -0
  250. package/src/lib/services/copyOnWrite.service.js.map +1 -0
  251. package/src/lib/services/index.d.ts +1 -0
  252. package/src/lib/services/index.d.ts.map +1 -1
  253. package/src/lib/services/index.js +1 -0
  254. package/src/lib/services/index.js.map +1 -1
  255. package/src/lib/services/memberStore.d.ts +17 -1
  256. package/src/lib/services/memberStore.d.ts.map +1 -1
  257. package/src/lib/services/memberStore.js +98 -17
  258. package/src/lib/services/memberStore.js.map +1 -1
  259. package/src/lib/services/messaging/emailEncryptionService.d.ts +162 -0
  260. package/src/lib/services/messaging/emailEncryptionService.d.ts.map +1 -1
  261. package/src/lib/services/messaging/emailEncryptionService.js +293 -0
  262. package/src/lib/services/messaging/emailEncryptionService.js.map +1 -1
  263. package/src/lib/services/messaging/emailMessageService.d.ts +64 -0
  264. package/src/lib/services/messaging/emailMessageService.d.ts.map +1 -1
  265. package/src/lib/services/messaging/emailMessageService.js +142 -13
  266. package/src/lib/services/messaging/emailMessageService.js.map +1 -1
  267. package/src/lib/services/messaging/gpgKeyManager.d.ts +130 -0
  268. package/src/lib/services/messaging/gpgKeyManager.d.ts.map +1 -0
  269. package/src/lib/services/messaging/gpgKeyManager.js +381 -0
  270. package/src/lib/services/messaging/gpgKeyManager.js.map +1 -0
  271. package/src/lib/services/messaging/index.d.ts +3 -0
  272. package/src/lib/services/messaging/index.d.ts.map +1 -1
  273. package/src/lib/services/messaging/index.js +3 -0
  274. package/src/lib/services/messaging/index.js.map +1 -1
  275. package/src/lib/services/messaging/recipientKeyResolver.d.ts +47 -0
  276. package/src/lib/services/messaging/recipientKeyResolver.d.ts.map +1 -0
  277. package/src/lib/services/messaging/recipientKeyResolver.js +132 -0
  278. package/src/lib/services/messaging/recipientKeyResolver.js.map +1 -0
  279. package/src/lib/services/messaging/smimeCertificateManager.d.ts +207 -0
  280. package/src/lib/services/messaging/smimeCertificateManager.d.ts.map +1 -0
  281. package/src/lib/services/messaging/smimeCertificateManager.js +696 -0
  282. package/src/lib/services/messaging/smimeCertificateManager.js.map +1 -0
  283. package/src/lib/utils/index.d.ts +6 -0
  284. package/src/lib/utils/index.d.ts.map +1 -1
  285. package/src/lib/utils/index.js +9 -0
  286. package/src/lib/utils/index.js.map +1 -1
  287. package/src/lib/utils/sortPair.d.ts +6 -0
  288. package/src/lib/utils/sortPair.d.ts.map +1 -0
  289. package/src/lib/utils/sortPair.js +11 -0
  290. package/src/lib/utils/sortPair.js.map +1 -0
@@ -0,0 +1,256 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CopyOnWriteService = void 0;
4
+ const cbl_1 = require("../blocks/cbl");
5
+ const extendedCbl_1 = require("../blocks/extendedCbl");
6
+ const memoryTuple_1 = require("../blocks/memoryTuple");
7
+ const rawData_1 = require("../blocks/rawData");
8
+ const constants_1 = require("../constants");
9
+ const blockEncryptionType_1 = require("../enumerations/blockEncryptionType");
10
+ const globalServiceProvider_1 = require("./globalServiceProvider");
11
+ /**
12
+ * CopyOnWriteService provides efficient versioning for CBL-based data.
13
+ *
14
+ * When data changes, instead of re-creating all blocks from scratch, this service:
15
+ * 1. Reads the new data in block-sized chunks
16
+ * 2. Compares each chunk against the corresponding original block data
17
+ * (recovered by XORing the tuple's constituent blocks)
18
+ * 3. Reuses the original tuple addresses for unchanged blocks
19
+ * 4. Creates new tuples only for blocks whose data has changed
20
+ * 5. Produces a new CBL that references both old (reused) and new block addresses
21
+ *
22
+ * This is analogous to copy-on-write in operating systems: we share unchanged
23
+ * pages and only allocate new storage for modified pages.
24
+ *
25
+ * The tuple size (typically 3) means each "logical block" in the CBL is
26
+ * represented by `tupleSize` addresses. When we say "reuse a tuple," we
27
+ * mean we copy all `tupleSize` addresses from the old CBL into the new one.
28
+ *
29
+ * @typeParam TID - The platform ID type (defaults to Uint8Array)
30
+ */
31
+ class CopyOnWriteService {
32
+ checksumService;
33
+ cblService;
34
+ constructor(checksumService, cblService) {
35
+ this.checksumService =
36
+ checksumService ?? (0, globalServiceProvider_1.getGlobalServiceProvider)().checksumService;
37
+ this.cblService =
38
+ cblService ?? (0, globalServiceProvider_1.getGlobalServiceProvider)().cblService;
39
+ }
40
+ /**
41
+ * Create a new version of a CBL by applying changes from new data,
42
+ * reusing unchanged blocks from the original CBL.
43
+ *
44
+ * @param originalCbl - The existing CBL to base the new version on
45
+ * @param newData - The complete new data as a Uint8Array
46
+ * @param creator - The member creating the new version
47
+ * @param blockSize - The block size to use (must match the original CBL's block size)
48
+ * @param getBlockData - Function to retrieve raw block data by checksum from the store
49
+ * @param whitenedBlockSource - Function to get whitened blocks for new tuples
50
+ * @param randomBlockSource - Function to get random blocks for new tuples
51
+ * @param persistTuple - Function to persist newly created tuples
52
+ * @param options - Optional settings for extended CBL creation
53
+ * @returns The new CBL and statistics about reuse
54
+ */
55
+ async newVersion(originalCbl, newData, creator, blockSize, getBlockData, whitenedBlockSource, randomBlockSource, persistTuple, options) {
56
+ const now = new Date();
57
+ const tupleSize = originalCbl.tupleSize;
58
+ const originalAddresses = originalCbl.addresses;
59
+ const blockSizeBytes = blockSize;
60
+ // Calculate how many logical blocks (tuples) the original CBL has
61
+ const originalTupleCount = Math.floor(originalAddresses.length / tupleSize);
62
+ // Calculate how many logical blocks the new data needs
63
+ const newTotalBlocks = Math.ceil(newData.length / blockSizeBytes);
64
+ // We'll build the new address list tuple by tuple
65
+ const newAddressList = [];
66
+ let reusedTupleCount = 0;
67
+ let newTupleCount = 0;
68
+ for (let tupleIndex = 0; tupleIndex < newTotalBlocks; tupleIndex++) {
69
+ // Extract the new data chunk for this block position
70
+ const chunkStart = tupleIndex * blockSizeBytes;
71
+ const chunkEnd = Math.min(chunkStart + blockSizeBytes, newData.length);
72
+ let newChunk = newData.slice(chunkStart, chunkEnd);
73
+ // Pad the last chunk if needed
74
+ if (newChunk.length < blockSizeBytes) {
75
+ const padded = new Uint8Array(blockSizeBytes);
76
+ padded.set(newChunk);
77
+ // Remaining bytes are zero-filled (deterministic padding for comparison)
78
+ newChunk = padded;
79
+ }
80
+ // Check if we can reuse the original tuple at this position
81
+ let reused = false;
82
+ if (tupleIndex < originalTupleCount) {
83
+ const originalTupleStart = tupleIndex * tupleSize;
84
+ const originalTupleAddresses = originalAddresses.slice(originalTupleStart, originalTupleStart + tupleSize);
85
+ // Recover the original plaintext by XORing all blocks in the tuple
86
+ try {
87
+ const originalPlaintext = await this.recoverTupleData(originalTupleAddresses, getBlockData);
88
+ // Compare: if the data is identical, reuse the old addresses
89
+ if (this.blocksEqual(newChunk, originalPlaintext)) {
90
+ for (const addr of originalTupleAddresses) {
91
+ newAddressList.push(addr);
92
+ }
93
+ reusedTupleCount++;
94
+ reused = true;
95
+ }
96
+ }
97
+ catch {
98
+ // If we can't recover the original data (block missing, etc.),
99
+ // fall through to create a new tuple
100
+ }
101
+ }
102
+ if (!reused) {
103
+ // Create a new tuple for this changed block
104
+ const tupleAddresses = await this.createNewTuple(newChunk, blockSize, creator, whitenedBlockSource, randomBlockSource, persistTuple, now);
105
+ for (const addr of tupleAddresses) {
106
+ newAddressList.push(addr);
107
+ }
108
+ newTupleCount++;
109
+ }
110
+ }
111
+ // Build the address list buffer
112
+ const addressListBuffer = this.addressesToBuffer(newAddressList);
113
+ // Create the new CBL header
114
+ const isExtended = options?.createExtendedCbl && options.fileName;
115
+ const extendedInfo = isExtended
116
+ ? {
117
+ fileName: options.fileName,
118
+ mimeType: options?.mimeType ?? 'application/octet-stream',
119
+ }
120
+ : undefined;
121
+ const { headerData } = this.cblService.makeCblHeader(creator, now, newAddressList.length, newData.length, addressListBuffer, blockSize, blockEncryptionType_1.BlockEncryptionType.None, extendedInfo, tupleSize);
122
+ // Assemble the full CBL data: header + address list
123
+ const cblData = new Uint8Array(headerData.length + addressListBuffer.length);
124
+ cblData.set(headerData, 0);
125
+ cblData.set(addressListBuffer, headerData.length);
126
+ // Create the CBL block
127
+ const cbl = isExtended
128
+ ? new extendedCbl_1.ExtendedCBL(cblData, creator, blockSize)
129
+ : new cbl_1.ConstituentBlockListBlock(cblData, creator, blockSize);
130
+ return {
131
+ cbl,
132
+ reusedTupleCount,
133
+ newTupleCount,
134
+ totalTupleCount: reusedTupleCount + newTupleCount,
135
+ };
136
+ }
137
+ /**
138
+ * Recover the original plaintext data from a tuple by XORing all
139
+ * constituent blocks together.
140
+ *
141
+ * In the OFF system, a tuple of `tupleSize` blocks is stored such that
142
+ * XORing all blocks together recovers the original data:
143
+ * plaintext = block[0] XOR block[1] XOR ... XOR block[tupleSize-1]
144
+ *
145
+ * @param tupleAddresses - The checksums of the blocks in the tuple
146
+ * @param getBlockData - Function to retrieve raw block data by checksum
147
+ * @returns The recovered plaintext data
148
+ */
149
+ async recoverTupleData(tupleAddresses, getBlockData) {
150
+ if (tupleAddresses.length === 0) {
151
+ throw new Error('Cannot recover data from empty tuple');
152
+ }
153
+ // Load all blocks in the tuple
154
+ const blockDataArrays = await Promise.all(tupleAddresses.map((addr) => getBlockData(addr)));
155
+ // XOR all blocks together to recover the plaintext
156
+ const result = new Uint8Array(blockDataArrays[0].length);
157
+ result.set(blockDataArrays[0]);
158
+ for (let i = 1; i < blockDataArrays.length; i++) {
159
+ const current = blockDataArrays[i];
160
+ for (let j = 0; j < result.length; j++) {
161
+ result[j] ^= current[j];
162
+ }
163
+ }
164
+ return result;
165
+ }
166
+ /**
167
+ * Compare two block-sized byte arrays for equality.
168
+ * Uses constant-length comparison (always checks all bytes).
169
+ */
170
+ blocksEqual(a, b) {
171
+ if (a.length !== b.length) {
172
+ return false;
173
+ }
174
+ let equal = true;
175
+ for (let i = 0; i < a.length; i++) {
176
+ if (a[i] !== b[i]) {
177
+ equal = false;
178
+ // Don't break early — we compare all bytes for consistency,
179
+ // though this isn't a security-sensitive comparison
180
+ break;
181
+ }
182
+ }
183
+ return equal;
184
+ }
185
+ /**
186
+ * Create a new tuple for a data block by XORing it with whitener/random blocks,
187
+ * then persist the tuple.
188
+ *
189
+ * @returns The checksums of all blocks in the new tuple (in order)
190
+ */
191
+ async createNewTuple(data, blockSize, creator, whitenedBlockSource, randomBlockSource, persistTuple, dateCreated) {
192
+ // Gather whitener/random blocks for the tuple (tupleSize - 1 blocks)
193
+ const companions = [];
194
+ for (let i = 0; i < constants_1.TUPLE.SIZE - 1; i++) {
195
+ const whitened = whitenedBlockSource();
196
+ if (whitened) {
197
+ companions.push(whitened);
198
+ }
199
+ else {
200
+ companions.push(randomBlockSource());
201
+ }
202
+ }
203
+ // XOR the data with all companion blocks to produce the "prime" block
204
+ let xored = new Uint8Array(data);
205
+ for (const companion of companions) {
206
+ const companionData = companion.data instanceof Uint8Array
207
+ ? companion.data
208
+ : await this.streamToUint8Array(companion.data);
209
+ for (let j = 0; j < xored.length; j++) {
210
+ xored[j] ^= companionData[j];
211
+ }
212
+ }
213
+ // Create the prime (XORed result) block
214
+ const primeChecksum = this.checksumService.calculateChecksum(xored);
215
+ const primeBlock = new rawData_1.RawDataBlock(blockSize, xored, dateCreated, primeChecksum);
216
+ // Build the tuple: [prime, ...companions]
217
+ const allBlocks = [primeBlock, ...companions];
218
+ const tuple = new memoryTuple_1.InMemoryBlockTuple(allBlocks);
219
+ await persistTuple(tuple);
220
+ // Return all block checksums in tuple order
221
+ return allBlocks.map((block) => block.idChecksum);
222
+ }
223
+ /**
224
+ * Convert an array of Checksums to a concatenated Uint8Array buffer.
225
+ */
226
+ addressesToBuffer(addresses) {
227
+ const buffers = addresses.map((addr) => addr.toUint8Array());
228
+ const totalLength = buffers.reduce((sum, buf) => sum + buf.length, 0);
229
+ const result = new Uint8Array(totalLength);
230
+ let offset = 0;
231
+ for (const buf of buffers) {
232
+ result.set(buf, offset);
233
+ offset += buf.length;
234
+ }
235
+ return result;
236
+ }
237
+ /**
238
+ * Helper to convert a Readable stream to Uint8Array.
239
+ */
240
+ async streamToUint8Array(readable) {
241
+ const chunks = [];
242
+ for await (const chunk of readable) {
243
+ chunks.push(new Uint8Array(chunk));
244
+ }
245
+ const totalLength = chunks.reduce((sum, c) => sum + c.length, 0);
246
+ const result = new Uint8Array(totalLength);
247
+ let offset = 0;
248
+ for (const chunk of chunks) {
249
+ result.set(chunk, offset);
250
+ offset += chunk.length;
251
+ }
252
+ return result;
253
+ }
254
+ }
255
+ exports.CopyOnWriteService = CopyOnWriteService;
256
+ //# sourceMappingURL=copyOnWrite.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"copyOnWrite.service.js","sourceRoot":"","sources":["../../../../../brightchain-lib/src/lib/services/copyOnWrite.service.ts"],"names":[],"mappings":";;;AACA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AAG,CAAC,MAAM,CAAC;QACvB,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;IAED;;OAEG;IACK,KAAK,CAAC,kBAAkB,CAAC,QAAkB;QACjD,MAAM,MAAM,GAAiB,EAAE,CAAC;QAChC,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,QAAQ,EAAE,CAAC;YACnC,MAAM,CAAC,IAAI,CAAC,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;QACrC,CAAC;QACD,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;QACjE,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,WAAW,CAAC,CAAC;QAC3C,IAAI,MAAM,GAAG,CAAC,CAAC;QACf,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC3B,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;YAC1B,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC;QACzB,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AApTD,gDAoTC"}
@@ -7,6 +7,7 @@ export * from './brightTrustService';
7
7
  export * from './brightTrustStateMachine';
8
8
  export * from './cblService';
9
9
  export * from './checksum.service';
10
+ export * from './copyOnWrite.service';
10
11
  export * from './crypto';
11
12
  export * from './fec.service';
12
13
  export * from './identitySealingPipeline';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../brightchain-lib/src/lib/services/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,yBAAyB,CAAC;AACxC,cAAc,sBAAsB,CAAC;AACrC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC;AACnC,cAAc,UAAU,CAAC;AACzB,cAAc,eAAe,CAAC;AAC9B,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,UAAU,CAAC;AACzB,cAAc,0BAA0B,CAAC;AACzC,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,iBAAiB,CAAC;AAChC,cAAc,uBAAuB,CAAC;AACtC,cAAc,eAAe,CAAC;AAC9B,cAAc,OAAO,CAAC;AAGtB,cAAc,cAAc,CAAC;AAG7B,cAAc,iBAAiB,CAAC;AAGhC,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../brightchain-lib/src/lib/services/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,yBAAyB,CAAC;AACxC,cAAc,sBAAsB,CAAC;AACrC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC;AACnC,cAAc,uBAAuB,CAAC;AACtC,cAAc,UAAU,CAAC;AACzB,cAAc,eAAe,CAAC;AAC9B,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,UAAU,CAAC;AACzB,cAAc,0BAA0B,CAAC;AACzC,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,iBAAiB,CAAC;AAChC,cAAc,uBAAuB,CAAC;AACtC,cAAc,eAAe,CAAC;AAC9B,cAAc,OAAO,CAAC;AAGtB,cAAc,cAAc,CAAC;AAG7B,cAAc,iBAAiB,CAAC;AAGhC,cAAc,YAAY,CAAC"}
@@ -10,6 +10,7 @@ tslib_1.__exportStar(require("./brightTrustService"), exports);
10
10
  tslib_1.__exportStar(require("./brightTrustStateMachine"), exports);
11
11
  tslib_1.__exportStar(require("./cblService"), exports);
12
12
  tslib_1.__exportStar(require("./checksum.service"), exports);
13
+ tslib_1.__exportStar(require("./copyOnWrite.service"), exports);
13
14
  tslib_1.__exportStar(require("./crypto"), exports);
14
15
  tslib_1.__exportStar(require("./fec.service"), exports);
15
16
  tslib_1.__exportStar(require("./identitySealingPipeline"), exports);
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../brightchain-lib/src/lib/services/index.ts"],"names":[],"mappings":";;;AAAA,0DAAgC;AAChC,4DAAkC;AAClC,kEAAwC;AACxC,+DAAqC;AACrC,yDAA+B;AAC/B,+DAAqC;AACrC,oEAA0C;AAC1C,uDAA6B;AAC7B,6DAAmC;AACnC,mDAAyB;AACzB,wDAA8B;AAC9B,oEAA0C;AAC1C,8DAAoC;AACpC,mDAAyB;AACzB,mEAAyC;AACzC,wDAA8B;AAC9B,sDAA4B;AAC5B,4DAAkC;AAClC,6DAAmC;AACnC,2DAAiC;AACjC,8DAAoC;AACpC,0DAAgC;AAChC,gEAAsC;AACtC,wDAA8B;AAC9B,gDAAsB;AAEtB,4CAA4C;AAC5C,uDAA6B;AAE7B,8DAA8D;AAC9D,0DAAgC;AAEhC,6DAA6D;AAC7D,qDAA2B"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../brightchain-lib/src/lib/services/index.ts"],"names":[],"mappings":";;;AAAA,0DAAgC;AAChC,4DAAkC;AAClC,kEAAwC;AACxC,+DAAqC;AACrC,yDAA+B;AAC/B,+DAAqC;AACrC,oEAA0C;AAC1C,uDAA6B;AAC7B,6DAAmC;AACnC,gEAAsC;AACtC,mDAAyB;AACzB,wDAA8B;AAC9B,oEAA0C;AAC1C,8DAAoC;AACpC,mDAAyB;AACzB,mEAAyC;AACzC,wDAA8B;AAC9B,sDAA4B;AAC5B,4DAAkC;AAClC,6DAAmC;AACnC,2DAAiC;AACjC,8DAAoC;AACpC,0DAAgC;AAChC,gEAAsC;AACtC,wDAA8B;AAC9B,gDAAsB;AAEtB,4CAA4C;AAC5C,uDAA6B;AAE7B,8DAA8D;AAC9D,0DAAgC;AAEhC,6DAA6D;AAC7D,qDAA2B"}
@@ -25,6 +25,16 @@ export declare class MemberStore<TID extends PlatformID = Uint8Array> implements
25
25
  */
26
26
  private idToBytes;
27
27
  constructor(blockStore: IBlockStore, db?: IMemberStoreDb);
28
+ /**
29
+ * Pick the smallest supported block size that can hold `dataLength` bytes.
30
+ *
31
+ * Delegates to `BlockService.getBlockSizeForData` with the block store's
32
+ * supported sizes so that only sizes the store actually accepts are
33
+ * considered.
34
+ *
35
+ * @throws MemberError if no supported block size is large enough.
36
+ */
37
+ private blockSizeForData;
28
38
  /**
29
39
  * Attach (or replace) the DB reference after construction.
30
40
  * Called by the plugin after connect() so MemberStore can query
@@ -99,7 +109,13 @@ export declare class MemberStore<TID extends PlatformID = Uint8Array> implements
99
109
  */
100
110
  deleteMember(id: TID): Promise<void>;
101
111
  /**
102
- * Update the member index
112
+ * Update the member index.
113
+ *
114
+ * Updates the in-memory indexes AND persists the entry to the
115
+ * `member_index` BrightDb collection (when `_db` is available) so that
116
+ * the data survives server restarts. Without this DB write, members
117
+ * created via `createMember()` / `register()` only existed in memory
118
+ * and were lost on restart.
103
119
  */
104
120
  updateIndex(entry: IMemberIndexEntry<TID>): Promise<void>;
105
121
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"memberStore.d.ts","sourceRoot":"","sources":["../../../../../brightchain-lib/src/lib/services/memberStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,MAAM,EACN,UA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C9C,aAAa,EAAE,gCAAgC,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;QAC5D,cAAc,EAAE,iCAAiC,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;KAC/D,CAAC;IAoLF;;;;;;;;;;OAUG;IACU,YAAY,CACvB,EAAE,EAAE,GAAG,EACP,OAAO,EAAE,cAAc,CAAC,GAAG,CAAC,GAC3B,OAAO,CAAC,IAAI,CAAC;IAiLhB;;OAEG;IACU,YAAY,CAAC,EAAE,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC;IAqCjD;;;;;;;;OAQG;IACU,WAAW,CAAC,KAAK,EAAE,iBAAiB,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAuFtE;;;;;;;OAOG;IACU,UAAU,CACrB,QAAQ,EAAE,oBAAoB,CAAC,GAAG,CAAC,GAClC,OAAO,CAAC,gBAAgB,CAAC,GAAG,CAAC,EAAE,CAAC;IAmFnC;;;;;;OAMG;YACW,iBAAiB;IAsF/B;;OAEG;IAEU,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAKtD;;OAEG;IACU,gBAAgB,CAC3B,QAAQ,EAAE,cAAc,CAAC,GAAG,CAAC,EAAE,GAC9B,OAAO,CAAC,IAAI,CAAC;CAIjB"}
@@ -9,6 +9,7 @@ const memberDocument_1 = require("../documents/member/memberDocument");
9
9
  const memberProfileDocument_1 = require("../documents/member/memberProfileDocument");
10
10
  const memberProfileHydration_1 = require("../documents/member/memberProfileHydration");
11
11
  const blockDataType_1 = require("../enumerations/blockDataType");
12
+ const blockSize_1 = require("../enumerations/blockSize");
12
13
  const blockType_1 = require("../enumerations/blockType");
13
14
  const brightChainStrings_1 = require("../enumerations/brightChainStrings");
14
15
  const memberErrorType_1 = require("../enumerations/memberErrorType");
@@ -66,6 +67,23 @@ class MemberStore {
66
67
  this.emailIndex = new Map();
67
68
  this._db = db ?? null;
68
69
  }
70
+ /**
71
+ * Pick the smallest supported block size that can hold `dataLength` bytes.
72
+ *
73
+ * Delegates to `BlockService.getBlockSizeForData` with the block store's
74
+ * supported sizes so that only sizes the store actually accepts are
75
+ * considered.
76
+ *
77
+ * @throws MemberError if no supported block size is large enough.
78
+ */
79
+ blockSizeForData(dataLength) {
80
+ const blockService = service_provider_1.ServiceProvider.getInstance().blockService;
81
+ const size = blockService.getBlockSizeForData(dataLength, this.blockStore.supportedBlockSizes);
82
+ if (size === blockSize_1.BlockSize.Unknown) {
83
+ throw new memberError_1.MemberError(memberErrorType_1.MemberErrorType.FailedToCreateMemberBlocks);
84
+ }
85
+ return size;
86
+ }
69
87
  /**
70
88
  * Attach (or replace) the DB reference after construction.
71
89
  * Called by the plugin after connect() so MemberStore can query
@@ -91,7 +109,7 @@ class MemberStore {
91
109
  const blocks = [];
92
110
  for (const tuple of tuples) {
93
111
  // Create metadata for XORed block
94
- const metadata = new blockMetadata_1.BlockMetadata(this.blockStore.blockSize, blockType_1.BlockType.RawData, blockDataType_1.BlockDataType.PublicMemberData, Number(cbl.originalDataLength), new Date());
112
+ const metadata = new blockMetadata_1.BlockMetadata(this.blockSizeForData(Number(cbl.originalDataLength)), blockType_1.BlockType.RawData, blockDataType_1.BlockDataType.PublicMemberData, Number(cbl.originalDataLength), new Date());
95
113
  const xoredBlock = await tuple.xor(this.blockStore, metadata);
96
114
  blocks.push(xoredBlock.data);
97
115
  }
@@ -150,10 +168,13 @@ class MemberStore {
150
168
  let publicProfileBlock;
151
169
  let privateProfileBlock;
152
170
  try {
153
- // Step 1: Create member document using factory method with block store's block size
154
- // Use the original 'member' which has the private key for signing CBLs
171
+ // Step 1: Create member document using factory method.
172
+ // The document creates an internal MemoryBlockStore for CBL operations.
173
+ // Measure the serialized member size so we pick a block size that fits.
174
+ const memberJsonLength = new TextEncoder().encode(member.toJson()).length;
175
+ const docBlockSize = this.blockSizeForData(memberJsonLength);
155
176
  doc = memberDocument_1.MemberDocument.create(member, member, undefined, undefined, {
156
- blockSize: this.blockStore.blockSize,
177
+ blockSize: docBlockSize,
157
178
  });
158
179
  rollbackOperations.push(async () => {
159
180
  // No cleanup needed for document creation
@@ -182,7 +203,11 @@ class MemberStore {
182
203
  dateCreated: new Date(),
183
204
  dateUpdated: new Date(),
184
205
  };
185
- profileDoc = memberProfileDocument_1.MemberProfileDocument.create(member, publicProfileData, privateProfileData, { blockSize: this.blockStore.blockSize });
206
+ // Estimate profile data size to pick an appropriate block size.
207
+ // Use a JSON-serializable snapshot (BigInt → string for measurement).
208
+ const profileSizeEstimate = Math.max(JSON.stringify(publicProfileData, (_k, v) => typeof v === 'bigint' ? v.toString() : v).length, JSON.stringify(privateProfileData, (_k, v) => typeof v === 'bigint' ? v.toString() : v).length);
209
+ const profileBlockSize = this.blockSizeForData(profileSizeEstimate);
210
+ profileDoc = memberProfileDocument_1.MemberProfileDocument.create(member, publicProfileData, privateProfileData, { blockSize: profileBlockSize });
186
211
  rollbackOperations.push(async () => {
187
212
  // No cleanup needed for document creation
188
213
  });
@@ -297,14 +322,14 @@ class MemberStore {
297
322
  // If we can't parse the CBL, skip copying constituent blocks
298
323
  }
299
324
  // Step 6: Create blocks for identity CBL data
300
- publicBlock = new rawData_1.RawDataBlock(this.blockStore.blockSize, publicCBLData, doc.dateCreated, undefined, // Let RawDataBlock calculate the checksum
325
+ publicBlock = new rawData_1.RawDataBlock(this.blockSizeForData(publicCBLData.length), publicCBLData, doc.dateCreated, undefined, // Let RawDataBlock calculate the checksum
301
326
  blockType_1.BlockType.RawData, blockDataType_1.BlockDataType.PublicMemberData);
302
- privateBlock = new rawData_1.RawDataBlock(this.blockStore.blockSize, privateCBLData, doc.dateCreated, undefined, // Let RawDataBlock calculate the checksum
327
+ privateBlock = new rawData_1.RawDataBlock(this.blockSizeForData(privateCBLData.length), privateCBLData, doc.dateCreated, undefined, // Let RawDataBlock calculate the checksum
303
328
  blockType_1.BlockType.RawData, blockDataType_1.BlockDataType.PrivateMemberData);
304
329
  // Step 7: Create blocks for profile CBL data
305
- publicProfileBlock = new rawData_1.RawDataBlock(this.blockStore.blockSize, publicProfileCBLData, profileDoc.dateCreated, undefined, // Let RawDataBlock calculate the checksum
330
+ publicProfileBlock = new rawData_1.RawDataBlock(this.blockSizeForData(publicProfileCBLData.length), publicProfileCBLData, profileDoc.dateCreated, undefined, // Let RawDataBlock calculate the checksum
306
331
  blockType_1.BlockType.RawData, blockDataType_1.BlockDataType.PublicMemberData);
307
- privateProfileBlock = new rawData_1.RawDataBlock(this.blockStore.blockSize, privateProfileCBLData, profileDoc.dateCreated, undefined, // Let RawDataBlock calculate the checksum
332
+ privateProfileBlock = new rawData_1.RawDataBlock(this.blockSizeForData(privateProfileCBLData.length), privateProfileCBLData, profileDoc.dateCreated, undefined, // Let RawDataBlock calculate the checksum
308
333
  blockType_1.BlockType.RawData, blockDataType_1.BlockDataType.PrivateMemberData);
309
334
  // Step 8: Store identity blocks
310
335
  await this.blockStore.setData(publicBlock);
@@ -451,7 +476,7 @@ class MemberStore {
451
476
  // Create ConstituentBlockListBlock from block data
452
477
  let cbl;
453
478
  try {
454
- cbl = new cbl_1.ConstituentBlockListBlock(publicBlock.data, memberWithCorrectId, this.blockStore.blockSize);
479
+ cbl = new cbl_1.ConstituentBlockListBlock(publicBlock.data, memberWithCorrectId, this.blockSizeForData(publicBlock.data.length));
455
480
  }
456
481
  catch {
457
482
  // If CBL creation fails, the data might be corrupted
@@ -499,6 +524,16 @@ class MemberStore {
499
524
  console.info(`[MemberStore] getMemberPublicKeyHex: found publicKey in users collection for id=${idHex}`);
500
525
  return userDoc.publicKey;
501
526
  }
527
+ // Document exists but publicKey is empty/missing
528
+ if (userDoc) {
529
+ console.warn(`[MemberStore] getMemberPublicKeyHex: user document found for id=${idHex} but publicKey is empty`);
530
+ }
531
+ else {
532
+ console.warn(`[MemberStore] getMemberPublicKeyHex: no user document found in users collection for id=${idHex}`);
533
+ }
534
+ }
535
+ else {
536
+ console.warn('[MemberStore] getMemberPublicKeyHex: no DB reference set, skipping fast path');
502
537
  }
503
538
  // Fallback — reconstruct from CBL blocks (works for createMember() members)
504
539
  try {
@@ -563,7 +598,7 @@ class MemberStore {
563
598
  try {
564
599
  // Get the member to use as creator for CBL validation
565
600
  const member = await this.getMember(id);
566
- const cbl = new cbl_1.ConstituentBlockListBlock(blockData, member, this.blockStore.blockSize);
601
+ const cbl = new cbl_1.ConstituentBlockListBlock(blockData, member, this.blockSizeForData(blockData.length));
567
602
  const storageData = await this.extractProfileDataFromCBL(cbl);
568
603
  // Validate required fields
569
604
  if (!storageData.id ||
@@ -625,7 +660,7 @@ class MemberStore {
625
660
  try {
626
661
  // Get the member to use as creator for CBL validation
627
662
  const member = await this.getMember(id);
628
- const cbl = new cbl_1.ConstituentBlockListBlock(blockData, member, this.blockStore.blockSize);
663
+ const cbl = new cbl_1.ConstituentBlockListBlock(blockData, member, this.blockSizeForData(blockData.length));
629
664
  const storageData = await this.extractProfileDataFromCBL(cbl);
630
665
  // Validate required fields
631
666
  if (!storageData.id ||
@@ -736,11 +771,11 @@ class MemberStore {
736
771
  // Encode JSON to bytes
737
772
  const publicProfileBytes = new TextEncoder().encode(publicProfileJson);
738
773
  const privateProfileBytes = new TextEncoder().encode(privateProfileJson);
739
- // Create new blocks for profile data — use the block store's block size
740
- // to ensure compatibility with the store's supported sizes.
741
- const newPublicProfileBlock = new rawData_1.RawDataBlock(this.blockStore.blockSize, publicProfileBytes, new Date(), undefined, // Let RawDataBlock calculate the checksum
774
+ // Create new blocks for profile data — pick the smallest supported
775
+ // block size that fits each payload.
776
+ const newPublicProfileBlock = new rawData_1.RawDataBlock(this.blockSizeForData(publicProfileBytes.length), publicProfileBytes, new Date(), undefined, // Let RawDataBlock calculate the checksum
742
777
  blockType_1.BlockType.RawData, blockDataType_1.BlockDataType.PublicMemberData);
743
- const newPrivateProfileBlock = new rawData_1.RawDataBlock(this.blockStore.blockSize, privateProfileBytes, new Date(), undefined, // Let RawDataBlock calculate the checksum
778
+ const newPrivateProfileBlock = new rawData_1.RawDataBlock(this.blockSizeForData(privateProfileBytes.length), privateProfileBytes, new Date(), undefined, // Let RawDataBlock calculate the checksum
744
779
  blockType_1.BlockType.RawData, blockDataType_1.BlockDataType.PrivateMemberData);
745
780
  // Store new blocks in block store
746
781
  await this.blockStore.setData(newPublicProfileBlock);
@@ -835,7 +870,13 @@ class MemberStore {
835
870
  // The garbage collector will handle unreferenced blocks
836
871
  }
837
872
  /**
838
- * Update the member index
873
+ * Update the member index.
874
+ *
875
+ * Updates the in-memory indexes AND persists the entry to the
876
+ * `member_index` BrightDb collection (when `_db` is available) so that
877
+ * the data survives server restarts. Without this DB write, members
878
+ * created via `createMember()` / `register()` only existed in memory
879
+ * and were lost on restart.
839
880
  */
840
881
  async updateIndex(entry) {
841
882
  const provider = service_provider_1.ServiceProvider.getInstance().idProvider;
@@ -874,6 +915,46 @@ class MemberStore {
874
915
  }
875
916
  // Update member index
876
917
  this.memberIndex.set(id, entry);
918
+ // ── Persist to the member_index BrightDb collection ──────────────
919
+ // This ensures the entry survives server restarts. The _queryIndexFromDb
920
+ // fallback queries this collection when the in-memory index is empty.
921
+ if (this._db) {
922
+ try {
923
+ const indexCol = this._db.collection('member_index');
924
+ const doc = {
925
+ id,
926
+ publicCBL: entry.publicCBL.toHex(),
927
+ privateCBL: entry.privateCBL.toHex(),
928
+ ...(entry.publicProfileCBL
929
+ ? { publicProfileCBL: entry.publicProfileCBL.toHex() }
930
+ : {}),
931
+ ...(entry.privateProfileCBL
932
+ ? { privateProfileCBL: entry.privateProfileCBL.toHex() }
933
+ : {}),
934
+ poolId: this._db.name ?? '',
935
+ type: entry.type,
936
+ status: entry.status,
937
+ lastUpdate: entry.lastUpdate.toISOString(),
938
+ ...(entry.region ? { region: entry.region } : {}),
939
+ reputation: entry.reputation,
940
+ };
941
+ // Upsert: try update first, insert if not found
942
+ const existing = await indexCol.findOne({ id });
943
+ if (existing) {
944
+ await indexCol.updateOne({ id }, { $set: doc });
945
+ }
946
+ else {
947
+ await indexCol.insertOne(doc);
948
+ }
949
+ }
950
+ catch (err) {
951
+ // Log but don't fail — the in-memory index is already updated.
952
+ // On restart, _queryIndexFromDb will miss this entry, but the
953
+ // users collection doc (written separately) may still allow
954
+ // partial recovery.
955
+ console.warn('[MemberStore] Failed to persist member_index entry to DB:', err instanceof Error ? err.message : String(err));
956
+ }
957
+ }
877
958
  }
878
959
  /**
879
960
  * Query the member index.