b58_test.gno
2.09 Kb · 82 lines
1package b58
2
3import (
4 "encoding/hex"
5 "testing"
6
7 "gno.land/p/nt/uassert/v0"
8)
9
10// Known Bitcoin Base58 test vectors (hex input → Base58 string).
11var vectors = []struct {
12 hexIn string
13 b58 string
14}{
15 {"", ""},
16 {"61", "2g"},
17 {"626262", "a3gV"},
18 {"636363", "aPEr"},
19 {"73696d706c792061206c6f6e6720737472696e67", "2cFupjhnEsSn59qHXstmK2ffpLv2"},
20 {"516b6fcd0f", "ABnLTmg"},
21 {"bf4f89001e670274dd", "3SEo3LWLoPntC"},
22 {"572e4794", "3EFU7m"},
23 {"ecac89cad93923c02321", "EJDM8drfXA6uyA"},
24 {"10c8511e", "Rt5zm"},
25 {"00000000000000000000", "1111111111"},
26 {"00eb15231dfceb60925886b67d065299925915aeb172c06647", "1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L"},
27}
28
29func TestEncodeVectors(t *testing.T) {
30 for _, v := range vectors {
31 in, err := hex.DecodeString(v.hexIn)
32 uassert.NoError(t, err)
33 got := Encode(in)
34 uassert.Equal(t, v.b58, got)
35 }
36}
37
38func TestDecodeVectors(t *testing.T) {
39 for _, v := range vectors {
40 want, err := hex.DecodeString(v.hexIn)
41 uassert.NoError(t, err)
42 got := Decode(v.b58)
43 uassert.Equal(t, hex.EncodeToString(want), hex.EncodeToString(got))
44 }
45}
46
47func TestRoundTrip(t *testing.T) {
48 inputs := []string{
49 "",
50 "00",
51 "0000ff",
52 "deadbeef",
53 "68656c6c6f20776f726c64", // "hello world"
54 "ffffffffffffffff",
55 }
56 for _, hx := range inputs {
57 raw, err := hex.DecodeString(hx)
58 uassert.NoError(t, err)
59 back := Decode(Encode(raw))
60 uassert.Equal(t, hex.EncodeToString(raw), hex.EncodeToString(back))
61 }
62}
63
64func TestLeadingZeros(t *testing.T) {
65 uassert.Equal(t, "1", Encode([]byte{0}))
66 uassert.Equal(t, "11", Encode([]byte{0, 0}))
67 // One leading zero byte + a nonzero byte.
68 got := Decode("1" + Encode([]byte{0x2a}))
69 uassert.Equal(t, "002a", hex.EncodeToString(got))
70}
71
72func TestIsValid(t *testing.T) {
73 uassert.True(t, IsValid("2cFupjhnEsSn59qHXstmK2ffpLv2"))
74 uassert.True(t, IsValid("")) // empty is trivially valid
75 uassert.False(t, IsValid("0OIl")) // all four excluded characters
76 uassert.False(t, IsValid("abc?")) // punctuation not in alphabet
77}
78
79func TestDecodeInvalid(t *testing.T) {
80 // A '0' is not part of the Base58 alphabet → nil.
81 uassert.Equal(t, true, Decode("invalid0char") == nil)
82}