btcsuite / btcutil

Provides bitcoin-specific convenience functions and types
479 stars 409 forks source link

Decoding failure #137

Closed husio closed 5 years ago

husio commented 5 years ago

I have noticed that decoding done with bech32.Decode returns different result than https://github.com/nym-zone/bech32

The below diff is adding a test that proves that

diff --git a/bech32/bech32_test.go b/bech32/bech32_test.go
index 1322386..9583a1c 100644
--- a/bech32/bech32_test.go
+++ b/bech32/bech32_test.go
@@ -5,6 +5,8 @@
 package bech32_test

 import (
+       "bytes"
+       "encoding/hex"
        "strings"
        "testing"

@@ -67,3 +69,25 @@ func TestBech32(t *testing.T) {
                }
        }
 }
+
+func TestBench32Encoding(t *testing.T) {
+       // bech32  -e -h tiov 746573742d7061796c6f6164
+       const enc = `tiov1w3jhxapdwpshjmr0v9jqymqq4y`
+
+       want, err := hex.DecodeString("746573742d7061796c6f6164")
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       _, payload, err := bech32.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")
+       }
+
+}
husio commented 5 years ago

I think I am missing bits conversion part. It all makes more sense when looking at the encoding example where the bits encoding is used as well https://github.com/btcsuite/btcutil/blob/master/bech32/example_test.go#L32-L49

Roasbeef commented 5 years ago

In your example, you aren't converting to base 5 before decoding.