@terminal3/t3n-sdk 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.OIDC.md +216 -0
- package/README.md +639 -0
- package/dist/demo.d.ts +25 -0
- package/dist/index.d.ts +819 -0
- package/dist/index.esm.js +2 -0
- package/dist/index.js +2 -0
- package/dist/src/client/actions.d.ts +17 -0
- package/dist/src/client/config.d.ts +35 -0
- package/dist/src/client/encryption.d.ts +30 -0
- package/dist/src/client/handlers.d.ts +45 -0
- package/dist/src/client/index.d.ts +10 -0
- package/dist/src/client/request-parser.d.ts +48 -0
- package/dist/src/client/t3n-client.d.ts +70 -0
- package/dist/src/client/transport.d.ts +107 -0
- package/dist/src/config/index.d.ts +67 -0
- package/dist/src/config/loader.d.ts +11 -0
- package/dist/src/config/types.d.ts +25 -0
- package/dist/src/index.d.ts +23 -0
- package/dist/src/types/auth.d.ts +54 -0
- package/dist/src/types/index.d.ts +35 -0
- package/dist/src/types/session.d.ts +24 -0
- package/dist/src/utils/contract-version.d.ts +5 -0
- package/dist/src/utils/crypto.d.ts +52 -0
- package/dist/src/utils/errors.d.ts +61 -0
- package/dist/src/utils/index.d.ts +9 -0
- package/dist/src/utils/logger.d.ts +102 -0
- package/dist/src/utils/redaction.d.ts +13 -0
- package/dist/src/utils/session.d.ts +37 -0
- package/dist/src/wasm/index.d.ts +5 -0
- package/dist/src/wasm/interface.d.ts +105 -0
- package/dist/src/wasm/loader.d.ts +43 -0
- package/dist/wasm/generated/interfaces/component-session-client-auth.d.ts +12 -0
- package/dist/wasm/generated/interfaces/component-session-client-handshake.d.ts +12 -0
- package/dist/wasm/generated/interfaces/component-session-server-auth.d.ts +12 -0
- package/dist/wasm/generated/interfaces/component-session-server-handshake.d.ts +12 -0
- package/dist/wasm/generated/interfaces/component-session-session.d.ts +8 -0
- package/dist/wasm/generated/interfaces/wasi-cli-environment.d.ts +2 -0
- package/dist/wasm/generated/interfaces/wasi-cli-exit.d.ts +3 -0
- package/dist/wasm/generated/interfaces/wasi-cli-stderr.d.ts +3 -0
- package/dist/wasm/generated/interfaces/wasi-cli-stdin.d.ts +3 -0
- package/dist/wasm/generated/interfaces/wasi-cli-stdout.d.ts +3 -0
- package/dist/wasm/generated/interfaces/wasi-clocks-wall-clock.d.ts +5 -0
- package/dist/wasm/generated/interfaces/wasi-filesystem-preopens.d.ts +3 -0
- package/dist/wasm/generated/interfaces/wasi-filesystem-types.d.ts +124 -0
- package/dist/wasm/generated/interfaces/wasi-io-error.d.ts +8 -0
- package/dist/wasm/generated/interfaces/wasi-io-streams.d.ts +28 -0
- package/dist/wasm/generated/session.core.wasm +0 -0
- package/dist/wasm/generated/session.core2.wasm +0 -0
- package/dist/wasm/generated/session.d.ts +16 -0
- package/dist/wasm/generated/session.js +3437 -0
- package/package.json +104 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WASM Action Creators
|
|
3
|
+
*
|
|
4
|
+
* Creates the initial action payloads for WASM state machines.
|
|
5
|
+
* These are JSON-serialized and passed to the WASM component to start flows.
|
|
6
|
+
*/
|
|
7
|
+
import { AuthInput } from "../types";
|
|
8
|
+
/**
|
|
9
|
+
* Create the initial handshake request
|
|
10
|
+
* This kicks off the handshake state machine in WASM
|
|
11
|
+
*/
|
|
12
|
+
export declare function createHandshakeAction(): Uint8Array;
|
|
13
|
+
/**
|
|
14
|
+
* Create the initial authentication request based on auth method
|
|
15
|
+
* @param authInput - The authentication input (Ethereum or OIDC)
|
|
16
|
+
*/
|
|
17
|
+
export declare function createAuthAction(authInput: AuthInput): Uint8Array;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration types for T3n Client
|
|
3
|
+
*/
|
|
4
|
+
import { WasmComponent } from "../wasm";
|
|
5
|
+
import { SessionId, GuestToHostHandlers } from "../types";
|
|
6
|
+
import { Logger, LogLevel } from "../utils/logger";
|
|
7
|
+
import { Transport } from "./transport";
|
|
8
|
+
/**
|
|
9
|
+
* Configuration interface for T3n Client
|
|
10
|
+
*/
|
|
11
|
+
export interface T3nClientConfig {
|
|
12
|
+
/** Base URL of the T3n node (used if transport not provided) */
|
|
13
|
+
baseUrl?: string;
|
|
14
|
+
/** WASM component instance for cryptographic operations */
|
|
15
|
+
wasmComponent: WasmComponent;
|
|
16
|
+
/** Optional transport layer - if not provided, uses HttpTransport with baseUrl */
|
|
17
|
+
transport?: Transport;
|
|
18
|
+
/** Optional session ID - will be generated if not provided */
|
|
19
|
+
sessionId?: SessionId;
|
|
20
|
+
/** Optional request timeout in milliseconds (default: 30000) - used for HttpTransport */
|
|
21
|
+
timeout?: number;
|
|
22
|
+
/** Optional custom headers to include in requests */
|
|
23
|
+
headers?: Record<string, string>;
|
|
24
|
+
/**
|
|
25
|
+
* Log level for this client instance.
|
|
26
|
+
* Defaults to global log level (LogLevel.ERROR) if not specified.
|
|
27
|
+
* Use LogLevel.DEBUG for verbose logging, LogLevel.INFO for informational messages,
|
|
28
|
+
* LogLevel.WARN for warnings, or LogLevel.ERROR for errors only.
|
|
29
|
+
*/
|
|
30
|
+
logLevel?: LogLevel;
|
|
31
|
+
/** Optional custom logger - if provided, overrides logLevel */
|
|
32
|
+
logger?: Logger;
|
|
33
|
+
/** Optional guest-to-host request handlers - provides custom behavior for WASM requests */
|
|
34
|
+
handlers?: GuestToHostHandlers;
|
|
35
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Encryption Service
|
|
3
|
+
*
|
|
4
|
+
* Handles encryption and decryption of data using the established WASM session.
|
|
5
|
+
* Keeps cryptographic operations isolated and simple.
|
|
6
|
+
*/
|
|
7
|
+
import { SessionCrypto } from "../wasm";
|
|
8
|
+
import { Logger } from "../utils/logger";
|
|
9
|
+
/**
|
|
10
|
+
* Encrypts and decrypts data using an established session
|
|
11
|
+
*/
|
|
12
|
+
export declare class SessionEncryption {
|
|
13
|
+
private sessionCrypto;
|
|
14
|
+
private logger;
|
|
15
|
+
constructor(sessionCrypto: SessionCrypto, logger: Logger);
|
|
16
|
+
/**
|
|
17
|
+
* Encrypt data using the session
|
|
18
|
+
* @param sessionState - The session state bytes (from handshake)
|
|
19
|
+
* @param data - The plaintext data to encrypt
|
|
20
|
+
* @returns Base64-encoded encrypted data
|
|
21
|
+
*/
|
|
22
|
+
encrypt(sessionState: Uint8Array, data: Uint8Array): Promise<string>;
|
|
23
|
+
/**
|
|
24
|
+
* Decrypt data using the session
|
|
25
|
+
* @param sessionState - The session state bytes (from handshake)
|
|
26
|
+
* @param encryptedData - Base64-encoded encrypted data
|
|
27
|
+
* @returns Decrypted plaintext bytes
|
|
28
|
+
*/
|
|
29
|
+
decrypt(sessionState: Uint8Array, encryptedData: string): Promise<Uint8Array>;
|
|
30
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Guest-to-Host Request Handlers
|
|
3
|
+
*
|
|
4
|
+
* These handle requests from WASM that need the host environment to perform side effects.
|
|
5
|
+
* Examples: signing challenges, providing public keys, generating random bytes.
|
|
6
|
+
*/
|
|
7
|
+
import { GuestToHostHandler, GuestToHostHandlers } from "../types";
|
|
8
|
+
import { Logger } from "../utils/logger";
|
|
9
|
+
/**
|
|
10
|
+
* Create an EthSign handler using MetaMask (window.ethereum)
|
|
11
|
+
* @param account - MetaMask account (string address or object with address property)
|
|
12
|
+
* @param logger - Optional logger instance. Defaults to a logger using the global log level (LogLevel.ERROR).
|
|
13
|
+
* Pass a custom logger to override logging behavior for this handler.
|
|
14
|
+
* @param privateKey - Optional private key for signing (if provided, MetaMask is not used)
|
|
15
|
+
*/
|
|
16
|
+
export declare function metamask_sign(account: any, logger?: Logger, privateKey?: string | undefined): GuestToHostHandler;
|
|
17
|
+
/**
|
|
18
|
+
* Get the current MetaMask address
|
|
19
|
+
* @returns Ethereum address (lowercase, 0x prefixed)
|
|
20
|
+
*/
|
|
21
|
+
export declare function metamask_get_address(): Promise<string>;
|
|
22
|
+
/**
|
|
23
|
+
* Get the address for a given private key
|
|
24
|
+
* @param privateKey - Ethereum private key (0x prefixed hex string)
|
|
25
|
+
* @returns Ethereum address (lowercase, 0x prefixed)
|
|
26
|
+
*/
|
|
27
|
+
export declare function eth_get_address(privateKey: string): string;
|
|
28
|
+
/**
|
|
29
|
+
* Create MlKemPublicKey handler that returns the environment-specific root public key
|
|
30
|
+
* Note: The Rust PkWithRho type serializes as an array of bytes, not a base64 string
|
|
31
|
+
*/
|
|
32
|
+
export declare function createMlKemPublicKeyHandler(): GuestToHostHandler;
|
|
33
|
+
/**
|
|
34
|
+
* Create Random handler backed by crypto.getRandomValues
|
|
35
|
+
* Note: The Rust Vec<u8> type serializes as an array of bytes, not a base64 string
|
|
36
|
+
*/
|
|
37
|
+
export declare function createRandomHandler(): GuestToHostHandler;
|
|
38
|
+
/**
|
|
39
|
+
* Create the default handler set required by the T3n handshake
|
|
40
|
+
*/
|
|
41
|
+
export declare function createDefaultHandlers(): GuestToHostHandlers;
|
|
42
|
+
/**
|
|
43
|
+
* Merge consumer-provided handlers with defaults (user handlers take precedence)
|
|
44
|
+
*/
|
|
45
|
+
export declare function mergeWithDefaultHandlers(handlers?: GuestToHostHandlers): GuestToHostHandlers;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WASM Request Parser
|
|
3
|
+
*
|
|
4
|
+
* Parses and categorizes requests from the WASM state machine.
|
|
5
|
+
* The WASM component outputs JSON with a `guest_to_host` tag that determines
|
|
6
|
+
* how the SDK should handle the request.
|
|
7
|
+
*
|
|
8
|
+
* See node/session/src/abi.rs for the GuestToHost enum definition.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Types of requests that can come from WASM
|
|
12
|
+
*/
|
|
13
|
+
export declare enum WasmRequestType {
|
|
14
|
+
/** Send data to remote server (PeerReply with action) */
|
|
15
|
+
SendRemote = "SendRemote",
|
|
16
|
+
/** Request to host (SDK) for side effects (MlKemPublicKey, Random, EthSign, etc.) */
|
|
17
|
+
GuestToHost = "GuestToHost",
|
|
18
|
+
/** Flow complete (Suspend) */
|
|
19
|
+
Suspend = "Suspend"
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Parsed result from WASM request
|
|
23
|
+
*/
|
|
24
|
+
export interface ParsedRequest {
|
|
25
|
+
type: WasmRequestType;
|
|
26
|
+
data: Record<string, unknown>;
|
|
27
|
+
raw: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Parses WASM request bytes into a categorized request type
|
|
31
|
+
*/
|
|
32
|
+
export declare function parseWasmRequest(requestBytes: Uint8Array): ParsedRequest;
|
|
33
|
+
/**
|
|
34
|
+
* Check if a request should be sent to the remote server
|
|
35
|
+
*/
|
|
36
|
+
export declare function isSendRemote(parsed: ParsedRequest): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Check if a request indicates flow completion
|
|
39
|
+
*/
|
|
40
|
+
export declare function isCompletion(parsed: ParsedRequest): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Check if a request needs a guest-to-host handler
|
|
43
|
+
*/
|
|
44
|
+
export declare function isGuestToHost(parsed: ParsedRequest): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Get the guest-to-host request type name (e.g., "MlKemPublicKey", "Random", "EthSign")
|
|
47
|
+
*/
|
|
48
|
+
export declare function getGuestToHostType(parsed: ParsedRequest): string | null;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* T3n Client - Main SDK class
|
|
3
|
+
*
|
|
4
|
+
* Provides a simple interface for establishing secure sessions with T3n nodes.
|
|
5
|
+
* All cryptographic complexity is handled in WASM components.
|
|
6
|
+
*/
|
|
7
|
+
import { T3nClientConfig } from "./config";
|
|
8
|
+
import { SessionId, Did, SessionStatus, AuthInput, HandshakeResult } from "../types";
|
|
9
|
+
/**
|
|
10
|
+
* Main T3n SDK Client
|
|
11
|
+
*/
|
|
12
|
+
export declare class T3nClient {
|
|
13
|
+
private readonly config;
|
|
14
|
+
private readonly transport;
|
|
15
|
+
private readonly sessionId;
|
|
16
|
+
private readonly logger;
|
|
17
|
+
private readonly encryption;
|
|
18
|
+
private status;
|
|
19
|
+
private wasmState;
|
|
20
|
+
private did;
|
|
21
|
+
private handshakeResult;
|
|
22
|
+
constructor(config: T3nClientConfig);
|
|
23
|
+
/**
|
|
24
|
+
* Start the handshake process with the T3n node
|
|
25
|
+
*/
|
|
26
|
+
handshake(): Promise<HandshakeResult>;
|
|
27
|
+
/**
|
|
28
|
+
* Authenticate with the T3n node
|
|
29
|
+
*/
|
|
30
|
+
authenticate(authInput: AuthInput): Promise<Did>;
|
|
31
|
+
/**
|
|
32
|
+
* Execute an action on the T3n node
|
|
33
|
+
*/
|
|
34
|
+
execute(payload: unknown): Promise<string>;
|
|
35
|
+
getSessionId(): SessionId;
|
|
36
|
+
getStatus(): SessionStatus;
|
|
37
|
+
getDid(): Did | null;
|
|
38
|
+
getLastSetCookie(): string | null;
|
|
39
|
+
getLastResponseHeaders(): Record<string, string>;
|
|
40
|
+
isAuthenticated(): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Run a WASM state machine flow to completion
|
|
43
|
+
*/
|
|
44
|
+
private runFlow;
|
|
45
|
+
/**
|
|
46
|
+
* Try to finalize the current flow
|
|
47
|
+
*/
|
|
48
|
+
private tryFinalize;
|
|
49
|
+
/**
|
|
50
|
+
* Handle a WASM request based on its type
|
|
51
|
+
*/
|
|
52
|
+
private handleWasmRequest;
|
|
53
|
+
/**
|
|
54
|
+
* Handle a send-remote request by calling the RPC endpoint
|
|
55
|
+
*/
|
|
56
|
+
private handleSendRemote;
|
|
57
|
+
private captureHandshakeResult;
|
|
58
|
+
/**
|
|
59
|
+
* Handle a guest-to-host request using configured handlers
|
|
60
|
+
*/
|
|
61
|
+
private handleGuestToHost;
|
|
62
|
+
/**
|
|
63
|
+
* Send an RPC request with automatic encryption/decryption
|
|
64
|
+
*/
|
|
65
|
+
private sendRpcRequest;
|
|
66
|
+
/**
|
|
67
|
+
* Get the current session state for encryption
|
|
68
|
+
*/
|
|
69
|
+
private getSessionState;
|
|
70
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport layer for T3n SDK
|
|
3
|
+
*
|
|
4
|
+
* Abstracts the communication with T3n nodes, allowing for different
|
|
5
|
+
* implementations (HTTP, WebSocket, Mock) and easy testing.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* JSON-RPC request structure
|
|
9
|
+
*/
|
|
10
|
+
export interface JsonRpcRequest {
|
|
11
|
+
jsonrpc: "2.0";
|
|
12
|
+
method: string;
|
|
13
|
+
params: any;
|
|
14
|
+
id: string | number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* JSON-RPC response structure
|
|
18
|
+
*/
|
|
19
|
+
export interface JsonRpcResponse {
|
|
20
|
+
jsonrpc: "2.0";
|
|
21
|
+
result?: any;
|
|
22
|
+
error?: {
|
|
23
|
+
code: number;
|
|
24
|
+
message: string;
|
|
25
|
+
data?: any;
|
|
26
|
+
};
|
|
27
|
+
id: string | number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Transport interface for sending requests to T3n nodes
|
|
31
|
+
*/
|
|
32
|
+
export interface Transport {
|
|
33
|
+
/**
|
|
34
|
+
* Send a JSON-RPC request to the T3n node
|
|
35
|
+
* @param request - The JSON-RPC request to send
|
|
36
|
+
* @param headers - Additional headers to include
|
|
37
|
+
* @returns Promise that resolves to the JSON-RPC response
|
|
38
|
+
*/
|
|
39
|
+
send(request: JsonRpcRequest, headers: Record<string, string>): Promise<JsonRpcResponse>;
|
|
40
|
+
/**
|
|
41
|
+
* Optional accessor for the latest Set-Cookie header value.
|
|
42
|
+
* (Useful in Node.js demos/tests; browsers block HttpOnly cookies.)
|
|
43
|
+
*/
|
|
44
|
+
getLastSetCookie?(): string | null;
|
|
45
|
+
/**
|
|
46
|
+
* Optional accessor for the last response headers (debugging).
|
|
47
|
+
*/
|
|
48
|
+
getLastResponseHeaders?(): Record<string, string>;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* HTTP transport implementation using fetch
|
|
52
|
+
*/
|
|
53
|
+
export declare class HttpTransport implements Transport {
|
|
54
|
+
private baseUrl;
|
|
55
|
+
private timeout;
|
|
56
|
+
private lastSetCookie;
|
|
57
|
+
private lastResponseHeaders;
|
|
58
|
+
constructor(baseUrl: string, timeout?: number);
|
|
59
|
+
getLastSetCookie(): string | null;
|
|
60
|
+
getLastResponseHeaders(): Record<string, string>;
|
|
61
|
+
send(request: JsonRpcRequest, headers: Record<string, string>): Promise<JsonRpcResponse>;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Mock transport for testing
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* const mockTransport = new MockTransport();
|
|
69
|
+
* mockTransport.mockResponse("auth.handshake", { result: "..." });
|
|
70
|
+
*
|
|
71
|
+
* const client = new T3nClient({
|
|
72
|
+
* transport: mockTransport,
|
|
73
|
+
* wasmComponent: mockWasm,
|
|
74
|
+
* });
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export declare class MockTransport implements Transport {
|
|
78
|
+
private responses;
|
|
79
|
+
private requests;
|
|
80
|
+
/**
|
|
81
|
+
* Mock a response for a specific method
|
|
82
|
+
*/
|
|
83
|
+
mockResponse(method: string, response: Partial<JsonRpcResponse>): void;
|
|
84
|
+
/**
|
|
85
|
+
* Mock an error response for a specific method
|
|
86
|
+
*/
|
|
87
|
+
mockError(method: string, code: number, message: string, data?: any): void;
|
|
88
|
+
/**
|
|
89
|
+
* Get all requests that were sent
|
|
90
|
+
*/
|
|
91
|
+
getRequests(): Array<{
|
|
92
|
+
request: JsonRpcRequest;
|
|
93
|
+
headers: Record<string, string>;
|
|
94
|
+
}>;
|
|
95
|
+
/**
|
|
96
|
+
* Get requests for a specific method
|
|
97
|
+
*/
|
|
98
|
+
getRequestsForMethod(method: string): Array<{
|
|
99
|
+
request: JsonRpcRequest;
|
|
100
|
+
headers: Record<string, string>;
|
|
101
|
+
}>;
|
|
102
|
+
/**
|
|
103
|
+
* Clear all recorded requests
|
|
104
|
+
*/
|
|
105
|
+
clearRequests(): void;
|
|
106
|
+
send(request: JsonRpcRequest, headers: Record<string, string>): Promise<JsonRpcResponse>;
|
|
107
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration entry point for T3n SDK
|
|
3
|
+
*
|
|
4
|
+
* This module imports all environment keys as base64 strings. All keys are included
|
|
5
|
+
* in the bundle, and the active key is selected at runtime via setEnvironment().
|
|
6
|
+
*
|
|
7
|
+
* Runtime key selection:
|
|
8
|
+
* - All keys (local, staging, production, test) are included in the bundle
|
|
9
|
+
* - Default environment is "production"
|
|
10
|
+
* - Use setEnvironment(env) to change the active key at runtime
|
|
11
|
+
*
|
|
12
|
+
* Binary files are converted to base64 strings at build time by the rollup binary plugin.
|
|
13
|
+
* In development mode (when running with tsx), files are read from the filesystem.
|
|
14
|
+
*/
|
|
15
|
+
import type { SdkConfig, Environment } from "./types";
|
|
16
|
+
/**
|
|
17
|
+
* Set the active environment for key selection
|
|
18
|
+
*
|
|
19
|
+
* This function allows engineers to configure which ML-KEM public key
|
|
20
|
+
* should be used at runtime. All keys are included in the bundle, so
|
|
21
|
+
* switching environments doesn't require rebuilding.
|
|
22
|
+
*
|
|
23
|
+
* @param env - The environment identifier (local, staging, production, or test)
|
|
24
|
+
* @throws Error if the environment is invalid
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* import { setEnvironment } from '@terminal3/t3n-sdk';
|
|
29
|
+
*
|
|
30
|
+
* // Use staging environment
|
|
31
|
+
* setEnvironment('staging');
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function setEnvironment(env: Environment): void;
|
|
35
|
+
/**
|
|
36
|
+
* Get the current environment identifier
|
|
37
|
+
*
|
|
38
|
+
* Returns the currently active environment, which defaults to "production".
|
|
39
|
+
* Use setEnvironment() to change the active environment.
|
|
40
|
+
*
|
|
41
|
+
* @returns The current environment identifier
|
|
42
|
+
*/
|
|
43
|
+
export declare function getEnvironment(): Environment;
|
|
44
|
+
/**
|
|
45
|
+
* Load configuration for the current environment
|
|
46
|
+
*
|
|
47
|
+
* Returns the SDK configuration with the ML-KEM public key for the
|
|
48
|
+
* currently active environment (set via setEnvironment()).
|
|
49
|
+
*
|
|
50
|
+
* @returns A minimal SdkConfig object with the ML-KEM public key
|
|
51
|
+
*/
|
|
52
|
+
export declare function loadConfig(): SdkConfig;
|
|
53
|
+
/**
|
|
54
|
+
* Get the ML-KEM public key from the current configuration
|
|
55
|
+
* This is the main entry point used by handlers
|
|
56
|
+
*
|
|
57
|
+
* @returns The base64-encoded ML-KEM public key for the current environment
|
|
58
|
+
*/
|
|
59
|
+
export declare function getMlKemPublicKey(): string;
|
|
60
|
+
/**
|
|
61
|
+
* Get the current environment identifier
|
|
62
|
+
*
|
|
63
|
+
* @returns The current environment name
|
|
64
|
+
*/
|
|
65
|
+
export declare function getEnvironmentName(): string;
|
|
66
|
+
export type { SdkConfig, Environment, ConfigValidationResult } from "./types";
|
|
67
|
+
export { validateConfig } from "./loader";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration loader for T3n SDK
|
|
3
|
+
*
|
|
4
|
+
* This module provides configuration validation utilities.
|
|
5
|
+
* All environment keys are included in the bundle, and runtime selection is done via setEnvironment().
|
|
6
|
+
*/
|
|
7
|
+
import type { ConfigValidationResult } from "./types";
|
|
8
|
+
/**
|
|
9
|
+
* Validate SDK configuration
|
|
10
|
+
*/
|
|
11
|
+
export declare function validateConfig(config: unknown): ConfigValidationResult;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration types for T3n SDK
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Environment type for SDK configuration
|
|
6
|
+
*/
|
|
7
|
+
export type Environment = "local" | "staging" | "production" | "test";
|
|
8
|
+
/**
|
|
9
|
+
* SDK configuration structure
|
|
10
|
+
*/
|
|
11
|
+
export interface SdkConfig {
|
|
12
|
+
/** Environment identifier */
|
|
13
|
+
environment: Environment;
|
|
14
|
+
/** Base64-encoded ML-KEM root public key */
|
|
15
|
+
mlKemPublicKey: string;
|
|
16
|
+
/** Configuration version */
|
|
17
|
+
version: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Configuration validation result
|
|
21
|
+
*/
|
|
22
|
+
export interface ConfigValidationResult {
|
|
23
|
+
valid: boolean;
|
|
24
|
+
errors: string[];
|
|
25
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* T3n TypeScript SDK
|
|
3
|
+
*
|
|
4
|
+
* A minimal TypeScript SDK that mirrors the server's RPC handler approach,
|
|
5
|
+
* keeping all state machine logic hidden in WASM and providing a clean,
|
|
6
|
+
* agnostic wrapper that doesn't expose authentication methods or internal states.
|
|
7
|
+
*/
|
|
8
|
+
export { T3nClient } from "./client";
|
|
9
|
+
export type { T3nClientConfig } from "./client";
|
|
10
|
+
export type { HandshakeResult } from "./types";
|
|
11
|
+
export type { Logger } from "./utils/logger";
|
|
12
|
+
export { LogLevel, createLogger, getLogger, setGlobalLogLevel, getGlobalLogLevel, } from "./utils/logger";
|
|
13
|
+
export type { Transport, JsonRpcRequest, JsonRpcResponse } from "./client";
|
|
14
|
+
export { HttpTransport, MockTransport } from "./client";
|
|
15
|
+
export type { SessionId, Did, OidcCredentials, AuthInput, EthAuthInput, OidcAuthInput, GuestToHostHandler, GuestToHostHandlers, } from "./types";
|
|
16
|
+
export { SessionStatus, AuthMethod, createEthAuthInput, createOidcAuthInput, } from "./types";
|
|
17
|
+
export { metamask_sign, metamask_get_address, eth_get_address, createDefaultHandlers, createMlKemPublicKeyHandler, createRandomHandler, } from "./client/handlers";
|
|
18
|
+
export type { WasmComponent, ClientHandshake, ClientAuth, SessionCrypto, WasmNextResult, } from "./wasm";
|
|
19
|
+
export { loadWasmComponent } from "./wasm";
|
|
20
|
+
export { generateRandomString, generateUUID, getScriptVersion, stringToBytes, bytesToString, redactSecrets, redactSecretsFromJson, } from "./utils";
|
|
21
|
+
export { T3nError, SessionStateError, AuthenticationError, HandshakeError, RpcError, WasmError, decodeWasmErrorMessage, extractWasmError, } from "./utils/errors";
|
|
22
|
+
export type { SdkConfig, Environment, ConfigValidationResult } from "./config";
|
|
23
|
+
export { loadConfig, getMlKemPublicKey, getEnvironmentName, getEnvironment, setEnvironment, validateConfig, } from "./config";
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authentication-related types for T3n SDK
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Authentication method enum
|
|
6
|
+
*/
|
|
7
|
+
export declare enum AuthMethod {
|
|
8
|
+
Ethereum = "eth",
|
|
9
|
+
OIDC = "oidc"
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Ethereum signer interface - only what user provides to start auth
|
|
13
|
+
*/
|
|
14
|
+
export interface EthereumSigner {
|
|
15
|
+
getPublicKey(): string;
|
|
16
|
+
signMessage(message: Uint8Array): Promise<Uint8Array>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* OIDC credentials interface
|
|
20
|
+
*/
|
|
21
|
+
export interface OidcCredentials {
|
|
22
|
+
provider: string;
|
|
23
|
+
idToken: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Base authentication input with method discriminator
|
|
27
|
+
*/
|
|
28
|
+
interface BaseAuthInput {
|
|
29
|
+
method: AuthMethod;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Ethereum authentication input
|
|
33
|
+
*/
|
|
34
|
+
export interface EthAuthInput extends BaseAuthInput {
|
|
35
|
+
method: AuthMethod.Ethereum;
|
|
36
|
+
address: string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* OIDC authentication input
|
|
40
|
+
*/
|
|
41
|
+
export interface OidcAuthInput extends BaseAuthInput {
|
|
42
|
+
method: AuthMethod.OIDC;
|
|
43
|
+
credentials: OidcCredentials;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Union type for all supported authentication inputs
|
|
47
|
+
*/
|
|
48
|
+
export type AuthInput = EthAuthInput | OidcAuthInput;
|
|
49
|
+
/**
|
|
50
|
+
* Helper functions to create auth inputs
|
|
51
|
+
*/
|
|
52
|
+
export declare function createEthAuthInput(address: string): EthAuthInput;
|
|
53
|
+
export declare function createOidcAuthInput(credentials: OidcCredentials): OidcAuthInput;
|
|
54
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public types export for T3n SDK
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Guest-to-Host request handler function type
|
|
6
|
+
* Handles requests from WASM guest that need host (SDK) to perform side effects
|
|
7
|
+
*/
|
|
8
|
+
export type GuestToHostHandler = (requestData: any) => Promise<Uint8Array>;
|
|
9
|
+
/**
|
|
10
|
+
* Map of guest-to-host request handlers
|
|
11
|
+
* Keys match the guest_to_host tag values from the WASM
|
|
12
|
+
*/
|
|
13
|
+
export interface GuestToHostHandlers {
|
|
14
|
+
/**
|
|
15
|
+
* Handle Ethereum signature requests
|
|
16
|
+
* requestData: { guest_to_host: "EthSign", challenge: string (base64) }
|
|
17
|
+
* Returns: JSON bytes of { host_to_guest: "EthSign", challenge: string, signature: string }
|
|
18
|
+
*/
|
|
19
|
+
EthSign?: GuestToHostHandler;
|
|
20
|
+
/**
|
|
21
|
+
* Handle MlKem public key requests
|
|
22
|
+
* requestData: { guest_to_host: "MlKemPublicKey" }
|
|
23
|
+
* Returns: JSON bytes of { host_to_guest: "MlKemPublicKey", key: string }
|
|
24
|
+
*/
|
|
25
|
+
MlKemPublicKey?: GuestToHostHandler;
|
|
26
|
+
/**
|
|
27
|
+
* Handle random bytes requests
|
|
28
|
+
* requestData: { guest_to_host: "Random", len?: number }
|
|
29
|
+
* Returns: JSON bytes of { host_to_guest: "Random", bytes: string (base64) }
|
|
30
|
+
*/
|
|
31
|
+
Random?: GuestToHostHandler;
|
|
32
|
+
[key: string]: GuestToHostHandler | undefined;
|
|
33
|
+
}
|
|
34
|
+
export * from "./session";
|
|
35
|
+
export * from "./auth";
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session-related types for T3n SDK
|
|
3
|
+
*/
|
|
4
|
+
export interface SessionId {
|
|
5
|
+
readonly value: string;
|
|
6
|
+
}
|
|
7
|
+
export interface Did {
|
|
8
|
+
readonly value: string;
|
|
9
|
+
toString(): string;
|
|
10
|
+
}
|
|
11
|
+
export interface HandshakeResult {
|
|
12
|
+
readonly sessionId: SessionId;
|
|
13
|
+
readonly expiry: number;
|
|
14
|
+
readonly authenticated: boolean;
|
|
15
|
+
readonly did?: Did;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Simple status enum - mirrors server SessionStatus only
|
|
19
|
+
*/
|
|
20
|
+
export declare enum SessionStatus {
|
|
21
|
+
Init = 0,
|
|
22
|
+
Encrypted = 1,
|
|
23
|
+
Authenticated = 2
|
|
24
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cryptographic utilities for T3n SDK
|
|
3
|
+
*
|
|
4
|
+
* Cross-platform crypto support:
|
|
5
|
+
* - Browsers: Uses Web Crypto API (global crypto)
|
|
6
|
+
* - Node.js 19+: Uses global crypto
|
|
7
|
+
* - Node.js 16-18: Imports crypto module
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Generate a cryptographically secure random string
|
|
11
|
+
* @param length - Length of the random string in bytes
|
|
12
|
+
* @returns Base64-encoded random string
|
|
13
|
+
*/
|
|
14
|
+
export declare function generateRandomString(length?: number): string;
|
|
15
|
+
/**
|
|
16
|
+
* Generate a UUID v4
|
|
17
|
+
* @returns UUID string
|
|
18
|
+
*/
|
|
19
|
+
export declare function generateUUID(): string;
|
|
20
|
+
/**
|
|
21
|
+
* Fill an array with cryptographically secure random values
|
|
22
|
+
* Cross-platform wrapper for crypto.getRandomValues
|
|
23
|
+
* @param array - Array to fill with random values
|
|
24
|
+
* @returns The same array (for compatibility with Web Crypto API)
|
|
25
|
+
*/
|
|
26
|
+
export declare function getRandomValues(array: Uint8Array): Uint8Array;
|
|
27
|
+
/**
|
|
28
|
+
* Convert a string to Uint8Array
|
|
29
|
+
* @param str - String to convert
|
|
30
|
+
* @returns Uint8Array representation
|
|
31
|
+
*/
|
|
32
|
+
export declare function stringToBytes(str: string): Uint8Array;
|
|
33
|
+
/**
|
|
34
|
+
* Convert Uint8Array to string
|
|
35
|
+
* @param bytes - Bytes to convert
|
|
36
|
+
* @returns String representation
|
|
37
|
+
*/
|
|
38
|
+
export declare function bytesToString(bytes: Uint8Array): string;
|
|
39
|
+
export declare function bytesToBase64(bytes: Uint8Array): string;
|
|
40
|
+
export declare function base64ToBytes(base64: string): Uint8Array;
|
|
41
|
+
/**
|
|
42
|
+
* Sign raw bytes using EIP-191 format (Ethereum Signed Message)
|
|
43
|
+
* This is used for signing webhook payloads for provider authentication
|
|
44
|
+
*
|
|
45
|
+
* Format: "\x19Ethereum Signed Message:\n{len}" + bytes
|
|
46
|
+
* Then hash with Keccak256 and sign
|
|
47
|
+
*
|
|
48
|
+
* @param privateKey - Ethereum private key (0x prefixed hex string)
|
|
49
|
+
* @param bytes - Raw bytes to sign
|
|
50
|
+
* @returns Hex-encoded signature with 0x prefix (65 bytes: r + s + v)
|
|
51
|
+
*/
|
|
52
|
+
export declare function signRawBytesWithEip191(privateKey: string, bytes: Uint8Array): Promise<string>;
|