2019-09-15 16:12:59 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2020-05-29 15:50:22 +00:00
|
|
|
"fmt"
|
2020-04-23 15:49:25 +00:00
|
|
|
"math/big"
|
|
|
|
|
2019-09-26 15:36:23 +00:00
|
|
|
"github.com/cosmos/ethermint/utils"
|
2019-09-15 16:12:59 +00:00
|
|
|
|
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
// TxData implements the Ethereum transaction data structure. It is used
|
|
|
|
// solely as intended in Ethereum abiding by the protocol.
|
|
|
|
type TxData struct {
|
|
|
|
AccountNonce uint64 `json:"nonce"`
|
|
|
|
Price *big.Int `json:"gasPrice"`
|
|
|
|
GasLimit uint64 `json:"gas"`
|
|
|
|
Recipient *ethcmn.Address `json:"to" rlp:"nil"` // nil means contract creation
|
|
|
|
Amount *big.Int `json:"value"`
|
|
|
|
Payload []byte `json:"input"`
|
|
|
|
|
|
|
|
// signature values
|
|
|
|
V *big.Int `json:"v"`
|
|
|
|
R *big.Int `json:"r"`
|
|
|
|
S *big.Int `json:"s"`
|
|
|
|
|
|
|
|
// hash is only used when marshaling to JSON
|
|
|
|
Hash *ethcmn.Hash `json:"hash" rlp:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// encodableTxData implements the Ethereum transaction data structure. It is used
|
2019-09-15 16:12:59 +00:00
|
|
|
// solely as intended in Ethereum abiding by the protocol.
|
2020-04-23 15:49:25 +00:00
|
|
|
type encodableTxData struct {
|
2019-09-15 16:12:59 +00:00
|
|
|
AccountNonce uint64 `json:"nonce"`
|
|
|
|
Price string `json:"gasPrice"`
|
|
|
|
GasLimit uint64 `json:"gas"`
|
|
|
|
Recipient *ethcmn.Address `json:"to" rlp:"nil"` // nil means contract creation
|
|
|
|
Amount string `json:"value"`
|
|
|
|
Payload []byte `json:"input"`
|
|
|
|
|
|
|
|
// signature values
|
|
|
|
V string `json:"v"`
|
|
|
|
R string `json:"r"`
|
|
|
|
S string `json:"s"`
|
|
|
|
|
|
|
|
// hash is only used when marshaling to JSON
|
|
|
|
Hash *ethcmn.Hash `json:"hash" rlp:"-"`
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:50:22 +00:00
|
|
|
func (td TxData) String() string {
|
|
|
|
if td.Recipient != nil {
|
|
|
|
return fmt.Sprintf("nonce=%d price=%s gasLimit=%d recipient=%s amount=%s data=0x%x v=%s r=%s s=%s",
|
|
|
|
td.AccountNonce, td.Price, td.GasLimit, td.Recipient.Hex(), td.Amount, td.Payload, td.V, td.R, td.S)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("nonce=%d price=%s gasLimit=%d recipient=nil amount=%s data=0x%x v=%s r=%s s=%s",
|
|
|
|
td.AccountNonce, td.Price, td.GasLimit, td.Amount, td.Payload, td.V, td.R, td.S)
|
|
|
|
}
|
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
// MarshalAmino defines custom encoding scheme for TxData
|
|
|
|
func (td TxData) MarshalAmino() ([]byte, error) {
|
|
|
|
gasPrice, err := utils.MarshalBigInt(td.Price)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-15 16:12:59 +00:00
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
amount, err := utils.MarshalBigInt(td.Amount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-15 16:12:59 +00:00
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
v, err := utils.MarshalBigInt(td.V)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := utils.MarshalBigInt(td.R)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := utils.MarshalBigInt(td.S)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
e := encodableTxData{
|
2019-09-15 16:12:59 +00:00
|
|
|
AccountNonce: td.AccountNonce,
|
2020-04-23 15:49:25 +00:00
|
|
|
Price: gasPrice,
|
2019-09-15 16:12:59 +00:00
|
|
|
GasLimit: td.GasLimit,
|
|
|
|
Recipient: td.Recipient,
|
2020-04-23 15:49:25 +00:00
|
|
|
Amount: amount,
|
2019-09-15 16:12:59 +00:00
|
|
|
Payload: td.Payload,
|
2020-04-23 15:49:25 +00:00
|
|
|
V: v,
|
|
|
|
R: r,
|
|
|
|
S: s,
|
|
|
|
Hash: td.Hash,
|
2019-09-15 16:12:59 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
return ModuleCdc.MarshalBinaryBare(e)
|
2019-09-15 16:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalAmino defines custom decoding scheme for TxData
|
2020-04-23 15:49:25 +00:00
|
|
|
func (td *TxData) UnmarshalAmino(data []byte) error {
|
|
|
|
var e encodableTxData
|
|
|
|
err := ModuleCdc.UnmarshalBinaryBare(data, &e)
|
2019-09-15 16:12:59 +00:00
|
|
|
if err != nil {
|
2020-03-16 22:53:24 +00:00
|
|
|
return err
|
2019-09-15 16:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
td.AccountNonce = e.AccountNonce
|
|
|
|
td.GasLimit = e.GasLimit
|
|
|
|
td.Recipient = e.Recipient
|
|
|
|
td.Payload = e.Payload
|
|
|
|
td.Hash = e.Hash
|
|
|
|
|
2019-09-26 15:36:23 +00:00
|
|
|
price, err := utils.UnmarshalBigInt(e.Price)
|
2019-09-15 16:12:59 +00:00
|
|
|
if err != nil {
|
2020-03-16 22:53:24 +00:00
|
|
|
return err
|
2019-09-15 16:12:59 +00:00
|
|
|
}
|
2020-03-16 22:53:24 +00:00
|
|
|
|
2019-09-15 16:12:59 +00:00
|
|
|
if td.Price != nil {
|
|
|
|
td.Price.Set(price)
|
|
|
|
} else {
|
|
|
|
td.Price = price
|
|
|
|
}
|
|
|
|
|
2019-09-26 15:36:23 +00:00
|
|
|
amt, err := utils.UnmarshalBigInt(e.Amount)
|
2019-09-15 16:12:59 +00:00
|
|
|
if err != nil {
|
2020-03-16 22:53:24 +00:00
|
|
|
return err
|
2019-09-15 16:12:59 +00:00
|
|
|
}
|
2020-03-16 22:53:24 +00:00
|
|
|
|
2019-09-15 16:12:59 +00:00
|
|
|
if td.Amount != nil {
|
|
|
|
td.Amount.Set(amt)
|
|
|
|
} else {
|
|
|
|
td.Amount = amt
|
|
|
|
}
|
|
|
|
|
2019-09-26 15:36:23 +00:00
|
|
|
v, err := utils.UnmarshalBigInt(e.V)
|
2019-09-15 16:12:59 +00:00
|
|
|
if err != nil {
|
2020-03-16 22:53:24 +00:00
|
|
|
return err
|
2019-09-15 16:12:59 +00:00
|
|
|
}
|
2020-03-16 22:53:24 +00:00
|
|
|
|
2019-09-15 16:12:59 +00:00
|
|
|
if td.V != nil {
|
|
|
|
td.V.Set(v)
|
|
|
|
} else {
|
|
|
|
td.V = v
|
|
|
|
}
|
|
|
|
|
2019-09-26 15:36:23 +00:00
|
|
|
r, err := utils.UnmarshalBigInt(e.R)
|
2019-09-15 16:12:59 +00:00
|
|
|
if err != nil {
|
2020-03-16 22:53:24 +00:00
|
|
|
return err
|
2019-09-15 16:12:59 +00:00
|
|
|
}
|
2020-03-16 22:53:24 +00:00
|
|
|
|
2019-09-15 16:12:59 +00:00
|
|
|
if td.R != nil {
|
|
|
|
td.R.Set(r)
|
|
|
|
} else {
|
|
|
|
td.R = r
|
|
|
|
}
|
|
|
|
|
2019-09-26 15:36:23 +00:00
|
|
|
s, err := utils.UnmarshalBigInt(e.S)
|
2019-09-15 16:12:59 +00:00
|
|
|
if err != nil {
|
2020-03-16 22:53:24 +00:00
|
|
|
return err
|
2019-09-15 16:12:59 +00:00
|
|
|
}
|
2020-03-16 22:53:24 +00:00
|
|
|
|
2019-09-15 16:12:59 +00:00
|
|
|
if td.S != nil {
|
|
|
|
td.S.Set(s)
|
|
|
|
} else {
|
|
|
|
td.S = s
|
|
|
|
}
|
|
|
|
|
2020-03-16 22:53:24 +00:00
|
|
|
return nil
|
2019-09-15 16:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Implement JSON marshaling/ unmarshaling for this type
|
|
|
|
|
|
|
|
// TODO: Implement YAML marshaling/ unmarshaling for this type
|