Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: add func BuildTxWithoutKeyDao for Avata #40

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func makeEncodingConfig() types.EncodingConfig {
TxConfig: txCfg,
Amino: amino,
}
RegisterLegacyAminoCodec(encodingConfig.Amino)
//RegisterLegacyAminoCodec(encodingConfig.Amino)
RegisterInterfaces(encodingConfig.InterfaceRegistry)
return encodingConfig
}
Expand Down
44 changes: 44 additions & 0 deletions client/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
commoncache "github.com/irisnet/core-sdk-go/common/cache"
commoncodec "github.com/irisnet/core-sdk-go/common/codec"
sdklog "github.com/irisnet/core-sdk-go/common/log"
sdk "github.com/irisnet/core-sdk-go/types"
sdktypes "github.com/irisnet/core-sdk-go/types"
"github.com/irisnet/core-sdk-go/types/tx"
)
Expand Down Expand Up @@ -137,6 +138,10 @@ func (base *baseClient) BuildAndSignWithAccount(addr string, accountNumber, sequ
return txByte, nil
}

func (base *baseClient) BroadcastTx(txBytes []byte, mode sdk.BroadcastMode) (res sdk.ResultTx, err sdk.Error) {
return base.broadcastTx(txBytes, mode)
}

func (base *baseClient) BuildAndSendWithAccount(addr string, accountNumber, sequence uint64, msg []sdktypes.Msg, baseTx sdktypes.BaseTx) (sdktypes.ResultTx, sdktypes.Error) {
txByte, ctx, err := base.buildTxWithAccount(addr, accountNumber, sequence, msg, baseTx)
if err != nil {
Expand Down Expand Up @@ -279,6 +284,45 @@ func (base *baseClient) SendBatch(msgs sdktypes.Msgs, baseTx sdktypes.BaseTx) (r
return rs, nil
}

func (base *baseClient) BuildTx(addr string, pubkey []byte, algo string, sequence, accountNumber uint64, msg []sdktypes.Msg, baseTx sdktypes.BaseTx) ([]byte, *sdktypes.Factory, sdktypes.TxBuilder, sdktypes.Error) {
builder, err := base.prepareWithAccount(addr, accountNumber, sequence, baseTx)
if err != nil {
return nil, nil, nil, sdktypes.Wrap(err)
}

unsignedTxBytes, txBuilder, err := builder.BuildTxWithoutKeyDao(pubkey, algo, msg)
if err != nil {
return nil, nil, nil, sdktypes.Wrap(err)
}

base.Logger().Debug("sign transaction success")
return unsignedTxBytes, builder, txBuilder, nil
}

func (base *baseClient) SetTxSignature(builder *sdktypes.Factory, txBuilder sdktypes.TxBuilder, pubkey []byte, algo string, signedData []byte, msgs []sdktypes.Msg) ([]byte, sdktypes.Error) {
txByte, err := builder.SetUnsignedTxSignature(txBuilder, pubkey, algo, signedData, msgs)
if err != nil {
return nil, sdktypes.Wrap(err)
}

base.Logger().Debug("sign transaction success")
return txByte, nil
}

func (base *baseClient) EstimateTxSize(addr string, pubkey []byte, algo string, sequence, accountNumber uint64, msgs []sdktypes.Msg, baseTx sdktypes.BaseTx, signedData []byte) ([]byte, sdktypes.Error) {
factory, err := base.prepareWithAccount(addr, accountNumber, sequence, baseTx)
if err != nil {
return nil, sdktypes.Wrap(err)
}
txByte, err := factory.SetUnsignedTxSignature(nil, pubkey, algo, signedData, msgs)
if err != nil {
return nil, sdktypes.Wrap(err)
}

base.Logger().Debug("sign transaction success")
return txByte, nil
}

func (base baseClient) QueryWithResponse(path string, data interface{}, result sdktypes.Response) error {
res, err := base.Query(path, data)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions client/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ func (k KeyManager) Import(name, password, armor string) (string, error) {
Algo: k.Algo,
}

fmt.Println(string(cryptoamino.MarshalPubkey(pubKey)))
fmt.Println(string(cryptoamino.MarshalPrivKey(priv)))
Comment on lines +135 to +136

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove unused code


err = k.KeyDAO.Write(name, password, info)
if err != nil {
return "", err
Expand Down
1 change: 1 addition & 0 deletions client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func (base baseClient) parseTxResult(res *ctypes.ResultTx, resBlock *ctypes.Resu
GasWanted: res.TxResult.GasWanted,
GasUsed: res.TxResult.GasUsed,
Events: sdk.StringifyEvents(res.TxResult.Events),
Data: res.TxResult.Data,
},
Timestamp: resBlock.Block.Time.Format(time.RFC3339),
}, nil
Expand Down
5 changes: 3 additions & 2 deletions common/codec/types/any.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions ethermint/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ethermint

import (
"github.com/ethereum/go-ethereum/common/hexutil"
codectypes "github.com/irisnet/core-sdk-go/common/codec/types"
evmtypes "github.com/irisnet/core-sdk-go/ethermint/x/evm/types"
sdktypes "github.com/irisnet/core-sdk-go/types"
"github.com/pkg/errors"
)

type Client struct {
sdktypes.BaseClient
txConfig sdktypes.TxConfig
}

// NewClient grant NewClient
func NewClient(bc sdktypes.BaseClient, txConfig sdktypes.TxConfig) Client {
return Client{
BaseClient: bc,
txConfig: txConfig,
}
}

func (cli *Client) RegisterInterfaceTypes(registry codectypes.InterfaceRegistry) {
evmtypes.RegisterInterfaces(registry)
}

func (cli *Client) BuildEvmTx(hexData string, feePayerAddr string, evmDemon string) ([]byte, error) {
data, err := hexutil.Decode(hexData)
if err != nil {
return nil, errors.Wrap(err, "failed to decode ethereum tx hex bytes")
}
msg := &evmtypes.MsgEthereumTx{}
if err := msg.UnmarshalBinary(data); err != nil {
return nil, err
}
if err := msg.ValidateBasic(); err != nil {
return nil, err
}
if len(feePayerAddr) > 0 {
msg.SetFeePayer(feePayerAddr)
}

builder := cli.txConfig.NewTxBuilder()
tx, err := msg.BuildTx(builder, evmDemon)
if err != nil {
return nil, err
}

return cli.txConfig.TxEncoder()(tx)
}
107 changes: 107 additions & 0 deletions ethermint/clinet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package ethermint

import (
"context"
"fmt"
"testing"

"github.com/gogo/protobuf/proto"

"github.com/ethereum/go-ethereum/common"

sdk "github.com/irisnet/core-sdk-go"
"github.com/irisnet/core-sdk-go/common/crypto"
evmtypes "github.com/irisnet/core-sdk-go/ethermint/x/evm/types"
"github.com/irisnet/core-sdk-go/types"
sdktypes "github.com/irisnet/core-sdk-go/types"
"github.com/irisnet/core-sdk-go/types/store"
)

func TestError(t *testing.T) {
hex := "0AFB140A1F2F65746865726D696E742E65766D2E76312E4D7367457468657265756D547812D7140A4230783264343739623930346663313034356265613936633236303035383136343166306434306137336662396333336334313738653363326533333766386633636412C0020A2A3078414132343546336138313865303961383365623431333135613261633963303662663537616339411242307831636633623033613663663139666132626162613464663134386539646361626564656137663861356330373834306532303765356330383962653935643365124230783030303030303030303030303030303030303030303030303835316130336438306331363130613763646165366635653039616139373065323739383766636520B8A8562A423078326434373962393034666331303435626561393663323630303538313634316630643430613733666239633333633431373865336332653333376638663363643A423078633333386136663333616565393930653032336433383632643333303261666566303066313032353365656630393435333832323732613131393163646332621286030A2A30784141323435463361383138653039613833656234313331356132616339633036626635376163394112423078386265303037396335333136353931343133343463643166643061346632383431393439376639373232613364616166653362343138366636623634353765301242307830303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030124230783030303030303030303030303030303030303030303030306535346535316137613438613962343634636164383233636438346436356133346365626461626420B8A8562A423078326434373962393034666331303435626561393663323630303538313634316630643430613733666239633333633431373865336332653333376638663363643A4230786333333861366633336165653939306530323364333836326433333032616665663030663130323533656566303934353338323237326131313931636463326240011286030A2A30784141323435463361383138653039613833656234313331356132616339633036626635376163394112423078386265303037396335333136353931343133343463643166643061346632383431393439376639373232613364616166653362343138366636623634353765301242307830303030303030303030303030303030303030303030303065353465353161376134386139623436346361643832336364383464363561333463656264616264124230783030303030303030303030303030303030303030303030303964663465303561633837666535363266333562633933373939636565323738653938366662363520B8A8562A423078326434373962393034666331303435626561393663323630303538313634316630643430613733666239633333633431373865336332653333376638663363643A42307863333338613666333361656539393065303233643338363264333330326166656630306631303235336565663039343533383232373261313139316364633262400212A0020A2A30784141323435463361383138653039613833656234313331356132616339633036626635376163394112423078376632366238336666393665316632623661363832663133333835326636373938613039633436356461393539323134363063656662333834373430323439381A20000000000000000000000000000000000000000000000000000000000000000120B8A8562A423078326434373962393034666331303435626561393663323630303538313634316630643430613733666239633333633431373865336332653333376638663363643A4230786333333861366633336165653939306530323364333836326433333032616665663030663130323533656566303934353338323237326131313931636463326240031286030A2A30784141323435463361383138653039613833656234313331356132616339633036626635376163394112423078386265303037396335333136353931343133343463643166643061346632383431393439376639373232613364616166653362343138366636623634353765301242307830303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303031124230783030303030303030303030303030303030303030303030306535346535316137613438613962343634636164383233636438346436356133346365626461626420B8A8562A423078326434373962393034666331303435626561393663323630303538313634316630643430613733666239633333633431373865336332653333376638663363643A4230786333333861366633336165653939306530323364333836326433333032616665663030663130323533656566303934353338323237326131313931636463326240041286030A2A30784141323435463361383138653039613833656234313331356132616339633036626635376163394112423078386265303037396335333136353931343133343463643166643061346632383431393439376639373232613364616166653362343138366636623634353765301242307830303030303030303030303030303030303030303030303065353465353161376134386139623436346361643832336364383464363561333463656264616264124230783030303030303030303030303030303030303030303030303964663465303561633837666535363266333562633933373939636565323738653938366662363520B8A8562A423078326434373962393034666331303435626561393663323630303538313634316630643430613733666239633333633431373865336332653333376638663363643A42307863333338613666333361656539393065303233643338363264333330326166656630306631303235336565663039343533383232373261313139316364633262400512E0020A2A30786535346535316137613438413942343634434164383233636438344436354133346345624461426412423078313162363030343366313863303965323165646234366263613936643036386131393565316234303637613263663133633562316538306633343463306530361A60000000000000000000000000851A03D80C1610A7CDAE6F5E09AA970E27987FCE0000000000000000000000009DF4E05AC87FE562F35BC93799CEE278E986FB65000000000000000000000000AA245F3A818E09A83EB41315A2AC9C06BF57AC9A20B8A8562A423078326434373962393034666331303435626561393663323630303538313634316630643430613733666239633333633431373865336332653333376638663363643A4230786333333861366633336165653939306530323364333836326433333032616665663030663130323533656566303934353338323237326131313931636463326240061A20000000000000000000000000AA245F3A818E09A83EB41315A2AC9C06BF57AC9A28EB8E3E"
bytesData := common.Hex2Bytes(hex)
resp := &evmtypes.MsgEthereumTxResponse{}
err := proto.Unmarshal(bytesData, resp)
if err != nil {
t.Fatal(err)
}
//revertErr := evmtypes.NewExecErrorWithReason(resp.Ret)
//t.Log(revertErr)
}

func TestClient(t *testing.T) {

//nodeURI := "tcp://localhost:26657"
//grpcAddr := "localhost:9090"
//chainID := "wenchangchain"
nodeURI := "tcp://192.168.0.160:26657"
grpcAddr := "192.168.0.160:9090"
chainID := "wenchangchain-qa"

bech32AddressPrefix := sdktypes.AddrPrefixCfg{
AccountAddr: "iaa",
ValidatorAddr: "iva",
ConsensusAddr: "ica",
AccountPub: "iap",
ValidatorPub: "ivp",
ConsensusPub: "icp",
}
options := []sdktypes.Option{
sdktypes.KeyDAOOption(store.NewMemory(nil)),
sdktypes.TimeoutOption(10),
sdktypes.KeyManagerOption(crypto.NewKeyManager()),
sdktypes.Bech32AddressPrefixOption(&bech32AddressPrefix),
sdktypes.BIP44PathOption(""),
}
cfg, err := types.NewClientConfig(nodeURI, grpcAddr, chainID, options...)
if err != nil {
panic(err)
}

sdkClient := sdk.NewClient(cfg)

cli := NewClient(sdkClient.BaseClient, sdkClient.EncodingConfig().TxConfig)
cli.RegisterInterfaceTypes(sdkClient.EncodingConfig().InterfaceRegistry)

resp1, err := sdkClient.QueryTx("51A728BAEA19FE27DC0F221B0F04C2A75D55C2D723F30FA67391AEDBF65F8049")
if err != nil {
panic(err)
}
txResponse, err := evmtypes.DecodeTxResponse(resp1.Result.Data)
if err != nil {
return
}

//if txResponse.Ret == nil {
// fmt.Println("no err")
// return
//}

revetErr := evmtypes.NewExecErrorWithReason(txResponse.Ret)
fmt.Println(revetErr.ErrorData())
fmt.Println(revetErr.ErrorCode())
fmt.Println(revetErr.Error())
if err != nil {
return
}
t.Log(revetErr)

return

txData := "0xf901470801831e8480941a6640c32b7e6413e839e9dfdb53970ee809b7fb80b8e4990711900000000000000000000000005892e7eeaea5ba624f5ba2900dbab8d2ea36d62b000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000001a687474703a2f2f6578616d706c652e636f6d2f746f6b656e2f35000000000000000000000000000000000000000000000000000000000000000000000000000974657374686173683600000000000000000000000000000000000000000000008209b2a050e1d159818efbae1c536b2c866920ac3c8c3720b9194b02c5ecea7717f85d2da05eabeb96aad81f432e1d7d3f84e6d38ce939e7a2d1f0bee3af2f20f507c91b96"
feePayer := "0x4579DB44FD3A6F645194058914E0A8D5E8F20DB8"
evmDenom := "ugas"
rawTx, err := cli.BuildEvmTx(txData, feePayer, evmDenom)
if err != nil {
t.Log(err)
return
}
priv := "-----BEGIN TENDERMINT PRIVATE KEY-----\nkdf: bcrypt\nsalt: 7FDD1E16A62E8B6AF914ECADCFF815FD\ntype: eth_secp256k1\n\nDHoFvjH6+FNcAj8be7oGXnpfQWlSnPG9o/yLezuDmnQWtkPcTqaAfizPG18PU3hq\nYWWxhh1a+/MNxF9u85h3Tv3vq/a7EebOsBf3AXI=\n=PI4S\n-----END TENDERMINT PRIVATE KEY-----"
sdkClient.Key.Import("test01", "12345678", priv)
resp, err := cli.BroadcastTxSync(context.Background(), rawTx)
if err != nil {
t.Log(err)
return
}
t.Log(resp)
}
5 changes: 5 additions & 0 deletions ethermint/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ethermint

type IClient interface {
BuildEvmTx(hexData string, feePayerAddr string, evmDemon string) error
}
39 changes: 39 additions & 0 deletions ethermint/types/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package types

import (
"bytes"
"math"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"
)

// IsEmptyHash returns true if the hash corresponds to an empty ethereum hex hash.
func IsEmptyHash(hash string) bool {
return bytes.Equal(common.HexToHash(hash).Bytes(), common.Hash{}.Bytes())
}

// IsZeroAddress returns true if the address corresponds to an empty ethereum hex address.
func IsZeroAddress(address string) bool {
return bytes.Equal(common.HexToAddress(address).Bytes(), common.Address{}.Bytes())
}

// ValidateAddress returns an error if the provided string is either not a hex formatted string address
func ValidateAddress(address string) error {
if !common.IsHexAddress(address) {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidAddress, "address '%s' is not a valid ethereum hex address",
address,
)
}
return nil
}

// SafeInt64 checks for overflows while casting a uint64 to int64 value.
func SafeInt64(value uint64) (int64, error) {
if value > uint64(math.MaxInt64) {
return 0, sdkerrors.Wrapf(sdkerrors.ErrInvalidHeight, "uint64 value %v cannot exceed %v", value, int64(math.MaxInt64))
}

return int64(value), nil
}
55 changes: 55 additions & 0 deletions ethermint/x/evm/types/access_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package types

import (
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
)

// AccessList is an EIP-2930 access list that represents the slice of
// the protobuf AccessTuples.
type AccessList []AccessTuple

// NewAccessList creates a new protobuf-compatible AccessList from an ethereum
// core AccessList type
func NewAccessList(ethAccessList *ethtypes.AccessList) AccessList {
if ethAccessList == nil {
return nil
}

al := AccessList{}
for _, tuple := range *ethAccessList {
storageKeys := make([]string, len(tuple.StorageKeys))

for i := range tuple.StorageKeys {
storageKeys[i] = tuple.StorageKeys[i].String()
}

al = append(al, AccessTuple{
Address: tuple.Address.String(),
StorageKeys: storageKeys,
})
}

return al
}

// ToEthAccessList is an utility function to convert the protobuf compatible
// AccessList to eth core AccessList from go-ethereum
func (al AccessList) ToEthAccessList() *ethtypes.AccessList {
var ethAccessList ethtypes.AccessList

for _, tuple := range al {
storageKeys := make([]common.Hash, len(tuple.StorageKeys))

for i := range tuple.StorageKeys {
storageKeys[i] = common.HexToHash(tuple.StorageKeys[i])
}

ethAccessList = append(ethAccessList, ethtypes.AccessTuple{
Address: common.HexToAddress(tuple.Address),
StorageKeys: storageKeys,
})
}

return &ethAccessList
}
Loading