laconicd-deprecated/x/evm/client/cli/utils.go
Austin Abell 6eef37b0c6
Finish and clean up module queries and txs (#152)
* Basic transactions set up (to be separated)

* Change transaction command to not include create operation (to include other command in next commit)

* set up create command and made minor changes

* wip implements module queries

* Added tests for query address decoding

* Added ambiguous encoding of to address in transaction and added tests

* Fix linting issue

* Move registering key types to application level to allow module usage to ignore

* Move genaccounts code to be reused

* Switches nonce increase to always happen in ante handler

* change SetNonce from keeper to point to actual nonce operation

* Remove no op nonce switch (not needed with clearing cache)

* Changes to update all accounts pre state transition and clear cache at end of block

* Update accounts before end of block commit (edge case where necessary)

* Fix nonce of sender going into evm in case it's checked, and let evm set contract starting nonce
2019-11-15 12:02:13 -05:00

64 lines
1.4 KiB
Go

package cli
import (
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/ethereum/go-ethereum/common"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func accountToHex(addr string) (string, error) {
if strings.HasPrefix(addr, sdk.Bech32PrefixAccAddr) {
// Check to see if address is Cosmos bech32 formatted
toAddr, err := sdk.AccAddressFromBech32(addr)
if err != nil {
return "", errors.Wrap(err, "must provide a valid Bech32 address")
}
ethAddr := common.BytesToAddress(toAddr.Bytes())
return ethAddr.Hex(), nil
}
if !strings.HasPrefix(addr, "0x") {
addr = "0x" + addr
}
valid := common.IsHexAddress(addr)
if !valid {
return "", fmt.Errorf("%s is not a valid Ethereum or Cosmos address", addr)
}
ethAddr := common.HexToAddress(addr)
return ethAddr.Hex(), nil
}
func formatKeyToHash(key string) string {
if !strings.HasPrefix(key, "0x") {
key = "0x" + key
}
ethkey := common.HexToHash(key)
return ethkey.Hex()
}
func cosmosAddressFromArg(addr string) (sdk.AccAddress, error) {
if strings.HasPrefix(addr, sdk.Bech32PrefixAccAddr) {
// Check to see if address is Cosmos bech32 formatted
toAddr, err := sdk.AccAddressFromBech32(addr)
if err != nil {
return nil, errors.Wrap(err, "invalid bech32 formatted address")
}
return toAddr, nil
}
// Strip 0x prefix if exists
addr = strings.TrimPrefix(addr, "0x")
return sdk.AccAddressFromHex(addr)
}