-
Notifications
You must be signed in to change notification settings - Fork 659
/
test_signer.go
58 lines (45 loc) · 1.42 KB
/
test_signer.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package warp
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
)
// SignerTests is a list of all signer tests
var SignerTests = []func(t *testing.T, s Signer, sk *bls.SecretKey, chainID ids.ID){
TestSignerWrongChainID,
TestSignerVerifies,
}
// Test that using a random SourceChainID results in an error
func TestSignerWrongChainID(t *testing.T, s Signer, _ *bls.SecretKey, _ ids.ID) {
require := require.New(t)
msg, err := NewUnsignedMessage(
ids.GenerateTestID(),
ids.GenerateTestID(),
[]byte("payload"),
)
require.NoError(err)
_, err = s.Sign(msg)
// TODO: require error to be errWrongSourceChainID
require.Error(err) //nolint:forbidigo // currently returns grpc errors too
}
// Test that a signature generated with the signer verifies correctly
func TestSignerVerifies(t *testing.T, s Signer, sk *bls.SecretKey, chainID ids.ID) {
require := require.New(t)
msg, err := NewUnsignedMessage(
chainID,
ids.GenerateTestID(),
[]byte("payload"),
)
require.NoError(err)
sigBytes, err := s.Sign(msg)
require.NoError(err)
sig, err := bls.SignatureFromBytes(sigBytes)
require.NoError(err)
pk := bls.PublicFromSecretKey(sk)
msgBytes := msg.Bytes()
valid := bls.Verify(pk, sig, msgBytes)
require.True(valid)
}