x/ibc-hooks
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:
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 ChannelKeeperCombinedIBCHooks implements five x/ibc-hooks interfaces:
| Interface | Active delegate(s) | Purpose |
|---|---|---|
OnRecvPacketOverrideHooks | x/ibc-rate-limit, then x/custom-hooks | Enforce inbound rate limits before running memo hooks via customHooksWrapper |
SendPacketOverrideHooks | x/ibc-rate-limit | Enforce outbound rate limits before forwarding via ICS4Middleware.SendPacketNext |
OnAcknowledgementPacketOverrideHooks | x/ibc-rate-limit | Refund outbound rate-limit quota if the counterparty returned an error acknowledgement |
OnTimeoutPacketOverrideHooks | x/ibc-rate-limit | Refund outbound rate-limit quota when an outbound packet times out |
WriteAcknowledgementAfterHooks | x/ibc-rate-limit | Observe 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): wrapsApp(porttypes.IBCModule) and holds a pointer toICS4Middleware. ImplementsSetICS4WrapperandSetUnderlyingApplicationfor ibc-go v11 stack wiring.ICS4Middleware(porttypes.ICS4Wrapper): wrapschannel(porttypes.ICS4Wrapper) and holdsHooks(any struct implementing one or more hook interfaces). ExposesSendPacketNextso aSendPacketOverrideimplementation 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:
<Callback>OverrideHooks: if implemented, the override runs instead of theBeforehook, the underlying app/channel call, and theAfterhook. The override is responsible for invoking the underlying app (im.App.<Callback>) ori.SendPacketNextas needed.<Callback>BeforeHooks: if no override is implemented, runs before the underlying call.- Underlying call: calls
im.App.<Callback>ori.channel.<Callback>. <Callback>AfterHooks: if no override is implemented, runs after the underlying call with its result/error.
Supported Callbacks
| Layer | Callbacks 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:
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)
}channelis the destination channel on BitBadges (for examplechannel-0).originalSenderis the ICS-20 packet'ssenderstring from the source chain.bech32Prefixisbbon BitBadges.
In JavaScript/TypeScript, compute the same address with the SDK helper:
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.Paramsis defined inproto/ibchooks/params.protoand currently has no fields. - Messages, Queries, and CLI:
x/ibc-hookshas 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).