Skip to content

Commit

Permalink
entries: Add support for revoke/reinstate of registry-entries
Browse files Browse the repository at this point in the history
Signed-off-by: Shreevatsa N <[email protected]>
  • Loading branch information
vatsa287 committed Oct 24, 2024
1 parent 7c2860d commit 9fac27e
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 2 deletions.
20 changes: 20 additions & 0 deletions demo/src/dedi/registry-entries-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
130 changes: 128 additions & 2 deletions packages/entries/src/Entries.chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<EntryUri>} - 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<EntryUri> {

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<EntryUri>} - 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<EntryUri> {

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}".`
)
}
}

0 comments on commit 9fac27e

Please sign in to comment.