From fcf81e13c71cbfb0a68a73dd1cfac52aec10face Mon Sep 17 00:00:00 2001 From: Hardik Sharma Date: Sat, 13 Apr 2024 01:52:18 +0530 Subject: [PATCH 1/3] Fixes #197 --- packages/chain-space/src/ChainSpace.chain.ts | 42 ++++++++++++++------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/packages/chain-space/src/ChainSpace.chain.ts b/packages/chain-space/src/ChainSpace.chain.ts index 3efaf634..a3828b12 100644 --- a/packages/chain-space/src/ChainSpace.chain.ts +++ b/packages/chain-space/src/ChainSpace.chain.ts @@ -37,6 +37,7 @@ import type { SpaceDigest, AuthorizationUri, SpaceUri, + SubmittableExtrinsic } from '@cord.network/types' import { SDKErrors, DecoderUtils } from '@cord.network/utils' import { @@ -240,20 +241,9 @@ export async function dispatchToChain( } try { - const api = ConfigService.get('api') - - const exists = await isChainSpaceStored(chainSpace.uri) - if (!exists) { - const tx = api.tx.chainSpace.create(chainSpace.digest) - const extrinsic = await Did.authorizeTx( - creatorUri, - tx, - signCallback, - authorAccount.address - ) - await Chain.signAndSubmitTx(extrinsic, authorAccount) - } + const extrinsic = await prepareCreateSpaceExtrinsic(chainSpace, creatorUri, signCallback, authorAccount) + await Chain.signAndSubmitTx(extrinsic, authorAccount) return returnObject } catch (error) { @@ -263,6 +253,32 @@ export async function dispatchToChain( } } +export async function prepareCreateSpaceExtrinsic( + chainSpace: IChainSpace, + creatorUri: DidUri, + signCallback: SignExtrinsicCallback, + authorAccount: CordKeyringPair +): Promise { + try { + const api = ConfigService.get('api') + + const tx = api.tx.chainSpace.create(chainSpace.digest) + const extrinsic = await Did.authorizeTx( + creatorUri, + tx, + signCallback, + authorAccount.address + ) + return extrinsic; + + + } catch (error) { + throw new SDKErrors.CordDispatchError( + `Error preparing extrinsic for creation: "${error}".` + ); + } +} + /** * Dispatches a Sub-ChainSpace creation transaction to the CORD blockchain. * From 80746848c9a9f7a3a7e630feec4a179f588e50ca Mon Sep 17 00:00:00 2001 From: Hardik Sharma Date: Wed, 17 Apr 2024 15:37:12 +0530 Subject: [PATCH 2/3] nit --- packages/chain-space/src/ChainSpace.chain.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/chain-space/src/ChainSpace.chain.ts b/packages/chain-space/src/ChainSpace.chain.ts index a3828b12..50b04ce1 100644 --- a/packages/chain-space/src/ChainSpace.chain.ts +++ b/packages/chain-space/src/ChainSpace.chain.ts @@ -274,7 +274,7 @@ export async function prepareCreateSpaceExtrinsic( } catch (error) { throw new SDKErrors.CordDispatchError( - `Error preparing extrinsic for creation: "${error}".` + `Error preparing extrinsic for creation of chainspace: "${error}".` ); } } From 29e0dd36135c75b873b13a130ee98a8a2dc29584 Mon Sep 17 00:00:00 2001 From: Hardik Sharma Date: Mon, 22 Apr 2024 16:26:30 +0530 Subject: [PATCH 3/3] moved prepare... above dispatch... function --- packages/chain-space/src/ChainSpace.chain.ts | 47 ++++++++++---------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/packages/chain-space/src/ChainSpace.chain.ts b/packages/chain-space/src/ChainSpace.chain.ts index 50b04ce1..6b6d4228 100644 --- a/packages/chain-space/src/ChainSpace.chain.ts +++ b/packages/chain-space/src/ChainSpace.chain.ts @@ -229,29 +229,6 @@ export async function getUriForSpace( * @returns A promise resolving to an object containing the ChainSpace URI and authorization ID. * @throws {SDKErrors.CordDispatchError} - Thrown when there's an error during the dispatch process. */ -export async function dispatchToChain( - chainSpace: IChainSpace, - creatorUri: DidUri, - authorAccount: CordKeyringPair, - signCallback: SignExtrinsicCallback -): Promise<{ uri: SpaceUri; authorization: AuthorizationUri }> { - const returnObject = { - uri: chainSpace.uri, - authorization: chainSpace.authorizationUri, - } - - try { - - const extrinsic = await prepareCreateSpaceExtrinsic(chainSpace, creatorUri, signCallback, authorAccount) - await Chain.signAndSubmitTx(extrinsic, authorAccount) - - return returnObject - } catch (error) { - throw new SDKErrors.CordDispatchError( - `Error dispatching to chain: "${error}".` - ) - } -} export async function prepareCreateSpaceExtrinsic( chainSpace: IChainSpace, @@ -279,6 +256,30 @@ export async function prepareCreateSpaceExtrinsic( } } +export async function dispatchToChain( + chainSpace: IChainSpace, + creatorUri: DidUri, + authorAccount: CordKeyringPair, + signCallback: SignExtrinsicCallback +): Promise<{ uri: SpaceUri; authorization: AuthorizationUri }> { + const returnObject = { + uri: chainSpace.uri, + authorization: chainSpace.authorizationUri, + } + + try { + + const extrinsic = await prepareCreateSpaceExtrinsic(chainSpace, creatorUri, signCallback, authorAccount) + await Chain.signAndSubmitTx(extrinsic, authorAccount) + + return returnObject + } catch (error) { + throw new SDKErrors.CordDispatchError( + `Error dispatching to chain: "${error}".` + ) + } +} + /** * Dispatches a Sub-ChainSpace creation transaction to the CORD blockchain. *