chainhash: JSON Unmarshal hash from appropriate string.

This commit is contained in:
RycCheen 2023-02-21 19:53:42 +08:00 committed by John C. Vernaleo
parent a18c2cfbf8
commit 72ea23ed1e

View file

@ -116,6 +116,21 @@ func (hash Hash) MarshalJSON() ([]byte, error) {
return json.Marshal(hash.String())
}
// UnmarshalJSON parses the hash with JSON appropriate string value.
func (hash *Hash) UnmarshalJSON(input []byte) error {
var sh string
err := json.Unmarshal(input, &sh)
if err != nil {
return err
}
newHash, err := NewHashFromStr(sh)
if err != nil {
return err
}
return hash.SetBytes(newHash[:])
}
// NewHash returns a new Hash from a byte slice. An error is returned if
// the number of bytes passed in is not HashSize.
func NewHash(newHash []byte) (*Hash, error) {