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

Created prepareDelegateAuthorizationExtrinsic() and modified dispatchDelegateAuthorization() function #240

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This configuration file was automatically generated by Gitpod.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is not required for the PR.

# 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


101 changes: 89 additions & 12 deletions packages/chain-space/src/ChainSpace.chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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.
Expand Down Expand Up @@ -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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function also requires did.authorizeTx() to be implemented for prepare....

Refer dispatchUpdateTxCapacityToChain().
The idea of prepare and dispatch is to separate authorisingfrom did and signing from account.

permission: PermissionType,
spaceId: string,
delegateId: string,
authId: string
): Promise<SubmittableExtrinsic> {
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.
*
Expand Down Expand Up @@ -577,33 +638,49 @@ export async function dispatchDelegateAuthorization(
signCallback: SignExtrinsicCallback
): Promise<AuthorizationId> {
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 = 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single error is enough as both mean the same.

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.
*
Expand Down
Loading