forked from alexbosworth/balanceofsatoshis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_paid_service.js
235 lines (199 loc) · 7.12 KB
/
get_paid_service.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
const asyncAuto = require('async/auto');
const {encode} = require('cbor');
const {genericSwapAuth} = require('goldengate');
const {genericSwapService} = require('goldengate');
const {getNetwork} = require('ln-sync');
const {getSwapMacaroon} = require('goldengate');
const {getSwapOutTerms} = require('goldengate');
const {lightningLabsSwapAuth} = require('goldengate');
const {lightningLabsSwapService} = require('goldengate');
const {paidMacaroon} = require('goldengate');
const {parsePaymentRequest} = require('ln-service');
const {payViaPaymentRequest} = require('ln-service');
const {returnResult} = require('asyncjs-util');
const {swapUserId} = require('goldengate');
const decodeSwapApiKey = require('./decode_swap_api_key');
const bitcoinNetwork = 'btc';
const bitcoinTestnetNetwork = 'btctestnet';
const bufferFromBase64 = base64 => Buffer.from(base64, 'base64');
const bufferFromHex = hex => Buffer.from(hex, 'hex');
const connectError = 'FailedToConnectToService';
const encodeCbor = json => encode(json).toString('hex');
const httpMatch = /^https?:\/\//;
const {isArray} = Array;
const mainnetSocket = 'https://balanceofsatoshis.com:11011';
const maxServiceFee = 1337;
const maxRoutingFee = 100;
const testnetSocket = 'https://balanceofsatoshis.com:11010';
/** Get a paid swap service object
{
fetch: <Fetch Function>
lnd: <Authenticated LND gRPC API Object>
[socket]: <Custom Backing Service Socket String>
[token]: <Prepaid Service Token CBOR Encoded String>
}
@returns via cbk or Promise
{
id: <Authenticated User Id string>
metadata: <Authenticated Service Metadata Object>
paid: <Paid Tokens Number>
service: <Authenticated Paid Swap Service Object>
token: <Authentication Token Hex String>
}
*/
module.exports = ({fetch, lnd, socket, token}, cbk) => {
return new Promise((resolve, reject) => {
return asyncAuto({
// Check arguments
validate: cbk => {
if (!fetch) {
return cbk([400, 'ExpectedFetchFunctionToGetPaidService']);
}
if (!lnd) {
return cbk([400, 'ExpectedLndToGetPaidService']);
}
return cbk();
},
// Decode service token when present
decodeToken: ['validate', ({}, cbk) => {
// Exit early when there is no token to decode
if (!token) {
return cbk();
}
return decodeSwapApiKey({key: token}, cbk);
}],
// Get network
getNetwork: ['validate', ({}, cbk) => getNetwork({lnd}, cbk)],
// Determine which endpoint to use for the service
endpoint: ['getNetwork', ({getNetwork}, cbk) => {
// Exit early when the endpoint is directly specified
if (!!socket) {
return cbk(null, socket);
}
const {metadata} = lightningLabsSwapAuth({});
const {network} = getNetwork;
const {service} = lightningLabsSwapService({network});
return getSwapOutTerms({metadata, service}, err => {
// Exit early when the standard endpoint is working
if (!isArray(err)) {
return cbk();
}
const [, message] = err;
// Exit early when the error is not a connection issue
if (message !== connectError) {
return cbk();
}
// Try to switch to a backup socket when the main socket fails
switch (getNetwork.network) {
case bitcoinNetwork:
return cbk(null, mainnetSocket);
case bitcoinTestnetNetwork:
return cbk(null, testnetSocket);
default:
return cbk();
}
});
return cbk();
}],
// Make a service object for the remote swap service
remote: ['endpoint', 'getNetwork', ({endpoint, getNetwork}, cbk) => {
// Exit early when using a generic service
if (!!endpoint && httpMatch.test(endpoint)) {
try {
return cbk(null, genericSwapService({fetch, socket: endpoint}));
} catch (err) {
return cbk([500, 'UnexpectedErrorInitiatingSwapService', {err}]);
}
}
try {
return cbk(null, lightningLabsSwapService({
network: getNetwork.network,
socket: endpoint,
}));
} catch (err) {
return cbk([500, 'UnexpectedErrorInitiatingSwapService', {err}]);
}
}],
// Get an unpaid swap macaroon
getUnpaidMacaroon: [
'decodeToken',
'remote',
({decodeToken, remote}, cbk) =>
{
// Exit early when there is already a service token macaroon
if (!!decodeToken) {
return cbk(null, {
id: swapUserId({macaroon: decodeToken.macaroon}).id,
macaroon: decodeToken.macaroon,
});
}
return getSwapMacaroon({service: remote.service}, cbk);
}],
// Pay for the macaroon
payForMacaroon: [
'decodeToken',
'getUnpaidMacaroon',
({decodeToken, getUnpaidMacaroon}, cbk) =>
{
// Exit early when there is already a paid service token
if (!!decodeToken) {
return cbk(null, {secret: decodeToken.preimage});
}
const {request} = getUnpaidMacaroon;
// Validate the service token payment request
try {
const {tokens} = parsePaymentRequest({request});
if (tokens > maxServiceFee) {
return cbk([503, 'UnexpectedlyHighServiceFee', {fee: tokens}]);
}
} catch (err) {
return cbk([503, 'FailedToParseServicePaymentRequest', {err}]);
}
// Pay the service token payment request to purchase the macaroon
return payViaPaymentRequest({
lnd,
request,
max_fee: maxRoutingFee,
},
cbk);
}],
// Create authentication metadata object
metadata: [
'decodeToken',
'endpoint',
'getUnpaidMacaroon',
'payForMacaroon',
({decodeToken, endpoint, getUnpaidMacaroon, payForMacaroon}, cbk) =>
{
if (!decodeToken && !payForMacaroon.secret) {
return cbk([400, 'FailedToPurchasePaidServiceToken']);
}
const {macaroon} = getUnpaidMacaroon;
const preimage = payForMacaroon.secret;
// Exit early when using a generic service
if (!!endpoint && httpMatch.test(endpoint)) {
return cbk(null, genericSwapAuth({macaroon, preimage}).metadata);
}
return cbk(null, lightningLabsSwapAuth({macaroon, preimage}).metadata);
}],
// Final service method details
paidService: [
'getUnpaidMacaroon',
'metadata',
'payForMacaroon',
'remote',
({getUnpaidMacaroon, metadata, payForMacaroon, remote}, cbk) =>
{
const {id} = getUnpaidMacaroon;
const paid = payForMacaroon.tokens;
const {service} = remote;
const token = encodeCbor({
macaroon: bufferFromBase64(getUnpaidMacaroon.macaroon),
preimage: bufferFromHex(payForMacaroon.secret),
});
return cbk(null, {id, metadata, paid, service, token});
}],
},
returnResult({reject, resolve, of: 'paidService'}, cbk));
});
};