-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcoinjoin.cpp
188 lines (131 loc) · 4.92 KB
/
coinjoin.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
#include <bitcoin/bitcoin.hpp>
#include <bitcoin/client.hpp>
using namespace bc;
using namespace bc::wallet;
using namespace bc::machine;
using namespace bc::chain;
points_value getUTXOs(payment_address Addy, uint64_t amount)
{
client::connection_type connection = {};
connection.retries = 3;
connection.timeout_seconds = 8;
connection.server = config::endpoint("tcp://testnet1.libbitcoin.net:19091");
client::obelisk_client client(connection);
points_value val1;
static const auto on_done = [&val1](const points_value& vals) {
std::cout << "Success: " << vals.value() << std::endl;
val1 = vals;
};
static const auto on_error = [](const code& ec) {
std::cout << "Error Code: " << ec.message() << std::endl;
};
if(!client.connect(connection))
{
std::cout << "Fail" << std::endl;
} else {
std::cout << "Connection Succeeded" << std::endl;
}
client.blockchain_fetch_unspent_outputs(on_error, on_done, Addy, amount, select_outputs::algorithm::greedy);
client.wait();
//return allPoints;
return val1;
}
transaction sigScript(ec_private wal, transaction tx)
{
int index = 0;
for (auto input: tx.inputs())
{
if(input.script() == script(script().to_pay_key_hash_pattern(wal.to_payment_address().hash())))
{
endorsement sig;
script().create_endorsement(sig, wal.secret(), input.script(), tx, index, all_anyone_can_pay);
operation::list ops {operation(sig), operation(to_chunk(wal.to_public().point()))};
script scriptSig(ops);
input.script().clear();
input.set_script(scriptSig);
tx.inputs()[index] = input;
}
index++;
}
return tx;
}
void broadcastTX(transaction tx)
{
client::connection_type connection = {};
connection.retries = 3;
connection.timeout_seconds = 8;
connection.server = config::endpoint("tcp://testnet1.libbitcoin.net:19091");
client::obelisk_client client(connection);
if(!client.connect(connection))
{
std::cout << "Fail" << std::endl;
} else {
std::cout << "Connection Succeeded" << std::endl;
}
static const auto on_done = [](const code& ec) {
std::cout << "Success: " << ec.message() << std::endl;
};
static const auto on_error2 = [](const code& ec) {
std::cout << "Error Code: " << ec.message() << std::endl;
};
client.transaction_pool_broadcast(on_error2, on_done, tx);
client.wait();
}
transaction makeInput(payment_address Addy, transaction tx, uint64_t amount)
{
points_value UTXOs = getUTXOs(Addy, amount);
input::list inputs {};
for (auto utxo: UTXOs.points)
{
input workingInput = input();
workingInput.set_previous_output(output_point(utxo));
std::cout << encode_base16(utxo.hash()) << std::endl;
workingInput.set_script(script(script().to_pay_key_hash_pattern(Addy.hash())));
workingInput.set_sequence(0xffffffff);
inputs.push_back(workingInput);
}
uint64_t change = UTXOs.value() - amount - 1000;
script outScript = script(script().to_pay_key_hash_pattern(Addy.hash()));
std::cout << "change: "<< change << std::endl;
tx.outputs().push_back(output(change, outScript));
extend_data(tx.inputs(), inputs);
return tx;
}
// output makeChange(uint64_t total, uint64_t amount, payment_address Addy)
// {
// uint64_t change = amount - total - 1000;
// script outScript = script(script().to_pay_key_hash_pattern(Addy.hash()));
// std::cout << "change: "<< change << std::endl;
// return output(change, outScript);
// }
output::list makeOuputs(uint64_t amount, payment_address address1, payment_address address2, payment_address address3)
{
output output1(amount, script(script().to_pay_key_hash_pattern(address1.hash())));
output output2(amount, script(script().to_pay_key_hash_pattern(address2.hash())));
output output3(amount, script(script().to_pay_key_hash_pattern(address3.hash())));
return output::list {output1, output2, output3};
}
int main()
{
ec_private wal1(bitcoin_hash(decode_mnemonic(split(""))),0xEF6F, 1);
ec_private wal2(bitcoin_hash(decode_mnemonic(split(""))),0xEF6F, 1);
ec_private wal3(bitcoin_hash(decode_mnemonic(split(""))),0xEF6F, 1);
std::cout << wal1.to_payment_address() << std::endl;
std::cout << wal2.to_payment_address() << std::endl;
std::cout << wal3.to_payment_address() << std::endl;
payment_address destination1("n2ge1S4bLDvJKx8AGXrK5JHY2D5cReVytu");
payment_address destination2("mnrnjVFimDFrNkszzMtecr4yrMKmEuMRbv");
payment_address destination3("n2BPFTRKLtM6VQGN6tCWCaEbBuFTEo5P6r");
transaction coinJoin = transaction();
uint64_t amount = 100000000;
output::list outputs = makeOuputs(amount, destination1, destination2, destination3);
coinJoin.set_outputs(outputs);
coinJoin = makeInput(wal1.to_payment_address(), coinJoin, amount);
coinJoin = makeInput(wal2.to_payment_address(), coinJoin, amount);
coinJoin = makeInput(wal3.to_payment_address(), coinJoin, amount);
coinJoin = sigScript(wal1, coinJoin);
coinJoin = sigScript(wal2, coinJoin);
coinJoin = sigScript(wal3, coinJoin);
std::cout << "\n" << encode_base16(coinJoin.to_data(1)) << std::endl;
broadcastTX(coinJoin);
}