From 26193505088e31524b50fb9b5285f1c6f1d24510 Mon Sep 17 00:00:00 2001 From: Jeremias Diaz Date: Thu, 8 Oct 2020 11:09:49 -0300 Subject: [PATCH] fix: convert claims correctly --- src/utils/__tests__/index.ts | 19 +++++---- src/utils/index.ts | 76 ++++++++++++++++++------------------ 2 files changed, 47 insertions(+), 48 deletions(-) diff --git a/src/utils/__tests__/index.ts b/src/utils/__tests__/index.ts index e869b0f37b..3dc576fcbf 100644 --- a/src/utils/__tests__/index.ts +++ b/src/utils/__tests__/index.ts @@ -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); @@ -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); @@ -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); diff --git a/src/utils/index.ts b/src/utils/index.ts index f68eada95e..49c9a2b82e 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -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: @@ -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); } } @@ -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 */