1
0
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:
Jeromy
2015-02-25 14:56:26 -08:00
parent 049b5ad945
commit 7fb63d7e43
13 changed files with 65 additions and 21 deletions

View File

@ -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)