Implement MarshalBinary and UnmarshalBinary for MerkleTreeHash

This commit is contained in:
Andrew Ayer 2023-08-25 07:40:20 -04:00
parent 70e05ea7b0
commit eb16a10c2e
1 changed files with 8 additions and 0 deletions

View File

@ -28,11 +28,19 @@ func (h Hash) MarshalJSON() ([]byte, error) {
return json.Marshal(h[:])
}
func (h Hash) MarshalBinary() ([]byte, error) {
return h[:], nil
}
func (h *Hash) UnmarshalJSON(b []byte) error {
var hashBytes []byte
if err := json.Unmarshal(b, &hashBytes); err != nil {
return err
}
return h.UnmarshalBinary(hashBytes)
}
func (h *Hash) UnmarshalBinary(hashBytes []byte) error {
if len(hashBytes) != HashLen {
return fmt.Errorf("Merkle Tree hash has wrong length (should be %d bytes long, not %d)", HashLen, len(hashBytes))
}