@integraledger/profile-facilitator 0.3.0 → 0.3.1
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/README.md +1 -1
- package/dist/index.js +119 -108
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -384,7 +384,7 @@ The package's entry point exports:
|
|
|
384
384
|
|
|
385
385
|
| Export | Kind | What it is |
|
|
386
386
|
|---|---|---|
|
|
387
|
-
| `serveProfileFacilitator(config)` | `(c: ProfileFacilitatorConfig) => Promise<{ close(): Promise<void> }>` | Opens the store, starts the HTTP server and resolves once it listens. |
|
|
387
|
+
| `serveProfileFacilitator(config)` | `(c: ProfileFacilitatorConfig) => Promise<{ close(): Promise<void> }>` | Opens the store, starts the HTTP server and resolves once it listens. If it cannot start, it closes the store and rejects with the error that stopped it, such as the listen's `EADDRINUSE`. |
|
|
388
388
|
| `ProfileFacilitatorConfig` | interface | The configuration above. |
|
|
389
389
|
|
|
390
390
|
### HTTP API
|
package/dist/index.js
CHANGED
|
@@ -35,120 +35,131 @@ export async function serveProfileFacilitator(c) {
|
|
|
35
35
|
const settleWaitMs = Number.isFinite(c.settleWaitMs) && c.settleWaitMs > 0 ? c.settleWaitMs : DEFAULT_SETTLE_WAIT_MS;
|
|
36
36
|
const log = guarded(c.operatorLog ?? operatorLine);
|
|
37
37
|
const store = await openStore(c.store.url, log);
|
|
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
|
-
return
|
|
70
|
-
const rail = railOf(r);
|
|
71
|
-
if (rail === undefined)
|
|
72
|
-
return { isValid: false, invalidReason: "invalid_network" };
|
|
73
|
-
try {
|
|
74
|
-
return await rail.verify(r);
|
|
75
|
-
}
|
|
76
|
-
catch (e) {
|
|
77
|
-
return invalid(unexpectedVerify(log, { event: "verify-error", network: r.paymentRequirements.network }, e));
|
|
38
|
+
// If the facilitator does not start, what it opened is closed before its error goes to the caller unchanged: the
|
|
39
|
+
// store's pool and sweep, and the Polkadot rail, which opens a node's client only on that node's first request.
|
|
40
|
+
let closeNodes = async () => { };
|
|
41
|
+
try {
|
|
42
|
+
const tron = createTron((c.tron ?? []).map((n) => ({ ...n, fullNode: trimmed(n.fullNode), solidityNode: trimmed(n.solidityNode) })), store, settleWaitMs, log);
|
|
43
|
+
const polkadot = createPolkadot((c.polkadot ?? []).map((n) => ({ ...n, rpc: trimmed(n.rpc) })), store, settleWaitMs, log);
|
|
44
|
+
closeNodes = () => polkadot.close();
|
|
45
|
+
const supported = {
|
|
46
|
+
kinds: [
|
|
47
|
+
...tron.networks.map((network) => ({
|
|
48
|
+
x402Version: 2,
|
|
49
|
+
scheme: "exact",
|
|
50
|
+
network,
|
|
51
|
+
extra: { assetTransferMethod: LCP_TRC20_MEMO },
|
|
52
|
+
})),
|
|
53
|
+
...polkadot.networks.map((network) => ({
|
|
54
|
+
x402Version: 2,
|
|
55
|
+
scheme: "exact",
|
|
56
|
+
network,
|
|
57
|
+
extra: { assetTransferMethod: LCP_ASSETS_REMARK },
|
|
58
|
+
})),
|
|
59
|
+
],
|
|
60
|
+
extensions: [],
|
|
61
|
+
signers: {},
|
|
62
|
+
};
|
|
63
|
+
function railOf(r) {
|
|
64
|
+
const n = r.paymentRequirements.network;
|
|
65
|
+
if (tron.networks.includes(n))
|
|
66
|
+
return tron;
|
|
67
|
+
if (polkadot.networks.includes(n))
|
|
68
|
+
return polkadot;
|
|
69
|
+
return undefined;
|
|
78
70
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
71
|
+
async function verify(body) {
|
|
72
|
+
const r = readRequest(body);
|
|
73
|
+
if (typeof r === "string")
|
|
74
|
+
return { isValid: false, invalidReason: r };
|
|
75
|
+
const rail = railOf(r);
|
|
76
|
+
if (rail === undefined)
|
|
77
|
+
return { isValid: false, invalidReason: "invalid_network" };
|
|
78
|
+
try {
|
|
79
|
+
return await rail.verify(r);
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
return invalid(unexpectedVerify(log, { event: "verify-error", network: r.paymentRequirements.network }, e));
|
|
83
|
+
}
|
|
90
84
|
}
|
|
91
|
-
|
|
92
|
-
|
|
85
|
+
async function settle(body) {
|
|
86
|
+
const r = readRequest(body);
|
|
87
|
+
if (typeof r === "string")
|
|
88
|
+
return failed(r, "", networkOf(body));
|
|
89
|
+
const network = r.paymentRequirements.network;
|
|
90
|
+
const rail = railOf(r);
|
|
91
|
+
if (rail === undefined)
|
|
92
|
+
return failed("invalid_network", "", network);
|
|
93
|
+
try {
|
|
94
|
+
return await rail.settle(r);
|
|
95
|
+
}
|
|
96
|
+
catch (e) {
|
|
97
|
+
return unexpectedSettle(log, { event: "settle-error", network }, e);
|
|
98
|
+
}
|
|
93
99
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
100
|
+
async function handle(req, res) {
|
|
101
|
+
const path = (req.url ?? "").split("?")[0];
|
|
102
|
+
if (path === "/supported") {
|
|
103
|
+
if (req.method !== "GET")
|
|
104
|
+
return sendJson(res, 405, { error: "method not allowed" });
|
|
105
|
+
return sendJson(res, 200, supported);
|
|
106
|
+
}
|
|
107
|
+
if (path !== "/verify" && path !== "/settle")
|
|
108
|
+
return sendJson(res, 404, { error: "not found" });
|
|
109
|
+
if (req.method !== "POST")
|
|
99
110
|
return sendJson(res, 405, { error: "method not allowed" });
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
if (path === "/verify") {
|
|
111
|
+
const read = await readRequestJson(req);
|
|
112
|
+
if (read !== undefined && "tooLarge" in read) {
|
|
113
|
+
res.setHeader("connection", "close");
|
|
114
|
+
sendJson(res, 413, { error: "the body is larger than 64 KiB" });
|
|
115
|
+
req.destroy();
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (read !== undefined && "left" in read) {
|
|
119
|
+
// The client left before its body was complete: there is no one to answer, and nothing failed here to log.
|
|
120
|
+
res.destroy();
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (path === "/verify") {
|
|
124
|
+
if (read === undefined)
|
|
125
|
+
return sendJson(res, 400, { isValid: false, invalidReason: "invalid_payload" });
|
|
126
|
+
return sendJson(res, 200, await verify(read.json));
|
|
127
|
+
}
|
|
119
128
|
if (read === undefined)
|
|
120
|
-
return sendJson(res, 400,
|
|
121
|
-
return sendJson(res, 200, await
|
|
129
|
+
return sendJson(res, 400, failed("invalid_payload", "", ""));
|
|
130
|
+
return sendJson(res, 200, await settle(read.json));
|
|
122
131
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
sendJson(res, 500, { error: "internal error" });
|
|
132
|
-
else
|
|
133
|
-
res.destroy();
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
server.requestTimeout = settleWaitMs + 60_000;
|
|
137
|
-
await new Promise((resolve, reject) => {
|
|
138
|
-
server.once("error", reject);
|
|
139
|
-
server.listen(port, host, () => {
|
|
140
|
-
server.off("error", reject);
|
|
141
|
-
resolve();
|
|
132
|
+
const server = createServer((req, res) => {
|
|
133
|
+
handle(req, res).catch((e) => {
|
|
134
|
+
log({ event: "request-error", path: (req.url ?? "").split("?")[0] }, e);
|
|
135
|
+
if (!res.headersSent)
|
|
136
|
+
sendJson(res, 500, { error: "internal error" });
|
|
137
|
+
else
|
|
138
|
+
res.destroy();
|
|
139
|
+
});
|
|
142
140
|
});
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
server.
|
|
148
|
-
|
|
141
|
+
server.requestTimeout = settleWaitMs + 60_000;
|
|
142
|
+
await new Promise((resolve, reject) => {
|
|
143
|
+
server.once("error", reject);
|
|
144
|
+
server.listen(port, host, () => {
|
|
145
|
+
server.off("error", reject);
|
|
146
|
+
resolve();
|
|
149
147
|
});
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
148
|
+
});
|
|
149
|
+
return {
|
|
150
|
+
async close() {
|
|
151
|
+
await new Promise((resolve) => {
|
|
152
|
+
server.close(() => resolve());
|
|
153
|
+
server.closeAllConnections();
|
|
154
|
+
});
|
|
155
|
+
await polkadot.close();
|
|
156
|
+
await store.close();
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
catch (e) {
|
|
161
|
+
await closeNodes();
|
|
162
|
+
await store.close();
|
|
163
|
+
throw e;
|
|
164
|
+
}
|
|
154
165
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@integraledger/profile-facilitator",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "An x402 facilitator for the LCP profiles x402/exact/tron/lcp-trc20-memo and x402/exact/polkadot/lcp-assets-remark: verifies and settles payments whose signed transaction carries the ATR hash.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"engines": {
|