@cmdoss/memwal-sdk 0.6.2 → 0.8.0

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 (247) hide show
  1. package/ARCHITECTURE.md +547 -547
  2. package/BENCHMARKS.md +238 -238
  3. package/README.md +310 -181
  4. package/dist/ai-sdk/tools.d.ts +2 -2
  5. package/dist/ai-sdk/tools.js +2 -2
  6. package/dist/client/ClientMemoryManager.js +2 -2
  7. package/dist/client/ClientMemoryManager.js.map +1 -1
  8. package/dist/client/PersonalDataWallet.d.ts.map +1 -1
  9. package/dist/client/SimplePDWClient.d.ts +29 -1
  10. package/dist/client/SimplePDWClient.d.ts.map +1 -1
  11. package/dist/client/SimplePDWClient.js +45 -13
  12. package/dist/client/SimplePDWClient.js.map +1 -1
  13. package/dist/client/namespaces/EmbeddingsNamespace.d.ts +1 -1
  14. package/dist/client/namespaces/EmbeddingsNamespace.js +1 -1
  15. package/dist/client/namespaces/MemoryNamespace.d.ts +31 -0
  16. package/dist/client/namespaces/MemoryNamespace.d.ts.map +1 -1
  17. package/dist/client/namespaces/MemoryNamespace.js +272 -39
  18. package/dist/client/namespaces/MemoryNamespace.js.map +1 -1
  19. package/dist/client/namespaces/consolidated/AINamespace.d.ts +2 -2
  20. package/dist/client/namespaces/consolidated/AINamespace.js +2 -2
  21. package/dist/client/namespaces/consolidated/BlockchainNamespace.d.ts +12 -2
  22. package/dist/client/namespaces/consolidated/BlockchainNamespace.d.ts.map +1 -1
  23. package/dist/client/namespaces/consolidated/BlockchainNamespace.js +62 -4
  24. package/dist/client/namespaces/consolidated/BlockchainNamespace.js.map +1 -1
  25. package/dist/client/namespaces/consolidated/StorageNamespace.d.ts +67 -2
  26. package/dist/client/namespaces/consolidated/StorageNamespace.d.ts.map +1 -1
  27. package/dist/client/namespaces/consolidated/StorageNamespace.js +549 -16
  28. package/dist/client/namespaces/consolidated/StorageNamespace.js.map +1 -1
  29. package/dist/config/ConfigurationHelper.js +61 -61
  30. package/dist/config/defaults.js +2 -2
  31. package/dist/config/defaults.js.map +1 -1
  32. package/dist/graph/GraphService.js +21 -21
  33. package/dist/graph/GraphService.js.map +1 -1
  34. package/dist/index.d.ts +3 -1
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +3 -1
  37. package/dist/index.js.map +1 -1
  38. package/dist/infrastructure/seal/EncryptionService.d.ts +9 -5
  39. package/dist/infrastructure/seal/EncryptionService.d.ts.map +1 -1
  40. package/dist/infrastructure/seal/EncryptionService.js +37 -15
  41. package/dist/infrastructure/seal/EncryptionService.js.map +1 -1
  42. package/dist/infrastructure/seal/SealService.d.ts +13 -5
  43. package/dist/infrastructure/seal/SealService.d.ts.map +1 -1
  44. package/dist/infrastructure/seal/SealService.js +36 -34
  45. package/dist/infrastructure/seal/SealService.js.map +1 -1
  46. package/dist/langchain/createPDWRAG.js +30 -30
  47. package/dist/retrieval/MemoryDecryptionPipeline.d.ts.map +1 -1
  48. package/dist/retrieval/MemoryDecryptionPipeline.js +2 -1
  49. package/dist/retrieval/MemoryDecryptionPipeline.js.map +1 -1
  50. package/dist/retrieval/MemoryRetrievalService.d.ts +31 -0
  51. package/dist/retrieval/MemoryRetrievalService.d.ts.map +1 -1
  52. package/dist/retrieval/MemoryRetrievalService.js +44 -4
  53. package/dist/retrieval/MemoryRetrievalService.js.map +1 -1
  54. package/dist/services/CapabilityService.d.ts.map +1 -1
  55. package/dist/services/CapabilityService.js +30 -14
  56. package/dist/services/CapabilityService.js.map +1 -1
  57. package/dist/services/CrossContextPermissionService.d.ts.map +1 -1
  58. package/dist/services/CrossContextPermissionService.js +9 -7
  59. package/dist/services/CrossContextPermissionService.js.map +1 -1
  60. package/dist/services/EmbeddingService.d.ts +28 -1
  61. package/dist/services/EmbeddingService.d.ts.map +1 -1
  62. package/dist/services/EmbeddingService.js +54 -0
  63. package/dist/services/EmbeddingService.js.map +1 -1
  64. package/dist/services/EncryptionService.d.ts.map +1 -1
  65. package/dist/services/EncryptionService.js +6 -5
  66. package/dist/services/EncryptionService.js.map +1 -1
  67. package/dist/services/GeminiAIService.js +309 -309
  68. package/dist/services/IndexManager.d.ts +5 -1
  69. package/dist/services/IndexManager.d.ts.map +1 -1
  70. package/dist/services/IndexManager.js +17 -40
  71. package/dist/services/IndexManager.js.map +1 -1
  72. package/dist/services/QueryService.js +1 -1
  73. package/dist/services/QueryService.js.map +1 -1
  74. package/dist/services/StorageService.d.ts +11 -0
  75. package/dist/services/StorageService.d.ts.map +1 -1
  76. package/dist/services/StorageService.js +73 -10
  77. package/dist/services/StorageService.js.map +1 -1
  78. package/dist/services/TransactionService.d.ts +20 -0
  79. package/dist/services/TransactionService.d.ts.map +1 -1
  80. package/dist/services/TransactionService.js +43 -0
  81. package/dist/services/TransactionService.js.map +1 -1
  82. package/dist/services/ViewService.js +2 -2
  83. package/dist/services/ViewService.js.map +1 -1
  84. package/dist/services/storage/QuiltBatchManager.d.ts +101 -1
  85. package/dist/services/storage/QuiltBatchManager.d.ts.map +1 -1
  86. package/dist/services/storage/QuiltBatchManager.js +410 -20
  87. package/dist/services/storage/QuiltBatchManager.js.map +1 -1
  88. package/dist/services/storage/index.d.ts +1 -1
  89. package/dist/services/storage/index.d.ts.map +1 -1
  90. package/dist/services/storage/index.js.map +1 -1
  91. package/dist/utils/LRUCache.d.ts +106 -0
  92. package/dist/utils/LRUCache.d.ts.map +1 -0
  93. package/dist/utils/LRUCache.js +281 -0
  94. package/dist/utils/LRUCache.js.map +1 -0
  95. package/dist/utils/index.d.ts +1 -0
  96. package/dist/utils/index.d.ts.map +1 -1
  97. package/dist/utils/index.js +2 -0
  98. package/dist/utils/index.js.map +1 -1
  99. package/dist/utils/memoryIndexOnChain.d.ts +212 -0
  100. package/dist/utils/memoryIndexOnChain.d.ts.map +1 -0
  101. package/dist/utils/memoryIndexOnChain.js +312 -0
  102. package/dist/utils/memoryIndexOnChain.js.map +1 -0
  103. package/dist/utils/rebuildIndexNode.d.ts +29 -0
  104. package/dist/utils/rebuildIndexNode.d.ts.map +1 -1
  105. package/dist/utils/rebuildIndexNode.js +366 -98
  106. package/dist/utils/rebuildIndexNode.js.map +1 -1
  107. package/dist/vector/HnswWasmService.d.ts +20 -5
  108. package/dist/vector/HnswWasmService.d.ts.map +1 -1
  109. package/dist/vector/HnswWasmService.js +73 -40
  110. package/dist/vector/HnswWasmService.js.map +1 -1
  111. package/dist/vector/IHnswService.d.ts +10 -1
  112. package/dist/vector/IHnswService.d.ts.map +1 -1
  113. package/dist/vector/IHnswService.js.map +1 -1
  114. package/dist/vector/NodeHnswService.d.ts +16 -0
  115. package/dist/vector/NodeHnswService.d.ts.map +1 -1
  116. package/dist/vector/NodeHnswService.js +84 -5
  117. package/dist/vector/NodeHnswService.js.map +1 -1
  118. package/dist/vector/createHnswService.d.ts +1 -1
  119. package/dist/vector/createHnswService.js +1 -1
  120. package/dist/vector/index.d.ts +1 -1
  121. package/dist/vector/index.js +1 -1
  122. package/package.json +157 -157
  123. package/src/access/PermissionService.ts +635 -635
  124. package/src/aggregation/AggregationService.ts +389 -389
  125. package/src/ai-sdk/PDWVectorStore.ts +715 -715
  126. package/src/ai-sdk/index.ts +65 -65
  127. package/src/ai-sdk/tools.ts +460 -460
  128. package/src/ai-sdk/types.ts +404 -404
  129. package/src/batch/BatchManager.ts +597 -597
  130. package/src/batch/BatchingService.ts +429 -429
  131. package/src/batch/MemoryProcessingCache.ts +492 -492
  132. package/src/batch/index.ts +30 -30
  133. package/src/browser.ts +200 -200
  134. package/src/client/ClientMemoryManager.ts +987 -987
  135. package/src/client/PersonalDataWallet.ts +345 -345
  136. package/src/client/SimplePDWClient.ts +1289 -1222
  137. package/src/client/factory.ts +154 -154
  138. package/src/client/namespaces/AnalyticsNamespace.ts +377 -377
  139. package/src/client/namespaces/BatchNamespace.ts +356 -356
  140. package/src/client/namespaces/CacheNamespace.ts +123 -123
  141. package/src/client/namespaces/CapabilityNamespace.ts +217 -217
  142. package/src/client/namespaces/ClassifyNamespace.ts +169 -169
  143. package/src/client/namespaces/ContextNamespace.ts +297 -297
  144. package/src/client/namespaces/EmbeddingsNamespace.ts +99 -99
  145. package/src/client/namespaces/EncryptionNamespace.ts +221 -221
  146. package/src/client/namespaces/GraphNamespace.ts +468 -468
  147. package/src/client/namespaces/IndexNamespace.ts +361 -361
  148. package/src/client/namespaces/MemoryNamespace.ts +1422 -1135
  149. package/src/client/namespaces/PermissionsNamespace.ts +254 -254
  150. package/src/client/namespaces/PipelineNamespace.ts +220 -220
  151. package/src/client/namespaces/SearchNamespace.ts +1049 -1049
  152. package/src/client/namespaces/StorageNamespace.ts +458 -458
  153. package/src/client/namespaces/TxNamespace.ts +260 -260
  154. package/src/client/namespaces/WalletNamespace.ts +243 -243
  155. package/src/client/namespaces/consolidated/AINamespace.ts +449 -449
  156. package/src/client/namespaces/consolidated/BlockchainNamespace.ts +607 -546
  157. package/src/client/namespaces/consolidated/SecurityNamespace.ts +648 -648
  158. package/src/client/namespaces/consolidated/StorageNamespace.ts +1141 -497
  159. package/src/client/namespaces/consolidated/index.ts +39 -39
  160. package/src/client/signers/KeypairSigner.ts +108 -108
  161. package/src/client/signers/UnifiedSigner.ts +110 -110
  162. package/src/client/signers/WalletAdapterSigner.ts +159 -159
  163. package/src/client/signers/index.ts +26 -26
  164. package/src/config/ConfigurationHelper.ts +412 -412
  165. package/src/config/defaults.ts +51 -51
  166. package/src/config/index.ts +8 -8
  167. package/src/config/validation.ts +70 -70
  168. package/src/core/index.ts +14 -14
  169. package/src/core/interfaces/IService.ts +307 -307
  170. package/src/core/interfaces/index.ts +8 -8
  171. package/src/core/types/capability.ts +297 -297
  172. package/src/core/types/index.ts +870 -870
  173. package/src/core/types/wallet.ts +270 -270
  174. package/src/core/types.ts +9 -9
  175. package/src/core/wallet.ts +222 -222
  176. package/src/embedding/index.ts +19 -19
  177. package/src/embedding/types.ts +357 -357
  178. package/src/errors/index.ts +602 -602
  179. package/src/errors/recovery.ts +461 -461
  180. package/src/errors/validation.ts +567 -567
  181. package/src/generated/pdw/capability.ts +319 -319
  182. package/src/graph/GraphService.ts +887 -887
  183. package/src/graph/KnowledgeGraphManager.ts +728 -728
  184. package/src/graph/index.ts +25 -25
  185. package/src/index.ts +498 -474
  186. package/src/infrastructure/index.ts +22 -22
  187. package/src/infrastructure/seal/EncryptionService.ts +628 -603
  188. package/src/infrastructure/seal/SealService.ts +613 -615
  189. package/src/infrastructure/seal/index.ts +9 -9
  190. package/src/infrastructure/sui/BlockchainManager.ts +627 -627
  191. package/src/infrastructure/sui/SuiService.ts +888 -888
  192. package/src/infrastructure/sui/index.ts +9 -9
  193. package/src/infrastructure/walrus/StorageManager.ts +604 -604
  194. package/src/infrastructure/walrus/WalrusStorageService.ts +612 -612
  195. package/src/infrastructure/walrus/index.ts +9 -9
  196. package/src/langchain/PDWEmbeddings.ts +145 -145
  197. package/src/langchain/PDWVectorStore.ts +456 -456
  198. package/src/langchain/createPDWRAG.ts +303 -303
  199. package/src/langchain/index.ts +47 -47
  200. package/src/permissions/ConsentRepository.browser.ts +249 -249
  201. package/src/permissions/ConsentRepository.ts +364 -364
  202. package/src/pipeline/MemoryPipeline.ts +862 -862
  203. package/src/pipeline/PipelineManager.ts +683 -683
  204. package/src/pipeline/index.ts +26 -26
  205. package/src/retrieval/AdvancedSearchService.ts +629 -629
  206. package/src/retrieval/MemoryAnalyticsService.ts +711 -711
  207. package/src/retrieval/MemoryDecryptionPipeline.ts +825 -824
  208. package/src/retrieval/MemoryRetrievalService.ts +904 -830
  209. package/src/retrieval/index.ts +42 -42
  210. package/src/services/BatchService.ts +352 -352
  211. package/src/services/CapabilityService.ts +464 -448
  212. package/src/services/ClassifierService.ts +465 -465
  213. package/src/services/CrossContextPermissionService.ts +486 -484
  214. package/src/services/EmbeddingService.ts +771 -706
  215. package/src/services/EncryptionService.ts +712 -711
  216. package/src/services/GeminiAIService.ts +753 -753
  217. package/src/services/IndexManager.ts +977 -1004
  218. package/src/services/MemoryIndexService.ts +1003 -1003
  219. package/src/services/MemoryService.ts +369 -369
  220. package/src/services/QueryService.ts +890 -890
  221. package/src/services/StorageService.ts +1182 -1111
  222. package/src/services/TransactionService.ts +838 -790
  223. package/src/services/VectorService.ts +462 -462
  224. package/src/services/ViewService.ts +484 -484
  225. package/src/services/index.ts +25 -25
  226. package/src/services/storage/BlobAttributesManager.ts +333 -333
  227. package/src/services/storage/KnowledgeGraphManager.ts +425 -425
  228. package/src/services/storage/MemorySearchManager.ts +387 -387
  229. package/src/services/storage/QuiltBatchManager.ts +1130 -660
  230. package/src/services/storage/WalrusMetadataManager.ts +268 -268
  231. package/src/services/storage/WalrusStorageManager.ts +287 -287
  232. package/src/services/storage/index.ts +57 -52
  233. package/src/types/index.ts +13 -13
  234. package/src/utils/LRUCache.ts +378 -0
  235. package/src/utils/index.ts +76 -68
  236. package/src/utils/memoryIndexOnChain.ts +507 -0
  237. package/src/utils/rebuildIndex.ts +290 -290
  238. package/src/utils/rebuildIndexNode.ts +771 -424
  239. package/src/vector/BrowserHnswIndexService.ts +758 -758
  240. package/src/vector/HnswWasmService.ts +731 -679
  241. package/src/vector/IHnswService.ts +233 -224
  242. package/src/vector/NodeHnswService.ts +833 -735
  243. package/src/vector/VectorManager.ts +478 -478
  244. package/src/vector/createHnswService.ts +135 -135
  245. package/src/vector/index.ts +56 -56
  246. package/src/wallet/ContextWalletService.ts +656 -656
  247. package/src/wallet/MainWalletService.ts +317 -317
