Skip to content

The x/ibc-hooks middleware on BitBadges: IBCMiddleware and ICS4Middleware hook interfaces, transfer stack wiring, CombinedIBCHooks precedence, and intermediate sender derivation.

x/ibc-hooks (module name ibchooks, store key hooks-for-ibc) is the IBC middleware framework that sits at the outer edge of BitBadges' ICS-20 transfer stack. It wraps an underlying porttypes.IBCModule and porttypes.ICS4Wrapper and dispatches typed Override, Before, and After hooks around every IBC channel lifecycle, packet receive, acknowledgement, timeout, and outbound SendPacket call.

On BitBadges, x/ibc-hooks hosts both x/ibc-rate-limit and x/custom-hooks via CombinedIBCHooks in app/ibc.go.

Transfer Stack Wiring

BitBadges wires the ICS-20 transfer port with x/ibc-hooks outermost so hooks control both inbound acknowledgements and outbound packet gating:

text
Inbound packet (OnRecvPacket / OnAcknowledgementPacket / OnTimeoutPacket):
  IBC Core
    -> x/ibc-hooks (IBCMiddleware -> CombinedIBCHooks)
         1. x/ibc-rate-limit (checks inbound/refund rate limits first)
         2. x/custom-hooks (parses memo & runs transfer_tokens / swap_and_action atomically)
    -> packet-forward-middleware (PFM)
    -> ibc-callbacks (no-op contract keeper)
    -> x/ibc/transfer

Outbound packet (SendPacket):
  x/ibc/transfer
    -> ibc-callbacks
    -> packet-forward-middleware (PFM)
    -> x/ibc-hooks (ICS4Middleware -> CombinedIBCHooks)
         1. x/ibc-rate-limit (SendPacketOverride checks outbound flow caps)
    -> IBC ChannelKeeper

CombinedIBCHooks implements five x/ibc-hooks interfaces:

InterfaceActive delegate(s)Purpose
OnRecvPacketOverrideHooksx/ibc-rate-limit, then x/custom-hooksEnforce inbound rate limits before running memo hooks via customHooksWrapper
SendPacketOverrideHooksx/ibc-rate-limitEnforce outbound rate limits before forwarding via ICS4Middleware.SendPacketNext
OnAcknowledgementPacketOverrideHooksx/ibc-rate-limitRefund outbound rate-limit quota if the counterparty returned an error acknowledgement
OnTimeoutPacketOverrideHooksx/ibc-rate-limitRefund outbound rate-limit quota when an outbound packet times out
WriteAcknowledgementAfterHooksx/ibc-rate-limitObserve written acknowledgements after packet processing

Middleware Architecture and Hook Interfaces

x/ibc-hooks defines two wrapper types in x/ibc-hooks/ibc_module.go and x/ibc-hooks/ics4_middleware.go:

  • IBCMiddleware (porttypes.Middleware): wraps App (porttypes.IBCModule) and holds a pointer to ICS4Middleware. Implements SetICS4Wrapper and SetUnderlyingApplication for ibc-go v11 stack wiring.
  • ICS4Middleware (porttypes.ICS4Wrapper): wraps channel (porttypes.ICS4Wrapper) and holds Hooks (any struct implementing one or more hook interfaces). Exposes SendPacketNext so a SendPacketOverride implementation can forward to the wrapped channel without re-entering itself.

For every supported callback, x/ibc-hooks checks the Hooks value by Go interface assertion in this exact order:

  1. <Callback>OverrideHooks: if implemented, the override runs instead of the Before hook, the underlying app/channel call, and the After hook. The override is responsible for invoking the underlying app (im.App.<Callback>) or i.SendPacketNext as needed.
  2. <Callback>BeforeHooks: if no override is implemented, runs before the underlying call.
  3. Underlying call: calls im.App.<Callback> or i.channel.<Callback>.
  4. <Callback>AfterHooks: if no override is implemented, runs after the underlying call with its result/error.

Supported Callbacks

LayerCallbacks with Override, Before, and After interfaces
IBCMiddleware (channel lifecycle)OnChanOpenInit, OnChanOpenTry, OnChanOpenAck, OnChanOpenConfirm, OnChanCloseInit, OnChanCloseConfirm
IBCMiddleware (packet lifecycle)OnRecvPacket, OnAcknowledgementPacket, OnTimeoutPacket
ICS4Middleware (ICS-4 wrapper)SendPacket, WriteAcknowledgement, GetAppVersion

When an OnRecvPacketOverride hook returns a non-success acknowledgement, IBCMiddleware.OnRecvPacket logs the packet sequence, source port, and source channel to aid relayer and node debugging.

Intermediate Sender Derivation

x/ibc-hooks/types provides the canonical deterministic address derivation used by x/custom-hooks to attribute inbound IBC hook actions to a source-chain sender without letting any local key control that address:

go
const SenderPrefix = "ibc-hook-intermediary"

func DeriveIntermediateSender(channel, originalSender, bech32Prefix string) (string, error) {
    senderStr := fmt.Sprintf("%s/%s", channel, originalSender)
    senderHash32 := address.Hash(SenderPrefix, []byte(senderStr))
    sender := types.AccAddress(senderHash32[:])
    return types.Bech32ifyAddressBytes(bech32Prefix, sender)
}
  • channel is the destination channel on BitBadges (for example channel-0).
  • originalSender is the ICS-20 packet's sender string from the source chain.
  • bech32Prefix is bb on BitBadges.

In JavaScript/TypeScript, compute the same address with the SDK helper:

ts
import { deriveIntermediateSender } from 'bitbadges';

const intermediary = deriveIntermediateSender(
  'channel-0',
  'osmo1p0rrel3365scadq5k9pv0x0zp9j22js6x44a9w',
  'bb'
);

Use this derived address whenever a collection's approval initiatedByList needs to authorize a specific cross-chain sender.

Messages, Queries, Params, and CLI

  • Params: ibchooks.Params is defined in proto/ibchooks/params.proto and currently has no fields.
  • Messages, Queries, and CLI: x/ibc-hooks has no standalone transaction messages, no gRPC/LCD query service, and no CLI subcommands. All behavior is configured through chain middleware wiring (app/ibc.go) and the hooked modules (x/custom-hooks and x/ibc-rate-limit).

Edit this page on GitHub