-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdispenser.html
269 lines (240 loc) · 11.2 KB
/
dispenser.html
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Create a Counterparty Dispenser w/ Electrum</title>
<link rel="stylesheet" href="css/main.css">
<script src="db/asset_longnames.js"></script>
<script src="db/assets_div.js"></script>
<script src="db/assets_indiv.js"></script>
<script src="js/xcp/hex.js"></script>
<script src="js/xcp/assets.js"></script>
<script src="js/xcp/build_tx_msg.js"></script>
<script src="js/xcp/decode_tx_msg.js"></script>
<script src="js/xcp/display.js"></script>
<script src="js/xcp/rc4.js"></script>
<script src="js/xcp/validate_address.js"></script>
<script src="js/lib/bitcoinjs-lib.js"></script>
<script src="js/lib/bitcoinjs-message.js"></script>
<script src="js/lib/jquery-3.4.1.min.js"></script>
<script src="js/lib/lozad.min.js"></script>
<script src="js/lib/sha256.js"></script>
<script src="js/lib/btc.js"></script>
<script>
let addr = '';
let asset = '';
let longname = '';
let utxo = '';
let div = '';
let max = '';
let network = 'bitcoin';
function get_url_parameters() {
//Get url parameter 'address'
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
if (urlParams.has('address')) {
addr = urlParams.get('address');
}
if (urlParams.has('asset')) {
asset = urlParams.get('asset');
document.getElementById('input_asset').value = asset;
}
if (urlParams.has('longname')) {
longname = urlParams.get('longname');
}
if (urlParams.has('utxo')) {
utxo = urlParams.get('utxo');
document.getElementById('input_utxo').value = utxo;
document.getElementById('div_utxo').style.display = 'none';
}
if (urlParams.has('div')) {
div = urlParams.get('div');
if (div == 'div') {
document.getElementById('input_divisibility').selectedIndex = 1;
document.getElementById('div_divisibility').style.display = 'none';
}
if (div == 'indiv') {
document.getElementById('input_divisibility').selectedIndex = 2;
document.getElementById('div_divisibility').style.display = 'none';
}
}
if (urlParams.has('max') && urlParams.has('div')) {
max = urlParams.get('max');
}
if (urlParams.has('network')) {
network = urlParams.get('network');
}
}
function display() {
let header = wallet_header;
header = header.replace('{address}', addr_short(addr));
if (utxo != '') {
header = header.replace('{utxo}', '<span onclick="document.getElementById(\'div_utxo\').style.display=\'block\';">UTXO '+utxo.slice(0,4)+'</span>');
} else{
header =header.replace('{utxo}', '');
}
document.getElementById('html_top').innerHTML = header;
show_alt_name();
}
function show_alt_name() {
document.getElementById('alt_name').innerHTML = alt_name(document.getElementById('input_asset').value);
show_max_amount();
}
function show_divisibility() {
let current_asset = document.getElementById('input_asset').value;
let div = get_divisibility(current_asset);
if (div == 'unknown') {
document.getElementById('input_divisibility').value = 'select';
document.getElementById('div_divisibility').style.display = 'block';
}
if (div == 'div') {
document.getElementById('input_divisibility').value = 'div';
document.getElementById('div_divisibility').style.display = 'none';
}
if (div == 'indiv') {
document.getElementById('input_divisibility').value = 'indiv';
document.getElementById('div_divisibility').style.display = 'none';
}
}
function show_max_amount() {
let max_message = '';
let show_max = true;
if (max == '') show_max = false;
if (asset != document.getElementById('input_asset').value) show_max = false;
if (div != 'div' && div != 'indiv') show_max = false;
if (show_max) max_message = 'Max ' + sat_to_displayx(max, div, '');
document.getElementById('max_amount').innerHTML = max_message;
}
function prepare_inputs() {
let inp_utxo = document.getElementById('input_utxo').value; //eg a1870614c4cd1b3e20f98316f062c8622313eca4bbeb3f7bb695a61c164a45e7
let inp_asset = document.getElementById('input_asset').value; //eg FLDC
let inp_escrow = document.getElementById('input_escrow').value; //eg 5843.57
let inp_give = document.getElementById('input_give').value;
let inp_btc = document.getElementById('input_btc').value;
let inp_divisibility = document.getElementById('input_divisibility').value; //'div' or 'indiv'
let inp_address = document.getElementById('input_address').value; //eg bc1qqqj54caf4cusvycr4dmzv24pgh875xeg8d5wrq
let inp_tip_btc = document.getElementById('input_tip').value;
//Inputs formatted ok?
inp_utxo = inp_utxo.substring(0,64);
inp_utxo = inp_utxo.toLowerCase();
if (inp_utxo.length != 64) {
print_result("UTXO is not valid"); return;
}
if (valid_name(inp_asset) == false) {
print_result("Asset name is not valid"); return;
}
if (isNaN(inp_escrow)) {
print_result("Escrow amount is not a number"); return;
}
if (isNaN(inp_give)) {
print_result("Give amount is not a number"); return;
}
if (isNaN(inp_btc)) {
print_result("BTC amount is not a number"); return;
}
if (inp_address != '' && valid_btc_addr(inp_address, network) == false) {
print_result("Dispenser is not a valid Bitcoin address"); return;
}
if (inp_address != '' && valid_xcp_addr(inp_address, network) == false) {
print_result("Dispenser's address is not supported by Counterparty"); return;
}
if (isNaN(inp_tip_btc)) {
print_result("Tip is not a number"); return;
}
let out = ' ';
//Check divisibility
let div = get_divisibility(inp_asset);
if (div == 'unknown') {
out += 'Warning: Confirm that divisibility is correct on {xchain} and {xcpdev}.'
} else if (div != inp_divisibility) {
print_result("Wrong divisibility"); return;
}
//Convert longname. If not possible return error.
if (inp_asset.includes('.')) {
inp_asset = longname_to_asset(inp_asset);
if (inp_asset == '') {
out += "Please input subasset's numeric name. See {xchain} and {xcpdev}.";
}
}
out += 'Make sure the first input coin is ' + addr_short(inp_utxo, 3, 2);
let amount_escrow = display_to_sat(inp_escrow, inp_divisibility);
let amount_give = display_to_sat(inp_give, inp_divisibility);
let amount_btc = display_to_sat(inp_btc, 'div');
let opreturn_unencoded = msg_create_dispenser(inp_asset, amount_escrow, amount_give, amount_btc, inp_address);
let opreturn = rc4_hex(inp_utxo, opreturn_unencoded);
console.log(opreturn);
//Sanity checks. Does decoded tx match inputs?
let info = decode_tx(opreturn, inp_utxo);
if (info.prefix != 'CNTRPRTY') {
print_result("Op_return encoding error. Wrong prefix."); return;
}
if (info.msg_id != 12) {
print_result("Op_return encoding error. Wrong messge type."); return;
}
if (info.prefix != 'CNTRPRTY') {
print_result("Op_return encoding error. Wrong prefix."); return;
}
if (info.asset_id != asset_id(inp_asset)) {
print_result("Op_return encoding error. Wrong asset id."); return;
}
if (Number(info.escrow_amount) != Number(amount_escrow)) {
print_result("Op_return encoding error. Wrong amount."); return;
}
if (Number(info.give_amount) != Number(amount_give)) {
print_result("Op_return encoding error. Wrong amount."); return;
}
if (Number(info.btc_amount) != Number(amount_btc)) {
print_result("Op_return encoding error. Wrong amount."); return;
}
if (info.hasOwnProperty('address') && info.address != inp_address) {
print_result("Op_return encoding error. Wrong recipient address."); return;
}
//Output messages and code
let code = 'OP_RETURN ' + opreturn + ',0' + tip_line(inp_tip_btc);
let url_xchain = 'https://xchain.io/asset/' + inp_asset
let url_xcpdev = 'https://www.xcp.dev/asset/' + inp_asset
out = out.replaceAll('{xchain}', '<a href="' + url_xchain + '">Xchain</a>');
out = out.replaceAll('{xcpdev}', '<a href="' + url_xcpdev + '">Xcp.dev</a>');
print_result(out, code)
}
function print_result(message = '', code = '') {
document.getElementById('text_output').innerHTML = '<br>' + message;
document.getElementById('code_output').innerHTML = code;
}
</script>
</head>
<body onload="get_url_parameters();display();">
<div id="content">
<div id="html_top"></div>
<h1>Create Dispenser</h1>
<div id="div_utxo">UTXO (Input Coin)<br>
<input id="input_utxo"><br><br></div>
<div id="div_asset">Asset<span id="alt_name" style="float:right"></span><br>
<input id="input_asset" oninput="show_alt_name();" onchange="show_divisibility();"><br><br></div>
<div id="div_escrow">Amount to Escrow<span id="max_amount" style="float:right">Max</span><br>
<input id="input_escrow"><br><br></div>
<div id="div_give">Amount to Give per Dispense<br>
<input id="input_give"><br><br></div>
<div id="div_divisibility">Divisible<br>
<select name="select_divisibility" id="input_divisibility">
<option value="select">Select</option>
<option value="div">True (Fractional tokens)</option>
<option value="indiv">False (Whole tokens)</option>
</select><br><br></div>
<div id="div_btc">BTC to Charge per Dispense<br>
<input id="input_btc"><br><br></div>
<div id="div_address">Optional Dispenser Address<br>
<input id="input_address"><br><br></div>
<div id="div_tip">Optional Tip to Developer (BTC)<span id="suggested_tip" style="float:right" onclick="document.getElementById('input_tip').value='0.0001';">Suggested: 0.0001</span><br>
<input id="input_tip"><br></div>
<button type="button" id="generate" onclick="prepare_inputs();" >Generate OP_RETURN</button><br><br>
<i><span style="font-size:75%">This is an experimental script. Peer review pending. Your tokens may get lost. Use at own risk!</span></i><br>
<span id="text_output"> </span>
<textarea id="code_output" rows="2"></textarea>
<button type="button" id="copy" onclick="document.getElementById('code_output').select();document.execCommand('copy');">Copy</button><br><br>
</div>
</body>
<script>
document.getElementById('input_tip').value = tip_btc;
</script>
</html>