From afd92ae7b0ac1f67e65daacd9a2be4cac6bea340 Mon Sep 17 00:00:00 2001 From: Andreas Auernhammer Date: Wed, 11 Sep 2024 14:59:53 +0200 Subject: [PATCH] add `Identity.IsZero` method This commit adds the `IsZero() bool` method to the `Identity` type. It returns true if the identity is equal to the Identity zero value. A 'valid' identity won't be zero since `H(data)` will not produce a hash value of all zero bits with overwhelming probability if a H is a collision-resistant hash function. The `IsZero` method is inline with the accepted Go JSON proposal adding the struct tag `omitzero`. Ref: https://github.com/golang/go/issues/45669#issuecomment-2329745918 Signed-off-by: Andreas Auernhammer --- identity.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/identity.go b/identity.go index b96478c..713585d 100644 --- a/identity.go +++ b/identity.go @@ -49,6 +49,11 @@ type Identity struct { hash [32]byte } +var zeroIdentity = Identity{} + +// IsZero returns true if i is the Identity zero value. +func (i Identity) IsZero() bool { return i == zeroIdentity } + // MarshalBinary returns a binary representation of the identity. func (i Identity) MarshalBinary() ([]byte, error) { var buf [35]byte