fix address encoding

This commit is contained in:
Roy Crihfield 2024-11-30 00:44:00 +08:00
parent 78da408563
commit 41c6d26ec9
13 changed files with 140 additions and 59 deletions

11
go.mod
View File

@ -30,7 +30,6 @@ require (
cosmossdk.io/tools/confix v0.1.0
github.com/99designs/gqlgen v0.17.22
github.com/cometbft/cometbft v1.0.0-rc1.0.20240908111210-ab0be101882f
github.com/cometbft/cometbft/api v1.0.0-rc.1
github.com/cosmos/cosmos-db v1.0.3-0.20240911104526-ddc3f09bfc22 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.5
github.com/cosmos/cosmos-sdk v1.0.0
@ -62,13 +61,8 @@ require (
)
require (
cosmossdk.io/server/v2/appmanager v0.0.0-20240802110823-cffeedff643d
cosmossdk.io/server/v2/stf v0.0.0-20240708142107-25e99c54bac1
cosmossdk.io/x/accounts v1.0.0-alpha.4
cosmossdk.io/x/accounts/defaults/base v0.0.0-00010101000000-000000000000
cosmossdk.io/x/accounts/defaults/lockup v0.0.0-00010101000000-000000000000
cosmossdk.io/x/accounts/defaults/multisig v0.0.0-00010101000000-000000000000
cosmossdk.io/x/bank v1.0.0-alpha.4
cosmossdk.io/x/bank v0.0.0-20240226161501-23359a0b6d91
cosmossdk.io/x/consensus v0.0.0-20241007000829-38662ecb209f
cosmossdk.io/x/distribution v0.0.0-20241007000829-38662ecb209f
cosmossdk.io/x/gov v1.0.0-alpha.4
@ -84,6 +78,8 @@ require (
cosmossdk.io/core/testing v0.0.0 // indirect
cosmossdk.io/errors/v2 v2.0.0-20240731132947-df72853b3ca5 // indirect
cosmossdk.io/schema v0.3.1-0.20241010135032-192601639cac // indirect
cosmossdk.io/server/v2/appmanager v0.0.0-20240802110823-cffeedff643d // indirect
cosmossdk.io/server/v2/stf v0.0.0-20240708142107-25e99c54bac1 // indirect
cosmossdk.io/x/epochs v0.0.0-20240522060652-a1ae4c3e0337 // indirect
cosmossdk.io/x/tx v1.0.0-alpha.1 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
@ -108,6 +104,7 @@ require (
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/cometbft/cometbft-db v1.0.1 // indirect
github.com/cometbft/cometbft/api v1.0.0-rc.1 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/crypto v0.1.2 // indirect
github.com/cosmos/gogogateway v1.2.0 // indirect

View File

@ -1,12 +1,12 @@
package utils
import (
coreaddress "cosmossdk.io/core/address"
"cosmossdk.io/core/address"
errorsmod "cosmossdk.io/errors"
govtypes "cosmossdk.io/x/gov/types"
"github.com/cosmos/cosmos-sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
addresstypes "github.com/cosmos/cosmos-sdk/types/address"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
@ -20,12 +20,12 @@ func AddressOrModuleAddress(preferred, alt string) types.AccAddress {
if addr, err := addrCodec.StringToBytes(preferred); err == nil {
return addr
}
return address.Module(preferred)
return addresstypes.Module(preferred)
}
return address.Module(alt)
return addresstypes.Module(alt)
}
func CheckAuthorityAddress(ac coreaddress.Codec, expected sdk.AccAddress, msgaddr string) error {
func CheckAuthorityAddress(ac address.Codec, expected sdk.AccAddress, msgaddr string) error {
authority, err := ac.StringToBytes(msgaddr)
if err != nil {
return errorsmod.Wrap(sdkerrors.ErrInvalidAddress, msgaddr)

View File

@ -69,11 +69,14 @@ func GetCmdCommitBid() *cobra.Command {
}
auctionId := args[0]
fromAddr, err := clientCtx.AddressCodec.BytesToString(clientCtx.GetFromAddress())
if err != nil {
return err
}
reveal := map[string]interface{}{
"chainId": chainId,
"auctionId": auctionId,
"bidderAddress": clientCtx.GetFromAddress().String(),
"bidderAddress": fromAddr,
"bidAmount": bidAmount.String(),
"noise": mnemonic,
}

View File

@ -329,7 +329,7 @@ func (k Keeper) CreateAuction(ctx context.Context, msg auctiontypes.MsgCreateAuc
Address: sdk.AccAddress(signerAddress),
AccNum: account.GetAccountNumber(),
Sequence: account.GetSequence(),
}.Generate()
}.Generate(k.addressCodec)
// Compute timestamps.
now := k.HeaderService.HeaderInfo(ctx).Time

View File

@ -7,6 +7,8 @@ import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"git.vdb.to/cerc-io/laconicd/utils"
)
var (
@ -27,6 +29,10 @@ func NewMsgCreateAuction(
numProviders int32,
signer sdk.AccAddress,
) MsgCreateAuction {
signerStr, err := utils.NewAddressCodec().BytesToString(signer)
if err != nil {
panic(err)
}
return MsgCreateAuction{
CommitsDuration: commitsDuration,
RevealsDuration: revealsDuration,
@ -36,33 +42,45 @@ func NewMsgCreateAuction(
MaxPrice: maxPrice,
Kind: kind,
NumProviders: numProviders,
Signer: signer.String(),
Signer: signerStr,
}
}
// NewMsgCommitBid is the constructor function for MsgCommitBid.
func NewMsgCommitBid(auctionId string, commitHash string, signer sdk.AccAddress) MsgCommitBid {
signerStr, err := utils.NewAddressCodec().BytesToString(signer)
if err != nil {
panic(err)
}
return MsgCommitBid{
AuctionId: auctionId,
CommitHash: commitHash,
Signer: signer.String(),
Signer: signerStr,
}
}
// NewMsgRevealBid is the constructor function for MsgRevealBid.
func NewMsgRevealBid(auctionId string, reveal string, signer sdk.AccAddress) MsgRevealBid {
signerStr, err := utils.NewAddressCodec().BytesToString(signer)
if err != nil {
panic(err)
}
return MsgRevealBid{
AuctionId: auctionId,
Reveal: reveal,
Signer: signer.String(),
Signer: signerStr,
}
}
// NewMsgReleaseFunds is the constructor function for MsgReleaseFunds.
func NewMsgReleaseFunds(auctionId string, signer sdk.AccAddress) MsgReleaseFunds {
signerStr, err := utils.NewAddressCodec().BytesToString(signer)
if err != nil {
panic(err)
}
return MsgReleaseFunds{
AuctionId: auctionId,
Signer: signer.String(),
Signer: signerStr,
}
}

View File

@ -5,6 +5,7 @@ import (
"encoding/hex"
"fmt"
"cosmossdk.io/core/address"
sdk "github.com/cosmos/cosmos-sdk/types"
)
@ -43,9 +44,13 @@ type AuctionId struct {
}
// Generate creates the auction id.
func (auctionId AuctionId) Generate() string {
func (auctionId AuctionId) Generate(ac address.Codec) string {
hasher := sha256.New()
str := fmt.Sprintf("%s:%d:%d", auctionId.Address.String(), auctionId.AccNum, auctionId.Sequence)
addrStr, err := ac.BytesToString(auctionId.Address.Bytes())
if err != nil {
panic(err)
}
str := fmt.Sprintf("%s:%d:%d", addrStr, auctionId.AccNum, auctionId.Sequence)
hasher.Write([]byte(str))
return hex.EncodeToString(hasher.Sum(nil))
}

View File

@ -79,6 +79,7 @@ func NewKeeper(
k := Keeper{
Environment: env,
cdc: cdc,
addressCodec: addressCodec,
logger: logger.With(log.ModuleKey, "x/"+bondtypes.ModuleName),
authority: authority,
accountKeeper: accountKeeper,
@ -116,10 +117,13 @@ type BondId struct {
}
// Generate creates the bond Id.
func (bondId BondId) Generate() string {
func (bondId BondId) Generate(ac address.Codec) string {
hasher := sha256.New()
// TODO use address.codec
str := fmt.Sprintf("%s:%d:%d", bondId.Address.String(), bondId.AccNum, bondId.Sequence)
addrStr, err := ac.BytesToString(bondId.Address.Bytes())
if err != nil {
panic(err)
}
str := fmt.Sprintf("%s:%d:%d", addrStr, bondId.AccNum, bondId.Sequence)
hasher.Write([]byte(str))
return hex.EncodeToString(hasher.Sum(nil))
}
@ -210,14 +214,18 @@ func (k Keeper) CreateBond(ctx context.Context, ownerAddress sdk.AccAddress, coi
Address: ownerAddress,
AccNum: account.GetAccountNumber(),
Sequence: account.GetSequence(),
}.Generate()
}.Generate(k.addressCodec)
maxBondAmount, err := k.getMaxBondAmount(ctx)
if err != nil {
return nil, err
}
bond := bondtypes.Bond{Id: bondId, Owner: ownerAddress.String(), Balance: coins}
addrStr, err := k.addressCodec.BytesToString(ownerAddress)
if err != nil {
return nil, err
}
bond := bondtypes.Bond{Id: bondId, Owner: addrStr, Balance: coins}
if bond.Balance.IsAnyGT(maxBondAmount) {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Max bond amount exceeded")
}
@ -250,7 +258,11 @@ func (k Keeper) RefillBond(ctx context.Context, id string, ownerAddress sdk.AccA
return nil, err
}
if bond.Owner != ownerAddress.String() {
addrStr, err := k.addressCodec.BytesToString(ownerAddress)
if err != nil {
return nil, err
}
if bond.Owner != addrStr {
return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Bond owner mismatch")
}
@ -300,7 +312,11 @@ func (k Keeper) WithdrawBond(ctx context.Context, id string, ownerAddress sdk.Ac
return nil, err
}
if bond.Owner != ownerAddress.String() {
addrStr, err := k.addressCodec.BytesToString(ownerAddress)
if err != nil {
return nil, err
}
if bond.Owner != addrStr {
return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Bond owner mismatch")
}
@ -338,7 +354,11 @@ func (k Keeper) CancelBond(ctx context.Context, id string, ownerAddress sdk.AccA
return nil, err
}
if bond.Owner != ownerAddress.String() {
addrStr, err := k.addressCodec.BytesToString(ownerAddress)
if err != nil {
return nil, err
}
if bond.Owner != addrStr {
return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Bond owner mismatch")
}

View File

@ -4,15 +4,21 @@ import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"git.vdb.to/cerc-io/laconicd/utils"
)
var _ sdk.Msg = &MsgCreateBond{}
// NewMsgCreateBond is the constructor function for MsgCreateBond.
func NewMsgCreateBond(coins sdk.Coins, signer sdk.AccAddress) MsgCreateBond {
signerStr, err := utils.NewAddressCodec().BytesToString(signer)
if err != nil {
panic(err)
}
return MsgCreateBond{
Coins: coins,
Signer: signer.String(),
Signer: signerStr,
}
}

View File

@ -18,7 +18,7 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"git.vdb.to/cerc-io/laconicd/utils"
onboardingTypes "git.vdb.to/cerc-io/laconicd/x/onboarding"
"git.vdb.to/cerc-io/laconicd/x/onboarding"
)
type Keeper struct {
@ -32,8 +32,8 @@ type Keeper struct {
// state management
Schema collections.Schema
Params collections.Item[onboardingTypes.Params]
Participants collections.Map[string, onboardingTypes.Participant]
Params collections.Item[onboarding.Params]
Participants collections.Map[string, onboarding.Participant]
}
// NewKeeper creates a new Keeper instance
@ -49,11 +49,11 @@ func NewKeeper(
k := Keeper{
Environment: env,
addressCodec: addressCodec,
logger: logger.With(log.ModuleKey, "x/"+onboardingTypes.ModuleName),
logger: logger.With(log.ModuleKey, "x/"+onboarding.ModuleName),
authority: authority,
Params: collections.NewItem(sb, onboardingTypes.ParamsPrefix, "params", codec.CollValue[onboardingTypes.Params](cdc)),
Params: collections.NewItem(sb, onboarding.ParamsPrefix, "params", codec.CollValue[onboarding.Params](cdc)),
Participants: collections.NewMap(
sb, onboardingTypes.ParticipantsPrefix, "participants", collections.StringKey, codec.CollValue[onboardingTypes.Participant](cdc),
sb, onboarding.ParticipantsPrefix, "participants", collections.StringKey, codec.CollValue[onboarding.Participant](cdc),
),
}
@ -69,9 +69,9 @@ func NewKeeper(
func (k Keeper) OnboardParticipant(
ctx context.Context,
msg *onboardingTypes.MsgOnboardParticipant,
msg *onboarding.MsgOnboardParticipant,
signerAddress sdk.AccAddress,
) (*onboardingTypes.MsgOnboardParticipantResponse, error) {
) (*onboarding.MsgOnboardParticipantResponse, error) {
params, err := k.Params.Get(ctx)
if err != nil {
return nil, err
@ -96,8 +96,13 @@ func (k Keeper) OnboardParticipant(
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Recovered ethereum address does not match the address set in payload")
}
participant := &onboardingTypes.Participant{
CosmosAddress: signerAddress.String(),
// TODO: redundant
cosmosAddr, err := k.addressCodec.BytesToString(signerAddress)
if err != nil {
return nil, err
}
participant := &onboarding.Participant{
CosmosAddress: cosmosAddr,
NitroAddress: nitroAddress,
Role: msg.Role,
KycId: msg.KycId,
@ -107,17 +112,17 @@ func (k Keeper) OnboardParticipant(
return nil, err
}
return &onboardingTypes.MsgOnboardParticipantResponse{}, nil
return &onboarding.MsgOnboardParticipantResponse{}, nil
}
func (k Keeper) StoreParticipant(ctx context.Context, participant *onboardingTypes.Participant) error {
func (k Keeper) StoreParticipant(ctx context.Context, participant *onboarding.Participant) error {
key := participant.CosmosAddress
return k.Participants.Set(ctx, key, *participant)
}
// ListParticipants - get all participants.
func (k Keeper) ListParticipants(ctx context.Context) ([]*onboardingTypes.Participant, error) {
var participants []*onboardingTypes.Participant
func (k Keeper) ListParticipants(ctx context.Context) ([]*onboarding.Participant, error) {
var participants []*onboarding.Participant
iter, err := k.Participants.Iterate(ctx, nil)
if err != nil {
@ -137,23 +142,23 @@ func (k Keeper) ListParticipants(ctx context.Context) ([]*onboardingTypes.Partic
}
// GetParticipantByAddress - get participant by cosmos (laconic) address.
func (k Keeper) GetParticipantByAddress(ctx context.Context, address string) (onboardingTypes.Participant, error) {
func (k Keeper) GetParticipantByAddress(ctx context.Context, address string) (onboarding.Participant, error) {
participant, err := k.Participants.Get(ctx, address)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return onboardingTypes.Participant{}, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "participant with given address not found.")
return onboarding.Participant{}, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "participant with given address not found.")
}
return onboardingTypes.Participant{}, err
return onboarding.Participant{}, err
}
return participant, nil
}
// GetParticipantByNitroAddress - get participant by nitro address.
func (k Keeper) GetParticipantByNitroAddress(ctx context.Context, nitroAddress string) (onboardingTypes.Participant, error) {
var participant onboardingTypes.Participant
func (k Keeper) GetParticipantByNitroAddress(ctx context.Context, nitroAddress string) (onboarding.Participant, error) {
var participant onboarding.Participant
err := k.Participants.Walk(ctx, nil, func(key string, value onboardingTypes.Participant) (bool, error) {
err := k.Participants.Walk(ctx, nil, func(key string, value onboarding.Participant) (bool, error) {
if value.NitroAddress == nitroAddress {
participant = value
return true, nil
@ -162,7 +167,7 @@ func (k Keeper) GetParticipantByNitroAddress(ctx context.Context, nitroAddress s
return false, nil
})
if err != nil {
return onboardingTypes.Participant{}, err
return onboarding.Participant{}, err
}
return participant, nil

View File

@ -22,11 +22,11 @@ func (msg MsgOnboardParticipant) ValidateBasic() error {
}
if len(msg.EthSignature) != 132 {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Invalid signature.")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Invalid Eth signature")
}
if len(msg.KycId) == 0 {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Empty KYC ID.")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Empty KYC ID")
}
isRoleValid := false

View File

@ -135,6 +135,7 @@ func NewKeeper(
k := Keeper{
Environment: env,
cdc: cdc,
addressCodec: addressCodec,
logger: logger.With(log.ModuleKey, "x/"+registrytypes.ModuleName),
authority: authority,
accountKeeper: accountKeeper,

View File

@ -475,7 +475,11 @@ func (k Keeper) checkLRNAccess(ctx context.Context, signer sdk.AccAddress, lrn s
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Invalid LRN.")
}
if authority.OwnerAddress != signer.String() {
signerAddr, err := k.addressCodec.BytesToString(signer)
if err != nil {
return err
}
if authority.OwnerAddress != signerAddr {
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Access denied.")
}

View File

@ -4,16 +4,21 @@ import (
"net/url"
errorsmod "cosmossdk.io/errors"
"git.vdb.to/cerc-io/laconicd/utils"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// NewMsgSetRecord is the constructor function for MsgSetRecord.
func NewMsgSetRecord(payload Payload, bondId string, signer sdk.AccAddress) *MsgSetRecord {
signerStr, err := utils.NewAddressCodec().BytesToString(signer)
if err != nil {
panic(err)
}
return &MsgSetRecord{
Payload: payload,
BondId: bondId,
Signer: signer.String(),
Signer: signerStr,
}
}
@ -50,10 +55,19 @@ func (msg MsgRenewRecord) ValidateBasic() error {
// NewMsgReserveAuthority is the constructor function for MsgReserveName.
func NewMsgReserveAuthority(name string, signer sdk.AccAddress, owner sdk.AccAddress) MsgReserveAuthority {
addrCodec := utils.NewAddressCodec()
ownerStr, err := addrCodec.BytesToString(owner)
if err != nil {
panic(err)
}
signerStr, err := addrCodec.BytesToString(signer)
if err != nil {
panic(err)
}
return MsgReserveAuthority{
Name: name,
Owner: owner.String(),
Signer: signer.String(),
Owner: ownerStr,
Signer: signerStr,
}
}
@ -72,9 +86,13 @@ func (msg MsgReserveAuthority) ValidateBasic() error {
// NewMsgSetAuthorityBond is the constructor function for MsgSetAuthorityBond.
func NewMsgSetAuthorityBond(name string, bondID string, signer sdk.AccAddress) MsgSetAuthorityBond {
signerStr, err := utils.NewAddressCodec().BytesToString(signer)
if err != nil {
panic(err)
}
return MsgSetAuthorityBond{
Name: name,
Signer: signer.String(),
Signer: signerStr,
BondId: bondID,
}
}
@ -97,10 +115,14 @@ func (msg MsgSetAuthorityBond) ValidateBasic() error {
// NewMsgSetName is the constructor function for MsgSetName.
func NewMsgSetName(lrn string, cid string, signer sdk.AccAddress) *MsgSetName {
signerStr, err := utils.NewAddressCodec().BytesToString(signer)
if err != nil {
panic(err)
}
return &MsgSetName{
Lrn: lrn,
Cid: cid,
Signer: signer.String(),
Signer: signerStr,
}
}