2023-01-16 15:42:49 +00:00
|
|
|
package consensus
|
2023-01-13 19:11:13 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types/ethtypes"
|
|
|
|
"github.com/filecoin-project/lotus/lib/sigs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2024-06-05 05:25:50 +00:00
|
|
|
signatureType := msg.Signature.Type
|
|
|
|
signatureCopy := msg.Signature
|
2023-01-13 19:11:13 +00:00
|
|
|
|
2024-06-05 05:25:50 +00:00
|
|
|
switch signatureType {
|
2023-01-13 19:11:13 +00:00
|
|
|
case crypto.SigTypeDelegated:
|
2024-06-05 05:25:50 +00:00
|
|
|
signatureCopy.Data = make([]byte, len(msg.Signature.Data))
|
|
|
|
copy(signatureCopy.Data, msg.Signature.Data)
|
|
|
|
ethTx, err := ethtypes.EthTransactionFromSignedFilecoinMessage(msg)
|
2023-01-13 19:11:13 +00:00
|
|
|
if err != nil {
|
2024-06-05 05:25:50 +00:00
|
|
|
return xerrors.Errorf("failed to reconstruct Ethereum transaction: %w", err)
|
2023-01-13 19:11:13 +00:00
|
|
|
}
|
2024-06-05 05:25:50 +00:00
|
|
|
|
|
|
|
filecoinMsg, err := ethTx.ToUnsignedFilecoinMessage(msg.Message.From)
|
2023-01-14 00:36:36 +00:00
|
|
|
if err != nil {
|
2024-06-05 05:25:50 +00:00
|
|
|
return xerrors.Errorf("failed to reconstruct Filecoin message: %w", err)
|
2023-01-14 00:36:36 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 05:25:50 +00:00
|
|
|
if !msg.Message.Equals(filecoinMsg) {
|
|
|
|
return xerrors.New("Ethereum transaction roundtrip mismatch")
|
2023-01-14 00:36:36 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 05:25:50 +00:00
|
|
|
rlpEncodedMsg, err := ethTx.ToRlpUnsignedMsg()
|
2023-01-13 19:11:13 +00:00
|
|
|
if err != nil {
|
2024-06-05 05:25:50 +00:00
|
|
|
return xerrors.Errorf("failed to encode RLP message: %w", err)
|
2023-01-13 19:11:13 +00:00
|
|
|
}
|
2023-01-14 00:36:36 +00:00
|
|
|
digest = rlpEncodedMsg
|
2024-06-05 05:25:50 +00:00
|
|
|
signatureCopy.Data, err = ethTx.ToVerifiableSignature(signatureCopy.Data)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to verify signature: %w", err)
|
|
|
|
}
|
2023-01-13 19:11:13 +00:00
|
|
|
default:
|
|
|
|
digest = msg.Message.Cid().Bytes()
|
|
|
|
}
|
|
|
|
|
2024-06-05 05:25:50 +00:00
|
|
|
if err := sigs.Verify(&signatureCopy, signer, digest); err != nil {
|
|
|
|
return xerrors.Errorf("invalid signature for message %s (type %d): %w", msg.Cid(), signatureType, err)
|
2023-01-13 19:11:13 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|