@konstantinbozukov/plugin-edge-signals 0.1.0 → 0.1.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/dist/index.js CHANGED
@@ -1,3 +1,50 @@
1
+ // src/read-response-json.ts
2
+ var MAX_RESPONSE_BYTES = 64 * 1024;
3
+ var ResponseTooLargeError = class extends Error {
4
+ constructor(maxBytes) {
5
+ super(`Response body exceeds ${maxBytes} bytes`);
6
+ this.name = "ResponseTooLargeError";
7
+ }
8
+ };
9
+ async function readTextLimited(res, maxBytes) {
10
+ if (!res.body) {
11
+ const text = await res.text();
12
+ if (text.length > maxBytes) {
13
+ throw new ResponseTooLargeError(maxBytes);
14
+ }
15
+ return text;
16
+ }
17
+ const reader = res.body.getReader();
18
+ const chunks = [];
19
+ let total = 0;
20
+ try {
21
+ while (true) {
22
+ const { done, value } = await reader.read();
23
+ if (done) break;
24
+ total += value.byteLength;
25
+ if (total > maxBytes) {
26
+ await reader.cancel();
27
+ throw new ResponseTooLargeError(maxBytes);
28
+ }
29
+ chunks.push(value);
30
+ }
31
+ } catch (err) {
32
+ await reader.cancel().catch(() => void 0);
33
+ throw err;
34
+ }
35
+ const merged = new Uint8Array(total);
36
+ let offset = 0;
37
+ for (const chunk of chunks) {
38
+ merged.set(chunk, offset);
39
+ offset += chunk.byteLength;
40
+ }
41
+ return new TextDecoder().decode(merged);
42
+ }
43
+ async function readResponseJsonLimited(res, maxBytes = MAX_RESPONSE_BYTES) {
44
+ const text = await readTextLimited(res, maxBytes);
45
+ return JSON.parse(text);
46
+ }
47
+
1
48
  // src/client.ts
2
49
  var BASE = "https://signals.edge.report";
3
50
  var fetchFn = null;
