Docs Reference

Transports

Options of PostMessageTransport, including how to allow several host origins.

A transport moves messages between the client and the wallet. Most apps never create one: pass hostOrigin to the client and it builds the right transport. Create a transport yourself only when something else must share it, such as HostStorageRelayAdapter.

Every transport implements ITransport:

MemberSignatureRequired
send(message: BridgeMessage) => Promise<void>yes
onMessage(handler: MessageHandler) => UnsubscribeFnyes
request<T>(message: Partial<BridgeMessage>, timeoutMs?: number) => Promise<BridgeMessage<T>>no; when missing, the client drives requests through send and onMessage
destroy() => voidno

PostMessageTransport

Used in embedded mode (the app runs in an iframe inside the App Center). Options type: PostMessageTransportOptions.

OptionTypeDefaultDescription
hostOriginstring | string[]requiredExact origin of the host shell, for example https://alpha.hydraone.app, or a list of origins. Each entry is normalized to its origin, so a trailing slash or path is dropped.
targetWindowPostMessageTargetwindow.parent (inside an iframe)Window that receives outgoing messages.
sourceWindowMessageEventTargetwindowWindow that emits incoming message events.
envstringprocess.env.NODE_ENV, else developmentEnvironment used for the wildcard check.
checkIframeSourcebooleantrueWhen inside an iframe, accept messages only from window.parent.
defaultTimeoutMsnumber15000Default response timeout for request().

Every incoming message is checked against the allowed origins first. A message from any other origin, or (with checkIframeSource) from a window other than window.parent, is silently dropped and never processed. No error is thrown, so a wrong hostOrigin shows up as a handshake timeout (ERR_TIMEOUT).

The wildcard '*' is accepted only when the environment is known to be development or test. The constructor throws HydraSecurityError (ERR_UNTRUSTED_ORIGIN) for '*' in any other environment, including when env is not set and process.env.NODE_ENV is unavailable.

The constructor throws HydraTransportError when hostOrigin is missing, empty, contains a blank entry or is not a valid origin.

Methods and properties

MemberDescription
send(message)Posts a message. Fills in id, timestamp and source when missing. Throws HydraTransportError with ERR_NOT_IN_IFRAME when there is no target window, or ERR_POSTMESSAGE_FAILED when postMessage throws.
request(message, timeoutMs?)Sends a message and resolves with the matching response. Rejects with HydraTimeoutError, HydraUserRejectedError, or HydraBridgeError carrying the host's code (ERR_RPC_FAILED when the host sent none).
onMessage(handler)Subscribes to every accepted message. Returns an unsubscribe function.
destroy()Removes the listener and rejects pending requests with HydraTransportError.
isClosed()Whether destroy() was called.
getPendingCount(), getPendingEntry(id)Pending request bookkeeping, for tests and diagnostics.
handleMessageEvent(event)The message listener itself. Exposed so tests can feed events directly.
hostOriginThe first allowed origin (or '*').
allowedOriginsThe full normalized list of allowed origins.
envThe resolved environment.

Several host origins

Pass an array when the app can be embedded by more than one host, for example production and staging:

new PostMessageTransport({
  hostOrigin: ['https://alpha.hydraone.app', 'https://staging.hydraone.app'],
});
  • Messages are accepted from any listed origin.
  • postMessage takes a single target origin, so replies go to the origin of the last accepted message, then to the parent frame's origin when it is listed.
  • Before the host has been heard from (the first CLIENT_READY), the message is posted to each listed origin. The browser delivers it only to the window whose origin matches, and nothing is ever posted outside the list.
  • The list is trimmed, normalized to origins and de-duplicated. An empty list or a blank entry throws.
  • A '*' entry collapses the list to '*' and follows the wildcard rule above.
  • transport.allowedOrigins holds the full list and transport.hostOrigin the first entry.

Let the client create the transport

WalletBridgeClient also accepts hostOrigin (a string or an array). When no transport is given, the client creates the PostMessageTransport itself. A transport you pass always takes precedence. Either way, client.destroy() destroys the client's transport.

See also