diff --git a/testutil/sample/sample.go b/testutil/sample/sample.go index 378c86f..ea76ef9 100644 --- a/testutil/sample/sample.go +++ b/testutil/sample/sample.go @@ -33,7 +33,7 @@ func AccAddress() string { func AddressBz() []byte { pk := ed25519.GenPrivKey().PubKey() address := sdk.AccAddress(pk.Address()).String() - _, bz, _ := bech32.DecodeToBase256(address) + _, bz, _ := bech32.DecodeNoLimit(address) return bz } @@ -47,7 +47,7 @@ type Account struct { func TestAccount() Account { pk := ed25519.GenPrivKey().PubKey() address := sdk.AccAddress(pk.Address()).String() - _, bz, _ := bech32.DecodeToBase256(address) + _, bz, _ := bech32.DecodeNoLimit(address) return Account{ Address: address, AddressBz: bz, @@ -58,11 +58,10 @@ func TestAccount() Account { func TestAccountBech32m() Account { pk := ed25519.GenPrivKey().PubKey() address := sdk.AccAddress(pk.Address()).String() - hrp, bz256, _ := bech32.DecodeToBase256(address) - bz32, _ := bech32.ConvertBits(bz256, 8, 5, true) // EncodeM only accepts base32 encoded data - address, _ = bech32.EncodeM(hrp, bz32) + hrp, bz, _ := bech32.DecodeNoLimit(address) + address, _ = bech32.EncodeM(hrp, bz) return Account{ Address: address, - AddressBz: bz256, + AddressBz: bz, } } diff --git a/x/blockibc/blockibc.go b/x/blockibc/blockibc.go index 4955ed9..4c5949a 100644 --- a/x/blockibc/blockibc.go +++ b/x/blockibc/blockibc.go @@ -18,7 +18,6 @@ package blockibc import ( "cosmossdk.io/errors" - "github.com/btcsuite/btcd/btcutil/bech32" "github.com/circlefin/noble-fiattokenfactory/x/fiattokenfactory/keeper" "github.com/circlefin/noble-fiattokenfactory/x/fiattokenfactory/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -123,7 +122,7 @@ func (im IBCMiddleware) OnRecvPacket( return channeltypes.NewErrorAcknowledgement(types.ErrPaused) } - _, addressBz, err := bech32.DecodeToBase256(data.Receiver) + _, addressBz, err := types.DecodeAddress(data.Receiver) if err != nil { return channeltypes.NewErrorAcknowledgement(err) } @@ -134,7 +133,7 @@ func (im IBCMiddleware) OnRecvPacket( return channeltypes.NewErrorAcknowledgement(ackErr) } - _, addressBz, err = bech32.DecodeToBase256(data.Sender) + _, addressBz, err = types.DecodeAddress(data.Sender) if err != nil { return channeltypes.NewErrorAcknowledgement(err) } diff --git a/x/blockibc/blockibc_test.go b/x/blockibc/blockibc_test.go index 2db7453..47c5a42 100644 --- a/x/blockibc/blockibc_test.go +++ b/x/blockibc/blockibc_test.go @@ -33,8 +33,8 @@ func TestBlockIBC(t *testing.T) { // ARRANGE: Mock sender and receiver. sender, receiver := sample.TestAccount(), sample.TestAccount() senderBech32m, receiverBech32m := sample.TestAccountBech32m(), sample.TestAccountBech32m() - receiverAddress, _ := codec.NewBech32Codec("osmo").BytesToString(receiver.AddressBz) - receiverBech32mAddress, _ := codec.NewBech32Codec("osmo").BytesToString(receiverBech32m.AddressBz) + receiverAddress, _ := codec.NewBech32Codec("osmo").BytesToString(fiattokenfactorytypes.MustConvertToBase256(receiver.AddressBz)) + receiverBech32mAddress, _ := codec.NewBech32Codec("osmo").BytesToString(fiattokenfactorytypes.MustConvertToBase256(receiverBech32m.AddressBz)) // ARRANGE: Organize table driven test cases. testCases := map[string]struct { diff --git a/x/fiattokenfactory/ante.go b/x/fiattokenfactory/ante.go index 4741306..532cecd 100644 --- a/x/fiattokenfactory/ante.go +++ b/x/fiattokenfactory/ante.go @@ -20,7 +20,6 @@ import ( "errors" sdkerrors "cosmossdk.io/errors" - "github.com/btcsuite/btcd/btcutil/bech32" fiattokenfactorykeeper "github.com/circlefin/noble-fiattokenfactory/x/fiattokenfactory/keeper" "github.com/circlefin/noble-fiattokenfactory/x/fiattokenfactory/types" fiattokenfactorytypes "github.com/circlefin/noble-fiattokenfactory/x/fiattokenfactory/types" @@ -167,7 +166,7 @@ func (ad IsBlacklistedDecorator) CheckMessages(ctx sdk.Context, msgs []sdk.Msg, func checkForBlacklistedAddressByTokenFactory(ctx sdk.Context, address string, c sdk.Coin, ctf *fiattokenfactorykeeper.Keeper) error { ctfMintingDenom := ctf.GetMintingDenom(ctx) if c.Denom == ctfMintingDenom.Denom { - _, addressBz, err := bech32.DecodeToBase256(address) + _, addressBz, err := types.DecodeAddress(address) if err != nil { return err } diff --git a/x/fiattokenfactory/keeper/grpc_query_blacklisted.go b/x/fiattokenfactory/keeper/grpc_query_blacklisted.go index 9f5d836..b2b2dea 100644 --- a/x/fiattokenfactory/keeper/grpc_query_blacklisted.go +++ b/x/fiattokenfactory/keeper/grpc_query_blacklisted.go @@ -20,7 +20,6 @@ import ( "context" "cosmossdk.io/store/prefix" - "github.com/btcsuite/btcd/btcutil/bech32" "github.com/circlefin/noble-fiattokenfactory/x/fiattokenfactory/types" "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/types/query" @@ -59,7 +58,7 @@ func (k Keeper) Blacklisted(ctx context.Context, req *types.QueryGetBlacklistedR return nil, status.Error(codes.InvalidArgument, "invalid request") } - _, addressBz, err := bech32.DecodeToBase256(req.Address) + _, addressBz, err := types.DecodeAddress(req.Address) if err != nil { return nil, err } diff --git a/x/fiattokenfactory/keeper/keeper.go b/x/fiattokenfactory/keeper/keeper.go index 8915ef0..6d2e8fe 100644 --- a/x/fiattokenfactory/keeper/keeper.go +++ b/x/fiattokenfactory/keeper/keeper.go @@ -20,7 +20,6 @@ import ( "context" "fmt" - "github.com/btcsuite/btcd/btcutil/bech32" "github.com/circlefin/noble-fiattokenfactory/x/fiattokenfactory/types" "cosmossdk.io/core/store" @@ -82,7 +81,7 @@ func (k Keeper) SendRestrictionFn(ctx context.Context, fromAddr, toAddr sdk.AccA grantees := ctx.Value(types.GranteeKey) if grantees != nil { for _, grantee := range grantees.([]string) { - _, addressBz, err := bech32.DecodeToBase256(grantee) + _, addressBz, err := types.DecodeAddress(grantee) if err != nil { return toAddr, err } diff --git a/x/fiattokenfactory/keeper/msg_server_blacklist.go b/x/fiattokenfactory/keeper/msg_server_blacklist.go index 06fabeb..2db075b 100644 --- a/x/fiattokenfactory/keeper/msg_server_blacklist.go +++ b/x/fiattokenfactory/keeper/msg_server_blacklist.go @@ -19,7 +19,6 @@ package keeper import ( "context" - "github.com/btcsuite/btcd/btcutil/bech32" "github.com/circlefin/noble-fiattokenfactory/x/fiattokenfactory/types" sdkerrors "cosmossdk.io/errors" @@ -38,7 +37,7 @@ func (k msgServer) Blacklist(goCtx context.Context, msg *types.MsgBlacklist) (*t return nil, sdkerrors.Wrapf(types.ErrUnauthorized, "you are not the blacklister") } - _, addressBz, err := bech32.DecodeToBase256(msg.Address) + _, addressBz, err := types.DecodeAddress(msg.Address) if err != nil { return nil, err } diff --git a/x/fiattokenfactory/keeper/msg_server_burn.go b/x/fiattokenfactory/keeper/msg_server_burn.go index d038606..47327ad 100644 --- a/x/fiattokenfactory/keeper/msg_server_burn.go +++ b/x/fiattokenfactory/keeper/msg_server_burn.go @@ -22,7 +22,6 @@ import ( "github.com/circlefin/noble-fiattokenfactory/x/fiattokenfactory/types" sdkerrors "cosmossdk.io/errors" - "github.com/btcsuite/btcd/btcutil/bech32" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -37,7 +36,7 @@ func (k Keeper) Burn(ctx sdk.Context, msg *types.MsgBurn) (*types.MsgBurnRespons return nil, sdkerrors.Wrapf(types.ErrBurn, "%v: you are not a minter", types.ErrUnauthorized) } - _, addressBz, err := bech32.DecodeToBase256(msg.From) + _, addressBz, err := types.DecodeAddress(msg.From) if err != nil { return nil, sdkerrors.Wrap(types.ErrBurn, err.Error()) } diff --git a/x/fiattokenfactory/keeper/msg_server_mint.go b/x/fiattokenfactory/keeper/msg_server_mint.go index 82896e7..26d6815 100644 --- a/x/fiattokenfactory/keeper/msg_server_mint.go +++ b/x/fiattokenfactory/keeper/msg_server_mint.go @@ -19,7 +19,6 @@ package keeper import ( "context" - "github.com/btcsuite/btcd/btcutil/bech32" "github.com/circlefin/noble-fiattokenfactory/x/fiattokenfactory/types" sdkerrors "cosmossdk.io/errors" @@ -38,7 +37,7 @@ func (k Keeper) Mint(ctx sdk.Context, msg *types.MsgMint) (*types.MsgMintRespons return nil, sdkerrors.Wrapf(types.ErrUnauthorized, "you are not a minter") } - _, addressBz, err := bech32.DecodeToBase256(msg.From) + _, addressBz, err := types.DecodeAddress(msg.From) if err != nil { return nil, err } @@ -48,7 +47,7 @@ func (k Keeper) Mint(ctx sdk.Context, msg *types.MsgMint) (*types.MsgMintRespons return nil, sdkerrors.Wrapf(types.ErrMint, "minter address is blacklisted") } - _, addressBz, err = bech32.DecodeToBase256(msg.Address) + _, addressBz, err = types.DecodeAddress(msg.Address) if err != nil { return nil, err } diff --git a/x/fiattokenfactory/keeper/msg_server_unblacklist.go b/x/fiattokenfactory/keeper/msg_server_unblacklist.go index 7cd477c..ec5a714 100644 --- a/x/fiattokenfactory/keeper/msg_server_unblacklist.go +++ b/x/fiattokenfactory/keeper/msg_server_unblacklist.go @@ -19,7 +19,6 @@ package keeper import ( "context" - "github.com/btcsuite/btcd/btcutil/bech32" "github.com/circlefin/noble-fiattokenfactory/x/fiattokenfactory/types" sdkerrors "cosmossdk.io/errors" @@ -38,7 +37,7 @@ func (k msgServer) Unblacklist(goCtx context.Context, msg *types.MsgUnblacklist) return nil, sdkerrors.Wrapf(types.ErrUnauthorized, "you are not the blacklister") } - _, addressBz, err := bech32.DecodeToBase256(msg.Address) + _, addressBz, err := types.DecodeAddress(msg.Address) if err != nil { return nil, err } diff --git a/x/fiattokenfactory/types/address.go b/x/fiattokenfactory/types/address.go new file mode 100644 index 0000000..18fa785 --- /dev/null +++ b/x/fiattokenfactory/types/address.go @@ -0,0 +1,43 @@ +// Copyright 2024 Circle Internet Group, Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: Apache-2.0 + +package types + +import ( + "github.com/btcsuite/btcd/btcutil/bech32" +) + +// DecodeAddress decodes the given address, returning the base32 encoded address bytes +// Support both bech32 and bech32m +func DecodeAddress(address string) (string, []byte, error) { + return bech32.DecodeNoLimit(address) +} + +// ConvertToBase256 converts base32 encoded address to base256 +func ConvertToBase256(address []byte) ([]byte, error) { + return bech32.ConvertBits(address, 5, 8, false) +} + +// MustConvertToBase256 converts base32 encoded address to base256 +// Panic if there is any error +func MustConvertToBase256(address []byte) []byte { + bz, err := bech32.ConvertBits(address, 5, 8, false) + if err != nil { + panic(err) + } + + return bz +}