Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 claim management #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@polkadot/wasm-crypto": "7.2.2",
"@polymeshassociation/hashicorp-vault-signing-manager": "^3.0.1",
"@polymeshassociation/local-signing-manager": "^3.1.0",
"@polymeshassociation/polymesh-sdk": "27.3.0-alpha.1",
"@polymeshassociation/polymesh-sdk": "27.3.1-alpha.1",
"cross-fetch": "^4.0.0",
"dotenv": "^16.0.3"
},
Expand Down
4 changes: 4 additions & 0 deletions src/rest/claims/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ export class Claims {
public async removeClaim(params: CreateClaimParams): Promise<PostResult> {
return this.client.post('/claims/remove', params);
}

public async editClaim(params: CreateClaimParams): Promise<PostResult> {
return this.client.post('/claims/edit', params);
}
}
16 changes: 16 additions & 0 deletions src/rest/identities/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,20 @@ export class Identities {
accounts,
});
}

public async getIssuedClaims(did: string): Promise<ResultSet<unknown>> {
return this.client.get(`/identities/${did}/issued-claims`);
}

public async getAssociatedClaims(did: string): Promise<ResultSet<unknown>> {
return this.client.get(`/identities/${did}/associated-claims`);
}

public async getCddClaims(did: string): Promise<ResultSet<Record<string, unknown>>> {
return this.client.get(`/identities/${did}/cdd-claims`);
}

public async findClaimScopesByDid(did: string): Promise<ResultSet<Record<string, unknown>>> {
return this.client.get(`/identities/${did}/claim-scopes`);
}
}
56 changes: 52 additions & 4 deletions src/sdk/identities/claims.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Polymesh } from '@polymeshassociation/polymesh-sdk';
import { BigNumber, Polymesh } from '@polymeshassociation/polymesh-sdk';
import { ClaimType, ScopeType } from '@polymeshassociation/polymesh-sdk/types';
import assert from 'node:assert';

Expand All @@ -9,10 +9,14 @@ import { awaitMiddlewareSynced } from '~/util';
- Add a claim
- Waits for middleware to sync
- Revoke a claim
- Edit a claim
- Get CDD claims
- Get investor uniqueness claims
- Get claims targeting a given Identity
- Get claims issued by given Identity
- Get identities with claims
- Get claim scopes
- Get CDD claims
*/
export const manageClaims = async (
sdk: Polymesh,
Expand Down Expand Up @@ -55,6 +59,16 @@ export const manageClaims = async (
});
assert(issuedClaims.data.length, 'The default signer should have at least one issued claim');

const identitiesWithAccreditedClaim = await sdk.claims.getIdentitiesWithClaims({
targets: [targetDid],
claimTypes: [ClaimType.Accredited],
size: new BigNumber(1),
start: new BigNumber(0),
includeExpired: true,
});
assert(Array.isArray(identitiesWithAccreditedClaim.data));
expect(identitiesWithAccreditedClaim.data.length).toBe(1);

// select the first one to revoke
const claimToRevoke = issuedClaims.data[0];

Expand All @@ -69,7 +83,18 @@ export const manageClaims = async (
await revokeClaimTx.run();
assert(revokeClaimTx.isSuccess);

await awaitMiddlewareSynced(revokeClaimTx, sdk, 15, 2000);
// edit claims
const editClaimTx = await sdk.claims.editClaims(
{
claims: [claimToRevoke],
},
{ signingAccount }
);

await editClaimTx.run();
assert(editClaimTx.isSuccess);

await awaitMiddlewareSynced(editClaimTx, sdk, 15, 2000);

// This following portion demonstrates different ways to fetch claims

Expand All @@ -81,10 +106,33 @@ export const manageClaims = async (
);

// `target` can specify which Identity to fetch Claims for
const targetingClaims = await sdk.claims.getTargetingClaims({ target: targetDid });
const targetingClaims = await sdk.claims.getTargetingClaims({ target: identity.did });
assert(Array.isArray(targetingClaims.data), 'Data should be an Array for `getTargetingClaims`');

// `target` here refers to the issuer of the claim
const claimsIssuedByTarget = await sdk.claims.getIssuedClaims({ target: targetDid });
const claimsIssuedByTarget = await sdk.claims.getIssuedClaims({ target: identity.did });
assert(Array.isArray(claimsIssuedByTarget.data), 'Data should be an Array for `getIssuedClaims`');

// get identities with claims
const identitiesWithClaims = await sdk.claims.getIdentitiesWithClaims({
targets: [targetDid],
claimTypes: [ClaimType.Accredited],
size: new BigNumber(1),
start: new BigNumber(0),
includeExpired: true,
});

assert(Array.isArray(identitiesWithClaims.data));
expect(identitiesWithClaims.data.length).toBeGreaterThan(0);

// get claim scopes
const claimScopes = await sdk.claims.getClaimScopes({ target: targetDid });
assert(Array.isArray(claimScopes));
expect(claimScopes.length).toBeGreaterThan(0);
expect(claimScopes[0].scope).toBeDefined();

// get cdd claims
const cddClaims = await sdk.claims.getCddClaims({ target: targetDid });
assert(Array.isArray(cddClaims));
expect(cddClaims.length).toBe(1);
};
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1510,10 +1510,10 @@
dependencies:
"@polymeshassociation/signing-manager-types" "^3.2.0"

"@polymeshassociation/[email protected].0-alpha.1":
version "27.3.0-alpha.1"
resolved "https://registry.yarnpkg.com/@polymeshassociation/polymesh-sdk/-/polymesh-sdk-27.3.0-alpha.1.tgz#51262fe6bdbb8219fa3b2f94eaaf269c54cdcb52"
integrity sha512-Pvo5QsMKWHvHIv3VDn6l/nmFZ+fnmOB1bLLi7f/wcnnjTZnaQnnnBwZz0vrhQ60cnMg59sRw3vMI4TDGFUekrg==
"@polymeshassociation/[email protected].1-alpha.1":
version "27.3.1-alpha.1"
resolved "https://registry.yarnpkg.com/@polymeshassociation/polymesh-sdk/-/polymesh-sdk-27.3.1-alpha.1.tgz#f26dc483788ef21925419ff00ecb35a860351cec"
integrity sha512-mpx8Zlnq36Pcv4bskUJDWESgps+3xKC3rjwwsuXG0GWXXdur8RWi4vR5NWEGXPaOHtNLzqNDINyIztcNbUi86A==
dependencies:
"@apollo/client" "^3.8.1"
"@polkadot/api" "11.2.1"
Expand Down