@@ -1,712 +1,713 @@
1
- /**
2
- * EncryptionService - SEAL-based encryption and access control
3
- *
4
- * Provides identity-based encryption using Mysten's SEAL SDK with decentralized
5
- * key management and onchain access control policies.
6
- */
7
-
8
- import { SessionKey } from '@mysten/seal';
9
- import { Transaction } from '@mysten/sui/transactions';
10
- import { fromHex, toHex } from '@mysten/sui/utils';
11
- import { keccak_256 } from '@noble/hashes/sha3.js';
12
- import { SealService } from '../infrastructure/seal/SealService';
13
- import { CrossContextPermissionService } from './CrossContextPermissionService';
14
- import type {
15
- ClientWithCoreApi,
16
- PDWConfig,
17
- AccessPermission,
18
- AccessControlOptions,
19
- Thunk,
20
- SealEncryptionResult,
21
- SealDecryptionOptions
22
- } from '../types';
23
-
24
- export interface AccessGrantOptions {
25
- ownerAddress: string;
26
- recipientAddress: string;
27
- contentId: string;
28
- accessLevel: 'read' | 'write';
29
- expiresIn?: number; // milliseconds from now
30
- }
31
-
32
- export interface AccessRevokeOptions {
33
- ownerAddress: string;
34
- recipientAddress: string;
35
- contentId: string;
36
- }
37
-
38
- export class EncryptionService {
39
- private sealService: SealService;
40
- private suiClient: any;
41
- private packageId: string;
42
- private sessionKeyCache = new Map<string, SessionKey>();
43
- private permissionService: CrossContextPermissionService;
44
-
45
- constructor(
46
- private client: ClientWithCoreApi,
47
- private config: PDWConfig
48
- ) {
49
- this.suiClient = (client as any).client || client;
50
- this.packageId = config.packageId || '';
51
- this.sealService = this.initializeSealService();
52
-
53
- // Initialize permission service for OAuth-style access control
54
- this.permissionService = new CrossContextPermissionService(
55
- {
56
- packageId: this.packageId,
57
- accessRegistryId: config.accessRegistryId || ''
58
- },
59
- this.suiClient
60
- );
61
- }
62
-
63
- /**
64
- * Initialize SEAL service with proper configuration
65
- */
66
- private initializeSealService(): SealService {
67
- const encryptionConfig = this.config.encryptionConfig;
68
-
69
- if (!encryptionConfig?.enabled) {
70
- console.warn('Encryption is disabled in configuration - creating SealService anyway');
71
- }
72
-
73
- // Default testnet key servers (replace with actual server object IDs)
74
- const defaultKeyServers = [
75
- '0x73d05d62c18d9374e3ea529e8e0ed6161da1a141a94d3f76ae3fe4e99356db75',
76
- '0xf5d14a81a982144ae441cd7d64b09027f116a468bd36e7eca494f750591623c8'
77
- ];
78
-
79
- const keyServerIds = encryptionConfig?.keyServers || defaultKeyServers;
80
-
81
- // Create SealService with proper configuration
82
- const sealConfig = {
83
- suiClient: this.suiClient,
84
- packageId: this.packageId,
85
- keyServerUrls: [], // Empty for now, URLs handled separately if needed
86
- keyServerObjectIds: keyServerIds,
87
- network: process.env.NODE_ENV === 'production' ? 'mainnet' as const : 'testnet' as const,
88
- threshold: 2,
89
- enableMetrics: true,
90
- retryAttempts: 3,
91
- timeoutMs: 30000
92
- };
93
-
94
- return new SealService(sealConfig);
95
- }
96
-
97
- /**
98
- * Build access approval transaction for SEAL key servers (LEGACY)
99
- *
100
- * @deprecated Use buildAccessTransactionForWallet instead for wallet-based permissions
101
- */
102
- async buildAccessTransaction(
103
- userAddress: string,
104
- accessType: 'read' | 'write' = 'read'
105
- ): Promise<Transaction> {
106
- console.warn('buildAccessTransaction is deprecated - use buildAccessTransactionForWallet for wallet-based permissions');
107
-
108
- return this.buildAccessTransactionForWallet(userAddress, userAddress, accessType);
109
- }
110
-
111
- /**
112
- * Build access approval transaction using capability pattern
113
- *
114
- * Uses pdw::capability::seal_approve which validates:
115
- * - User owns the MemoryCap object
116
- * - Provided key_id matches computed key from owner + nonce
117
- *
118
- * @param keyId - SEAL key ID bytes (computed from owner + nonce)
119
- * @param memoryCapId - MemoryCap object ID on Sui
120
- * @returns Transaction for SEAL key server approval
121
- */
122
- buildCapabilityApproveTransaction(
123
- keyId: Uint8Array,
124
- memoryCapId: string
125
- ): Transaction {
126
- return this.permissionService.buildSealApproveTransaction(keyId, memoryCapId);
127
- }
128
-
129
- /**
130
- * Build access approval transaction for a requesting wallet address
131
- * Uses CrossContextPermissionService for proper permission validation
132
- *
133
- * @deprecated Use buildCapabilityApproveTransaction for capability-based access
134
- * @param userAddress - User's wallet address (used as SEAL identity)
135
- * @param requestingWallet - Wallet requesting access
136
- * @param accessType - Access level (read/write)
137
- * @returns Transaction for SEAL key server approval
138
- */
139
- async buildAccessTransactionForWallet(
140
- userAddress: string,
141
- requestingWallet: string,
142
- accessType: 'read' | 'write' = 'read'
143
- ): Promise<Transaction> {
144
- // Legacy: Convert user address to bytes for SEAL identity
145
- const identityBytes = fromHex(userAddress.replace('0x', ''));
146
-
147
- // Use legacy method if memoryCapId is not available
148
- return this.permissionService.buildSealApproveTransactionLegacy(
149
- identityBytes,
150
- requestingWallet
151
- );
152
- }
153
-
154
- /**
155
- * Encrypt data using SEAL via SealService
156
- *
157
- * @param data - Data to encrypt
158
- * @param userAddress - User's wallet address (used as SEAL identity/id)
159
- * @param threshold - Minimum key servers required for decryption (default: 2)
160
- * @returns Encrypted data and backup key
161
- */
162
- async encrypt(
163
- data: Uint8Array,
164
- userAddress: string,
165
- threshold?: number
166
- ): Promise<{ encryptedObject: Uint8Array; backupKey: Uint8Array }> {
167
- try {
168
- console.log('🔒 EncryptionService: Starting SEAL encryption...');
169
- console.log(` User address (identity): ${userAddress}`);
170
- console.log(` Data size: ${data.length} bytes`);
171
- console.log(` Threshold: ${threshold || 2} key servers`);
172
-
173
- const result = await this.sealService.encryptData({
174
- data,
175
- id: userAddress, // Use user's address as SEAL identity
176
- threshold: threshold || 2
177
- });
178
-
179
- console.log('✅ EncryptionService: SEAL encryption successful');
180
- console.log(` Encrypted size: ${result.encryptedObject.length} bytes`);
181
- console.log(` Backup key generated: ${result.key.length} bytes`);
182
-
183
- return {
184
- encryptedObject: result.encryptedObject,
185
- backupKey: result.key
186
- };
187
- } catch (error) {
188
- throw new Error(`Encryption failed: ${error}`);
189
- }
190
- }
191
-
192
- /**
193
- * Decrypt data using SEAL with session keys via SealService
194
- * Handles both new binary format (Uint8Array) and legacy base64 format
195
- *
196
- * Supports two access control patterns:
197
- * 1. Capability pattern (recommended): Requires memoryCapId and keyId
198
- * 2. Legacy allowlist pattern: Uses requestingWallet for permission check
199
- */
200
- async decrypt(options: SealDecryptionOptions): Promise<Uint8Array> {
201
- try {
202
- console.log('🔓 EncryptionService: Starting SEAL decryption...');
203
- console.log(` User address: ${options.userAddress}`);
204
- const requestingWallet = options.requestingWallet ?? options.userAddress;
205
-
206
- // Get or create session key
207
- let activeSessionKey = options.sessionKey;
208
- if (!activeSessionKey) {
209
- console.log('🔄 Creating session key...');
210
- activeSessionKey = await this.getOrCreateSessionKey(options.userAddress);
211
- }
212
-
213
- // Build access transaction if not provided
214
- let txBytes = options.signedTxBytes;
215
- if (!txBytes) {
216
- // Check if capability pattern is being used
217
- if (options.memoryCapId && options.keyId) {
218
- console.log('🔄 Building capability-based access transaction...');
219
- console.log(` MemoryCap ID: ${options.memoryCapId}`);
220
- console.log(` Key ID length: ${options.keyId.length} bytes`);
221
-
222
- const tx = this.buildCapabilityApproveTransaction(options.keyId, options.memoryCapId);
223
- // CRITICAL: Set sender before building - required by Transaction.build()
224
- tx.setSender(requestingWallet);
225
- console.log(` Transaction sender set to: ${requestingWallet}`);
226
- // SEAL REQUIREMENT: Must use onlyTransactionKind: true for PTB validation
227
- txBytes = await tx.build({ client: this.suiClient, onlyTransactionKind: true });
228
- } else {
229
- // Fallback to legacy allowlist pattern
230
- console.log('🔄 Building legacy access transaction (allowlist pattern)...');
231
- console.warn(' ⚠️ Consider using capability pattern with memoryCapId and keyId');
232
- console.log(` Requesting wallet: ${requestingWallet}`);
233
-
234
- const tx = await this.buildAccessTransactionForWallet(
235
- options.userAddress,
236
- requestingWallet,
237
- 'read'
238
- );
239
- // CRITICAL: Set sender before building - required by Transaction.build()
240
- tx.setSender(requestingWallet);
241
- console.log(` Transaction sender set to: ${requestingWallet}`);
242
- // SEAL REQUIREMENT: Must use onlyTransactionKind: true for PTB validation
243
- txBytes = await tx.build({ client: this.suiClient, onlyTransactionKind: true });
244
- }
245
- }
246
-
247
- if (!txBytes) {
248
- throw new Error('Failed to build SEAL approval transaction bytes');
249
- }
250
-
251
- // ✅ CRITICAL: Handle both binary and legacy formats
252
- let encryptedBytes: Uint8Array;
253
-
254
- if (options.encryptedContent && options.encryptedContent instanceof Uint8Array) {
255
- // **NEW BINARY FORMAT** (preferred - matches memory-workflow-seal.ts)
256
- encryptedBytes = options.encryptedContent;
257
- console.log('✅ Using new binary format (Uint8Array)');
258
- console.log(` Binary data size: ${encryptedBytes.length} bytes`);
259
- console.log(` Format: Direct binary (preserves SEAL integrity)`);
260
- } else if (options.encryptedData) {
261
- // **LEGACY BASE64 FORMAT** (deprecated but supported for backward compatibility)
262
- console.log('⚠️ Using legacy base64 format (deprecated)');
263
- const encryptedDataBase64 = options.encryptedData;
264
- const binaryString = atob(encryptedDataBase64);
265
- encryptedBytes = new Uint8Array(binaryString.length);
266
- for (let i = 0; i < binaryString.length; i++) {
267
- encryptedBytes[i] = binaryString.charCodeAt(i);
268
- }
269
- console.log(` Converted from base64: ${encryptedDataBase64.length} chars → ${encryptedBytes.length} bytes`);
270
- console.log(' Recommendation: Use encryptedContent (Uint8Array) for better performance');
271
- } else {
272
- throw new Error('No encrypted data provided. Use either encryptedContent (Uint8Array) or encryptedData (base64 string)');
273
- }
274
-
275
- console.log('🔄 Calling SEAL decryption...');
276
- console.log(` Encrypted data length: ${encryptedBytes.length} bytes`);
277
- console.log(` Session key available: ${!!activeSessionKey}`);
278
- console.log(` Transaction bytes length: ${txBytes.length} bytes`);
279
-
280
- // Use SealService for decryption (matches memory-workflow-seal.ts pattern)
281
- const decryptResult = await this.sealService.decryptData({
282
- encryptedObject: encryptedBytes,
283
- sessionKey: activeSessionKey,
284
- txBytes
285
- });
286
-
287
- console.log(`✅ EncryptionService: SEAL decryption successful`);
288
- console.log(` Decrypted data size: ${decryptResult.length} bytes`);
289
- console.log(` Binary integrity preserved throughout process`);
290
-
291
- return decryptResult;
292
- } catch (error) {
293
- throw new Error(`Decryption failed: ${error}`);
294
- }
295
- }
296
-
297
- // ==================== SESSION KEY MANAGEMENT ====================
298
-
299
- /**
300
- * Create a new session key for a user via SealService
301
- *
302
- * Two usage patterns:
303
- * 1. Frontend: Pass signPersonalMessageFn from @mysten/dapp-kit useSignPersonalMessage hook
304
- * 2. Backend: Pass keypair for direct signing (auto-converts format)
305
- */
306
- async createSessionKey(
307
- userAddress: string,
308
- signer?: {
309
- // Frontend pattern: dapp-kit signPersonalMessage function
310
- signPersonalMessageFn?: (message: string) => Promise<{ signature: string }>;
311
- // Backend pattern: Ed25519Keypair for direct signing
312
- keypair?: any;
313
- }
314
- ): Promise<SessionKey> {
315
- try {
316
- console.log('🔄 EncryptionService: Creating SEAL session key...');
317
- console.log(` User address: ${userAddress}`);
318
- console.log(` TTL: 30 minutes (maximum allowed)`);
319
-
320
- const sessionResult = await this.sealService.createSession({
321
- address: userAddress,
322
- packageId: this.packageId,
323
- ttlMin: 30, // Use 30 minutes (maximum allowed)
324
- });
325
-
326
- // Handle signing based on provided signer type
327
- if (signer?.signPersonalMessageFn) {
328
- // Frontend pattern: Use dapp-kit signPersonalMessage (RECOMMENDED)
329
- console.log('🔄 Signing with dapp-kit signPersonalMessage (recommended)...');
330
- const personalMessage = sessionResult.personalMessage;
331
-
332
- // Convert to string if it's a byte array
333
- const messageString = typeof personalMessage === 'string'
334
- ? personalMessage
335
- : new TextDecoder().decode(personalMessage);
336
-
337
- console.log(` Message (first 100 chars): ${messageString.substring(0, 100)}...`);
338
-
339
- // Use dapp-kit signPersonalMessage - returns signature in correct format
340
- const result = await signer.signPersonalMessageFn(messageString);
341
-
342
- console.log(` Signature from dapp-kit: ${result.signature.substring(0, 20)}...`);
343
- console.log(` ✅ Using dapp-kit signature format (already compatible with SEAL)`);
344
-
345
- // Set signature directly - dapp-kit returns it in SEAL-compatible format
346
- await sessionResult.sessionKey.setPersonalMessageSignature(result.signature);
347
- console.log('✅ Personal message signed with dapp-kit');
348
-
349
- } else if (signer?.keypair) {
350
- // Backend pattern: Use Ed25519Keypair with format conversion
351
- console.log('🔄 Signing with Ed25519Keypair (backend fallback)...');
352
- const personalMessage = sessionResult.personalMessage;
353
-
354
- // Convert to string if it's a byte array
355
- const messageString = typeof personalMessage === 'string'
356
- ? personalMessage
357
- : new TextDecoder().decode(personalMessage);
358
-
359
- console.log(` Message (first 100 chars): ${messageString.substring(0, 100)}...`);
360
-
361
- // Sign with keypair
362
- const messageSignature = await signer.keypair.signPersonalMessage(new TextEncoder().encode(messageString));
363
-
364
- // CRITICAL FIX: Use signature as-is from Ed25519Keypair (SEAL expects original format)
365
- // According to SEAL documentation, pass signature directly from keypair.signPersonalMessage()
366
- console.log(` ✅ Using signature as-is from Ed25519Keypair (SEAL-compatible format)`);
367
- console.log(` Original signature: ${messageSignature.signature.substring(0, 20)}...`);
368
-
369
- // Set signature exactly as returned by keypair (no conversion needed)
370
- await sessionResult.sessionKey.setPersonalMessageSignature(messageSignature.signature);
371
- console.log('✅ Personal message signed with Ed25519Keypair');
372
-
373
- } else {
374
- console.log('⚠️ No signer provided - session key created but not signed');
375
- console.log(' Note: Call setPersonalMessageSignature() later with wallet-signed message');
376
- console.log(' Frontend: Use dapp-kit useSignPersonalMessage hook');
377
- console.log(' Backend: Provide Ed25519Keypair');
378
- }
379
-
380
- // Cache the session key
381
- this.sessionKeyCache.set(userAddress, sessionResult.sessionKey);
382
-
383
- console.log('✅ EncryptionService: Session key created and cached');
384
- return sessionResult.sessionKey;
385
- } catch (error) {
386
- throw new Error(`Failed to create session key: ${error}`);
387
- }
388
- }
389
-
390
- /**
391
- * Get cached session key or create new one
392
- */
393
- async getOrCreateSessionKey(userAddress: string): Promise<SessionKey> {
394
- const cached = this.sessionKeyCache.get(userAddress);
395
- if (cached) {
396
- return cached;
397
- }
398
-
399
- return this.createSessionKey(userAddress);
400
- }
401
-
402
- /**
403
- * Export session key for persistence
404
- */
405
- async exportSessionKey(sessionKey: SessionKey): Promise<string> {
406
- try {
407
- const exported = sessionKey.export();
408
- return JSON.stringify(exported);
409
- } catch (error) {
410
- throw new Error(`Failed to export session key: ${error}`);
411
- }
412
- }
413
-
414
- /**
415
- * Import previously exported session key
416
- */
417
- async importSessionKey(exportedKey: string, userAddress?: string): Promise<SessionKey> {
418
- try {
419
- const keyData = JSON.parse(exportedKey);
420
- const sessionKey = SessionKey.import(keyData, this.suiClient);
421
-
422
- if (userAddress) {
423
- this.sessionKeyCache.set(userAddress, sessionKey);
424
- }
425
-
426
- return sessionKey;
427
- } catch (error) {
428
- throw new Error(`Failed to import session key: ${error}`);
429
- }
430
- }
431
-
432
- // ==================== ACCESS CONTROL TRANSACTIONS ====================
433
-
434
- /**
435
- * Create SEAL approval transaction bytes (matches memory-workflow-seal.ts pattern)
436
- * Returns raw PTB format bytes for SEAL verification
437
- */
438
- async createSealApproveTransaction(userAddress: string, contentOwner: string): Promise<Uint8Array> {
439
- try {
440
- console.log('🔄 EncryptionService: Creating SEAL approval transaction...');
441
- console.log(` User address: ${userAddress}`);
442
- console.log(` Content owner: ${contentOwner}`);
443
-
444
- // Create the approval transaction (not signed, just bytes)
445
- const tx = await this.buildAccessTransactionForWallet(userAddress, contentOwner, 'read');
446
- // CRITICAL: Set sender before building - required by Transaction.build()
447
- tx.setSender(contentOwner);
448
- const txBytes = await tx.build({ client: this.suiClient });
449
-
450
- console.log(`✅ Created SEAL approval transaction bytes (${txBytes.length} bytes)`);
451
- console.log(' Format: Raw PTB (Programmable Transaction Block) for SEAL verification');
452
-
453
- return txBytes;
454
- } catch (error) {
455
- throw new Error(`Failed to create SEAL approval transaction: ${error}`);
456
- }
457
- }
458
-
459
- /**
460
- * Build transaction to grant access to another user
461
- */
462
- async buildGrantAccessTransaction(options: AccessGrantOptions): Promise<Transaction> {
463
- const { ownerAddress, recipientAddress, contentId, accessLevel, expiresIn } = options;
464
- const tx = new Transaction();
465
-
466
- const expiresAt = expiresIn ? Date.now() + expiresIn : Date.now() + 86400000; // 24h default
467
-
468
- tx.moveCall({
469
- target: `${this.packageId}::seal_access_control::grant_access`,
470
- arguments: [
471
- tx.pure.address(ownerAddress),
472
- tx.pure.address(recipientAddress),
473
- tx.pure.string(contentId),
474
- tx.pure.string(accessLevel),
475
- tx.pure.u64(expiresAt),
476
- ],
477
- });
478
-
479
- return tx;
480
- }
481
-
482
- /**
483
- * Build transaction to revoke access from a user
484
- */
485
- async buildRevokeAccessTransaction(options: AccessRevokeOptions): Promise<Transaction> {
486
- const { ownerAddress, recipientAddress, contentId } = options;
487
- const tx = new Transaction();
488
-
489
- tx.moveCall({
490
- target: `${this.packageId}::seal_access_control::revoke_access`,
491
- arguments: [
492
- tx.pure.address(ownerAddress),
493
- tx.pure.address(recipientAddress),
494
- tx.pure.string(contentId),
495
- ],
496
- });
497
-
498
- return tx;
499
- }
500
-
501
- /**
502
- * Build transaction to register content ownership
503
- */
504
- async buildRegisterContentTransaction(
505
- ownerAddress: string,
506
- contentId: string,
507
- contentHash: string
508
- ): Promise<Transaction> {
509
- const tx = new Transaction();
510
-
511
- tx.moveCall({
512
- target: `${this.packageId}::seal_access_control::register_content`,
513
- arguments: [
514
- tx.pure.address(ownerAddress),
515
- tx.pure.string(contentId),
516
- tx.pure.string(contentHash),
517
- tx.pure.string(''), // encryption_info
518
- ],
519
- });
520
-
521
- return tx;
522
- }
523
-
524
- // ==================== TRANSACTION BUILDERS ====================
525
-
526
- get tx() {
527
- return {
528
- /**
529
- * Grant access to encrypted memory
530
- */
531
- grantAccess: (options: AccessGrantOptions) => {
532
- return this.buildGrantAccessTransaction(options);
533
- },
534
-
535
- /**
536
- * Revoke access to encrypted memory
537
- */
538
- revokeAccess: (options: AccessRevokeOptions) => {
539
- return this.buildRevokeAccessTransaction(options);
540
- },
541
-
542
- /**
543
- * Register content ownership
544
- */
545
- registerContent: (ownerAddress: string, contentId: string, contentHash: string) => {
546
- return this.buildRegisterContentTransaction(ownerAddress, contentId, contentHash);
547
- },
548
-
549
- /**
550
- * Build access approval transaction
551
- */
552
- buildAccessTransaction: (userAddress: string, accessType: 'read' | 'write' = 'read') => {
553
- return this.buildAccessTransaction(userAddress, accessType);
554
- },
555
- };
556
- }
557
-
558
- // ==================== MOVE CALL BUILDERS ====================
559
-
560
- get call() {
561
- return {
562
- /**
563
- * Move call for granting access
564
- */
565
- grantAccess: (options: AccessGrantOptions): Thunk => {
566
- return async (tx) => {
567
- const grantTx = await this.buildGrantAccessTransaction(options);
568
- return grantTx;
569
- };
570
- },
571
-
572
- /**
573
- * Move call for revoking access
574
- */
575
- revokeAccess: (options: AccessRevokeOptions): Thunk => {
576
- return async (tx) => {
577
- const revokeTx = await this.buildRevokeAccessTransaction(options);
578
- return revokeTx;
579
- };
580
- },
581
- };
582
- }
583
-
584
- // ==================== ACCESS CONTROL QUERIES ====================
585
-
586
- /**
587
- * Check if a user has access to decrypt content
588
- */
589
- async hasAccess(
590
- userAddress: string,
591
- contentId: string,
592
- ownerAddress: string
593
- ): Promise<boolean> {
594
- try {
595
- if (userAddress === ownerAddress) {
596
- return true;
597
- }
598
-
599
- const tx = new Transaction();
600
- tx.moveCall({
601
- target: `${this.packageId}::seal_access_control::check_access`,
602
- arguments: [
603
- tx.pure.address(userAddress),
604
- tx.pure.string(contentId),
605
- tx.pure.address(ownerAddress),
606
- ],
607
- });
608
-
609
- const result = await this.suiClient.devInspectTransactionBlock({
610
- transactionBlock: tx,
611
- sender: userAddress,
612
- });
613
-
614
- return result.effects.status.status === 'success';
615
- } catch (error) {
616
- console.error(`Error checking access: ${error}`);
617
- return false;
618
- }
619
- }
620
-
621
- // ==================== VIEW METHODS ====================
622
-
623
- get view() {
624
- return {
625
- /**
626
- * Get access permissions for memories
627
- */
628
- getAccessPermissions: async (userAddress: string, memoryId?: string): Promise<AccessPermission[]> => {
629
- // Note: This would typically require event queries or indexing
630
- // For now, return empty array as this requires additional infrastructure
631
- console.warn('getAccessPermissions: This method requires event indexing infrastructure');
632
- return [];
633
- },
634
-
635
- /**
636
- * Check if user has access to content
637
- */
638
- hasAccess: (userAddress: string, contentId: string, ownerAddress: string) => {
639
- return this.hasAccess(userAddress, contentId, ownerAddress);
640
- },
641
- };
642
- }
643
-
644
- // ==================== UTILITY METHODS ====================
645
-
646
- /**
647
- * Compute SEAL key_id from owner address and nonce
648
- *
649
- * Mirrors the Move implementation in capability.move:
650
- * key_id = keccak256(owner || nonce)
651
- *
652
- * @param ownerAddress - Owner's Sui address
653
- * @param nonce - Nonce from MemoryCap object (32 bytes keccak hash)
654
- * @returns key_id bytes for SEAL approval (32 bytes)
655
- */
656
- computeKeyId(ownerAddress: string, nonce: Uint8Array): Uint8Array {
657
- // Convert owner address to bytes (BCS format - 32 bytes)
658
- const ownerBytes = fromHex(ownerAddress.replace('0x', '').padStart(64, '0'));
659
-
660
- // Concatenate owner || nonce (matches Move: bcs::to_bytes(&owner) + nonce)
661
- const data = new Uint8Array(ownerBytes.length + nonce.length);
662
- data.set(ownerBytes, 0);
663
- data.set(nonce, ownerBytes.length);
664
-
665
- // Use keccak256 hash (matches Move implementation exactly)
666
- const keyId = keccak_256(data);
667
-
668
- console.log(`🔑 Computed key_id from owner ${ownerAddress.slice(0, 10)}... and nonce (${nonce.length} bytes)`);
669
- return keyId;
670
- }
671
-
672
- /**
673
- * Generate content hash for verification
674
- */
675
- private async generateContentHash(data: Uint8Array): Promise<string> {
676
- // Create a new Uint8Array to ensure proper typing
677
- const dataArray = new Uint8Array(data);
678
- const hashBuffer = await crypto.subtle.digest('SHA-256', dataArray);
679
- const hashArray = Array.from(new Uint8Array(hashBuffer));
680
- return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
681
- }
682
-
683
- /**
684
- * Verify content integrity
685
- */
686
- async verifyContentHash(data: Uint8Array, expectedHash: string): Promise<boolean> {
687
- const actualHash = await this.generateContentHash(data);
688
- return actualHash === expectedHash;
689
- }
690
-
691
- /**
692
- * Check if SEAL service is available
693
- */
694
- isAvailable(): boolean {
695
- return this.sealService !== null;
696
- }
697
-
698
- /**
699
- * Get SEAL service configuration info
700
- */
701
- getClientInfo(): {
702
- isInitialized: boolean;
703
- packageId: string;
704
- encryptionEnabled: boolean;
705
- } {
706
- return {
707
- isInitialized: this.sealService !== null,
708
- packageId: this.packageId,
709
- encryptionEnabled: this.config.encryptionConfig?.enabled || false,
710
- };
711
- }
1
+ /**
2
+ * EncryptionService - SEAL-based encryption and access control
3
+ *
4
+ * Provides identity-based encryption using Mysten's SEAL SDK with decentralized
5
+ * key management and onchain access control policies.
6
+ */
7
+
8
+ import { SessionKey } from '@mysten/seal';
9
+ import { Transaction } from '@mysten/sui/transactions';
10
+ import { fromHex, toHex } from '@mysten/sui/utils';
11
+ import { keccak_256 } from '@noble/hashes/sha3.js';
12
+ import { SealService } from '../infrastructure/seal/SealService';
13
+ import { CrossContextPermissionService } from './CrossContextPermissionService';
14
+ import type {
15
+ ClientWithCoreApi,
16
+ PDWConfig,
17
+ AccessPermission,
18
+ AccessControlOptions,
19
+ Thunk,
20
+ SealEncryptionResult,
21
+ SealDecryptionOptions
22
+ } from '../types';
23
+
24
+ export interface AccessGrantOptions {
25
+ ownerAddress: string;
26
+ recipientAddress: string;
27
+ contentId: string;
28
+ accessLevel: 'read' | 'write';
29
+ expiresIn?: number; // milliseconds from now
30
+ }
31
+
32
+ export interface AccessRevokeOptions {
33
+ ownerAddress: string;
34
+ recipientAddress: string;
35
+ contentId: string;
36
+ }
37
+
38
+ export class EncryptionService {
39
+ private sealService: SealService;
40
+ private suiClient: any;
41
+ private packageId: string;
42
+ private sessionKeyCache = new Map<string, SessionKey>();
43
+ private permissionService: CrossContextPermissionService;
44
+
45
+ constructor(
46
+ private client: ClientWithCoreApi,
47
+ private config: PDWConfig
48
+ ) {
49
+ this.suiClient = (client as any).client || client;
50
+ this.packageId = config.packageId || '';
51
+ this.sealService = this.initializeSealService();
52
+
53
+ // Initialize permission service for OAuth-style access control
54
+ this.permissionService = new CrossContextPermissionService(
55
+ {
56
+ packageId: this.packageId,
57
+ accessRegistryId: config.accessRegistryId || ''
58
+ },
59
+ this.suiClient
60
+ );
61
+ }
62
+
63
+ /**
64
+ * Initialize SEAL service with proper configuration
65
+ */
66
+ private initializeSealService(): SealService {
67
+ const encryptionConfig = this.config.encryptionConfig;
68
+
69
+ if (!encryptionConfig?.enabled) {
70
+ console.warn('Encryption is disabled in configuration - creating SealService anyway');
71
+ }
72
+
73
+ // Default testnet key servers (replace with actual server object IDs)
74
+ const defaultKeyServers = [
75
+ '0x73d05d62c18d9374e3ea529e8e0ed6161da1a141a94d3f76ae3fe4e99356db75',
76
+ '0xf5d14a81a982144ae441cd7d64b09027f116a468bd36e7eca494f750591623c8'
77
+ ];
78
+
79
+ const keyServerIds = encryptionConfig?.keyServers || defaultKeyServers;
80
+
81
+ // Create SealService with proper configuration
82
+ const sealConfig = {
83
+ suiClient: this.suiClient,
84
+ packageId: this.packageId,
85
+ keyServerUrls: [], // Empty for now, URLs handled separately if needed
86
+ keyServerObjectIds: keyServerIds,
87
+ network: process.env.NODE_ENV === 'production' ? 'mainnet' as const : 'testnet' as const,
88
+ threshold: 2,
89
+ enableMetrics: true,
90
+ retryAttempts: 3,
91
+ timeoutMs: 30000
92
+ };
93
+
94
+ return new SealService(sealConfig);
95
+ }
96
+
97
+ /**
98
+ * Build access approval transaction for SEAL key servers (LEGACY)
99
+ *
100
+ * @deprecated Use buildAccessTransactionForWallet instead for wallet-based permissions
101
+ */
102
+ async buildAccessTransaction(
103
+ userAddress: string,
104
+ accessType: 'read' | 'write' = 'read'
105
+ ): Promise<Transaction> {
106
+ console.warn('buildAccessTransaction is deprecated - use buildAccessTransactionForWallet for wallet-based permissions');
107
+
108
+ return this.buildAccessTransactionForWallet(userAddress, userAddress, accessType);
109
+ }
110
+
111
+ /**
112
+ * Build access approval transaction using capability pattern
113
+ *
114
+ * Uses pdw::capability::seal_approve which validates:
115
+ * - User owns the MemoryCap object
116
+ * - Provided key_id matches computed key from owner + nonce
117
+ *
118
+ * @param keyId - SEAL key ID bytes (computed from owner + nonce)
119
+ * @param memoryCapId - MemoryCap object ID on Sui
120
+ * @returns Transaction for SEAL key server approval
121
+ */
122
+ buildCapabilityApproveTransaction(
123
+ keyId: Uint8Array,
124
+ memoryCapId: string
125
+ ): Transaction {
126
+ return this.permissionService.buildSealApproveTransaction(keyId, memoryCapId);
127
+ }
128
+
129
+ /**
130
+ * Build access approval transaction for a requesting wallet address
131
+ * Uses CrossContextPermissionService for proper permission validation
132
+ *
133
+ * @deprecated Use buildCapabilityApproveTransaction for capability-based access
134
+ * @param userAddress - User's wallet address (used as SEAL identity)
135
+ * @param requestingWallet - Wallet requesting access
136
+ * @param accessType - Access level (read/write)
137
+ * @returns Transaction for SEAL key server approval
138
+ */
139
+ async buildAccessTransactionForWallet(
140
+ userAddress: string,
141
+ requestingWallet: string,
142
+ accessType: 'read' | 'write' = 'read'
143
+ ): Promise<Transaction> {
144
+ // Legacy: Convert user address to bytes for SEAL identity
145
+ const identityBytes = fromHex(userAddress.replace('0x', ''));
146
+
147
+ // Use legacy method if memoryCapId is not available
148
+ return this.permissionService.buildSealApproveTransactionLegacy(
149
+ identityBytes,
150
+ requestingWallet
151
+ );
152
+ }
153
+
154
+ /**
155
+ * Encrypt data using SEAL via SealService
156
+ *
157
+ * @param data - Data to encrypt
158
+ * @param userAddress - User's wallet address (used as SEAL identity/id)
159
+ * @param threshold - Minimum key servers required for decryption (default: 2)
160
+ * @returns Encrypted data and backup key
161
+ */
162
+ async encrypt(
163
+ data: Uint8Array,
164
+ userAddress: string,
165
+ threshold?: number
166
+ ): Promise<{ encryptedObject: Uint8Array; backupKey: Uint8Array }> {
167
+ try {
168
+ console.log('🔒 EncryptionService: Starting SEAL encryption...');
169
+ console.log(` User address (identity): ${userAddress}`);
170
+ console.log(` Data size: ${data.length} bytes`);
171
+ console.log(` Threshold: ${threshold || 2} key servers`);
172
+
173
+ const result = await this.sealService.encryptData({
174
+ data,
175
+ id: userAddress, // Use user's address as SEAL identity
176
+ threshold: threshold || 2
177
+ });
178
+
179
+ console.log('✅ EncryptionService: SEAL encryption successful');
180
+ console.log(` Encrypted size: ${result.encryptedObject.length} bytes`);
181
+ console.log(` Backup key generated: ${result.key.length} bytes`);
182
+
183
+ return {
184
+ encryptedObject: result.encryptedObject,
185
+ backupKey: result.key
186
+ };
187
+ } catch (error) {
188
+ throw new Error(`Encryption failed: ${error}`);
189
+ }
190
+ }
191
+
192
+ /**
193
+ * Decrypt data using SEAL with session keys via SealService
194
+ * Handles both new binary format (Uint8Array) and legacy base64 format
195
+ *
196
+ * Supports two access control patterns:
197
+ * 1. Capability pattern (recommended): Requires memoryCapId and keyId
198
+ * 2. Legacy allowlist pattern: Uses requestingWallet for permission check
199
+ */
200
+ async decrypt(options: SealDecryptionOptions): Promise<Uint8Array> {
201
+ try {
202
+ console.log('🔓 EncryptionService: Starting SEAL decryption...');
203
+ console.log(` User address: ${options.userAddress}`);
204
+ const requestingWallet = options.requestingWallet ?? options.userAddress;
205
+
206
+ // Get or create session key
207
+ let activeSessionKey = options.sessionKey;
208
+ if (!activeSessionKey) {
209
+ console.log('🔄 Creating session key...');
210
+ activeSessionKey = await this.getOrCreateSessionKey(options.userAddress);
211
+ }
212
+
213
+ // Build access transaction if not provided
214
+ let txBytes = options.signedTxBytes;
215
+ if (!txBytes) {
216
+ // Check if capability pattern is being used
217
+ if (options.memoryCapId && options.keyId) {
218
+ console.log('🔄 Building capability-based access transaction...');
219
+ console.log(` MemoryCap ID: ${options.memoryCapId}`);
220
+ console.log(` Key ID length: ${options.keyId.length} bytes`);
221
+
222
+ const tx = this.buildCapabilityApproveTransaction(options.keyId, options.memoryCapId);
223
+ // CRITICAL: Set sender before building - required by Transaction.build()
224
+ tx.setSender(requestingWallet);
225
+ console.log(` Transaction sender set to: ${requestingWallet}`);
226
+ // SEAL REQUIREMENT: Must use onlyTransactionKind: true for PTB validation
227
+ txBytes = await tx.build({ client: this.suiClient, onlyTransactionKind: true });
228
+ } else {
229
+ // Fallback to legacy allowlist pattern
230
+ console.log('🔄 Building legacy access transaction (allowlist pattern)...');
231
+ console.warn(' ⚠️ Consider using capability pattern with memoryCapId and keyId');
232
+ console.log(` Requesting wallet: ${requestingWallet}`);
233
+
234
+ const tx = await this.buildAccessTransactionForWallet(
235
+ options.userAddress,
236
+ requestingWallet,
237
+ 'read'
238
+ );
239
+ // CRITICAL: Set sender before building - required by Transaction.build()
240
+ tx.setSender(requestingWallet);
241
+ console.log(` Transaction sender set to: ${requestingWallet}`);
242
+ // SEAL REQUIREMENT: Must use onlyTransactionKind: true for PTB validation
243
+ txBytes = await tx.build({ client: this.suiClient, onlyTransactionKind: true });
244
+ }
245
+ }
246
+
247
+ if (!txBytes) {
248
+ throw new Error('Failed to build SEAL approval transaction bytes');
249
+ }
250
+
251
+ // ✅ CRITICAL: Handle both binary and legacy formats
252
+ let encryptedBytes: Uint8Array;
253
+
254
+ if (options.encryptedContent && options.encryptedContent instanceof Uint8Array) {
255
+ // **NEW BINARY FORMAT** (preferred - matches memory-workflow-seal.ts)
256
+ encryptedBytes = options.encryptedContent;
257
+ console.log('✅ Using new binary format (Uint8Array)');
258
+ console.log(` Binary data size: ${encryptedBytes.length} bytes`);
259
+ console.log(` Format: Direct binary (preserves SEAL integrity)`);
260
+ } else if (options.encryptedData) {
261
+ // **LEGACY BASE64 FORMAT** (deprecated but supported for backward compatibility)
262
+ console.log('⚠️ Using legacy base64 format (deprecated)');
263
+ const encryptedDataBase64 = options.encryptedData;
264
+ const binaryString = atob(encryptedDataBase64);
265
+ encryptedBytes = new Uint8Array(binaryString.length);
266
+ for (let i = 0; i < binaryString.length; i++) {
267
+ encryptedBytes[i] = binaryString.charCodeAt(i);
268
+ }
269
+ console.log(` Converted from base64: ${encryptedDataBase64.length} chars → ${encryptedBytes.length} bytes`);
270
+ console.log(' Recommendation: Use encryptedContent (Uint8Array) for better performance');
271
+ } else {
272
+ throw new Error('No encrypted data provided. Use either encryptedContent (Uint8Array) or encryptedData (base64 string)');
273
+ }
274
+
275
+ console.log('🔄 Calling SEAL decryption...');
276
+ console.log(` Encrypted data length: ${encryptedBytes.length} bytes`);
277
+ console.log(` Session key available: ${!!activeSessionKey}`);
278
+ console.log(` Transaction bytes length: ${txBytes.length} bytes`);
279
+
280
+ // Use SealService for decryption (matches memory-workflow-seal.ts pattern)
281
+ const decryptResult = await this.sealService.decryptData({
282
+ encryptedObject: encryptedBytes,
283
+ sessionKey: activeSessionKey,
284
+ txBytes
285
+ });
286
+
287
+ console.log(`✅ EncryptionService: SEAL decryption successful`);
288
+ console.log(` Decrypted data size: ${decryptResult.length} bytes`);
289
+ console.log(` Binary integrity preserved throughout process`);
290
+
291
+ return decryptResult;
292
+ } catch (error) {
293
+ throw new Error(`Decryption failed: ${error}`);
294
+ }
295
+ }
296
+
297
+ // ==================== SESSION KEY MANAGEMENT ====================
298
+
299
+ /**
300
+ * Create a new session key for a user via SealService
301
+ *
302
+ * Two usage patterns:
303
+ * 1. Frontend: Pass signPersonalMessageFn from @mysten/dapp-kit useSignPersonalMessage hook
304
+ * 2. Backend: Pass keypair for direct signing (auto-converts format)
305
+ */
306
+ async createSessionKey(
307
+ userAddress: string,
308
+ signer?: {
309
+ // Frontend pattern: dapp-kit signPersonalMessage function
310
+ signPersonalMessageFn?: (message: string) => Promise<{ signature: string }>;
311
+ // Backend pattern: Ed25519Keypair for direct signing
312
+ keypair?: any;
313
+ }
314
+ ): Promise<SessionKey> {
315
+ try {
316
+ console.log('🔄 EncryptionService: Creating SEAL session key...');
317
+ console.log(` User address: ${userAddress}`);
318
+ console.log(` TTL: 30 minutes (maximum allowed)`);
319
+
320
+ const sessionResult = await this.sealService.createSession({
321
+ address: userAddress,
322
+ packageId: this.packageId,
323
+ ttlMin: 30, // Use 30 minutes (maximum allowed)
324
+ });
325
+
326
+ // Handle signing based on provided signer type
327
+ if (signer?.signPersonalMessageFn) {
328
+ // Frontend pattern: Use dapp-kit signPersonalMessage (RECOMMENDED)
329
+ console.log('🔄 Signing with dapp-kit signPersonalMessage (recommended)...');
330
+ const personalMessage = sessionResult.personalMessage;
331
+
332
+ // Convert to string if it's a byte array
333
+ const messageString = typeof personalMessage === 'string'
334
+ ? personalMessage
335
+ : new TextDecoder().decode(personalMessage);
336
+
337
+ console.log(` Message (first 100 chars): ${messageString.substring(0, 100)}...`);
338
+
339
+ // Use dapp-kit signPersonalMessage - returns signature in correct format
340
+ const result = await signer.signPersonalMessageFn(messageString);
341
+
342
+ console.log(` Signature from dapp-kit: ${result.signature.substring(0, 20)}...`);
343
+ console.log(` ✅ Using dapp-kit signature format (already compatible with SEAL)`);
344
+
345
+ // Set signature directly - dapp-kit returns it in SEAL-compatible format
346
+ await sessionResult.sessionKey.setPersonalMessageSignature(result.signature);
347
+ console.log('✅ Personal message signed with dapp-kit');
348
+
349
+ } else if (signer?.keypair) {
350
+ // Backend pattern: Use Ed25519Keypair with format conversion
351
+ console.log('🔄 Signing with Ed25519Keypair (backend fallback)...');
352
+ const personalMessage = sessionResult.personalMessage;
353
+
354
+ // Convert to string if it's a byte array
355
+ const messageString = typeof personalMessage === 'string'
356
+ ? personalMessage
357
+ : new TextDecoder().decode(personalMessage);
358
+
359
+ console.log(` Message (first 100 chars): ${messageString.substring(0, 100)}...`);
360
+
361
+ // Sign with keypair
362
+ const messageSignature = await signer.keypair.signPersonalMessage(new TextEncoder().encode(messageString));
363
+
364
+ // CRITICAL FIX: Use signature as-is from Ed25519Keypair (SEAL expects original format)
365
+ // According to SEAL documentation, pass signature directly from keypair.signPersonalMessage()
366
+ console.log(` ✅ Using signature as-is from Ed25519Keypair (SEAL-compatible format)`);
367
+ console.log(` Original signature: ${messageSignature.signature.substring(0, 20)}...`);
368
+
369
+ // Set signature exactly as returned by keypair (no conversion needed)
370
+ await sessionResult.sessionKey.setPersonalMessageSignature(messageSignature.signature);
371
+ console.log('✅ Personal message signed with Ed25519Keypair');
372
+
373
+ } else {
374
+ console.log('⚠️ No signer provided - session key created but not signed');
375
+ console.log(' Note: Call setPersonalMessageSignature() later with wallet-signed message');
376
+ console.log(' Frontend: Use dapp-kit useSignPersonalMessage hook');
377
+ console.log(' Backend: Provide Ed25519Keypair');
378
+ }
379
+
380
+ // Cache the session key
381
+ this.sessionKeyCache.set(userAddress, sessionResult.sessionKey);
382
+
383
+ console.log('✅ EncryptionService: Session key created and cached');
384
+ return sessionResult.sessionKey;
385
+ } catch (error) {
386
+ throw new Error(`Failed to create session key: ${error}`);
387
+ }
388
+ }
389
+
390
+ /**
391
+ * Get cached session key or create new one
392
+ */
393
+ async getOrCreateSessionKey(userAddress: string): Promise<SessionKey> {
394
+ const cached = this.sessionKeyCache.get(userAddress);
395
+ if (cached) {
396
+ return cached;
397
+ }
398
+
399
+ return this.createSessionKey(userAddress);
400
+ }
401
+
402
+ /**
403
+ * Export session key for persistence
404
+ */
405
+ async exportSessionKey(sessionKey: SessionKey): Promise<string> {
406
+ try {
407
+ const exported = sessionKey.export();
408
+ return JSON.stringify(exported);
409
+ } catch (error) {
410
+ throw new Error(`Failed to export session key: ${error}`);
411
+ }
412
+ }
413
+
414
+ /**
415
+ * Import previously exported session key
416
+ */
417
+ async importSessionKey(exportedKey: string, userAddress?: string): Promise<SessionKey> {
418
+ try {
419
+ const keyData = JSON.parse(exportedKey);
420
+ const sessionKey = SessionKey.import(keyData, this.suiClient);
421
+
422
+ if (userAddress) {
423
+ this.sessionKeyCache.set(userAddress, sessionKey);
424
+ }
425
+
426
+ return sessionKey;
427
+ } catch (error) {
428
+ throw new Error(`Failed to import session key: ${error}`);
429
+ }
430
+ }
431
+
432
+ // ==================== ACCESS CONTROL TRANSACTIONS ====================
433
+
434
+ /**
435
+ * Create SEAL approval transaction bytes (matches memory-workflow-seal.ts pattern)
436
+ * Returns raw PTB format bytes for SEAL verification
437
+ */
438
+ async createSealApproveTransaction(userAddress: string, contentOwner: string): Promise<Uint8Array> {
439
+ try {
440
+ console.log('🔄 EncryptionService: Creating SEAL approval transaction...');
441
+ console.log(` User address: ${userAddress}`);
442
+ console.log(` Content owner: ${contentOwner}`);
443
+
444
+ // Create the approval transaction (not signed, just bytes)
445
+ const tx = await this.buildAccessTransactionForWallet(userAddress, contentOwner, 'read');
446
+ // CRITICAL: Set sender before building - required by Transaction.build()
447
+ tx.setSender(contentOwner);
448
+ // SEAL REQUIREMENT: Must use onlyTransactionKind: true for PTB validation
449
+ const txBytes = await tx.build({ client: this.suiClient, onlyTransactionKind: true });
450
+
451
+ console.log(`✅ Created SEAL approval transaction bytes (${txBytes.length} bytes)`);
452
+ console.log(' Format: Raw PTB (Programmable Transaction Block) for SEAL verification');
453
+
454
+ return txBytes;
455
+ } catch (error) {
456
+ throw new Error(`Failed to create SEAL approval transaction: ${error}`);
457
+ }
458
+ }
459
+
460
+ /**
461
+ * Build transaction to grant access to another user
462
+ */
463
+ async buildGrantAccessTransaction(options: AccessGrantOptions): Promise<Transaction> {
464
+ const { ownerAddress, recipientAddress, contentId, accessLevel, expiresIn } = options;
465
+ const tx = new Transaction();
466
+
467
+ const expiresAt = expiresIn ? Date.now() + expiresIn : Date.now() + 86400000; // 24h default
468
+
469
+ tx.moveCall({
470
+ target: `${this.packageId}::capability::grant_access`,
471
+ arguments: [
472
+ tx.pure.address(ownerAddress),
473
+ tx.pure.address(recipientAddress),
474
+ tx.pure.string(contentId),
475
+ tx.pure.string(accessLevel),
476
+ tx.pure.u64(expiresAt),
477
+ ],
478
+ });
479
+
480
+ return tx;
481
+ }
482
+
483
+ /**
484
+ * Build transaction to revoke access from a user
485
+ */
486
+ async buildRevokeAccessTransaction(options: AccessRevokeOptions): Promise<Transaction> {
487
+ const { ownerAddress, recipientAddress, contentId } = options;
488
+ const tx = new Transaction();
489
+
490
+ tx.moveCall({
491
+ target: `${this.packageId}::capability::revoke_access`,
492
+ arguments: [
493
+ tx.pure.address(ownerAddress),
494
+ tx.pure.address(recipientAddress),
495
+ tx.pure.string(contentId),
496
+ ],
497
+ });
498
+
499
+ return tx;
500
+ }
501
+
502
+ /**
503
+ * Build transaction to register content ownership
504
+ */
505
+ async buildRegisterContentTransaction(
506
+ ownerAddress: string,
507
+ contentId: string,
508
+ contentHash: string
509
+ ): Promise<Transaction> {
510
+ const tx = new Transaction();
511
+
512
+ tx.moveCall({
513
+ target: `${this.packageId}::capability::register_content`,
514
+ arguments: [
515
+ tx.pure.address(ownerAddress),
516
+ tx.pure.string(contentId),
517
+ tx.pure.string(contentHash),
518
+ tx.pure.string(''), // encryption_info
519
+ ],
520
+ });
521
+
522
+ return tx;
523
+ }
524
+
525
+ // ==================== TRANSACTION BUILDERS ====================
526
+
527
+ get tx() {
528
+ return {
529
+ /**
530
+ * Grant access to encrypted memory
531
+ */
532
+ grantAccess: (options: AccessGrantOptions) => {
533
+ return this.buildGrantAccessTransaction(options);
534
+ },
535
+
536
+ /**
537
+ * Revoke access to encrypted memory
538
+ */
539
+ revokeAccess: (options: AccessRevokeOptions) => {
540
+ return this.buildRevokeAccessTransaction(options);
541
+ },
542
+
543
+ /**
544
+ * Register content ownership
545
+ */
546
+ registerContent: (ownerAddress: string, contentId: string, contentHash: string) => {
547
+ return this.buildRegisterContentTransaction(ownerAddress, contentId, contentHash);
548
+ },
549
+
550
+ /**
551
+ * Build access approval transaction
552
+ */
553
+ buildAccessTransaction: (userAddress: string, accessType: 'read' | 'write' = 'read') => {
554
+ return this.buildAccessTransaction(userAddress, accessType);
555
+ },
556
+ };
557
+ }
558
+
559
+ // ==================== MOVE CALL BUILDERS ====================
560
+
561
+ get call() {
562
+ return {
563
+ /**
564
+ * Move call for granting access
565
+ */
566
+ grantAccess: (options: AccessGrantOptions): Thunk => {
567
+ return async (tx) => {
568
+ const grantTx = await this.buildGrantAccessTransaction(options);
569
+ return grantTx;
570
+ };
571
+ },
572
+
573
+ /**
574
+ * Move call for revoking access
575
+ */
576
+ revokeAccess: (options: AccessRevokeOptions): Thunk => {
577
+ return async (tx) => {
578
+ const revokeTx = await this.buildRevokeAccessTransaction(options);
579
+ return revokeTx;
580
+ };
581
+ },
582
+ };
583
+ }
584
+
585
+ // ==================== ACCESS CONTROL QUERIES ====================
586
+
587
+ /**
588
+ * Check if a user has access to decrypt content
589
+ */
590
+ async hasAccess(
591
+ userAddress: string,
592
+ contentId: string,
593
+ ownerAddress: string
594
+ ): Promise<boolean> {
595
+ try {
596
+ if (userAddress === ownerAddress) {
597
+ return true;
598
+ }
599
+
600
+ const tx = new Transaction();
601
+ tx.moveCall({
602
+ target: `${this.packageId}::capability::check_access`,
603
+ arguments: [
604
+ tx.pure.address(userAddress),
605
+ tx.pure.string(contentId),
606
+ tx.pure.address(ownerAddress),
607
+ ],
608
+ });
609
+
610
+ const result = await this.suiClient.devInspectTransactionBlock({
611
+ transactionBlock: tx,
612
+ sender: userAddress,
613
+ });
614
+
615
+ return result.effects.status.status === 'success';
616
+ } catch (error) {
617
+ console.error(`Error checking access: ${error}`);
618
+ return false;
619
+ }
620
+ }
621
+
622
+ // ==================== VIEW METHODS ====================
623
+
624
+ get view() {
625
+ return {
626
+ /**
627
+ * Get access permissions for memories
628
+ */
629
+ getAccessPermissions: async (userAddress: string, memoryId?: string): Promise<AccessPermission[]> => {
630
+ // Note: This would typically require event queries or indexing
631
+ // For now, return empty array as this requires additional infrastructure
632
+ console.warn('getAccessPermissions: This method requires event indexing infrastructure');
633
+ return [];
634
+ },
635
+
636
+ /**
637
+ * Check if user has access to content
638
+ */
639
+ hasAccess: (userAddress: string, contentId: string, ownerAddress: string) => {
640
+ return this.hasAccess(userAddress, contentId, ownerAddress);
641
+ },
642
+ };
643
+ }
644
+
645
+ // ==================== UTILITY METHODS ====================
646
+
647
+ /**
648
+ * Compute SEAL key_id from owner address and nonce
649
+ *
650
+ * Mirrors the Move implementation in capability.move:
651
+ * key_id = keccak256(owner || nonce)
652
+ *
653
+ * @param ownerAddress - Owner's Sui address
654
+ * @param nonce - Nonce from MemoryCap object (32 bytes keccak hash)
655
+ * @returns key_id bytes for SEAL approval (32 bytes)
656
+ */
657
+ computeKeyId(ownerAddress: string, nonce: Uint8Array): Uint8Array {
658
+ // Convert owner address to bytes (BCS format - 32 bytes)
659
+ const ownerBytes = fromHex(ownerAddress.replace('0x', '').padStart(64, '0'));
660
+
661
+ // Concatenate owner || nonce (matches Move: bcs::to_bytes(&owner) + nonce)
662
+ const data = new Uint8Array(ownerBytes.length + nonce.length);
663
+ data.set(ownerBytes, 0);
664
+ data.set(nonce, ownerBytes.length);
665
+
666
+ // Use keccak256 hash (matches Move implementation exactly)
667
+ const keyId = keccak_256(data);
668
+
669
+ console.log(`🔑 Computed key_id from owner ${ownerAddress.slice(0, 10)}... and nonce (${nonce.length} bytes)`);
670
+ return keyId;
671
+ }
672
+
673
+ /**
674
+ * Generate content hash for verification
675
+ */
676
+ private async generateContentHash(data: Uint8Array): Promise<string> {
677
+ // Create a new Uint8Array to ensure proper typing
678
+ const dataArray = new Uint8Array(data);
679
+ const hashBuffer = await crypto.subtle.digest('SHA-256', dataArray);
680
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
681
+ return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
682
+ }
683
+
684
+ /**
685
+ * Verify content integrity
686
+ */
687
+ async verifyContentHash(data: Uint8Array, expectedHash: string): Promise<boolean> {
688
+ const actualHash = await this.generateContentHash(data);
689
+ return actualHash === expectedHash;
690
+ }
691
+
692
+ /**
693
+ * Check if SEAL service is available
694
+ */
695
+ isAvailable(): boolean {
696
+ return this.sealService !== null;
697
+ }
698
+
699
+ /**
700
+ * Get SEAL service configuration info
701
+ */
702
+ getClientInfo(): {
703
+ isInitialized: boolean;
704
+ packageId: string;
705
+ encryptionEnabled: boolean;
706
+ } {
707
+ return {
708
+ isInitialized: this.sealService !== null,
709
+ packageId: this.packageId,
710
+ encryptionEnabled: this.config.encryptionConfig?.enabled || false,
711
+ };
712
+ }
712
713
  }