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

fix: 🐛 allow specify customClaimTypeId for TrustedClaimIssuer #1445

Draft
wants to merge 1 commit into
base: alpha
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,15 @@ export abstract class TransferRestrictionBase<
const claimType = meshClaimTypeToClaimType(rawClaimType);
const issuer = new Identity({ did: identityIdToString(rawIssuer) }, context);

claims.push({ claimType, issuer });
if (typeof claimType === 'object') {
claims.push({
claimType: claimType.type,
customClaimTypeId: claimType.customClaimTypeId,
issuer,
});
} else {
claims.push({ claimType, issuer });
}
};

[...currentStats].forEach(stat => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@ describe('TransferRestrictionBase class', () => {
let rawCountStatType: PolymeshPrimitivesStatisticsStatType;
let rawPercentageStatType: PolymeshPrimitivesStatisticsStatType;
let rawClaimCountStatType: PolymeshPrimitivesStatisticsStatType;
let rawCustomClaimCountStatType: PolymeshPrimitivesStatisticsStatType;
let rawClaimPercentageStatType: PolymeshPrimitivesStatisticsStatType;

let activeAssetStatsMock: jest.Mock;
Expand All @@ -721,6 +722,13 @@ describe('TransferRestrictionBase class', () => {
issuerDid,
]),
});
rawCustomClaimCountStatType = dsMockUtils.createMockStatisticsStatType({
operationType: dsMockUtils.createMockStatisticsOpType(StatType.Count),
claimIssuer: dsMockUtils.createMockOption([
dsMockUtils.createMockClaimType(ClaimType.Custom, new BigNumber(1)),
issuerDid,
]),
});
rawClaimPercentageStatType = dsMockUtils.createMockStatisticsStatType({
operationType: dsMockUtils.createMockStatisticsOpType(StatType.ScopedBalance),
claimIssuer: dsMockUtils.createMockOption([
Expand Down Expand Up @@ -790,15 +798,20 @@ describe('TransferRestrictionBase class', () => {
});

it('should return the active stats status for (ClaimCount) when stats enabled', async () => {
activeAssetStatsMock.mockResolvedValueOnce([rawClaimCountStatType]);
activeAssetStatsMock.mockResolvedValueOnce([
rawCustomClaimCountStatType,
rawClaimCountStatType,
]);
const claimCount = new ClaimCount(asset, context);

const result = await claimCount.getStat();

expect(result.isSet).toBeTruthy();
expect(result.claims).toBeDefined();

const [claim] = result.claims || [];
const [customClaim, claim] = result.claims || [];
expect(customClaim.claimType).toEqual(ClaimType.Custom);
expect(customClaim.customClaimTypeId).toEqual(new BigNumber(1));
expect(claim.claimType).toEqual(ClaimType.Affiliate);
});

Expand Down
4 changes: 2 additions & 2 deletions src/api/entities/DefaultTrustedClaimIssuer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Context, FungibleAsset, Identity, PolymeshError } from '~/internal';
import { trustedClaimIssuerQuery } from '~/middleware/queries/claims';
import { Query } from '~/middleware/types';
import { ClaimType, ErrorCode, EventIdentifier } from '~/types';
import { ErrorCode, EventIdentifier, TrustedFor } from '~/types';
import { Ensured } from '~/types/utils';
import {
assetToMeshAssetId,
Expand Down Expand Up @@ -78,7 +78,7 @@ export class DefaultTrustedClaimIssuer extends Identity {
/**
* Retrieve claim types for which this Claim Issuer is trusted. A null value means that the issuer is trusted for all claim types
*/
public async trustedFor(): Promise<ClaimType[] | null> {
public async trustedFor(): Promise<TrustedFor[] | null> {
const {
context: {
polymeshApi: {
Expand Down
6 changes: 4 additions & 2 deletions src/api/entities/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ export interface ClaimScope {
assetId?: string;
}

export type TrustedFor = ClaimType | { type: ClaimType.Custom; customClaimTypeId: BigNumber };

/**
* @param IsDefault - whether the Identity is a default trusted claim issuer for an asset or just
* for a specific compliance condition. Defaults to false
Expand All @@ -315,7 +317,7 @@ export interface TrustedClaimIssuer<IsDefault extends boolean = false> {
/**
* a null value means that the issuer is trusted for all claim types
*/
trustedFor: ClaimType[] | null;
trustedFor: TrustedFor[] | null;
}

export type InputTrustedClaimIssuer = Modify<
Expand Down Expand Up @@ -833,5 +835,5 @@ export type PortfolioMovement = FungiblePortfolioMovement | NonFungiblePortfolio

export type ActiveStats = {
isSet: boolean;
claims?: { claimType: ClaimType; issuer: Identity }[];
claims?: { claimType: ClaimType; customClaimTypeId?: BigNumber; issuer: Identity }[];
};
11 changes: 9 additions & 2 deletions src/testUtils/mocks/dataSources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,6 @@ function initApi(): void {
} as unknown as Registry;
mockInstanceContainer.apiInstance.createType = jest.fn();
mockInstanceContainer.apiInstance.runtimeVersion = {} as RuntimeVersion;
// mockInstanceContainer.apiInstance.errors = {} as DecoratedErrors<'promise'>;

initTx();
initQuery();
Expand Down Expand Up @@ -2913,7 +2912,8 @@ export const createMockConditionType = (
* NOTE: `isEmpty` will be set to true if no value is passed
*/
export const createMockClaimType = (
claimType?: ClaimType
claimType?: ClaimType,
customClaimTypeId?: BigNumber
): MockCodec<PolymeshPrimitivesIdentityClaimClaimType> => {
const claimIndexes = {
Accredited: 1,
Expand All @@ -2929,6 +2929,13 @@ export const createMockClaimType = (
InvestorUniquenessV2: 11,
Custom: 12,
};

if (claimType === ClaimType.Custom) {
return createMockEnum<PolymeshPrimitivesIdentityClaimClaimType>({
Custom: createMockU32(customClaimTypeId ?? new BigNumber(0)),
});
}

return createMockEnum<PolymeshPrimitivesIdentityClaimClaimType>(
claimType,
claimType ? claimIndexes[claimType] : 0
Expand Down
57 changes: 45 additions & 12 deletions src/utils/__tests__/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4918,12 +4918,10 @@ describe('meshClaimTypeToClaimType and claimTypeToMeshClaimType', () => {
result = meshClaimTypeToClaimType(claimType);
expect(result).toEqual(fakeResult);

fakeResult = ClaimType.Custom;

claimType = dsMockUtils.createMockClaimType(fakeResult);
claimType = dsMockUtils.createMockClaimType(ClaimType.Custom);

result = meshClaimTypeToClaimType(claimType);
expect(result).toEqual(fakeResult);
expect(result).toEqual({ type: ClaimType.Custom, customClaimTypeId: new BigNumber(0) });
});

it('should throw an error if it un-parses an unknown type', () => {
Expand Down Expand Up @@ -7247,12 +7245,18 @@ describe('trustedClaimIssuerToTrustedIssuer and trustedIssuerToTrustedClaimIssue
});

describe('trustedClaimIssuerToTrustedIssuer', () => {
it('should convert a did string into an PolymeshPrimitivesIdentityId', () => {
const did = 'someDid';
const fakeResult = 'type' as unknown as PolymeshPrimitivesConditionTrustedIssuer;
const context = dsMockUtils.getContextInstance();
let context: Context;
let did: string;
let fakeResult: PolymeshPrimitivesConditionTrustedIssuer;

let issuer: TrustedClaimIssuer = {
beforeEach(() => {
context = dsMockUtils.getContextInstance();
did = 'someDid';
fakeResult = 'type' as unknown as PolymeshPrimitivesConditionTrustedIssuer;
});

it('should handle null trustedFor', () => {
const issuer: TrustedClaimIssuer = {
identity: entityMockUtils.getIdentityInstance({ did }),
trustedFor: null,
};
Expand All @@ -7264,10 +7268,12 @@ describe('trustedClaimIssuerToTrustedIssuer and trustedIssuerToTrustedClaimIssue
})
.mockReturnValue(fakeResult);

let result = trustedClaimIssuerToTrustedIssuer(issuer, context);
const result = trustedClaimIssuerToTrustedIssuer(issuer, context);
expect(result).toBe(fakeResult);
});

issuer = {
it('should handle array of claim types', () => {
const issuer: TrustedClaimIssuer = {
identity: entityMockUtils.getIdentityInstance({ did }),
trustedFor: [ClaimType.Accredited, ClaimType.Blocked],
};
Expand All @@ -7279,7 +7285,34 @@ describe('trustedClaimIssuerToTrustedIssuer and trustedIssuerToTrustedClaimIssue
})
.mockReturnValue(fakeResult);

result = trustedClaimIssuerToTrustedIssuer(issuer, context);
const result = trustedClaimIssuerToTrustedIssuer(issuer, context);
expect(result).toBe(fakeResult);
});

it('should handle custom claim type', () => {
const customClaimTypeId = new BigNumber(1);
const issuer: TrustedClaimIssuer = {
identity: entityMockUtils.getIdentityInstance({ did }),
trustedFor: [{ type: ClaimType.Custom, customClaimTypeId }],
};

const mockTrustedFor = 'Custom' as unknown as PolymeshPrimitivesIdentityClaimClaimType;

when(context.createType)
.calledWith('PolymeshPrimitivesIdentityClaimClaimType', {
Custom: bigNumberToU32(customClaimTypeId, context),
})
.mockReturnValue(mockTrustedFor);

when(context.createType)
.calledWith('PolymeshPrimitivesConditionTrustedIssuer', {
issuer: stringToIdentityId(did, context),
trustedFor: { Specific: [mockTrustedFor] },
})
.mockReturnValue(fakeResult);

const result = trustedClaimIssuerToTrustedIssuer(issuer, context);

expect(result).toBe(fakeResult);
});
});
Expand Down
22 changes: 18 additions & 4 deletions src/utils/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ import {
TransferRestrictionType,
TransferStatus,
TrustedClaimIssuer,
TrustedFor,
TxGroup,
TxTag,
TxTags,
Expand Down Expand Up @@ -2627,7 +2628,7 @@ export function stringToTargetIdentity(
*/
export function meshClaimTypeToClaimType(
claimType: PolymeshPrimitivesIdentityClaimClaimType
): ClaimType {
): TrustedFor {
const type = claimType.type;
if (type === 'Jurisdiction') {
return ClaimType.Jurisdiction;
Expand Down Expand Up @@ -2662,7 +2663,9 @@ export function meshClaimTypeToClaimType(
}

if (type === 'Custom') {
return ClaimType.Custom;
const claim = claimType.asCustom;

return { type: ClaimType.Custom, customClaimTypeId: u32ToBigNumber(claim) };
}

if (type === 'Blocked') {
Expand All @@ -2683,7 +2686,7 @@ export function trustedIssuerToTrustedClaimIssuer(

const identity = new Identity({ did: identityIdToString(issuer) }, context);

let trustedFor: ClaimType[] | null = null;
let trustedFor: TrustedFor[] | null = null;

if (claimTypes.isSpecific) {
trustedFor = claimTypes.asSpecific.map(meshClaimTypeToClaimType);
Expand All @@ -2710,7 +2713,18 @@ export function trustedClaimIssuerToTrustedIssuer(
if (!claimTypes) {
trustedFor = 'Any';
} else {
trustedFor = { Specific: claimTypes };
trustedFor = {
Specific: claimTypes.map(claimType => {
if (typeof claimType === 'object' && claimType.customClaimTypeId) {
// Create a proper Custom enum value
return context.createType('PolymeshPrimitivesIdentityClaimClaimType', {
Custom: bigNumberToU32(claimType.customClaimTypeId, context),
});
}
// For non-custom claim types
return claimType;
}),
};
}

return context.createType('PolymeshPrimitivesConditionTrustedIssuer', {
Expand Down
Loading