-
Notifications
You must be signed in to change notification settings - Fork 464
/
signatures.go
60 lines (51 loc) · 1.84 KB
/
signatures.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
60
package chain
import (
"fmt"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/venus/pkg/crypto"
"github.com/filecoin-project/venus/venus-shared/types"
)
// AuthenticateMessage authenticates the message by verifying that the supplied
// SignedMessage was signed by the indicated Address, computing the correct
// signature payload depending on the signature type. The supplied Address type
// must be recognized by the registered verifier for the signature type.
func AuthenticateMessage(msg *types.SignedMessage, signer address.Address) error {
var digest []byte
typ := msg.Signature.Type
switch typ {
case crypto.SigTypeDelegated:
txArgs, err := types.EthTxArgsFromUnsignedEthMessage(&msg.Message)
if err != nil {
return fmt.Errorf("failed to reconstruct eth transaction: %w", err)
}
roundTripMsg, err := txArgs.ToUnsignedMessage(msg.Message.From)
if err != nil {
return fmt.Errorf("failed to reconstruct filecoin msg: %w", err)
}
if !msg.Message.Equals(roundTripMsg) {
return fmt.Errorf("ethereum tx failed to roundtrip")
}
rlpEncodedMsg, err := txArgs.ToRlpUnsignedMsg()
if err != nil {
return fmt.Errorf("failed to repack eth rlp message: %w", err)
}
digest = rlpEncodedMsg
default:
digest = msg.Message.Cid().Bytes()
}
if err := crypto.Verify(&msg.Signature, signer, digest); err != nil {
return fmt.Errorf("message %s has invalid signature (type %d): %w", msg.Cid(), typ, err)
}
return nil
}
// IsValidSecpkSigType checks that a signature type is valid for the network
// version, for a "secpk" message.
func IsValidSecpkSigType(nv network.Version, typ crypto.SigType) bool {
switch {
case nv < network.Version18:
return typ == crypto.SigTypeSecp256k1
default:
return typ == crypto.SigTypeSecp256k1 || typ == crypto.SigTypeDelegated
}
}