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 23, 2024
1 parent 86b0e36 commit 78fe2eb
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 38 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
78 changes: 42 additions & 36 deletions demo/src/func-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { randomUUID } from 'crypto'
import { addNetworkMember } from './utils/createAuthorities'
import { createAccount } from './utils/createAccount'

import { BN } from 'bn.js'

function getChallenge(): string {
return Cord.Utils.UUID.generate()
}
Expand Down Expand Up @@ -42,6 +44,10 @@ async function main() {
// Step 2: Setup Identities
console.log(`\n❄️ Demo Identities (KeyRing)`)

let api = Cord.ConfigService.get('api')
let tx = await api.tx.balances.transferAllowDeath(authorIdentity.address, new BN('1000000000000000'));
await Cord.Chain.signAndSubmitTx(tx, authorityAuthorIdentity);

/* Creating the DIDs for the different parties involved in the demo. */
// Create Verifier DID
const { mnemonic: verifierMnemonic, document: verifierDid } =
Expand Down Expand Up @@ -163,42 +169,42 @@ async function main() {
)
console.log(`✅ Chain Space Approved`)

// Step 3.5: Subspace
const subSpaceProperties = await Cord.ChainSpace.buildFromProperties(
issuerDid.uri
)
console.dir(subSpaceProperties, {
depth: null,
colors: true,
})
const subSpace = await Cord.ChainSpace.dispatchSubspaceCreateToChain(
subSpaceProperties,
issuerDid.uri,
authorIdentity,
200,
space.uri,
async ({ data }) => ({
signature: issuerKeys.authentication.sign(data),
keyType: issuerKeys.authentication.type,
})
)
console.dir(subSpace, {
depth: null,
colors: true,
})
console.log(`\n❄️ SubSpace is created`)

const subSpaceTx = await Cord.ChainSpace.dispatchUpdateTxCapacityToChain(
subSpace.uri,
issuerDid.uri,
authorIdentity,
300,
async ({ data }) => ({
signature: issuerKeys.authentication.sign(data),
keyType: issuerKeys.authentication.type,
})
)
console.log(`\n❄️ SubSpace limit is updated`)
// // Step 3.5: Subspace
// const subSpaceProperties = await Cord.ChainSpace.buildFromProperties(
// issuerDid.uri
// )
// console.dir(subSpaceProperties, {
// depth: null,
// colors: true,
// })
// const subSpace = await Cord.ChainSpace.dispatchSubspaceCreateToChain(
// subSpaceProperties,
// issuerDid.uri,
// authorIdentity,
// 200,
// space.uri,
// async ({ data }) => ({
// signature: issuerKeys.authentication.sign(data),
// keyType: issuerKeys.authentication.type,
// })
// )
// console.dir(subSpace, {
// depth: null,
// colors: true,
// })
// console.log(`\n❄️ SubSpace is created`)

// const subSpaceTx = await Cord.ChainSpace.dispatchUpdateTxCapacityToChain(
// subSpace.uri,
// issuerDid.uri,
// authorIdentity,
// 300,
// async ({ data }) => ({
// signature: issuerKeys.authentication.sign(data),
// keyType: issuerKeys.authentication.type,
// })
// )
// console.log(`\n❄️ SubSpace limit is updated`)

// Step 4: Add Delelegate Two as Registry Delegate
console.log(`\n❄️ Space Delegate Authorization `)
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 78fe2eb

Please sign in to comment.