Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve bech32 decoding #30

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions testutil/sample/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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,
Expand All @@ -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,
}
}
5 changes: 2 additions & 3 deletions x/blockibc/blockibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions x/blockibc/blockibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions x/fiattokenfactory/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions x/fiattokenfactory/keeper/grpc_query_blacklisted.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions x/fiattokenfactory/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions x/fiattokenfactory/keeper/msg_server_blacklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions x/fiattokenfactory/keeper/msg_server_burn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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())
}
Expand Down
5 changes: 2 additions & 3 deletions x/fiattokenfactory/keeper/msg_server_mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions x/fiattokenfactory/keeper/msg_server_unblacklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down
43 changes: 43 additions & 0 deletions x/fiattokenfactory/types/address.go
Original file line number Diff line number Diff line change
@@ -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
}