-
Notifications
You must be signed in to change notification settings - Fork 1
/
bip21.hpp
112 lines (90 loc) · 3.08 KB
/
bip21.hpp
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
#include <cctype>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <iostream>
#include <optional>
namespace sk::bip21 {
static auto split(const std::string& str, const char delimiter) -> std::vector<std::string> {
std::vector<std::string> internal;
std::stringstream ss(str);
std::string tok;
while (getline(ss, tok, delimiter)) {
internal.push_back(tok);
}
return internal;
}
static void tolower(std::string& s) {
for(auto& c: s) {
std::tolower(c);
}
}
using Component = std::vector<std::pair<std::string, std::string>>;
struct Bip21 {
std::string address;
Component options;
friend std::ostream& operator<<(std::ostream& os, const Bip21& b) {
os << "Address ==> " << b.address << "\n";
for(auto& v: b.options){
std::cout << v.first << " ==> " << v.second << "\n";
}
return os;
}
};
using Error = std::string;
using Result = std::pair<Bip21, Error>;
static auto decode(const std::string& uri, std::optional<std::string> customScheme) -> Result {
std::string schemeToUse = "bitcoin:";
if (customScheme) {
schemeToUse = customScheme.value();
}
auto it = std::find_if(uri.begin(), uri.end(), [](char c){
return c == ':';
});
std::string schemeComponent{uri.begin(), it};
tolower(schemeComponent);
auto queryStrIt = std::find_if(uri.begin(), uri.end(), [](char c){
return c == '?';
});
std::string base58Addr{uri.begin() + schemeToUse.size(), uri.end()};
if (queryStrIt == uri.end()) {
return {Bip21{base58Addr, {}}, {}};
}
Bip21 res;
res.address = std::move(base58Addr);
std::string params{std::next(queryStrIt), uri.end()};
auto v = split(params, '&');
for(auto& e: v) {
auto c = split(e, '=');
res.options.push_back({c[0], c[1]});
}
return {res, {}};
}
static auto decode(const std::string& uri) -> Result {
return decode(uri, std::nullopt);
}
static auto encode(const std::string& addr, std::optional<std::string> scheme,
std::optional<Component> components) -> std::string {
if (!scheme) {
std::string r{"bitcoin:" + addr};
if (components) {
ushort i{0};
r += "?";
for (const auto& v: components.value()) {
i++ == 0? r += v.first + "=" + v.second : r += "&"+v.first + "=" + v.second;
}
}
return r;
}
std::string r{scheme.value() + addr};
if (components) {
ushort i{0};
r += "?";
for (const auto& v: components.value()) {
i++ == 0? r += v.first + "=" + v.second : r += "&"+v.first + "=" + v.second;
}
}
return r;
}
} //namespace sk::bip21