lightning 9.13.4 → 10.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/grpc/protos/lightning.proto +5 -0
- package/index.d.ts +0 -1
- package/index.js +0 -6
- package/lnd_methods/info/constants.json +1 -0
- package/lnd_methods/onchain/open_channel.d.ts +2 -0
- package/lnd_methods/onchain/open_channel.js +5 -0
- package/package.json +6 -12
- package/test/lnd_methods/onchain/test_open_channel.js +1 -0
- package/lnd_gateway/bearer_token.js +0 -30
- package/lnd_gateway/decode_cbor_body.js +0 -30
- package/lnd_gateway/emit_event.js +0 -18
- package/lnd_gateway/emit_grpc_events.d.ts +0 -17
- package/lnd_gateway/emit_grpc_events.js +0 -50
- package/lnd_gateway/encode_response.js +0 -42
- package/lnd_gateway/execute_request.js +0 -74
- package/lnd_gateway/gateway_request.js +0 -143
- package/lnd_gateway/gateway_subscribe.js +0 -53
- package/lnd_gateway/grpc_response.js +0 -42
- package/lnd_gateway/grpc_router.d.ts +0 -13
- package/lnd_gateway/grpc_router.js +0 -45
- package/lnd_gateway/handle_errors.js +0 -37
- package/lnd_gateway/index.d.ts +0 -3
- package/lnd_gateway/index.js +0 -5
- package/lnd_gateway/lnd_gateway.d.ts +0 -37
- package/lnd_gateway/lnd_gateway.js +0 -118
- package/lnd_gateway/return_response.js +0 -38
- package/lnd_gateway/subscribe_to_response.js +0 -40
- package/lnd_gateway/ws_url.js +0 -34
- package/test/lnd_gateway/test_bearer_token.js +0 -31
- package/test/lnd_gateway/test_decode_cbor_body.js +0 -37
- package/test/lnd_gateway/test_emit_event.js +0 -51
- package/test/lnd_gateway/test_emit_grpc_events.js +0 -31
- package/test/lnd_gateway/test_encode_response.js +0 -37
- package/test/lnd_gateway/test_gateway_request.js +0 -139
- package/test/lnd_gateway/test_gateway_subscribe.js +0 -97
- package/test/lnd_gateway/test_grpc_response.js +0 -29
- package/test/lnd_gateway/test_grpc_router.js +0 -20
- package/test/lnd_gateway/test_handle_errors.js +0 -48
- package/test/lnd_gateway/test_lnd_gateway.js +0 -67
- package/test/lnd_gateway/test_return_response.js +0 -51
- package/test/lnd_gateway/test_ws_url.js +0 -32
- package/test/typescript/emit_grpc_events.test-d.ts +0 -12
- package/test/typescript/grpc_router.test-d.ts +0 -8
- package/test/typescript/lnd_gateway.test-d.ts +0 -36
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import {Router} from 'express';
|
|
2
|
-
|
|
3
|
-
export type Credentials = {
|
|
4
|
-
/** Base64 or Hex Serialized LND TLS Cert String */
|
|
5
|
-
cert?: string;
|
|
6
|
-
/** Host:Port Network Address String */
|
|
7
|
-
socket?: string;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Get a gRPC gateway router
|
|
12
|
-
*/
|
|
13
|
-
export function grpcRouter(credentials: Credentials): Router;
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
const {raw} = require('body-parser');
|
|
2
|
-
const {Router} = require('express');
|
|
3
|
-
|
|
4
|
-
const bearerToken = require('./bearer_token');
|
|
5
|
-
const decodeCborBody = require('./decode_cbor_body');
|
|
6
|
-
const grpcResponse = require('./grpc_response');
|
|
7
|
-
const handleErrors = require('./handle_errors');
|
|
8
|
-
|
|
9
|
-
const caseSensitive = true;
|
|
10
|
-
const cborType = 'application/cbor';
|
|
11
|
-
const path = '/:service/:method';
|
|
12
|
-
const strict = true;
|
|
13
|
-
|
|
14
|
-
/** Get a gRPC gateway router
|
|
15
|
-
|
|
16
|
-
{
|
|
17
|
-
[cert]: <Base64 or Hex Serialized LND TLS Cert String>
|
|
18
|
-
[socket]: <Host:Port Network Address String>
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
@returns
|
|
22
|
-
<Router Object>
|
|
23
|
-
*/
|
|
24
|
-
module.exports = ({cert, socket}) => {
|
|
25
|
-
const credentials = {cert, socket};
|
|
26
|
-
const router = Router({caseSensitive, strict});
|
|
27
|
-
|
|
28
|
-
// Parse out the bearer token
|
|
29
|
-
router.use(bearerToken({}).middleware);
|
|
30
|
-
|
|
31
|
-
// Decode body into binary buffer
|
|
32
|
-
router.use(raw({type: cborType}));
|
|
33
|
-
|
|
34
|
-
// Decode CBOR binary body into object
|
|
35
|
-
router.use(decodeCborBody({}).middleware);
|
|
36
|
-
|
|
37
|
-
// Proxy CBOR arguments into an LND request
|
|
38
|
-
router.post(path, grpcResponse({credentials}).middleware);
|
|
39
|
-
|
|
40
|
-
// Handle any errors that pop up along the way
|
|
41
|
-
router.use(handleErrors({}).middleware);
|
|
42
|
-
|
|
43
|
-
return router;
|
|
44
|
-
};
|
|
45
|
-
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
const {isArray} = Array;
|
|
2
|
-
const serverErrorCode = 500;
|
|
3
|
-
|
|
4
|
-
/** Handle errors
|
|
5
|
-
|
|
6
|
-
{
|
|
7
|
-
err: <Error Object>
|
|
8
|
-
next: <Next Express Middleware Function>
|
|
9
|
-
res: {
|
|
10
|
-
headersSent: <Headers Already Sent Bool>
|
|
11
|
-
send: <Send Response Function>
|
|
12
|
-
status: <Set Status Function>
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
@returns
|
|
17
|
-
{
|
|
18
|
-
middleware: <Express Middleware Function>
|
|
19
|
-
}
|
|
20
|
-
*/
|
|
21
|
-
module.exports = ({}) => {
|
|
22
|
-
const middleware = (err, req, res, next) => {
|
|
23
|
-
if (res.headersSent) {
|
|
24
|
-
return next(err)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (isArray(err)) {
|
|
28
|
-
const [statusCode] = err;
|
|
29
|
-
|
|
30
|
-
return res.status(statusCode || serverErrorCode) && res.send();
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return res.status(serverErrorCode) && res.send();
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
return {middleware};
|
|
37
|
-
};
|
package/lnd_gateway/index.d.ts
DELETED
package/lnd_gateway/index.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import * as request from 'request';
|
|
2
|
-
import WebSocket = require('ws');
|
|
3
|
-
import {AuthenticatedLnd} from '../lnd_grpc/authenticated_lnd_grpc';
|
|
4
|
-
import {UnauthenticatedLnd} from '../lnd_grpc/unauthenticated_lnd_grpc';
|
|
5
|
-
|
|
6
|
-
export type UnauthenticatedLndGatewayServer = {
|
|
7
|
-
/** Base64 or Hex Serialized Gateway TLS Cert String */
|
|
8
|
-
cert?: string;
|
|
9
|
-
/** Request Function */
|
|
10
|
-
request: (
|
|
11
|
-
options: request.Options,
|
|
12
|
-
callback: request.RequestCallback
|
|
13
|
-
) => request.Request;
|
|
14
|
-
/** LND Gateway URL String */
|
|
15
|
-
url: string;
|
|
16
|
-
/** Websocket Constructor Function */
|
|
17
|
-
websocket: typeof WebSocket;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export type AuthenticatedLndGatewayServer = UnauthenticatedLndGatewayServer & {
|
|
21
|
-
/** Use Base 64 Encoded Macaroon String */
|
|
22
|
-
macaroon: string;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Interface to an LND gateway server.
|
|
27
|
-
*/
|
|
28
|
-
export function lndGateway(
|
|
29
|
-
server: AuthenticatedLndGatewayServer
|
|
30
|
-
): {lnd: AuthenticatedLnd};
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Interface to an LND gateway server.
|
|
34
|
-
*/
|
|
35
|
-
export function lndGateway(
|
|
36
|
-
server: UnauthenticatedLndGatewayServer
|
|
37
|
-
): {lnd: UnauthenticatedLnd};
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
const {join} = require('path');
|
|
2
|
-
|
|
3
|
-
const {loadPackageDefinition} = require('@grpc/grpc-js');
|
|
4
|
-
const {loadSync} = require('@grpc/proto-loader');
|
|
5
|
-
|
|
6
|
-
const gatewayRequest = require('./gateway_request');
|
|
7
|
-
const gatewaySubscribe = require('./gateway_subscribe');
|
|
8
|
-
const {packageTypes} = require('./../grpc');
|
|
9
|
-
const {protoFiles} = require('./../grpc');
|
|
10
|
-
const {protosDir} = require('./../grpc');
|
|
11
|
-
const {serviceTypes} = require('./../grpc');
|
|
12
|
-
const {unauthenticatedPackageTypes} = require('./../grpc');
|
|
13
|
-
const {unauthenticatedServiceTypes} = require('./../grpc');
|
|
14
|
-
|
|
15
|
-
const {keys} = Object;
|
|
16
|
-
|
|
17
|
-
/** Interface to an LND gateway server.
|
|
18
|
-
|
|
19
|
-
{
|
|
20
|
-
[cert]: <Base64 or Hex Serialized Gateway TLS Cert String>
|
|
21
|
-
[macaroon]: <Use Base 64 Encoded Macaroon String>
|
|
22
|
-
request: <Request Function>
|
|
23
|
-
url: <LND Gateway URL String>
|
|
24
|
-
websocket: <Websocket Constructor Object>
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
@throws
|
|
28
|
-
<Error>
|
|
29
|
-
|
|
30
|
-
@returns
|
|
31
|
-
{
|
|
32
|
-
lnd: <LND gRPC Gateway Object>
|
|
33
|
-
}
|
|
34
|
-
*/
|
|
35
|
-
module.exports = ({cert, macaroon, request, url, websocket}) => {
|
|
36
|
-
if (!(request instanceof Function)) {
|
|
37
|
-
throw new Error('ExpectedRequestMethodForLndGateway');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (!url) {
|
|
41
|
-
throw new Error('ExpectedUrlForLndGateway');
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (!websocket) {
|
|
45
|
-
throw new Error('ExpectedWebSocketConstructorForLndGateway');
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const {services, types} = (() => {
|
|
49
|
-
const services = !macaroon ? unauthenticatedPackageTypes : packageTypes;
|
|
50
|
-
const types = !macaroon ? unauthenticatedServiceTypes : serviceTypes;
|
|
51
|
-
|
|
52
|
-
return {types, services: keys(services)};
|
|
53
|
-
})();
|
|
54
|
-
|
|
55
|
-
const servers = keys(types).reduce((sum, n) => {
|
|
56
|
-
sum[types[n]] = n;
|
|
57
|
-
|
|
58
|
-
return sum;
|
|
59
|
-
},
|
|
60
|
-
{});
|
|
61
|
-
|
|
62
|
-
const lnd = services.reduce((clients, service) => {
|
|
63
|
-
const protoFile = protoFiles[service];
|
|
64
|
-
const server = servers[service];
|
|
65
|
-
|
|
66
|
-
const protoPath = join(__dirname, protosDir, protoFile);
|
|
67
|
-
|
|
68
|
-
const rpc = loadPackageDefinition(loadSync(protoPath));
|
|
69
|
-
|
|
70
|
-
const packageService = rpc[packageTypes[service]][service];
|
|
71
|
-
|
|
72
|
-
const definitions = packageService.service;
|
|
73
|
-
|
|
74
|
-
const directResponseMethods = keys(definitions)
|
|
75
|
-
.filter(n => !definitions[n].requestStream)
|
|
76
|
-
.filter(n => !definitions[n].responseStream)
|
|
77
|
-
.map(n => definitions[n].originalName);
|
|
78
|
-
|
|
79
|
-
const streamingResponseMethods = keys(definitions)
|
|
80
|
-
.filter(n => !definitions[n].requestStream)
|
|
81
|
-
.filter(n => definitions[n].responseStream)
|
|
82
|
-
.map(n => definitions[n].originalName);
|
|
83
|
-
|
|
84
|
-
const streaming = streamingResponseMethods.reduce((client, method) => {
|
|
85
|
-
client[method] = (params, cbk) => {
|
|
86
|
-
return gatewaySubscribe({
|
|
87
|
-
url,
|
|
88
|
-
websocket,
|
|
89
|
-
bearer: macaroon,
|
|
90
|
-
call: {method, params, server},
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return client;
|
|
95
|
-
},
|
|
96
|
-
{});
|
|
97
|
-
|
|
98
|
-
clients[server] = directResponseMethods.reduce((client, method) => {
|
|
99
|
-
client[method] = (params, cbk) => {
|
|
100
|
-
return gatewayRequest({
|
|
101
|
-
request,
|
|
102
|
-
url,
|
|
103
|
-
bearer: macaroon,
|
|
104
|
-
call: {method, params, server},
|
|
105
|
-
},
|
|
106
|
-
cbk);
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
return client;
|
|
110
|
-
},
|
|
111
|
-
streaming);
|
|
112
|
-
|
|
113
|
-
return clients;
|
|
114
|
-
},
|
|
115
|
-
{});
|
|
116
|
-
|
|
117
|
-
return {lnd};
|
|
118
|
-
};
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
const {encodeAsync} = require('cbor');
|
|
2
|
-
const {decodeFirst} = require('cbor');
|
|
3
|
-
|
|
4
|
-
const cborType = 'application/cbor';
|
|
5
|
-
const serverErrorCode = 500;
|
|
6
|
-
|
|
7
|
-
/** Return response
|
|
8
|
-
|
|
9
|
-
{
|
|
10
|
-
res: {
|
|
11
|
-
send: <Send Response Body Function>
|
|
12
|
-
status: <Set Response Status Code Function>
|
|
13
|
-
type: <Set Response Content Type Header Function>
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
@returns
|
|
18
|
-
{
|
|
19
|
-
responder: <Responder Function>
|
|
20
|
-
}
|
|
21
|
-
*/
|
|
22
|
-
module.exports = ({res}) => {
|
|
23
|
-
const responder = async (err, response) => {
|
|
24
|
-
if (!!err) {
|
|
25
|
-
return res.status(serverErrorCode) && res.send();
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
try {
|
|
29
|
-
const result = {err: response.err, res: response.res};
|
|
30
|
-
|
|
31
|
-
return res.type(cborType) && res.send(await encodeAsync(result));
|
|
32
|
-
} catch (err) {
|
|
33
|
-
return res.status(serverErrorCode) && res.send();
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
return {responder};
|
|
38
|
-
};
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
const {encodeAsync} = require('cbor');
|
|
2
|
-
|
|
3
|
-
/** Make a request to LND that returns subscription events
|
|
4
|
-
|
|
5
|
-
{
|
|
6
|
-
lnd: <LND API Object>
|
|
7
|
-
method: <LND Method Name String>
|
|
8
|
-
params: <Method Arguments Object>
|
|
9
|
-
server: <LND RPC Server Name String>
|
|
10
|
-
ws: {
|
|
11
|
-
on: <Websocket Attach Listener Function>
|
|
12
|
-
send: <Send Response Function>
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
@returns
|
|
17
|
-
{
|
|
18
|
-
cancel: <Cancel Request Function>
|
|
19
|
-
}
|
|
20
|
-
*/
|
|
21
|
-
module.exports = ({lnd, method, params, server, ws}) => {
|
|
22
|
-
const sub = lnd[server][method](params);
|
|
23
|
-
|
|
24
|
-
const sendResponse = async (ws, event, data) => {
|
|
25
|
-
return ws.send(await encodeAsync({data, event}));
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
sub.on('data', data => sendResponse(ws, 'data', data));
|
|
29
|
-
sub.on('end', () => sendResponse(ws, 'end'));
|
|
30
|
-
sub.on('status', s => sendResponse(ws, 'status', s));
|
|
31
|
-
|
|
32
|
-
sub.on('error', err => sendResponse(ws, 'error', {
|
|
33
|
-
details: err.details,
|
|
34
|
-
message: err.message,
|
|
35
|
-
}));
|
|
36
|
-
|
|
37
|
-
ws.on('close', () => sub.cancel());
|
|
38
|
-
|
|
39
|
-
return {cancel: () => sub.cancel()};
|
|
40
|
-
};
|
package/lnd_gateway/ws_url.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
const encryptedProtocol = 'https:';
|
|
2
|
-
const encryptedWsProtocol = 'wss:';
|
|
3
|
-
const plaintextProtocol = 'http:';
|
|
4
|
-
const plaintextWsProtocol = 'ws:';
|
|
5
|
-
|
|
6
|
-
/** Websocket URL for URL
|
|
7
|
-
|
|
8
|
-
{
|
|
9
|
-
url: <Gateway Server URL String>
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
@returns
|
|
13
|
-
{
|
|
14
|
-
url: <WebSocket URL String>
|
|
15
|
-
}
|
|
16
|
-
*/
|
|
17
|
-
module.exports = ({url}) => {
|
|
18
|
-
const remote = new URL(url);
|
|
19
|
-
|
|
20
|
-
switch (remote.protocol) {
|
|
21
|
-
case encryptedProtocol:
|
|
22
|
-
remote.protocol = encryptedWsProtocol;
|
|
23
|
-
break;
|
|
24
|
-
|
|
25
|
-
case plaintextProtocol:
|
|
26
|
-
remote.protocol = plaintextWsProtocol;
|
|
27
|
-
break;
|
|
28
|
-
|
|
29
|
-
default:
|
|
30
|
-
break;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return {url: remote.toString()};
|
|
34
|
-
};
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
const {deepStrictEqual} = require('node:assert').strict;
|
|
2
|
-
const test = require('node:test');
|
|
3
|
-
const {throws} = require('node:assert').strict;
|
|
4
|
-
|
|
5
|
-
const bearerToken = require('./../../lnd_gateway/bearer_token');
|
|
6
|
-
|
|
7
|
-
const tests = [
|
|
8
|
-
{
|
|
9
|
-
args: {},
|
|
10
|
-
description: 'Bearer token middleware skips when there is no auth',
|
|
11
|
-
expected: {locals: {}},
|
|
12
|
-
},
|
|
13
|
-
{
|
|
14
|
-
args: {header: 'Bearer macaroon'},
|
|
15
|
-
description: 'Bearer token middleware returns auth token',
|
|
16
|
-
expected: {locals: {auth: {bearer: 'macaroon'}}},
|
|
17
|
-
},
|
|
18
|
-
];
|
|
19
|
-
|
|
20
|
-
tests.forEach(({args, description, expected}) => {
|
|
21
|
-
return test(description, (t, end) => {
|
|
22
|
-
const {middleware} = bearerToken({});
|
|
23
|
-
const res = {locals: {}};
|
|
24
|
-
|
|
25
|
-
return middleware({get: key => args.header}, res, () => {
|
|
26
|
-
deepStrictEqual(res.locals, expected.locals, 'Got expected locals');
|
|
27
|
-
|
|
28
|
-
return end();
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
});
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
const {deepStrictEqual} = require('node:assert').strict;
|
|
2
|
-
const test = require('node:test');
|
|
3
|
-
const {throws} = require('node:assert').strict;
|
|
4
|
-
|
|
5
|
-
const {encode} = require('cbor');
|
|
6
|
-
|
|
7
|
-
const decodeCborBody = require('./../../lnd_gateway/decode_cbor_body');
|
|
8
|
-
|
|
9
|
-
const tests = [
|
|
10
|
-
{
|
|
11
|
-
args: {body: Buffer.alloc(3)},
|
|
12
|
-
description: 'CBOR middleware expects CBOR encoded data',
|
|
13
|
-
error: [400, 'ExpectedCborRequestArgs'],
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
args: {body: encode({arguments: 'arguments'})},
|
|
17
|
-
description: 'CBOR middleware decodes cbor encoded data',
|
|
18
|
-
expected: {body: {arguments: 'arguments'}},
|
|
19
|
-
},
|
|
20
|
-
];
|
|
21
|
-
|
|
22
|
-
tests.forEach(({args, description, error, expected}) => {
|
|
23
|
-
return test(description, (t, end) => {
|
|
24
|
-
const {middleware} = decodeCborBody({});
|
|
25
|
-
const req = {body: args.body};
|
|
26
|
-
|
|
27
|
-
return middleware(req, null, err => {
|
|
28
|
-
if (!!error) {
|
|
29
|
-
deepStrictEqual(err, error, 'Got expected error');
|
|
30
|
-
} else {
|
|
31
|
-
deepStrictEqual(req.body, expected.body, 'Got expected body');
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return end();
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
});
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
const EventEmitter = require('events');
|
|
2
|
-
const {deepStrictEqual} = require('node:assert').strict;
|
|
3
|
-
const {once} = require('events');
|
|
4
|
-
const {rejects} = require('node:assert').strict;
|
|
5
|
-
const test = require('node:test');
|
|
6
|
-
|
|
7
|
-
const {encode} = require('cbor');
|
|
8
|
-
|
|
9
|
-
const emitEvent = require('./../../lnd_gateway/emit_event');
|
|
10
|
-
|
|
11
|
-
const all = promise => Promise.all(promise);
|
|
12
|
-
|
|
13
|
-
const tests = [
|
|
14
|
-
{
|
|
15
|
-
args: {message: encode({event: 'event', data: 'data'})},
|
|
16
|
-
description: 'Emitter emits CBOR encoded event data',
|
|
17
|
-
expected: {event: 'event', emitted: 'data'},
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
args: {message: Buffer.alloc(3)},
|
|
21
|
-
description: 'Emitter emits error when CBOR is invalid',
|
|
22
|
-
error: new Error('FailedToDecodeGatewayResponse'),
|
|
23
|
-
},
|
|
24
|
-
];
|
|
25
|
-
|
|
26
|
-
tests.forEach(({args, description, error, expected}) => {
|
|
27
|
-
return test(description, async () => {
|
|
28
|
-
const emitter = new EventEmitter();
|
|
29
|
-
const message = args.message;
|
|
30
|
-
|
|
31
|
-
const emit = new Promise((resolve, reject) => {
|
|
32
|
-
emitEvent({emitter, message});
|
|
33
|
-
|
|
34
|
-
return resolve();
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
const waitForError = new Promise((resolve, reject) => {
|
|
38
|
-
emitter.on('error', err => reject(err));
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
if (!!error) {
|
|
42
|
-
await rejects(all([emit, waitForError]), error, 'Got error');
|
|
43
|
-
} else {
|
|
44
|
-
const [, [emitted]] = await all([emit, once(emitter, expected.event)]);
|
|
45
|
-
|
|
46
|
-
deepStrictEqual(emitted, expected.emitted, 'Got expected emitted data');
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return;
|
|
50
|
-
});
|
|
51
|
-
});
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
const {equal} = require('node:assert').strict;
|
|
2
|
-
const EventEmitter = require('events');
|
|
3
|
-
const test = require('node:test');
|
|
4
|
-
|
|
5
|
-
const {emitGrpcEvents} = require('./../../');
|
|
6
|
-
|
|
7
|
-
const tests = [
|
|
8
|
-
{
|
|
9
|
-
args: {message: Buffer.alloc(3)},
|
|
10
|
-
description: 'Event is emitted',
|
|
11
|
-
expected: {
|
|
12
|
-
message: 'a26464617461a16764657461696c73781a457870656374656456616c696443626f7257734d657373616765656576656e74656572726f72',
|
|
13
|
-
},
|
|
14
|
-
},
|
|
15
|
-
];
|
|
16
|
-
|
|
17
|
-
tests.forEach(({args, description, expected}) => {
|
|
18
|
-
return test(description, (t, end) => {
|
|
19
|
-
const ws = new EventEmitter();
|
|
20
|
-
|
|
21
|
-
ws.send = message => {
|
|
22
|
-
equal(message.toString('hex'), expected.message, 'Got expected msg');
|
|
23
|
-
|
|
24
|
-
return end();
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
emitGrpcEvents({ws});
|
|
28
|
-
|
|
29
|
-
ws.emit('message', args.message);
|
|
30
|
-
});
|
|
31
|
-
});
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
const {equal} = require('node:assert').strict;
|
|
2
|
-
const {rejects} = require('node:assert').strict;
|
|
3
|
-
const test = require('node:test');
|
|
4
|
-
|
|
5
|
-
const encodeResponse = require('./../../lnd_gateway/encode_response');
|
|
6
|
-
|
|
7
|
-
const tests = [
|
|
8
|
-
{
|
|
9
|
-
args: {},
|
|
10
|
-
description: 'Data is required to encode',
|
|
11
|
-
error: [400, 'ExpectedDataToEncodeResponse'],
|
|
12
|
-
},
|
|
13
|
-
{
|
|
14
|
-
args: {data: {}},
|
|
15
|
-
description: 'Event is required to encode',
|
|
16
|
-
error: [400, 'ExpectedEventToEncodeResponse'],
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
args: {data: {}, event: 'event'},
|
|
20
|
-
description: 'Encoded data and event',
|
|
21
|
-
expected: {response: 'a26464617461a0656576656e74656576656e74'},
|
|
22
|
-
},
|
|
23
|
-
];
|
|
24
|
-
|
|
25
|
-
tests.forEach(({args, description, error, expected}) => {
|
|
26
|
-
return test(description, async () => {
|
|
27
|
-
if (!!error) {
|
|
28
|
-
await rejects(encodeResponse(args), error, 'Got expected error');
|
|
29
|
-
} else {
|
|
30
|
-
const {response} = await encodeResponse(args);
|
|
31
|
-
|
|
32
|
-
equal(response.toString('hex'), expected.response, 'Encoded response');
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return;
|
|
36
|
-
});
|
|
37
|
-
});
|