@nevermined-io/core-kit 0.1.36 → 0.1.41
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.
|
@@ -9,24 +9,48 @@ export type X402Resource = {
|
|
|
9
9
|
mimeType?: string;
|
|
10
10
|
};
|
|
11
11
|
/**
|
|
12
|
-
* x402
|
|
12
|
+
* Supported x402 payment schemes
|
|
13
|
+
*/
|
|
14
|
+
export type X402Scheme = 'nvm:erc4337' | 'nvm:card-delegation';
|
|
15
|
+
/**
|
|
16
|
+
* x402 Accepted scheme fields (common to all schemes)
|
|
13
17
|
*/
|
|
14
18
|
export type X402Accepted = {
|
|
15
|
-
scheme:
|
|
19
|
+
scheme: X402Scheme;
|
|
16
20
|
network: string;
|
|
17
|
-
planId
|
|
21
|
+
planId?: string;
|
|
18
22
|
extra: {
|
|
19
23
|
version: string;
|
|
20
24
|
agentId?: string;
|
|
21
25
|
httpVerb?: string;
|
|
22
26
|
};
|
|
23
27
|
};
|
|
28
|
+
/**
|
|
29
|
+
* Payload specific to the nvm:erc4337 scheme
|
|
30
|
+
*/
|
|
31
|
+
export type X402Erc4337Payload = {
|
|
32
|
+
signature: `0x${string}`;
|
|
33
|
+
authorization: {
|
|
34
|
+
from: `0x${string}`;
|
|
35
|
+
sessionKeysProvider: string;
|
|
36
|
+
sessionKeys: {
|
|
37
|
+
id: string;
|
|
38
|
+
data: string;
|
|
39
|
+
}[];
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Payload specific to the nvm:card-delegation scheme
|
|
44
|
+
*/
|
|
45
|
+
export type X402CardDelegationPayload = {
|
|
46
|
+
token: string;
|
|
47
|
+
};
|
|
24
48
|
/**
|
|
25
49
|
* x402 Access Token aligned with x402 v2 spec
|
|
26
50
|
*
|
|
27
51
|
* Key changes from previous version:
|
|
28
52
|
* - `subscriberAddress` moved to `payload.authorization.from`
|
|
29
|
-
* - `planId` is now in `accepted` (required)
|
|
53
|
+
* - `planId` is now in `accepted` (optional — required for erc4337, optional for card-delegation)
|
|
30
54
|
* - `agentId` moved to `accepted.extra.agentId` (optional)
|
|
31
55
|
* - `scheme` and `network` moved to `accepted`
|
|
32
56
|
* - Added `resource` and `extensions` for x402 v2 alignment
|
|
@@ -35,17 +59,7 @@ export type AgentX402AccessToken = {
|
|
|
35
59
|
x402Version: 2;
|
|
36
60
|
resource?: X402Resource;
|
|
37
61
|
accepted: X402Accepted;
|
|
38
|
-
payload:
|
|
39
|
-
signature: `0x${string}`;
|
|
40
|
-
authorization: {
|
|
41
|
-
from: `0x${string}`;
|
|
42
|
-
sessionKeysProvider: string;
|
|
43
|
-
sessionKeys: {
|
|
44
|
-
id: string;
|
|
45
|
-
data: string;
|
|
46
|
-
}[];
|
|
47
|
-
};
|
|
48
|
-
};
|
|
62
|
+
payload: X402Erc4337Payload | X402CardDelegationPayload;
|
|
49
63
|
validUntil?: number;
|
|
50
64
|
extensions: Record<string, unknown>;
|
|
51
65
|
};
|
|
@@ -130,12 +144,6 @@ export type X402TokenValidationResult = {
|
|
|
130
144
|
valid: boolean;
|
|
131
145
|
errors: string[];
|
|
132
146
|
};
|
|
133
|
-
/**
|
|
134
|
-
* Validate the structure of an x402 access token
|
|
135
|
-
*
|
|
136
|
-
* @param token - The token to validate (unknown type for safety)
|
|
137
|
-
* @returns Validation result with errors if any
|
|
138
|
-
*/
|
|
139
147
|
export declare const validateX402TokenStructure: (token: unknown) => X402TokenValidationResult;
|
|
140
148
|
export declare const b64EncodeX402AccessToken: (accessToken: AgentX402AccessToken) => string;
|
|
141
149
|
export declare const b64DecodeX402AccessToken: (encoded: string) => AgentX402AccessToken;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AgentX402AccessToken.d.ts","sourceRoot":"","sources":["../../src/models/AgentX402AccessToken.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,YAAY,EAAgC,MAAM,MAAM,CAAA;AAC5E,OAAO,EAAE,YAAY,EAAE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IAAI,EAAE,KAAK,MAAM,EAAE,CAAA;IACnB,mBAAmB,EAAE,MAAM,CAAA;IAC3B,WAAW,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,CAAA;CAC5C,CAAA;AAED;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACrC,UAAU,EAAE,YAAY,CAAA;IACxB,eAAe,EAAE,MAAM,CAAA;IACvB,gBAAgB,EAAE,MAAM,CAAA;IACxB,YAAY,EAAE,YAAY,CAAA;IAC1B,MAAM,EAAE,MAAM,CAAA;IACd,oEAAoE;IACpE,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,QAAQ,CAAC,EAAE,YAAY,CAAA;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,yDAAyD;IACzD,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,4BAA4B,GACvC,SAAS,wBAAwB,KAChC,OAAO,CAAC,oBAAoB,CA4E9B,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,0BAA0B,GACrC,aAAa,oBAAoB,EACjC,cAAc,YAAY,KACzB,OAAO,CAAC,OAAO,CAqBjB,CAAA;AAED;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG;IACtC,KAAK,EAAE,OAAO,CAAA;IACd,MAAM,EAAE,MAAM,EAAE,CAAA;CACjB,CAAA;AAUD,eAAO,MAAM,0BAA0B,GAAI,OAAO,OAAO,KAAG,yBA4F3D,CAAA;AAED,eAAO,MAAM,wBAAwB,GAAI,aAAa,oBAAoB,KAAG,MAE5E,CAAA;AAED,eAAO,MAAM,wBAAwB,GAAI,SAAS,MAAM,KAAG,oBAE1D,CAAA"}
|
|
@@ -83,15 +83,16 @@ export const generateAgentX402AccessToken = async (options) => {
|
|
|
83
83
|
* @returns true if signature is valid
|
|
84
84
|
*/
|
|
85
85
|
export const verifyAgentX402AccessToken = async (accessToken, publicClient) => {
|
|
86
|
+
const erc4337Payload = accessToken.payload;
|
|
86
87
|
// Extract subscriber address from payload.authorization.from
|
|
87
|
-
const subscriberAddress =
|
|
88
|
+
const subscriberAddress = erc4337Payload.authorization.from;
|
|
88
89
|
const domain = {
|
|
89
90
|
name: 'Nevermined',
|
|
90
91
|
version: '1',
|
|
91
92
|
chainId: publicClient.chain?.id,
|
|
92
93
|
verifyingContract: subscriberAddress,
|
|
93
94
|
};
|
|
94
|
-
const { signature, authorization } =
|
|
95
|
+
const { signature, authorization } = erc4337Payload;
|
|
95
96
|
const isValid = await publicClient.verifyTypedData({
|
|
96
97
|
address: subscriberAddress,
|
|
97
98
|
signature,
|
|
@@ -108,6 +109,7 @@ export const verifyAgentX402AccessToken = async (accessToken, publicClient) => {
|
|
|
108
109
|
* @param token - The token to validate (unknown type for safety)
|
|
109
110
|
* @returns Validation result with errors if any
|
|
110
111
|
*/
|
|
112
|
+
const SUPPORTED_SCHEMES = ['nvm:erc4337', 'nvm:card-delegation'];
|
|
111
113
|
export const validateX402TokenStructure = (token) => {
|
|
112
114
|
const errors = [];
|
|
113
115
|
if (!token || typeof token !== 'object') {
|
|
@@ -124,15 +126,13 @@ export const validateX402TokenStructure = (token) => {
|
|
|
124
126
|
}
|
|
125
127
|
else {
|
|
126
128
|
const accepted = t.accepted;
|
|
127
|
-
|
|
128
|
-
|
|
129
|
+
const scheme = accepted.scheme;
|
|
130
|
+
if (!SUPPORTED_SCHEMES.includes(scheme)) {
|
|
131
|
+
errors.push(`scheme must be one of: ${SUPPORTED_SCHEMES.join(', ')}`);
|
|
129
132
|
}
|
|
130
133
|
if (!accepted.network || typeof accepted.network !== 'string') {
|
|
131
134
|
errors.push('network is required in accepted');
|
|
132
135
|
}
|
|
133
|
-
if (!accepted.planId || typeof accepted.planId !== 'string') {
|
|
134
|
-
errors.push('planId is required in accepted');
|
|
135
|
-
}
|
|
136
136
|
// Validate extra
|
|
137
137
|
if (!accepted.extra || typeof accepted.extra !== 'object') {
|
|
138
138
|
errors.push('extra is required in accepted');
|
|
@@ -143,32 +143,47 @@ export const validateX402TokenStructure = (token) => {
|
|
|
143
143
|
errors.push('version is required in accepted.extra');
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
|
+
// Scheme-specific accepted validation
|
|
147
|
+
if (scheme === 'nvm:erc4337') {
|
|
148
|
+
if (!accepted.planId || typeof accepted.planId !== 'string') {
|
|
149
|
+
errors.push('planId is required in accepted for nvm:erc4337');
|
|
150
|
+
}
|
|
151
|
+
}
|
|
146
152
|
}
|
|
147
153
|
// Validate payload
|
|
148
154
|
if (!t.payload || typeof t.payload !== 'object') {
|
|
149
155
|
errors.push('payload is required');
|
|
150
156
|
}
|
|
151
157
|
else {
|
|
158
|
+
const accepted = t.accepted;
|
|
159
|
+
const scheme = accepted?.scheme;
|
|
152
160
|
const payload = t.payload;
|
|
153
|
-
if (
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if (!payload.authorization || typeof payload.authorization !== 'object') {
|
|
157
|
-
errors.push('authorization is required in payload');
|
|
158
|
-
}
|
|
159
|
-
else {
|
|
160
|
-
const auth = payload.authorization;
|
|
161
|
-
if (!auth.from || typeof auth.from !== 'string') {
|
|
162
|
-
errors.push('from address is required in payload.authorization');
|
|
161
|
+
if (scheme === 'nvm:erc4337') {
|
|
162
|
+
if (!payload.signature || typeof payload.signature !== 'string') {
|
|
163
|
+
errors.push('signature is required in payload');
|
|
163
164
|
}
|
|
164
|
-
|
|
165
|
-
errors.push('
|
|
165
|
+
if (!payload.authorization || typeof payload.authorization !== 'object') {
|
|
166
|
+
errors.push('authorization is required in payload');
|
|
166
167
|
}
|
|
167
|
-
|
|
168
|
-
|
|
168
|
+
else {
|
|
169
|
+
const auth = payload.authorization;
|
|
170
|
+
if (!auth.from || typeof auth.from !== 'string') {
|
|
171
|
+
errors.push('from address is required in payload.authorization');
|
|
172
|
+
}
|
|
173
|
+
else if (!auth.from.startsWith('0x')) {
|
|
174
|
+
errors.push('from must be a valid hex address');
|
|
175
|
+
}
|
|
176
|
+
if (!auth.sessionKeysProvider || typeof auth.sessionKeysProvider !== 'string') {
|
|
177
|
+
errors.push('sessionKeysProvider is required in payload.authorization');
|
|
178
|
+
}
|
|
179
|
+
if (!Array.isArray(auth.sessionKeys)) {
|
|
180
|
+
errors.push('sessionKeys array is required in payload.authorization');
|
|
181
|
+
}
|
|
169
182
|
}
|
|
170
|
-
|
|
171
|
-
|
|
183
|
+
}
|
|
184
|
+
else if (scheme === 'nvm:card-delegation') {
|
|
185
|
+
if (!payload.token || typeof payload.token !== 'string') {
|
|
186
|
+
errors.push('token is required in payload for nvm:card-delegation');
|
|
172
187
|
}
|
|
173
188
|
}
|
|
174
189
|
}
|