mirror of
https://github.com/btcsuite/btcd.git
synced 2025-03-12 10:30:49 +01:00
Merge pull request #2025 from Beerosagos/hash-unmarshal
chainhash: add support to legacy-marshaled hashes
This commit is contained in:
commit
f12a0b3b56
2 changed files with 30 additions and 0 deletions
|
@ -118,6 +118,12 @@ func (hash Hash) MarshalJSON() ([]byte, error) {
|
|||
|
||||
// UnmarshalJSON parses the hash with JSON appropriate string value.
|
||||
func (hash *Hash) UnmarshalJSON(input []byte) error {
|
||||
// If the first byte indicates an array, the hash could have been marshalled
|
||||
// using the legacy method and e.g. persisted.
|
||||
if len(input) > 0 && input[0] == '[' {
|
||||
return decodeLegacy(hash, input)
|
||||
}
|
||||
|
||||
var sh string
|
||||
err := json.Unmarshal(input, &sh)
|
||||
if err != nil {
|
||||
|
@ -217,3 +223,17 @@ func Decode(dst *Hash, src string) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
// decodeLegacy decodes an Hash that has been encoded with the legacy method
|
||||
// (i.e. represented as a bytes array) to a destination.
|
||||
func decodeLegacy(dst *Hash, src []byte) error {
|
||||
var hashBytes []byte
|
||||
err := json.Unmarshal(src, &hashBytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(hashBytes) != HashSize {
|
||||
return ErrHashStrSize
|
||||
}
|
||||
return dst.SetBytes(hashBytes)
|
||||
}
|
||||
|
|
|
@ -199,6 +199,7 @@ func TestNewHashFromStr(t *testing.T) {
|
|||
// TestHashJsonMarshal tests json marshal and unmarshal.
|
||||
func TestHashJsonMarshal(t *testing.T) {
|
||||
hashStr := "000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
|
||||
legacyHashStr := []byte("[6,229,51,253,26,218,134,57,31,63,108,52,50,4,176,210,120,212,170,236,28,11,32,170,39,186,3,0,0,0,0,0]")
|
||||
|
||||
hash, err := NewHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
|
@ -219,4 +220,13 @@ func TestHashJsonMarshal(t *testing.T) {
|
|||
if !hash.IsEqual(&newHash) {
|
||||
t.Errorf("String: wrong hash string - got %v, want %v", newHash.String(), hashStr)
|
||||
}
|
||||
|
||||
err = newHash.UnmarshalJSON(legacyHashStr)
|
||||
if err != nil {
|
||||
t.Errorf("Unmarshal legacy json error:%v, hash:%v", err, legacyHashStr)
|
||||
}
|
||||
|
||||
if !hash.IsEqual(&newHash) {
|
||||
t.Errorf("String: wrong hash string - got %v, want %v", newHash.String(), hashStr)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue