-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.html
289 lines (240 loc) · 10.6 KB
/
index.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<html>
<head>
<style>
body {
font-family: 'Courier New', sans-serif !important;
background-color: #000 !important;
color: #0f0 !important;
}
hr {
background-color: #fff;
}
p {
margin-bottom: 0px !important;
}
</style>
<link rel="stylesheet" href="./node_modules/bootstrap/dist/css/bootstrap.min.css" crossorigin="anonymous">
<script src="./node_modules/jquery/dist/jquery.min.js"></script>
<script src="./node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="./node_modules/secure-random/lib/secure-random.js"></script>
<script src="./node_modules/base-58/Base58.js"></script>
<script src="./node_modules/crypto-js/crypto-js.js"></script>
<script src="./node_modules/@waves/waves-api/dist/waves-api.min.js"></script>
<script src="./node_modules/web3/dist/web3.min.js"></script>
<script src="./dist/thridparties/bip39.min.js"></script>
<script src="./dist/thridparties/request-promise.min.js"></script>
<script src="./dist/thridparties/ethereumjs-tx/ethereumjs-tx-1.3.3.min.js"></script>
<script src="./dist/thridparties/ethereumjs-wallet/ethereumjs-wallet-0.6.0.min.js"></script>
<script src="./dist/thridparties/ethereumjs-wallet-hd/ethereumjs-wallet-hd-0.6.0.min.js"></script>
<script src="./dist/waves.js"></script>
<script src="./dist/ether.js"></script>
<script src="./dist/app.js"></script>
</head>
<body>
<div class="container">
running on testnet - <strong>use browser console for debug</strong>
<div id="initialize">
<select name="send" class="form-control">
<option value="ETH">Send Ether</option>
<option value="WAVES">Send Waves</option>
</select>
<select name="receive" class="form-control">
<option value="WAVES">Receive Waves</option>
<option value="ETH">Receive Ether</option>
</select>
<select name="party" class="form-control">
<option value="initiator">Initiate</option>
<option value="participant">Participate</option>
</select>
<button name="btnStart" class="form-control">Go</button>
</div>
<div id="initiateDiv" style="display: none">
<input name="walletMnemonic" class="form-control"/>
<input name="counterPartyAddress" class="form-control"/>
<input name="amount" class="form-control"/>
<button name="btnInitiate" class="form-control">Create contract</button>
</div>
<div id="participateDiv" style="display: none">
<input name="walletMnemonic" class="form-control"/>
<input name="counterPartyAddress" class="form-control"/>
<input name="initiatorContractTxId" class="form-control"/>
<input name="amount" class="form-control"/>
<button name="btnParticipate" class="form-control">Create contract</button>
</div>
<div id="initiatorRedeemDiv" style="display: none">
<input name="walletMnemonic" class="form-control"/>
<input name="secret" class="form-control"/>
<input name="counterPartyContractTxId" class="form-control"/>
<button name="btnInitiatorRedeem" class="form-control">Redeem</button>
<button name="btnRefund" class="form-control">Refund</button>
</div>
<div id="participantRedeemDiv" style="display: none">
<input name="walletMnemonic" class="form-control"/>
<input name="counterPartyRedeemTxId" class="form-control"/>
<!--
<input name="counterPartyContractTxId" />
-->
<button name="btnParticipantRedeem" class="form-control">Redeem</button>
<button name="btnRefund" class="form-control">Refund</button>
</div>
<br/>
<div id="logDiv">
</div>
</div>
</body>
<script>
$(function() {
const app = new App();
var send = null;
var receive = null;
var party = null;
var initiation = null;
var participation = null;
var initiatorRedeem = null;
var participantRedeem = null;
var wallets = [];
$("[name='btnInitiate']").click(async function(){
let div = $("#initiateDiv");
let mnemonic = div.find("[name='walletMnemonic']").val();
let participantAddress = div.find("[name='counterPartyAddress']").val();
let amount = div.find("[name='amount']").val();
wallets[send] = app.getAccount(send, mnemonic);
initiation = await app.initiate(send, wallets[send], participantAddress, amount);
log('<hr/>');
log('contract txid: ' + makeTxLink(send, initiation.txid) + ' (send to counter party)');
log('secret hash: ' + initiation.secretHash);
log('secret: ' + initiation.secret + ' (keep it safe for now)');
log('contract successfully created!');
prepareAndShowRedeemDiv();
div.hide();
});
$("[name='btnParticipate']").click(async function(){
let div = $("#participateDiv");
let inititationTxId = div.find("[name='initiatorContractTxId']").val();
let mnemonic = div.find("[name='walletMnemonic']").val();
let initiatorAddress = div.find("[name='counterPartyAddress']").val();
let amount = div.find("[name='amount']").val();
let initiationContractInfo = await app.getContractTxInfo(receive, inititationTxId);
wallets[send] = app.getAccount(send, mnemonic);
let secretHash = initiationContractInfo.secretHash;
participation = await app.participate(send, wallets[send], initiatorAddress, amount, secretHash);
log('<hr/>');
log('contract txid: ' + makeTxLink(send, participation.txid) + ' (send to counter party)');
log('contract successfully created!');
prepareAndShowRedeemDiv();
div.hide();
});
$("[name='btnInitiatorRedeem']").click(async function(){
let div = $("#initiatorRedeemDiv");
let mnemonic = div.find("[name='walletMnemonic']").val();
let participationTxId = div.find("[name='counterPartyContractTxId']").val();
let secret = div.find("[name='secret']").val();
wallets[receive] = app.getAccount(receive, mnemonic);
let participantContractInfo = await app.getContractTxInfo(receive, participationTxId);
let initiatorRedeem = await app.redeem(receive, wallets[receive], participantContractInfo, secret);
log('<hr/>');
log('redeem txid: ' + makeTxLink(receive, initiatorRedeem.txid) + ' (send to counter party)');
log('transaction successfully sent!');
div.find('input,button').hide();
div.find("[name='btnRefund']").show();
log('<hr/>');
log('if counter party don\'t redeem, you can refund after ' + ((party == 'participant') ? app.getParticipantRefundHours() : app.getInititatorRefundHours()) + ' hours')
});
$("[name='btnParticipantRedeem']").click(async function(){
let div = $("#participantRedeemDiv");
let initiationTxId = $("#participateDiv").find("[name='initiatorContractTxId']").val();
let mnemonic = div.find("[name='walletMnemonic']").val();
let initiatorRedeemTxId = div.find("[name='counterPartyRedeemTxId']").val();
//let initiationTxId = div.find("[name='counterPartyContractTxId']").val();
wallets[receive] = app.getAccount(receive, mnemonic);
let initiationTxInfo = await app.getContractTxInfo(receive, initiationTxId);
let initiatorRedeemTxInfo = await app.getRedeemTxInfo(send, initiatorRedeemTxId);
let participantRedeem = await app.redeem(receive, wallets[receive], initiationTxInfo, initiatorRedeemTxInfo.secret);
log('<hr/>');
log('redeem txid: ' + makeTxLink(receive, participantRedeem.txid));
log('transaction successfully sent!');
div.find('input,button').hide();
div.find("[name='btnRefund']").show();
log('<hr/>');
log('if counter party don\'t redeem, you can refund after ' + ((party == 'participant') ? app.getParticipantRefundHours() : app.getInititatorRefundHours()) + ' hours')
});
$("[name='btnStart']").click(function(){
send = $("select[name='send']").val();
receive = $("select[name='receive']").val();
if(send == receive) {
send = null;
receive = null;
log('invalid operation!');
} else {
party = $("select[name='party']").val();
if(party == 'initiator')
prepareAndShowInitiateDiv();
else if(party == 'participant')
prepareAndShowParticipateDiv();
$("#initialize").hide();
}
});
var prepareAndShowRedeemDiv = () => {
if(party == 'initiator')
prepareAndShowInitiatorRedeemDiv();
else if(party == 'participant')
prepareAndShowParticipantRedeemDiv();
};
var prepareAndShowParticipantRedeemDiv = () => {
let div = $("#participantRedeemDiv");
div.find("[name='walletMnemonic']").attr('placeholder','mnemonic of your '+receive+' wallet');
div.find("[name='counterPartyRedeemTxId']").attr('placeholder','initiator '+send+' redeem txid');
div.find("[name='counterPartyContractTxId']").attr('placeholder','initiator '+receive+' contract txid');
div.find("[name='btnRefund']").hide();
div.show();
};
var prepareAndShowInitiatorRedeemDiv = () => {
let div = $("#initiatorRedeemDiv");
div.find("[name='walletMnemonic']").attr('placeholder','mnemonic of your '+receive+' wallet');
div.find("[name='secret']").val(initiation.secret);
div.find("[name='counterPartyContractTxId']").attr('placeholder','participant '+receive+' contract txid');
div.find("[name='btnRefund']").hide();
div.show();
};
var prepareAndShowInitiateDiv = () => {
let div = $("#initiateDiv");
div.find("[name='walletMnemonic']").attr('placeholder','mnemonic of your '+send+' wallet');
div.find("[name='counterPartyAddress']").attr('placeholder','participant '+send+' address');
div.find("[name='amount']").attr('placeholder',send + ' amount to send (eg. 0.1)');
div.show();
};
var prepareAndShowParticipateDiv = () => {
let div = $("#participateDiv");
div.find("[name='walletMnemonic']").attr('placeholder','mnemonic of your '+send+' wallet');
div.find("[name='counterPartyAddress']").attr('placeholder','initiator '+send+' address');
div.find("[name='initiatorContractTxId']").attr('placeholder', 'initiator '+receive+' contract txid');
div.find("[name='amount']").attr('placeholder',send + ' amount to send (eg. 0.1)');
div.show();
};
var makeAddressLink = (currency, address) => {
let url = makeExplorerUrl(currency, 'address', address);
return "<a href='" + url + "' target='_blank'>" + address + "</a>";
}
var makeTxLink = (currency, txid) => {
let url = makeExplorerUrl(currency, 'tx', txid);
return "<a href='" + url + "' target='_blank'>" + txid + "</a>";
}
var makeExplorerUrl = (currency, type, value) => {
if(currency == 'ETH')
return 'https://ropsten.etherscan.io/'+type+'/'+value;
else if(currency == 'WAVES')
return 'https://testnet.wavesexplorer.com/'+type+'/'+value;
}
});
function log(msg) {
$("#logDiv").prepend('<p>'+msg+'</p>');
}
function wait(ms){
var start = new Date().getTime();
var end = start;
while(end < start + ms) {
end = new Date().getTime();
}
}
</script>
</html>