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

Commit 237111a

Browse files
committed
refactor: avoid nested code
#41 (comment)
1 parent b655f6b commit 237111a

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

ipns.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,28 +133,27 @@ func createCborDataForIpnsEntry(e *pb.IpnsEntry) ([]byte, error) {
133133
// Validates validates the given IPNS entry against the given public key.
134134
func Validate(pk ic.PubKey, entry *pb.IpnsEntry) error {
135135
// Check the ipns record signature with the public key
136-
137-
// Check v2 signature if it's available
138-
if entry.GetSignatureV2() != nil {
139-
sig2Data, err := ipnsEntryDataForSigV2(entry)
140-
if err != nil {
141-
return fmt.Errorf("could not compute signature data: %w", err)
142-
}
143-
if ok, err := pk.Verify(sig2Data, entry.GetSignatureV2()); err != nil || !ok {
144-
return ErrSignature
145-
}
146-
147-
// TODO: If we switch from pb.IpnsEntry to a more generic IpnsRecord type then perhaps we should only check
148-
// this if there is no v1 signature. In the meanwhile this helps avoid some potential rough edges around people
149-
// checking the entry fields instead of doing CBOR decoding everywhere.
150-
if err := validateCborDataMatchesPbData(entry); err != nil {
151-
return err
152-
}
153-
} else {
136+
if entry.GetSignatureV2() == nil {
154137
// always error if no valid signature could be found
155138
return ErrSignature
156139
}
157140

141+
sig2Data, err := ipnsEntryDataForSigV2(entry)
142+
if err != nil {
143+
return fmt.Errorf("could not compute signature data: %w", err)
144+
}
145+
if ok, err := pk.Verify(sig2Data, entry.GetSignatureV2()); err != nil || !ok {
146+
return ErrSignature
147+
}
148+
149+
// TODO: If we switch from pb.IpnsEntry to a more generic IpnsRecord type then perhaps we should only check
150+
// this if there is no v1 signature. In the meanwhile this helps avoid some potential rough edges around people
151+
// checking the entry fields instead of doing CBOR decoding everywhere.
152+
// See https://github.com/ipfs/go-ipns/pull/42 for next steps here
153+
if err := validateCborDataMatchesPbData(entry); err != nil {
154+
return err
155+
}
156+
158157
eol, err := GetEOL(entry)
159158
if err != nil {
160159
return err

0 commit comments

Comments
 (0)