* Start implementation * Add implementation + some e2e test * Fix lint * Squashed: sdk upgrade to v0.50 * rebuild protos with newer proto builder (cherry picked from commit fd8f4c1d0d2163f0a504356c16cd2d250f6218f3) * update ibc-go (cherry picked from commit fb8667960fbeedb7d242baa644572986a154d4b6) * bump cosmos-sdk and ibc in the v50 branch (#1616) * tidy * upgade ibc * remove the toolchain command * Bump sdk version * Use correct bech32 prefix * Bump SDK * Enable fraud system test again * Fix genesis param name * Fix import/export simulations * set log level for benchmarks (cherry picked from commit 1cfb93008c596db62d22aba882f37a469546bfb9) * Apply review comments * Remove gov beta1 helpers * Bump sdk version to latest in branch * Fix linter * Setup mergify for main * Update mergify for better branch name --------- Co-authored-by: Pino' Surace <pino.surace@live.it> Co-authored-by: Jacob Gadikian <jacobgadikian@gmail.com>
59 lines
2.0 KiB
Go
59 lines
2.0 KiB
Go
package keeper
|
|
|
|
import (
|
|
"strings"
|
|
|
|
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
|
|
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
|
|
|
|
errorsmod "cosmossdk.io/errors"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/CosmWasm/wasmd/x/wasm/types"
|
|
)
|
|
|
|
// bindIbcPort will reserve the port.
|
|
// returns a string name of the port or error if we cannot bind it.
|
|
// this will fail if call twice.
|
|
func (k Keeper) bindIbcPort(ctx sdk.Context, portID string) error {
|
|
portCap := k.portKeeper.BindPort(ctx, portID)
|
|
return k.ClaimCapability(ctx, portCap, host.PortPath(portID))
|
|
}
|
|
|
|
// ensureIbcPort is like registerIbcPort, but it checks if we already hold the port
|
|
// before calling register, so this is safe to call multiple times.
|
|
// Returns success if we already registered or just registered and error if we cannot
|
|
// (lack of permissions or someone else has it)
|
|
func (k Keeper) ensureIbcPort(ctx sdk.Context, contractAddr sdk.AccAddress) (string, error) {
|
|
portID := PortIDForContract(contractAddr)
|
|
if _, ok := k.capabilityKeeper.GetCapability(ctx, host.PortPath(portID)); ok {
|
|
return portID, nil
|
|
}
|
|
return portID, k.bindIbcPort(ctx, portID)
|
|
}
|
|
|
|
const portIDPrefix = "wasm."
|
|
|
|
func PortIDForContract(addr sdk.AccAddress) string {
|
|
return portIDPrefix + addr.String()
|
|
}
|
|
|
|
func ContractFromPortID(portID string) (sdk.AccAddress, error) {
|
|
if !strings.HasPrefix(portID, portIDPrefix) {
|
|
return nil, errorsmod.Wrapf(types.ErrInvalid, "without prefix")
|
|
}
|
|
return sdk.AccAddressFromBech32(portID[len(portIDPrefix):])
|
|
}
|
|
|
|
// AuthenticateCapability wraps the scopedKeeper's AuthenticateCapability function
|
|
func (k Keeper) AuthenticateCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) bool {
|
|
return k.capabilityKeeper.AuthenticateCapability(ctx, cap, name)
|
|
}
|
|
|
|
// ClaimCapability allows the transfer module to claim a capability
|
|
// that IBC module passes to it
|
|
func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error {
|
|
return k.capabilityKeeper.ClaimCapability(ctx, cap, name)
|
|
}
|