Skip to content

Commit

Permalink
Registries: Add registries SDK package for CORD pallet-registries (
Browse files Browse the repository at this point in the history
…#245)

This PR,
- Adds the `registries` SDK module which allows for managing a `registry` lifecycle. 
  It supports,
  - `creation`,
  - `updation`,
  -  `revocation`,
  -  `reinstating revoked registry`,
  -  `archival`,
  - `restoring a archived registry`.
  
  It also supports `delegation management` for a `registry`.
  - `addition of admin delegate`,
  - `addition of delegate with delegate permission`,
  - `addition of delegate with assert permission`,
  - `removal of a delegate`.
  
- Adds a `registry-tx.ts` script which showcases all above functionalities.

Signed-off-by: Shreevatsa N <[email protected]>
  • Loading branch information
vatsa287 authored Oct 4, 2024
1 parent 98fc7b5 commit 44c338e
Show file tree
Hide file tree
Showing 15 changed files with 1,848 additions and 2 deletions.
288 changes: 288 additions & 0 deletions demo/src/registry-tx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
import * as Cord from '@cord.network/sdk'
import { createAccount } from './utils/createAccount'

import {
BN
} from 'bn.js';

async function getBalance(address: string, api) {
Cord.ConfigService.set({ submitTxResolveOn: Cord.Chain.IS_IN_BLOCK })

const { data: balance } = await api.query.system.account(address);
return balance.free.toString(); // Returns free balance as a string
}

async function main() {
const networkAddress = process.env.NETWORK_ADDRESS
? process.env.NETWORK_ADDRESS
: 'ws://127.0.0.1:9944'

Cord.ConfigService.set({ submitTxResolveOn: Cord.Chain.IS_IN_BLOCK })
await Cord.connect(networkAddress)

const api = Cord.ConfigService.get('api');

// Step 1: Setup Membership
// Setup transaction author account - CORD Account.

console.log(`\n❄️ New Network Member`)
const authorityAuthorIdentity = Cord.Utils.Crypto.makeKeypairFromUri(
process.env.ANCHOR_URI ? process.env.ANCHOR_URI : '//Alice',
'sr25519'
)

// Setup network member account.
const { account: authorIdentity } = await createAccount()
console.log(`🏦 Member (${authorIdentity.type}): ${authorIdentity.address}`)

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

// Create a Registry.
const blob = {
"name": "Companies Registry",
"description": "A centralized registry that tracks the registration, incorporation status, and key business details of companies across various industries.",
"metadata": {
"category": "business",
"totalCompaniesRegistered": 15000,
"industriesCovered": [
"Technology",
"Healthcare",
"Renewable Energy",
"Finance",
"Manufacturing"
],
"lastUpdated": "01-10-2024",
"regulatoryAuthority": "National Business Bureau",
"registrationRequirements": {
"documentsNeeded": [
"Incorporation Certificate",
"Tax Identification Number",
"Proof of Address",
"Board Resolution"
],
"feeStructure": {
"smallBusiness": "INR500",
"mediumBusiness": "INR1000",
"largeBusiness": "INR5000"
}
}
}
};
const stringified_blob = JSON.stringify(blob);
const digest = await Cord.Registries.getDigestFromRawData(stringified_blob);

const registryDetails = await Cord.Registries.registryCreateProperties(
authorIdentity.address,
digest, //digest
null, //schemaId
blob, //blob
);

console.log(`\n❄️ Registry Create Details `, registryDetails);

const registry = await Cord.Registries.dispatchCreateRegistryToChain(
registryDetails,
authorIdentity,
);

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

// Update a existing Registry.
const new_blob = {
"name": "Companies Registry - A",
"description": "A centralized registry that tracks the registration, incorporation status, and key business details of companies across various industries.",
"metadata": {
"category": "business",
"totalCompaniesRegistered": 15000,
"industriesCovered": [
"Technology",
"Healthcare",
"Renewable Energy",
"Finance",
"Manufacturing"
],
"lastUpdated": "01-10-2024",
"regulatoryAuthority": "National Business Bureau",
"registrationRequirements": {
"documentsNeeded": [
"Incorporation Certificate",
"Tax Identification Number",
"Proof of Address",
"Board Resolution"
],
"feeStructure": {
"smallBusiness": "INR500",
"mediumBusiness": "INR1000",
"largeBusiness": "INR5000"
}
}
}
};
const new_stringified_blob = JSON.stringify(new_blob);
const new_digest = await Cord.Registries.getDigestFromRawData(new_stringified_blob);

const registryUpdateDetails = await Cord.Registries.registryUpdateProperties(
registry.uri,
registry.authorizationUri,
authorIdentity.address,
new_digest, //digest
new_blob, //blob
);

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

const registry_update = await Cord.Registries.dispatchUpdateRegistryToChain(
registryUpdateDetails,
authorIdentity,
);

console.log('\n✅ Registry updated!');

// Revoke a Registry
console.log(`\n❄️ Revoking Registry `, registry.uri);
const registry_revoke = await Cord.Registries.dispatchRevokeToChain(
registry.uri,
registry.authorizationUri,
authorIdentity
);
console.log('✅ Registry Revoked!');

// Reinstate a Revoked Registry
console.log(`\n❄️ Reinstating Revoked Registry `, registry.uri);
const registry_reinstate = await Cord.Registries.dispatchReinstateToChain(
registry.uri,
registry.authorizationUri,
authorIdentity
);
console.log('✅ Revoked Registry Reinstated!');

// Archive a Registry
console.log(`\n❄️ Archiving Registry `, registry.uri);
const registry_archive = await Cord.Registries.dispatchArchiveToChain(
registry.uri,
registry.authorizationUri,
authorIdentity
);
console.log('✅ Registry Archived!');

// Restore a Archived Registry
console.log(`\n❄️ Restoring Archived Registry `, registry.uri);
const registry_restore = await Cord.Registries.dispatchRestoreToChain(
registry.uri,
registry.authorizationUri,
authorIdentity
);
console.log('✅ Archived Registry Restored!');

// Setup a account to be added as a `ASSERT` delegate.
const { account: assertIdentity } = await createAccount()
console.log(`\n🏦 Delegate Member (${assertIdentity.type}): ${assertIdentity.address}`)

console.log(`\n❄️ Registry Assert Authorization `);

// 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 delegateAssertAuthorizationUri = await Cord.Registries.dispatchDelegateAuthorization(
registryAssertAuthProperties,
registry.authorizationUri,
authorIdentity
)

console.log(`\n✅ Registry Authorization added with ASSERT permission - ${delegateAssertAuthorizationUri} - added!`)

// Setup a account to be added as a `DELEGATE` delegate.
const { account: delegateIdentity } = await createAccount()
console.log(`\n🏦 Delegate Member (${delegateIdentity.type}): ${delegateIdentity.address}`)

console.log(`\n❄️ Registry Delegate Authorization `);

// Add a delegate with DELEGATE permission
const delegatePermission: Cord.RegistryPermissionType = Cord.RegistryPermission.DELEGATE;
const registryDelegateAuthProperties =
await Cord.Registries.registryAuthorizationProperties(
registry.uri,
delegateIdentity.address,
delegatePermission,
authorIdentity.address
)

console.dir(registryDelegateAuthProperties, {
depth: null,
colors: true,
})

const delegateAuthorizationUri = await Cord.Registries.dispatchDelegateAuthorization(
registryDelegateAuthProperties,
registry.authorizationUri,
authorIdentity
)

console.log(`\n✅ Registry Authorization added with DELEGATE permission - ${delegateAuthorizationUri} - added!`)

// Setup a account to be added as a `DELEGATE` delegate.
const { account: adminIdentity } = await createAccount()
console.log(`\n🏦 Delegate Member (${adminIdentity.type}): ${adminIdentity.address}`)

console.log(`\n❄️ Registry Admin Authorization `);

// Add a delegate with DELEGATE permission
const adminPermission: Cord.RegistryPermissionType = Cord.RegistryPermission.ADMIN;
const registryAdminAuthProperties =
await Cord.Registries.registryAuthorizationProperties(
registry.uri,
adminIdentity.address,
adminPermission,
authorIdentity.address
)

console.dir(registryAdminAuthProperties, {
depth: null,
colors: true,
})

const delegateAdminAuthorizationUri = await Cord.Registries.dispatchDelegateAuthorization(
registryAdminAuthProperties,
registry.authorizationUri,
authorIdentity
)

console.log(`\n✅ Registry Authorization added with ADMIN permission - ${delegateAdminAuthorizationUri} - added!`)

console.log(`\n❄️ Remove Registry Assert Authorization `);

// Remove a delegate with ASSERT permission
const removeAuthObj = await Cord.Registries.dispatchRemoveDelegateToChain(
registry.uri,
delegateAssertAuthorizationUri,
registry.authorizationUri,
authorIdentity
)

console.log(`\n✅ Registry ASSERT Authorization removed - ${delegateAssertAuthorizationUri} - removed!`)

console.log("Balance of Registry Creator after all transactions", await getBalance(authorIdentity.address, api));
}

