Transports
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:
| Member | Signature | Required |
|---|---|---|
send | (message: BridgeMessage) => Promise<void> | yes |
onMessage | (handler: MessageHandler) => UnsubscribeFn | yes |
request | <T>(message: Partial<BridgeMessage>, timeoutMs?: number) => Promise<BridgeMessage<T>> | no; when missing, the client drives requests through send and onMessage |
destroy | () => void | no |
PostMessageTransport
Used in embedded mode (the app runs in an iframe inside the App Center). Options type: PostMessageTransportOptions.
| Option | Type | Default | Description |
|---|---|---|---|
hostOrigin | string | string[] | required | Exact 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. |
targetWindow | PostMessageTarget | window.parent (inside an iframe) | Window that receives outgoing messages. |
sourceWindow | MessageEventTarget | window | Window that emits incoming message events. |
env | string | process.env.NODE_ENV, else development | Environment used for the wildcard check. |
checkIframeSource | boolean | true | When inside an iframe, accept messages only from window.parent. |
defaultTimeoutMs | number | 15000 | Default 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).
'*' 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
| Member | Description |
|---|---|
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. |
hostOrigin | The first allowed origin (or '*'). |
allowedOrigins | The full normalized list of allowed origins. |
env | The 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.
postMessagetakes 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.allowedOriginsholds the full list andtransport.hostOriginthe 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
- Security model for what the transport validates.
- Client options for the options of the client itself.
