@@ -18,11 +18,13 @@ package types
1818
1919import (
2020 "errors"
21+ "fmt"
2122 "math/big"
2223 "testing"
2324
2425 "github.com/ethereum/go-ethereum/common"
2526 "github.com/ethereum/go-ethereum/crypto"
27+ "github.com/ethereum/go-ethereum/params"
2628 "github.com/ethereum/go-ethereum/rlp"
2729)
2830
@@ -136,3 +138,53 @@ func TestChainId(t *testing.T) {
136138 t .Error ("expected no error" )
137139 }
138140}
141+
142+ type nilSigner struct {
143+ v , r , s * big.Int
144+ Signer
145+ }
146+
147+ func (ns * nilSigner ) SignatureValues (tx * Transaction , sig []byte ) (r , s , v * big.Int , err error ) {
148+ return ns .v , ns .r , ns .s , nil
149+ }
150+
151+ // TestNilSigner ensures a faulty Signer implementation does not result in nil signature values or panics.
152+ func TestNilSigner (t * testing.T ) {
153+ key , _ := crypto .GenerateKey ()
154+ innerSigner := LatestSignerForChainID (big .NewInt (1 ))
155+ for i , signer := range []Signer {
156+ & nilSigner {v : nil , r : nil , s : nil , Signer : innerSigner },
157+ & nilSigner {v : big .NewInt (1 ), r : big .NewInt (1 ), s : nil , Signer : innerSigner },
158+ & nilSigner {v : big .NewInt (1 ), r : nil , s : big .NewInt (1 ), Signer : innerSigner },
159+ & nilSigner {v : nil , r : big .NewInt (1 ), s : big .NewInt (1 ), Signer : innerSigner },
160+ } {
161+ t .Run (fmt .Sprintf ("signer_%d" , i ), func (t * testing.T ) {
162+ t .Run ("legacy" , func (t * testing.T ) {
163+ legacyTx := createTestLegacyTxInner ()
164+ _ , err := SignNewTx (key , signer , legacyTx )
165+ if ! errors .Is (err , ErrInvalidSig ) {
166+ t .Fatal ("expected signature values error, no nil result or panic" )
167+ }
168+ })
169+ // test Blob tx specifically, since the signature value types changed
170+ t .Run ("blobtx" , func (t * testing.T ) {
171+ blobtx := createEmptyBlobTxInner (false )
172+ _ , err := SignNewTx (key , signer , blobtx )
173+ if ! errors .Is (err , ErrInvalidSig ) {
174+ t .Fatal ("expected signature values error, no nil result or panic" )
175+ }
176+ })
177+ })
178+ }
179+ }
180+
181+ func createTestLegacyTxInner () * LegacyTx {
182+ return & LegacyTx {
183+ Nonce : uint64 (0 ),
184+ To : nil ,
185+ Value : big .NewInt (0 ),
186+ Gas : params .TxGas ,
187+ GasPrice : big .NewInt (params .GWei ),
188+ Data : nil ,
189+ }
190+ }
0 commit comments