Skip to content

Commit

Permalink
go/common/encoding/bech32: Replace to-be-removed dependency
Browse files Browse the repository at this point in the history
The github.com/tendermint/tendermint/libs/bech32 module has been removed in
later Tendermint Core versions. As it only contained a small helper, we just
copy it over.
  • Loading branch information
kostko committed Jun 18, 2020
1 parent 2ca7a1d commit 974aa04
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
24 changes: 21 additions & 3 deletions go/common/encoding/bech32/bech32.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,32 @@
// BIP 173: https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki.
package bech32

import "github.com/tendermint/tendermint/libs/bech32"
import (
"fmt"

"github.com/btcsuite/btcutil/bech32"
)

// Encode encodes 8-bits per byte byte-slice to a Bech32-encoded string.
func Encode(hrp string, data []byte) (string, error) {
return bech32.ConvertAndEncode(hrp, data)
// NOTE: Taken from github.com/tendermint/tendermint/libs/bech32 (licensed under Apache-2).
converted, err := bech32.ConvertBits(data, 8, 5, true)
if err != nil {
return "", fmt.Errorf("encoding bech32 failed: %w", err)
}
return bech32.Encode(hrp, converted)
}

// Decode decodes a Bech32-encoded string to a 8-bits per byte byte-slice.
func Decode(text string) (string, []byte, error) {
return bech32.DecodeAndConvert(text)
// NOTE: Taken from github.com/tendermint/tendermint/libs/bech32 (licensed under Apache-2).
hrp, data, err := bech32.Decode(text)
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
}
1 change: 1 addition & 0 deletions go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ replace (

require (
github.com/blevesearch/bleve v1.0.9
github.com/btcsuite/btcutil v1.0.2
github.com/cenkalti/backoff/v4 v4.0.0
github.com/cznic/b v0.0.0-20181122101859-a26611c4d92d // indirect
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
Expand Down

0 comments on commit 974aa04

Please sign in to comment.