github.com/elastos/Elastos.ELA.SideChain.ETH@v0.2.2/chainbridge-core/chains/evm/evmtransaction/evm-tx.go (about)

     1  // Copyright 2020 ChainSafe Systems
     2  // SPDX-License-Identifier: LGPL-3.0-only
     3  
     4  package evmtransaction
     5  
     6  import (
     7  	"crypto/ecdsa"
     8  	"math/big"
     9  
    10  	"github.com/elastos/Elastos.ELA.SideChain.ESC/accounts/abi/bind"
    11  	"github.com/elastos/Elastos.ELA.SideChain.ESC/common"
    12  	"github.com/elastos/Elastos.ELA.SideChain.ESC/core/types"
    13  	"github.com/elastos/Elastos.ELA.SideChain.ESC/crypto"
    14  	"github.com/elastos/Elastos.ELA.SideChain.ESC/rlp"
    15  )
    16  
    17  type TX struct {
    18  	tx *types.Transaction
    19  }
    20  
    21  // RawWithSignature mostly copies WithSignature interface of type.Transaction from go-ethereum,
    22  // but return raw byte representation of transaction to be compatible and interchangeable between different go-ethereum forks
    23  // WithSignature returns a new transaction with the given signature.
    24  // This signature needs to be in the [R || S || V] format where V is 0 or 1.
    25  func (a *TX) RawWithSignature(key *ecdsa.PrivateKey, chainID *big.Int) ([]byte, error) {
    26  	opts := bind.NewKeyedTransactor(key)
    27  	signer := types.NewEIP155Signer(chainID)
    28  	tx, err := opts.Signer(signer, crypto.PubkeyToAddress(key.PublicKey), a.tx)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	a.tx = tx
    33  	rawTX, err := rlp.EncodeToBytes(tx)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	return rawTX, nil
    38  }
    39  
    40  func NewTransaction(nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *TX {
    41  	tx := types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, data)
    42  	return &TX{tx: tx}
    43  }
    44  
    45  func (a *TX) Hash() common.Hash {
    46  	return a.tx.Hash()
    47  }