Skip to content

Commit

Permalink
Fix merge errors
Browse files Browse the repository at this point in the history
  • Loading branch information
krhubert committed Aug 30, 2019
1 parent dae744c commit d1d5aa5
Show file tree
Hide file tree
Showing 6 changed files with 415 additions and 415 deletions.
17 changes: 17 additions & 0 deletions hash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ func Random() (Hash, error) {
return hash, nil
}

// Decode decodes the base58 encoded hash. It returns error
// when a hash isn't base58,encoded or hash length is invalid.
func Decode(h string) (Hash, error) {
hash, err := base58.Decode(h)
if err != nil {
return nil, fmt.Errorf("hash: %s", err)
}
if len(hash) != size {
return nil, fmt.Errorf("hash: invalid length string")
}
return Hash(hash), nil
}

// IsZero reports whethere h represents empty hash.
func (h Hash) IsZero() bool {
return len(h) == 0
Expand Down Expand Up @@ -126,6 +139,10 @@ func (h *Hash) UnmarshalJSON(data []byte) error {
return err
}

if str == "" {
return nil
}

h1, err := base58.Decode(str)
if err != nil {
return err
Expand Down
12 changes: 12 additions & 0 deletions hash/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ func TestRandom(t *testing.T) {
require.NoError(t, quick.Check(f, nil))
}

func TestDecode(t *testing.T) {
hash, err := Decode("4uQeVj5tqViQh7yWWGStvkEG1Zmhx6uasJtWCJziofM")
assert.NoError(t, err)
assert.Equal(t, hash, one)

_, err = Decode("0")
assert.Equal(t, "hash: invalid base58 digit ('0')", err.Error())

_, err = Decode("1")
assert.Equal(t, "hash: invalid length string", err.Error())
}

func TestIsZero(t *testing.T) {
assert.True(t, Hash{}.IsZero())
}
Expand Down
Loading

0 comments on commit d1d5aa5

Please sign in to comment.