Skip to content

Commit

Permalink
fix to convert caller address to cosmos address before signer checking (
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 authored May 22, 2024
1 parent a23ca15 commit e728aa7
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions x/evm/precompiles/cosmos/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,31 +151,27 @@ func (e CosmosPrecompile) ExtendedRun(caller vm.ContractRef, input []byte, suppl
return nil, ctx.GasMeter().GasConsumedToLimit(), types.ErrPrecompileFailed.Wrap(err.Error())
}

for _, signer := range signers {
if bytes.Equal(caller.Address().Bytes(), signer) {
continue
callerAddr := caller.Address().Bytes()
callerAccount := e.ak.GetAccount(ctx, callerAddr)

// if the caller is a shorthand account, then use shorthand account's original address
// as the caller address.
if shorthandCallerAccount, ok := callerAccount.(types.ShorthandAccountI); ok {
addr, err := shorthandCallerAccount.GetOriginalAddress(e.ac)
if err != nil {
return nil, ctx.GasMeter().GasConsumedToLimit(), types.ErrPrecompileFailed.Wrap(err.Error())
}

// if signer is different from the caller, check if the signer is a shorthand account.
// and then check shorthand account's original address is the same with the caller.
if len(signer) != common.AddressLength {
signerAccount := e.ak.GetAccount(ctx, signer)
if shorthandCallerAccount, ok := signerAccount.(types.ShorthandAccountI); ok {
addr, err := shorthandCallerAccount.GetOriginalAddress(e.ac)
if err != nil {
return nil, ctx.GasMeter().GasConsumedToLimit(), types.ErrPrecompileFailed.Wrap(err.Error())
}

if bytes.Equal(addr.Bytes(), signer) {
continue
}
}
}
callerAddr = addr.Bytes()
}

return nil, ctx.GasMeter().GasConsumedToLimit(), sdkerrors.ErrUnauthorized.Wrapf(
"required signer: `%s`, given signer: `%s`",
hexutil.Encode(signer), caller.Address(),
)
for _, signer := range signers {
if !bytes.Equal(callerAddr, signer) {
return nil, ctx.GasMeter().GasConsumedToLimit(), sdkerrors.ErrUnauthorized.Wrapf(
"required signer: `%s`, given signer: `%s`",
hexutil.Encode(signer), caller.Address(),
)
}
}

messages := ctx.Value(types.CONTEXT_KEY_COSMOS_MESSAGES).(*[]sdk.Msg)
Expand Down

0 comments on commit e728aa7

Please sign in to comment.