@x402api/agent-wallet-core 0.1.0

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.
Files changed (62) hide show
  1. package/README.md +12 -0
  2. package/dist/balances.d.ts +32 -0
  3. package/dist/balances.d.ts.map +1 -0
  4. package/dist/balances.js +278 -0
  5. package/dist/balances.js.map +1 -0
  6. package/dist/errors.d.ts +15 -0
  7. package/dist/errors.d.ts.map +1 -0
  8. package/dist/errors.js +73 -0
  9. package/dist/errors.js.map +1 -0
  10. package/dist/index.d.ts +15 -0
  11. package/dist/index.d.ts.map +1 -0
  12. package/dist/index.js +15 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/notifications.d.ts +71 -0
  15. package/dist/notifications.d.ts.map +1 -0
  16. package/dist/notifications.js +209 -0
  17. package/dist/notifications.js.map +1 -0
  18. package/dist/payment/attempt-store.d.ts +49 -0
  19. package/dist/payment/attempt-store.d.ts.map +1 -0
  20. package/dist/payment/attempt-store.js +283 -0
  21. package/dist/payment/attempt-store.js.map +1 -0
  22. package/dist/payment/authorize.d.ts +22 -0
  23. package/dist/payment/authorize.d.ts.map +1 -0
  24. package/dist/payment/authorize.js +386 -0
  25. package/dist/payment/authorize.js.map +1 -0
  26. package/dist/payment/contracts.d.ts +36 -0
  27. package/dist/payment/contracts.d.ts.map +1 -0
  28. package/dist/payment/contracts.js +132 -0
  29. package/dist/payment/contracts.js.map +1 -0
  30. package/dist/protocol/base.d.ts +78 -0
  31. package/dist/protocol/base.d.ts.map +1 -0
  32. package/dist/protocol/base.js +459 -0
  33. package/dist/protocol/base.js.map +1 -0
  34. package/dist/protocol/external-recipient.d.ts +22 -0
  35. package/dist/protocol/external-recipient.d.ts.map +1 -0
  36. package/dist/protocol/external-recipient.js +149 -0
  37. package/dist/protocol/external-recipient.js.map +1 -0
  38. package/dist/protocol/http.d.ts +52 -0
  39. package/dist/protocol/http.d.ts.map +1 -0
  40. package/dist/protocol/http.js +283 -0
  41. package/dist/protocol/http.js.map +1 -0
  42. package/dist/protocol/solana.d.ts +147 -0
  43. package/dist/protocol/solana.d.ts.map +1 -0
  44. package/dist/protocol/solana.js +549 -0
  45. package/dist/protocol/solana.js.map +1 -0
  46. package/dist/protocol/tron.d.ts +130 -0
  47. package/dist/protocol/tron.d.ts.map +1 -0
  48. package/dist/protocol/tron.js +539 -0
  49. package/dist/protocol/tron.js.map +1 -0
  50. package/dist/storage/keystore.d.ts +51 -0
  51. package/dist/storage/keystore.d.ts.map +1 -0
  52. package/dist/storage/keystore.js +317 -0
  53. package/dist/storage/keystore.js.map +1 -0
  54. package/dist/storage/paths.d.ts +8 -0
  55. package/dist/storage/paths.d.ts.map +1 -0
  56. package/dist/storage/paths.js +22 -0
  57. package/dist/storage/paths.js.map +1 -0
  58. package/dist/storage/private-files.d.ts +8 -0
  59. package/dist/storage/private-files.d.ts.map +1 -0
  60. package/dist/storage/private-files.js +83 -0
  61. package/dist/storage/private-files.js.map +1 -0
  62. package/package.json +35 -0
