Skip to content
This repository was archived by the owner on Jul 15, 2018. It is now read-only.

PubKeyFromBytes: return zero value PubKey on error #49

Merged
merged 1 commit into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pub_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import (
)

func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) {
err = wire.ReadBinaryBytes(pubKeyBytes, &pubKey)
return
if err := wire.ReadBinaryBytes(pubKeyBytes, &pubKey); err != nil {
return PubKey{}, err
}
return pubKey, nil
}

//----------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions pub_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/btcsuite/btcutil/base58"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type keyData struct {
Expand Down Expand Up @@ -39,3 +40,9 @@ func TestPubKeySecp256k1Address(t *testing.T) {
assert.Equal(t, addr, addrB, "Expected addresses to match")
}
}

func TestPubKeyInvalidDataProperReturnsEmpty(t *testing.T) {
pk, err := PubKeyFromBytes([]byte("foo"))
require.NotNil(t, err, "expecting a non-nil error")
require.True(t, pk.Empty(), "expecting an empty public key on error")
}