@@ -14,9 +14,7 @@ import type {
14
14
Did ,
15
15
HexString ,
16
16
} from '@kiltprotocol/types'
17
- import type { ApiPromise } from '@polkadot/api'
18
17
import type { GenericCall , Option } from '@polkadot/types'
19
- import type { Call } from '@polkadot/types/interfaces'
20
18
import type { BN } from '@polkadot/util'
21
19
import type {
22
20
PublicCredentialsCredentialsCredential ,
@@ -125,28 +123,6 @@ export function fromChain(
125
123
}
126
124
127
125
// Given a (nested) call, flattens them and filter by calls that are of type `api.tx.publicCredentials.add`.
128
- function extractPublicCredentialCreationCallsFromDidCall (
129
- api : ApiPromise ,
130
- call : Call
131
- ) : Array < GenericCall < typeof api . tx . publicCredentials . add . args > > {
132
- const extrinsicCalls = Blockchain . flattenCalls ( call , api )
133
- return extrinsicCalls . filter (
134
- ( c ) : c is GenericCall < typeof api . tx . publicCredentials . add . args > =>
135
- api . tx . publicCredentials . add . is ( c )
136
- )
137
- }
138
-
139
- // Given a (nested) call, flattens them and filter by calls that are of type `api.tx.did.submitDidCall`.
140
- function extractDidCallsFromBatchCall (
141
- api : ApiPromise ,
142
- call : Call
143
- ) : Array < GenericCall < typeof api . tx . did . submitDidCall . args > > {
144
- const extrinsicCalls = Blockchain . flattenCalls ( call , api )
145
- return extrinsicCalls . filter (
146
- ( c ) : c is GenericCall < typeof api . tx . did . submitDidCall . args > =>
147
- api . tx . did . submitDidCall . is ( c )
148
- )
149
- }
150
126
151
127
/**
152
128
* Retrieves from the blockchain the {@link IPublicCredential} that is identified by the provided identifier.
@@ -164,14 +140,19 @@ export async function fetchCredentialFromChain(
164
140
const publicCredentialEntry = await api . call . publicCredentials . getById (
165
141
credentialId
166
142
)
167
- const { blockNumber, revoked } = publicCredentialEntry . unwrap ( )
143
+ const {
144
+ blockNumber,
145
+ revoked,
146
+ attester : attesterId ,
147
+ } = publicCredentialEntry . unwrap ( )
148
+ const attester = didFromChain ( attesterId )
168
149
169
150
const extrinsic = await Blockchain . retrieveExtrinsicFromBlock (
170
151
blockNumber ,
171
152
( { events } ) =>
172
153
events . some (
173
154
( event ) =>
174
- api . events . publicCredentials . CredentialStored . is ( event ) &&
155
+ api . events . publicCredentials ? .CredentialStored ? .is ( event ) &&
175
156
event . data [ 1 ] . toString ( ) === credentialId
176
157
) ,
177
158
api
@@ -183,58 +164,40 @@ export async function fetchCredentialFromChain(
183
164
)
184
165
}
185
166
186
- if (
187
- ! Blockchain . isBatch ( extrinsic , api ) &&
188
- ! api . tx . did . submitDidCall . is ( extrinsic )
189
- ) {
190
- throw new SDKErrors . PublicCredentialError (
191
- 'Extrinsic should be either a `did.submitDidCall` extrinsic or a batch with at least a `did.submitDidCall` extrinsic'
192
- )
193
- }
194
-
195
- // If we're dealing with a batch, flatten any nested `submit_did_call` calls,
196
- // otherwise the extrinsic is itself a submit_did_call, so just take it.
197
- const didCalls = Blockchain . isBatch ( extrinsic , api )
198
- ? extrinsic . args [ 0 ] . flatMap ( ( batchCall ) =>
199
- extractDidCallsFromBatchCall ( api , batchCall )
200
- )
201
- : [ extrinsic ]
202
-
203
- // From the list of DID calls, only consider public_credentials::add calls, bundling each of them with their DID submitter.
204
- // It returns a list of [reconstructedCredential, attesterDid].
205
- const callCredentialsContent = didCalls . flatMap ( ( didCall ) => {
206
- const publicCredentialCalls =
207
- extractPublicCredentialCreationCallsFromDidCall ( api , didCall . args [ 0 ] . call )
208
- // Re-create the issued public credential for each call identified.
209
- return publicCredentialCalls . map (
210
- ( credentialCreationCall ) =>
211
- [
212
- credentialInputFromChain ( credentialCreationCall . args [ 0 ] ) ,
213
- didFromChain ( didCall . args [ 0 ] . did ) ,
214
- ] as const
215
- )
216
- } )
167
+ // Unpack any nested calls, e.g., within a batch or `submit_did_call`
168
+ const extrinsicCalls = Blockchain . flattenCalls ( extrinsic , api )
217
169
218
- // If more than one call is present, it always considers the last one as the valid one, and takes its attester.
219
- const lastRightCredentialCreationCall = callCredentialsContent
220
- . reverse ( )
221
- . find ( ( [ credential , attester ] ) => {
222
- const reconstructedId = getIdForCredential ( credential , attester )
223
- return reconstructedId === credentialId
224
- } )
170
+ // only consider public_credentials::add calls
171
+ const publicCredentialCalls = extrinsicCalls . filter (
172
+ ( c ) : c is GenericCall < typeof api . tx . publicCredentials . add . args > =>
173
+ api . tx . publicCredentials ?. add ?. is ( c )
174
+ )
225
175
226
- if ( ! lastRightCredentialCreationCall ) {
176
+ // Re-create the issued public credential for each call identified to find the credential with the id we're looking for
177
+ const credentialInput = publicCredentialCalls . reduceRight <
178
+ IPublicCredentialInput | undefined
179
+ > ( ( selectedCredential , credentialCreationCall ) => {
180
+ if ( selectedCredential ) {
181
+ return selectedCredential
182
+ }
183
+ const credential = credentialInputFromChain ( credentialCreationCall . args [ 0 ] )
184
+ const reconstructedId = getIdForCredential ( credential , attester )
185
+ if ( reconstructedId === credentialId ) {
186
+ return credential
187
+ }
188
+ return undefined
189
+ } , undefined )
190
+
191
+ if ( typeof credentialInput === 'undefined' ) {
227
192
throw new SDKErrors . PublicCredentialError (
228
193
'Block should always contain the full credential, eventually.'
229
194
)
230
195
}
231
196
232
- const [ credentialInput , attester ] = lastRightCredentialCreationCall
233
-
234
197
return {
235
198
...credentialInput ,
236
199
attester,
237
- id : getIdForCredential ( credentialInput , attester ) ,
200
+ id : credentialId ,
238
201
blockNumber,
239
202
revoked : revoked . toPrimitive ( ) ,
240
203
}
0 commit comments