github.com/cosmos/cosmos-sdk@v0.50.10/types/address/store_key.go (about)

     1  package address
     2  
     3  import (
     4  	errorsmod "cosmossdk.io/errors"
     5  
     6  	sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
     7  )
     8  
     9  // MaxAddrLen is the maximum allowed length (in bytes) for an address.
    10  const MaxAddrLen = 255
    11  
    12  // LengthPrefix prefixes the address bytes with its length, this is used
    13  // for example for variable-length components in store keys.
    14  func LengthPrefix(bz []byte) ([]byte, error) {
    15  	bzLen := len(bz)
    16  	if bzLen == 0 {
    17  		return bz, nil
    18  	}
    19  
    20  	if bzLen > MaxAddrLen {
    21  		return nil, errorsmod.Wrapf(sdkerrors.ErrUnknownAddress, "address length should be max %d bytes, got %d", MaxAddrLen, bzLen)
    22  	}
    23  
    24  	return append([]byte{byte(bzLen)}, bz...), nil
    25  }
    26  
    27  // MustLengthPrefix is LengthPrefix with panic on error.
    28  func MustLengthPrefix(bz []byte) []byte {
    29  	res, err := LengthPrefix(bz)
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  
    34  	return res
    35  }