Skip to content

Commit

Permalink
feat(claims): register onchain claim optionally
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiter committed Dec 3, 2021
1 parent b355459 commit c7eb237
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 45 deletions.
Expand Up @@ -340,13 +340,17 @@ ___

**issueClaimRequest**(`__namedParameters`): `Promise`<`void`\>

Issue a claim request by signing both off-chain and on-chain request and persisting result to the cache-server.
Optionally, issue on-chain role can be submitted to the ClaimManager contract as well.

#### Parameters

| Name | Type |
| :------ | :------ |
| `__namedParameters` | `Object` |
| `__namedParameters.id` | `string` |
| `__namedParameters.issuerFields?` | { `key`: `string` ; `value`: `string` \| `number` }[] |
| `__namedParameters.publishOnChain?` | `boolean` |
| `__namedParameters.registrationTypes` | [`RegistrationTypes`](../enums/modules_claims_claims_types.RegistrationTypes.md)[] |
| `__namedParameters.requester` | `string` |
| `__namedParameters.subjectAgreement` | `string` |
Expand Down Expand Up @@ -391,7 +395,7 @@ ___

| Name | Type |
| :------ | :------ |
| `claim` | [`Claim`](../interfaces/modules_claims_claims_types.Claim.md) |
| `claim` | `Pick`<[`Claim`](../interfaces/modules_claims_claims_types.Claim.md), ``"token"`` \| ``"subjectAgreement"`` \| ``"onChainProof"``\> |

#### Returns

Expand Down
4 changes: 2 additions & 2 deletions docs/api/modules/modules_claims_claims_types.md
Expand Up @@ -105,7 +105,7 @@ ___

### readyToBeRegisteredOnchain

`Const` **readyToBeRegisteredOnchain**(`claim`): claim is Required<Pick<Claim, "id" \| "isAccepted" \| "token" \| "claimTypeVersion" \| "claimType" \| "subject" \| "subjectAgreement" \| "onChainProof"\>\>
`Const` **readyToBeRegisteredOnchain**(`claim`): claim is Required<Pick<Claim, "token" \| "subjectAgreement" \| "onChainProof"\>\>

#### Parameters

Expand All @@ -115,4 +115,4 @@ ___

#### Returns

claim is Required<Pick<Claim, "id" \| "isAccepted" \| "token" \| "claimTypeVersion" \| "claimType" \| "subject" \| "subjectAgreement" \| "onChainProof"\>\>
claim is Required<Pick<Claim, "token" \| "subjectAgreement" \| "onChainProof"\>\>
5 changes: 3 additions & 2 deletions e2e/claims.service.e2e.ts
Expand Up @@ -147,7 +147,8 @@ describe("Enrollment claim tests", () => {
subjectDID,
claimType,
registrationTypes = [RegistrationTypes.OnChain],
}: { subjectDID: string; claimType: string; registrationTypes?: RegistrationTypes[] },
publishOnChain = true,
}: { subjectDID: string; claimType: string; registrationTypes?: RegistrationTypes[]; publishOnChain?: boolean },
) {
await signerService.connect(requestSigner, ProviderType.PrivateKey);
const requesterDID = signerService.did;
Expand All @@ -160,7 +161,7 @@ describe("Enrollment claim tests", () => {

await signerService.connect(issueSigner, ProviderType.PrivateKey);
const issuerDID = signerService.did;
await claimsService.issueClaimRequest({ ...message });
await claimsService.issueClaimRequest({ publishOnChain, ...message });
const [, data] = mockIssueClaim.mock.calls.pop();

const { issuedToken, requester, claimIssuer, onChainProof, acceptedBy } = data;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

29 changes: 13 additions & 16 deletions src/modules/claims/claims.service.ts
Expand Up @@ -181,23 +181,30 @@ export class ClaimsService {
await this._cacheClient.requestClaim(subject, message);
}

/**
* Issue a claim request by signing both off-chain and on-chain request and persisting result to the cache-server.
* Optionally, issue on-chain role can be submitted to the ClaimManager contract as well.
* @param params.publishOnChain If issuing an on-chain role, then if true then will submit role to chain (incurring tx cost). Default is true
*/
async issueClaimRequest({
requester,
token,
id,
subjectAgreement,
registrationTypes,
issuerFields,
publishOnChain = true,
}: {
requester: string;
token: string;
id: string;
subjectAgreement: string;
registrationTypes: RegistrationTypes[];
issuerFields?: { key: string; value: string | number }[];
publishOnChain?: boolean;
}) {
const { claimData, sub } = this._didRegistry.jwt.decode(token) as {
claimData: { claimType: string; claimTypeVersion: number; expiry: number };
claimData: { claimType: string; claimTypeVersion: number };
sub: string;
};

Expand All @@ -222,22 +229,12 @@ export class ClaimsService {
}
if (registrationTypes.includes(RegistrationTypes.OnChain)) {
const { claimType: role, claimTypeVersion: version } = claimData;
const expiry = claimData.expiry === undefined ? defaultClaimExpiry : claimData.expiry;
const expiry = defaultClaimExpiry;
const onChainProof = await this.createOnChainProof(role, version, expiry, sub);
const data = this._claimManagerInterface.encodeFunctionData("register", [
addressOf(sub),
namehash(role),
version,
expiry,
addressOf(this._signerService.did),
subjectAgreement,
onChainProof,
]);
await this._signerService.send({
to: this._claimManager,
data,
});
message.onChainProof = onChainProof;
if (publishOnChain) {
await this.registerOnchain({ token, subjectAgreement, onChainProof });
}
}

return this._cacheClient.issueClaim(this._signerService.did, message);
Expand All @@ -248,7 +245,7 @@ export class ClaimsService {
*
* @param claimId - id of signed onchain claim
*/
async registerOnchain(claim: Claim) {
async registerOnchain(claim: Pick<Claim, "token" | "subjectAgreement" | "onChainProof">) {
if (!readyToBeRegisteredOnchain(claim)) {
throw new Error(ERROR_MESSAGES.CLAIM_WAS_NOT_ISSUED);
}
Expand Down
25 changes: 2 additions & 23 deletions src/modules/claims/claims.types.ts
Expand Up @@ -50,29 +50,8 @@ export interface Claim {

export const readyToBeRegisteredOnchain = (
claim: any,
): claim is Required<
Pick<
Claim,
| "id"
| "subject"
| "claimType"
| "claimTypeVersion"
| "token"
| "subjectAgreement"
| "onChainProof"
| "isAccepted"
>
> => {
const requiredProps = [
"id",
"subject",
"claimType",
"ClaimTypeVersion",
"token",
"subjectAgreement",
"onChainProof",
"isAccepted",
];
): claim is Required<Pick<Claim, "token" | "subjectAgreement" | "onChainProof">> => {
const requiredProps = ["token", "subjectAgreement", "onChainProof"];
const claimProps = Object.keys(claim);
return requiredProps.every((p) => claimProps.includes(p));
};
Expand Down

0 comments on commit c7eb237

Please sign in to comment.