Skip to content

Commit

Permalink
Adds error for FromBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJoiner committed Feb 26, 2024
1 parent 91fde05 commit 479c63a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
5 changes: 4 additions & 1 deletion examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ func ExampleToHex() {
func ExampleFromBytes() {
// Generate a mnemonic from a byte slice
bytes := []byte{'z', 35, 67, 0xff, 0x89, 0, 0xcd, 0xef}
words := mnemonic.FromBytes(bytes)
words, err := mnemonic.FromBytes(bytes)
if err != nil {
panic(err)
}

fmt.Println(words)
// Output: [kick borrow zoo bamboo art wasp]
Expand Down
6 changes: 3 additions & 3 deletions mnemonic.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ func FromHex(data string) ([]string, error) {

// FromBytes converts a byte slice to a list of mnemonic words.
// The length in bits of the byte slice must be a multiple of 32.
func FromBytes(data []byte) []string {
words, _ := FromBigIntFixed(big.NewInt(0).SetBytes(data), len(data)*bitsPerByte)
return words
func FromBytes(data []byte) ([]string, error) {
return FromBigIntFixed(big.NewInt(0).SetBytes(data), len(data)*bitsPerByte)

}

// FromUint32WithObfuscation behaves the same as FromUint, but the provided data is obfuscated first.
Expand Down
6 changes: 5 additions & 1 deletion mnemonic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ func TestFromHex(t *testing.T) {
func TestFromBytes(t *testing.T) {
t.Parallel()
bytes := []byte{0, 0, 0x45, 0x67, 0x89, 0xab, 0, 0}
words := mnemonic.FromBytes(bytes)
words, err := mnemonic.FromBytes(bytes)
if err != nil {
t.Errorf("Error converting bytes to mnemonic: %v", err)
return
}

decodedBytes, err := mnemonic.ToBytes(words)
if err != nil {
Expand Down

0 comments on commit 479c63a

Please sign in to comment.