-
Notifications
You must be signed in to change notification settings - Fork 2
/
bob.js
75 lines (47 loc) · 1.65 KB
/
bob.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
const web3 = require('./web3')
const lndInit = require('./lnd')
const fs = require('fs')
const Contract = require('./contract')
const wallet = require('./wallet')
wallet.init('0x79ac3fac7e8dbaeeb117d84e78815b9ce253bbb01a3daa6298d6a2aead8f0a6e')
const ALICE_ADDRESS = '0x15a38F422F81Fb90003323760E0A71f4DcA34255'
const BOB_ADDRESS = wallet.account.address
console.log('BOB ADDRESS', BOB_ADDRESS)
const sha256 = require('js-sha256')
const host = process.env.HOST || 'localhost'
const url = `${host}:10002`
const bobMacaroon = './bob/admin.macaroon'
const pay_req = fs.readFileSync('./pay_req').toString()
lndInit(url, bobMacaroon).then(async (lnd) => {
console.log('BOB')
// get pay req
console.log('get pay req')
console.log(pay_req)
// decode pay req
console.log('decode pay req')
const req = await lnd.decodePayReq({ pay_req })
console.log('req', req)
const hash = req.payment_hash
// check ETH is locked
console.log('check ETH is locked')
const swap = new Contract()
// check balance
// const balance = await swap.getBalance()
// check hash
console.log('check hash')
// hash == swap.secretHash.call()
// send payment pay req
console.log('send payment pay req')
const pay = await lnd.sendPaymentSync({ payment_request: pay_req })
console.log(pay)
// obtain preimage
console.log('obtain preimage')
const secret = pay.payment_preimage.toString('hex')
console.log(secret)
console.log('hash', sha256(Buffer.from(secret, 'hex')))
// withdraw ETH
console.log('withdraw ETH waiting...')
const receipt = await swap.withdraw(ALICE_ADDRESS, secret)
console.log('receipt', receipt)
console.log('tx hash ', receipt.transactionHash)
})