forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* bring over bech32 from tendermint * fix spacing * address comment * change to fmt.Errorf * space dem comments * remove err cheks
- Loading branch information
1 parent
d488260
commit 4173ea5
Showing
5 changed files
with
59 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package bech32 | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/btcsuite/btcutil/bech32" | ||
) | ||
|
||
// ConvertAndEncode converts from a base64 encoded byte string to base32 encoded byte string and then to bech32. | ||
func ConvertAndEncode(hrp string, data []byte) (string, error) { | ||
converted, err := bech32.ConvertBits(data, 8, 5, true) | ||
if err != nil { | ||
return "", fmt.Errorf("encoding bech32 failed: %w", err) | ||
} | ||
|
||
return bech32.Encode(hrp, converted) | ||
} | ||
|
||
// DecodeAndConvert decodes a bech32 encoded string and converts to base64 encoded bytes. | ||
func DecodeAndConvert(bech string) (string, []byte, error) { | ||
hrp, data, err := bech32.Decode(bech) | ||
if err != nil { | ||
return "", nil, fmt.Errorf("decoding bech32 failed: %w", err) | ||
} | ||
|
||
converted, err := bech32.ConvertBits(data, 5, 8, false) | ||
if err != nil { | ||
return "", nil, fmt.Errorf("decoding bech32 failed: %w", err) | ||
} | ||
|
||
return hrp, converted, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package bech32_test | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/tendermint/tendermint/libs/bech32" | ||
) | ||
|
||
func TestEncodeAndDecode(t *testing.T) { | ||
sum := sha256.Sum256([]byte("hello world\n")) | ||
ss := "shasum" | ||
|
||
bech, err := bech32.ConvertAndEncode(ss, sum[:]) | ||
require.NoError(t, err) | ||
|
||
hrp, data, err := bech32.DecodeAndConvert(bech) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, hrp, ss, "Invalid hrp") | ||
require.True(t, bytes.Equal(data, sum[:]), "Invalid decode") | ||
} |