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

core/types: add derived chain ID to LegacyTx JSON encoding #27452

Merged
merged 5 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions core/types/transaction_marshalling.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func (tx *Transaction) MarshalJSON() ([]byte, error) {
// Other fields are set conditionally depending on tx type.
switch itx := tx.inner.(type) {
case *LegacyTx:
// Only derive the chain ID if tx is signed and post EIP-155
if (itx.V.Sign() != 0 || itx.R.Sign() != 0 || itx.S.Sign() != 0) && tx.ChainId().Sign() != 0 {
lightclient marked this conversation as resolved.
Show resolved Hide resolved
enc.ChainID = (*hexutil.Big)(tx.ChainId())
}
enc.Nonce = (*hexutil.Uint64)(&itx.Nonce)
enc.To = tx.To()
enc.Gas = (*hexutil.Uint64)(&itx.Gas)
Expand Down
5 changes: 5 additions & 0 deletions core/types/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"math/big"
"math/rand"
"reflect"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -501,6 +502,10 @@ func encodeDecodeJSON(tx *Transaction) (*Transaction, error) {
if err := json.Unmarshal(data, &parsedTx); err != nil {
return nil, fmt.Errorf("json decoding failed: %v", err)
}

if !strings.Contains(string(data), "chainId") {
lightclient marked this conversation as resolved.
Show resolved Hide resolved
return nil, fmt.Errorf("json encoding does not contain chainId: %v", string(data))
}
return parsedTx, nil
}

Expand Down