From abcd873250dcadda44d3043a0dd6dfd12d9aab67 Mon Sep 17 00:00:00 2001 From: YASH-YADAV- dynamo Date: Sat, 14 Sep 2024 13:30:41 +0000 Subject: [PATCH 1/3] separated preparation of DelegateAuthorization extrinsic from chain dispatch --- .gitpod.yml | 10 ++ packages/chain-space/src/ChainSpace.chain.ts | 100 ++++++++++++++++--- 2 files changed, 98 insertions(+), 12 deletions(-) create mode 100644 .gitpod.yml diff --git a/.gitpod.yml b/.gitpod.yml new file mode 100644 index 00000000..3a96fa7e --- /dev/null +++ b/.gitpod.yml @@ -0,0 +1,10 @@ +# This configuration file was automatically generated by Gitpod. +# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml) +# and commit this file to your remote git repository to share the goodness with others. + +# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart + +tasks: + - init: yarn install && yarn run build + + diff --git a/packages/chain-space/src/ChainSpace.chain.ts b/packages/chain-space/src/ChainSpace.chain.ts index 01425027..75dfe3c0 100644 --- a/packages/chain-space/src/ChainSpace.chain.ts +++ b/packages/chain-space/src/ChainSpace.chain.ts @@ -53,7 +53,6 @@ import { AUTH_PREFIX, blake2AsHex, Bytes, - Permission, } from '@cord.network/types' import { ConfigService } from '@cord.network/config' import * as Did from '@cord.network/did' @@ -63,6 +62,7 @@ import type { PalletChainSpacePermissions, } from '@cord.network/augment-api' import { Chain } from '@cord.network/network' +import { Permission } from '@cord.network/types'; /** * Checks the existence of a Chain Space on the CORD blockchain. @@ -532,6 +532,67 @@ function dispatchDelegateAuthorizationTx( } } +/** + * Prepares a delegate authorization extrinsic. + * + * @remarks + * Creates an extrinsic for delegating authorization based on the provided parameters. + * + * @param permission - The type of permission being granted. + * @param spaceId - The identifier of the space to which the delegate authorization is being added. + * @param delegateId - The decentralized identifier (DID) of the delegate receiving the authorization. + * @param authId - The identifier of the specific authorization transaction being constructed. + * @returns A promise resolving to the prepared extrinsic. + * @throws {SDKErrors.CordQueryError} - Thrown on error during preparation. + */ +export async function prepareDelegateAuthorizationExtrinsic( + permission: PermissionType, + spaceId: string, + delegateId: string, + authId: string +): Promise> { + try { + const api = ConfigService.get('api'); + + // Prepare the extrinsic based on permission type + switch (permission) { + case Permission.ASSERT: + return api.tx.chainSpace.addDelegate(spaceId, delegateId, authId); + case Permission.DELEGATE: + return api.tx.chainSpace.addDelegator(spaceId, delegateId, authId); + case Permission.ADMIN: + return api.tx.chainSpace.addAdminDelegate(spaceId, delegateId, authId); + default: + throw new SDKErrors.InvalidPermissionError( + `Permission not valid: "${permission}".` + ); + } + } catch (error) { + throw new SDKErrors.CordQueryError( + `Error preparing the delegate authorization extrinsic: ${error}` + ); + } +} + +/** + * Dispatches a delegate authorization request to the CORD blockchain. + * + * @remarks + * This function handles the submission of delegate authorization requests to the CORD blockchain. It manages + * the process of transaction preparation, signing, and submission, facilitating the delegation of specific + * permissions within a ChainSpace. The function ensures that the authorization is correctly dispatched to + * the blockchain with the necessary signatures. + * + * @param permission - The type of permission being granted. + * @param spaceId - The identifier of the space to which the delegate authorization is being added. + * @param delegateId - The decentralized identifier (DID) of the delegate receiving the authorization. + * @param authId - The identifier of the specific authorization transaction being constructed. + * @param sender - The account ID of the sender. + * @returns A promise resolving to the transaction result. + * @throws {SDKErrors.CordDispatchError} - Thrown when there's an error during the dispatch process. + * @throws {SDKErrors.CordQueryError} - Thrown when there's an error preparing the extrinsic. + */ + /** * Dispatches a delegate authorization transaction to the CORD blockchain. * @@ -577,33 +638,48 @@ export async function dispatchDelegateAuthorization( signCallback: SignExtrinsicCallback ): Promise { try { - const spaceId = uriToIdentifier(request.uri) - const delegateId = Did.toChain(request.delegateUri) - const delegatorAuthId = uriToIdentifier(authorizationUri) + // Convert URIs to identifiers + const spaceId: string = uriToIdentifier(request.uri); + const delegateId: string = Did.toChain(request.delegateUri); + const delegatorAuthId: string = uriToIdentifier(authorizationUri); - const tx = dispatchDelegateAuthorizationTx( + // Prepare the transaction + const tx: SubmittableExtrinsic<'promise'> = dispatchDelegateAuthorizationTx( request.permission, spaceId, delegateId, delegatorAuthId - ) + ); + + // Authorize the transaction const extrinsic = await Did.authorizeTx( request.delegatorUri as DidUri, tx, signCallback, authorAccount.address - ) + ); - await Chain.signAndSubmitTx(extrinsic, authorAccount) + // Sign and submit the transaction + const result = await Chain.signAndSubmitTx(extrinsic, authorAccount); - return request.authorizationUri + // Return the result of the transaction + return request.authorizationUri; } catch (error) { - throw new SDKErrors.CordDispatchError( - `Error dispatching delegate authorization: ${error}` - ) + if (error instanceof Error) { + throw new SDKErrors.CordDispatchError( + `Error dispatching delegate authorization: ${error.message}` + ); + } else { + throw new SDKErrors.CordDispatchError( + `Unexpected error dispatching delegate authorization: ${String(error)}` + ); + } } } + + + /** * Decodes the details of a space from its blockchain-encoded representation. * From 3ec29f1ea8184fe216447389ad6d6dd49aae8fa5 Mon Sep 17 00:00:00 2001 From: YASH-YADAV- dynamo Date: Sat, 14 Sep 2024 18:13:26 +0000 Subject: [PATCH 2/3] removed generic typecase --- packages/chain-space/src/ChainSpace.chain.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/chain-space/src/ChainSpace.chain.ts b/packages/chain-space/src/ChainSpace.chain.ts index 75dfe3c0..e1cebb88 100644 --- a/packages/chain-space/src/ChainSpace.chain.ts +++ b/packages/chain-space/src/ChainSpace.chain.ts @@ -550,7 +550,7 @@ export async function prepareDelegateAuthorizationExtrinsic( spaceId: string, delegateId: string, authId: string -): Promise> { +): Promise { try { const api = ConfigService.get('api'); @@ -644,7 +644,7 @@ export async function dispatchDelegateAuthorization( const delegatorAuthId: string = uriToIdentifier(authorizationUri); // Prepare the transaction - const tx: SubmittableExtrinsic<'promise'> = dispatchDelegateAuthorizationTx( + const tx: SubmittableExtrinsic = dispatchDelegateAuthorizationTx( request.permission, spaceId, delegateId, @@ -661,6 +661,7 @@ export async function dispatchDelegateAuthorization( // Sign and submit the transaction const result = await Chain.signAndSubmitTx(extrinsic, authorAccount); + // Return the result of the transaction return request.authorizationUri; From 3204b0583704f25de940f4d71c029ea2858fa568 Mon Sep 17 00:00:00 2001 From: YASH-YADAV-dynamo Date: Tue, 17 Sep 2024 23:09:43 +0530 Subject: [PATCH 3/3] added did.authoriseTx() in prepareDelegateAuthorizationExtrinsic() and made error single --- .gitpod.yml | 10 ------ packages/chain-space/src/ChainSpace.chain.ts | 36 ++++++++++++-------- 2 files changed, 21 insertions(+), 25 deletions(-) delete mode 100644 .gitpod.yml diff --git a/.gitpod.yml b/.gitpod.yml deleted file mode 100644 index 3a96fa7e..00000000 --- a/.gitpod.yml +++ /dev/null @@ -1,10 +0,0 @@ -# This configuration file was automatically generated by Gitpod. -# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml) -# and commit this file to your remote git repository to share the goodness with others. - -# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart - -tasks: - - init: yarn install && yarn run build - - diff --git a/packages/chain-space/src/ChainSpace.chain.ts b/packages/chain-space/src/ChainSpace.chain.ts index e1cebb88..5e36ebc2 100644 --- a/packages/chain-space/src/ChainSpace.chain.ts +++ b/packages/chain-space/src/ChainSpace.chain.ts @@ -545,31 +545,43 @@ function dispatchDelegateAuthorizationTx( * @returns A promise resolving to the prepared extrinsic. * @throws {SDKErrors.CordQueryError} - Thrown on error during preparation. */ + export async function prepareDelegateAuthorizationExtrinsic( permission: PermissionType, spaceId: string, delegateId: string, - authId: string + authId: string, + did: Did.DID // Pass the DID object for authorization ): Promise { try { const api = ConfigService.get('api'); - // Prepare the extrinsic based on permission type + // Prepare the extrinsic based on the permission type + let extrinsic: SubmittableExtrinsic; + switch (permission) { case Permission.ASSERT: - return api.tx.chainSpace.addDelegate(spaceId, delegateId, authId); + extrinsic = api.tx.chainSpace.addDelegate(spaceId, delegateId, authId); + break; case Permission.DELEGATE: - return api.tx.chainSpace.addDelegator(spaceId, delegateId, authId); + extrinsic = api.tx.chainSpace.addDelegator(spaceId, delegateId, authId); + break; case Permission.ADMIN: - return api.tx.chainSpace.addAdminDelegate(spaceId, delegateId, authId); + extrinsic = api.tx.chainSpace.addAdminDelegate(spaceId, delegateId, authId); + break; default: throw new SDKErrors.InvalidPermissionError( `Permission not valid: "${permission}".` ); } + + // Now authorize the extrinsic using the DID (sign it with DID keys) + const authorizedExtrinsic = await did.authorizeTx(extrinsic); + + return authorizedExtrinsic; } catch (error) { throw new SDKErrors.CordQueryError( - `Error preparing the delegate authorization extrinsic: ${error}` + `Error preparing the delegate authorization extrinsic: ${error.message || error}` ); } } @@ -666,15 +678,9 @@ export async function dispatchDelegateAuthorization( // Return the result of the transaction return request.authorizationUri; } catch (error) { - if (error instanceof Error) { - throw new SDKErrors.CordDispatchError( - `Error dispatching delegate authorization: ${error.message}` - ); - } else { - throw new SDKErrors.CordDispatchError( - `Unexpected error dispatching delegate authorization: ${String(error)}` - ); - } + throw new SDKErrors.CordDispatchError( + `Error dispatching delegate authorization: ${String(error)}` + ); } }