Skip to content

Commit 86ec213

Browse files
reinerRubinfjl
authored andcommitted
core/types: make tx signature values optional in JSON (#17742)
1 parent 3d782bc commit 86ec213

2 files changed

Lines changed: 23 additions & 12 deletions

File tree

‎core/types/transaction.go‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,21 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error {
153153
if err := dec.UnmarshalJSON(input); err != nil {
154154
return err
155155
}
156-
var V byte
157-
if isProtectedV(dec.V) {
158-
chainID := deriveChainId(dec.V).Uint64()
159-
V = byte(dec.V.Uint64() - 35 - 2*chainID)
160-
} else {
161-
V = byte(dec.V.Uint64() - 27)
162-
}
163-
if !crypto.ValidateSignatureValues(V, dec.R, dec.S, false) {
164-
return ErrInvalidSig
156+
157+
withSignature := dec.V.Sign() != 0 || dec.R.Sign() != 0 || dec.S.Sign() != 0
158+
if withSignature {
159+
var V byte
160+
if isProtectedV(dec.V) {
161+
chainID := deriveChainId(dec.V).Uint64()
162+
V = byte(dec.V.Uint64() - 35 - 2*chainID)
163+
} else {
164+
V = byte(dec.V.Uint64() - 27)
165+
}
166+
if !crypto.ValidateSignatureValues(V, dec.R, dec.S, false) {
167+
return ErrInvalidSig
168+
}
165169
}
170+
166171
*tx = Transaction{data: dec}
167172
return nil
168173
}

‎core/types/transaction_test.go‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ func TestTransactionJSON(t *testing.T) {
185185
}
186186
signer := NewEIP155Signer(common.Big1)
187187

188+
transactions := make([]*Transaction, 0, 50)
188189
for i := uint64(0); i < 25; i++ {
189190
var tx *Transaction
190191
switch i % 2 {
@@ -193,20 +194,25 @@ func TestTransactionJSON(t *testing.T) {
193194
case 1:
194195
tx = NewContractCreation(i, common.Big0, 1, common.Big2, []byte("abcdef"))
195196
}
197+
transactions = append(transactions, tx)
196198

197-
tx, err := SignTx(tx, signer, key)
199+
signedTx, err := SignTx(tx, signer, key)
198200
if err != nil {
199201
t.Fatalf("could not sign transaction: %v", err)
200202
}
201203

204+
transactions = append(transactions, signedTx)
205+
}
206+
207+
for _, tx := range transactions {
202208
data, err := json.Marshal(tx)
203209
if err != nil {
204-
t.Errorf("json.Marshal failed: %v", err)
210+
t.Fatalf("json.Marshal failed: %v", err)
205211
}
206212

207213
var parsedTx *Transaction
208214
if err := json.Unmarshal(data, &parsedTx); err != nil {
209-
t.Errorf("json.Unmarshal failed: %v", err)
215+
t.Fatalf("json.Unmarshal failed: %v", err)
210216
}
211217

212218
// compare nonce, price, gaslimit, recipient, amount, payload, V, R, S

0 commit comments

Comments
 (0)