-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsweep.html
177 lines (154 loc) · 7.08 KB
/
sweep.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sweep Counterparty Address 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 utxo = '';
let network = 'bitcoin';
let xcp_balance = '';
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('utxo')) {
utxo = urlParams.get('utxo');
document.getElementById('input_utxo').value = utxo;
document.getElementById('div_utxo').style.display = 'none';
}
if (urlParams.has('xcp')) {
xcp_balance = urlParams.get('xcp');
}
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;
//Error message if insufficient XCP
if (xcp_balance != '' && xcp_balance < 5e7) { //0.5 xcp sweep fee
document.getElementById('content').innerHTML = addr + ' holds ' + balance_format(xcp_balance, 'div') + ' XCP.<br><br>This is insufficient for paying the 0.5 XCP sweep fee.';
}
}
function prepare_inputs() {
let inp_utxo = document.getElementById('input_utxo').value; //eg a1870614c4cd1b3e20f98316f062c8622313eca4bbeb3f7bb695a61c164a45e7
let inp_sweep_type = document.getElementById('input_sweep_type').value;
let inp_recipient = document.getElementById('input_recipient').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_btc_addr(inp_recipient, network) == false) {
print_result("Recipient is not a valid Bitcoin address"); return;
}
if (valid_xcp_addr(inp_recipient, network) == false) {
print_result("Recipient's address is not supported by Counterparty"); return;
}
if (isNaN(inp_tip_btc)) {
print_result("Tip is not a number"); return;
}
let out = ' ';
out += 'Make sure the first input coin is ' + addr_short(inp_utxo, 3, 2);
if (xcp_balance == '') {
out += '<br>and that your <a href="{xchain_addr}">XCP balance</a> is at least 0.5 XCP to cover the sweep fee.';
}
if (addr == '') {
out = out. replace('{xchain_addr}', 'https://xchain.io/');
} else {
out = out. replace('{xchain_addr}', 'https://xchain.io/address/'+addr);
}
let sweep_flag = 0;
if (inp_sweep_type == 'tokens') sweep_flag = 1;
if (inp_sweep_type == 'assets') sweep_flag = 2;
if (inp_sweep_type == 'everything') sweep_flag = 3;
let opreturn_unencoded = msg_sweep(inp_recipient, sweep_flag);
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 != 4) {
print_result("Op_return encoding error. Wrong messge type."); return;
}
if (info.address != inp_recipient) {
console.log(info.address);
console.log(inp_recipient);
print_result("Op_return encoding error. Wrong recipient."); return;
}
if (info.sweep_type != inp_sweep_type) {
print_result("Op_return encoding error. Wrong sweep type."); return;
}
if (info.sweep_flag != sweep_flag) {
print_result("Op_return encoding error. Wrong sweep flag."); return;
}
//Output messages and code
let code = 'OP_RETURN ' + opreturn + ',0' + tip_line(inp_tip_btc);
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>Sweep Address</h1>
<div id="div_utxo">UTXO (Input Coin)<br>
<input id="input_utxo"><br><br></div>
<div id="div_sweep_type">Sweep Type<br>
<select name="sweep_type" id="input_sweep_type">
<option value="select">Select</option>
<option value="tokens">Token Balances</option>
<option value="assets">Asset Ownerships</option>
<option value="everything">Everything (Assets and Tokens)</option>
</select><br><br></div>
<div id="div_recipient">Recipient Address<br>
<input id="input_recipient"><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>