Skip to content

Commit

Permalink
fix(claims): dont reregister onchain claim
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiter committed Jul 22, 2022
1 parent b5822eb commit ebd46c0
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 37 deletions.
48 changes: 44 additions & 4 deletions e2e/claims.service.e2e.ts
Expand Up @@ -371,7 +371,7 @@ describe('Сlaim tests', () => {
namespace: claimType,
version: version.toString(),
},
issuerFields,
issuerFields: issuerFields || [],
});
expect(vpObject.verifiableCredential[0].issuer).toEqual(
signerService.didHex
Expand Down Expand Up @@ -415,7 +415,9 @@ describe('Сlaim tests', () => {
expect(requester).toEqual(requesterDID);
expect(claimIssuer).toEqual([issuerDID]);

if (registrationTypes.includes(RegistrationTypes.OnChain)) {
if (
registrationTypes.includes(RegistrationTypes.OnChain)
) {
expect(onChainProof).toHaveLength(132);

if (expirationTimestamp || roleDefinitionValidityPeriod) {
Expand Down Expand Up @@ -691,8 +693,6 @@ describe('Сlaim tests', () => {
};

test('should be able to issue and publish onchain', async () => {
mockGetClaimsBySubject.mockImplementationOnce(() => [role1Claim]); // to verify requesting

await enrolAndIssue(rootOwner, staticIssuer, {
subjectDID: rootOwnerDID,
claimType,
Expand Down Expand Up @@ -747,6 +747,46 @@ describe('Сlaim tests', () => {
await claimsService.hasOnChainRole(rootOwnerDID, claimType, version)
).toBe(false);
});

test('should be able to issue when role registered onchain', async () => {
await enrolAndIssue(rootOwner, staticIssuer, {
subjectDID: rootOwnerDID,
claimType,
registrationTypes: [
RegistrationTypes.OffChain,
RegistrationTypes.OnChain,
],
publishOnChain: true,
});
expect(
await claimsService.hasOnChainRole(rootOwnerDID, claimType, version)
).toBe(true);

await enrolAndIssue(rootOwner, staticIssuer, {
subjectDID: rootOwnerDID,
claimType,
registrationTypes: [
RegistrationTypes.OffChain,
RegistrationTypes.OnChain,
],
publishOnChain: true,
});
});

test('should be able to issue without publishing onchain', async () => {
await enrolAndIssue(rootOwner, staticIssuer, {
subjectDID: rootOwnerDID,
claimType,
registrationTypes: [
RegistrationTypes.OffChain,
RegistrationTypes.OnChain,
],
publishOnChain: false,
});
expect(
await claimsService.hasOnChainRole(rootOwnerDID, claimType, version)
).toBe(false);
});
});

test('should issue claim request with additional issuer fields', async () => {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 24 additions & 23 deletions src/modules/claims/claims.service.ts
Expand Up @@ -7,9 +7,7 @@ import {
PreconditionType,
RoleCredentialSubject,
} from '@energyweb/credential-governance';
import {
VerifiableCredential,
} from '@ew-did-registry/credentials-interface';
import { VerifiableCredential } from '@ew-did-registry/credentials-interface';
import { ClaimRevocation } from '@energyweb/onchain-claims';
import { Methods } from '@ew-did-registry/did';
import { Algorithms } from '@ew-did-registry/jwt';
Expand Down Expand Up @@ -80,6 +78,7 @@ import { compareDID, isValidDID } from '../../utils/did';
import { readyToBeRegisteredOnchain } from './claims.types';
import { VerifiableCredentialsServiceBase } from '../verifiable-credentials';
import { StatusListEntryVerification } from '@ew-did-registry/revocation';
import { getLogger } from '../../config';

const {
id,
Expand Down Expand Up @@ -453,13 +452,13 @@ export class ClaimsService {
expiry,
sub
);
if (!subjectAgreement) {
throw new Error(
ERROR_MESSAGES.ONCHAIN_ROLE_SUBJECT_AGREEMENT_NOT_SPECIFIED
);
}
message.onChainProof = onChainProof;
if (publishOnChain) {
if (!subjectAgreement) {
throw new Error(
ERROR_MESSAGES.ONCHAIN_ROLE_SUBJECT_AGREEMENT_NOT_SPECIFIED
);
}
await this.registerOnchain({
token,
subjectAgreement,
Expand Down Expand Up @@ -524,20 +523,6 @@ export class ClaimsService {
if (claim.token) {
claim = { ...claim, ...this.extractClaimRequest(claim.token) };
}

if (
!claim.subjectAgreement &&
claim.subject === this._signerService.did &&
claim.claimType &&
claim.claimTypeVersion
) {
claim.subjectAgreement = await this.approveRolePublishing({
subject: this._signerService.did,
role: claim.claimType as string,
version: +claim.claimTypeVersion,
});
}

if (!readyToBeRegisteredOnchain(claim)) {
throw new Error(ERROR_MESSAGES.CLAIM_WAS_NOT_ISSUED);
}
Expand All @@ -546,10 +531,26 @@ export class ClaimsService {
claimTypeVersion,
claimType,
acceptedBy,
subjectAgreement,
onChainProof,
expirationTimestamp,
} = claim;
let { subjectAgreement } = claim;

if (await this.hasOnChainRole(subject, claimType, +claimTypeVersion)) {
getLogger().warn(
`[ClaimsService]: ${claimType} already registered for ${subject}`
);
return;
}

if (!subjectAgreement && subject === this._signerService.did) {
subjectAgreement = await this.approveRolePublishing({
subject: this._signerService.did,
role: claimType,
version: +claimTypeVersion,
});
}

const expiry = expirationTimestamp || eternityTimestamp;
const data = this._claimManagerInterface.encodeFunctionData('register', [
addressOf(subject),
Expand Down
10 changes: 2 additions & 8 deletions src/modules/claims/claims.types.ts
Expand Up @@ -76,13 +76,8 @@ export const readyToBeRegisteredOnchain = (
): claim is Required<
Pick<
Claim,
| 'claimType'
| 'claimTypeVersion'
| 'subject'
| 'onChainProof'
| 'acceptedBy'
| 'subjectAgreement'
> & { expirationTimestamp?: number }
'claimType' | 'claimTypeVersion' | 'subject' | 'onChainProof' | 'acceptedBy'
> & { expirationTimestamp?: number; subjectAgreement?: string }
> => {
if (!claim) return false;
if (typeof claim !== 'object') return false;
Expand All @@ -92,7 +87,6 @@ export const readyToBeRegisteredOnchain = (
'subject',
'onChainProof',
'acceptedBy',
'subjectAgreement',
];
const claimProps = Object.keys(claim);

Expand Down

0 comments on commit ebd46c0

Please sign in to comment.