@faremeter/types 0.16.0 → 0.17.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/src/client.d.ts +66 -1
- package/dist/src/client.d.ts.map +1 -1
- package/dist/src/client.js +52 -1
- package/dist/src/evm.d.ts +18 -0
- package/dist/src/evm.d.ts.map +1 -1
- package/dist/src/evm.js +18 -0
- package/dist/src/facilitator.d.ts +25 -4
- package/dist/src/facilitator.d.ts.map +1 -1
- package/dist/src/facilitator.js +1 -1
- package/dist/src/index.d.ts +43 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +43 -0
- package/dist/src/literal.d.ts +8 -0
- package/dist/src/literal.d.ts.map +1 -1
- package/dist/src/literal.js +8 -0
- package/dist/src/solana.d.ts +58 -0
- package/dist/src/solana.d.ts.map +1 -1
- package/dist/src/solana.js +70 -0
- package/dist/src/validation.d.ts +13 -0
- package/dist/src/validation.d.ts.map +1 -1
- package/dist/src/validation.js +13 -0
- package/dist/src/x402-adapters.d.ts +130 -0
- package/dist/src/x402-adapters.d.ts.map +1 -0
- package/dist/src/x402-adapters.js +297 -0
- package/dist/src/x402-adapters.test.d.ts +3 -0
- package/dist/src/x402-adapters.test.d.ts.map +1 -0
- package/dist/src/x402-adapters.test.js +912 -0
- package/dist/src/x402.d.ts +113 -9
- package/dist/src/x402.d.ts.map +1 -1
- package/dist/src/x402.js +121 -4
- package/dist/src/x402v2.d.ts +190 -0
- package/dist/src/x402v2.d.ts.map +1 -0
- package/dist/src/x402v2.js +81 -0
- package/dist/src/x402v2.test.d.ts +3 -0
- package/dist/src/x402v2.test.d.ts.map +1 -0
- package/dist/src/x402v2.test.js +63 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +11 -1
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared conversion functions between x402 v1 and v2 protocol types.
|
|
3
|
+
*
|
|
4
|
+
* All packages that need to convert between v1 and v2 types should import
|
|
5
|
+
* from this module to avoid duplicating conversion logic.
|
|
6
|
+
*/
|
|
7
|
+
import type { x402PaymentRequirements as x402PaymentRequirementsV1, x402PaymentPayload as x402PaymentPayloadV1, x402PaymentRequiredResponse as x402PaymentRequiredResponseV1, x402PaymentRequiredResponseLenient, x402VerifyResponse as x402VerifyResponseV1, x402VerifyResponseLenient, x402SettleResponse as x402SettleResponseV1, x402SettleResponseLegacy, x402SettleResponseLenient, x402SupportedKind as x402SupportedKindV1 } from "./x402.js";
|
|
8
|
+
import type { x402PaymentRequirements, x402PaymentPayload, x402PaymentRequiredResponse, x402ResourceInfo, x402VerifyResponse, x402SettleResponse, x402SupportedKind, x402SupportedKindAny } from "./x402v2.js";
|
|
9
|
+
/**
|
|
10
|
+
* Callback for translating network identifiers between formats.
|
|
11
|
+
*
|
|
12
|
+
* For v1→v2 adapters: translates legacy network names to CAIP-2 identifiers.
|
|
13
|
+
* For v2→v1 adapters: translates CAIP-2 identifiers to legacy network names.
|
|
14
|
+
*
|
|
15
|
+
* Returns the translated network identifier, or the input unchanged if
|
|
16
|
+
* no translation is available.
|
|
17
|
+
*/
|
|
18
|
+
export type NetworkTranslator = (network: string) => string;
|
|
19
|
+
/**
|
|
20
|
+
* Converts v1 payment requirements to v2 format.
|
|
21
|
+
*
|
|
22
|
+
* @param req - The v1 payment requirements
|
|
23
|
+
* @param translateNetwork - Function to translate legacy network IDs to CAIP-2
|
|
24
|
+
* @returns The v2 payment requirements
|
|
25
|
+
*/
|
|
26
|
+
export declare function adaptRequirementsV1ToV2(req: x402PaymentRequirementsV1, translateNetwork: NetworkTranslator): x402PaymentRequirements;
|
|
27
|
+
/**
|
|
28
|
+
* Converts v2 payment requirements to v1 format.
|
|
29
|
+
*
|
|
30
|
+
* @param req - The v2 payment requirements
|
|
31
|
+
* @param resource - Resource information to populate v1 fields
|
|
32
|
+
* @param translateNetwork - Optional function to translate CAIP-2 to legacy IDs
|
|
33
|
+
* @returns The v1 payment requirements with mimeType guaranteed
|
|
34
|
+
*/
|
|
35
|
+
export declare function adaptRequirementsV2ToV1(req: x402PaymentRequirements, resource: x402ResourceInfo, translateNetwork?: NetworkTranslator): x402PaymentRequirementsV1 & {
|
|
36
|
+
mimeType: string;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Extracts resource information from v1 payment requirements.
|
|
40
|
+
*
|
|
41
|
+
* @param req - The v1 payment requirements containing resource fields
|
|
42
|
+
* @returns The extracted resource information
|
|
43
|
+
*/
|
|
44
|
+
export declare function extractResourceInfoV1(req: x402PaymentRequirementsV1): x402ResourceInfo;
|
|
45
|
+
/**
|
|
46
|
+
* Converts a v1 payment payload to v2 format.
|
|
47
|
+
*
|
|
48
|
+
* @param payload - The v1 payment payload
|
|
49
|
+
* @param requirements - The v1 requirements used for resource extraction
|
|
50
|
+
* @param translateNetwork - Function to translate legacy network IDs to CAIP-2
|
|
51
|
+
* @returns The v2 payment payload
|
|
52
|
+
*/
|
|
53
|
+
export declare function adaptPayloadV1ToV2(payload: x402PaymentPayloadV1, requirements: x402PaymentRequirementsV1, translateNetwork: NetworkTranslator): x402PaymentPayload;
|
|
54
|
+
/**
|
|
55
|
+
* Converts a v1 payment required response to v2 format.
|
|
56
|
+
*
|
|
57
|
+
* @param v1Response - The v1 payment required response
|
|
58
|
+
* @param resourceURL - The URL of the protected resource
|
|
59
|
+
* @param translateNetwork - Function to translate legacy network IDs to CAIP-2
|
|
60
|
+
* @returns The v2 payment required response
|
|
61
|
+
*/
|
|
62
|
+
export declare function adaptPaymentRequiredResponseV1ToV2(v1Response: x402PaymentRequiredResponseLenient, resourceURL: string, translateNetwork: NetworkTranslator): x402PaymentRequiredResponse;
|
|
63
|
+
/**
|
|
64
|
+
* Converts a v2 payment required response to v1 format.
|
|
65
|
+
*
|
|
66
|
+
* @param v2Response - The v2 payment required response
|
|
67
|
+
* @param translateNetwork - Optional function to translate CAIP-2 to legacy IDs
|
|
68
|
+
* @returns The v1 payment required response
|
|
69
|
+
*/
|
|
70
|
+
export declare function adaptPaymentRequiredResponseV2ToV1(v2Response: x402PaymentRequiredResponse, translateNetwork?: NetworkTranslator): x402PaymentRequiredResponseV1;
|
|
71
|
+
/**
|
|
72
|
+
* Converts a v2 verify response to v1 format.
|
|
73
|
+
*
|
|
74
|
+
* @param res - The v2 verify response
|
|
75
|
+
* @returns The v1 verify response
|
|
76
|
+
*/
|
|
77
|
+
export declare function adaptVerifyResponseV2ToV1(res: x402VerifyResponse): x402VerifyResponseV1;
|
|
78
|
+
/**
|
|
79
|
+
* Converts a v1 verify response to v2 format.
|
|
80
|
+
*
|
|
81
|
+
* @param res - The v1 verify response (lenient)
|
|
82
|
+
* @returns The v2 verify response
|
|
83
|
+
*/
|
|
84
|
+
export declare function adaptVerifyResponseV1ToV2(res: x402VerifyResponseLenient): x402VerifyResponse;
|
|
85
|
+
/**
|
|
86
|
+
* Adapt v2 settle response to spec-compliant v1 format.
|
|
87
|
+
* Since v1 spec uses the same field names as v2 (transaction, network, errorReason),
|
|
88
|
+
* this is primarily a network translation pass.
|
|
89
|
+
*/
|
|
90
|
+
export declare function adaptSettleResponseV2ToV1(res: x402SettleResponse, translateNetwork?: NetworkTranslator): x402SettleResponseV1;
|
|
91
|
+
/**
|
|
92
|
+
* Adapt v2 settle response to legacy v1 format with old field names.
|
|
93
|
+
* Use this only for backward compatibility with clients expecting
|
|
94
|
+
* txHash/networkId/error field names.
|
|
95
|
+
* @deprecated Prefer adaptSettleResponseV2ToV1 for spec-compliant output
|
|
96
|
+
*/
|
|
97
|
+
export declare function adaptSettleResponseV2ToV1Legacy(res: x402SettleResponse, translateNetwork?: NetworkTranslator): x402SettleResponseLegacy;
|
|
98
|
+
/**
|
|
99
|
+
* Adapt v1 settle response to v2 format.
|
|
100
|
+
* Accepts lenient input that may have optional/nullable fields from older handlers.
|
|
101
|
+
*/
|
|
102
|
+
export declare function adaptSettleResponseV1ToV2(res: x402SettleResponseLenient): x402SettleResponse;
|
|
103
|
+
/**
|
|
104
|
+
* Adapt legacy v1 settle response (with txHash/networkId/error) to v2 format.
|
|
105
|
+
* Use this when receiving data from older clients that use legacy field names.
|
|
106
|
+
*/
|
|
107
|
+
export declare function adaptSettleResponseLegacyToV2(res: x402SettleResponseLegacy): x402SettleResponse;
|
|
108
|
+
/**
|
|
109
|
+
* Adapt a lenient v1 settle response (accepting either legacy or spec-compliant
|
|
110
|
+
* field names) to v2 format. This is the most flexible adapter for parsing
|
|
111
|
+
* incoming settle responses from unknown sources.
|
|
112
|
+
*/
|
|
113
|
+
export declare function adaptSettleResponseLenientToV2(res: x402SettleResponseLenient): x402SettleResponse;
|
|
114
|
+
/**
|
|
115
|
+
* Converts a v2 supported kind to v1 format.
|
|
116
|
+
*
|
|
117
|
+
* @param kind - The v2 supported kind
|
|
118
|
+
* @param translateNetwork - Optional function to translate CAIP-2 to legacy IDs
|
|
119
|
+
* @returns The v1 supported kind
|
|
120
|
+
*/
|
|
121
|
+
export declare function adaptSupportedKindV2ToV1(kind: x402SupportedKind, translateNetwork?: NetworkTranslator): x402SupportedKindAny;
|
|
122
|
+
/**
|
|
123
|
+
* Converts a v1 supported kind to v2 format.
|
|
124
|
+
*
|
|
125
|
+
* @param kind - The v1 supported kind
|
|
126
|
+
* @param translateNetwork - Function to translate legacy network IDs to CAIP-2
|
|
127
|
+
* @returns The v2 supported kind
|
|
128
|
+
*/
|
|
129
|
+
export declare function adaptSupportedKindV1ToV2(kind: x402SupportedKindV1, translateNetwork: NetworkTranslator): x402SupportedKind;
|
|
130
|
+
//# sourceMappingURL=x402-adapters.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"x402-adapters.d.ts","sourceRoot":"","sources":["../../src/x402-adapters.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,uBAAuB,IAAI,yBAAyB,EACpD,kBAAkB,IAAI,oBAAoB,EAC1C,2BAA2B,IAAI,6BAA6B,EAC5D,kCAAkC,EAClC,kBAAkB,IAAI,oBAAoB,EAC1C,yBAAyB,EACzB,kBAAkB,IAAI,oBAAoB,EAC1C,wBAAwB,EACxB,yBAAyB,EACzB,iBAAiB,IAAI,mBAAmB,EACzC,MAAM,QAAQ,CAAC;AAIhB,OAAO,KAAK,EACV,uBAAuB,EACvB,kBAAkB,EAClB,2BAA2B,EAC3B,gBAAgB,EAChB,kBAAkB,EAClB,kBAAkB,EAClB,iBAAiB,EACjB,oBAAoB,EACrB,MAAM,UAAU,CAAC;AAElB;;;;;;;;GAQG;AACH,MAAM,MAAM,iBAAiB,GAAG,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM,CAAC;AAE5D;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CACrC,GAAG,EAAE,yBAAyB,EAC9B,gBAAgB,EAAE,iBAAiB,GAClC,uBAAuB,CAazB;AAED;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CACrC,GAAG,EAAE,uBAAuB,EAC5B,QAAQ,EAAE,gBAAgB,EAC1B,gBAAgB,CAAC,EAAE,iBAAiB,GACnC,yBAAyB,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAA;CAAE,CAgBlD;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,GAAG,EAAE,yBAAyB,GAC7B,gBAAgB,CAWlB;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,oBAAoB,EAC7B,YAAY,EAAE,yBAAyB,EACvC,gBAAgB,EAAE,iBAAiB,GAClC,kBAAkB,CAOpB;AAED;;;;;;;GAOG;AACH,wBAAgB,kCAAkC,CAChD,UAAU,EAAE,kCAAkC,EAC9C,WAAW,EAAE,MAAM,EACnB,gBAAgB,EAAE,iBAAiB,GAClC,2BAA2B,CAyB7B;AAED;;;;;;GAMG;AACH,wBAAgB,kCAAkC,CAChD,UAAU,EAAE,2BAA2B,EACvC,gBAAgB,CAAC,EAAE,iBAAiB,GACnC,6BAA6B,CAQ/B;AAED;;;;;GAKG;AACH,wBAAgB,yBAAyB,CACvC,GAAG,EAAE,kBAAkB,GACtB,oBAAoB,CAStB;AAED;;;;;GAKG;AACH,wBAAgB,yBAAyB,CACvC,GAAG,EAAE,yBAAyB,GAC7B,kBAAkB,CAWpB;AAED;;;;GAIG;AACH,wBAAgB,yBAAyB,CACvC,GAAG,EAAE,kBAAkB,EACvB,gBAAgB,CAAC,EAAE,iBAAiB,GACnC,oBAAoB,CAWtB;AAED;;;;;GAKG;AACH,wBAAgB,+BAA+B,CAC7C,GAAG,EAAE,kBAAkB,EACvB,gBAAgB,CAAC,EAAE,iBAAiB,GACnC,wBAAwB,CAa1B;AAED;;;GAGG;AACH,wBAAgB,yBAAyB,CACvC,GAAG,EAAE,yBAAyB,GAC7B,kBAAkB,CAwBpB;AAED;;;GAGG;AACH,wBAAgB,6BAA6B,CAC3C,GAAG,EAAE,wBAAwB,GAC5B,kBAAkB,CAqBpB;AAED;;;;GAIG;AACH,wBAAgB,8BAA8B,CAC5C,GAAG,EAAE,yBAAyB,GAC7B,kBAAkB,CAGpB;AAED;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,iBAAiB,EACvB,gBAAgB,CAAC,EAAE,iBAAiB,GACnC,oBAAoB,CAUtB;AAED;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,mBAAmB,EACzB,gBAAgB,EAAE,iBAAiB,GAClC,iBAAiB,CAUnB"}
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared conversion functions between x402 v1 and v2 protocol types.
|
|
3
|
+
*
|
|
4
|
+
* All packages that need to convert between v1 and v2 types should import
|
|
5
|
+
* from this module to avoid duplicating conversion logic.
|
|
6
|
+
*/
|
|
7
|
+
import { normalizeSettleResponse } from "./x402.js";
|
|
8
|
+
/**
|
|
9
|
+
* Converts v1 payment requirements to v2 format.
|
|
10
|
+
*
|
|
11
|
+
* @param req - The v1 payment requirements
|
|
12
|
+
* @param translateNetwork - Function to translate legacy network IDs to CAIP-2
|
|
13
|
+
* @returns The v2 payment requirements
|
|
14
|
+
*/
|
|
15
|
+
export function adaptRequirementsV1ToV2(req, translateNetwork) {
|
|
16
|
+
const result = {
|
|
17
|
+
scheme: req.scheme,
|
|
18
|
+
network: translateNetwork(req.network),
|
|
19
|
+
amount: req.maxAmountRequired,
|
|
20
|
+
asset: req.asset,
|
|
21
|
+
payTo: req.payTo,
|
|
22
|
+
maxTimeoutSeconds: req.maxTimeoutSeconds,
|
|
23
|
+
};
|
|
24
|
+
if (req.extra !== undefined) {
|
|
25
|
+
result.extra = req.extra;
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Converts v2 payment requirements to v1 format.
|
|
31
|
+
*
|
|
32
|
+
* @param req - The v2 payment requirements
|
|
33
|
+
* @param resource - Resource information to populate v1 fields
|
|
34
|
+
* @param translateNetwork - Optional function to translate CAIP-2 to legacy IDs
|
|
35
|
+
* @returns The v1 payment requirements with mimeType guaranteed
|
|
36
|
+
*/
|
|
37
|
+
export function adaptRequirementsV2ToV1(req, resource, translateNetwork) {
|
|
38
|
+
const result = {
|
|
39
|
+
scheme: req.scheme,
|
|
40
|
+
network: translateNetwork ? translateNetwork(req.network) : req.network,
|
|
41
|
+
maxAmountRequired: req.amount,
|
|
42
|
+
resource: resource.url,
|
|
43
|
+
description: resource.description ?? "",
|
|
44
|
+
mimeType: resource.mimeType ?? "",
|
|
45
|
+
payTo: req.payTo,
|
|
46
|
+
maxTimeoutSeconds: req.maxTimeoutSeconds,
|
|
47
|
+
asset: req.asset,
|
|
48
|
+
};
|
|
49
|
+
if (req.extra !== undefined) {
|
|
50
|
+
result.extra = req.extra;
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Extracts resource information from v1 payment requirements.
|
|
56
|
+
*
|
|
57
|
+
* @param req - The v1 payment requirements containing resource fields
|
|
58
|
+
* @returns The extracted resource information
|
|
59
|
+
*/
|
|
60
|
+
export function extractResourceInfoV1(req) {
|
|
61
|
+
const result = {
|
|
62
|
+
url: req.resource,
|
|
63
|
+
};
|
|
64
|
+
if (req.description) {
|
|
65
|
+
result.description = req.description;
|
|
66
|
+
}
|
|
67
|
+
if (req.mimeType) {
|
|
68
|
+
result.mimeType = req.mimeType;
|
|
69
|
+
}
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Converts a v1 payment payload to v2 format.
|
|
74
|
+
*
|
|
75
|
+
* @param payload - The v1 payment payload
|
|
76
|
+
* @param requirements - The v1 requirements used for resource extraction
|
|
77
|
+
* @param translateNetwork - Function to translate legacy network IDs to CAIP-2
|
|
78
|
+
* @returns The v2 payment payload
|
|
79
|
+
*/
|
|
80
|
+
export function adaptPayloadV1ToV2(payload, requirements, translateNetwork) {
|
|
81
|
+
return {
|
|
82
|
+
x402Version: 2,
|
|
83
|
+
accepted: adaptRequirementsV1ToV2(requirements, translateNetwork),
|
|
84
|
+
payload: payload.payload,
|
|
85
|
+
resource: extractResourceInfoV1(requirements),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Converts a v1 payment required response to v2 format.
|
|
90
|
+
*
|
|
91
|
+
* @param v1Response - The v1 payment required response
|
|
92
|
+
* @param resourceURL - The URL of the protected resource
|
|
93
|
+
* @param translateNetwork - Function to translate legacy network IDs to CAIP-2
|
|
94
|
+
* @returns The v2 payment required response
|
|
95
|
+
*/
|
|
96
|
+
export function adaptPaymentRequiredResponseV1ToV2(v1Response, resourceURL, translateNetwork) {
|
|
97
|
+
const firstAccept = v1Response.accepts[0];
|
|
98
|
+
const resourceInfo = {
|
|
99
|
+
url: resourceURL,
|
|
100
|
+
};
|
|
101
|
+
if (firstAccept?.description) {
|
|
102
|
+
resourceInfo.description = firstAccept.description;
|
|
103
|
+
}
|
|
104
|
+
if (firstAccept?.mimeType) {
|
|
105
|
+
resourceInfo.mimeType = firstAccept.mimeType;
|
|
106
|
+
}
|
|
107
|
+
const result = {
|
|
108
|
+
x402Version: 2,
|
|
109
|
+
resource: resourceInfo,
|
|
110
|
+
accepts: v1Response.accepts.map((req) => adaptRequirementsV1ToV2(req, translateNetwork)),
|
|
111
|
+
};
|
|
112
|
+
if (v1Response.error) {
|
|
113
|
+
result.error = v1Response.error;
|
|
114
|
+
}
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Converts a v2 payment required response to v1 format.
|
|
119
|
+
*
|
|
120
|
+
* @param v2Response - The v2 payment required response
|
|
121
|
+
* @param translateNetwork - Optional function to translate CAIP-2 to legacy IDs
|
|
122
|
+
* @returns The v1 payment required response
|
|
123
|
+
*/
|
|
124
|
+
export function adaptPaymentRequiredResponseV2ToV1(v2Response, translateNetwork) {
|
|
125
|
+
return {
|
|
126
|
+
x402Version: 1,
|
|
127
|
+
accepts: v2Response.accepts.map((req) => adaptRequirementsV2ToV1(req, v2Response.resource, translateNetwork)),
|
|
128
|
+
error: v2Response.error ?? "",
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Converts a v2 verify response to v1 format.
|
|
133
|
+
*
|
|
134
|
+
* @param res - The v2 verify response
|
|
135
|
+
* @returns The v1 verify response
|
|
136
|
+
*/
|
|
137
|
+
export function adaptVerifyResponseV2ToV1(res) {
|
|
138
|
+
const result = {
|
|
139
|
+
isValid: res.isValid,
|
|
140
|
+
payer: res.payer ?? "",
|
|
141
|
+
};
|
|
142
|
+
if (res.invalidReason !== undefined) {
|
|
143
|
+
result.invalidReason = res.invalidReason;
|
|
144
|
+
}
|
|
145
|
+
return result;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Converts a v1 verify response to v2 format.
|
|
149
|
+
*
|
|
150
|
+
* @param res - The v1 verify response (lenient)
|
|
151
|
+
* @returns The v2 verify response
|
|
152
|
+
*/
|
|
153
|
+
export function adaptVerifyResponseV1ToV2(res) {
|
|
154
|
+
const result = {
|
|
155
|
+
isValid: res.isValid,
|
|
156
|
+
};
|
|
157
|
+
if (res.invalidReason !== undefined && res.invalidReason !== null) {
|
|
158
|
+
result.invalidReason = res.invalidReason;
|
|
159
|
+
}
|
|
160
|
+
if (res.payer) {
|
|
161
|
+
result.payer = res.payer;
|
|
162
|
+
}
|
|
163
|
+
return result;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Adapt v2 settle response to spec-compliant v1 format.
|
|
167
|
+
* Since v1 spec uses the same field names as v2 (transaction, network, errorReason),
|
|
168
|
+
* this is primarily a network translation pass.
|
|
169
|
+
*/
|
|
170
|
+
export function adaptSettleResponseV2ToV1(res, translateNetwork) {
|
|
171
|
+
const result = {
|
|
172
|
+
success: res.success,
|
|
173
|
+
transaction: res.transaction,
|
|
174
|
+
network: translateNetwork ? translateNetwork(res.network) : res.network,
|
|
175
|
+
payer: res.payer ?? "",
|
|
176
|
+
};
|
|
177
|
+
if (res.errorReason !== undefined) {
|
|
178
|
+
result.errorReason = res.errorReason;
|
|
179
|
+
}
|
|
180
|
+
return result;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Adapt v2 settle response to legacy v1 format with old field names.
|
|
184
|
+
* Use this only for backward compatibility with clients expecting
|
|
185
|
+
* txHash/networkId/error field names.
|
|
186
|
+
* @deprecated Prefer adaptSettleResponseV2ToV1 for spec-compliant output
|
|
187
|
+
*/
|
|
188
|
+
export function adaptSettleResponseV2ToV1Legacy(res, translateNetwork) {
|
|
189
|
+
const result = {
|
|
190
|
+
success: res.success,
|
|
191
|
+
txHash: res.transaction,
|
|
192
|
+
networkId: translateNetwork ? translateNetwork(res.network) : res.network,
|
|
193
|
+
};
|
|
194
|
+
if (res.errorReason !== undefined) {
|
|
195
|
+
result.error = res.errorReason;
|
|
196
|
+
}
|
|
197
|
+
if (res.payer !== undefined) {
|
|
198
|
+
result.payer = res.payer;
|
|
199
|
+
}
|
|
200
|
+
return result;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Adapt v1 settle response to v2 format.
|
|
204
|
+
* Accepts lenient input that may have optional/nullable fields from older handlers.
|
|
205
|
+
*/
|
|
206
|
+
export function adaptSettleResponseV1ToV2(res) {
|
|
207
|
+
const network = res.network ?? res.networkId;
|
|
208
|
+
const transaction = res.transaction ?? res.txHash;
|
|
209
|
+
if (network == null) {
|
|
210
|
+
throw new Error("v1 settle response is missing network");
|
|
211
|
+
}
|
|
212
|
+
if (res.success && transaction == null) {
|
|
213
|
+
throw new Error("v1 settle response has success: true but missing transaction");
|
|
214
|
+
}
|
|
215
|
+
const result = {
|
|
216
|
+
success: res.success,
|
|
217
|
+
transaction: transaction ?? "",
|
|
218
|
+
network: network,
|
|
219
|
+
};
|
|
220
|
+
const errorReason = res.errorReason ?? res.error;
|
|
221
|
+
if (errorReason !== undefined && errorReason !== null) {
|
|
222
|
+
result.errorReason = errorReason;
|
|
223
|
+
}
|
|
224
|
+
if (res.payer) {
|
|
225
|
+
result.payer = res.payer;
|
|
226
|
+
}
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Adapt legacy v1 settle response (with txHash/networkId/error) to v2 format.
|
|
231
|
+
* Use this when receiving data from older clients that use legacy field names.
|
|
232
|
+
*/
|
|
233
|
+
export function adaptSettleResponseLegacyToV2(res) {
|
|
234
|
+
if (res.networkId == null) {
|
|
235
|
+
throw new Error("legacy v1 settle response is missing networkId");
|
|
236
|
+
}
|
|
237
|
+
if (res.success && res.txHash == null) {
|
|
238
|
+
throw new Error("legacy v1 settle response has success: true but missing txHash");
|
|
239
|
+
}
|
|
240
|
+
const result = {
|
|
241
|
+
success: res.success,
|
|
242
|
+
transaction: res.txHash ?? "",
|
|
243
|
+
network: res.networkId,
|
|
244
|
+
};
|
|
245
|
+
if (res.error !== undefined && res.error !== null) {
|
|
246
|
+
result.errorReason = res.error;
|
|
247
|
+
}
|
|
248
|
+
if (res.payer !== undefined) {
|
|
249
|
+
result.payer = res.payer;
|
|
250
|
+
}
|
|
251
|
+
return result;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Adapt a lenient v1 settle response (accepting either legacy or spec-compliant
|
|
255
|
+
* field names) to v2 format. This is the most flexible adapter for parsing
|
|
256
|
+
* incoming settle responses from unknown sources.
|
|
257
|
+
*/
|
|
258
|
+
export function adaptSettleResponseLenientToV2(res) {
|
|
259
|
+
const normalized = normalizeSettleResponse(res);
|
|
260
|
+
return adaptSettleResponseV1ToV2(normalized);
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Converts a v2 supported kind to v1 format.
|
|
264
|
+
*
|
|
265
|
+
* @param kind - The v2 supported kind
|
|
266
|
+
* @param translateNetwork - Optional function to translate CAIP-2 to legacy IDs
|
|
267
|
+
* @returns The v1 supported kind
|
|
268
|
+
*/
|
|
269
|
+
export function adaptSupportedKindV2ToV1(kind, translateNetwork) {
|
|
270
|
+
const result = {
|
|
271
|
+
x402Version: 1,
|
|
272
|
+
scheme: kind.scheme,
|
|
273
|
+
network: translateNetwork ? translateNetwork(kind.network) : kind.network,
|
|
274
|
+
};
|
|
275
|
+
if (kind.extra !== undefined) {
|
|
276
|
+
result.extra = kind.extra;
|
|
277
|
+
}
|
|
278
|
+
return result;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Converts a v1 supported kind to v2 format.
|
|
282
|
+
*
|
|
283
|
+
* @param kind - The v1 supported kind
|
|
284
|
+
* @param translateNetwork - Function to translate legacy network IDs to CAIP-2
|
|
285
|
+
* @returns The v2 supported kind
|
|
286
|
+
*/
|
|
287
|
+
export function adaptSupportedKindV1ToV2(kind, translateNetwork) {
|
|
288
|
+
const result = {
|
|
289
|
+
x402Version: 2,
|
|
290
|
+
scheme: kind.scheme,
|
|
291
|
+
network: translateNetwork(kind.network),
|
|
292
|
+
};
|
|
293
|
+
if (kind.extra !== undefined) {
|
|
294
|
+
result.extra = kind.extra;
|
|
295
|
+
}
|
|
296
|
+
return result;
|
|
297
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"x402-adapters.test.d.ts","sourceRoot":"","sources":["../../src/x402-adapters.test.ts"],"names":[],"mappings":""}
|