-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
executable file
·108 lines (99 loc) · 3.03 KB
/
index.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
#!/usr/bin/env node
const plugin = require('ilp-plugin')()
const SPSP = require('ilp-protocol-spsp')
const PSK2 = require('ilp-protocol-psk2')
const debug = require('debug')('ilp-spsp')
require('yargs')
.option('pointer', {
alias: ['receiver', 'p', 'r'],
description: 'SPSP payment pointer'
})
.command('send', 'send money via SPSP', {
amount: {
alias: 'a',
required: true,
description: 'amount to send to payment pointer (source account base units)'
}
}, async argv => {
console.log(`paying ${argv.amount} to "${argv.pointer}"...`)
try {
debug('connecting plugin')
await plugin.connect()
debug('sending payment')
try {
const sentAmount = await SPSP.pay(plugin, {
pointer: argv.pointer,
sourceAmount: argv.amount,
streamOpts: { timeout: 10000 }
})
console.log('sent ' + sentAmount.totalSent + ' units!')
} catch (e) {
console.log('sent ' + (e instanceof SPSP.PaymentError ? e.totalSent : '0') + ' units!')
}
} catch (e) {
console.error(e)
process.exit(1)
}
process.exit(0)
})
.command('invoice', 'pay an SPSP invoice', {}, async argv => {
console.log(`paying invoice at "${argv.pointer}"...`)
try {
debug('connecting plugin')
await plugin.connect()
debug('querying SPSP payment pointer')
const query = await SPSP.query(argv.pointer)
if (!query.balance) {
console.error('query result has no balance')
process.exit(1)
}
if (query.contentType.indexOf('application/spsp+json') === -1) {
console.error('use the \'send\' command to fill an invoice on SPSPv4.')
process.exit(1)
}
debug('paying invoice')
await PSK2.sendDestinationAmount(plugin, {
...query,
destinationAmount: String(query.balance.maximum - query.balance.current)
})
console.log('paid!')
process.exit(0)
} catch (e) {
console.error(e)
process.exit(1)
}
})
.command('pull', 'pull money via SPSP', {
amount: {
alias: 'a',
required: true,
description: 'amount to pull from payment pointer (receiving account base units)'
}
}, async argv => {
console.log(`pulling from "${argv.pointer}"...`)
try {
debug('connecting plugin')
await plugin.connect()
debug('pulling payment')
try {
const pulledAmount = await SPSP.pull(plugin, {
pointer: argv.pointer,
amount: argv.amount,
streamOpts: { timeout: 10000 }
})
console.log('pulled ' + pulledAmount.totalReceived + ' units!')
} catch (e) {
console.log('pulled ' + (e instanceof SPSP.PaymentError ? e.totalReceived : '0') + ' units!')
}
} catch (e) {
console.error(e)
process.exit(1)
}
process.exit(0)
})
.command('query', 'query SPSP endpoint', {}, async argv => {
const response = await SPSP.query(argv.pointer)
console.log(JSON.stringify(response, null, 2))
process.exit(0)
})
.argv