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

[bug] Chainspace: Add isChainSpaceStored before creation #219

Merged
merged 1 commit into from
May 22, 2024
Merged
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
71 changes: 40 additions & 31 deletions packages/chain-space/src/ChainSpace.chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,40 @@ export async function getUriForSpace(
return chainSpaceDetails
}

/**
* Prepares the creation of a chain space extrinsic for later dispatch to the blockchain.
* @param chainSpace - The ChainSpace object containing necessary information for creating the ChainSpace on the blockchain.
* @param creatorUri - The DID URI of the creator, used to authorize the transaction.
* @param signCallback - The callback function for signing the transaction.
* @param authorAccount - The blockchain account used for signing and submitting the transaction.
* @returns The prepared extrinsic ready for batch signing and submitting.
*/
export async function prepareCreateSpaceExtrinsic(
chainSpace: IChainSpace,
creatorUri: DidUri,
signCallback: SignExtrinsicCallback,
authorAccount: CordKeyringPair
): Promise<SubmittableExtrinsic> {
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 of chainspace: "${error}".`
);
}
}

/**
* Dispatches a ChainSpace creation transaction to the CORD blockchain.
*
Expand Down Expand Up @@ -229,33 +263,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 prepareCreateSpaceExtrinsic(
chainSpace: IChainSpace,
creatorUri: DidUri,
signCallback: SignExtrinsicCallback,
authorAccount: CordKeyringPair
): Promise<SubmittableExtrinsic> {
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 of chainspace: "${error}".`
);
}
}

export async function dispatchToChain(
chainSpace: IChainSpace,
creatorUri: DidUri,
Expand All @@ -268,10 +275,11 @@ export async function dispatchToChain(
}

try {

const extrinsic = await prepareCreateSpaceExtrinsic(chainSpace, creatorUri, signCallback, authorAccount)
await Chain.signAndSubmitTx(extrinsic, authorAccount)

const exists = await isChainSpaceStored(chainSpace.uri)
if (!exists) {
const extrinsic = await prepareCreateSpaceExtrinsic(chainSpace, creatorUri, signCallback, authorAccount)
await Chain.signAndSubmitTx(extrinsic, authorAccount)
}
return returnObject
} catch (error) {
throw new SDKErrors.CordDispatchError(
Expand All @@ -284,6 +292,7 @@ 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 parent - The chainspace under which the sub-space will be created.
* @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.
Expand Down