Skip to content

Commit

Permalink
Merge pull request CosmWasm#2111 from CosmWasm/co/allow-custom-addres…
Browse files Browse the repository at this point in the history
…s-len

Make `ContractAddrLen` variable
  • Loading branch information
chipshort authored Jan 31, 2025
2 parents 5f0b5ef + b856840 commit 6c087f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
22 changes: 22 additions & 0 deletions x/wasm/keeper/addresses_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package keeper

import (
"encoding/hex"
"encoding/json"
"fmt"
"testing"

tmbytes "github.com/cometbft/cometbft/libs/bytes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/CosmWasm/wasmd/x/wasm/types"
)

func prepareCleanup(t *testing.T) {
Expand Down Expand Up @@ -69,6 +73,24 @@ func TestBuildContractAddressClassic(t *testing.T) {
}
}

func TestBuildContractAddressPredictableShort(t *testing.T) {
types.ContractAddrLen = 20
// reset to default value after test completion
defer func() { types.ContractAddrLen = 32 }()

checksum, err := hex.DecodeString("13a1fc994cc6d1c81b746ee0c0ff6f90043875e0bf1d9be6b7d779fc978dc2a5")
require.NoError(t, err)
creator, err := sdk.AccAddressFromHexUnsafe("9999999999aaaaaaaaaabbbbbbbbbbcccccccccc")
require.NoError(t, err)
salt, err := hex.DecodeString("61")
require.NoError(t, err)
expAddr, err := sdk.AccAddressFromHexUnsafe("5e865d3e45ad3e961f77fd77d46543417ced44d9")
require.NoError(t, err)

addr := BuildContractAddressPredictable(checksum, creator, salt, []byte{})
assert.Equal(t, expAddr, addr)
}

func TestBuildContractAddressPredictable(t *testing.T) {
// set cleanup function
prepareCleanup(t)
Expand Down
5 changes: 3 additions & 2 deletions x/wasm/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ const (
defaultSmartQueryGasLimit uint64 = 3_000_000
defaultContractDebugMode = false

// ContractAddrLen defines a valid address length for contracts
ContractAddrLen = 32
// SDKAddrLen defines a valid address length that was used in sdk address generation
SDKAddrLen = 20
)

// ContractAddrLen defines a valid address length for contracts
var ContractAddrLen = 32

func (m Model) ValidateBasic() error {
if len(m.Key) == 0 {
return errorsmod.Wrap(ErrEmpty, "key")
Expand Down

0 comments on commit 6c087f9

Please sign in to comment.