mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-10 14:34:24 +08:00
move signing options into a validation checker struct
This commit is contained in:
@ -25,7 +25,12 @@ var ErrInvalidRecordType = errors.New("invalid record keytype")
|
||||
// Validator is an object that helps ensure routing records are valid.
|
||||
// It is a collection of validator functions, each of which implements
|
||||
// its own notion of validity.
|
||||
type Validator map[string]ValidatorFunc
|
||||
type Validator map[string]*ValidChecker
|
||||
|
||||
type ValidChecker struct {
|
||||
Func ValidatorFunc
|
||||
Sign bool
|
||||
}
|
||||
|
||||
// VerifyRecord checks a record and ensures it is still valid.
|
||||
// It runs needed validators
|
||||
@ -37,13 +42,30 @@ func (v Validator) VerifyRecord(r *pb.Record) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
fnc, ok := v[parts[1]]
|
||||
val, ok := v[parts[1]]
|
||||
if !ok {
|
||||
log.Infof("Unrecognized key prefix: %s", parts[1])
|
||||
return ErrInvalidRecordType
|
||||
}
|
||||
|
||||
return fnc(u.Key(r.GetKey()), r.GetValue())
|
||||
return val.Func(u.Key(r.GetKey()), r.GetValue())
|
||||
}
|
||||
|
||||
func (v Validator) IsSigned(k u.Key) (bool, error) {
|
||||
// Now, check validity func
|
||||
parts := strings.Split(string(k), "/")
|
||||
if len(parts) < 3 {
|
||||
log.Infof("Record key does not have validator: %s", k)
|
||||
return false, nil
|
||||
}
|
||||
|
||||
val, ok := v[parts[1]]
|
||||
if !ok {
|
||||
log.Infof("Unrecognized key prefix: %s", parts[1])
|
||||
return false, ErrInvalidRecordType
|
||||
}
|
||||
|
||||
return val.Sign, nil
|
||||
}
|
||||
|
||||
// ValidatePublicKeyRecord implements ValidatorFunc and
|
||||
@ -62,6 +84,11 @@ func ValidatePublicKeyRecord(k u.Key, val []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var PublicKeyValidator = &ValidChecker{
|
||||
Func: ValidatePublicKeyRecord,
|
||||
Sign: false,
|
||||
}
|
||||
|
||||
func CheckRecordSig(r *pb.Record, pk ci.PubKey) error {
|
||||
blob := RecordBlobForSig(r)
|
||||
good, err := pk.Verify(blob, r.Signature)
|
||||
|
Reference in New Issue
Block a user