Skip to content

Commit

Permalink
feat(claims): check is claim issued
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiter committed Dec 3, 2021
1 parent 926eb95 commit 1cb6d83
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 5 deletions.
Expand Up @@ -365,9 +365,9 @@ ___

#### Parameters

| Name | Type |
| :------ | :------ |
| `claimId` | `string` |
| Name | Type | Description |
| :------ | :------ | :------ |
| `claimId` | `string` | id of signed onchain claim |

#### Returns

Expand Down
7 changes: 7 additions & 0 deletions docs/api/enums/errors_ErrorMessages.ERROR_MESSAGES.md
Expand Up @@ -8,6 +8,7 @@

- [APP\_WITH\_ROLES](errors_ErrorMessages.ERROR_MESSAGES.md#app_with_roles)
- [CAN\_NOT\_UPDATE\_NOT\_CONTROLLED\_DOCUMENT](errors_ErrorMessages.ERROR_MESSAGES.md#can_not_update_not_controlled_document)
- [CLAIM\_WAS\_NOT\_ISSUED](errors_ErrorMessages.ERROR_MESSAGES.md#claim_was_not_issued)
- [ENS\_TYPE\_NOT\_SUPPORTED](errors_ErrorMessages.ERROR_MESSAGES.md#ens_type_not_supported)
- [ERROR\_IN\_AZURE\_PROVIDER](errors_ErrorMessages.ERROR_MESSAGES.md#error_in_azure_provider)
- [INSUFFICIENT\_BALANCE](errors_ErrorMessages.ERROR_MESSAGES.md#insufficient_balance)
Expand Down Expand Up @@ -40,6 +41,12 @@ ___

___

### CLAIM\_WAS\_NOT\_ISSUED

**CLAIM\_WAS\_NOT\_ISSUED** = `"Claim was not issued"`

___

### ENS\_TYPE\_NOT\_SUPPORTED

**ENS\_TYPE\_NOT\_SUPPORTED** = `"ENS type not supported"`
Expand Down
7 changes: 7 additions & 0 deletions docs/api/modules/index.md
Expand Up @@ -89,6 +89,7 @@
- [initWithMetamask](index.md#initwithmetamask)
- [initWithPrivateKeySigner](index.md#initwithprivatekeysigner)
- [initWithWalletConnect](index.md#initwithwalletconnect)
- [isIssuedOnchain](index.md#isissuedonchain)
- [isMetamaskExtensionPresent](index.md#ismetamaskextensionpresent)
- [isValidDID](index.md#isvaliddid)
- [parseDID](index.md#parsedid)
Expand Down Expand Up @@ -611,6 +612,12 @@ Re-exports: [initWithWalletConnect](init.md#initwithwalletconnect)

___

### isIssuedOnchain

Re-exports: [isIssuedOnchain](modules_claims_claims_types.md#isissuedonchain)

___

### isMetamaskExtensionPresent

Re-exports: [isMetamaskExtensionPresent](modules_signer_metamaskSigner.md#ismetamaskextensionpresent)
Expand Down
7 changes: 7 additions & 0 deletions docs/api/modules/modules_claims.md
Expand Up @@ -16,6 +16,7 @@
- [agreement\_type\_hash](modules_claims.md#agreement_type_hash)
- [defaultClaimExpiry](modules_claims.md#defaultclaimexpiry)
- [erc712\_type\_hash](modules_claims.md#erc712_type_hash)
- [isIssuedOnchain](modules_claims.md#isissuedonchain)
- [proof\_type\_hash](modules_claims.md#proof_type_hash)
- [typedMsgPrefix](modules_claims.md#typedmsgprefix)

Expand Down Expand Up @@ -93,6 +94,12 @@ Re-exports: [erc712\_type\_hash](modules_claims_claims_types.md#erc712_type_hash

___

### isIssuedOnchain

Re-exports: [isIssuedOnchain](modules_claims_claims_types.md#isissuedonchain)

___

### proof\_type\_hash

Re-exports: [proof\_type\_hash](modules_claims_claims_types.md#proof_type_hash)
Expand Down
20 changes: 20 additions & 0 deletions docs/api/modules/modules_claims_claims_types.md
Expand Up @@ -27,6 +27,10 @@
- [proof\_type\_hash](modules_claims_claims_types.md#proof_type_hash)
- [typedMsgPrefix](modules_claims_claims_types.md#typedmsgprefix)

### Functions

- [isIssuedOnchain](modules_claims_claims_types.md#isissuedonchain)

## Type aliases

### IssueClaim
Expand Down Expand Up @@ -95,3 +99,19 @@ ___
### typedMsgPrefix

`Const` **typedMsgPrefix**: ``"1901"``

## Functions

### isIssuedOnchain

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

#### Parameters

| Name | Type |
| :------ | :------ |
| `claim` | `any` |

#### Returns

claim is Required<Pick<Claim, "id" \| "isAccepted" \| "token" \| "claimTypeVersion" \| "claimType" \| "subject" \| "subjectAgreement" \| "onChainProof"\>\>
1 change: 1 addition & 0 deletions src/errors/ErrorMessages.ts
Expand Up @@ -18,4 +18,5 @@ export enum ERROR_MESSAGES {
NOT_AUTHORIZED_TO_CHANGE_DOMAIN = "Not authorized to change domain",
ERROR_IN_AZURE_PROVIDER = "Error in Azure Provider",
JWT_ALGORITHM_NOT_SUPPORTED = "Jwt algorithm not supported",
CLAIM_WAS_NOT_ISSUED = "Claim was not issued",
}
10 changes: 8 additions & 2 deletions src/modules/claims/claims.service.ts
Expand Up @@ -32,6 +32,7 @@ import { ClaimData } from "../didRegistry/did.types";
import { isValidDID } from "../../utils/did";
import { JWT } from "@ew-did-registry/jwt";
import { privToPem, KeyType } from "@ew-did-registry/keys";
import { isIssuedOnchain } from ".";

const { id, keccak256, defaultAbiCoder, solidityKeccak256, namehash, arrayify } = utils;

Expand Down Expand Up @@ -219,15 +220,20 @@ export class ClaimsService {

/**
* @description Registers issued onchain claim with Claim manager
*
* @param claimId - id of signed onchain claim
*/
async registerOnchain(claimId: Claim["id"]) {
const claim = (await this.getClaimById(claimId)) as Required<Claim>;
const claim = await this.getClaimById(claimId);
if (!isIssuedOnchain(claim)) {
throw new Error(ERROR_MESSAGES.CLAIM_WAS_NOT_ISSUED);
}
const { token, subjectAgreement, onChainProof } = claim;
const { claimData, sub } = this._didRegistry.jwt.decode(token) as {
claimData: { claimType: string; claimTypeVersion: number; expiry: number };
sub: string;
};
const expiry = claimData.expiry === undefined ? defaultClaimExpiry : claimData.expiry;
const expiry = defaultClaimExpiry;
const { claimType: role, claimTypeVersion: version } = claimData;
const data = this._claimManagerInterface.encodeFunctionData("register", [
addressOf(sub),
Expand Down
29 changes: 29 additions & 0 deletions src/modules/claims/claims.types.ts
Expand Up @@ -42,6 +42,35 @@ export interface Claim {
namespace: string;
}

export const isIssuedOnchain = (
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",
];
const claimProps = Object.keys(claim);
return requiredProps.every((p) => claimProps.includes(p));
};

export const typedMsgPrefix = "1901";
export const erc712_type_hash = utils.id(
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)",
Expand Down

0 comments on commit 1cb6d83

Please sign in to comment.