Skip to main content
Use @thru/protocol when you are implementing the embedded wallet message layer directly, such as a host page, an iframe messenger, or a compatibility shim for @thru/browser-sdk.

Install

npm install @thru/protocol

When to use it

Choose this package when you need the wire format, not the higher-level wallet API:
  • Build a custom embedded wallet host.
  • Implement an iframe messenger that speaks the Thru protocol.
  • Validate request and response envelopes in tests or middleware.
  • Share request/event constants across browser packages.

Entry point

The package exposes a single public entry point at @thru/protocol.
import {
  DEFAULT_IFRAME_URL,
  EMBEDDED_PROVIDER_EVENTS,
  ErrorCode,
  IFRAME_READY_EVENT,
  POST_MESSAGE_EVENT_TYPE,
  POST_MESSAGE_REQUEST_TYPES,
  createRequestId,
} from '@thru/protocol';

What it exports

AreaKey exports
Request kindsPOST_MESSAGE_REQUEST_TYPES
Request envelopesPostMessageRequest, ConnectRequestMessage, SignMessageRequestMessage, SignTransactionRequestMessage, GetAccountsRequestMessage, SelectAccountRequestMessage
Response envelopesPostMessageResponse, SuccessfulPostMessageResponse, InferPostMessageResponse, InferSuccessfulPostMessageResponse
Channel eventsPOST_MESSAGE_EVENT_TYPE, IFRAME_READY_EVENT, EMBEDDED_PROVIDER_EVENTS, PostMessageEvent
Errors and helpersErrorCode, createRequestId, DEFAULT_IFRAME_URL
Shared payload typesAppMetadata, ConnectResult, WalletAccount

Example

import {
  POST_MESSAGE_REQUEST_TYPES,
  createRequestId,
} from "@thru/protocol";

const request = {
  id: createRequestId(),
  origin: window.location.origin,
  type: POST_MESSAGE_REQUEST_TYPES.CONNECT,
  payload: {
    metadata: {
      appName: "Explorer",
      appUrl: window.location.origin,
    },
  },
};

Request flow

Every request envelope includes an id and origin, plus a type and optional payload:
type PostMessageRequest =
  | ConnectRequestMessage
  | DisconnectRequestMessage
  | SignMessageRequestMessage
  | SignTransactionRequestMessage
  | GetAccountsRequestMessage
  | SelectAccountRequestMessage;
connect requests carry ConnectRequestPayload, which currently accepts optional app metadata. signMessage and signTransaction requests carry the corresponding payload types, while getAccounts and disconnect are payload-free.

Response flow

Responses are discriminated by success:
  • success: true returns a typed result based on the request type
  • success: false returns an error payload with an ErrorCode and message
The package also exports InferPostMessageResponse and InferSuccessfulPostMessageResponse so you can derive the matching response type from a request type at compile time.

Shared types

The package re-exports AppMetadata and ConnectResult from @thru/chain-interfaces, and its payloads also depend on WalletAccount. If you are already using @thru/browser-sdk or @thru/embedded-provider, this package is the lower-level protocol layer those packages build on.