@@ -21,7 +68,7 @@ function buildTokenVerdictUrl(token, chain = "base") {
21
68
  async function fetchTokenVerdict(token, chain = "base") {
22
69
  const res = await getEdgeSignalsFetch()(buildTokenVerdictUrl(token, chain));
23
70
  if (!res.ok) throw new Error(`Token Verdict HTTP ${res.status}`);
24
- return await res.json();
71
+ return await readResponseJsonLimited(res);
25
72
  }
26
73
  function extractErc20Address(text) {
27
74
  const match = text.match(/0x[a-fA-F0-9]{40}/);
@@ -39,7 +86,7 @@ var tokenVerdictBeforeSwapAction = {
39
86
  "EDGE_SIGNALS_VERDICT",
40
87
  "SCREEN_TOKEN_BEFORE_TRADE"
41
88
  ],
42
- description: "Pay $0.01 for Edge Signals Token Verdict on an ERC-20 before approving or swapping.",
89
+ description: "Pay for Edge Signals Token Verdict on an ERC-20 before approving or swapping (x402 price from HTTP 402; currently about $0.01 USDC on Base).",
43
90
  validate: async (_runtime, message) => {
44
91
  const text = message.content?.text ?? "";
45
92
  return /swap|buy|trade|verdict|risk/i.test(text) && !!extractErc20Address(text);
@@ -139,7 +186,7 @@ var edgeSignalsPlugin = {
139
186
  init: async (_config, runtime) => {
140
187
  const pk = runtime.getSetting("X402_PAYER_PRIVATE_KEY") ?? process.env.X402_PAYER_PRIVATE_KEY ?? process.env.EDGE_SIGNALS_PAYER_PRIVATE_KEY;
141
188
  if (pk) {
142
- const { loadX402Fetch } = await import("./loadX402Fetch-OGOTSAUV.js");
189
+ const { loadX402Fetch } = await import("./loadX402Fetch-4XMVGC5F.js");
143
190
  setEdgeSignalsFetch(await loadX402Fetch(pk));
144
191
  }
145
192
  }
@@ -0,0 +1,77 @@
1
+ // src/safe-fetch.ts
2
+ var PAYMENT_HEADER_NAMES = ["payment-signature", "x-payment"];
3
+ var SafeFetchError = class extends Error {
4
+ constructor(message) {
5
+ super(message);
6
+ this.name = "SafeFetchError";
7
+ }
8
+ };
9
+ function isLocalhostHost(hostname) {
10
+ return hostname === "localhost" || hostname === "127.0.0.1" || hostname === "[::1]";
11
+ }
12
+ function resolveRequestUrl(input) {
13
+ if (input instanceof URL) return input;
14
+ if (typeof input === "string") return new URL(input);
15
+ return new URL(input.url);
16
+ }
17
+ function assertAllowedBaseUrl(url, allowHttpLocalhost = false) {
18
+ if (url.protocol === "https:") return;
19
+ if (allowHttpLocalhost && url.protocol === "http:" && isLocalhostHost(url.hostname)) {
20
+ return;
21
+ }
22
+ throw new SafeFetchError(
23
+ `Refused non-HTTPS URL for paid Edge Signals calls: ${url.href}`
24
+ );
25
+ }
26
+ function hasPaymentHeaders(headers) {
27
+ return PAYMENT_HEADER_NAMES.some((name) => headers.has(name));
28
+ }
29
+ function stripPaymentHeaders(headers) {
30
+ for (const name of PAYMENT_HEADER_NAMES) headers.delete(name);
31
+ }
32
+ function createSafeFetch(fetchFn, options = {}) {
33
+ let paymentAllowedHost = null;
34
+ return async (input, init) => {
35
+ const url = resolveRequestUrl(input);
36
+ assertAllowedBaseUrl(url, options.allowHttpLocalhost);
37
+ const headers = new Headers(init?.headers);
38
+ const host = url.hostname;
39
+ if (hasPaymentHeaders(headers)) {
40
+ if (paymentAllowedHost === null) {
41
+ paymentAllowedHost = host;
42
+ } else if (host !== paymentAllowedHost) {
43
+ stripPaymentHeaders(headers);
44
+ }
45
+ }
46
+ const res = await fetchFn(input, {
47
+ ...init,
48
+ redirect: "manual",
49
+ headers
50
+ });
51
+ if (res.status >= 300 && res.status < 400) {
52
+ throw new SafeFetchError(
53
+ `HTTP ${res.status} redirect refused (x402 paid calls must not follow redirects)`
54
+ );
55
+ }
56
+ if (res.status === 402) {
57
+ paymentAllowedHost = host;
58
+ }
59
+ return res;
60
+ };
61
+ }
62
+
63
+ // src/loadX402Fetch.ts
64
+ async function loadX402Fetch(privateKey) {
65
+ const { privateKeyToAccount } = await import("viem/accounts");
66
+ const { x402Client, wrapFetchWithPayment } = await import("@x402/fetch");
67
+ const { registerExactEvmScheme } = await import("@x402/evm/exact/client/register");
68
+ const client = registerExactEvmScheme(new x402Client(), {
69
+ signer: privateKeyToAccount(privateKey)
70
+ });
71
+ const baseFetch = createSafeFetch(fetch);
72
+ const payingFetch = wrapFetchWithPayment(baseFetch, client);
73
+ return createSafeFetch(payingFetch);
74
+ }
75
+ export {
76
+ loadX402Fetch
77
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@konstantinbozukov/plugin-edge-signals",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "ElizaOS plugin: Token Verdict before swaps (Edge Signals x402)",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -1,13 +0,0 @@
1
- // src/loadX402Fetch.ts
2
- async function loadX402Fetch(privateKey) {
3
- const { privateKeyToAccount } = await import("viem/accounts");
4
- const { x402Client, wrapFetchWithPayment } = await import("@x402/fetch");
5
- const { registerExactEvmScheme } = await import("@x402/evm/exact/client/register");
6
- const client = registerExactEvmScheme(new x402Client(), {
7
- signer: privateKeyToAccount(privateKey)
8
- });
9
- return wrapFetchWithPayment(fetch, client);
10
- }
11
- export {
12
- loadX402Fetch
13
- };