-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalice.js
74 lines (49 loc) · 1.85 KB
/
alice.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
const web3 = require('./web3')
const lndInit = require('./lnd')
const fs = require('fs')
const Contract = require('./contract')
const wallet = require('./wallet')
wallet.init('0x8b8d2ec664a9b0a2553c414040fd626ba01d8adee7d826cfc124369cf4afe170')
const ALICE_ADDRESS = wallet.account.address
const BOB_ADDRESS = '0x49A463bE2b7aAefEc4a2f7356c477D1085dA25f3'
console.log('ALICE ADDRESS', ALICE_ADDRESS)
const SWAP_ETH_VALUE = '0.1'
const SWAP_BTC_VALUE = 10000
const sha256 = require('js-sha256')
const host = process.env.HOST || 'localhost'
const url = `${host}:10001`
const aliceMacaroon = './alice/admin.macaroon'
lndInit(url, aliceMacaroon).then(async (lnd) => {
console.log('ALICE')
// check balance
const balance = await lnd.walletBalance({})
console.log('check balance', balance)
// create pay req
console.log('create pay req')
const invoice = await lnd.addInvoice({ value: SWAP_BTC_VALUE })
console.log(invoice)
// detailed invoice info
console.log('get detailed')
const r_hash = invoice.r_hash.toString('hex')
const detailed = await lnd.lookupInvoice({ r_hash_str: r_hash })
console.log(detailed)
// extract preimage
const preimage = detailed.r_preimage
console.log('extract preimage')
console.log(preimage.toString('hex'))
// hash preimage
console.log('hash preimage')
const secret_bytes = preimage //Buffer.from(preimage, 'hex')
const hash = sha256(secret_bytes)
console.log(hash)
// deploy ETH contract with preimage hash
console.log('update ETH contract with preimage hash')
const swap = new Contract()
console.log('Alice waiting for confirmation...')
const receipt = await swap.fund(BOB_ADDRESS, hash, SWAP_ETH_VALUE) // ETH
console.log(receipt)
console.log('send bob pay req')
// send bob pay_req
fs.writeFileSync("./pay_req", invoice.payment_request)
console.log('pay req set:', fs.readFileSync("./pay_req").toString())
})