From f876c9fdb1730d5aed4d0d57bb3bd9ff44f5e834 Mon Sep 17 00:00:00 2001 From: Shreevatsa N Date: Thu, 24 Oct 2024 10:25:52 +0530 Subject: [PATCH] entries: Add support for revoke/reinstate of registry-entries Signed-off-by: Shreevatsa N --- demo/src/dedi/registry-entries-tx.ts | 20 ++++ packages/entries/src/Entries.chain.ts | 130 +++++++++++++++++++++++++- 2 files changed, 148 insertions(+), 2 deletions(-) diff --git a/demo/src/dedi/registry-entries-tx.ts b/demo/src/dedi/registry-entries-tx.ts index c7dc435c..bf41d8c3 100644 --- a/demo/src/dedi/registry-entries-tx.ts +++ b/demo/src/dedi/registry-entries-tx.ts @@ -207,6 +207,26 @@ async function main() { ); console.log('\n✅ Registry Entry updated!', registryEntryUpdate); + + console.log(`\n❄️ Revoking Registry Entry`, registryEntryUpdateDetails.uri); + + const registryEntryRevoke = await Cord.Entries.dispatchRevokeEntryToChain( + registryEntryUpdateDetails.uri, + registryEntryUpdateDetails.authorizationUri, + authorIdentity, + ); + + console.log('\n✅ Registry Entry revoked!', registryEntryRevoke); + + console.log(`\n❄️ Reinstating Revoked Registry Entry`, registryEntryUpdateDetails.uri); + + const registryEntryReinstate = await Cord.Entries.dispatchReinstateEntryToChain( + registryEntryUpdateDetails.uri, + registryEntryUpdateDetails.authorizationUri, + authorIdentity, + ); + + console.log('\n✅ Registry Entry reinstated!', registryEntryReinstate); } main() diff --git a/packages/entries/src/Entries.chain.ts b/packages/entries/src/Entries.chain.ts index 148d2532..611ad0e4 100644 --- a/packages/entries/src/Entries.chain.ts +++ b/packages/entries/src/Entries.chain.ts @@ -47,12 +47,12 @@ import { IRegistryEntry, EntryUri, CordKeyringPair, + Option, + RegistryAuthorizationUri, } from '@cord.network/types'; import { Chain } from '@cord.network/network'; -import { Option } from '@polkadot/types'; - import { ConfigService } from '@cord.network/config' import type { @@ -214,3 +214,129 @@ export async function dispatchUpdateEntryToChain( ); } } + + +/** + * Revokes a registry entry on the blockchain by dispatching an extrinsic. + * Ensures that the registry entry exists before attempting revocation. + * + * @param {EntryUri} registryEntryUri - The URI that identifies the registry entry to revoke. + * @param {RegistryAuthorizationUri} authorizationUri - The URI identifying the authorization for revocation. + * @param {CordKeyringPair} authorAccount - The account that signs and submits the transaction. + * + * @returns {Promise} - Resolves to the same `EntryUri` if revocation succeeds. + * + * @throws {SDKErrors.CordDispatchError} - Throws if the registry entry does not exist or if an error occurs during dispatch. + * + * @example + * try { + * const revokedEntryUri = await dispatchRevokeToChain( + * 'entry-uri', + * 'authorization-uri', + * authorAccount + * ); + * console.log(`Successfully revoked: ${revokedEntryUri}`); + * } catch (error) { + * console.error(`Revocation failed: ${error.message}`); + * } + */ +export async function dispatchRevokeEntryToChain( + registryEntryUri: EntryUri, + authorizationUri: RegistryAuthorizationUri, + authorAccount: CordKeyringPair, +): Promise { + + const registryEntryObj = uriToEntryIdAndDigest(registryEntryUri); + + const registryEntryId = registryEntryObj.identifier; + const authorizationId = uriToIdentifier(authorizationUri); + + const registryEntryExists = await isRegistryEntryStored(registryEntryId); + if (!registryEntryExists) { + throw new SDKErrors.CordDispatchError( + `Registry Entry does not exists at URI: "${registryEntryUri}".` + ); + } + + try { + const api = ConfigService.get('api') + + const extrinsic = api.tx.entries.revoke( + registryEntryId, + authorizationId, + ); + + await Chain.signAndSubmitTx(extrinsic, authorAccount); + + return registryEntryUri + } catch(error) { + const errorMessage = + error instanceof Error ? error.message : JSON.stringify(error) + throw new SDKErrors.CordDispatchError( + `Error dispatching to chain: "${errorMessage}".` + ) + } +} + + +/** + * Reinstates a previously revoked registry entry on the blockchain. + * Validates the existence of the registry entry before attempting the reinstatement. + * + * @param {EntryUri} registryEntryUri - The URI that identifies the registry entry to reinstate. + * @param {RegistryAuthorizationUri} authorizationUri - The URI used to authorize the reinstatement. + * @param {CordKeyringPair} authorAccount - The account used to sign and submit the reinstatement transaction. + * + * @returns {Promise} - Resolves to the same `EntryUri` if the reinstatement succeeds. + * + * @throws {SDKErrors.CordDispatchError} - Throws if the registry entry does not exist or an error occurs during the transaction. + * + * @example + * try { + * const reinstatedEntryUri = await dispatchReinstateToChain( + * 'entry-uri', + * 'authorization-uri', + * authorAccount + * ); + * console.log(`Successfully reinstated: ${reinstatedEntryUri}`); + * } catch (error) { + * console.error(`Reinstatement failed: ${error.message}`); + * } + */ +export async function dispatchReinstateEntryToChain( + registryEntryUri: EntryUri, + authorizationUri: RegistryAuthorizationUri, + authorAccount: CordKeyringPair, +): Promise { + + const registryEntryObj = uriToEntryIdAndDigest(registryEntryUri); + + const registryEntryId = registryEntryObj.identifier; + const authorizationId = uriToIdentifier(authorizationUri); + + const registryEntryExists = await isRegistryEntryStored(registryEntryId); + if (!registryEntryExists) { + throw new SDKErrors.CordDispatchError( + `Registry Entry does not exists at URI: "${registryEntryUri}".` + ); + } + + try { + const api = ConfigService.get('api') + + const extrinsic = api.tx.entries.reinstate( + registryEntryId, + authorizationId, + ); + + await Chain.signAndSubmitTx(extrinsic, authorAccount); + + return registryEntryUri + } catch(error) { + const errorMessage = + error instanceof Error ? error.message : JSON.stringify(error) + throw new SDKErrors.CordDispatchError( + `Error dispatching to chain: "${errorMessage}".` + ) + } +}