Class: CteEncoder

CteEncoder()

Provides a fluent interface to encode data according to the CTE specification. Each instance manages its own independent WASM encoder module instance and memory. Must be instantiated asynchronously using the static `create` method.

Constructor

new CteEncoder()

Source:

Methods

addCommandData(data) → {this}

Adds a Command Data field. Requires input as `Uint8Array`.
Parameters:
Name Type Description
data Uint8Array The command payload bytes. Length must not exceed `CTE.CTE_COMMAND_EXTENDED_MAX_LEN`.
Source:
Throws:
If data is not a `Uint8Array` or exceeds max length, or WASM fails.
Type
Error
Returns:
The encoder instance for chaining.
Type
this

addIxDataBoolean() → {this}

Source:
Returns:
Type
this

addIxDataFloat32() → {this}

Source:
Returns:
Type
this

addIxDataFloat64() → {this}

Source:
Returns:
Type
this

addIxDataIndexReference() → {this}

Source:
Returns:
Type
this

addIxDataInt16() → {this}

Source:
Returns:
Type
this

addIxDataInt32() → {this}

Source:
Returns:
Type
this

addIxDataInt64() → {this}

Source:
Returns:
Type
this

addIxDataInt8() → {this}

Source:
Returns:
Type
this

addIxDataSleb128() → {this}

Source:
Returns:
Type
this

addIxDataUint16() → {this}

Source:
Returns:
Type
this

addIxDataUint32() → {this}

Source:
Returns:
Type
this

addIxDataUint64() → {this}

Source:
Returns:
Type
this

addIxDataUint8() → {this}

Source:
Returns:
Type
this

addIxDataUleb128() → {this}

Source:
Returns:
Type
this

addPublicKeyList() → {this}

Source:
Returns:
Type
this

addSignatureList() → {this}

Source:
Returns:
Type
this

destroy()

Cleans up Javascript references associated with this encoder instance. Does not explicitly free WASM memory. Future calls to this instance will fail.
Source:

getEncodedData() → {Uint8Array}

Retrieves the currently encoded data from this instance as a byte array. Returns a copy. Terminates an encoding chain.
Source:
Throws:
If the encoder instance handle is invalid or WASM memory access fails.
Type
Error
Returns:
A copy of the encoded data bytes.
Type
Uint8Array

getPublicKeySize(typeCode) → {number}

Gets the expected size in bytes for a public key item based on crypto type code, using this instance's WASM module.
Parameters:
Name Type Description
typeCode number The crypto type code (e.g., `CTE.CTE_CRYPTO_TYPE_ED25519`).
Source:
Throws:
If the encoder instance handle is invalid or the WASM function returns an invalid size.
Type
Error
Returns:
The size in bytes.
Type
number

getSignatureItemSize(typeCode) → {number}

Gets the expected size in bytes for a signature/hash item based on crypto type code, using this instance's WASM module.
Parameters:
Name Type Description
typeCode number The crypto type code (e.g., `CTE.CTE_CRYPTO_TYPE_ED25519`).
Source:
Throws:
If the encoder instance handle is invalid or the WASM function returns an invalid size.
Type
Error
Returns:
The size in bytes.
Type
number

reset() → {this}

Resets the encoder state for this instance, allowing reuse of its allocated buffer.
Source:
Throws:
If the encoder instance handle is invalid (e.g., after destroy).
Type
Error
Returns:
The encoder instance for chaining.
Type
this

(async, static) create(capacity) → {Promise.<CteEncoder>}

Asynchronously creates and initializes a new, independent CTE encoder instance. Loads and instantiates a fresh copy of the encoder WASM module for this instance.
Parameters:
Name Type Description
capacity number The fixed buffer capacity in bytes for the encoder. Should be large enough for the expected transaction.
Source:
Throws:
If WASM binary is invalid, instantiation fails, exports are missing, or C-level encoder initialization (`cte_encoder_init`) fails.
Type
Error
Returns:
A promise that resolves to the initialized CteEncoder instance.
Type
Promise.<CteEncoder>
Example
import { CteEncoder } from '@leachain/ctejs-core'; // Use package name

async function main() {
try {
// Each call creates a new WASM instance
const encoder1 = await CteEncoder.create(1024);
const encoder2 = await CteEncoder.create(2048);
console.log('Encoders ready!');
} catch (err) {
console.error("Failed to create encoder:", err);
}
}
main();