From 237111a8d232ee4adfe2c8907fa95f779acf7170 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Tue, 20 Sep 2022 23:06:50 +0200 Subject: [PATCH] refactor: avoid nested code https://github.com/ipfs/go-ipns/pull/41#discussion_r975078116 --- ipns.go | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/ipns.go b/ipns.go index 5f9eb25..fae3f6e 100644 --- a/ipns.go +++ b/ipns.go @@ -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