npm package: @leachain/cte-core
Description: JavaScript wrapper for the CTE CORE WASM library providing encoding and decoding capabilities.
Author: Allwin Ketnawang
Latest version: 1.0.4
Deprecated: Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

# 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

```bash
npm install @leachain/cte-core
```

## Usage

### CteEncoder

The `CteEncoder` class provides a fluent interface for building CTE-compliant transaction buffers.

```javascript
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.

```javascript
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:

```bash
npm run build:jsdoc
```

This 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:

```bash
npm run build
```

## Running Tests

To run the test suite, use the following command:

```bash
npm run test
```