zpay32: adjust uint64 encoding to account for math.MaxUnit64
In this commit, we fix a logic error in our routine for converting a uint64 to/from base32. Before this commit, we assumed that the max number of groups was 12. However, the math.MaxUint64 (1<<64 - 1) can actually consume more than 12 groups with an extra set of bits. Before this commit, we would panic when attempting to parse an invoice generated like so: * addinvoice --amt 1337000 --expiry 99999999999999999 To fix this issue, we modify our logic to expect at most 13 groups. Additionally, we've added a new test that would panic before applying this commit. Fixes #972.
This commit is contained in:
2 files changed
+43
-9
No files matched your search
@@ -2,6 +2,7 @@ package zpay32
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"math"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -527,7 +528,11 @@ func TestParseExpiry(t *testing.T) {
|
||||
result: &testExpiry60,
|
||||
},
|
||||
{
|
||||
data: []byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc},
|
||||
data: []byte{
|
||||
0x0, 0x1, 0x2, 0x3, 0x4, 0x5,
|
||||
0x6, 0x7, 0x8, 0x9, 0xa, 0xb,
|
||||
0xc, 0x3,
|
||||
},
|
||||
valid: false, // data too long
|
||||
},
|
||||
}
|
||||
@@ -547,7 +552,8 @@ func TestParseExpiry(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseMinFinalCLTVExpiry checks that the minFinalCLTVExpiry is properly parsed.
|
||||
// TestParseMinFinalCLTVExpiry checks that the minFinalCLTVExpiry is properly
|
||||
// parsed.
|
||||
func TestParseMinFinalCLTVExpiry(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -567,12 +573,20 @@ func TestParseMinFinalCLTVExpiry(t *testing.T) {
|
||||
result: 60,
|
||||
},
|
||||
{
|
||||
data: []byte{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc},
|
||||
data: []byte{
|
||||
0x1, 0x2, 0x3, 0x4, 0x5,
|
||||
0x6, 0x7, 0x8, 0x9, 0xa,
|
||||
0xb, 0xc,
|
||||
},
|
||||
valid: true,
|
||||
result: 38390726480144748,
|
||||
},
|
||||
{
|
||||
data: []byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc},
|
||||
data: []byte{
|
||||
0x0, 0x1, 0x2, 0x3, 0x4, 0x5,
|
||||
0x6, 0x7, 0x8, 0x9, 0xa, 0xb,
|
||||
0xc, 0x94,
|
||||
},
|
||||
valid: false, // data too long
|
||||
},
|
||||
}
|
||||
@@ -592,6 +606,26 @@ func TestParseMinFinalCLTVExpiry(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseMinFinalCLTVExpiry tests that were able to properly encode/decode
|
||||
// the math.MaxUint64 integer without panicking.
|
||||
func TestParseMaxUint64Expiry(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
expiry := uint64(math.MaxUint64)
|
||||
|
||||
expiryBytes := uint64ToBase32(expiry)
|
||||
|
||||
expiryReParse, err := base32ToUint64(expiryBytes)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to parse uint64: %v", err)
|
||||
}
|
||||
|
||||
if expiryReParse != expiry {
|
||||
t.Fatalf("wrong expiry: expected %v got %v", expiry,
|
||||
expiryReParse)
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseFallbackAddr checks that the fallback address is properly parsed.
|
||||
func TestParseFallbackAddr(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
Reference in new issue
Block a user