-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to specify a JSON serialized condition using bech32 encoding. resolve #362
- Loading branch information
Showing
5 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,33 @@ | ||
package bech32 | ||
|
||
import ( | ||
"github.com/btcsuite/btcutil/bech32" | ||
"github.com/iov-one/weave/errors" | ||
) | ||
|
||
// Decode converts given bech32 encoded representation into raw payload and a | ||
// human readable part. | ||
func Decode(raw string) (string, []byte, error) { | ||
hrp, payload, err := bech32.Decode(raw) | ||
if err != nil { | ||
return "", nil, errors.Wrap(err, "bech32 decode") | ||
} | ||
payload, err = bech32.ConvertBits(payload, 5, 8, false) | ||
if err != nil { | ||
return "", nil, errors.Wrap(err, "convert bits") | ||
} | ||
return hrp, payload, nil | ||
} | ||
|
||
// Encode converts given bytes into bech32 encoded representation. | ||
func Encode(hrp string, payload []byte) ([]byte, error) { | ||
payload, err := bech32.ConvertBits(payload, 8, 5, true) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "convert bits") | ||
} | ||
raw, err := bech32.Encode(hrp, payload) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "bech32 encode") | ||
} | ||
return []byte(raw), 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,37 @@ | ||
package bech32 | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"testing" | ||
) | ||
|
||
func TestBench32EncodeDecode(t *testing.T) { | ||
// bech32 -e -h tiov 746573742d7061796c6f6164 | ||
const enc = `tiov1w3jhxapdwpshjmr0v9jqymqq4y` | ||
|
||
want, err := hex.DecodeString("746573742d7061796c6f6164") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
hrp, payload, err := Decode(enc) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !bytes.Equal(want, payload) { | ||
t.Logf("want %d", want) | ||
t.Logf("got %d", payload) | ||
t.Fatal("invalid decode") | ||
} | ||
|
||
raw, err := Encode(hrp, payload) | ||
if err != nil { | ||
t.Fatalf("cannot encode: %s", err) | ||
} | ||
|
||
if string(raw) != enc { | ||
t.Fatalf("invalid encoding: %q", raw) | ||
} | ||
} |