Skip to content

Commit

Permalink
entries: Add capability for updation of registry-entries (#247)
Browse files Browse the repository at this point in the history
This PR,

Adds capability to update the registry-entry.

Updates to have support for elementURI which is a combination of registry-entry ID digest & entry input digest at identifier level. At full will be seen as entry:cord:<IdDigest>:<InputEntryDigest>.

Demo script updates to demonstrate above.

Added `yarn demo-registry` & `yarn demo-registry-entries` for easy run of the dedi scripts, also moved the registry-tx.ts & registry-entries-tx.ts under demo/dedi for easy access as the pile of demo-scripts are growing under root.

Signed-off-by: Shreevatsa N <[email protected]>
  • Loading branch information
vatsa287 authored Oct 15, 2024
1 parent 47f3bde commit a73cc20
Show file tree
Hide file tree
Showing 7 changed files with 523 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Cord from '@cord.network/sdk'
import { createAccount } from './utils/createAccount'
import { createAccount } from '../utils/createAccount'

import {
BN
Expand Down Expand Up @@ -115,12 +115,12 @@ async function main() {
const entryDigest = await Cord.Registries.getDigestFromRawData(stringifiedEntryBlob);

// Create a Registry Entry Properties.
const registryEntryDetails = await Cord.Entries.CreateEntriesProperties(
const registryEntryDetails = await Cord.Entries.createEntriesProperties(
authorIdentity.address,
registry.uri, //registryUri
registry.authorizationUri, //registryAuthUri
entryDigest, //digest
entryBlob, //blob
registry.uri, //registryUri
registry.authorizationUri //registryAuthUri
);

console.log(`\n❄️ Registry Entry Create Details `, registryEntryDetails);
Expand All @@ -131,7 +131,52 @@ async function main() {
authorIdentity,
)

console.log('\n✅ Registry Entry created!');
console.log('\n✅ Registry Entry created!', registryEntry);

// Update the Registry Entry
const updateEntryBlob = {
"name": "New Tech Solutions Ltd.",
"description": "A technology company providing software development and IT consulting services.",
"metadata": {
"category": "Technology",
"registrationDate": "15-06-2022",
"status": "Active",
"registrationNumber": "TSL12345",
"industry": "Technology",
"regulatoryAuthority": "National Business Bureau",
"documentsProvided": [
"Incorporation Certificate",
"Tax Identification Number",
"Proof of Address",
"Board Resolution"
],
"feePaid": "INR500",
"lastUpdated": "01-10-2024"
}
};

const updateStringifiedEntryBlob = JSON.stringify(updateEntryBlob);
const updateEntryDigest = await Cord.Registries.getDigestFromRawData(updateStringifiedEntryBlob);

// Create Update Entry Properties
const registryEntryUpdateDetails = await Cord.Entries.updateEntriesProperties(
registryEntry,
authorIdentity.address,
registry.uri,
registry.authorizationUri,
updateEntryDigest, //digest
updateEntryBlob, //blob
);

console.log(`\n❄️ Registry Entry Update Details `, registryEntryUpdateDetails);

// Dispatch the Property to the chain
const registryEntryUpdate = await Cord.Entries.dispatchUpdateEntryToChain(
registryEntryUpdateDetails,
authorIdentity,
);

console.log('\n✅ Registry Entry updated!', registryEntryUpdate);
}

main()
Expand Down
2 changes: 1 addition & 1 deletion demo/src/registry-tx.ts → demo/src/dedi/registry-tx.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Cord from '@cord.network/sdk'
import { createAccount } from './utils/createAccount'
import { createAccount } from '../utils/createAccount'

import {
BN
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
"test:watch": "yarn test --watch",
"demo-statement": "tsx --no-cache demo/src/func-test.ts",
"demo-network-score": "tsx --no-cache demo/src/network-score-test.ts",
"demo-asset": "tsx --no-cache demo/src/asset-tx.ts"
"demo-asset": "tsx --no-cache demo/src/asset-tx.ts",
"demo-registry": "tsx --no-cache demo/src/dedi/registry-tx.ts",
"demo-registry-entries": "tsx --no-cache demo/src/dedi/registry-entries-tx.ts"
},
"husky": {
"hooks": {
Expand Down
160 changes: 129 additions & 31 deletions packages/entries/src/Entries.chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
*
* The Entries module provides several functions for managing registry entries:
*
* - `dispatchCreateToChain`: Creates a new registry entry in a decentralized registry.
* - `dispatchUpdateToChain`: Updates an existing registry entry with new data.
* - `dispatchRevokeToChain`: Revokes a registry entry, marking it as inactive or invalid.
* - `dispatchReinstateToChain`: Restores a revoked registry entry to an active state.
* - `dispatchCreateEntryToChain`: Creates a new registry entry in a decentralized registry.
* - `dispatchUpdateEntryToChain`: Updates an existing registry entry with new data.
* - `dispatchRevokeEntryToChain`: Revokes a registry entry, marking it as inactive or invalid.
* - `dispatchReinstateEntryToChain`: Restores a revoked registry entry to an active state.
*
* ## Usage
*
Expand All @@ -39,23 +39,47 @@
*
*/
import {
CordKeyringPair,
} from '@cord.network/types';
SDKErrors,
} from '@cord.network/utils';

import { SDKErrors } from '@cord.network/utils';

import {
IRegistryEntry, EntryUri
IRegistryEntry,
EntryUri,
CordKeyringPair,
} from '@cord.network/types';

import { Chain } from '@cord.network/network';

import { Option } from '@polkadot/types';

import { ConfigService } from '@cord.network/config'

import type {
PalletEntriesRegistryEntryDetails,
} from '@cord.network/augment-api'

import {
uriToIdentifier,
uriToEntryIdAndDigest,
} from '@cord.network/identifier'

export async function isRegistryEntryStored(
registryEntryId: string
): Promise<boolean> {
try {
const api = ConfigService.get('api');
const encoded = await api.query.entries.registryEntries(registryEntryId) as Option<PalletEntriesRegistryEntryDetails>;

return !encoded.isNone
} catch (error) {
throw new SDKErrors.CordQueryError(
`Error querying the registry-entry: ${error}`
)
}
}


/**
* Dispatches the creation of a new registry entry to the CORD blockchain by submitting an extrinsic.
*
Expand Down Expand Up @@ -90,29 +114,103 @@ import {
* ```
*/
export async function dispatchCreateEntryToChain(
registryEntryDetails: IRegistryEntry,
authorAccount: CordKeyringPair
registryEntryDetails: IRegistryEntry,
authorAccount: CordKeyringPair
): Promise<EntryUri> {
try {
const api = ConfigService.get('api');

const registryEntryObj = uriToEntryIdAndDigest(registryEntryDetails.uri);

const registryEntryId = registryEntryObj.identifier;
const authorizationId = uriToIdentifier(registryEntryDetails.authorizationUri);

const registryEntryExists = await isRegistryEntryStored(registryEntryId);
if (registryEntryExists) {
throw new SDKErrors.CordDispatchError(
`Registry Entry already exists at URI: "${registryEntryDetails.uri}".`
);
}

const extrinsic = api.tx.entries.create(
registryEntryId,
authorizationId,
registryEntryDetails.digest,
registryEntryDetails.blob,
);

await Chain.signAndSubmitTx(extrinsic, authorAccount);
return registryEntryDetails.uri;
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : JSON.stringify(error);
throw new SDKErrors.CordDispatchError(
`Error dispatching to chain: "${errorMessage}".`
);
}
}


/**
* Dispatches an update operation for a registry entry to the blockchain.
* The function verifies the existence of the entry on-chain and submits an extrinsic
* to update it with the provided digest and blob values.
*
* It ensures that the registry entry exists before proceeding with the update and handles
* errors related to on-chain operations by throwing relevant exceptions.
*
* @param {IRegistryEntry} registryEntryDetails - An object containing the registry entry's details:
* - `uri`: The URI identifying the registry entry to be updated.
* - `digest`: A hash representing the contents of the blob associated with the entry.
* - `blob`: The optional serialized content associated with the registry entry.
* - `authorizationUri`: The URI authorizing the update.
* - `registryUri`: The URI identifying the registry to which the entry belongs.
* - `creatorUri`: The DID URI of the account that initially created the registry entry.
*
* @param {CordKeyringPair} authorAccount - The keypair of the account authorized to sign
* and submit the transaction.
*
* @throws {SDKErrors.CordDispatchError} - Thrown if:
* - The registry entry does not exist on-chain.
* - An error occurs during the transaction dispatch or validation.
*
* @returns {Promise<EntryUri>} - A promise that resolves to the URI of the successfully updated
* registry entry.
*
*/
export async function dispatchUpdateEntryToChain(
registryEntryDetails: IRegistryEntry,
authorAccount: CordKeyringPair
): Promise<EntryUri> {
try {
const api = ConfigService.get('api');

const registryEntryId = uriToIdentifier(registryEntryDetails.uri);
const authorizationId = uriToIdentifier(registryEntryDetails.authorizationUri);

const extrinsic = api.tx.entries.create(
registryEntryId,
authorizationId,
registryEntryDetails.digest,
registryEntryDetails.blob,
);

await Chain.signAndSubmitTx(extrinsic, authorAccount);
return registryEntryDetails.uri;
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : JSON.stringify(error);
throw new SDKErrors.CordDispatchError(
`Error dispatching to chain: "${errorMessage}".`
);
try {
const api = ConfigService.get('api');

const registryEntryObj = uriToEntryIdAndDigest(registryEntryDetails.uri);

const registryEntryId = registryEntryObj.identifier;
const authorizationId = uriToIdentifier(registryEntryDetails.authorizationUri);

const registryEntryExists = await isRegistryEntryStored(registryEntryId);
if (!registryEntryExists) {
throw new SDKErrors.CordDispatchError(
`Registry Entry does not exists at URI: "${registryEntryDetails.uri}".`
);
}

const extrinsic = api.tx.entries.update(
registryEntryId,
authorizationId,
registryEntryDetails.digest,
registryEntryDetails.blob,
);

await Chain.signAndSubmitTx(extrinsic, authorAccount);
return registryEntryDetails.uri;
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : JSON.stringify(error);
throw new SDKErrors.CordDispatchError(
`Error dispatching to chain: "${errorMessage}".`
);
}
}
Loading

0 comments on commit a73cc20

Please sign in to comment.