Skip to content

Commit

Permalink
chainhash: JSON Unmarshal hash from appropriate string.
Browse files Browse the repository at this point in the history
  • Loading branch information
LindenWang01 authored and jcvernaleo committed Mar 21, 2023
1 parent a18c2cf commit 72ea23e
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions chaincfg/chainhash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 72ea23e

Please sign in to comment.