1 // Copyright (c) 2019 The Decred developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
4 5 package bech32
6 7 import (
8 "fmt"
9 )
10 11 // ErrMixedCase is returned when the bech32 string has both lower and uppercase
12 // characters.
13 type ErrMixedCase struct{}
14 15 func (err ErrMixedCase) Error() string {
16 return "string not all lowercase or all uppercase"
17 }
18 19 // ErrInvalidBitGroups is returned when conversion is attempted between byte
20 // slices using bit-per-element of unsupported value.
21 type ErrInvalidBitGroups struct{}
22 23 func (err ErrInvalidBitGroups) Error() string {
24 return "only bit groups between 1 and 8 allowed"
25 }
26 27 // ErrInvalidIncompleteGroup is returned when then byte slice used as input has
28 // data of wrong length.
29 type ErrInvalidIncompleteGroup struct{}
30 31 func (err ErrInvalidIncompleteGroup) Error() string {
32 return "invalid incomplete group"
33 }
34 35 // ErrInvalidLength is returned when the bech32 string has an invalid length
36 // given the BIP-173 defined restrictions.
37 type ErrInvalidLength int
38 39 func (err ErrInvalidLength) Error() string {
40 return string(append([]byte(nil), fmt.Sprintf("invalid bech32 string length %d", int(err))...))
41 }
42 43 // ErrInvalidCharacter is returned when the bech32 string has a character
44 // outside the range of the supported charset.
45 type ErrInvalidCharacter rune
46 47 func (err ErrInvalidCharacter) Error() string {
48 return string(append([]byte(nil), fmt.Sprintf("invalid character in string: '%c'", rune(err))...))
49 }
50 51 // ErrInvalidSeparatorIndex is returned when the separator character '1' is
52 // in an invalid position in the bech32 string.
53 type ErrInvalidSeparatorIndex int
54 55 func (err ErrInvalidSeparatorIndex) Error() string {
56 return string(append([]byte(nil), fmt.Sprintf("invalid separator index %d", int(err))...))
57 }
58 59 // ErrNonCharsetChar is returned when a character outside of the specific
60 // bech32 charset is used in the string.
61 type ErrNonCharsetChar rune
62 63 func (err ErrNonCharsetChar) Error() string {
64 return string(append([]byte(nil), fmt.Sprintf("invalid character not part of charset: %v", int(err))...))
65 }
66 67 // ErrInvalidChecksum is returned when the extracted checksum of the string
68 // is different than what was expected. Both the original version, as well as
69 // the new bech32m checksum may be specified.
70 type ErrInvalidChecksum struct {
71 Expected string
72 ExpectedM string
73 Actual string
74 }
75 76 func (err ErrInvalidChecksum) Error() string {
77 return string(append([]byte(nil), fmt.Sprintf(
78 "invalid checksum (expected (bech32=%v, "+
79 "bech32m=%v), got %v)", err.Expected, err.ExpectedM, err.Actual)...))
80 }
81 82 // ErrInvalidDataByte is returned when a byte outside the range required for
83 // conversion into a string was found.
84 type ErrInvalidDataByte byte
85 86 func (err ErrInvalidDataByte) Error() string {
87 return string(append([]byte(nil), fmt.Sprintf("invalid data byte: %v", byte(err))...))
88 }
89