Skip to content

Commit

Permalink
feat: validate CddId when adding a Cdd claim
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Jan 20, 2021
1 parent 4be6655 commit 4235f8f
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/api/procedures/modifyClaims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,44 @@ export async function prepareModifyClaims(
modifyClaimArgs.map(([identityId, claim]) => tuple(identityId, claim))
);
} else {
if (operation === ClaimOperation.Add) {
const existingCddId: string[] = [];

const claimTargets = claims.filter(
({ claim: { type } }) => type === ClaimType.CustomerDueDiligence
);
const issuedCddClaims = await context.issuedClaims({
targets: claimTargets.map(({ target }) => target),
claimTypes: [ClaimType.CustomerDueDiligence],
includeExpired: false,
});

claimTargets.map(({ target, claim }) => {
const did = signerToString(target);
const filterCdds = issuedCddClaims.data.filter(
({ target: issuedTarget }) => issuedTarget.did === did
);

// we know the claim is a CDD claim, so it must have an id property
const { id } = filterCdds[0].claim as { id: string };
const { id: cddId } = claim as { id: string };

if (id !== cddId) {
existingCddId.push(id);
}
});

if (existingCddId.length) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'This Identity already has CDD claims with a different ID',
data: {
existingCddId: uniq(existingCddId),
},
});
}
}

this.addBatchTransaction(identity.addClaim, { groupByFn: groupByDid }, modifyClaimArgs);
}
}
Expand Down

0 comments on commit 4235f8f

Please sign in to comment.