Skip to content

Commit 7234553

Browse files
committed
chore: export encoding functions
1 parent 941ae0c commit 7234553

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ import { TransferBody as _TransferBody } from './funder/funding_types';
3939
import { Config as _Config, KwilConfig as _KwilConfig } from './api_client/config';
4040
import { EthSigner as _EthSigner } from './core/signature';
4141
import { Kwil as _Kwil } from './client/kwil';
42+
import { formatEncodedValue as _formatEncodedValue } from './utils/parameterEncoding';
43+
import { encodeEncodedValue as _encodeEncodedValue } from './utils/kwilEncoding';
44+
import { EncodedValue as _EncodedValue } from './core/payload';
4245

4346
namespace Types {
4447
export type TxReceipt = _TxReceipt;
@@ -65,6 +68,7 @@ namespace Types {
6568
export type NamedParams = _NamedParams
6669
export type PositionalParams = _PositionalParams
6770
export type ValueType = _ValueType
71+
export type EncodedValue = _EncodedValue
6872

6973
// below are deprecated and can be removed on next release (kwil-js v0.10)
7074
export type Database = _Database;
@@ -98,6 +102,18 @@ namespace Utils {
98102
* `DataType` holds the different data types that can be asserted as action inputs.
99103
*/
100104
export import DataType = _DataType;
105+
106+
/**
107+
* Converts a JavaScript value into an EncodedValue structure.
108+
* Used for encoding arguments for Kwil actions.
109+
*/
110+
export const formatEncodedValue = _formatEncodedValue;
111+
112+
/**
113+
* Serializes an EncodedValue into bytes using kwil-db's MarshalBinary format.
114+
* Used for advanced argument encoding scenarios.
115+
*/
116+
export const encodeEncodedValue = _encodeEncodedValue;
101117
}
102118

103119
export { NodeKwil, WebKwil, KwilSigner, Types, Utils, Client, EnvironmentType };

src/utils/kwilEncoding.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,14 @@ export function encodeTransfer(transfer: TransferPayload): string {
154154
return bytesToBase64(concatBytes(encodedVersion, encodedTo, encodedAmount));
155155
}
156156

157-
function encodeEncodedValue(ev: EncodedValue): Uint8Array {
157+
/**
158+
* Encodes an EncodedValue into bytes using kwil-db's MarshalBinary format.
159+
* This is used internally for action encoding and exported for advanced use cases.
160+
*
161+
* @param ev - The EncodedValue to encode
162+
* @returns Serialized bytes matching kwil-db's EncodedValue.MarshalBinary() format
163+
*/
164+
export function encodeEncodedValue(ev: EncodedValue): Uint8Array {
158165
// To encode an `EncodedValue` we need to concat a bytes array with all of the necessary elements
159166
// The order is important.
160167

src/utils/parameterEncoding.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,15 @@ export function encodeRawStatementParameters(params: QueryParams) {
5151
});
5252
}
5353

54-
// Used by the executeSql() method and the encodeActionInputs() method
55-
function formatEncodedValue(val: ValueType | ValueType[], o?: DataInfo): EncodedValue {
54+
/**
55+
* Converts a JavaScript value into an EncodedValue structure.
56+
* This is the primary function for encoding values for use with Kwil actions.
57+
*
58+
* @param val - The value to encode (string, number, boolean, Uint8Array, null, or arrays of these)
59+
* @param o - Optional DataInfo to override type detection
60+
* @returns EncodedValue ready for serialization
61+
*/
62+
export function formatEncodedValue(val: ValueType | ValueType[], o?: DataInfo): EncodedValue {
5663
const base = formatDataType(val, o);
5764

5865
if (Array.isArray(val)) {

0 commit comments

Comments
 (0)