(feat): add codec helpers to ts-sdk
All checks were successful
CI / checks (push) Successful in 8m38s

This commit is contained in:
Alois 2026-07-03 14:45:55 +02:00
commit 8dc8b54b26
3 changed files with 51 additions and 0 deletions

View file

@ -72,6 +72,15 @@ console.log("Connected MTP client", clientId, client.state);
Use `MTPClient.crypto` for SDK-level crypto helpers such as `generateKeyring()`, `generateEd25519()`, `keyringFromEd25519()`, `verifyEd25519()`, `sha256()`, `sha256Double()`, `hkdfExpand()`, and `deriveEncryptionKey()`. Use `MTPClient.crypto` for SDK-level crypto helpers such as `generateKeyring()`, `generateEd25519()`, `keyringFromEd25519()`, `verifyEd25519()`, `sha256()`, `sha256Double()`, `hkdfExpand()`, and `deriveEncryptionKey()`.
Use `codec` to encode and decode MTP frames from the main SDK export:
```typescript
import { codec } from "mtp";
const frame = codec.encode("SomeType", { value: "hello" });
const parsed = codec.decode(frame);
```
## Getting Started ## Getting Started
Add the `mtp` crate with your desired features: Add the `mtp` crate with your desired features:

View file

@ -258,6 +258,16 @@ Raw message helpers that remain available include:
- `format_frame(frame)` - `format_frame(frame)`
- `parse_auth_response(frame)` - `parse_auth_response(frame)`
The SDK export also exposes the same frame codec through `codec`:
```typescript
import { codec } from "mtp";
const frame = codec.encode("SomeType", { value: "hello" });
const parsed = codec.decode(frame);
const display = codec.format(frame);
```
Raw crypto and key helpers include: Raw crypto and key helpers include:
- `ed25519_generate()` - `ed25519_generate()`

View file

@ -75,6 +75,36 @@ export interface MTPRaw {
export type MTPBytesInput = Uint8Array | number[]; export type MTPBytesInput = Uint8Array | number[];
export interface MTPCodecOptions {
id?: number;
sender?: bigint | number;
receiver?: bigint | number;
}
export interface MTPCodec {
encode(type: MTPCommunicationType, data: Record<string, unknown>, options?: MTPCodecOptions): Uint8Array;
decode(frame: MTPBytesInput): ParsedFrame;
format(frame: MTPBytesInput): string;
}
export function encode(type: MTPCommunicationType, data: Record<string, unknown>, options?: MTPCodecOptions): Uint8Array {
return bindings.build_frame(type, data, options ?? {});
}
export function decode(frame: MTPBytesInput): ParsedFrame {
return bindings.parse_frame(bytesFrom(frame, "frame"));
}
export function format(frame: MTPBytesInput): string {
return bindings.format_frame(bytesFrom(frame, "frame"));
}
export const codec: MTPCodec = {
encode,
decode,
format,
};
export interface MTPCredentials { export interface MTPCredentials {
clientId: bigint | string | number | null; clientId: bigint | string | number | null;
keyring: MTPBytesInput; keyring: MTPBytesInput;
@ -335,12 +365,14 @@ async function withTimeout(promise, timeoutMs, message) {
export class MTPClient { export class MTPClient {
static readonly crypto = crypto; static readonly crypto = crypto;
static readonly codec = codec;
#credentials: InternalCredentials | null; #credentials: InternalCredentials | null;
#options: NormalizedMTPClientOptions; #options: NormalizedMTPClientOptions;
readonly raw: MTPRaw; readonly raw: MTPRaw;
readonly crypto = MTPClient.crypto; readonly crypto = MTPClient.crypto;
readonly codec = MTPClient.codec;
private constructor(options: NormalizedMTPClientOptions, client: RawBindings.WasmClient) { private constructor(options: NormalizedMTPClientOptions, client: RawBindings.WasmClient) {
this.#options = options; this.#options = options;