@@ -23,6 +23,16 @@ import { getConfiguration } from '../../../utils/config.js'
2323import { isRemoteDDO } from '../../core/utils/validateDdoHandler.js'
2424
2525export class MetadataEventProcessor extends BaseEventProcessor {
26+ private isDDO ( data : any ) : data is Record < string , any > {
27+ return (
28+ data &&
29+ typeof data === 'object' &&
30+ ! Array . isArray ( data ) &&
31+ typeof data . id === 'string' &&
32+ typeof data . version === 'string'
33+ )
34+ }
35+
2636 async processEvent (
2737 event : ethers . Log ,
2838 chainId : number ,
@@ -122,25 +132,57 @@ export class MetadataEventProcessor extends BaseEventProcessor {
122132 metadataHash ,
123133 metadata
124134 )
135+ const isRemoteMetadata = isRemoteDDO ( decryptDDO )
136+ const isEncryptedMetadata = ( parseInt ( flag ) & 2 ) !== 0
125137 let ddo = await this . processDDO ( decryptDDO )
126- if (
127- ! isRemoteDDO ( decryptDDO ) &&
128- parseInt ( flag ) !== 2 &&
129- ! this . checkDdoHash ( ddo , metadataHash )
130- ) {
138+ if ( ! isEncryptedMetadata && ! this . checkDdoHash ( ddo , metadataHash ) ) {
131139 return
132140 }
133141 if ( ddo . encryptedData ) {
142+ let { encryptedData } = ddo
143+ if ( ( parseInt ( flag ) & 2 ) !== 0 ) {
144+ try {
145+ const decryptedIpfsPayload = await this . decryptDDO (
146+ decodedEventData . args [ 2 ] ,
147+ flag ,
148+ owner ,
149+ event . address ,
150+ chainId ,
151+ '' ,
152+ '' ,
153+ ddo . encryptedData
154+ )
155+ encryptedData = decryptedIpfsPayload . encryptedData || encryptedData
156+ } catch ( error ) {
157+ INDEXER_LOGGER . log (
158+ LOG_LEVELS_STR . LEVEL_ERROR ,
159+ `Unable to decrypt encrypted IPFS DDO payload, trying plaintext payload fallback: ${
160+ error instanceof Error ? error . message : String ( error )
161+ } `
162+ )
163+ }
164+ }
165+
134166 const proof = await this . decryptDDOIPFS (
135167 decodedEventData . args [ 2 ] ,
136168 owner ,
137- ddo . encryptedData
169+ encryptedData
138170 )
139- const data = this . getDataFromProof ( proof )
140- const ddoInstance = DDOManager . getDDOClass ( data . ddoObj )
141- ddo = ddoInstance . updateFields ( {
142- proof : { signature : data . signature , header : data . header }
143- } )
171+ const data = typeof proof === 'string' ? this . getDataFromProof ( proof ) : null
172+
173+ const ddoObj = data ?. ddoObj || ( this . isDDO ( proof ) ? proof : null )
174+ if ( ! ddoObj ) {
175+ throw new Error (
176+ 'IPFS encryptedData payload is neither a DDO nor a supported DDO proof.'
177+ )
178+ }
179+ const ddoInstance = DDOManager . getDDOClass ( ddoObj )
180+ ddo =
181+ data ?. signature && data ?. header
182+ ? ddoInstance . updateFields ( {
183+ proof : { signature : data . signature , header : data . header }
184+ } )
185+ : ddoInstance . getDDOData ( )
144186 }
145187 const clonedDdo = structuredClone ( ddo )
146188 const updatedDdo = deleteIndexedMetadataIfExists ( clonedDdo )
@@ -159,8 +201,12 @@ export class MetadataEventProcessor extends BaseEventProcessor {
159201 )
160202 return
161203 }
162- // for unencrypted DDOs
163- if ( ( parseInt ( flag ) & 2 ) === 0 && ! this . checkDdoHash ( updatedDdo , metadataHash ) ) {
204+ // for unencrypted inline DDOs
205+ if (
206+ ! isRemoteMetadata &&
207+ ! isEncryptedMetadata &&
208+ ! this . checkDdoHash ( updatedDdo , metadataHash )
209+ ) {
164210 INDEXER_LOGGER . error ( 'Unencrypted DDO hash does not match metadata hash.' )
165211 await ddoState . update (
166212 this . networkId ,
0 commit comments