@agentcash/router 1.5.0 → 1.5.2
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 +80 -17
- package/dist/index.cjs +1811 -1336
- package/dist/index.d.cts +111 -44
- package/dist/index.d.ts +111 -44
- package/dist/index.js +1811 -1336
- package/package.json +3 -3
- package/dist/client/index.cjs +0 -94
- package/dist/client/index.d.cts +0 -86
- package/dist/client/index.d.ts +0 -86
- package/dist/client/index.js +0 -56
- package/dist/siwx-BMlja_nt.d.cts +0 -9
- package/dist/siwx-BMlja_nt.d.ts +0 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentcash/router",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.2",
|
|
4
4
|
"description": "Unified route builder for Next.js App Router APIs with x402, MPP, SIWX, and API key auth",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"@x402/evm": "^2.9.0",
|
|
30
30
|
"@x402/extensions": "^2.9.0",
|
|
31
31
|
"@x402/svm": "^2.9.0",
|
|
32
|
-
"mppx": "^0.5
|
|
32
|
+
"mppx": "^0.6.5",
|
|
33
33
|
"next": ">=15.0.0",
|
|
34
34
|
"zod": "^4.0.0",
|
|
35
35
|
"zod-openapi": "^5.0.0"
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"@x402/extensions": "^2.9.0",
|
|
62
62
|
"@x402/svm": "^2.9.0",
|
|
63
63
|
"eslint": "^10.0.0",
|
|
64
|
-
"mppx": "^0.5
|
|
64
|
+
"mppx": "^0.6.5",
|
|
65
65
|
"next": "^15.0.0",
|
|
66
66
|
"prettier": "^3.8.1",
|
|
67
67
|
"react": "^19.0.0",
|
package/dist/client/index.cjs
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/client/index.ts
|
|
31
|
-
var client_exports = {};
|
|
32
|
-
__export(client_exports, {
|
|
33
|
-
SIWX_ERROR_MESSAGES: () => SIWX_ERROR_MESSAGES,
|
|
34
|
-
fetchWithSiwx: () => fetchWithSiwx
|
|
35
|
-
});
|
|
36
|
-
module.exports = __toCommonJS(client_exports);
|
|
37
|
-
|
|
38
|
-
// src/auth/siwx.ts
|
|
39
|
-
var SIWX_ERROR_MESSAGES = {
|
|
40
|
-
siwx_missing_header: "Missing SIGN-IN-WITH-X header",
|
|
41
|
-
siwx_malformed: "Malformed SIWX payload",
|
|
42
|
-
siwx_expired: "SIWX message expired \u2014 request a new challenge",
|
|
43
|
-
siwx_nonce_used: "Nonce already used \u2014 request a new challenge",
|
|
44
|
-
siwx_invalid_signature: "Invalid signature \u2014 wallet mismatch or corrupted proof"
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
// src/client/index.ts
|
|
48
|
-
async function fetchWithSiwx(url, options) {
|
|
49
|
-
const { signer, headers, ...init } = options;
|
|
50
|
-
const challengeRes = await fetch(url, {
|
|
51
|
-
...init,
|
|
52
|
-
headers
|
|
53
|
-
});
|
|
54
|
-
if (challengeRes.status !== 402) {
|
|
55
|
-
return challengeRes;
|
|
56
|
-
}
|
|
57
|
-
let body;
|
|
58
|
-
try {
|
|
59
|
-
body = await challengeRes.json();
|
|
60
|
-
} catch {
|
|
61
|
-
throw new Error("Expected JSON body in 402 response");
|
|
62
|
-
}
|
|
63
|
-
const siwxExtension = body.extensions?.["sign-in-with-x"];
|
|
64
|
-
if (!siwxExtension) {
|
|
65
|
-
throw new Error(
|
|
66
|
-
"Expected SIWX challenge in 402 response. This endpoint may require payment instead of SIWX auth."
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
const { createSIWxPayload, encodeSIWxHeader } = await import("@x402/extensions/sign-in-with-x");
|
|
70
|
-
const chainInfo = siwxExtension.supportedChains?.find((c) => c.type === "eip191") ?? {
|
|
71
|
-
chainId: siwxExtension.info.chainId,
|
|
72
|
-
type: siwxExtension.info.type
|
|
73
|
-
};
|
|
74
|
-
const completeInfo = {
|
|
75
|
-
...siwxExtension.info,
|
|
76
|
-
chainId: chainInfo.chainId,
|
|
77
|
-
type: chainInfo.type,
|
|
78
|
-
...chainInfo.signatureScheme ? { signatureScheme: chainInfo.signatureScheme } : {}
|
|
79
|
-
};
|
|
80
|
-
const payload = await createSIWxPayload(completeInfo, signer);
|
|
81
|
-
const header = encodeSIWxHeader(payload);
|
|
82
|
-
return fetch(url, {
|
|
83
|
-
...init,
|
|
84
|
-
headers: {
|
|
85
|
-
...headers instanceof Headers ? Object.fromEntries(headers.entries()) : headers,
|
|
86
|
-
"SIGN-IN-WITH-X": header
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
91
|
-
0 && (module.exports = {
|
|
92
|
-
SIWX_ERROR_MESSAGES,
|
|
93
|
-
fetchWithSiwx
|
|
94
|
-
});
|
package/dist/client/index.d.cts
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
export { S as SIWX_ERROR_MESSAGES, a as SiwxErrorCode } from '../siwx-BMlja_nt.cjs';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @agentcash/router/client
|
|
5
|
-
*
|
|
6
|
-
* Client-side utilities for SIWX (Sign-In With X) authentication.
|
|
7
|
-
* Use these to authenticate with SIWX-protected endpoints.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* import { fetchWithSiwx } from '@agentcash/router/client';
|
|
12
|
-
*
|
|
13
|
-
* const response = await fetchWithSiwx('https://api.example.com/protected', {
|
|
14
|
-
* method: 'GET',
|
|
15
|
-
* signer: walletClient, // viem WalletClient or PrivateKeyAccount
|
|
16
|
-
* });
|
|
17
|
-
* ```
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* SIWX challenge structure from 402 response.
|
|
22
|
-
* This is what the server returns in `extensions['sign-in-with-x'].info`.
|
|
23
|
-
*/
|
|
24
|
-
interface SiwxChallenge {
|
|
25
|
-
domain: string;
|
|
26
|
-
uri: string;
|
|
27
|
-
version: string;
|
|
28
|
-
chainId: string;
|
|
29
|
-
type: 'eip191' | 'ed25519';
|
|
30
|
-
nonce: string;
|
|
31
|
-
issuedAt: string;
|
|
32
|
-
expirationTime?: string;
|
|
33
|
-
statement?: string;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Fetch options with SIWX signer.
|
|
37
|
-
* The signer must be compatible with @x402/extensions EVMSigner interface.
|
|
38
|
-
*/
|
|
39
|
-
interface FetchWithSiwxOptions extends Omit<RequestInit, 'headers'> {
|
|
40
|
-
/**
|
|
41
|
-
* Wallet signer compatible with viem's WalletClient or PrivateKeyAccount.
|
|
42
|
-
* Must have a `signMessage` method that accepts `{ message: string }`.
|
|
43
|
-
*/
|
|
44
|
-
signer: {
|
|
45
|
-
signMessage: (args: {
|
|
46
|
-
message: string;
|
|
47
|
-
account?: unknown;
|
|
48
|
-
}) => Promise<string>;
|
|
49
|
-
account?: {
|
|
50
|
-
address: string;
|
|
51
|
-
};
|
|
52
|
-
address?: string;
|
|
53
|
-
};
|
|
54
|
-
/**
|
|
55
|
-
* Additional headers to include in the request.
|
|
56
|
-
*/
|
|
57
|
-
headers?: HeadersInit;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Fetch a SIWX-protected endpoint with automatic challenge-response handling.
|
|
61
|
-
*
|
|
62
|
-
* 1. Makes initial request
|
|
63
|
-
* 2. If 402 with SIWX challenge, extracts challenge from response
|
|
64
|
-
* 3. Signs the challenge with the provided signer
|
|
65
|
-
* 4. Retries request with SIGN-IN-WITH-X header
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```ts
|
|
69
|
-
* import { fetchWithSiwx } from '@agentcash/router/client';
|
|
70
|
-
* import { createWalletClient, custom } from 'viem';
|
|
71
|
-
*
|
|
72
|
-
* const walletClient = createWalletClient({
|
|
73
|
-
* transport: custom(window.ethereum),
|
|
74
|
-
* });
|
|
75
|
-
*
|
|
76
|
-
* const response = await fetchWithSiwx('https://api.example.com/jobs', {
|
|
77
|
-
* method: 'GET',
|
|
78
|
-
* signer: walletClient,
|
|
79
|
-
* });
|
|
80
|
-
*
|
|
81
|
-
* const jobs = await response.json();
|
|
82
|
-
* ```
|
|
83
|
-
*/
|
|
84
|
-
declare function fetchWithSiwx(url: string, options: FetchWithSiwxOptions): Promise<Response>;
|
|
85
|
-
|
|
86
|
-
export { type FetchWithSiwxOptions, type SiwxChallenge, fetchWithSiwx };
|
package/dist/client/index.d.ts
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
export { S as SIWX_ERROR_MESSAGES, a as SiwxErrorCode } from '../siwx-BMlja_nt.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @agentcash/router/client
|
|
5
|
-
*
|
|
6
|
-
* Client-side utilities for SIWX (Sign-In With X) authentication.
|
|
7
|
-
* Use these to authenticate with SIWX-protected endpoints.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* import { fetchWithSiwx } from '@agentcash/router/client';
|
|
12
|
-
*
|
|
13
|
-
* const response = await fetchWithSiwx('https://api.example.com/protected', {
|
|
14
|
-
* method: 'GET',
|
|
15
|
-
* signer: walletClient, // viem WalletClient or PrivateKeyAccount
|
|
16
|
-
* });
|
|
17
|
-
* ```
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* SIWX challenge structure from 402 response.
|
|
22
|
-
* This is what the server returns in `extensions['sign-in-with-x'].info`.
|
|
23
|
-
*/
|
|
24
|
-
interface SiwxChallenge {
|
|
25
|
-
domain: string;
|
|
26
|
-
uri: string;
|
|
27
|
-
version: string;
|
|
28
|
-
chainId: string;
|
|
29
|
-
type: 'eip191' | 'ed25519';
|
|
30
|
-
nonce: string;
|
|
31
|
-
issuedAt: string;
|
|
32
|
-
expirationTime?: string;
|
|
33
|
-
statement?: string;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Fetch options with SIWX signer.
|
|
37
|
-
* The signer must be compatible with @x402/extensions EVMSigner interface.
|
|
38
|
-
*/
|
|
39
|
-
interface FetchWithSiwxOptions extends Omit<RequestInit, 'headers'> {
|
|
40
|
-
/**
|
|
41
|
-
* Wallet signer compatible with viem's WalletClient or PrivateKeyAccount.
|
|
42
|
-
* Must have a `signMessage` method that accepts `{ message: string }`.
|
|
43
|
-
*/
|
|
44
|
-
signer: {
|
|
45
|
-
signMessage: (args: {
|
|
46
|
-
message: string;
|
|
47
|
-
account?: unknown;
|
|
48
|
-
}) => Promise<string>;
|
|
49
|
-
account?: {
|
|
50
|
-
address: string;
|
|
51
|
-
};
|
|
52
|
-
address?: string;
|
|
53
|
-
};
|
|
54
|
-
/**
|
|
55
|
-
* Additional headers to include in the request.
|
|
56
|
-
*/
|
|
57
|
-
headers?: HeadersInit;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Fetch a SIWX-protected endpoint with automatic challenge-response handling.
|
|
61
|
-
*
|
|
62
|
-
* 1. Makes initial request
|
|
63
|
-
* 2. If 402 with SIWX challenge, extracts challenge from response
|
|
64
|
-
* 3. Signs the challenge with the provided signer
|
|
65
|
-
* 4. Retries request with SIGN-IN-WITH-X header
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```ts
|
|
69
|
-
* import { fetchWithSiwx } from '@agentcash/router/client';
|
|
70
|
-
* import { createWalletClient, custom } from 'viem';
|
|
71
|
-
*
|
|
72
|
-
* const walletClient = createWalletClient({
|
|
73
|
-
* transport: custom(window.ethereum),
|
|
74
|
-
* });
|
|
75
|
-
*
|
|
76
|
-
* const response = await fetchWithSiwx('https://api.example.com/jobs', {
|
|
77
|
-
* method: 'GET',
|
|
78
|
-
* signer: walletClient,
|
|
79
|
-
* });
|
|
80
|
-
*
|
|
81
|
-
* const jobs = await response.json();
|
|
82
|
-
* ```
|
|
83
|
-
*/
|
|
84
|
-
declare function fetchWithSiwx(url: string, options: FetchWithSiwxOptions): Promise<Response>;
|
|
85
|
-
|
|
86
|
-
export { type FetchWithSiwxOptions, type SiwxChallenge, fetchWithSiwx };
|
package/dist/client/index.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
// src/auth/siwx.ts
|
|
2
|
-
var SIWX_ERROR_MESSAGES = {
|
|
3
|
-
siwx_missing_header: "Missing SIGN-IN-WITH-X header",
|
|
4
|
-
siwx_malformed: "Malformed SIWX payload",
|
|
5
|
-
siwx_expired: "SIWX message expired \u2014 request a new challenge",
|
|
6
|
-
siwx_nonce_used: "Nonce already used \u2014 request a new challenge",
|
|
7
|
-
siwx_invalid_signature: "Invalid signature \u2014 wallet mismatch or corrupted proof"
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
// src/client/index.ts
|
|
11
|
-
async function fetchWithSiwx(url, options) {
|
|
12
|
-
const { signer, headers, ...init } = options;
|
|
13
|
-
const challengeRes = await fetch(url, {
|
|
14
|
-
...init,
|
|
15
|
-
headers
|
|
16
|
-
});
|
|
17
|
-
if (challengeRes.status !== 402) {
|
|
18
|
-
return challengeRes;
|
|
19
|
-
}
|
|
20
|
-
let body;
|
|
21
|
-
try {
|
|
22
|
-
body = await challengeRes.json();
|
|
23
|
-
} catch {
|
|
24
|
-
throw new Error("Expected JSON body in 402 response");
|
|
25
|
-
}
|
|
26
|
-
const siwxExtension = body.extensions?.["sign-in-with-x"];
|
|
27
|
-
if (!siwxExtension) {
|
|
28
|
-
throw new Error(
|
|
29
|
-
"Expected SIWX challenge in 402 response. This endpoint may require payment instead of SIWX auth."
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
const { createSIWxPayload, encodeSIWxHeader } = await import("@x402/extensions/sign-in-with-x");
|
|
33
|
-
const chainInfo = siwxExtension.supportedChains?.find((c) => c.type === "eip191") ?? {
|
|
34
|
-
chainId: siwxExtension.info.chainId,
|
|
35
|
-
type: siwxExtension.info.type
|
|
36
|
-
};
|
|
37
|
-
const completeInfo = {
|
|
38
|
-
...siwxExtension.info,
|
|
39
|
-
chainId: chainInfo.chainId,
|
|
40
|
-
type: chainInfo.type,
|
|
41
|
-
...chainInfo.signatureScheme ? { signatureScheme: chainInfo.signatureScheme } : {}
|
|
42
|
-
};
|
|
43
|
-
const payload = await createSIWxPayload(completeInfo, signer);
|
|
44
|
-
const header = encodeSIWxHeader(payload);
|
|
45
|
-
return fetch(url, {
|
|
46
|
-
...init,
|
|
47
|
-
headers: {
|
|
48
|
-
...headers instanceof Headers ? Object.fromEntries(headers.entries()) : headers,
|
|
49
|
-
"SIGN-IN-WITH-X": header
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
export {
|
|
54
|
-
SIWX_ERROR_MESSAGES,
|
|
55
|
-
fetchWithSiwx
|
|
56
|
-
};
|
package/dist/siwx-BMlja_nt.d.cts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SIWX verification error codes.
|
|
3
|
-
* Enables clients to auto-retry transient failures (e.g., expired challenge).
|
|
4
|
-
*/
|
|
5
|
-
type SiwxErrorCode = 'siwx_missing_header' | 'siwx_malformed' | 'siwx_expired' | 'siwx_nonce_used' | 'siwx_invalid_signature';
|
|
6
|
-
/** Human-readable error messages for each SIWX error code. */
|
|
7
|
-
declare const SIWX_ERROR_MESSAGES: Record<SiwxErrorCode, string>;
|
|
8
|
-
|
|
9
|
-
export { SIWX_ERROR_MESSAGES as S, type SiwxErrorCode as a };
|
package/dist/siwx-BMlja_nt.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SIWX verification error codes.
|
|
3
|
-
* Enables clients to auto-retry transient failures (e.g., expired challenge).
|
|
4
|
-
*/
|
|
5
|
-
type SiwxErrorCode = 'siwx_missing_header' | 'siwx_malformed' | 'siwx_expired' | 'siwx_nonce_used' | 'siwx_invalid_signature';
|
|
6
|
-
/** Human-readable error messages for each SIWX error code. */
|
|
7
|
-
declare const SIWX_ERROR_MESSAGES: Record<SiwxErrorCode, string>;
|
|
8
|
-
|
|
9
|
-
export { SIWX_ERROR_MESSAGES as S, type SiwxErrorCode as a };
|