From 524b7bec89803ebf88089d7b51dff47b854e4738 Mon Sep 17 00:00:00 2001 From: Shreevatsa N Date: Mon, 30 Dec 2024 16:28:30 +0530 Subject: [PATCH] Entries: Feature to transfer the ownership of Registry Entry Signed-off-by: Shreevatsa N --- demo/src/dedi/registry-entries-tx.ts | 39 +++++++++++ packages/entries/src/Entries.chain.ts | 95 ++++++++++++++++++++++++++- packages/entries/src/Entries.ts | 4 +- 3 files changed, 132 insertions(+), 6 deletions(-) diff --git a/demo/src/dedi/registry-entries-tx.ts b/demo/src/dedi/registry-entries-tx.ts index 1401aadd..2dcb0cec 100644 --- a/demo/src/dedi/registry-entries-tx.ts +++ b/demo/src/dedi/registry-entries-tx.ts @@ -242,6 +242,45 @@ async function main() { } else { console.log(`šŸš« Verification failed! - "${verificationResult.message}" šŸš«`) } + + // Setup a account to be added as a new onwer. + const { account: assertIdentity } = await createAccount() + console.log(`\nšŸ¦ New Owner Member (${assertIdentity.type})`) + + // Add a delegate with ASSERT permission + const assertPermission: Cord.RegistryPermissionType = Cord.RegistryPermission.ASSERT; + const registryAssertAuthProperties = + await Cord.Registries.registryAuthorizationProperties( + registry.uri, + assertIdentity.address, + assertPermission, + authorIdentity.address + ) + + console.dir(registryAssertAuthProperties, { + depth: null, + colors: true, + }) + + const newOwnerAssertAuthorizationUri = await Cord.Registries.dispatchDelegateAuthorization( + registryAssertAuthProperties, + registry.authorizationUri, + authorIdentity + ) + + console.log(`\nāœ… Registry Authorization added with ASSERT permission - ${newOwnerAssertAuthorizationUri} - added!`) + + // Transfer the ownership of the entry to new owner + const entryOwnershipUpdatedUri = + await Cord.Entries.dispatchUpdateOwnershipToChain( + registryEntryDetails.uri, + registryEntryDetails.authorizationUri, + assertIdentity.address, + newOwnerAssertAuthorizationUri, + authorIdentity + ); + + console.log(`\nāœ… Registry Entry Ownership Updated - ${assertIdentity.AccountId}!`) } main() diff --git a/packages/entries/src/Entries.chain.ts b/packages/entries/src/Entries.chain.ts index 5c53db69..7d34cd28 100644 --- a/packages/entries/src/Entries.chain.ts +++ b/packages/entries/src/Entries.chain.ts @@ -52,7 +52,8 @@ import { RegistryAuthorizationUri, IRegistryEntryChainStorage, RegistryUri, - DidUri + DidUri, + CordAddress } from '@cord.network/types'; import { Chain } from '@cord.network/network'; @@ -398,8 +399,6 @@ export function decodeRegistryEntryDetailsFromChain( ) as RegistryUri }; - console.log("chainRegistryEntry after", registryEntry); - return registryEntry; } @@ -495,3 +494,93 @@ export async function fetchRegistryEntryDetailsFromChain( return entryDetails; } + + +/** + * Dispatches an extrinsic to update the ownership of a registry entry on the blockchain. + * This function sends an on-chain transaction to transfer ownership of a specified registry + * entry to a new owner, using the provided authorization identifiers. + * + * @param {EntryUri} registryEntryUri - + * The URI of the registry entry for which ownership is being updated. + * + * @param {RegistryAuthorizationUri} authorizationUri - + * The URI of the authorization linked to the current owner of the registry entry. + * + * @param {CordAddress} newOwnerAccount - + * The address of the new owner to whom ownership is being transferred. + * + * @param {RegistryAuthorizationUri} newOwnerAuthorizationUri - + * The URI of the authorization linked to the new owner. + * + * @param {CordKeyringPair} authorAccount - + * The account of the current owner or authorized delegate, used to authorize and sign the transaction. + * + * @returns {Promise} + * - Returns a promise that resolves to the URI of the registry entry after successfully updating ownership. + * + * @throws {SDKErrors.CordDispatchError} + * Throws an error if the registry entry does not exist or if there is any issue while dispatching the extrinsic to the chain. + * + * @example + * // Example Usage: + * const registryEntryUri = "someEntryUri"; + * const authorizationUri = "someAuthorizationUri"; + * const newOwnerAccount = "5Dw8p5aZxtLKLDBnYP5r9ePNSwD7FozknBhXtXaV4awZ6kfK"; + * const newOwnerAuthorizationUri = "newOwnerAuthorizationUri"; + * const authorAccount = authorKeyringPair; + * + * try { + * const updatedUri = await dispatchUpdateOwnershipToChain( + * registryEntryUri, + * authorizationUri, + * newOwnerAccount, + * newOwnerAuthorizationUri, + * authorAccount + * ); + * console.log(updatedUri); // Outputs the updated registry entry URI. + * } catch (error) { + * console.error(error.message); // Handle the error accordingly. + * } + * + */ +export async function dispatchUpdateOwnershipToChain( + registryEntryUri: EntryUri, + authorizationUri: RegistryAuthorizationUri, + newOwnerAccount: CordAddress, + newOwnerAuthorizationUri: RegistryAuthorizationUri, + authorAccount: CordKeyringPair, +): Promise { + try { + + const api = ConfigService.get('api'); + const registryEntryObj = uriToEntryIdAndDigest(registryEntryUri); + + const registryEntryId = registryEntryObj.identifier; + const authorizationId = uriToIdentifier(authorizationUri); + const newOwnerAuthorizationId = uriToIdentifier(newOwnerAuthorizationUri); + + const registryEntryExists = await isRegistryEntryStored(registryEntryId); + if (!registryEntryExists) { + throw new SDKErrors.CordDispatchError( + `Registry Entry does not exists at URI: "${registryEntryUri}".` + ); + } + + const extrinsic = api.tx.entries.updateOwnership( + registryEntryId, + authorizationId, + newOwnerAccount, + newOwnerAuthorizationId, + ); + + 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: "${JSON.stringify(errorMessage)}".` + ); + } +} diff --git a/packages/entries/src/Entries.ts b/packages/entries/src/Entries.ts index d76958c4..819be39c 100644 --- a/packages/entries/src/Entries.ts +++ b/packages/entries/src/Entries.ts @@ -434,9 +434,7 @@ export async function verifyAgainstInputProperties( message: `Registry Entry details for "${digest}" not found.`, } } - - console.log("uri", entryUri, "digest", digest, "creatorUri", creatorUri, "registryUri", registryUri); - + if (digest !== registryEntryStatus.digest) { return { isValid: false,