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

draft: feat: package/asset: implement revoke() call #168

Closed
wants to merge 2 commits into from
Closed
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
63 changes: 48 additions & 15 deletions packages/asset/src/Asset.chain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
AssetUri,
IAssetEntry,
Expand All @@ -9,21 +8,18 @@ import {
SignExtrinsicCallback,
AuthorizationId,
ASSET_PREFIX,
DidUri,
} from '@cord.network/types'
import type { Option } from '@cord.network/types'
import type {
PalletAssetAssetEntry,
} from '@cord.network/augment-api'
import type { PalletAssetAssetEntry } from '@cord.network/augment-api'

import * as Did from '@cord.network/did'
import { uriToIdentifier } from '@cord.network/identifier'
import { Chain } from '@cord.network/network'
import { ConfigService } from '@cord.network/config'
import { SDKErrors } from '@cord.network/utils'

export async function isAssetStored(
assetUri: AssetUri
): Promise<boolean> {
export async function isAssetStored(assetUri: AssetUri): Promise<boolean> {
try {
const api = ConfigService.get('api')
const identifier = uriToIdentifier(assetUri)
Expand Down Expand Up @@ -91,9 +87,7 @@ export async function dispatchIssueToChain(

const authorizationId: AuthorizationId = uriToIdentifier(authorizationUri)

const exists = await isAssetStored(
assetEntry.entry.assetId as AssetUri
)
const exists = await isAssetStored(assetEntry.entry.assetId as AssetUri)

if (!exists) {
throw new SDKErrors.CordDispatchError(`Asset Entry not found on chain.`)
Expand Down Expand Up @@ -124,7 +118,6 @@ export async function dispatchIssueToChain(
}
}


export async function dispatchTransferToChain(
assetEntry: IAssetTransfer,
authorAccount: CordKeyringPair,
Expand All @@ -133,10 +126,7 @@ export async function dispatchTransferToChain(
try {
const api = ConfigService.get('api')

const tx = api.tx.asset.transfer(
assetEntry.entry,
assetEntry.digest,
)
const tx = api.tx.asset.transfer(assetEntry.entry, assetEntry.digest)

const extrinsic = await Did.authorizeTx(
assetEntry.owner,
Expand All @@ -157,3 +147,46 @@ export async function dispatchTransferToChain(
}
}

export async function dispatchRevokeAssetToChain(
assetId: AssetUri,
assetInstanceId: string,
assetOwnerDid: DidUri,
authorAccount: CordKeyringPair,
authorizationUri: AuthorizationUri,
signCallback: SignExtrinsicCallback
): Promise<void> {
try {
const api = ConfigService.get('api')

let encodedAssetInstanceDetail = await api.query.asset.issuance(
assetId,
assetInstanceId
)
let assetInstanceDetail = JSON.parse(encodedAssetInstanceDetail.toString())

if (assetOwnerDid != assetInstanceDetail.assetInstanceOwner) {
throw new SDKErrors.AssetOwnerMismatch(`Error: Assset owner mismatch`)
}
if (assetInstanceDetail.assetInstanceStatus != 'ACTIVE') {
throw new SDKErrors.AssetInstanceNotActiveError(
`Error: Asset Instance is not active`
)
}

const authorizationId: AuthorizationId = uriToIdentifier(authorizationUri)
const tx = api.tx.asset.revoke(assetId, assetInstanceId, authorizationId)

const extrinsic = await Did.authorizeTx(
assetOwnerDid,
tx,
signCallback,
authorAccount.address
)

await Chain.signAndSubmitTx(extrinsic, authorAccount)
} catch (error) {
throw new SDKErrors.CordDispatchError(
`Error dispatching to chain: "${error}".`
)
}
}
4 changes: 4 additions & 0 deletions packages/utils/src/SDKErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,7 @@ export class DecodingMessageError extends SDKError {}
export class TimeoutError extends SDKError {}

export class CodecMismatchError extends SDKError {}

export class AssetInstanceNotActiveError extends SDKError {}

export class AssetOwnerMismatch extends SDKError {}
Loading