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

entries: Add support for revoke/reinstate of registry-entries #252

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
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
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}".`
)
}
}