@@ -0,0 +1,149 @@
1
+ import { canonicalJson, } from "./http.js";
2
+ export const EXTERNAL_RECIPIENT_EXTENSION = "com.k1hub.external-recipient";
3
+ const SHA256 = /^sha256:[0-9a-f]{64}$/;
4
+ const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
5
+ const CANONICAL_UTC = /^(?:[0-9]{4})-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])T(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](?:\.[0-9]{1,6})?Z$/;
6
+ function object(value, field) {
7
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
8
+ throw new Error(`${field} is malformed`);
9
+ }
10
+ return value;
11
+ }
12
+ function exact(value, keys, field) {
13
+ const result = object(value, field);
14
+ if (Object.keys(result).sort().join(",") !== [...keys].sort().join(",")) {
15
+ throw new Error(`${field} contains missing or unknown fields`);
16
+ }
17
+ return result;
18
+ }
19
+ function canonicalTimestamp(value, field) {
20
+ if (typeof value !== "string" ||
21
+ !CANONICAL_UTC.test(value) ||
22
+ !Number.isFinite(Date.parse(value))) {
23
+ throw new Error(`${field} is not a canonical UTC timestamp`);
24
+ }
25
+ }
26
+ function validateDescriptor(value, recipient) {
27
+ const candidate = object(value, "external recipient descriptor");
28
+ const type = candidate.type;
29
+ const rotation = type === "com.k1hub.external-receiving-address-rotation.v1";
30
+ const descriptor = exact(candidate, rotation
31
+ ? [
32
+ "type",
33
+ "tenantId",
34
+ "network",
35
+ "address",
36
+ "controlChallengeDigest",
37
+ "replacesWalletVersionId",
38
+ "reason",
39
+ "coolingOffEndsAt",
40
+ ]
41
+ : ["type", "tenantId", "network", "address", "controlChallengeDigest"], "external recipient descriptor");
42
+ if ((!rotation &&
43
+ descriptor.type !== "com.k1hub.external-receiving-address.v1") ||
44
+ typeof descriptor.tenantId !== "string" ||
45
+ !UUID.test(descriptor.tenantId) ||
46
+ descriptor.network !== recipient.network ||
47
+ descriptor.address !== recipient.payTo ||
48
+ typeof descriptor.controlChallengeDigest !== "string" ||
49
+ !SHA256.test(descriptor.controlChallengeDigest)) {
50
+ throw new Error("external recipient descriptor does not match the payment recipient");
51
+ }
52
+ if (rotation) {
53
+ if (typeof descriptor.replacesWalletVersionId !== "string" ||
54
+ !UUID.test(descriptor.replacesWalletVersionId) ||
55
+ typeof descriptor.reason !== "string" ||
56
+ descriptor.reason.trim() !== descriptor.reason ||
57
+ descriptor.reason.length < 1 ||
58
+ descriptor.reason.length > 1_024) {
59
+ throw new Error("external recipient rotation descriptor is malformed");
60
+ }
61
+ canonicalTimestamp(descriptor.coolingOffEndsAt, "external recipient coolingOffEndsAt");
62
+ }
63
+ return descriptor;
64
+ }
65
+ function utf8(value) {
66
+ return new TextEncoder().encode(value);
67
+ }
68
+ function arrayBuffer(value) {
69
+ return value.buffer.slice(value.byteOffset, value.byteOffset + value.byteLength);
70
+ }
71
+ function hex(value) {
72
+ return [...new Uint8Array(value)]
73
+ .map((byte) => byte.toString(16).padStart(2, "0"))
74
+ .join("");
75
+ }
76
+ async function digest(value) {
77
+ return `sha256:${hex(await crypto.subtle.digest("SHA-256", arrayBuffer(utf8(canonicalJson(value)))))}`;
78
+ }
79
+ /**
80
+ * Verify the immutable address-only recipient declaration before a browser
81
+ * asks any wallet for accounts, signatures, or a transaction.
82
+ *
83
+ * This declaration is intentionally separate from the dormant managed-wallet
84
+ * manifest protocol. Its digest binds the exact receiving-address descriptor
85
+ * whose signed-nonce proof of control was admitted by K1Hub.
86
+ */
87
+ export async function verifyExternalRecipientDeclaration(options) {
88
+ const extensions = options.paymentRequired.extensions ?? {};
89
+ if (extensions["com.k1hub.wallet-manifest"] !== undefined) {
90
+ throw new Error("external payment challenge must not contain a managed wallet manifest");
91
+ }
92
+ const declaration = extensions[EXTERNAL_RECIPIENT_EXTENSION];
93
+ const info = exact(declaration?.info, ["version", "recipients"], "external-recipient info");
94
+ if (info.version !== 1 || !Array.isArray(info.recipients)) {
95
+ throw new Error("unsupported external-recipient declaration version");
96
+ }
97
+ if (info.recipients.length < 1 || info.recipients.length > 32) {
98
+ throw new Error("external-recipient list is outside the supported bound");
99
+ }
100
+ const verified = [];
101
+ const identities = new Set();
102
+ for (const value of info.recipients) {
103
+ const entry = exact(value, [
104
+ "network",
105
+ "asset",
106
+ "payTo",
107
+ "recipientDescriptorDigest",
108
+ "recipientDescriptor",
109
+ ], "external recipient");
110
+ if (typeof entry.network !== "string" ||
111
+ typeof entry.asset !== "string" ||
112
+ typeof entry.payTo !== "string" ||
113
+ typeof entry.recipientDescriptorDigest !== "string" ||
114
+ !SHA256.test(entry.recipientDescriptorDigest)) {
115
+ throw new Error("external recipient declaration is malformed");
116
+ }
117
+ const descriptor = validateDescriptor(entry.recipientDescriptor, {
118
+ network: entry.network,
119
+ payTo: entry.payTo,
120
+ });
121
+ if ((await digest(descriptor)) !== entry.recipientDescriptorDigest) {
122
+ throw new Error("external recipient descriptor digest is invalid");
123
+ }
124
+ const identity = canonicalJson({
125
+ network: entry.network,
126
+ asset: entry.asset,
127
+ payTo: entry.payTo,
128
+ });
129
+ if (identities.has(identity)) {
130
+ throw new Error("external recipient declaration contains a duplicate");
131
+ }
132
+ identities.add(identity);
133
+ verified.push({
134
+ network: entry.network,
135
+ asset: entry.asset,
136
+ payTo: entry.payTo,
137
+ recipientDescriptorDigest: entry.recipientDescriptorDigest,
138
+ recipientDescriptor: descriptor,
139
+ });
140
+ }
141
+ const matches = verified.filter((entry) => entry.network === options.accepted.network &&
142
+ entry.asset === options.accepted.asset &&
143
+ entry.payTo === options.accepted.payTo);
144
+ if (matches.length !== 1) {
145
+ throw new Error("accepted requirement has no unique external recipient declaration");
146
+ }
147
+ return matches[0];
148
+ }
149
+ //# sourceMappingURL=external-recipient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"external-recipient.js","sourceRoot":"","sources":["../../src/protocol/external-recipient.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,GAId,MAAM,WAAW,CAAC;AAEnB,MAAM,CAAC,MAAM,4BAA4B,GAAG,8BAA8B,CAAC;AAE3E,MAAM,MAAM,GAAG,uBAAuB,CAAC;AACvC,MAAM,IAAI,GACR,2EAA2E,CAAC;AAC9E,MAAM,aAAa,GACjB,0HAA0H,CAAC;AAU7H,SAAS,MAAM,CAAC,KAAc,EAAE,KAAa;IAC3C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QACxE,MAAM,IAAI,KAAK,CAAC,GAAG,KAAK,eAAe,CAAC,CAAC;IAC3C,CAAC;IACD,OAAO,KAAgC,CAAC;AAC1C,CAAC;AAED,SAAS,KAAK,CACZ,KAAc,EACd,IAAuB,EACvB,KAAa;IAEb,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACpC,IAAI,MAAM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O,EAAE,KAAK,CAAC,OAAO;YACtB,KAAK,EAAE,KAAK,CAAC,KAAK;YAClB,KAAK,EAAE,KAAK,CAAC,KAAK;YAClB,yBAAyB,EAAE,KAAK,CAAC,yBAAyB;YAC1D,mBAAmB,EAAE,UAAU;SAChC,CAAC,CAAC;IACL,CAAC;IAED,MAAM,OAAO,GAAG,QAAQ,CAAC,MAAM,CAC7B,CAAC,KAAK,EAAE,EAAE,CACR,KAAK,CAAC,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,OAAO;QAC1C,KAAK,CAAC,KAAK,KAAK,OAAO,CAAC,QAAQ,CAAC,KAAK;QACtC,KAAK,CAAC,KAAK,KAAK,OAAO,CAAC,QAAQ,CAAC,KAAK,CACzC,CAAC;IACF,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CACb,mEAAmE,CACpE,CAAC;IACJ,CAAC;IACD,OAAO,OAAO,CAAC,CAAC,CAAE,CAAC;AACrB,CAAC"}
@@ -0,0 +1,52 @@
1
+ export type JsonScalar = string | number | boolean | null;
2
+ export type JsonValue = JsonScalar | JsonObject | JsonValue[];
3
+ export type JsonObject = {
4
+ [key: string]: JsonValue;
5
+ };
6
+ export type PaymentRequirement = {
7
+ scheme: string;
8
+ network: string;
9
+ amount: string;
10
+ asset: string;
11
+ payTo: string;
12
+ maxTimeoutSeconds: number;
13
+ extra: JsonObject;
14
+ };
15
+ export type ResourceInfo = {
16
+ url: string;
17
+ description?: string;
18
+ mimeType?: string;
19
+ serviceName?: string;
20
+ tags?: string[];
21
+ iconUrl?: string;
22
+ };
23
+ export type PaymentRequired = {
24
+ x402Version: 2;
25
+ error?: string;
26
+ resource: ResourceInfo;
27
+ accepts: PaymentRequirement[];
28
+ extensions?: Record<string, ExtensionDeclaration>;
29
+ };
30
+ export type ExtensionDeclaration = {
31
+ info: JsonObject;
32
+ schema?: JsonObject;
33
+ };
34
+ export type PaymentPayload = {
35
+ x402Version: 2;
36
+ accepted: PaymentRequirement;
37
+ payload: JsonObject;
38
+ extensions: Record<string, ExtensionDeclaration>;
39
+ resource?: PaymentRequired["resource"];
40
+ };
41
+ export declare function canonicalJson(value: JsonValue): string;
42
+ export declare function decodePaymentRequiredHeader(value: string): PaymentRequired;
43
+ export declare function encodePaymentRequiredHeader(value: PaymentRequired): string;
44
+ export declare function createPaymentPayload(options: {
45
+ paymentRequired: PaymentRequired;
46
+ accepted: PaymentRequirement;
47
+ schemePayload: JsonObject;
48
+ paymentIdentifier?: string;
49
+ }): PaymentPayload;
50
+ export declare function encodePaymentSignature(value: PaymentPayload): string;
51
+ export declare function decodePaymentSignature(value: string): PaymentPayload;
52
+ //# sourceMappingURL=http.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/protocol/http.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAC1D,MAAM,MAAM,SAAS,GAAG,UAAU,GAAG,UAAU,GAAG,SAAS,EAAE,CAAC;AAC9D,MAAM,MAAM,UAAU,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAEtD,MAAM,MAAM,kBAAkB,GAAG;IAC/B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,iBAAiB,EAAE,MAAM,CAAC;IAC1B,KAAK,EAAE,UAAU,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,CAAC,EA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 @@
1
+ const MAX_HEADER_BYTES = 64 * 1024;
2
+ const MAX_SIGNATURE_BYTES = 512 * 1024;
3
+ const DECIMAL = /^(?:0|[1-9][0-9]{0,77})$/;
4
+ const CAIP2_NETWORK = /^[-a-z0-9]{3,8}:[-_a-zA-Z0-9]{1,32}$/;
5
+ const PAYMENT_IDENTIFIER = /^[A-Za-z0-9_-]{16,128}$/;
6
+ const BASE64 = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
7
+ const PRINTABLE_ASCII = /^[\x20-\x7e]+$/;
8
+ function isObject(value) {
9
+ return typeof value === "object" && value !== null && !Array.isArray(value);
10
+ }
11
+ function assertJsonValue(value) {
12
+ if (value === null ||
13
+ typeof value === "string" ||
14
+ typeof value === "boolean") {
15
+ return;
16
+ }
17
+ if (typeof value === "number") {
18
+ if (!Number.isSafeInteger(value)) {
19
+ throw new Error("cryptographic JSON numbers must be safe integers");
20
+ }
21
+ return;
22
+ }
23
+ if (Array.isArray(value)) {
24
+ value.forEach(assertJsonValue);
25
+ return;
26
+ }
27
+ if (isObject(value)) {
28
+ Object.values(value).forEach(assertJsonValue);
29
+ return;
30
+ }
31
+ throw new Error("value is not JSON");
32
+ }
33
+ export function canonicalJson(value) {
34
+ assertJsonValue(value);
35
+ if (value === null || typeof value !== "object") {
36
+ return JSON.stringify(value);
37
+ }
38
+ if (Array.isArray(value)) {
39
+ return `[${value.map((item) => canonicalJson(item)).join(",")}]`;
40
+ }
41
+ return `{${Object.keys(value)
42
+ .sort()
43
+ .map((key) => `${JSON.stringify(key)}:${canonicalJson(value[key])}`)
44
+ .join(",")}}`;
45
+ }
46
+ function decodeBase64Json(value, maximumBytes) {
47
+ if (value.length === 0 || value.length > maximumBytes || !BASE64.test(value)) {
48
+ throw new Error("x402 header is not canonical base64");
49
+ }
50
+ let bytes;
51
+ try {
52
+ const binary = atob(value);
53
+ if (btoa(binary) !== value)
54
+ throw new Error("non-canonical base64");
55
+ bytes = Uint8Array.from(binary, (character) => character.charCodeAt(0));
56
+ }
57
+ catch {
58
+ throw new Error("x402 header is not valid base64");
59
+ }
60
+ if (bytes.byteLength > maximumBytes) {
61
+ throw new Error("x402 header exceeds the maximum size");
62
+ }
63
+ try {
64
+ return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
65
+ }
66
+ catch {
67
+ throw new Error("x402 header is not UTF-8 JSON");
68
+ }
69
+ }
70
+ function encodeBase64Json(value, maximumBytes) {
71
+ const bytes = new TextEncoder().encode(canonicalJson(value));
72
+ if (bytes.byteLength > maximumBytes) {
73
+ throw new Error("x402 value exceeds the maximum size");
74
+ }
75
+ let binary = "";
76
+ for (const byte of bytes)
77
+ binary += String.fromCharCode(byte);
78
+ const encoded = btoa(binary);
79
+ if (encoded.length > maximumBytes) {
80
+ throw new Error("x402 header exceeds the maximum size");
81
+ }
82
+ return encoded;
83
+ }
84
+ function requirement(value) {
85
+ if (!isObject(value))
86
+ throw new Error("payment requirement is not an object");
87
+ const allowed = new Set([
88
+ "scheme",
89
+ "network",
90
+ "amount",
91
+ "asset",
92
+ "payTo",
93
+ "maxTimeoutSeconds",
94
+ "extra",
95
+ ]);
96
+ if (Object.keys(value).some((key) => !allowed.has(key))) {
97
+ throw new Error("payment requirement has unknown fields");
98
+ }
99
+ if (typeof value.scheme !== "string" ||
100
+ typeof value.network !== "string" ||
101
+ !CAIP2_NETWORK.test(value.network) ||
102
+ typeof value.amount !== "string" ||
103
+ !DECIMAL.test(value.amount) ||
104
+ value.amount === "0" ||
105
+ typeof value.asset !== "string" ||
106
+ typeof value.payTo !== "string" ||
107
+ !Number.isSafeInteger(value.maxTimeoutSeconds) ||
108
+ value.maxTimeoutSeconds < 1 ||
109
+ !isObject(value.extra)) {
110
+ throw new Error("payment requirement is malformed");
111
+ }
112
+ assertJsonValue(value.extra);
113
+ return value;
114
+ }
115
+ function optionalString(value) {
116
+ return value === undefined || typeof value === "string";
117
+ }
118
+ function resource(value) {
119
+ const allowed = new Set([
120
+ "url",
121
+ "description",
122
+ "mimeType",
123
+ "serviceName",
124
+ "tags",
125
+ "iconUrl",
126
+ ]);
127
+ if (!isObject(value) ||
128
+ Object.keys(value).some((key) => !allowed.has(key)) ||
129
+ typeof value.url !== "string" ||
130
+ !optionalString(value.description) ||
131
+ !optionalString(value.mimeType) ||
132
+ !optionalString(value.serviceName) ||
133
+ (value.serviceName !== undefined &&
134
+ (value.serviceName.length < 1 ||
135
+ value.serviceName.length > 32 ||
136
+ !PRINTABLE_ASCII.test(value.serviceName))) ||
137
+ (value.tags !== undefined &&
138
+ (!Array.isArray(value.tags) ||
139
+ value.tags.length > 5 ||
140
+ value.tags.some((tag) => typeof tag !== "string" ||
141
+ tag.length < 1 ||
142
+ tag.length > 32 ||
143
+ !PRINTABLE_ASCII.test(tag)))) ||
144
+ !optionalString(value.iconUrl)) {
145
+ throw new Error("payment resource is malformed");
146
+ }
147
+ return value;
148
+ }
149
+ function paymentPayloadExtensions(value) {
150
+ if (!isObject(value)) {
151
+ throw new Error("payment payload extensions are malformed");
152
+ }
153
+ const extensions = {};
154
+ for (const [name, declaration] of Object.entries(value)) {
155
+ if (!isObject(declaration) ||
156
+ Object.keys(declaration).some((key) => key !== "info" && key !== "schema") ||
157
+ !isObject(declaration.info) ||
158
+ (declaration.schema !== undefined && !isObject(declaration.schema))) {
159
+ throw new Error(`payment payload extension ${name} is malformed`);
160
+ }
161
+ assertJsonValue(declaration.info);
162
+ if (declaration.schema !== undefined)
163
+ assertJsonValue(declaration.schema);
164
+ extensions[name] = declaration;
165
+ }
166
+ return extensions;
167
+ }
168
+ export function decodePaymentRequiredHeader(value) {
169
+ const parsed = decodeBase64Json(value, MAX_HEADER_BYTES);
170
+ if (!isObject(parsed))
171
+ throw new Error("PAYMENT-REQUIRED is not an object");
172
+ const allowed = new Set([
173
+ "x402Version",
174
+ "error",
175
+ "resource",
176
+ "accepts",
177
+ "extensions",
178
+ ]);
179
+ if (Object.keys(parsed).some((key) => !allowed.has(key))) {
180
+ throw new Error("PAYMENT-REQUIRED has unknown fields");
181
+ }
182
+ if (parsed.x402Version !== 2 ||
183
+ (parsed.error !== undefined && typeof parsed.error !== "string") ||
184
+ !Array.isArray(parsed.accepts) ||
185
+ parsed.accepts.length < 1 ||
186
+ (parsed.extensions !== undefined && !isObject(parsed.extensions))) {
187
+ throw new Error("PAYMENT-REQUIRED is malformed");
188
+ }
189
+ const accepts = parsed.accepts.map(requirement);
190
+ const extensions = {};
191
+ if (isObject(parsed.extensions)) {
192
+ for (const [name, declaration] of Object.entries(parsed.extensions)) {
193
+ if (!isObject(declaration) ||
194
+ Object.keys(declaration).some((key) => key !== "info" && key !== "schema") ||
195
+ !isObject(declaration.info) ||
196
+ (declaration.schema !== undefined && !isObject(declaration.schema))) {
197
+ throw new Error(`extension ${name} is malformed`);
198
+ }
199
+ assertJsonValue(declaration.info);
200
+ if (declaration.schema !== undefined)
201
+ assertJsonValue(declaration.schema);
202
+ extensions[name] = declaration;
203
+ }
204
+ }
205
+ return {
206
+ x402Version: 2,
207
+ ...(parsed.error === undefined ? {} : { error: parsed.error }),
208
+ resource: resource(parsed.resource),
209
+ accepts,
210
+ ...(Object.keys(extensions).length > 0 ? { extensions } : {}),
211
+ };
212
+ }
213
+ export function encodePaymentRequiredHeader(value) {
214
+ return encodeBase64Json(value, MAX_HEADER_BYTES);
215
+ }
216
+ function copiedExtensionInfo(declarations) {
217
+ const result = {};
218
+ for (const [name, declaration] of Object.entries(declarations ?? {})) {
219
+ result[name] = JSON.parse(canonicalJson(declaration));
220
+ }
221
+ return result;
222
+ }
223
+ export function createPaymentPayload(options) {
224
+ const { paymentRequired, accepted, schemePayload, paymentIdentifier } = options;
225
+ const advertised = paymentRequired.accepts.some((candidate) => canonicalJson(candidate) ===
226
+ canonicalJson(accepted));
227
+ if (!advertised)
228
+ throw new Error("accepted requirement was not advertised");
229
+ assertJsonValue(schemePayload);
230
+ const extensions = copiedExtensionInfo(paymentRequired.extensions);
231
+ const identifier = extensions["payment-identifier"];
232
+ if (identifier) {
233
+ if (!paymentIdentifier || !PAYMENT_IDENTIFIER.test(paymentIdentifier)) {
234
+ throw new Error("a valid buyer payment identifier is required");
235
+ }
236
+ identifier.info.id = paymentIdentifier;
237
+ }
238
+ else if (paymentIdentifier !== undefined) {
239
+ throw new Error("server did not declare the payment-identifier extension");
240
+ }
241
+ return {
242
+ x402Version: 2,
243
+ resource: paymentRequired.resource,
244
+ accepted,
245
+ payload: schemePayload,
246
+ extensions,
247
+ };
248
+ }
249
+ export function encodePaymentSignature(value) {
250
+ return encodeBase64Json(value, MAX_SIGNATURE_BYTES);
251
+ }
252
+ export function decodePaymentSignature(value) {
253
+ const parsed = decodeBase64Json(value, MAX_SIGNATURE_BYTES);
254
+ if (!isObject(parsed)) {
255
+ throw new Error("PAYMENT-SIGNATURE is not an object");
256
+ }
257
+ const allowed = new Set([
258
+ "x402Version",
259
+ "accepted",
260
+ "payload",
261
+ "extensions",
262
+ "resource",
263
+ ]);
264
+ if (Object.keys(parsed).some((key) => !allowed.has(key))) {
265
+ throw new Error("PAYMENT-SIGNATURE has unknown fields");
266
+ }
267
+ if (parsed.x402Version !== 2 ||
268
+ !isObject(parsed.payload) ||
269
+ !isObject(parsed.extensions)) {
270
+ throw new Error("PAYMENT-SIGNATURE is malformed");
271
+ }
272
+ assertJsonValue(parsed.payload);
273
+ return {
274
+ x402Version: 2,
275
+ accepted: requirement(parsed.accepted),
276
+ payload: parsed.payload,
277
+ extensions: paymentPayloadExtensions(parsed.extensions),
278
+ ...(parsed.resource === undefined
279
+ ? {}
280
+ : { resource: resource(parsed.resource) }),
281
+ };
282
+ }
283
+ //# sourceMappingURL=http.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.js","sourceRoot":"","sources":["../../src/protocol/http.ts"],"names":[],"mappings":"AA4CA,MAAM,gBAAgB,GAAG,EAAE,GAAG,IAAI,CAAC;AACnC,MAAM,mBAAmB,GAAG,GAAG,GAAG,IA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Aa;QACb,UAAU;QACV,SAAS;QACT,YAAY;QACZ,UAAU;KACX,CAAC,CAAC;IACH,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;QACzD,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;IAC1D,CAAC;IACD,IACE,MAAM,CAAC,WAAW,KAAK,CAAC;QACxB,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC;QACzB,CAAC,QAAQ,CAAC,MAAM,CAAC,UAAU,CAAC,EAC5B,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;IACpD,CAAC;IACD,eAAe,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAChC,OAAO;QACL,WAAW,EAAE,CAAC;QACd,QAAQ,EAAE,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC;QACtC,OAAO,EAAE,MAAM,CAAC,OAAqB;QACrC,UAAU,EAAE,wBAAwB,CAAC,MAAM,CAAC,UAAU,CAAC;QACvD,GAAG,CAAC,MAAM,CAAC,QAAQ,KAAK,SAAS;YAC/B,CAAC,CAAC,EAAE;YACJ,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC;KAC7C,CAAC;AACJ,CAAC"}
@@ -0,0 +1,147 @@
1
+ import { type PaymentPayload, type PaymentRequired, type PaymentRequirement } from "./http.js";
2
+ export declare const SOLANA_MAINNET_NETWORK = "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp";
3
+ export declare const SOLANA_USDC_MAINNET_MINT = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
4
+ export declare const SOLANA_USDT_MAINNET_MINT = "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB";
5
+ export declare const SOLANA_TOKEN_PROGRAM = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
6
+ export declare const SOLANA_ASSOCIATED_TOKEN_PROGRAM = "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL";
7
+ export declare const SOLANA_COMPUTE_BUDGET_PROGRAM = "ComputeBudget111111111111111111111111111111";
8
+ export declare const SOLANA_MEMO_PROGRAM = "MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr";
9
+ export declare const SOLANA_USDT_BUYER_FUNDED_PROFILE = "com.k1hub.x402.solana-buyer-funded.v1";
10
+ export interface SolanaRpc {
11
+ latestBlockhash(): Promise<string>;
12
+ }
13
+ export interface SolanaWalletTransport {
14
+ connect(network: typeof SOLANA_MAINNET_NETWORK): Promise<string>;
15
+ signTransaction(options: {
16
+ network: typeof SOLANA_MAINNET_NETWORK;
17
+ transactionBase64: string;
18
+ }): Promise<string>;
19
+ }
20
+ export interface SolanaWalletRpcProvider {
21
+ request(args: {
22
+ method: string;
23
+ params?: Record<string, unknown>;
24
+ }, chain?: string): Promise<unknown>;
25
+ }
26
+ export interface InjectedSolanaProvider {
27
+ connect(): Promise<{
28
+ publicKey: {
29
+ toString(): string;
30
+ };
31
+ }>;
32
+ signTransaction(transaction: RawSolanaVersionedTransaction): Promise<{
33
+ serialize(): Uint8Array;
34
+ }>;
35
+ }
36
+ export declare function decodeSolanaBase58(value: string): Uint8Array;
37
+ export declare function encodeSolanaBase58(value: Uint8Array): string;
38
+ export declare function solanaAssociatedTokenAddress(options: {
39
+ owner: string;
40
+ mint: string;
41
+ }): Promise<string>;
42
+ export declare class RawSolanaVersionedTransaction {
43
+ private readonly messageBytes;
44
+ readonly version = 0;
45
+ readonly message: {
46
+ version: 0;
47
+ serialize(): Uint8Array;
48
+ };
49
+ signatures: Uint8Array[];
50
+ constructor(messageBytes: Uint8Array, signatures?: Uint8Array[]);
51
+ serialize(): Uint8Array;
52
+ }
53
+ export declare class InjectedSolanaWalletTransport implements SolanaWalletTransport {
54
+ private readonly provider;
55
+ private payer;
56
+ constructor(provider: InjectedSolanaProvider);
57
+ connect(network: typeof SOLANA_MAINNET_NETWORK): Promise<string>;
58
+ signTransaction(options: {
59
+ network: typeof SOLANA_MAINNET_NETWORK;
60
+ transactionBase64: string;
61
+ }): Promise<string>;
62
+ }
63
+ /**
64
+ * Build the dormant tenant-sponsored Solana transaction profile.
65
+ *
66
+ * @deprecated V1 uses `buildBuyerFundedSolanaUsdtTransaction`. This helper is
67
+ * retained only for managed-wallet regression and the isolated payment lab.
68
+ */
69
+ export declare function buildSolanaUsdtTransaction(options: {
70
+ accepted: PaymentRequirement;
71
+ payer: string;
72
+ recentBlockhash: string;
73
+ computeUnitLimit?: number;
74
+ computeUnitPriceMicroLamports?: number;
75
+ }): Promise<{
76
+ message: Uint8Array;
77
+ transaction: Uint8Array;
78
+ }>;
79
+ /**
80
+ * Build an exact V1 buyer-funded issuer-native Solana stablecoin transaction.
81
+ *
82
+ * The connected buyer is both the SPL token authority and the only transaction
83
+ * signer/fee payer. The frozen v0 message contains exactly two compute-budget
84
+ * instructions, one `TransferChecked`, and the challenge memo.
85
+ */
86
+ export declare function buildBuyerFundedSolanaPayment(options: {
87
+ accepted: PaymentRequirement;
88
+ payer: string;
89
+ recentBlockhash: string;
90
+ computeUnitLimit?: number;
91
+ computeUnitPriceMicroLamports?: number;
92
+ }): Promise<{
93
+ message: Uint8Array;
94
+ transaction: Uint8Array;
95
+ }>;
96
+ /** @deprecated Use `buildBuyerFundedSolanaPayment`; retained for compatibility. */
97
+ export declare const buildBuyerFundedSolanaUsdtTransaction: typeof buildBuyerFundedSolanaPayment;
98
+ export declare class ConnectedSolanaWalletRpcTransport implements SolanaWalletTransport {
99
+ private readonly provider;
100
+ private readonly account;
101
+ constructor(provider: SolanaWalletRpcProvider, account: () => Promise<string>);
102
+ connect(network: typeof SOLANA_MAINNET_NETWORK): Promise<string>;
103
+ signTransaction(options: {
104
+ network: typeof SOLANA_MAINNET_NETWORK;
105
+ transactionBase64: string;
106
+ }): Promise<string>;
107
+ }
108
+ export declare class SolanaJsonRpc implements SolanaRpc {
109
+ private readonly endpoint;
110
+ constructor(endpoint: string);
111
+ latestBlockhash(): Promise<string>;
112
+ }
113
+ export declare function createSolanaPayment(options: {
114
+ rpc: SolanaRpc;
115
+ wallet: SolanaWalletTransport;
116
+ paymentRequired: PaymentRequired;
117
+ accepted: PaymentRequirement;
118
+ buyerPaymentIdentifier: string;
119
+ now?: Date;
120
+ }): Promise<PaymentPayload>;
121
+ export declare const createSolanaUsdcPayment: typeof createSolanaPayment;
122
+ export declare const createSolanaUsdtPayment: typeof createSolanaPayment;
123
+ export declare const createBuyerFundedSolanaPayment: typeof createSolanaPayment;
124
+ export declare const createBuyerFundedSolanaUsdtPayment: typeof createSolanaPayment;
125
+ export type SponsoredSolanaPaymentLabAdmission = {
126
+ kind: "tenant-sponsored-solana-payment-lab-v1";
127
+ payer: string;
128
+ recipient: string;
129
+ asset: typeof SOLANA_USDT_MAINNET_MINT;
130
+ amountAtomic: string;
131
+ resourceUrl: string;
132
+ expiresAt: string;
133
+ };
134
+ /**
135
+ * @deprecated Isolated tenant-sponsored payment-lab helper. It is not part of
136
+ * the external-wallet V1 product path.
137
+ */
138
+ export declare function createSolanaUsdtLabPayment(options: {
139
+ rpc: SolanaRpc;
140
+ wallet: SolanaWalletTransport;
141
+ paymentRequired: PaymentRequired;
142
+ accepted: PaymentRequirement;
143
+ buyerPaymentIdentifier: string;
144
+ admission: SponsoredSolanaPaymentLabAdmission;
145
+ now?: Date;
146
+ }): Promise<PaymentPayload>;
147
+ //# sourceMappingURL=solana.d.ts.map