Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/bech32plus2 #164

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add LIMIT for decode to bech32plus and call this in bech32.go
  • Loading branch information
Kynea0b committed Jan 15, 2021
commit 6317741ce181ab48caa83a10decd205e3a362001
9 changes: 4 additions & 5 deletions libs/bech32/bech32.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package bech32

import (
"github.com/btcsuite/btcutil/bech32"
"github.com/pkg/errors"
)

//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)
converted, err := ConvertBits(data, 8, 5, true)
if err != nil {
return "", errors.Wrap(err, "encoding bech32 failed")
}
return bech32.Encode(hrp, converted)
return 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)
hrp, data, err := Decode(bech)
if err != nil {
return "", nil, errors.Wrap(err, "decoding bech32 failed")
}
converted, err := bech32.ConvertBits(data, 5, 8, false)
converted, err := ConvertBits(data, 5, 8, false)
if err != nil {
return "", nil, errors.Wrap(err, "decoding bech32 failed")
}
Expand Down
13 changes: 6 additions & 7 deletions libs/bech32/bech32plus.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
// Copyright (c) 2017 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package bech32

import (
"fmt"
"strings"
)

const charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
const (
charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
LIMIT = 200
)

var gen = []int{0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3}

// Decode decodes a bech32 encoded string, returning the human-readable
// part and the data part excluding the checksum.
func Decode(bech string) (string, []byte, error) {
// The maximum allowed length for a bech32 string is 90. It must also
// The maximum allowed length for a bech32 string is LIMIT which is constant variable. It must also
// be at least 8 characters, since it needs a non-empty HRP, a
// separator, and a 6 character checksum.
if len(bech) < 8 || len(bech) > 90 {
if len(bech) < 8 || len(bech) > LIMIT {
return "", nil, fmt.Errorf("invalid bech32 string length %d",
len(bech))
}
Expand Down
17 changes: 8 additions & 9 deletions libs/bech32/bech32plus_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// Copyright (c) 2017 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package bech32_test

import (
Expand All @@ -21,13 +17,16 @@ func TestBech32(t *testing.T) {
{"abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw", true},
{"11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j", true},
{"split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", true},
{"split1checkupstagehandshakeupstreamerranterredcaperred2y9e2w", false}, // invalid checksum
{"s lit1checkupstagehandshakeupstreamerranterredcaperredp8hs2p", false}, // invalid character (space) in hrp
{"spl" + string(127) + "t1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", false}, // invalid character (DEL) in hrp
{"split1checkupstagehandshakeupstreamerranterredcaperred2y9e2w", false}, // invalid checksum
{"s lit1checkupstagehandshakeupstreamerranterredcaperredp8hs2p", false}, // invalid character (space) in hrp
{"spl" + string(rune(127)) + "t1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", false}, // invalid character (DEL) in hrp
{"split1cheo2y9e2w", false}, // invalid character (o) in data part
{"split1a2y9w", false}, // too short data part
{"1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", false}, // empty hrp
{"11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqsqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j", false}, // too long
{"1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", false}, // empty hrp
// 165 characters plus using LIMIT
{"linkvalconspub1668lhsfs4zxq4gakkq5yj8ujn22g43s7qz7498el7k57cw88t30y56sxa6eszusd470ncx3j79slqg3aphy2c93ymejzqxzpu36pjrv5u0wphpf5eluc7am08m0ekkfns4jcnrj7u2hc5f6240tlze", true},
// 201 characters plus using LIMIT
{"linkvalconspub1668lhsfs4zxq4gakkq5yj8ujn22g43s7qz7498el7k57cw88t30y56sxa6eszusd470ncx3j79slqg3aphy2c93ymejzqxzpu36pjrv5u0wphpf5eluc7am08m0ekkfns4jcnrj7u2hc5f6240tlze668lhsfs4zxq4gakkq5yj8ujn22g43s7qz74", false},
}

for _, test := range tests {
Expand Down
4 changes: 0 additions & 4 deletions libs/bech32/exampleplus_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// Copyright (c) 2017 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package bech32_test

import (
Expand Down