Browse Source

Bech32 (#216)

* Add support for regular bech32 to tmlibs

* Add bech32 to gopkg.toml
pull/1780/head
Zaki Manian 7 years ago
committed by Anton Kaliaev
parent
commit
44f1bdb0d5
4 changed files with 69 additions and 16 deletions
  1. +7
    -16
      Gopkg.lock
  2. +3
    -0
      Gopkg.toml
  3. +28
    -0
      bech32/bech32.go
  4. +31
    -0
      bech32/bech32_test.go

+ 7
- 16
Gopkg.lock View File

@ -1,6 +1,12 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
branch = "master"
name = "github.com/btcsuite/btcutil"
packages = ["bech32"]
revision = "d4cc87b860166d00d6b5b9e0d3b3d71d6088d4d4"
[[projects]]
name = "github.com/davecgh/go-spew"
packages = ["spew"]
@ -41,16 +47,6 @@
revision = "817915b46b97fd7bb80e8ab6b69f01a53ac3eebf"
version = "v1.6.0"
[[projects]]
name = "github.com/gogo/protobuf"
packages = [
"gogoproto",
"proto",
"protoc-gen-gogo/descriptor"
]
revision = "1adfc126b41513cc696b209667c8656ea7aac67c"
version = "v1.0.0"
[[projects]]
name = "github.com/golang/protobuf"
packages = [
@ -196,11 +192,6 @@
]
revision = "b89cc31ef7977104127d34c1bd31ebd1a9db2199"
[[projects]]
name = "golang.org/x/crypto"
packages = ["ripemd160"]
revision = "edd5e9b0879d13ee6970a50153d85b8fec9f7686"
[[projects]]
branch = "master"
name = "golang.org/x/net"
@ -285,6 +276,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "8aa4ea7ef6d0ff170127eb5bca89c6c37c767d58047159cfd26a431c5cd5e7ad"
inputs-digest = "e0c0af880b57928787ea78a820abefd2759e6aee4cba18e67ab36b80e62ad581"
solver-name = "gps-cdcl"
solver-version = 1

+ 3
- 0
Gopkg.toml View File

@ -61,6 +61,9 @@
name = "github.com/stretchr/testify"
version = "1.2.1"
[[constraint]]
name = "github.com/btcsuite/btcutil"
branch ="master"
[prune]
go-tests = true
unused-packages = true

+ 28
- 0
bech32/bech32.go View File

@ -0,0 +1,28 @@
package bech32
import (
"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 "", 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, err
}
converted, err := bech32.ConvertBits(data, 5, 8, false)
if err != nil {
return "", nil, err
}
return hrp, converted, nil
}

+ 31
- 0
bech32/bech32_test.go View File

@ -0,0 +1,31 @@
package bech32_test
import (
"bytes"
"crypto/sha256"
"testing"
"github.com/tendermint/tmlibs/bech32"
)
func TestEncodeAndDecode(t *testing.T) {
sum := sha256.Sum256([]byte("hello world\n"))
bech, err := bech32.ConvertAndEncode("shasum", sum[:])
if err != nil {
t.Error(err)
}
hrp, data, err := bech32.DecodeAndConvert(bech)
if err != nil {
t.Error(err)
}
if hrp != "shasum" {
t.Error("Invalid hrp")
}
if bytes.Compare(data, sum[:]) != 0 {
t.Error("Invalid decode")
}
}

Loading…
Cancel
Save