main()
.then(() => console.log('\nBye! 👋 👋 👋 '))
.finally(Cord.disconnect)

process.on('SIGINT', async () => {
console.log('\nBye! 👋 👋 👋 \n')
Cord.disconnect()
process.exit(0)
})
10 changes: 10 additions & 0 deletions packages/identifier/src/Identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ import {
u8aConcat,
u8aToU8a,
stringToU8a,
REGISTRY_IDENT,
REGISTRY_PREFIX,
REGISTRYAUTH_IDENT,
REGISTRYAUTH_PREFIX,
} from '@cord.network/types'
import { SDKErrors } from '@cord.network/utils'

Expand All @@ -85,6 +89,8 @@ const VALID_IDENTS = new Set([
ACCOUNT_IDENT,
ASSET_IDENT,
ASSET_INSTANCE_IDENT,
REGISTRY_IDENT,
REGISTRYAUTH_IDENT,
])

const VALID_PREFIXES = [
Expand All @@ -95,6 +101,8 @@ const VALID_PREFIXES = [
AUTH_PREFIX,
ACCOUNT_PREFIX,
ASSET_PREFIX,
REGISTRY_PREFIX,
REGISTRYAUTH_PREFIX,
]

const IDENT_TO_PREFIX_MAP = new Map([
Expand All @@ -106,6 +114,8 @@ const IDENT_TO_PREFIX_MAP = new Map([
[ACCOUNT_IDENT, ACCOUNT_PREFIX],
[ASSET_IDENT, ASSET_PREFIX],
[ASSET_INSTANCE_IDENT, ASSET_PREFIX],
[REGISTRY_IDENT, REGISTRY_PREFIX],
[REGISTRYAUTH_IDENT, REGISTRYAUTH_PREFIX]
])

/**
Expand Down
42 changes: 42 additions & 0 deletions packages/registries/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@cord.network/registries",
"version": "0.9.3-1rc4",
"description": "CORD Registry Management",
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js",
"types": "./lib/cjs/index.d.ts",
"exports": {
".": {
"import": "./lib/esm/index.js",
"require": "./lib/cjs/index.js"
}
},
"files": [
"lib/**/*"
],
"scripts": {
"clean": "rimraf ./lib",
"build": "yarn clean && yarn build:ts",
"build:ts": "yarn build:cjs && yarn build:esm",
"build:cjs": "tsc --declaration -p tsconfig.build.json && echo '{\"type\":\"commonjs\"}' > ./lib/cjs/package.json",
"build:esm": "tsc --declaration -p tsconfig.esm.json && echo '{\"type\":\"module\"}' > ./lib/esm/package.json"
},
"repository": "github:dhiway/cord-js",
"engines": {
"node": ">=20.0"
},
"author": "Dhiway",
"bugs": "https://github.com/dhiway/cord.js/issues",
"homepage": "https://github.com/dhiway/cord.js#readme",
"devDependencies": {
"rimraf": "^5.0.5",
"typescript": "^5.3.3"
},
"dependencies": {
"@cord.network/config": "workspace:*",
"@cord.network/identifier": "workspace:*",
"@cord.network/network": "workspace:*",
"@cord.network/types": "workspace:*",
"@cord.network/utils": "workspace:*"
}
}
Loading

0 comments on commit 44c338e

Please sign in to comment.