WebAssembly bindings for Polymesh DART (Decentralized, Anonymous and Regulation-Friendly Tokenization).
This package provides JavaScript/TypeScript bindings for the Polymesh DART protocol, enabling privacy-preserving asset transfers with zero-knowledge proofs in web browsers and Node.js environments.
- Account Management: Generate and manage DART account keys
- Zero-Knowledge Proofs: Generate privacy-preserving proofs for confidential transactions
- Asset Handling: Work with asset states and mediators/auditors
- Settlement Operations: Create and manage confidential asset settlements
- TypeScript Support: Full TypeScript type definitions included
- Framework Flexible: Works with or without on-chain integration (use with Polkadot.js, PolymeshClient, or any other chain client)
npm install @polymesh/dart-wasmThis library focuses on zero-knowledge proof generation for confidential transactions. You have two options for chain integration:
Option 1: Use Polkadot.js (Recommended for most developers)
- Use Polkadot.js to query chain state and submit transactions
- Use
polymesh-dart-wasmto generate proofs - This approach is more flexible and requires fewer dependencies
- Perfect if you're already using Polkadot.js in your project
Option 2: Use PolymeshClient (For testing/development - requires special build)
PolymeshClientandPolymeshSignerprovide a convenient wrapper around Polymesh-specific operations- Best for rapid testing and development
- Requires building with
build_with_rust_client.shscript - See DEVELOPMENT.md for detailed integration examples
The core proof generation APIs (AccountKeys, AssetState, SettlementBuilder, etc.) work independently and are the primary focus of this library.
import init, { AccountKeys, generateRandomSeed } from '@polymesh/dart-wasm';
// Initialize the WASM module
await init();
// Generate a random seed for account keys
const seed = generateRandomSeed();
console.log('Seed:', seed);
// Create account keys from the seed
const accountKeys = new AccountKeys(seed);
// Get public keys
const publicKeys = accountKeys.publicKeys();
console.log('Public keys:', publicKeys.toJson());
// Export keys as bytes
const keyBytes = accountKeys.toBytes();
console.log('Key bytes length:', keyBytes.length);const { AccountKeys, generateRandomSeed } = require('@polymesh/dart-wasm');
// Generate account keys
const seed = generateRandomSeed();
const accountKeys = AccountKeys.fromSeed("my-secret-seed");
const publicKeys = accountKeys.publicKeys();
console.log('Account public key:', publicKeys.accountPublicKey().toJson());
console.log('Encryption public key:', publicKeys.encryptionPublicKey().toJson());import init, {
AccountKeys,
AccountPublicKeys,
AssetState,
generateRandomSeed
} from '@polymesh/dart-wasm';
await init();
// Generate keys with full type safety
const seed: string = generateRandomSeed();
const keys: AccountKeys = new AccountKeys(seed);
const pubKeys: AccountPublicKeys = keys.publicKeys();- Rust (latest stable)
- wasm-pack (
cargo install wasm-pack)
# Build for all targets (web, node, bundler)
./build.sh
# Or build for specific target
wasm-pack build --target web
wasm-pack build --target nodejs
wasm-pack build --target bundlerThe built packages will be in:
pkg/- For bundlers (webpack, rollup, etc.)pkg-web/- For web browsers (ES modules)pkg-node/- For Node.js
Generates a cryptographically secure random seed (32 bytes, hex-encoded).
Represents an account's private keys.
new AccountKeys(seedHex: string)- Create from 64-character hex seedfromSeed(seed: string)- Create from any string (will be hashed)fromBytes(bytes: Uint8Array)- Import from SCALE-encoded bytestoBytes(): Uint8Array- Export as SCALE-encoded bytespublicKeys(): AccountPublicKeys- Get corresponding public keys
Represents an account's public keys.
fromBytes(bytes: Uint8Array)- Import from bytestoBytes(): Uint8Array- Export as bytesaccountPublicKey(): AccountPublicKey- Get account public keyencryptionPublicKey(): EncryptionPublicKey- Get encryption public key
Represents the confidential state of an account for a specific asset.
fromBytes(bytes: Uint8Array)- Import from SCALE-encoded bytestoBytes(): Uint8Array- Export as SCALE-encoded bytesassetId(): number- Get the asset IDbalance(): number- Get current balanceleafIndex(): number- Get the leaf index in the account tree (after committing)commitPendingState(leafIndex: number)- Commit pending state after on-chain transactiongetProof(): Uint8Array- Get current proof state
Represents account commitment stored in the account tree.
fromBytes(bytes: Uint8Array)- Import from SCALE-encoded bytestoBytes(): Uint8Array- Export as SCALE-encoded bytesassetId(): number- Get asset IDbalance(): number- Get balancecounter(): number- Get transaction counter
Represents a confidential asset's state, including mediators and auditors.
new AssetState(assetId: number, mediators: Array, auditors: Array)- Create new asset state- Automatically converts chain data (Uint8Array, hex strings, or EncryptionPublicKey objects)
- Perfect for data from Polkadot.js queries
fromBytes(bytes: Uint8Array)- Import from SCALE-encoded bytestoBytes(): Uint8Array- Export as SCALE-encoded bytesassetId(): number- Get asset IDmediatorCount(): number- Get number of mediatorsauditorCount(): number- Get number of auditors
Builds a confidential settlement with multiple transfer legs.
new SettlementBuilder(memo: string|Uint8Array, blockNumber: number, assetTreeRoot: AssetTreeRoot)- Create builderaddLeg(leg: LegBuilder)- Add a transfer legaddAssetPath(assetId: number, path: AssetLeafPath)- Add asset curve tree path (once per asset)build(): SettlementProof- Generate the settlement proof
Represents a single transfer leg in a settlement.
new LegBuilder(senderKeys: AccountKeys, receiverKeys: AccountPublicKeys, assetState: AssetState, amount: BigInt)- Create leg- Handles encryption of transfer details for mediators and auditors
The zero-knowledge proof for a settlement.
fromBytes(bytes: Uint8Array)- Import from bytestoBytes(): Uint8Array- Export as bytesfromHex(hex: string)- Import from hex stringtoHex(): string- Export as hex string
All proof types support serialization:
All proof types support:
fromBytes(bytes: Uint8Array)- Import from bytestoBytes(): Uint8Array- Export as bytesfromHex(hex: string)- Import from hextoHex(): string- Export as hex
Available proofs:
AccountRegistrationProof- Register account keys on-chainAccountAssetRegistrationProof- Register account for a specific assetAssetMintingProof- Mint tokens of a confidential assetSenderAffirmationProof- Sender affirmation for a settlement legReceiverAffirmationProof- Receiver affirmation for a settlement legReceiverClaimProof- Claim received tokensSenderCounterUpdateProof- Update sender's counter after transactionSenderReversalProof- Reverse a failed settlementMediatorAffirmationProof- Mediator affirmation for a settlementSettlementProof- Complete settlement with multiple legs
See the examples/ directory for complete working examples:
basic-keys.html- Key generation and managementnode-example.js- Node.js usage with PolymeshClient
For most production use cases, you'll want to use Polkadot.js for chain queries and transactions. Here's how to integrate:
import { ApiPromise, WsProvider } from '@polkadot/api';
import {
AccountKeys,
AssetState,
SettlementBuilder,
LegBuilder,
generateRandomSeed
} from '@polymesh/dart-wasm';
// Connect to chain using Polkadot.js
const provider = new WsProvider('ws://localhost:9944');
const api = await ApiPromise.create({ provider });
// Generate DART keys for confidential transactions
const dartKeys = new AccountKeys(generateRandomSeed());
const publicKeys = dartKeys.publicKeys();
// Query asset state from chain
const assetId = 1;
const assetDetails = await api.query.confidentialAssets.dartAssetDetails(assetId);
const assetState = new AssetState(
assetId,
assetDetails.mediators, // Automatically converted from chain format
assetDetails.auditors
);
// Generate proof for registration
const accountDid = '0x1234...'; // Your account DID
const proof = dartKeys.registerAccountAssetProof(assetId, accountDid);
// Submit transaction using Polkadot.js
const extrinsic = api.tx.confidentialAssets.registerAccountAsset(
proof.getProof().toBytes()
);
const hash = await extrinsic.signAndSend(keyring.getPair(signer));
console.log('Transaction hash:', hash);GPL-3.0