-
Notifications
You must be signed in to change notification settings - Fork 5
/
cpp_other_examples.cpp
123 lines (98 loc) · 3.76 KB
/
cpp_other_examples.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "bech32.h"
#include <iostream>
// make sure we can check these examples even when building a release version
#undef NDEBUG
#include <cassert>
/**
* Illustrates encoding a "human readable part" and a "data part" to a bech32 string, then
* decoding that bech32 string and comparing the results.
*/
void encodeAndDecode() {
// human-readable part
std::string hrp = "example";
// data values can be 0-31
std::vector<unsigned char> data = {0, 1, 2, 3, 4, 5, 6, 7, 8};
// expected bech32 string output
char expected[] = "example1qpzry9x8ge8sqgv";
// encode
std::string bstr = bech32::encode(hrp, data);
assert(expected == bstr);
// decode
bech32::DecodedResult decodedResult = bech32::decode(bstr);
assert(hrp == decodedResult.hrp);
assert(data == decodedResult.dp);
assert(bech32::Encoding::Bech32m == decodedResult.encoding);
}
/**
* Illustrates decoding a possibly "dirty" bech32 string into its "human readable part" and
* "data part". Then encodes that data to another bech32 string and compares the results.
*/
void decodeAndEncode() {
// bech32 string with extra invalid characters
std::string bstr = " example1:qpz!r--y9#x8&%&%ge-8-sqgv ";
std::string expected = "example1qpzry9x8ge8sqgv";
// decode - make sure to strip invalid characters before trying to decode
bech32::DecodedResult decodedResult = bech32::decode(bech32::stripUnknownChars(bstr));
// verify decoding
assert(!decodedResult.hrp.empty() && !decodedResult.dp.empty());
assert(bech32::Encoding::Bech32m == decodedResult.encoding);
// encode
bstr = bech32::encode(decodedResult.hrp, decodedResult.dp);
// encoding of "cleaned" decoded data should match expected string
assert(bstr == expected);
}
/**
* Illustrates exception thrown when attempting to encode bad data--in this case, "33" is not in the
* range of allowed data values (0-31).
*/
void badEncoding() {
// hrp and data to encode
std::string hrp = "example";
std::vector<unsigned char> data = {0, 1, 2, 3, 4, 5, 6, 7, 33};
// encode
try {
std::string bstr = bech32::encode(hrp, data);
}
catch (std::exception &e) {
assert(std::string(e.what()) == "data value is out of range");
}
}
/**
* Illustrates failure when attempting to decode bad data--in this case, the "z" character from
* the bech32 string has been corrupted and changed to a "x". This will cause the checksum
* verification to fail and an "Invalid" encoding to be returned.
*/
void badDecoding_corruptData() {
// valid bech32 string
std::string bstr = "example1qpzry9x8ge8sqgv";
// simulate corrupted data--checksum verification will fail
bstr[10] = 'x';
// decode
bech32::DecodedResult decodedResult = bech32::decode(bstr);
// verify decoding failed
assert(decodedResult.hrp.empty() && decodedResult.dp.empty());
assert(bech32::Encoding::Invalid == decodedResult.encoding);
}
/**
* Illustrates failure when attempting to decode bad data--in this case, the "s" character from
* the bech32 string's checksum has been corrupted and changed to a "q". This will cause the
* checksum verification to fail and an "Invalid" encoding to be returned.
*/
void badDecoding_corruptChecksum() {
// valid bech32 string
std::string bstr = "example1qpzry9x8ge8sqgv";
// simulate corrupted checksum--verification will fail
bstr[19] = 'q';
// decode
bech32::DecodedResult decodedResult = bech32::decode(bstr);
// verify decoding failed
assert(decodedResult.hrp.empty() && decodedResult.dp.empty());
assert(bech32::Encoding::Invalid == decodedResult.encoding);
}
int main() {
encodeAndDecode();
decodeAndEncode();
badEncoding();
badDecoding_corruptData();
badDecoding_corruptChecksum();
}