From ce90fc4110da465df3f4343a979fa08e5fc41698 Mon Sep 17 00:00:00 2001 From: Mohammed Ismail <111173270+masterismail@users.noreply.github.com> Date: Wed, 10 Jul 2024 12:16:37 +0530 Subject: [PATCH] Asset: Separate preparation of creation of VC Asset from dispatch (#215) --- packages/asset/src/Asset.chain.ts | 65 +++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/packages/asset/src/Asset.chain.ts b/packages/asset/src/Asset.chain.ts index 8d0b654d..a34b9b65 100644 --- a/packages/asset/src/Asset.chain.ts +++ b/packages/asset/src/Asset.chain.ts @@ -83,7 +83,7 @@ export async function dispatchCreateToChain( signCallback: SignExtrinsicCallback ): Promise { try { - + const extrinsic = await prepareCreateExtrinsic( assetEntry, authorAccount, @@ -103,24 +103,19 @@ export async function dispatchCreateToChain( } } -export async function dispatchCreateVcToChain( +export async function prepareCreateVcExtrinsic( assetQty: number, digest: string, creator: DidUri, authorAccount: CordKeyringPair, authorizationUri: AuthorizationUri, - assetEntryUri: AssetUri, signCallback: SignExtrinsicCallback -): Promise { +): Promise { try { - const api = ConfigService.get('api') - const authorizationId: AuthorizationId = uriToIdentifier(authorizationUri) + const api = ConfigService.get('api'); + const authorizationId: AuthorizationId = uriToIdentifier(authorizationUri); - const tx = api.tx.asset.vcCreate( - assetQty, - digest, - authorizationId - ) + const tx = api.tx.asset.vcCreate(assetQty, digest, authorizationId); const extrinsic = await Did.authorizeTx( creator, @@ -128,16 +123,44 @@ export async function dispatchCreateVcToChain( signCallback, authorAccount.address ) + return extrinsic; + } catch (error) { + const errorMessage = + error instanceof Error ? error.message : JSON.stringify(error); + throw new SDKErrors.CordDispatchError( + `Error preparing VC Asset Entry extrinsic: "${errorMessage}".` + ); + } +} - await Chain.signAndSubmitTx(extrinsic, authorAccount) +export async function dispatchCreateVcToChain( + assetQty: number, + digest: string, + creator: DidUri, + authorAccount: CordKeyringPair, + authorizationUri: AuthorizationUri, + assetEntryUri: AssetUri, + signCallback: SignExtrinsicCallback +): Promise { + try { + const extrinsic = await prepareCreateVcExtrinsic( + assetQty, + digest, + creator, + authorAccount, + authorizationUri, + signCallback + ); + + await Chain.signAndSubmitTx(extrinsic, authorAccount); - return assetEntryUri + return assetEntryUri; } catch (error) { const errorMessage = - error instanceof Error ? error.message : JSON.stringify(error) + error instanceof Error ? error.message : JSON.stringify(error); throw new SDKErrors.CordDispatchError( `Error dispatching to chain: "${errorMessage}".` - ) + ); } } @@ -167,8 +190,8 @@ export async function prepareExtrinsic( return extrinsic } catch (error) { - const errorMessage = - error instanceof Error ? error.message : JSON.stringify(error) + const errorMessage = + error instanceof Error ? error.message : JSON.stringify(error) throw new SDKErrors.CordDispatchError( `Error preparing extrinsic: "${errorMessage}".` ) @@ -183,7 +206,7 @@ export async function dispatchIssueToChain( ): Promise { try { - const extrinsic = await prepareExtrinsic(assetEntry, authorAccount, authorizationUri, signCallback) + const extrinsic = await prepareExtrinsic(assetEntry, authorAccount, authorizationUri, signCallback) await Chain.signAndSubmitTx(extrinsic, authorAccount) return assetEntry.uri @@ -222,8 +245,8 @@ export async function prepareVcExtrinsic( return extrinsic } catch (error) { - const errorMessage = - error instanceof Error ? error.message : JSON.stringify(error) + const errorMessage = + error instanceof Error ? error.message : JSON.stringify(error) throw new SDKErrors.CordDispatchError( `Error preparing extrinsic: "${errorMessage}".` ) @@ -238,7 +261,7 @@ export async function dispatchIssueVcToChain( ): Promise { try { - const extrinsic = await prepareVcExtrinsic(assetEntry, authorAccount, authorizationUri, signCallback) + const extrinsic = await prepareVcExtrinsic(assetEntry, authorAccount, authorizationUri, signCallback) await Chain.signAndSubmitTx(extrinsic, authorAccount) return assetEntry.uri