Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
refactor: avoid nested code
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Sep 20, 2022
1 parent b655f6b commit 237111a
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions ipns.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,28 +133,27 @@ func createCborDataForIpnsEntry(e *pb.IpnsEntry) ([]byte, error) {
// Validates validates the given IPNS entry against the given public key.
func Validate(pk ic.PubKey, entry *pb.IpnsEntry) error {
// Check the ipns record signature with the public key

// Check v2 signature if it's available
if entry.GetSignatureV2() != nil {
sig2Data, err := ipnsEntryDataForSigV2(entry)
if err != nil {
return fmt.Errorf("could not compute signature data: %w", err)
}
if ok, err := pk.Verify(sig2Data, entry.GetSignatureV2()); err != nil || !ok {
return ErrSignature
}

// TODO: If we switch from pb.IpnsEntry to a more generic IpnsRecord type then perhaps we should only check
// this if there is no v1 signature. In the meanwhile this helps avoid some potential rough edges around people
// checking the entry fields instead of doing CBOR decoding everywhere.
if err := validateCborDataMatchesPbData(entry); err != nil {
return err
}
} else {
if entry.GetSignatureV2() == nil {
// always error if no valid signature could be found
return ErrSignature
}

sig2Data, err := ipnsEntryDataForSigV2(entry)
if err != nil {
return fmt.Errorf("could not compute signature data: %w", err)
}
if ok, err := pk.Verify(sig2Data, entry.GetSignatureV2()); err != nil || !ok {
return ErrSignature
}

// TODO: If we switch from pb.IpnsEntry to a more generic IpnsRecord type then perhaps we should only check
// this if there is no v1 signature. In the meanwhile this helps avoid some potential rough edges around people
// checking the entry fields instead of doing CBOR decoding everywhere.
// See https://github.com/ipfs/go-ipns/pull/42 for next steps here
if err := validateCborDataMatchesPbData(entry); err != nil {
return err
}

eol, err := GetEOL(entry)
if err != nil {
return err
Expand Down

0 comments on commit 237111a

Please sign in to comment.