core/types: make tx signature values optional in JSON (#17742)

This commit is contained in:
reinerRubin 2018-09-29 23:47:59 +03:00 committed by Felix Lange
parent 3d782bc727
commit 86ec213076
2 changed files with 23 additions and 12 deletions

View File

@ -153,16 +153,21 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error {
if err := dec.UnmarshalJSON(input); err != nil { if err := dec.UnmarshalJSON(input); err != nil {
return err return err
} }
var V byte
if isProtectedV(dec.V) { withSignature := dec.V.Sign() != 0 || dec.R.Sign() != 0 || dec.S.Sign() != 0
chainID := deriveChainId(dec.V).Uint64() if withSignature {
V = byte(dec.V.Uint64() - 35 - 2*chainID) var V byte
} else { if isProtectedV(dec.V) {
V = byte(dec.V.Uint64() - 27) chainID := deriveChainId(dec.V).Uint64()
} V = byte(dec.V.Uint64() - 35 - 2*chainID)
if !crypto.ValidateSignatureValues(V, dec.R, dec.S, false) { } else {
return ErrInvalidSig V = byte(dec.V.Uint64() - 27)
}
if !crypto.ValidateSignatureValues(V, dec.R, dec.S, false) {
return ErrInvalidSig
}
} }
*tx = Transaction{data: dec} *tx = Transaction{data: dec}
return nil return nil
} }

View File

@ -185,6 +185,7 @@ func TestTransactionJSON(t *testing.T) {
} }
signer := NewEIP155Signer(common.Big1) signer := NewEIP155Signer(common.Big1)
transactions := make([]*Transaction, 0, 50)
for i := uint64(0); i < 25; i++ { for i := uint64(0); i < 25; i++ {
var tx *Transaction var tx *Transaction
switch i % 2 { switch i % 2 {
@ -193,20 +194,25 @@ func TestTransactionJSON(t *testing.T) {
case 1: case 1:
tx = NewContractCreation(i, common.Big0, 1, common.Big2, []byte("abcdef")) tx = NewContractCreation(i, common.Big0, 1, common.Big2, []byte("abcdef"))
} }
transactions = append(transactions, tx)
tx, err := SignTx(tx, signer, key) signedTx, err := SignTx(tx, signer, key)
if err != nil { if err != nil {
t.Fatalf("could not sign transaction: %v", err) t.Fatalf("could not sign transaction: %v", err)
} }
transactions = append(transactions, signedTx)
}
for _, tx := range transactions {
data, err := json.Marshal(tx) data, err := json.Marshal(tx)
if err != nil { if err != nil {
t.Errorf("json.Marshal failed: %v", err) t.Fatalf("json.Marshal failed: %v", err)
} }
var parsedTx *Transaction var parsedTx *Transaction
if err := json.Unmarshal(data, &parsedTx); err != nil { if err := json.Unmarshal(data, &parsedTx); err != nil {
t.Errorf("json.Unmarshal failed: %v", err) t.Fatalf("json.Unmarshal failed: %v", err)
} }
// compare nonce, price, gaslimit, recipient, amount, payload, V, R, S // compare nonce, price, gaslimit, recipient, amount, payload, V, R, S