Skip to content

Commit

Permalink
Create util module and move batch check in there
Browse files Browse the repository at this point in the history
  • Loading branch information
ntn-x2 committed Dec 16, 2022
1 parent 80cacb5 commit 10c4a93
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
7 changes: 2 additions & 5 deletions packages/core/src/publicCredential/PublicCredential.chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { validateUri } from '@kiltprotocol/asset-did'
import { SDKErrors } from '@kiltprotocol/utils'

import { getIdForCredential } from './PublicCredential.js'
import isBatch from '../utils.js'

export interface EncodedPublicCredential {
ctypeHash: CTypeHash
Expand Down Expand Up @@ -199,11 +200,7 @@ export async function credentialFromChain(
submitter = extrinsicDidOrigin
}
// ... or a utility::{batch,batch_all,force_batch} extrinsic which include a DID-authorized call
else if (
api.tx.utility.batch.is(extrinsic) ||
api.tx.utility.batchAll.is(extrinsic) ||
api.tx.utility.forceBatch.is(extrinsic)
) {
else if (isBatch(extrinsic)) {
// From the batch, only consider did::submit_did calls
const didCalls = extrinsic.args[0].flatMap((batchCall) =>
extractDidCallsFromBatchCall(api, batchCall)
Expand Down
34 changes: 34 additions & 0 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (c) 2018-2022, BOTLabs GmbH.
*
* This source code is licensed under the BSD 4-Clause "Original" license
* found in the LICENSE file in the root directory of this source tree.
*/

import type { Extrinsic } from '@polkadot/types/interfaces'

import { ConfigService } from '@kiltprotocol/config'
import { GenericExtrinsic } from '@polkadot/types'

/**
* Checks wheather the provided extrinsic is one of the potential batch extrinsics.
*
* @param extrinsic The input [[Extrinsic]].
*
* @returns True if the extrinsic is a batch extrinsic, false otherwise.
*/
export function isBatch(
extrinsic: Extrinsic
): extrinsic is GenericExtrinsic<
| typeof api.tx.utility.batch.args
| typeof api.tx.utility.batchAll.args
| typeof api.tx.utility.forceBatch.args
> {
const api = ConfigService.get('api')

return (
api.tx.utility.batch.is(extrinsic) ||
api.tx.utility.batchAll.is(extrinsic) ||
api.tx.utility.forceBatch.is(extrinsic)
)
}

0 comments on commit 10c4a93

Please sign in to comment.