nirium 0.15.0 → 0.16.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 +159 -37
- package/dist/index.d.ts +27 -0
- package/dist/index.js +78 -15
- package/dist/x402-policy.d.ts +178 -0
- package/dist/x402-policy.js +334 -0
- package/dist/x402-spend-cap.d.ts +65 -0
- package/dist/x402-spend-cap.js +129 -0
- package/dist/x402serve-smoke.test.js +10 -2
- package/package.json +12 -8
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ═══════════════════════════════════════════════════════════════
|
|
3
|
+
// Error claro para el tope por pago de @x402/core.
|
|
4
|
+
//
|
|
5
|
+
// Desde @x402/core 2.23.0 el cliente rechaza, al seleccionar los requisitos
|
|
6
|
+
// y antes de crear el payload, todo pago por encima de un tope por defecto
|
|
7
|
+
// ($1). Lo hace con un Error genérico que x402Fetch re-envuelve
|
|
8
|
+
// ("Failed to create payment payload: All payment requirements were
|
|
9
|
+
// rejected by spendControls.maxAmountPerPayment (...)"), sin decir cuánto se
|
|
10
|
+
// pedía ni que nadie firmó nada. Este módulo reconoce ese caso y lo convierte
|
|
11
|
+
// en un error que lo dice. No cambia el tope ni lo expone: solo lo explica.
|
|
12
|
+
// ═══════════════════════════════════════════════════════════════
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.X402SpendCapError = void 0;
|
|
15
|
+
exports.formatAtomic = formatAtomic;
|
|
16
|
+
exports.readOfferedRequirements = readOfferedRequirements;
|
|
17
|
+
exports.captureRequirements = captureRequirements;
|
|
18
|
+
exports.toSpendCapError = toSpendCapError;
|
|
19
|
+
/**
|
|
20
|
+
* x402Fetch refused to pay because the amount the server asked for is above
|
|
21
|
+
* the per-payment cap enforced by @x402/core. Thrown before the signer is
|
|
22
|
+
* reached: nothing was signed, nothing was sent.
|
|
23
|
+
*/
|
|
24
|
+
class X402SpendCapError extends Error {
|
|
25
|
+
code = 'X402_SPEND_CAP_EXCEEDED';
|
|
26
|
+
/** Always false for this error: the cap is checked before any signing. */
|
|
27
|
+
signerCalled = false;
|
|
28
|
+
url;
|
|
29
|
+
amount;
|
|
30
|
+
asset;
|
|
31
|
+
network;
|
|
32
|
+
cap;
|
|
33
|
+
formattedAmount;
|
|
34
|
+
constructor(d) {
|
|
35
|
+
const asked = d.formattedAmount
|
|
36
|
+
? `${d.formattedAmount} (${d.amount} atomic units)`
|
|
37
|
+
: d.amount !== undefined
|
|
38
|
+
? `${d.amount} atomic units${d.asset ? ` of ${d.asset}` : ''}`
|
|
39
|
+
: 'more than the cap';
|
|
40
|
+
const cap = d.cap ? `the per-payment cap is ${d.cap}` : 'it is above the per-payment cap';
|
|
41
|
+
super(`x402Fetch refused to pay ${d.url}: the server asked for ${asked} and ${cap}. `
|
|
42
|
+
+ 'The signer was not called; nothing was signed or sent. '
|
|
43
|
+
+ 'The cap is enforced by @x402/core and cannot be changed from initX402() yet.', d.cause !== undefined ? { cause: d.cause } : undefined);
|
|
44
|
+
this.name = 'X402SpendCapError';
|
|
45
|
+
this.url = d.url;
|
|
46
|
+
this.amount = d.amount;
|
|
47
|
+
this.asset = d.asset;
|
|
48
|
+
this.network = d.network;
|
|
49
|
+
this.cap = d.cap;
|
|
50
|
+
this.formattedAmount = d.formattedAmount;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.X402SpendCapError = X402SpendCapError;
|
|
54
|
+
const isAtomic = (v) => typeof v === 'string' && /^\d+$/.test(v);
|
|
55
|
+
/** `20000000` with 7 decimals and `USDC` -> `2 USDC`. */
|
|
56
|
+
function formatAtomic(amount, decimals, symbol) {
|
|
57
|
+
const raw = BigInt(amount);
|
|
58
|
+
const base = 10n ** BigInt(decimals);
|
|
59
|
+
const whole = raw / base;
|
|
60
|
+
const frac = (raw % base).toString().padStart(decimals, '0').replace(/0+$/, '');
|
|
61
|
+
return `${frac ? `${whole}.${frac}` : whole.toString()} ${symbol}`;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Reads the `accepts` list out of a 402 response's PAYMENT-REQUIRED header
|
|
65
|
+
* (base64 JSON, x402 v2). Returns undefined if it is absent or unreadable;
|
|
66
|
+
* the error is still produced, just without the amount.
|
|
67
|
+
*/
|
|
68
|
+
function readOfferedRequirements(response) {
|
|
69
|
+
try {
|
|
70
|
+
const header = response.headers.get('payment-required');
|
|
71
|
+
if (!header)
|
|
72
|
+
return undefined;
|
|
73
|
+
const bytes = Uint8Array.from(atob(header), (c) => c.charCodeAt(0));
|
|
74
|
+
const accepts = JSON.parse(new TextDecoder().decode(bytes))?.accepts;
|
|
75
|
+
return Array.isArray(accepts) ? accepts : undefined;
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Wraps a fetch so the requirements of every 402 it sees are written to
|
|
83
|
+
* `sink`. Passive: the response is returned untouched.
|
|
84
|
+
*/
|
|
85
|
+
function captureRequirements(base, sink) {
|
|
86
|
+
return (async (input, init) => {
|
|
87
|
+
const response = await base(input, init);
|
|
88
|
+
if (response.status === 402) {
|
|
89
|
+
const offered = readOfferedRequirements(response);
|
|
90
|
+
if (offered)
|
|
91
|
+
sink.offered = offered;
|
|
92
|
+
}
|
|
93
|
+
return response;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
// El mensaje de @x402/core que reconocemos. Si una versión futura lo cambia,
|
|
97
|
+
// el error genérico vuelve a salir tal cual (sin perder nada) y el test que lo
|
|
98
|
+
// fija (test/x402-spend-cap.test.ts) falla al subir la dependencia.
|
|
99
|
+
const CAP_REJECTION = /rejected by spendControls\.maxAmountPerPayment(?: \(([^)]*)\))?/;
|
|
100
|
+
/**
|
|
101
|
+
* If `error` is @x402/core refusing a payment for being above its
|
|
102
|
+
* per-payment USD cap, returns the clear error; otherwise null so the caller
|
|
103
|
+
* rethrows the original. Other spendControls rejections (for example a
|
|
104
|
+
* non-default asset) are deliberately left alone.
|
|
105
|
+
*/
|
|
106
|
+
function toSpendCapError(error, url, offered, describeAsset) {
|
|
107
|
+
const message = error instanceof Error ? error.message : '';
|
|
108
|
+
const hit = CAP_REJECTION.exec(message);
|
|
109
|
+
if (!hit)
|
|
110
|
+
return null;
|
|
111
|
+
// "($1, including USDC)" -> "$1"
|
|
112
|
+
const cap = hit[1]?.split(',')[0]?.trim() || undefined;
|
|
113
|
+
const cheapest = (offered ?? [])
|
|
114
|
+
.filter((o) => isAtomic(o.amount))
|
|
115
|
+
.sort((a, b) => (BigInt(a.amount) < BigInt(b.amount) ? -1 : BigInt(a.amount) > BigInt(b.amount) ? 1 : 0))[0];
|
|
116
|
+
let formattedAmount;
|
|
117
|
+
if (cheapest?.asset && cheapest.network) {
|
|
118
|
+
try {
|
|
119
|
+
const info = describeAsset?.(cheapest.asset, cheapest.network);
|
|
120
|
+
if (info)
|
|
121
|
+
formattedAmount = formatAtomic(cheapest.amount, info.decimals, info.symbol);
|
|
122
|
+
}
|
|
123
|
+
catch { /* sin formato legible: se informan las unidades atómicas */ }
|
|
124
|
+
}
|
|
125
|
+
return new X402SpendCapError({
|
|
126
|
+
url, cap, formattedAmount, cause: error,
|
|
127
|
+
amount: cheapest?.amount, asset: cheapest?.asset, network: cheapest?.network,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
@@ -27,8 +27,16 @@ jest.mock('@x402/stellar/exact/client', () => ({
|
|
|
27
27
|
ExactStellarScheme: class {
|
|
28
28
|
},
|
|
29
29
|
}));
|
|
30
|
-
jest.mock('mppx', () => ({
|
|
31
|
-
|
|
30
|
+
jest.mock('mppx/client', () => ({
|
|
31
|
+
Mppx: { create: () => ({ fetch: async () => new Response('{}') }) },
|
|
32
|
+
}));
|
|
33
|
+
jest.mock('@stellar/mpp/charge/client', () => ({
|
|
34
|
+
stellar: { charge: () => ({}) },
|
|
35
|
+
}));
|
|
36
|
+
// stellar-sdk 16 loads ESM-only @noble/hashes; index.ts pulls it in through
|
|
37
|
+
// x402-policy.ts. Only the constants read at module load are needed here.
|
|
38
|
+
jest.mock('@stellar/stellar-sdk', () => ({
|
|
39
|
+
Networks: { TESTNET: 'Test SDF Network ; September 2015', PUBLIC: 'Public Global Stellar Network ; September 2015' },
|
|
32
40
|
}));
|
|
33
41
|
const index_1 = require("./index");
|
|
34
42
|
const metrics_1 = require("./metrics");
|
package/package.json
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nirium",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Autonomous treasury and agentic-payments infrastructure for Stellar (x402
|
|
3
|
+
"version": "0.16.1",
|
|
4
|
+
"description": "Autonomous treasury and agentic-payments infrastructure for Stellar (x402)",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"homepage": "https://nirium.xyz",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
|
-
"url": "git+https://github.com/nirium-protocol/nirium
|
|
10
|
+
"url": "git+https://github.com/nirium-protocol/nirium.git",
|
|
11
|
+
"directory": "packages/sdk"
|
|
11
12
|
},
|
|
12
13
|
"bugs": {
|
|
13
|
-
"url": "https://github.com/nirium-protocol/nirium
|
|
14
|
+
"url": "https://github.com/nirium-protocol/nirium/issues",
|
|
14
15
|
"email": "niriumprotocol@gmail.com"
|
|
15
16
|
},
|
|
16
17
|
"author": {
|
|
@@ -22,6 +23,9 @@
|
|
|
22
23
|
"dist",
|
|
23
24
|
"README.md"
|
|
24
25
|
],
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=22.12.0"
|
|
28
|
+
},
|
|
25
29
|
"scripts": {
|
|
26
30
|
"build": "tsc",
|
|
27
31
|
"prepublishOnly": "npm run build",
|
|
@@ -31,9 +35,9 @@
|
|
|
31
35
|
},
|
|
32
36
|
"dependencies": {
|
|
33
37
|
"@stellar/mpp": "^0.7.1",
|
|
34
|
-
"@stellar/stellar-sdk": "^
|
|
35
|
-
"@x402/fetch": "^2.
|
|
36
|
-
"@x402/stellar": "^2.
|
|
38
|
+
"@stellar/stellar-sdk": "^16.3.0",
|
|
39
|
+
"@x402/fetch": "^2.28.0",
|
|
40
|
+
"@x402/stellar": "^2.28.0",
|
|
37
41
|
"mppx": "^0.6.31",
|
|
38
42
|
"viem": "^2.55.19",
|
|
39
43
|
"ws": "^8.21.3"
|
|
@@ -42,7 +46,7 @@
|
|
|
42
46
|
"@types/jest": "^30.0.0",
|
|
43
47
|
"@types/node": "^26.2.0",
|
|
44
48
|
"@types/ws": "^8.18.1",
|
|
45
|
-
"@x402/core": "^2.
|
|
49
|
+
"@x402/core": "^2.28.0",
|
|
46
50
|
"jest": "^30.4.2",
|
|
47
51
|
"ts-jest": "^29.4.12",
|
|
48
52
|
"typescript": "^5.9.3"
|