Skip to content

Commit

Permalink
Entries: Feature to transfer the ownership of Registry Entry
Browse files Browse the repository at this point in the history
Signed-off-by: Shreevatsa N <[email protected]>
  • Loading branch information
vatsa287 committed Dec 30, 2024
1 parent cecdad9 commit 524b7be
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 6 deletions.
39 changes: 39 additions & 0 deletions demo/src/dedi/registry-entries-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
95 changes: 92 additions & 3 deletions packages/entries/src/Entries.chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ import {
RegistryAuthorizationUri,
IRegistryEntryChainStorage,
RegistryUri,
DidUri
DidUri,
CordAddress
} from '@cord.network/types';

import { Chain } from '@cord.network/network';
Expand Down Expand Up @@ -398,8 +399,6 @@ export function decodeRegistryEntryDetailsFromChain(
) as RegistryUri
};

console.log("chainRegistryEntry after", registryEntry);

return registryEntry;
}

Expand Down Expand Up @@ -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<EntryUri>}
* - 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<EntryUri> {
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)}".`
);
}
}
4 changes: 1 addition & 3 deletions packages/entries/src/Entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 524b7be

Please sign in to comment.