-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexample.js
50 lines (44 loc) · 1.89 KB
/
example.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
const crypto = require('crypto')
const IlpPluginBtp = require('ilp-plugin-btp')
const PSK2 = require('.')
;(async () => {
const receiverPlugin = new IlpPluginBtp({
server: `btp+wss://:${crypto.randomBytes(16).toString('hex')}@amundsen.ilpdemo.org:1801`
})
const receiver = await PSK2.createReceiver({
plugin: receiverPlugin,
requestHandler: async (params) => {
params.accept(Buffer.from('thanks!'))
console.log(`Receiver got paid request with ${params.amount} attached and data: ${params.data.toString('utf8')}`)
receiver.close()
}
})
console.log('Receiver listening')
// These would normally be passed through some application layer protcol
const { destinationAccount, sharedSecret } = receiver.generateAddressAndSecret()
console.log(`Using destinationAccount: ${destinationAccount} and sharedSecret: ${sharedSecret.toString('hex')}`)
const senderPlugin = new IlpPluginBtp({
server: `btp+wss://:${crypto.randomBytes(16).toString('hex')}@amundsen.ilpdemo.org:1801`
})
await senderPlugin.connect()
// Get a quote using an unfulfillable request
console.log('Getting a quote using an unfulfillable request')
const sourceAmount = '100'
const { destinationAmount } = await PSK2.sendRequest(senderPlugin, {
destinationAccount,
sharedSecret,
sourceAmount,
unfulfillableCondition: crypto.randomBytes(32)
})
console.log(`Path exchange rate is: ${destinationAmount.dividedBy(sourceAmount)}`)
console.log('Sending a real paid request now')
const result = await PSK2.sendRequest(senderPlugin, {
destinationAccount,
sharedSecret,
sourceAmount,
minDestinationAmount: '98',
data: Buffer.from('here is some money!', 'utf8')
})
console.log(`Sent payment of ${sourceAmount}, receiver got ${result.destinationAmount} and responded with message: ${result.data.toString('utf8')}`)
process.exit(0)
})().catch(err => console.log(err))