doken-precomputer
v0.0.5
Published
Precomputes Doken IDs
Readme
doken-precomputer
A utility library to precompute doken token identifiers
The Doken Precomputer is a TypeScript library designed to precompute Doken Identifiers for profiles, registries, and entries on a Cord Blockchain ecosystem. Previously, Doken Identifiers were generated dynamically and relied on event tracking due to their dependency on more than just account hashes or IDs. This library simplifies the process by enabling developers to precompute these identifiers efficiently, reducing reliance on event-based tracking and improving application layer performance. This was required since we moved from static to dynamic generation of identifiers based on genesis-network-id, pallet-index and many more prefixes.
This alleviates the fear of missing events for a particular transaction and reducing having overhead of extra watcher for every application running on top of CORD.
Installation
Install the library and its dependencies:
npm install doken-precomputeror
yarn add doken-precomputerIntegration with SDK/App layer:
This repository is not a standalone SDK, but meant to work with existing application's/sdk's connected with CORD blockchain.
Currently it supports generation of Doken URIs for following pallets: Profile, Registry & Entry.
Profile:
computeProfileDigest(api: ApiPromise, accountAddress: string): Promise<string>Computes a Blake2-256 hash (digest) for a profile based on the provided SS58 account address.
Parameters:
api: An ApiPromise instance connected to the blockchain.accountAddress: The SS58-encoded account address.Returns: A hex-encoded digest string used to create a Profile Doken ID.
computeProfileDokenId(api: ApiPromise, accountAddress: string): Promise<string | null>Computes a Profile Doken Identifier based on the account address. Validates the SS58 address and uses the profile digest to generate the identifier.
Parameters:
api: An ApiPromise instance.accountAddress: The SS58 account address.Returns: A Doken Identifier string or null if an error occurs.
Registry
computeRegistryDigest(api: ApiPromise, tx_hash: string, accountAddress: string): Promise<string>Computes a digest for a registry identifier using a transaction hash and the profile ID associated with the account address. Parameters:
api: An ApiPromise instance.tx_hash: A hex-encoded transaction hash (32 bytes, e.g., 0x1234...).accountAddress: The SS58 account address.Returns: A hex-encoded digest string.
computeRegistryDokenId(api: ApiPromise, tx_hash: string, accountAddress: string): Promise<string | null>Computes a Registry Doken Identifier based on the transaction hash and account address.
Parameters:
api: An ApiPromise instance.tx_hash: A hex-encoded transaction hash.accountAddress: The SS58 account address.Returns: A Doken Identifier string or null if an error occurs.
Entry
computeEntryDigest(api: ApiPromise, tx_hash: string, registryId: string, accountAddress: string): Promise<string>Computes a digest for an entry identifier using a transaction hash, registry ID, and profile ID. Parameters:
api: An ApiPromise instance.tx_hash: A hex-encoded transaction hash.registryId: The ID of the registry the entry is associated with.accountAddress: The SS58 account address.Returns: A hex-encoded digest string.
computeEntryDokenId(api: ApiPromise, tx_hash: string, registryId: string, accountAddress: string): Promise<string | null>Computes an Entry Doken Identifier based on the transaction hash, registry ID, and account address. Parameters:
api: An ApiPromise instance.tx_hash: A hex-encoded transaction hash.registryId: The registry ID.accountAddress: The SS58 account address.Returns: A Doken Identifier string or null if an error occurs.
Example
import { ApiPromise, WsProvider } from '@polkadot/api';
import {
computeProfileDokenId,
computeRegistryDokenId,
computeEntryDokenId,
isValidAddress
} from 'doken-precomputer';
async function main() {
// Initialize the CORD API
const provider = new WsProvider('wss://127.0.0.1:9944'); // Replace with required CORD Wss (Ex. Weavenet)
const api = await ApiPromise.create({ provider });
// Example inputs
const accountAddress = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'; // Replace with valid SS58 address
const txHash = '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'; // Replace with valid tx hash
const registryId = 'registry-123'; // Replace with valid registry ID
try {
// Compute Profile Doken ID
const profileDokenId = await computeProfileDokenId(api, accountAddress);
console.log('Profile Doken ID:', profileDokenId);
// Compute Registry Doken ID
const registryDokenId = await computeRegistryDokenId(api, txHash, accountAddress);
console.log('Registry Doken ID:', registryDokenId);
// Compute Entry Doken ID
const entryDokenId = await computeEntryDokenId(api, txHash, registryId, accountAddress);
console.log('Entry Doken ID:', entryDokenId);
} catch (error) {
console.error('Error:', error instanceof Error ? error.message : error);
} finally {
await api.disconnect();
}
}
main();Usage Cautions
- Dynamic generation of URIs require the chain-state to be configured, meaning some of the prefixes required for generation of the final URI must be set, ex, the pallet-index for the required pallet say Profile must be called. Similarly other prefixes as well.
- This can return Null is such cases, so handle gracefully.
- To have maximum probability, run a test script with all required pallets once before using this repo on a new chain.
