From 48356b4b9c0efad96527ba1469c32d78982479ec Mon Sep 17 00:00:00 2001 From: Kartikay Date: Mon, 22 Apr 2024 06:11:58 +0530 Subject: [PATCH 1/2] new function prepareCreateSubSpaceExtrinsic() Signed-off-by: Kartikay --- packages/chain-space/src/ChainSpace.chain.ts | 44 ++++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/packages/chain-space/src/ChainSpace.chain.ts b/packages/chain-space/src/ChainSpace.chain.ts index 3efaf634..86ca08e7 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 { @@ -308,6 +309,42 @@ export async function dispatchSubspaceCreateToChain( authorization: chainSpace.authorizationUri, } + try { + + const extrinsic = await prepareCreateSubSpaceExtrinsic( + chainSpace, + authorAccount, + count, + parent, + creatorUri, + signCallback + ) + await Chain.signAndSubmitTx(extrinsic, authorAccount) + + return returnObject; + } catch (error) { + throw new SDKErrors.CordDispatchError( + `Error dispatching to chain: "${error}".` + ) + } +} + +/** + * Prepares the creation of a sub-space extrinsic for later dispatch to the blockchain. + * @param chainSpace - The ChainSpace object containing necessary information for creating the ChainSpace on the blockchain. + * @param authorAccount - The blockchain account used for signing and submitting the transaction. + * @param count - The count of subspaces to be created. + * @param creatorUri - The DID URI of the creator, used to authorize the transaction. + * @returns The prepared extrinsic ready for batch signing and submitting. + */ +export async function prepareCreateSubSpaceExtrinsic( + chainSpace: IChainSpace, + authorAccount: CordKeyringPair, + count: number, + parent: SpaceUri, + creatorUri: DidUri, + signCallback: SignExtrinsicCallback +): Promise { try { const api = ConfigService.get('api') @@ -319,17 +356,16 @@ export async function dispatchSubspaceCreateToChain( authorAccount.address ) - await Chain.signAndSubmitTx(extrinsic, authorAccount) - - return returnObject; + return extrinsic; } catch (error) { throw new SDKErrors.CordDispatchError( - `Error dispatching to chain: "${error}".` + `Error preparing extrinsic: "${error}".` ) } } + /** * Approves a ChainSpace on the CORD blockchain using sudo privileges. * From 693bde520fda4b5664bffc8a1e81356657a081a1 Mon Sep 17 00:00:00 2001 From: Kartikay Date: Mon, 22 Apr 2024 14:11:24 +0530 Subject: [PATCH 2/2] suggestions implemented Signed-off-by: Kartikay --- packages/chain-space/src/ChainSpace.chain.ts | 70 ++++++++++---------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/packages/chain-space/src/ChainSpace.chain.ts b/packages/chain-space/src/ChainSpace.chain.ts index 86ca08e7..90edf5dc 100644 --- a/packages/chain-space/src/ChainSpace.chain.ts +++ b/packages/chain-space/src/ChainSpace.chain.ts @@ -264,6 +264,41 @@ export async function dispatchToChain( } } +/** + * Prepares the creation of a sub-space extrinsic for later dispatch to the blockchain. + * @param chainSpace - The ChainSpace object containing necessary information for creating the ChainSpace on the blockchain. + * @param authorAccount - The blockchain account used for signing and submitting the transaction. + * @param count - The count of transactions permitted to be performed on the chain for the subspace. + * @param creatorUri - The DID URI of the creator, used to authorize the transaction. + * @returns The prepared extrinsic ready for batch signing and submitting. + */ +export async function prepareCreateSubSpaceExtrinsic( + chainSpace: IChainSpace, + authorAccount: CordKeyringPair, + count: number, + parent: SpaceUri, + creatorUri: DidUri, + signCallback: SignExtrinsicCallback +): Promise { + try { + const api = ConfigService.get('api') + + const tx = api.tx.chainSpace.subspaceCreate(chainSpace.digest, count, parent?.replace('space:cord:', '')) + const extrinsic = await Did.authorizeTx( + creatorUri, + tx, + signCallback, + authorAccount.address + ) + + return extrinsic; + } catch (error) { + throw new SDKErrors.CordDispatchError( + `Error preparing extrinsic: "${error}".` + ) + } +} + /** * Dispatches a Sub-ChainSpace creation transaction to the CORD blockchain. * @@ -329,41 +364,6 @@ export async function dispatchSubspaceCreateToChain( } } -/** - * Prepares the creation of a sub-space extrinsic for later dispatch to the blockchain. - * @param chainSpace - The ChainSpace object containing necessary information for creating the ChainSpace on the blockchain. - * @param authorAccount - The blockchain account used for signing and submitting the transaction. - * @param count - The count of subspaces to be created. - * @param creatorUri - The DID URI of the creator, used to authorize the transaction. - * @returns The prepared extrinsic ready for batch signing and submitting. - */ -export async function prepareCreateSubSpaceExtrinsic( - chainSpace: IChainSpace, - authorAccount: CordKeyringPair, - count: number, - parent: SpaceUri, - creatorUri: DidUri, - signCallback: SignExtrinsicCallback -): Promise { - try { - const api = ConfigService.get('api') - - const tx = api.tx.chainSpace.subspaceCreate(chainSpace.digest, count, parent?.replace('space:cord:', '')) - const extrinsic = await Did.authorizeTx( - creatorUri, - tx, - signCallback, - authorAccount.address - ) - - return extrinsic; - } catch (error) { - throw new SDKErrors.CordDispatchError( - `Error preparing extrinsic: "${error}".` - ) - } -} - /**