-
Notifications
You must be signed in to change notification settings - Fork 2
/
cpp_other_examples.cpp
136 lines (86 loc) · 4.04 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
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "txref.h"
#include <string>
#include <iostream>
// make sure we can check these examples even when building a release version
#undef NDEBUG
#include <cassert>
void encodingExamples() {
int blockHeight, transactionIndex, txoIndex;
std::string txref;
// mainnet, standard txref example
blockHeight = 10000;
transactionIndex = 2;
txref = txref::encode(blockHeight, transactionIndex);
std::cout << "mainnet, standard txref for (blockHeight = 10000, transactionIndex=2):" << std::endl;
std::cout << txref << "\n\n";
assert(txref == "tx1:rq3n-qqzq-qk8k-mzd");
// testnet, standard txref example
blockHeight = 10000;
transactionIndex = 4;
txref = txref::encodeTestnet(blockHeight, transactionIndex);
std::cout << "testnet, standard txref for (blockHeight = 10000, transactionIndex=4):" << std::endl;
std::cout << txref << "\n\n";
assert(txref == "txtest1:xq3n-qqyq-qhrg-gy3");
// mainnet, extended txref example
blockHeight = 10000;
transactionIndex = 2;
txoIndex = 3;
txref = txref::encode(blockHeight, transactionIndex, txoIndex);
std::cout << "mainnet, extended txref for (blockHeight = 10000, transactionIndex=2, txoIndex=3):" << std::endl;
std::cout << txref << "\n\n";
assert(txref == "tx1:yq3n-qqzq-qrqq-9z4d-2n");
// testnet, extended txref example
blockHeight = 10000;
transactionIndex = 4;
txoIndex = 6;
txref = txref::encodeTestnet(blockHeight, transactionIndex, txoIndex);
std::cout << "testnet, extended txref for (blockHeight = 10000, transactionIndex=4, txoIndex=6):" << std::endl;
std::cout << txref << "\n\n";
assert(txref == "txtest1:8q3n-qqyq-qxqq-v3x4-ze");
}
void decodingExamples() {
txref::DecodedResult decodedResult;
// mainnet, standard txref example
decodedResult = txref::decode("tx1:rq3n-qqzq-qk8k-mzd");
assert(decodedResult.hrp == txref::BECH32_HRP_MAIN);
assert(decodedResult.magicCode == txref::MAGIC_CODE_MAIN);
assert(decodedResult.blockHeight == 10000);
assert(decodedResult.transactionIndex == 2);
assert(decodedResult.encoding == txref::Encoding::Bech32m);
// testnet, standard txref example
decodedResult = txref::decode("txtest1:xq3n-qqyq-qhrg-gy3");
assert(decodedResult.hrp == txref::BECH32_HRP_TEST);
assert(decodedResult.magicCode == txref::MAGIC_CODE_TEST);
assert(decodedResult.blockHeight == 10000);
assert(decodedResult.transactionIndex == 4);
assert(decodedResult.encoding == txref::Encoding::Bech32m);
// mainnet, extended txref example
decodedResult = txref::decode("tx1:yq3n-qqzq-qrqq-9z4d-2n");
assert(decodedResult.hrp == "tx");
assert(decodedResult.magicCode == txref::MAGIC_CODE_MAIN_EXTENDED);
assert(decodedResult.blockHeight == 10000);
assert(decodedResult.transactionIndex == 2);
assert(decodedResult.txoIndex == 3);
assert(decodedResult.encoding == txref::Encoding::Bech32m);
// testnet, extended txref example
decodedResult = txref::decode("txtest1:8q3n-qqyq-qxqq-v3x4-ze");
assert(decodedResult.hrp == "txtest");
assert(decodedResult.magicCode == txref::MAGIC_CODE_TEST_EXTENDED);
assert(decodedResult.blockHeight == 10000);
assert(decodedResult.transactionIndex == 4);
assert(decodedResult.txoIndex == 6);
assert(decodedResult.encoding == txref::Encoding::Bech32m);
// example of decoding an "original" which used the old Bech32 encoding
decodedResult = txref::decode("txtest1:xjk0-uqay-zat0-dz8");
assert(decodedResult.hrp == "txtest");
assert(decodedResult.magicCode == txref::MAGIC_CODE_TEST);
assert(decodedResult.blockHeight == 466793);
assert(decodedResult.transactionIndex == 2205);
assert(decodedResult.txoIndex == 0);
assert(decodedResult.encoding == txref::Encoding::Bech32); // note: Bech32 rather than Bech32m
assert(decodedResult.commentary.find("txtest1:xjk0-uqay-zat0-dz8 uses an old encoding scheme and should be updated") != std::string::npos);
}
int main() {
encodingExamples();
decodingExamples();
}