Skip to content

Commit

Permalink
types: add bech32 (cosmos#6200)
Browse files Browse the repository at this point in the history
* bring over bech32 from tendermint

* fix spacing

* address comment

* change to fmt.Errorf

* space dem comments

* remove err cheks
  • Loading branch information
tac0turtle authored May 12, 2020
1 parent d488260 commit 4173ea5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion client/keys/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/spf13/viper"
yaml "gopkg.in/yaml.v2"

"github.com/tendermint/tendermint/libs/bech32"
"github.com/cosmos/cosmos-sdk/types/bech32"
"github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require (
github.com/99designs/keyring v1.1.5
github.com/bgentry/speakeasy v0.1.0
github.com/btcsuite/btcd v0.20.1-beta
github.com/btcsuite/btcutil v1.0.2
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d
github.com/cosmos/ledger-cosmos-go v0.11.1
github.com/gibson042/canonicaljson-go v1.0.3
Expand Down
2 changes: 1 addition & 1 deletion types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
tmamino "github.com/tendermint/tendermint/crypto/encoding/amino"
yaml "gopkg.in/yaml.v2"

"github.com/tendermint/tendermint/libs/bech32"
"github.com/cosmos/cosmos-sdk/types/bech32"
)

const (
Expand Down
32 changes: 32 additions & 0 deletions types/bech32/bech32.go
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
}
24 changes: 24 additions & 0 deletions types/bech32/bech32_test.go
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")
}

0 comments on commit 4173ea5

Please sign in to comment.