@leachain/cte-core
v1.0.4
Published
JavaScript wrapper for the CTE CORE WASM library providing encoding and decoding capabilities.
Maintainers
Readme
LEA Chain CTE-CORE-JS
This library provides a JavaScript interface to the LEA Chain CTE (Common Transaction Encoding) core, which is written in C and compiled to WebAssembly (WASM). It offers a robust and efficient way to encode and decode blockchain transactions in a standardized format.
The library is designed to be used in both Node.js and modern browser environments, providing separate builds for each.
Features
- High-Performance Encoding/Decoding: Leverages the speed of WASM for all core operations.
- Independent Instances: Each encoder and decoder instance runs in its own sandboxed WASM environment, preventing conflicts and ensuring data integrity.
- Fluent Encoder API: A clean, chainable API for building complex transactions step-by-step.
- Push-Based Decoder: A simple and efficient push-based decoding mechanism that processes an entire buffer at once.
- Type-Safe: Includes TypeScript definitions for all public APIs.
Installation
npm install @leachain/cte-coreUsage
CteEncoder
The CteEncoder class provides a fluent interface for building CTE-compliant transaction buffers.
import { CteEncoder } from '@leachain/cte-core';
async function buildTransaction() {
try {
// 1. Create an encoder instance with a specified buffer capacity.
const encoder = await CteEncoder.create(4096); // 4KB buffer
// 2. Add data to the encoder using the fluent API.
const publicKeyData = new Uint8Array(32).fill(0xAA);
const signatureData = new Uint8Array(64).fill(0xBB);
encoder
.addPublicKeyVector(1, 0, publicKeyData) // 1 key, size code 0
.addSignatureVector(1, 1, signatureData) // 1 signature, size code 1
.writeIxDataUint32(12345)
.writeIxDataBoolean(true);
// 3. Get the final encoded data.
const encodedBytes = encoder.getEncodedData();
console.log('Encoded Data:', encodedBytes);
// 4. Clean up the encoder instance.
encoder.destroy();
return encodedBytes;
} catch (err) {
console.error('Failed to encode:', err);
}
}CteDecoder
The CteDecoder class processes an encoded buffer and returns the decoded data items.
import { CteDecoder } from '@leachain/cte-core';
async function readTransaction(encodedBytes) {
try {
// 1. Create a decoder instance.
const decoder = await CteDecoder.create();
// 2. Run the decoder on the encoded buffer.
const decodedItems = decoder.run(encodedBytes);
// 3. Process the array of decoded items.
console.log(`Decoded ${decodedItems.length} items:`);
for (const item of decodedItems) {
// The 'type' corresponds to internal CTE type identifiers.
// The 'data' is a Uint8Array of the raw decoded bytes for that item.
console.log(` - Type: ${item.type}, Data:`, item.data);
}
// 4. Clean up the decoder instance.
decoder.destroy();
} catch (err) {
console.error('Failed to decode:', err);
}
}
// Example of running both:
buildTransaction().then(encoded => {
if (encoded) {
readTransaction(encoded);
}
});API Documentation
This project uses jsdoc to generate API documentation from the source code comments. To generate the documentation locally, run the following command:
npm run build:jsdocThis will create a docs directory in the project root containing the full HTML documentation.
Building the Project
To build the project from the source, including all Node.js and browser-specific bundles, run:
npm run buildRunning Tests
To run the test suite, use the following command:
npm run test