-
Notifications
You must be signed in to change notification settings - Fork 31
/
dev-boltz.cpp
203 lines (185 loc) · 4.97 KB
/
dev-boltz.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include"Bitcoin/Tx.hpp"
#include"Boltz/Connection.hpp"
#include"Boltz/EnvIF.hpp"
#include"Boltz/Service.hpp"
#include"Boltz/ServiceFactory.hpp"
#include"Boltz/SwapInfo.hpp"
#include"Ev/Io.hpp"
#include"Ev/ThreadPool.hpp"
#include"Ev/start.hpp"
#include"Jsmn/Object.hpp"
#include"Json/Out.hpp"
#include"Ln/Amount.hpp"
#include"Secp256k1/PrivKey.hpp"
#include"Secp256k1/PubKey.hpp"
#include"Secp256k1/Signature.hpp"
#include"Secp256k1/SignerIF.hpp"
#include"Sha256/Hash.hpp"
#include"Sha256/Hasher.hpp"
#include"Sqlite3/Db.hpp"
#include"Util/make_unique.hpp"
#include<algorithm>
#include<basicsecure.h>
#include<iomanip>
#include<iostream>
#include<iterator>
#include<sstream>
#include<string>
#include<vector>
namespace {
class Env : public Boltz::EnvIF {
private:
Boltz::Connection conn;
public:
explicit
Env(Ev::ThreadPool& threadpool) : conn(threadpool) { }
Ev::Io<void> logd(std::string msg) override {
return Ev::lift().then([msg]() {
std::cout << "DEBUG " << msg << std::endl;
return Ev::lift();
});
}
Ev::Io<void> loge(std::string msg) override {
return Ev::lift().then([msg]() {
std::cout << "ERRROR " << msg << std::endl;
return Ev::lift();
});
}
Ev::Io<std::uint32_t> get_feerate() override {
return conn.api("/getfeeestimation", nullptr
).then([](Jsmn::Object res) {
auto perb = (double) res["BTC"];
auto perkw = std::uint32_t(perb * 1000 / 4);
return Ev::lift(perkw);
});
}
Ev::Io<bool> broadcast_tx(Bitcoin::Tx tx) override {
auto str = std::string(tx);
auto params = Json::Out()
.start_object()
.field("currency", "BTC")
.field("transactionHex", str)
.end_object()
;
return conn.api( "/broadcasttransaction"
, Util::make_unique<Json::Out>(
std::move(params)
)
).then([this](Jsmn::Object res) {
if (res.has("error")) {
return loge( std::string("broadcast_tx: ")
+ std::string(res["error"])
).then([]() {
return Ev::lift(false);
});
}
if (res.has("transactionId")) {
return Ev::lift(true);
}
return Ev::lift(false);
});
}
};
class Signer : public Secp256k1::SignerIF {
private:
Secp256k1::PrivKey sk;
public:
/* Yeah, everyone totally knows the below private key, so insecure. */
Signer() : sk("54e28e74151d6b0f30453288d6842f240820dc7553afb424d4f68ae57eef28fa") { }
Secp256k1::PubKey
get_pubkey_tweak(Secp256k1::PrivKey const& tweak) override {
return Secp256k1::PubKey(tweak * sk);
}
Secp256k1::Signature
get_signature_tweak( Secp256k1::PrivKey const& tweak
, Sha256::Hash const& m
) override {
return Secp256k1::Signature::create
( tweak * sk
, m
);
}
Sha256::Hash
get_privkey_salted_hash(std::uint8_t salt[32]) override {
auto hasher = Sha256::Hasher();
hasher.feed(salt, 32);
std::uint8_t buf[32];
sk.to_buffer(buf);
hasher.feed(buf, 32);
basicsecure_clear(buf, sizeof(buf));
return std::move(hasher).finalize();
}
};
std::uint64_t to_number(std::string const& s) {
auto is = std::istringstream(s);
auto rv = std::uint64_t();
is >> std::dec >> rv;
return rv;
}
}
int main(int argc, char** c_argv) {
auto argv = std::vector<std::string>();
std::copy( c_argv, c_argv + argc
, std::back_inserter(argv)
);
if (argv.size() < 3) {
std::cout << "Need argument:" << std::endl
<< " get-quotation $satoshis" << std::endl
<< " on-block $currentblockheight" << std::endl
<< " swap $satoshis $addr $currentblockheight" << std::endl
;
return 0;
}
Ev::ThreadPool threadpool;
auto db = Sqlite3::Db("data.dev-boltz");
auto signer = Signer();
auto env = Env(threadpool);
auto factory = Boltz::ServiceFactory
( threadpool
, db
, signer
, env
);
auto service = std::shared_ptr<Boltz::Service>();
auto code = Ev::lift().then([&]() {
/* Create service. */
return factory.create_service("https://boltz.exchange/api");
}).then([&](std::unique_ptr<Boltz::Service> n_service) {
service = std::move(n_service);
if (argv[1] == "get-quotation") {
auto num = to_number(argv[2]);
auto value = Ln::Amount::sat(num);
return service->get_quotation(value
).then([](std::unique_ptr<Ln::Amount> fee) {
if (fee)
std::cout << fee->to_sat()
<< std::endl
;
return Ev::lift(0);
});
} else if (argv[1] == "on-block") {
auto num = to_number(argv[2]);
return service->on_block(num).then([]() {
return Ev::lift(0);
});
} else if (argv[1] == "swap" && argv.size() >= 5) {
auto num = to_number(argv[2]);
auto value = Ln::Amount::sat(num);
auto addr = argv[3];
auto blockheight = std::uint32_t(to_number(argv[4]));
return service->swap(value, addr, blockheight
).then([](Boltz::SwapInfo result) {
auto invoice = result.invoice;
auto hash = result.hash;
auto timeout = result.timeout;
std::cout << invoice << std::endl;
std::cout << hash << std::endl;
std::cout << timeout << std::endl;
return Ev::lift(0);
});
}
std::cerr << "argument error" << std::endl;
return Ev::lift(1);
});
return Ev::start(code);
}