Precomputes Doken IDs
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.
```
npm install doken-precomputer
or
``
yarn add doken-precomputer
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.
- `computeProfileDigest(api: ApiPromise, accountAddress: string): Promise`
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`
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.
- `computeRegistryDigest(api: ApiPromise, tx_hash: string, accountAddress: string): Promise`
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`
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.
- `computeEntryDigest(api: ApiPromise, tx_hash: string, registryId: string, accountAddress: string): Promise`
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`
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.
`typescript
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();
``