forked from timxor/Zcash-Eth-Atomic-Trades
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
405 lines (363 loc) · 12.9 KB
/
server.js
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
var crypto = require('crypto');
var express = require('express')
var contract = require("truffle-contract");
var Web3 = require('web3');
var bodyParser = require('body-parser');
var uuidv4 = require('uuid/v4');
var zcash = require('./ZBXCAT/zcash');
var addrs = require('./addrs');
var app = express();
var json = require("./build/contracts/hashlock.json");
var HashLockContract = contract(json);
// use ejs template engine
app.set('view engine', 'ejs');
// serve up static files from
app.use('/static',express.static('./node_modules'));
// serve up static files from public
app.use('/static',express.static('./public'));
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var provider = new Web3.providers.HttpProvider("http://localhost:8545");
HashLockContract.setProvider(provider);
var web3 = new Web3(provider);
try{
HashLockContract.deployed().then(function(instance){
/*****************************************************************
* API
*****************************************************************/
/*app.post('/setup', function(req, res){
var pw = req.body.password
var genpriv = addrs.genPrivKey(pw, 'testnet')
console.log("genpriv.privkey", typeof(genpriv.privkey))
// increment the num of keys generated
var rand = Math.floor(Math.random() * 200);
var genpub = addrs.newPubKey(genpriv.privkey, rand)
// this is bad. get working on client side with browserify
res.send({
code: genpriv.code.toString(),
privkey: genpriv.privkey.toString(),
pubkey: genpub.pubkey.toString(),
address: genpub.address.toString()
});
});
app.post('/wallet', function(req, res){
var privkey = req.body.privkey;
console.log("privkey", privkey)
var rand = Math.floor(Math.random() * 200);
var genpub = addrs.newPubKey(privkey, rand)
res.send({
address: genpub.address.toString()
});
});*/
/**
* Generates a random UUID
*/
app.get('/api/random', function(req, res){
res.send({
random: uuidv4()
});
});
/**
* Computes sha256 hash of data
*/
app.post('/api/hash', function(req, res){
if(req.body['data']){
var hash = crypto.createHash('sha256');
hash.update(req.body.data);
res.send({
hash: "0x"+hash.digest('hex')
});
}else{
res.send({
error: 'data is not defined'
});
}
});
/**
* info about swap contract
*/
app.get('/api/swap', function (req, res) {
res.send({
abi: HashLockContract._json.abi,
address: instance.address,
});
});
/**
* Get Swap info
*/
app.get('/api/swap/get/:id', function(req, res){
function getTrade(tradeId, res){
instance.trades(tradeId).then(function(tradeData){
console.log(tradeData)
res.send({
id: req.params.id,
sender: tradeData[0],
redeemer: tradeData[1],
senderZAddr: tradeData[2],
redeemerZAddr: tradeData[3],
hash: tradeData[4],
amount: tradeData[5],
timeoutBlock: tradeData[6],
fundTx: tradeData[7],
p2sh: tradeData[8],
redeemScript: tradeData[9],
zecAmount: tradeData[10]
});
}).catch(function(err){
res.send({
error: err.toString()
});
});
}
if(!req.params.id){
res.send({
error: "Invalid parameters"
});
}
/*else if(/^0x/.test(req.params.id)){
// id is a transaction hash
var txId = req.params.id;
web3.eth.getTransactionReceipt(txId,function(err,result){
if(err == null || result == null){
res.send({
error: "tx: " + txId + " was not found."
});
}
else if(err){
res.send({
error: err.toString()
});
}else{
// TODO: get trade id
getTrade(txId,res);
}
});
}*/else if(/\d+/.test(req.params.id)){
// id is the trade id
getTrade(req.params.id, res);
}else{
res.send({
error: "Invalid parameters"
});
}
});
/**
* Gets just the zcash redeemscript/p2sh
*/
app.post('/api/zec/p2sh', function(req, res){
var ethBlocks = req.body.ethBlocks
var zecBlocks = Math.ceil(ethBlocks / 20)
console.log('zecblocks', zecBlocks)
var contractData = {
initiator: req.body.initiator, // B
fulfiller: req.body.redeemer, // A
timeLock: zecBlocks,
hash: req.body.hash,
secret: req.body.secret
}
console.log("CONTRACTDATA", contractData)
zcash.call('make', contractData)
.then(function(contract){
console.log("Response from make func:", contract)
console.log("p2sh", contract['p2sh'])
res.send({
redeemblocknum: contract['redeemblocknum'],
redeemScript: contract['redeemScript'],
p2sh: contract['p2sh']
// rawTx: rawTx
});
}).catch(function(err){
res.send({
error: err.toString()
});
});;
})
/**
* Gets funding data transaction P2SH
*/
app.post('/api/zec/txdata', function(req, res){
var tradeId = req.body.tradeId
if(!tradeId){
res.send({
error: 'tradeId is required'
});
}
zcash.call('getdata', '')
.then(function(contract){
console.log("Response from getdata:", contract)
var data = {};
for(var key in contract){
data[key] = contract[key];
}
res.send(data);
}).catch(function(err){
res.send({
error: err.toString()
});
});
});
/**
* Submits Bob's funding transaction
*/
app.post('/api/zec/tx/fund', function(req, res){
var data = {
p2sh: req.body.p2sh,
amt: req.body.amount
}
zcash.call('fund', data)
.then(function(contract){
console.log("Contract returning from call", contract)
console.log("Fund txid returning from call", contract['fund_tx'])
res.send({
tx: contract['fund_tx'],
redeemScript: contract['redeemScript']
});
}).catch(function(err){
res.send({
error: err.toString()
});
});;
});
/**
* Submits Alice's redeem transaction
*/
app.post('/api/zec/tx/redeem', function(req, res){
var data = {
preimage: req.body.preimage,
fundTx: req.body.fundTx,
p2sh: req.body.p2sh,
redeemer: req.body.redeemer,
tradeid: req.body.tradeId
}
zcash.call('redeem', data)
.then(function(contract){
console.log("Contract returning from redeem call", contract)
console.log("Redeem txid returning from call", contract['redeem_tx'])
res.send({
tx: contract['redeem_tx']
});
}).catch(function(err){
res.send({
error: err.toString()
});
});;
});
/**
* Submits Bob's refund transaction
*/
app.post('/api/zec/tx/refund', function(req, res){
zcash.call('refund', '')
.then(function(contract){
console.log("Contract returning from refund call", contract)
console.log("Redeem txid returning from call", contract['refund_tx'])
res.send({
tx: contract['refund_tx']
});
}).catch(function(err){
res.send({
error: err.toString()
});
});
})
app.post('/api/zec/address', function(req, res){
var data = {
role: req.body.role
}
zcash.call('getaddr', data)
.then(function(contract){
console.log("Getting contract", contract)
res.send({
address: contract[data['role']]
});
}).catch(function(err){
res.send({
error: err.toString()
});
});
})
/**
* Creates a "hash lock ETH contract" between Alice and Bob
*/
/*app.post('/api/eth/lock', function(req, res){
var hash = req.body.hash;
var redeemer = req.body.redeemer;
var sender = req.body.sender;
var senderZAddr = req.body.senderZAddr;
var redeemerZAddr = req.body.redeemerZAddr;
var expiry = req.body.expiry;
var amount = req.body.amount;
instance.lock(hash, redeemer, expiry, senderZAddr, redeemerZAddr, {
from: sender,
value: amount,
gas: 1248090
}).then(function(result){
res.send({
tradeId: result.logs[0].args.trade_id,
tx: result.tx
});
}).catch(function(err){
res.send({
error: err.toString()
});
});
});*/
/*app.post('/api/eth/unlock', function(req, res){
var tradeId = req.body.tradeId;
var preimage = req.body.preimage;
var redeemer = req.body.redeemer;
// can the redeemer be just the address, or must be accessed through web3?
instance.unlock(tradeId, preimage, {
from: redeemer,
gas: 1248090
}).then(function(result){
res.send({
tx: result.tx
});
}).catch(function(err){
res.send({
error: err.toString()
});
});
});*/
/*****************************************************************
* pages
*****************************************************************/
app.get('/',function(req,res){
res.render('pages/index',{
title: "Home"
});
});
app.get('/wallet', function(req, res){
res.render('pages/wallet',{
title: "Wallet"
});
});
app.get('/trade/eth/init',function(req,res){
res.render('pages/trade/eth-init',{
title: "Init ETH HLC"
});
});
app.get('/trade/zec/init',function(req,res){
res.render('pages/trade/zec-init',{
title: "Init ZEC HLC"
});
});
app.get('/trade/zec/settle',function(req,res){
res.render('pages/trade/zec-settle',{
title: "Settle ZEC HLC"
});
});
app.get('/trade/eth/settle',function(req,res){
res.render('pages/trade/eth-settle',{
title: "Settle ETH HLC"
});
});
app.listen(3000,function(){
console.log("http://localhost:3000");
});
});
}catch(e){
console.log(e);
console.log("Contract hasn't been deployed to blockchain. Try running truffle migrate.");
}