-
Notifications
You must be signed in to change notification settings - Fork 38
/
utils_test.go
59 lines (46 loc) · 1.75 KB
/
utils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package core
import (
"math/big"
"testing"
"github.com/ten-protocol/go-ten/integration/datagenerator"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)
const testChainID = 1234
func TestValidSignatureVerifies(t *testing.T) {
tx := datagenerator.CreateL2Tx()
privateKey, _ := crypto.GenerateKey()
signer := types.NewLondonSigner(big.NewInt(testChainID))
signedTx, _ := types.SignTx(tx, signer, privateKey)
if err := VerifySignature(testChainID, signedTx); err != nil {
t.Errorf("validly-signed transaction did not pass verification: %v", err)
}
}
func TestUnsignedTxDoesNotVerify(t *testing.T) {
tx := datagenerator.CreateL2Tx()
if err := VerifySignature(testChainID, tx); err == nil {
t.Errorf("transaction was not signed but verified anyway: %v", err)
}
}
func TestModifiedTxDoesNotVerify(t *testing.T) {
txData := datagenerator.CreateL2TxData()
tx := types.NewTx(txData)
privateKey, _ := crypto.GenerateKey()
signer := types.NewLondonSigner(big.NewInt(testChainID))
_, _ = types.SignTx(tx, signer, privateKey)
// We create a new transaction around the transaction data, breaking the signature.
modifiedTx := types.NewTx(txData)
if err := VerifySignature(testChainID, modifiedTx); err == nil {
t.Errorf("transaction was modified after signature but verified anyway: %v", err)
}
}
func TestIncorrectSignerDoesNotVerify(t *testing.T) {
tx := datagenerator.CreateL2Tx()
privateKey, _ := crypto.GenerateKey()
incorrectChainID := int64(testChainID + 1)
signer := types.NewLondonSigner(big.NewInt(incorrectChainID))
signedTx, _ := types.SignTx(tx, signer, privateKey)
if err := VerifySignature(testChainID, signedTx); err == nil {
t.Errorf("transaction used incorrect signer but verified anyway: %v", err)
}
}