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 8, 2020
1 parent b5fb041 commit 2619350
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 @@ -1653,10 +1653,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 @@ -1668,9 +1671,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 @@ -1681,10 +1683,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 @@ -955,11 +955,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 @@ -968,11 +1003,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 @@ -1021,41 +1056,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 2619350

Please sign in to comment.