@elizaos/plugin-x402 2.0.0-alpha.6 → 2.0.3-beta.5
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/LICENSE +21 -0
- package/README.md +151 -0
- package/dist/index.d.ts +57 -2
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2542 -1915
- package/dist/index.js.map +14 -21
- package/dist/payment-config.d.ts +256 -0
- package/dist/payment-config.d.ts.map +1 -0
- package/dist/payment-wrapper.d.ts +42 -0
- package/dist/payment-wrapper.d.ts.map +1 -0
- package/dist/startup-validator.d.ts +28 -0
- package/dist/startup-validator.d.ts.map +1 -0
- package/dist/types.d.ts +158 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/x402-facilitator-binding.d.ts +9 -0
- package/dist/x402-facilitator-binding.d.ts.map +1 -0
- package/dist/x402-replay-durable.d.ts +30 -0
- package/dist/x402-replay-durable.d.ts.map +1 -0
- package/dist/x402-replay-guard.d.ts +28 -0
- package/dist/x402-replay-guard.d.ts.map +1 -0
- package/dist/x402-replay-keys.d.ts +21 -0
- package/dist/x402-replay-keys.d.ts.map +1 -0
- package/dist/x402-resolve.d.ts +6 -0
- package/dist/x402-resolve.d.ts.map +1 -0
- package/dist/x402-standard-payment.d.ts +130 -0
- package/dist/x402-standard-payment.d.ts.map +1 -0
- package/dist/x402-types.d.ts +130 -0
- package/dist/x402-types.d.ts.map +1 -0
- package/package.json +63 -94
- package/src/__tests__/core-test-mock.ts +10 -0
- package/src/index.ts +115 -0
- package/src/payment-config.ts +737 -0
- package/src/payment-wrapper.test.ts +234 -0
- package/src/payment-wrapper.ts +1997 -0
- package/src/startup-validator.test.ts +86 -0
- package/src/startup-validator.ts +351 -0
- package/src/types.ts +177 -0
- package/src/x402-facilitator-binding.ts +104 -0
- package/src/x402-replay-durable.ts +320 -0
- package/src/x402-replay-guard.ts +165 -0
- package/src/x402-replay-keys.ts +151 -0
- package/src/x402-resolve.ts +43 -0
- package/src/x402-standard-payment.ts +519 -0
- package/src/x402-types.ts +376 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Standard x402 **buyer → seller** payloads carried in `PAYMENT-SIGNATURE`, `X-Payment`,
|
|
3
|
+
* or legacy `X-Payment` headers (often base64-wrapped JSON).
|
|
4
|
+
*
|
|
5
|
+
* **Why this file exists:** the agent historically verified “proof strings” (tx
|
|
6
|
+
* hashes, legacy formats, facilitator payment IDs). Modern clients instead send
|
|
7
|
+
* a structured **payment payload** plus expect the seller to validate it against
|
|
8
|
+
* **payment requirements** through a facilitator (`POST /verify`, `POST /settle`).
|
|
9
|
+
* Centralizing decode, requirement construction, and HTTP calls here keeps
|
|
10
|
+
* `payment-wrapper.ts` readable and avoids duplicating facilitator contracts.
|
|
11
|
+
*
|
|
12
|
+
* **Why verify *and* settle:** authorization-like payloads are not settlement.
|
|
13
|
+
* Unlocking paid HTTP work only after settle succeeds matches facilitator-centric
|
|
14
|
+
* flows and closes the “valid signature, no transfer” gap.
|
|
15
|
+
*
|
|
16
|
+
* **Why URL helpers are flexible:** facilitator vendors mount `/verify` and
|
|
17
|
+
* `/settle` under different prefixes; Eliza Cloud uses `/api/v1/x402/*` while
|
|
18
|
+
* other stacks use a single base URL with trailing paths. Explicit override envs
|
|
19
|
+
* exist so production does not depend on one hardcoded layout.
|
|
20
|
+
*/
|
|
21
|
+
import { type PaymentConfigDefinition } from "./payment-config.js";
|
|
22
|
+
import type { X402Runtime } from "./types.js";
|
|
23
|
+
/** Decoded X-Payment body (x402-fetch / CDP-style clients). */
|
|
24
|
+
export type X402StandardPaymentPayload = {
|
|
25
|
+
x402Version: number;
|
|
26
|
+
accepted: {
|
|
27
|
+
scheme: string;
|
|
28
|
+
network: string;
|
|
29
|
+
asset: string;
|
|
30
|
+
amount?: string;
|
|
31
|
+
maxAmountRequired?: string;
|
|
32
|
+
payTo: string;
|
|
33
|
+
};
|
|
34
|
+
payload: {
|
|
35
|
+
signature: string;
|
|
36
|
+
authorization: {
|
|
37
|
+
from: string;
|
|
38
|
+
to: string;
|
|
39
|
+
value: string;
|
|
40
|
+
validAfter?: string;
|
|
41
|
+
validBefore: string;
|
|
42
|
+
nonce: string;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
export type FacilitatorPaymentRequirements = {
|
|
47
|
+
scheme: string;
|
|
48
|
+
network: string;
|
|
49
|
+
asset: string;
|
|
50
|
+
amount: string;
|
|
51
|
+
payTo: string;
|
|
52
|
+
maxTimeoutSeconds?: number;
|
|
53
|
+
extra?: Record<string, unknown>;
|
|
54
|
+
};
|
|
55
|
+
export type StandardPaymentRequiredAccept = {
|
|
56
|
+
scheme: "exact";
|
|
57
|
+
network: string;
|
|
58
|
+
maxAmountRequired: string;
|
|
59
|
+
resource: string;
|
|
60
|
+
description: string;
|
|
61
|
+
mimeType: string;
|
|
62
|
+
payTo: string;
|
|
63
|
+
maxTimeoutSeconds: number;
|
|
64
|
+
asset: string;
|
|
65
|
+
extra?: Record<string, unknown>;
|
|
66
|
+
};
|
|
67
|
+
export type StandardPaymentRequired = {
|
|
68
|
+
x402Version: 2;
|
|
69
|
+
accepts: StandardPaymentRequiredAccept[];
|
|
70
|
+
error?: string;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Decode `X-Payment` / `X-PAYMENT` value: base64(JSON) first, then raw JSON.
|
|
74
|
+
*/
|
|
75
|
+
export declare function decodeXPaymentHeader(raw: string): unknown | null;
|
|
76
|
+
export declare function isX402StandardPaymentPayload(v: unknown): v is X402StandardPaymentPayload;
|
|
77
|
+
export declare function toStandardNetwork(network: PaymentConfigDefinition["network"]): string;
|
|
78
|
+
export declare function standardAssetForConfig(cfg: PaymentConfigDefinition): string;
|
|
79
|
+
export declare function buildStandardPaymentRequiredAccept(params: {
|
|
80
|
+
routePath: string;
|
|
81
|
+
description: string;
|
|
82
|
+
priceInCents: number;
|
|
83
|
+
configName: string;
|
|
84
|
+
agentId?: string;
|
|
85
|
+
}): StandardPaymentRequiredAccept;
|
|
86
|
+
export declare function buildStandardPaymentRequired(params: {
|
|
87
|
+
routePath: string;
|
|
88
|
+
description: string;
|
|
89
|
+
priceInCents: number;
|
|
90
|
+
paymentConfigNames: string[];
|
|
91
|
+
agentId?: string;
|
|
92
|
+
error?: string;
|
|
93
|
+
}): StandardPaymentRequired;
|
|
94
|
+
/**
|
|
95
|
+
* Build facilitator `paymentRequirements` for this route/config (must match what we advertise in `accepts`).
|
|
96
|
+
*/
|
|
97
|
+
export declare function buildFacilitatorPaymentRequirements(params: {
|
|
98
|
+
routePath: string;
|
|
99
|
+
priceInCents: number;
|
|
100
|
+
configName: string;
|
|
101
|
+
agentId?: string;
|
|
102
|
+
}): FacilitatorPaymentRequirements;
|
|
103
|
+
export declare function findMatchingPaymentConfigForStandardPayload(payload: X402StandardPaymentPayload, paymentConfigNames: string[], priceInCents: number, agentId?: string): {
|
|
104
|
+
name: string;
|
|
105
|
+
cfg: PaymentConfigDefinition;
|
|
106
|
+
} | null;
|
|
107
|
+
export declare function getFacilitatorVerifyPostUrl(runtime: X402Runtime): string | null;
|
|
108
|
+
export declare function getFacilitatorSettlePostUrl(runtime: X402Runtime): string | null;
|
|
109
|
+
export type FacilitatorVerifyPostResult = {
|
|
110
|
+
ok: true;
|
|
111
|
+
payer?: string;
|
|
112
|
+
} | {
|
|
113
|
+
ok: false;
|
|
114
|
+
invalidReason?: string;
|
|
115
|
+
};
|
|
116
|
+
export type FacilitatorSettlePostResult = {
|
|
117
|
+
ok: true;
|
|
118
|
+
paymentResponse: string;
|
|
119
|
+
transaction?: string;
|
|
120
|
+
payer?: string;
|
|
121
|
+
} | {
|
|
122
|
+
ok: false;
|
|
123
|
+
invalidReason?: string;
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* POST `{ paymentPayload, paymentRequirements }` to facilitator verify (Eliza Cloud–compatible).
|
|
127
|
+
*/
|
|
128
|
+
export declare function verifyPaymentPayloadViaFacilitatorPost(runtime: X402Runtime, paymentPayload: X402StandardPaymentPayload, paymentRequirements: FacilitatorPaymentRequirements): Promise<FacilitatorVerifyPostResult>;
|
|
129
|
+
export declare function settlePaymentPayloadViaFacilitatorPost(runtime: X402Runtime, paymentPayload: X402StandardPaymentPayload, paymentRequirements: FacilitatorPaymentRequirements): Promise<FacilitatorSettlePostResult>;
|
|
130
|
+
//# sourceMappingURL=x402-standard-payment.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"x402-standard-payment.d.ts","sourceRoot":"","sources":["../src/x402-standard-payment.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAGL,KAAK,uBAAuB,EAG7B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,+DAA+D;AAC/D,MAAM,MAAM,0BAA0B,GAAG;IACvC,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE;QACR,MAAM,EAAE,MAAM,CAAC;QACf,OAAO,EAAE,MAAM,CAAC;QAChB,KAAK,EAAE,MAAM,CAAC;QACd,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,iBAAiB,CAAC,EAAE,MAAM,CAAC;QAC3B,KAAK,EAAE,MAAM,CAAC;KACf,CAAC;IACF,OAAO,EAAE;QACP,SAAS,EAAE,MAAM,CAAC;QAClB,aAAa,EAAE;YACb,IAAI,EAAE,MAAM,CAAC;YACb,EAAE,EAAE,MAAM,CAAC;YACX,KAAK,EAAE,MAAM,CAAC;YACd,UAAU,CAAC,EAAE,MAAM,CAAC;YACpB,WAAW,EAAE,MAAM,CAAC;YACpB,KAAK,EAAE,MAAM,CAAC;SACf,CAAC;KACH,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,8BAA8B,GAAG;IAC3C,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,C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AAC,EAAE,MAAM,CAAA;CAAE,GAC3E;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,aAAa,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAE1C;;GAEG;AACH,wBAAsB,sCAAsC,CAC1D,OAAO,EAAE,WAAW,EACpB,cAAc,EAAE,0BAA0B,EAC1C,mBAAmB,EAAE,8BAA8B,GAClD,OAAO,CAAC,2BAA2B,CAAC,CA4CtC;AAED,wBAAsB,sCAAsC,CAC1D,OAAO,EAAE,WAAW,EACpB,cAAc,EAAE,0BAA0B,EAC1C,mBAAmB,EAAE,8BAA8B,GAClD,OAAO,CAAC,2BAA2B,CAAC,CAuEtC"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* x402scan Validation Schema Types
|
|
3
|
+
* Stricter schema required for listing on x402scan
|
|
4
|
+
* Allows UI-based resource invocation
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Field definition for input/output schema
|
|
8
|
+
*/
|
|
9
|
+
export type FieldDef = {
|
|
10
|
+
type?: string;
|
|
11
|
+
required?: boolean | string[];
|
|
12
|
+
description?: string;
|
|
13
|
+
enum?: string[];
|
|
14
|
+
properties?: Record<string, FieldDef>;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* JSON Schema type for API output
|
|
18
|
+
*/
|
|
19
|
+
export type OutputSchemaType = {
|
|
20
|
+
type?: "object" | "array" | "string" | "number" | "boolean" | "null";
|
|
21
|
+
description?: string;
|
|
22
|
+
properties?: Record<string, FieldDef>;
|
|
23
|
+
items?: FieldDef;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Output schema describing input and output expectations for the paid endpoint
|
|
27
|
+
*/
|
|
28
|
+
export type OutputSchema = {
|
|
29
|
+
input: {
|
|
30
|
+
type: "http";
|
|
31
|
+
method: "GET" | "POST";
|
|
32
|
+
bodyType?: "json" | "form-data" | "multipart-form-data" | "text" | "binary";
|
|
33
|
+
pathParams?: Record<string, FieldDef>;
|
|
34
|
+
queryParams?: Record<string, FieldDef>;
|
|
35
|
+
bodyFields?: Record<string, FieldDef>;
|
|
36
|
+
headerFields?: Record<string, FieldDef>;
|
|
37
|
+
};
|
|
38
|
+
output?: OutputSchemaType;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Valid x402scan network types (as per their API specification)
|
|
42
|
+
*/
|
|
43
|
+
export type X402ScanNetwork = "base-sepolia" | "base" | "avalanche-fuji" | "avalanche" | "iotex" | "solana-devnet" | "solana" | "sei" | "sei-testnet" | "polygon" | "polygon-amoy" | "bsc" | "bsc-testnet" | "peaq";
|
|
44
|
+
/**
|
|
45
|
+
* EIP-712 domain information for EVM chains
|
|
46
|
+
*/
|
|
47
|
+
export type EIP712DomainInfo = {
|
|
48
|
+
name: string;
|
|
49
|
+
version: string;
|
|
50
|
+
chainId: number;
|
|
51
|
+
verifyingContract: string;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Extra metadata for payment configuration
|
|
55
|
+
*/
|
|
56
|
+
export type PaymentExtraMetadata = {
|
|
57
|
+
priceInCents: number;
|
|
58
|
+
priceUSD: string;
|
|
59
|
+
symbol: string;
|
|
60
|
+
paymentConfig: string;
|
|
61
|
+
expiresIn: number;
|
|
62
|
+
name?: string;
|
|
63
|
+
version?: string;
|
|
64
|
+
eip712Domain?: EIP712DomainInfo;
|
|
65
|
+
[key: string]: string | number | EIP712DomainInfo | undefined;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Accepts object defining payment terms for a resource
|
|
69
|
+
*/
|
|
70
|
+
export type Accepts = {
|
|
71
|
+
scheme: "exact";
|
|
72
|
+
network: X402ScanNetwork;
|
|
73
|
+
maxAmountRequired: string;
|
|
74
|
+
resource: string;
|
|
75
|
+
description: string;
|
|
76
|
+
mimeType: string;
|
|
77
|
+
payTo: string;
|
|
78
|
+
maxTimeoutSeconds: number;
|
|
79
|
+
asset: string;
|
|
80
|
+
outputSchema?: OutputSchema;
|
|
81
|
+
extra?: PaymentExtraMetadata;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* X402 Response structure
|
|
85
|
+
*/
|
|
86
|
+
export type X402Response = {
|
|
87
|
+
x402Version: number;
|
|
88
|
+
error?: string;
|
|
89
|
+
accepts?: Array<Accepts>;
|
|
90
|
+
payer?: string;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Validation result type
|
|
94
|
+
*/
|
|
95
|
+
export type ValidationResult = {
|
|
96
|
+
valid: boolean;
|
|
97
|
+
errors: string[];
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Validate that an Accepts object conforms to the x402scan schema
|
|
101
|
+
*/
|
|
102
|
+
export declare function validateAccepts(accepts: Partial<Accepts>): ValidationResult;
|
|
103
|
+
/**
|
|
104
|
+
* Validate that an X402Response conforms to the x402scan schema
|
|
105
|
+
*/
|
|
106
|
+
export declare function validateX402Response(response: Partial<X402Response>): ValidationResult;
|
|
107
|
+
/**
|
|
108
|
+
* Create a validated Accepts object with sensible defaults
|
|
109
|
+
*/
|
|
110
|
+
export declare function createAccepts(params: {
|
|
111
|
+
network: X402ScanNetwork;
|
|
112
|
+
maxAmountRequired: string;
|
|
113
|
+
resource: string;
|
|
114
|
+
description: string;
|
|
115
|
+
payTo: string;
|
|
116
|
+
asset: string;
|
|
117
|
+
mimeType?: string;
|
|
118
|
+
maxTimeoutSeconds?: number;
|
|
119
|
+
outputSchema?: OutputSchema;
|
|
120
|
+
extra?: PaymentExtraMetadata;
|
|
121
|
+
}): Accepts;
|
|
122
|
+
/**
|
|
123
|
+
* Create a validated X402Response
|
|
124
|
+
*/
|
|
125
|
+
export declare function createX402Response(params: {
|
|
126
|
+
accepts?: Accepts[];
|
|
127
|
+
error?: string;
|
|
128
|
+
payer?: string;
|
|
129
|
+
}): X402Response;
|
|
130
|
+
//# sourceMappingURL=x402-types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+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|
+
"version": "2.0.3-beta.5",
|
|
4
|
+
"description": "x402 micropayment middleware for elizaOS plugin HTTP routes",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"module": "dist/index.js",
|
|
8
8
|
"types": "dist/index.d.ts",
|
|
9
|
+
"sideEffects": false,
|
|
10
|
+
"license": "MIT",
|
|
9
11
|
"repository": {
|
|
10
12
|
"type": "git",
|
|
11
|
-
"url": "https://github.com/elizaos
|
|
12
|
-
|
|
13
|
-
"publishConfig": {
|
|
14
|
-
"access": "public"
|
|
13
|
+
"url": "git+https://github.com/elizaos/eliza.git",
|
|
14
|
+
"directory": "plugins/plugin-x402"
|
|
15
15
|
},
|
|
16
16
|
"exports": {
|
|
17
17
|
"./package.json": "./package.json",
|
|
18
18
|
".": {
|
|
19
19
|
"types": "./dist/index.d.ts",
|
|
20
|
+
"eliza-source": {
|
|
21
|
+
"types": "./src/index.ts",
|
|
22
|
+
"import": "./src/index.ts",
|
|
23
|
+
"default": "./src/index.ts"
|
|
24
|
+
},
|
|
20
25
|
"import": "./dist/index.js",
|
|
21
26
|
"default": "./dist/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./*.css": "./dist/*.css",
|
|
29
|
+
"./*": {
|
|
30
|
+
"types": "./dist/*.d.ts",
|
|
31
|
+
"eliza-source": {
|
|
32
|
+
"types": "./src/*.ts",
|
|
33
|
+
"import": "./src/*.ts",
|
|
34
|
+
"default": "./src/*.ts"
|
|
35
|
+
},
|
|
36
|
+
"import": "./dist/*.js",
|
|
37
|
+
"default": "./dist/*.js"
|
|
22
38
|
}
|
|
23
39
|
},
|
|
24
|
-
"sideEffects": false,
|
|
25
40
|
"files": [
|
|
26
|
-
"dist"
|
|
41
|
+
"dist",
|
|
42
|
+
"src"
|
|
27
43
|
],
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"@biomejs/biome": "^2.3.11",
|
|
36
|
-
"@types/better-sqlite3": "^7.6.12",
|
|
37
|
-
"@types/node": "^25.0.3",
|
|
38
|
-
"@types/pg": "^8.11.10",
|
|
39
|
-
"typescript": "^5.9.3"
|
|
44
|
+
"elizaos": {
|
|
45
|
+
"plugin": {
|
|
46
|
+
"capabilities": [
|
|
47
|
+
"payments",
|
|
48
|
+
"x402"
|
|
49
|
+
]
|
|
50
|
+
}
|
|
40
51
|
},
|
|
52
|
+
"keywords": [
|
|
53
|
+
"elizaos",
|
|
54
|
+
"plugin",
|
|
55
|
+
"x402",
|
|
56
|
+
"payments",
|
|
57
|
+
"micropayments",
|
|
58
|
+
"http-402"
|
|
59
|
+
],
|
|
41
60
|
"scripts": {
|
|
42
61
|
"build": "bun run build.ts",
|
|
43
62
|
"build:ts": "bun run build.ts",
|
|
44
|
-
"
|
|
45
|
-
"clean": "rm -rf dist .turbo node_modules",
|
|
46
|
-
"format": "bunx @biomejs/biome format --write .",
|
|
47
|
-
"format:check": "bunx @biomejs/biome format .",
|
|
48
|
-
"lint": "bunx @biomejs/biome check --write --unsafe .",
|
|
49
|
-
"lint:check": "bunx @biomejs/biome check .",
|
|
50
|
-
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
63
|
+
"clean": "node ../../packages/scripts/rm-path-recursive.mjs dist",
|
|
51
64
|
"test": "vitest run",
|
|
52
|
-
"
|
|
53
|
-
"
|
|
65
|
+
"typecheck": "tsgo --noEmit",
|
|
66
|
+
"lint": "bun run typecheck",
|
|
67
|
+
"lint:check": "bun run typecheck"
|
|
54
68
|
},
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
"
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
69
|
+
"dependencies": {
|
|
70
|
+
"@elizaos/core": "2.0.3-beta.5",
|
|
71
|
+
"@solana/web3.js": "1.98.4",
|
|
72
|
+
"drizzle-orm": "0.45.2",
|
|
73
|
+
"viem": "^2.48.8"
|
|
74
|
+
},
|
|
75
|
+
"devDependencies": {
|
|
76
|
+
"@types/node": "^24.0.0",
|
|
77
|
+
"typescript": "^6.0.3",
|
|
78
|
+
"vitest": "^4.0.18"
|
|
79
|
+
},
|
|
80
|
+
"peerDependencies": {
|
|
81
|
+
"@elizaos/core": "2.0.3-beta.5"
|
|
82
|
+
},
|
|
83
|
+
"publishConfig": {
|
|
84
|
+
"access": "public"
|
|
63
85
|
},
|
|
64
86
|
"agentConfig": {
|
|
65
|
-
"
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
"required": false,
|
|
70
|
-
"sensitive": false
|
|
71
|
-
},
|
|
72
|
-
"X402_PRIVATE_KEY": {
|
|
73
|
-
"type": "string",
|
|
74
|
-
"description": "Private key for X402",
|
|
75
|
-
"required": true,
|
|
76
|
-
"sensitive": true
|
|
77
|
-
},
|
|
78
|
-
"X402_NETWORK": {
|
|
79
|
-
"type": "string",
|
|
80
|
-
"description": "Network name (e.g. mainnet, testnet)",
|
|
81
|
-
"required": false,
|
|
82
|
-
"sensitive": false
|
|
83
|
-
},
|
|
84
|
-
"X402_PAY_TO": {
|
|
85
|
-
"type": "string",
|
|
86
|
-
"description": "Payment recipient address",
|
|
87
|
-
"required": false,
|
|
88
|
-
"sensitive": false
|
|
89
|
-
},
|
|
90
|
-
"X402_FACILITATOR_URL": {
|
|
91
|
-
"type": "string",
|
|
92
|
-
"description": "Facilitator service URL",
|
|
93
|
-
"required": false,
|
|
94
|
-
"sensitive": false
|
|
95
|
-
},
|
|
96
|
-
"X402_MAX_PAYMENT_USD": {
|
|
97
|
-
"type": "number",
|
|
98
|
-
"description": "Maximum single payment in USD",
|
|
99
|
-
"required": false,
|
|
100
|
-
"sensitive": false
|
|
101
|
-
},
|
|
102
|
-
"X402_MAX_TOTAL_USD": {
|
|
103
|
-
"type": "number",
|
|
104
|
-
"description": "Maximum total spend in USD",
|
|
105
|
-
"required": false,
|
|
106
|
-
"sensitive": false
|
|
107
|
-
},
|
|
108
|
-
"X402_ENABLED": {
|
|
109
|
-
"type": "boolean",
|
|
110
|
-
"description": "Enable or disable this feature",
|
|
111
|
-
"required": false,
|
|
112
|
-
"sensitive": false
|
|
113
|
-
},
|
|
114
|
-
"X402_DB_PATH": {
|
|
115
|
-
"type": "string",
|
|
116
|
-
"description": "Database file path",
|
|
117
|
-
"required": false,
|
|
118
|
-
"sensitive": false
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
87
|
+
"pluginType": "elizaos:plugin:1.0.0",
|
|
88
|
+
"pluginParameters": {}
|
|
89
|
+
},
|
|
90
|
+
"gitHead": "ff6157011c9459670021cc28a6797592a78b8817"
|
|
122
91
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* x402 Payment Middleware for ElizaOS
|
|
3
|
+
*
|
|
4
|
+
* Provides micropayment protection for plugin routes using the x402 protocol.
|
|
5
|
+
*
|
|
6
|
+
* **Why this module exists (product):** plugin authors should declare `x402` on
|
|
7
|
+
* routes and get a consistent gate—402 with payment options, verification, and
|
|
8
|
+
* optional facilitator settlement—without reimplementing payment math, replay
|
|
9
|
+
* safety, or HTTP header quirks in every plugin.
|
|
10
|
+
*
|
|
11
|
+
* **Why both “legacy JSON 402” and V2 headers:** older clients and scanners read
|
|
12
|
+
* the JSON body; protocol V2 buyers read `PAYMENT-REQUIRED` / `PAYMENT-RESPONSE`.
|
|
13
|
+
* Serving both avoids breaking existing integrations while still interoperating
|
|
14
|
+
* with modern wallets.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { applyPaymentProtection } from '@elizaos/plugin-x402';
|
|
19
|
+
*
|
|
20
|
+
* // In your plugin:
|
|
21
|
+
* export const routes: Route[] = [
|
|
22
|
+
* {
|
|
23
|
+
* type: 'GET',
|
|
24
|
+
* path: '/api/analytics/trending',
|
|
25
|
+
* public: true,
|
|
26
|
+
* x402: {
|
|
27
|
+
* priceInCents: 10,
|
|
28
|
+
* paymentConfigs: ['base_usdc', 'solana_usdc']
|
|
29
|
+
* },
|
|
30
|
+
* handler: async (req, res, runtime) => {
|
|
31
|
+
* // Your handler logic
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
* ];
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
export type {
|
|
39
|
+
BuiltInPaymentConfig,
|
|
40
|
+
CharacterX402Settings,
|
|
41
|
+
PaymentEnabledRoute,
|
|
42
|
+
X402Config,
|
|
43
|
+
X402RequestValidator,
|
|
44
|
+
X402ValidationResult,
|
|
45
|
+
} from "@elizaos/core";
|
|
46
|
+
|
|
47
|
+
export type { Network } from "./payment-config.js";
|
|
48
|
+
export {
|
|
49
|
+
atomicAmountForPriceInCents,
|
|
50
|
+
BUILT_IN_NETWORKS,
|
|
51
|
+
getBaseUrl,
|
|
52
|
+
getPaymentAddress,
|
|
53
|
+
getPaymentConfig,
|
|
54
|
+
getX402Health,
|
|
55
|
+
listX402Configs,
|
|
56
|
+
PAYMENT_ADDRESSES,
|
|
57
|
+
PAYMENT_CONFIGS,
|
|
58
|
+
type PaymentConfigDefinition,
|
|
59
|
+
registerX402Config,
|
|
60
|
+
toResourceUrl,
|
|
61
|
+
toX402Network,
|
|
62
|
+
} from "./payment-config.js";
|
|
63
|
+
export {
|
|
64
|
+
applyPaymentProtection,
|
|
65
|
+
createPaymentAwareHandler,
|
|
66
|
+
isRoutePaymentWrapped,
|
|
67
|
+
X402_ROUTE_PAYMENT_WRAPPED,
|
|
68
|
+
} from "./payment-wrapper.js";
|
|
69
|
+
export {
|
|
70
|
+
type StartupValidationResult,
|
|
71
|
+
validateAndThrowIfInvalid,
|
|
72
|
+
validateX402Startup,
|
|
73
|
+
} from "./startup-validator.js";
|
|
74
|
+
export {
|
|
75
|
+
resolveEffectiveX402,
|
|
76
|
+
X402_EVENT_PAYMENT_REQUIRED,
|
|
77
|
+
X402_EVENT_PAYMENT_VERIFIED,
|
|
78
|
+
} from "./x402-resolve.js";
|
|
79
|
+
|
|
80
|
+
export {
|
|
81
|
+
type Accepts,
|
|
82
|
+
createAccepts,
|
|
83
|
+
createX402Response,
|
|
84
|
+
type OutputSchema,
|
|
85
|
+
validateAccepts,
|
|
86
|
+
validateX402Response,
|
|
87
|
+
type X402Response,
|
|
88
|
+
type X402ScanNetwork,
|
|
89
|
+
} from "./x402-types.js";
|
|
90
|
+
|
|
91
|
+
import type { Plugin } from "@elizaos/core";
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* elizaOS plugin descriptor for x402.
|
|
95
|
+
*
|
|
96
|
+
* The middleware exported above is the actual integration surface — plugins
|
|
97
|
+
* declare `x402` on their routes and the agent's HTTP dispatch wraps them via
|
|
98
|
+
* `applyPaymentProtection` / `createPaymentAwareHandler`. This Plugin object
|
|
99
|
+
* exists so the runtime's plugin loader can register `@elizaos/plugin-x402` as
|
|
100
|
+
* a first-class auto-loadable plugin (config: `x402.enabled`).
|
|
101
|
+
*/
|
|
102
|
+
const x402Plugin: Plugin = {
|
|
103
|
+
name: "x402",
|
|
104
|
+
description:
|
|
105
|
+
"x402 micropayment middleware for elizaOS plugin HTTP routes (HTTP 402 / payment-required).",
|
|
106
|
+
actions: [],
|
|
107
|
+
providers: [],
|
|
108
|
+
evaluators: [],
|
|
109
|
+
services: [],
|
|
110
|
+
// Middleware-only plugin — no service instances or persistent resources to dispose.
|
|
111
|
+
dispose: async (_runtime) => {},
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export default x402Plugin;
|
|
115
|
+
export { x402Plugin };
|