Skip to content

Commit

Permalink
feat(claims.service): ICL-159 add hasOnChainRole utility
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhender committed Dec 2, 2021
1 parent 34fda9e commit abe6d86
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
24 changes: 24 additions & 0 deletions docs/api/classes/modules_claims_claims_service.ClaimsService.md
Expand Up @@ -22,6 +22,7 @@
- [getClaimsBySubject](modules_claims_claims_service.ClaimsService.md#getclaimsbysubject)
- [getClaimsBySubjects](modules_claims_claims_service.ClaimsService.md#getclaimsbysubjects)
- [getUserClaims](modules_claims_claims_service.ClaimsService.md#getuserclaims)
- [hasOnChainRole](modules_claims_claims_service.ClaimsService.md#hasonchainrole)
- [init](modules_claims_claims_service.ClaimsService.md#init)
- [issueClaim](modules_claims_claims_service.ClaimsService.md#issueclaim)
- [issueClaimRequest](modules_claims_claims_service.ClaimsService.md#issueclaimrequest)
Expand Down Expand Up @@ -280,6 +281,29 @@ getUserClaims

___

### hasOnChainRole

**hasOnChainRole**(`did`, `role`, `version`): `Promise`<`boolean`\>

A utility function to check the blockchain directly if a DID has a role
TODO: fail if the DID chain ID doesn't match the configured signer network connect

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `did` | `string` | The ethr DID to check |
| `role` | `string` | The role to check (the full namespace) |
| `version` | `number` | The version to check |

#### Returns

`Promise`<`boolean`\>

true if DID has role at the version. false if not.

___

### init

**init**(): `Promise`<`void`\>
Expand Down
22 changes: 22 additions & 0 deletions docs/api/classes/modules_signer_signer_service.SignerService.md
Expand Up @@ -21,6 +21,7 @@
### Methods

- [balance](modules_signer_signer_service.SignerService.md#balance)
- [call](modules_signer_signer_service.SignerService.md#call)
- [closeConnection](modules_signer_signer_service.SignerService.md#closeconnection)
- [connect](modules_signer_signer_service.SignerService.md#connect)
- [emit](modules_signer_signer_service.SignerService.md#emit)
Expand Down Expand Up @@ -128,6 +129,27 @@ ___

___

### call

**call**(`__namedParameters`): `Promise`<`string`\>

Makes a (readonly) call to a smart contract
https://docs.ethers.io/v5/single-page/#/v5/api/providers/provider/-%23-Provider-call

#### Parameters

| Name | Type |
| :------ | :------ |
| `__namedParameters` | `TransactionRequest` |

#### Returns

`Promise`<`string`\>

The result of the call

___

### closeConnection

**closeConnection**(): `Promise`<`boolean`\>
Expand Down
7 changes: 4 additions & 3 deletions e2e/claims.service.e2e.ts
Expand Up @@ -196,14 +196,15 @@ describe("Enrollment claim tests", () => {
test("enrollment by issuer of type DID", async () => {
const requester = rootOwner;
const issuer = staticIssuer;
const role = `${roleName1}.${root}`;
expect(await claimsService.hasOnChainRole(rootOwnerDID, role, version)).toBe(false);
await enrolAndIssue(requester, issuer, {
subjectDID: rootOwnerDID,
claimType: `${roleName1}.${root}`,
});

return expect(
claimManager.hasRole(addressOf(rootOwnerDID), namehash(`${roleName1}.${root}`), version),
).resolves.toBe(true);
expect(claimManager.hasRole(addressOf(rootOwnerDID), namehash(role), version)).resolves.toBe(true);
expect(await claimsService.hasOnChainRole(rootOwnerDID, role, version)).toBe(true);
});

test("asset enrollment by issuer of type DID", async () => {
Expand Down
21 changes: 21 additions & 0 deletions src/modules/claims/claims.service.ts
Expand Up @@ -63,6 +63,27 @@ export class ClaimsService {
this._claimManager = chainConfigs()[chainId].claimManagerAddress;
}

/**
* A utility function to check the blockchain directly if a DID has a role
* TODO: fail if the DID chain ID doesn't match the configured signer network connect
* @param did The ethr DID to check
* @param role The role to check (the full namespace)
* @param version The version to check
* @returns true if DID has role at the version. false if not.
*/
async hasOnChainRole(did: string, role: string, version: number): Promise<boolean> {
const data = this._claimManagerInterface.encodeFunctionData("hasRole", [
addressOf(did),
namehash(role),
version,
]);
const result = await this._signerService.call({
to: this._claimManager,
data,
});
return Boolean(Number.parseInt(result));
}

async getClaimsBySubjects(subjects: string[]) {
return this._cacheClient.getClaimsBySubjects(subjects);
}
Expand Down
13 changes: 13 additions & 0 deletions src/modules/signer/signer.service.ts
Expand Up @@ -124,6 +124,19 @@ export class SignerService {
return receipt;
}

/**
* Makes a (readonly) call to a smart contract
* https://docs.ethers.io/v5/single-page/#/v5/api/providers/provider/-%23-Provider-call
* @param params.to adddress of contract
* @param params.data call data
* @returns The result of the call
*/
async call({ to, data }: providers.TransactionRequest): Promise<string> {
const tx = { to, from: this.address, data };
const result = await this._signer.call(tx);
return result;
}

async signMessage(message: Uint8Array) {
return this.signer.signMessage(message);
}
Expand Down

0 comments on commit abe6d86

Please sign in to comment.