Skip to content

Commit

Permalink
fix: convert claims correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
monitz87 committed Oct 12, 2020
1 parent 7817921 commit 8f4aa1e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 48 deletions.
19 changes: 9 additions & 10 deletions src/utils/__tests__/index.ts
Expand Up @@ -1713,10 +1713,13 @@ describe('claimToMeshClaim and meshClaimToClaim', () => {
scope: { type: ScopeType.Identity, value: 'someTickerDid' },
};
const fakeResult = ('meshClaim' as unknown) as MeshClaim;
const fakeScope = ('scope' as unknown) as MeshScope;

dsMockUtils
.getCreateTypeStub()
.withArgs('Claim', { [value.type]: [value.code, value.scope] })
const createTypeStub = dsMockUtils.getCreateTypeStub();

createTypeStub.withArgs('Scope', sinon.match.any).returns(fakeScope);
createTypeStub
.withArgs('Claim', { [value.type]: [value.code, scopeToMeshScope(value.scope, context)] })
.returns(fakeResult);

let result = claimToMeshClaim(value, context);
Expand All @@ -1728,9 +1731,8 @@ describe('claimToMeshClaim and meshClaimToClaim', () => {
scope: { type: ScopeType.Identity, value: 'someTickerDid' },
};

dsMockUtils
.getCreateTypeStub()
.withArgs('Claim', { [value.type]: value.scope })
createTypeStub
.withArgs('Claim', { [value.type]: scopeToMeshScope(value.scope, context) })
.returns(fakeResult);

result = claimToMeshClaim(value, context);
Expand All @@ -1741,10 +1743,7 @@ describe('claimToMeshClaim and meshClaimToClaim', () => {
type: ClaimType.NoData,
};

dsMockUtils
.getCreateTypeStub()
.withArgs('Claim', { [value.type]: null })
.returns(fakeResult);
createTypeStub.withArgs('Claim', { [value.type]: null }).returns(fakeResult);

result = claimToMeshClaim(value, context);

Expand Down
76 changes: 38 additions & 38 deletions src/utils/index.ts
Expand Up @@ -998,11 +998,46 @@ export function canTransferResultToTransferStatus(
return u8ToTransferStatus(canTransferResult.asOk);
}

/**
* @hidden
*/
export function scopeToMeshScope(scope: Scope, context: Context): MeshScope {
const { type, value } = scope;

return context.polymeshApi.createType('Scope', {
[type]: value,
});
}

/**
* @hidden
*/
export function meshScopeToScope(scope: MeshScope): Scope {
if (scope.isTicker) {
return {
type: ScopeType.Ticker,
value: tickerToString(scope.asTicker),
};
}

if (scope.isIdentity) {
return {
type: ScopeType.Identity,
value: identityIdToString(scope.asIdentity),
};
}

return {
type: ScopeType.Custom,
value: u8aToString(scope.asCustom),
};
}

/**
* @hidden
*/
export function claimToMeshClaim(claim: Claim, context: Context): MeshClaim {
let value: unknown;
let value;

switch (claim.type) {
case ClaimType.NoData:
Expand All @@ -1011,11 +1046,11 @@ export function claimToMeshClaim(claim: Claim, context: Context): MeshClaim {
break;
}
case ClaimType.Jurisdiction: {
value = tuple(claim.code, claim.scope);
value = tuple(claim.code, scopeToMeshScope(claim.scope, context));
break;
}
default: {
value = claim.scope;
value = scopeToMeshScope(claim.scope, context);
}
}

Expand Down Expand Up @@ -1064,41 +1099,6 @@ export function createClaim(
return { type, scope };
}

/**
* @hidden
*/
export function scopeToMeshScope(scope: Scope, context: Context): MeshScope {
const { type, value } = scope;

return context.polymeshApi.createType('Scope', {
[type]: value,
});
}

/**
* @hidden
*/
export function meshScopeToScope(scope: MeshScope): Scope {
if (scope.isTicker) {
return {
type: ScopeType.Ticker,
value: tickerToString(scope.asTicker),
};
}

if (scope.isIdentity) {
return {
type: ScopeType.Identity,
value: identityIdToString(scope.asIdentity),
};
}

return {
type: ScopeType.Custom,
value: u8aToString(scope.asCustom),
};
}

/**
* @hidden
*/
Expand Down

0 comments on commit 8f4aa1e

Please sign in to comment.