consolidate signature validation logic.

This commit is contained in:
Raúl Kripalani 2023-01-10 22:30:46 +00:00 committed by raulk
parent ac38c9776c
commit a8c33de80b
3 changed files with 50 additions and 34 deletions

View File

@ -40,7 +40,6 @@ import (
"github.com/filecoin-project/lotus/chain/stmgr"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
"github.com/filecoin-project/lotus/chain/vm"
"github.com/filecoin-project/lotus/lib/async"
"github.com/filecoin-project/lotus/lib/sigs"
@ -578,10 +577,15 @@ func (filec *FilecoinEC) checkBlockMessages(ctx context.Context, b *types.FullBl
smArr := blockadt.MakeEmptyArray(tmpstore)
for i, m := range b.SecpkMessages {
if filec.sm.GetNetworkVersion(ctx, b.Header.Height) >= network.Version14 {
if m.Signature.Type != crypto.SigTypeSecp256k1 && m.Signature.Type != crypto.SigTypeDelegated {
switch nv := filec.sm.GetNetworkVersion(ctx, b.Header.Height); {
case nv >= network.Version14 && nv < network.Version18:
if typ := m.Signature.Type; typ != crypto.SigTypeSecp256k1 {
return xerrors.Errorf("block had invalid secpk message at index %d: %w", i, err)
}
case nv >= network.Version18:
if typ := m.Signature.Type; typ != crypto.SigTypeSecp256k1 && typ != crypto.SigTypeDelegated {
return xerrors.Errorf("block had invalid signed message at index %d: %w", i, err)
}
}
if err := checkMsg(m); err != nil {
@ -595,21 +599,8 @@ func (filec *FilecoinEC) checkBlockMessages(ctx context.Context, b *types.FullBl
return xerrors.Errorf("failed to resolve key addr: %w", err)
}
digest := m.Message.Cid().Bytes()
if m.Signature.Type == crypto.SigTypeDelegated {
txArgs, err := ethtypes.NewEthTxArgsFromMessage(&m.Message)
if err != nil {
return err
}
msg, err := txArgs.ToRlpUnsignedMsg()
if err != nil {
return err
}
digest = msg
}
if err := sigs.Verify(&m.Signature, kaddr, digest); err != nil {
return xerrors.Errorf("secpk message %s has invalid signature: %w", m.Cid(), err)
if err := chain.AuthenticateMessage(m, kaddr); err != nil {
return xerrors.Errorf("failed to validate signature: %w", err)
}
c, err := store.PutMessage(ctx, tmpbs, m)

View File

@ -11,6 +11,7 @@ import (
"sync"
"time"
"github.com/filecoin-project/lotus/chain"
"github.com/hashicorp/go-multierror"
lru "github.com/hashicorp/golang-lru"
"github.com/ipfs/go-cid"
@ -37,10 +38,8 @@ import (
"github.com/filecoin-project/lotus/chain/stmgr"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
"github.com/filecoin-project/lotus/chain/vm"
"github.com/filecoin-project/lotus/journal"
"github.com/filecoin-project/lotus/lib/sigs"
"github.com/filecoin-project/lotus/metrics"
"github.com/filecoin-project/lotus/node/modules/dtypes"
)
@ -795,20 +794,8 @@ func (mp *MessagePool) VerifyMsgSig(m *types.SignedMessage) error {
return nil
}
if m.Signature.Type == crypto.SigTypeDelegated {
txArgs, err := ethtypes.NewEthTxArgsFromMessage(&m.Message)
if err != nil {
return xerrors.Errorf("failed to convert to eth tx args: %w", err)
}
msg, err := txArgs.ToRlpUnsignedMsg()
if err != nil {
return err
}
if err := sigs.Verify(&m.Signature, m.Message.From, msg); err != nil {
return err
}
} else if err := sigs.Verify(&m.Signature, m.Message.From, m.Message.Cid().Bytes()); err != nil {
return err
if err := chain.AuthenticateMessage(m, m.Message.From); err != nil {
return xerrors.Errorf("failed to validate signature: %w", err)
}
mp.sigValCache.Add(sck, struct{}{})

38
chain/signatures.go Normal file
View File

@ -0,0 +1,38 @@
package chain
import (
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
"github.com/filecoin-project/lotus/lib/sigs"
"golang.org/x/xerrors"
)
// 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
switch typ := msg.Signature.Type; typ {
case crypto.SigTypeDelegated:
txArgs, err := ethtypes.NewEthTxArgsFromMessage(&msg.Message)
if err != nil {
return xerrors.Errorf("failed to reconstruct eth transaction: %w", err)
}
msg, err := txArgs.ToRlpUnsignedMsg()
if err != nil {
return xerrors.Errorf("failed to repack eth rlp message: %w", err)
}
digest = msg
default:
digest = msg.Message.Cid().Bytes()
}
if err := sigs.Verify(&msg.Signature, signer, digest); err != nil {
return xerrors.Errorf("secpk message %s has invalid signature: %w", msg.Cid(), err)
}
return nil
}