Skip to content

Commit

Permalink
Merge pull request #3030 from oasisprotocol/kostko/fix/bech32-notm
Browse files Browse the repository at this point in the history
go/common/encoding/bech32: Replace to-be-removed dependency
  • Loading branch information
kostko authored Jun 18, 2020
2 parents 2ca7a1d + 3c435d5 commit 02a9458
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions .changelog/3030.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/common/encoding/bech32: Replace to-be-removed dependency
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 02a9458

Please sign in to comment.