@integraledger/profile-facilitator 0.3.0 → 0.4.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.
Files changed (3) hide show
  1. package/README.md +6 -6
  2. package/dist/index.js +119 -108
  3. package/package.json +4 -4
package/README.md CHANGED
@@ -20,7 +20,7 @@ Approving that transaction is then the buyer's acceptance of that exact record,
20
20
  | `x402/exact/polkadot/lcp-assets-remark` | `polkadot:68d56f15f85d3136970ec16946040bc1` (Polkadot Asset Hub) or `polkadot:67f9723393ef76214df0118c34bbbd3d` (Westend Asset Hub) | `lcp-assets-remark` | `system.remark_with_event(R)`, where R is exactly `lcp:sha256:` followed by H in lowercase hex, batched with `assets.transfer_keep_alive` in one `utility.batch_all`. |
21
21
 
22
22
  This package implements each profile's rule 5, the facilitator's duties. The profiles themselves are published with
23
- `@integraledger/lcp`, in [its repository at 0.4.0](https://github.com/IntegraLedger/integra-protocol/tree/v0.4.0/lcp/profiles), the release this package pins.
23
+ `@integraledger/lcp`, in [its repository at 0.5.0](https://github.com/IntegraLedger/integra-protocol/tree/v0.5.0/lcp/profiles), the release this package pins.
24
24
 
25
25
  The facilitator is a pass-through. It checks the payment against the requirements it is given, asks a node to
26
26
  validate or simulate it, submits exactly the bytes the payer signed, deduplicates, and reports what the network shows.
@@ -61,14 +61,14 @@ sequenceDiagram
61
61
  ## Install
62
62
 
63
63
  ```sh
64
- npm install @integraledger/profile-facilitator @integraledger/lcp@~0.4.0
64
+ npm install @integraledger/profile-facilitator @integraledger/lcp@~0.5.0
65
65
  ```
66
66
 
67
- It needs Node.js `>=26.11.1`, Postgres (the tests run on Postgres 18), and an HTTP endpoint for each network you
67
+ It needs Node.js `>=24`, Postgres (the tests run on Postgres 18), and an HTTP endpoint for each network you
68
68
  serve: a Tron FullNode that keeps the block history (not a lite FullNode) and a Solidity node, or a Polkadot Asset Hub
69
69
  RPC node. A Polkadot node that keeps the state of every block (an archive node) gives the events of any block; a
70
70
  pruning node discards the state of older blocks, and a block that holds the extrinsic and whose state is discarded
71
- keeps the answer `settlement_pending`. It is ESM only. `@integraledger/lcp` (`~0.4.0`) is its peer dependency: install
71
+ keeps the answer `settlement_pending`. It is ESM only. `@integraledger/lcp` (`~0.5.0`) is its peer dependency: install
72
72
  it beside it.
73
73
 
74
74
  ## Quickstart
@@ -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
@@ -559,7 +559,7 @@ false. On Polkadot they also cover a repeat of a consumed payment, a block answe
559
559
 
560
560
  ## Requirements
561
561
 
562
- - Node.js `>=26.11.1`.
562
+ - Node.js `>=24`.
563
563
  - Postgres; the tests run on Postgres 18.
564
564
  - For Tron, a FullNode and a Solidity node with java-tron's HTTP API: the FullNode serves `/wallet/getaccount`,
565
565
  `/wallet/getnowblock`, `/wallet/getblock`, `/wallet/gettransactionbyid`, `/wallet/gettransactionfrompending`,
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
- const tron = createTron((c.tron ?? []).map((n) => ({ ...n, fullNode: trimmed(n.fullNode), solidityNode: trimmed(n.solidityNode) })), store, settleWaitMs, log);
39
- const polkadot = createPolkadot((c.polkadot ?? []).map((n) => ({ ...n, rpc: trimmed(n.rpc) })), store, settleWaitMs, log);
40
- const supported = {
41
- kinds: [
42
- ...tron.networks.map((network) => ({
43
- x402Version: 2,
44
- scheme: "exact",
45
- network,
46
- extra: { assetTransferMethod: LCP_TRC20_MEMO },
47
- })),
48
- ...polkadot.networks.map((network) => ({
49
- x402Version: 2,
50
- scheme: "exact",
51
- network,
52
- extra: { assetTransferMethod: LCP_ASSETS_REMARK },
53
- })),
54
- ],
55
- extensions: [],
56
- signers: {},
57
- };
58
- function railOf(r) {
59
- const n = r.paymentRequirements.network;
60
- if (tron.networks.includes(n))
61
- return tron;
62
- if (polkadot.networks.includes(n))
63
- return polkadot;
64
- return undefined;
65
- }
66
- async function verify(body) {
67
- const r = readRequest(body);
68
- if (typeof r === "string")
69
- return { isValid: false, invalidReason: r };
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
- async function settle(body) {
81
- const r = readRequest(body);
82
- if (typeof r === "string")
83
- return failed(r, "", networkOf(body));
84
- const network = r.paymentRequirements.network;
85
- const rail = railOf(r);
86
- if (rail === undefined)
87
- return failed("invalid_network", "", network);
88
- try {
89
- return await rail.settle(r);
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
- catch (e) {
92
- return unexpectedSettle(log, { event: "settle-error", network }, e);
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
- async function handle(req, res) {
96
- const path = (req.url ?? "").split("?")[0];
97
- if (path === "/supported") {
98
- if (req.method !== "GET")
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
- return sendJson(res, 200, supported);
101
- }
102
- if (path !== "/verify" && path !== "/settle")
103
- return sendJson(res, 404, { error: "not found" });
104
- if (req.method !== "POST")
105
- return sendJson(res, 405, { error: "method not allowed" });
106
- const read = await readRequestJson(req);
107
- if (read !== undefined && "tooLarge" in read) {
108
- res.setHeader("connection", "close");
109
- sendJson(res, 413, { error: "the body is larger than 64 KiB" });
110
- req.destroy();
111
- return;
112
- }
113
- if (read !== undefined && "left" in read) {
114
- // The client left before its body was complete: there is no one to answer, and nothing failed here to log.
115
- res.destroy();
116
- return;
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, { isValid: false, invalidReason: "invalid_payload" });
121
- return sendJson(res, 200, await verify(read.json));
129
+ return sendJson(res, 400, failed("invalid_payload", "", ""));
130
+ return sendJson(res, 200, await settle(read.json));
122
131
  }
123
- if (read === undefined)
124
- return sendJson(res, 400, failed("invalid_payload", "", ""));
125
- return sendJson(res, 200, await settle(read.json));
126
- }
127
- const server = createServer((req, res) => {
128
- handle(req, res).catch((e) => {
129
- log({ event: "request-error", path: (req.url ?? "").split("?")[0] }, e);
130
- if (!res.headersSent)
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
- return {
145
- async close() {
146
- await new Promise((resolve) => {
147
- server.close(() => resolve());
148
- server.closeAllConnections();
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
- await polkadot.close();
151
- await store.close();
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,10 +1,10 @@
1
1
  {
2
2
  "name": "@integraledger/profile-facilitator",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
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": {
7
- "node": ">=26.11.1"
7
+ "node": ">=24"
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
@@ -39,7 +39,7 @@
39
39
  "LICENSE"
40
40
  ],
41
41
  "peerDependencies": {
42
- "@integraledger/lcp": "~0.4.0"
42
+ "@integraledger/lcp": "~0.5.0"
43
43
  },
44
44
  "dependencies": {
45
45
  "@noble/curves": "2.4.0",
@@ -48,7 +48,7 @@
48
48
  "pg": "8.23.1"
49
49
  },
50
50
  "devDependencies": {
51
- "@integraledger/lcp": "0.4.0",
51
+ "@integraledger/lcp": "0.5.0",
52
52
  "vitest": "5.0.4"
53
53
  },
54
54
  "scripts": {