@armory-sh/base 0.2.9 → 0.2.11

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/index.d.ts CHANGED
@@ -1,13 +1,14 @@
1
1
  export type { Address as X402Address, Hex, X402Version, Scheme, Network as X402Network, ExactEvmAuthorization, ExactEvmPayload, PaymentPayload as X402PaymentPayload, UnsignedPaymentPayload as X402UnsignedPaymentPayload, PaymentRequirements as X402PaymentRequirements, SettlementResponse as X402SettlementResponse, VerifyResponse, X402Response, PaymentPayloadV1 as X402PayloadV1, PaymentPayloadV2 as X402PayloadV2, LegacyPaymentPayload as X402LegacyPayload, } from "./types/x402";
2
2
  export { X402_VERSION, SCHEMES, isPaymentPayload, isExactEvmPayload, legacyToPaymentPayload, } from "./types/x402";
3
3
  export { safeBase64Encode, safeBase64Decode, encodePayment, decodePayment, encodeSettlementResponse, decodeSettlementResponse, encodeX402Response, decodeX402Response, X402_HEADERS, detectPaymentVersion, extractPaymentFromHeaders, createPaymentRequiredHeaders, createSettlementHeaders, isLegacyV1, isLegacyV2, } from "./encoding/x402";
4
- export { createNonce, toAtomicUnits, fromAtomicUnits, parseSignature, combineSignature, caip2ToNetwork, networkToCaip2, getCurrentTimestamp, isValidAddress, normalizeAddress, } from "./utils/x402";
5
- export type { PaymentPayloadV1, PaymentRequirementsV1, SettlementResponseV1, } from "./types/v1";
6
- export { V1_HEADERS, encodePaymentPayload, decodePaymentPayload, encodeSettlementResponse as encodeSettlementResponseV1, decodeSettlementResponse as decodeSettlementResponseV1, } from "./types/v1";
7
- export type { CAIP2ChainId, CAIPAssetId, Address, Signature, PayToV2, Extensions, PaymentPayloadV2, PaymentRequirementsV2, SettlementResponseV2, } from "./types/v2";
8
- export { V2_HEADERS, isCAIP2ChainId, isCAIPAssetId, isAddress, } from "./types/v2";
9
- export type { PaymentPayload, PaymentRequirements, SettlementResponse, } from "./types/protocol";
10
- export { isV1, isV2, getPaymentVersion, getRequirementsVersion, getSettlementVersion, getPaymentHeaderName, getPaymentResponseHeaderName, getPaymentRequiredHeaderName, isSettlementSuccessful, getTxHash, } from "./types/protocol";
4
+ export { createNonce, toAtomicUnits, fromAtomicUnits, caip2ToNetwork, networkToCaip2, getCurrentTimestamp, isValidAddress, normalizeAddress, } from "./utils/x402";
5
+ export type { PaymentPayloadV1, PaymentRequirementsV1, SettlementResponseV1, X402PaymentPayloadV1, X402PaymentRequiredV1, X402PaymentRequirementsV1, X402SettlementResponseV1, X402SchemePayloadV1, EIP3009AuthorizationV1, LegacyPaymentPayloadV1, LegacyPaymentRequirementsV1, LegacySettlementResponseV1, } from "./types/v1";
6
+ export { V1_HEADERS, encodeX402PaymentRequiredV1, decodeX402PaymentRequiredV1, encodeX402PaymentPayloadV1, decodeX402PaymentPayloadV1, encodeX402SettlementResponseV1, decodeX402SettlementResponseV1, isX402PaymentRequiredV1, isX402PaymentPayloadV1, isLegacyPaymentPayloadV1, encodePaymentPayloadLegacy, decodePaymentPayloadLegacy, encodeSettlementResponseLegacy, decodeSettlementResponseLegacy, } from "./types/v1";
7
+ export { encodePaymentPayloadLegacy as encodePaymentPayload, decodePaymentPayloadLegacy as decodePaymentPayload } from "./types/v1";
8
+ export type { CAIP2Network as CAIP2ChainId, CAIPAssetId, Address, Signature, PayToV2, Extensions, PaymentPayloadV2, PaymentRequirementsV2, SettlementResponseV2, PaymentRequiredV2, ResourceInfo, EIP3009Authorization, SchemePayloadV2, } from "./types/v2";
9
+ export { V2_HEADERS, isCAIP2ChainId, isCAIPAssetId, isAddress, isPaymentRequiredV2, isPaymentPayloadV2, assetIdToAddress, addressToAssetId, parseSignature as parseSignatureV2, combineSignature as combineSignatureV2, } from "./types/v2";
10
+ export type { PaymentPayload, PaymentRequirements, SettlementResponse, PaymentRequired, } from "./types/protocol";
11
+ export { isV1, isV2, isX402V1Payload, isX402V2Payload, isLegacyV1Payload, isX402V1Requirements, isX402V2Requirements, isX402V1Settlement, isX402V2Settlement, isX402V1PaymentRequired, isX402V2PaymentRequired, getPaymentVersion, getRequirementsVersion, getSettlementVersion, getPaymentRequiredVersion, getPaymentHeaderName, getPaymentResponseHeaderName, getPaymentRequiredHeaderName, isSettlementSuccessful, getTxHash, } from "./types/protocol";
11
12
  export type { NetworkConfig, CustomToken } from "./types/networks";
12
13
  export { NETWORKS, getNetworkConfig, getNetworkByChainId, getMainnets, getTestnets, registerToken, getCustomToken, getAllCustomTokens, unregisterToken, isCustomToken, } from "./types/networks";
13
14
  export type { TransferWithAuthorizationParams, ReceiveWithAuthorizationParams, BalanceOfParams, BalanceOfReturnType, NameReturnType, SymbolReturnType, ERC20Abi, } from "./abi/erc20";
package/dist/index.js CHANGED
@@ -239,19 +239,6 @@ function fromAtomicUnits(amount, decimals = 6) {
239
239
  }
240
240
  return `${whole}.${fractional}`;
241
241
  }
242
- function parseSignature(signature) {
243
- const hex = signature.startsWith("0x") ? signature.slice(2) : signature;
244
- const r = `0x${hex.slice(0, 64)}`;
245
- const s = `0x${hex.slice(64, 128)}`;
246
- const v = parseInt(hex.slice(128, 130) || "1c", 16);
247
- return { r, s, v };
248
- }
249
- function combineSignature(r, s, v) {
250
- const rHex = r.startsWith("0x") ? r.slice(2) : r;
251
- const sHex = s.startsWith("0x") ? s.slice(2) : s;
252
- const vHex = v.toString(16).padStart(2, "0");
253
- return `0x${rHex}${sHex}${vHex}`;
254
- }
255
242
  function caip2ToNetwork(caip2Id) {
256
243
  const match = caip2Id.match(/^eip155:(\d+)$/);
257
244
  if (!match) {
@@ -305,20 +292,48 @@ function normalizeAddress(address) {
305
292
  // src/types/v1.ts
306
293
  var V1_HEADERS = {
307
294
  PAYMENT: "X-PAYMENT",
308
- PAYMENT_RESPONSE: "X-PAYMENT-RESPONSE"
295
+ PAYMENT_RESPONSE: "X-PAYMENT-RESPONSE",
296
+ PAYMENT_REQUIRED: "X-PAYMENT-REQUIRED"
309
297
  };
310
- function encodePaymentPayload(payload) {
298
+ function encodeX402PaymentRequiredV1(data) {
299
+ return Buffer.from(JSON.stringify(data)).toString("base64");
300
+ }
301
+ function decodeX402PaymentRequiredV1(encoded) {
302
+ return JSON.parse(Buffer.from(encoded, "base64").toString("utf-8"));
303
+ }
304
+ function encodeX402PaymentPayloadV1(data) {
305
+ return Buffer.from(JSON.stringify(data)).toString("base64");
306
+ }
307
+ function decodeX402PaymentPayloadV1(encoded) {
308
+ return JSON.parse(Buffer.from(encoded, "base64").toString("utf-8"));
309
+ }
310
+ function encodeX402SettlementResponseV1(data) {
311
+ return Buffer.from(JSON.stringify(data)).toString("base64");
312
+ }
313
+ function decodeX402SettlementResponseV1(encoded) {
314
+ return JSON.parse(Buffer.from(encoded, "base64").toString("utf-8"));
315
+ }
316
+ function encodePaymentPayloadLegacy(payload) {
311
317
  return Buffer.from(JSON.stringify(payload)).toString("base64");
312
318
  }
313
- function decodePaymentPayload(encoded) {
319
+ function decodePaymentPayloadLegacy(encoded) {
314
320
  return JSON.parse(Buffer.from(encoded, "base64").toString("utf-8"));
315
321
  }
316
- function encodeSettlementResponse2(response) {
322
+ function encodeSettlementResponseLegacy(response) {
317
323
  return Buffer.from(JSON.stringify(response)).toString("base64");
318
324
  }
319
- function decodeSettlementResponse2(encoded) {
325
+ function decodeSettlementResponseLegacy(encoded) {
320
326
  return JSON.parse(Buffer.from(encoded, "base64").toString("utf-8"));
321
327
  }
328
+ function isX402PaymentRequiredV1(obj) {
329
+ return typeof obj === "object" && obj !== null && "x402Version" in obj && obj.x402Version === 1 && "accepts" in obj && Array.isArray(obj.accepts);
330
+ }
331
+ function isX402PaymentPayloadV1(obj) {
332
+ return typeof obj === "object" && obj !== null && "x402Version" in obj && obj.x402Version === 1 && "scheme" in obj && "network" in obj && "payload" in obj;
333
+ }
334
+ function isLegacyPaymentPayloadV1(obj) {
335
+ return typeof obj === "object" && obj !== null && "v" in obj && "r" in obj && "s" in obj && "chainId" in obj && "contractAddress" in obj;
336
+ }
322
337
 
323
338
  // src/types/v2.ts
324
339
  var V2_HEADERS = {
@@ -335,18 +350,116 @@ function isCAIPAssetId(value) {
335
350
  function isAddress(value) {
336
351
  return /^0x[a-fA-F0-9]{40}$/.test(value);
337
352
  }
353
+ function isPaymentRequiredV2(obj) {
354
+ return typeof obj === "object" && obj !== null && "x402Version" in obj && obj.x402Version === 2 && "resource" in obj && "accepts" in obj && Array.isArray(obj.accepts);
355
+ }
356
+ function isPaymentPayloadV2(obj) {
357
+ return typeof obj === "object" && obj !== null && "x402Version" in obj && obj.x402Version === 2 && "accepted" in obj && "payload" in obj;
358
+ }
359
+ function assetIdToAddress(assetId) {
360
+ const match = assetId.match(/\/erc20:(0x[a-fA-F0-9]{40})$/);
361
+ if (!match) throw new Error(`Invalid CAIP asset ID: ${assetId}`);
362
+ return match[1];
363
+ }
364
+ function addressToAssetId(address, chainId) {
365
+ const chain = typeof chainId === "number" ? `eip155:${chainId}` : chainId;
366
+ if (!isCAIP2ChainId(chain)) throw new Error(`Invalid chain ID: ${chain}`);
367
+ return `${chain}/erc20:${address}`;
368
+ }
369
+ function parseSignature(signature) {
370
+ const sig = signature.slice(2);
371
+ return {
372
+ r: `0x${sig.slice(0, 64)}`,
373
+ s: `0x${sig.slice(64, 128)}`,
374
+ v: parseInt(sig.slice(128, 130), 16)
375
+ };
376
+ }
377
+ function combineSignature(v, r, s) {
378
+ const rClean = r.startsWith("0x") ? r.slice(2) : r;
379
+ const sClean = s.startsWith("0x") ? s.slice(2) : s;
380
+ const vHex = v.toString(16).padStart(2, "0");
381
+ return `0x${rClean}${sClean}${vHex}`;
382
+ }
338
383
 
339
384
  // src/types/protocol.ts
340
- var isV1 = (payload) => "contractAddress" in payload && "network" in payload && "v" in payload;
341
- var isV2 = (payload) => "chainId" in payload && "assetId" in payload && "signature" in payload;
342
- var getPaymentVersion = (payload) => isV1(payload) ? 1 : 2;
343
- var getRequirementsVersion = (requirements) => "contractAddress" in requirements && "network" in requirements ? 1 : 2;
344
- var getSettlementVersion = (response) => "success" in response ? 1 : 2;
345
- var getPaymentHeaderName = (version) => version === 1 ? "X-PAYMENT" : "PAYMENT-SIGNATURE";
346
- var getPaymentResponseHeaderName = (version) => version === 1 ? "X-PAYMENT-RESPONSE" : "PAYMENT-RESPONSE";
347
- var getPaymentRequiredHeaderName = (version) => version === 1 ? "X-PAYMENT-REQUIRED" : "PAYMENT-REQUIRED";
348
- var isSettlementSuccessful = (response) => "success" in response ? response.success : response.status === "success";
349
- var getTxHash = (response) => response.txHash;
385
+ function isX402V1Payload(obj) {
386
+ return typeof obj === "object" && obj !== null && "x402Version" in obj && obj.x402Version === 1 && "scheme" in obj && "network" in obj && "payload" in obj;
387
+ }
388
+ function isX402V2Payload(obj) {
389
+ return typeof obj === "object" && obj !== null && "x402Version" in obj && obj.x402Version === 2 && "accepted" in obj && "payload" in obj;
390
+ }
391
+ function isLegacyV1Payload(obj) {
392
+ return typeof obj === "object" && obj !== null && "v" in obj && "r" in obj && "s" in obj && "chainId" in obj && "contractAddress" in obj && !("x402Version" in obj);
393
+ }
394
+ function isLegacyV2Payload(obj) {
395
+ if (typeof obj !== "object" || obj === null) return false;
396
+ const record = obj;
397
+ const signature = record.signature;
398
+ return "signature" in record && typeof signature === "object" && signature !== null && "v" in signature && "chainId" in record && typeof record.chainId === "string" && record.chainId.startsWith("eip155:") && "assetId" in record && !("x402Version" in record);
399
+ }
400
+ function getPaymentVersion(payload) {
401
+ if (isX402V1Payload(payload)) return 1;
402
+ if (isX402V2Payload(payload)) return 2;
403
+ return 1;
404
+ }
405
+ function isX402V1Requirements(obj) {
406
+ return typeof obj === "object" && obj !== null && "scheme" in obj && "network" in obj && typeof obj.network === "string" && !obj.network.includes(":") && // Not CAIP-2 format
407
+ "maxAmountRequired" in obj;
408
+ }
409
+ function isX402V2Requirements(obj) {
410
+ return typeof obj === "object" && obj !== null && "scheme" in obj && "network" in obj && typeof obj.network === "string" && obj.network.startsWith("eip155:") && // CAIP-2 format
411
+ "amount" in obj;
412
+ }
413
+ function getRequirementsVersion(requirements) {
414
+ if (isX402V1Requirements(requirements)) return 1;
415
+ if (isX402V2Requirements(requirements)) return 2;
416
+ return 1;
417
+ }
418
+ function isX402V1Settlement(obj) {
419
+ return typeof obj === "object" && obj !== null && "success" in obj && "network" in obj && typeof obj.network === "string" && !obj.network.includes(":");
420
+ }
421
+ function isX402V2Settlement(obj) {
422
+ return typeof obj === "object" && obj !== null && "success" in obj && "network" in obj && typeof obj.network === "string" && obj.network.startsWith("eip155:");
423
+ }
424
+ function getSettlementVersion(response) {
425
+ if (isX402V1Settlement(response)) return 1;
426
+ if (isX402V2Settlement(response)) return 2;
427
+ return 1;
428
+ }
429
+ function isX402V1PaymentRequired(obj) {
430
+ return typeof obj === "object" && obj !== null && "x402Version" in obj && obj.x402Version === 1 && "accepts" in obj;
431
+ }
432
+ function isX402V2PaymentRequired(obj) {
433
+ return typeof obj === "object" && obj !== null && "x402Version" in obj && obj.x402Version === 2 && "resource" in obj && "accepts" in obj;
434
+ }
435
+ function getPaymentRequiredVersion(obj) {
436
+ if (isX402V1PaymentRequired(obj)) return 1;
437
+ if (isX402V2PaymentRequired(obj)) return 2;
438
+ return 1;
439
+ }
440
+ function getPaymentHeaderName(version) {
441
+ return version === 1 ? "X-PAYMENT" : "PAYMENT-SIGNATURE";
442
+ }
443
+ function getPaymentResponseHeaderName(version) {
444
+ return version === 1 ? "X-PAYMENT-RESPONSE" : "PAYMENT-RESPONSE";
445
+ }
446
+ function getPaymentRequiredHeaderName(version) {
447
+ return version === 1 ? "X-PAYMENT-REQUIRED" : "PAYMENT-REQUIRED";
448
+ }
449
+ function isSettlementSuccessful(response) {
450
+ return "success" in response ? response.success : false;
451
+ }
452
+ function getTxHash(response) {
453
+ if ("transaction" in response) return response.transaction;
454
+ if ("txHash" in response) return response.txHash;
455
+ return void 0;
456
+ }
457
+ function isV1(obj) {
458
+ return isX402V1Payload(obj) || isLegacyV1Payload(obj);
459
+ }
460
+ function isV2(obj) {
461
+ return isX402V2Payload(obj) || isLegacyV2Payload(obj);
462
+ }
350
463
 
351
464
  // src/types/networks.ts
352
465
  var tokenRegistry = /* @__PURE__ */ new Map();
@@ -978,9 +1091,11 @@ export {
978
1091
  V2_HEADERS,
979
1092
  X402_HEADERS,
980
1093
  X402_VERSION,
1094
+ addressToAssetId,
1095
+ assetIdToAddress,
981
1096
  caip2ToNetwork,
982
1097
  checkFacilitatorSupport,
983
- combineSignature,
1098
+ combineSignature as combineSignatureV2,
984
1099
  createEIP712Domain,
985
1100
  createError,
986
1101
  createNonce,
@@ -989,26 +1104,34 @@ export {
989
1104
  createTransferWithAuthorization,
990
1105
  decodePayment,
991
1106
  decodePayment2 as decodePaymentLegacy,
992
- decodePaymentPayload,
1107
+ decodePaymentPayloadLegacy as decodePaymentPayload,
1108
+ decodePaymentPayloadLegacy,
993
1109
  decodePaymentV1,
994
1110
  decodePaymentV2,
995
1111
  decodeSettlement as decodeSettlementLegacy,
996
1112
  decodeSettlementResponse,
997
- decodeSettlementResponse2 as decodeSettlementResponseV1,
1113
+ decodeSettlementResponseLegacy,
998
1114
  decodeSettlementV1,
999
1115
  decodeSettlementV2,
1116
+ decodeX402PaymentPayloadV1,
1117
+ decodeX402PaymentRequiredV1,
1000
1118
  decodeX402Response,
1119
+ decodeX402SettlementResponseV1,
1001
1120
  detectPaymentVersion,
1002
1121
  detectPaymentVersion2 as detectPaymentVersionLegacy,
1003
1122
  encodePayment,
1004
- encodePaymentPayload,
1123
+ encodePaymentPayloadLegacy as encodePaymentPayload,
1124
+ encodePaymentPayloadLegacy,
1005
1125
  encodePaymentV1,
1006
1126
  encodePaymentV2,
1007
1127
  encodeSettlementResponse,
1008
- encodeSettlementResponse2 as encodeSettlementResponseV1,
1128
+ encodeSettlementResponseLegacy,
1009
1129
  encodeSettlementV1,
1010
1130
  encodeSettlementV2,
1131
+ encodeX402PaymentPayloadV1,
1132
+ encodeX402PaymentRequiredV1,
1011
1133
  encodeX402Response,
1134
+ encodeX402SettlementResponseV1,
1012
1135
  extractPaymentFromHeaders,
1013
1136
  fromAtomicUnits,
1014
1137
  getAllCustomTokens,
@@ -1021,6 +1144,7 @@ export {
1021
1144
  getNetworkConfig,
1022
1145
  getPaymentHeaderName,
1023
1146
  getPaymentRequiredHeaderName,
1147
+ getPaymentRequiredVersion,
1024
1148
  getPaymentResponseHeaderName,
1025
1149
  getPaymentVersion,
1026
1150
  getRequirementsVersion,
@@ -1032,9 +1156,13 @@ export {
1032
1156
  isCAIPAssetId,
1033
1157
  isCustomToken,
1034
1158
  isExactEvmPayload,
1159
+ isLegacyPaymentPayloadV1,
1035
1160
  isLegacyV1,
1161
+ isLegacyV1Payload,
1036
1162
  isLegacyV2,
1037
1163
  isPaymentPayload,
1164
+ isPaymentPayloadV2,
1165
+ isPaymentRequiredV2,
1038
1166
  isPaymentV1,
1039
1167
  isPaymentV2,
1040
1168
  isResolvedNetwork,
@@ -1046,11 +1174,21 @@ export {
1046
1174
  isV2,
1047
1175
  isValidAddress,
1048
1176
  isValidationError,
1177
+ isX402PaymentPayloadV1,
1178
+ isX402PaymentRequiredV1,
1179
+ isX402V1Payload,
1180
+ isX402V1PaymentRequired,
1181
+ isX402V1Requirements,
1182
+ isX402V1Settlement,
1183
+ isX402V2Payload,
1184
+ isX402V2PaymentRequired,
1185
+ isX402V2Requirements,
1186
+ isX402V2Settlement,
1049
1187
  legacyToPaymentPayload,
1050
1188
  networkToCaip2,
1051
1189
  normalizeAddress,
1052
1190
  normalizeNetworkName,
1053
- parseSignature,
1191
+ parseSignature as parseSignatureV2,
1054
1192
  registerToken,
1055
1193
  resolveFacilitator,
1056
1194
  resolveNetwork,
@@ -1,15 +1,107 @@
1
- import type { PaymentPayloadV1, PaymentRequirementsV1, SettlementResponseV1 } from "./v1";
2
- import type { PaymentPayloadV2, PaymentRequirementsV2, SettlementResponseV2 } from "./v2";
3
- export type PaymentPayload = PaymentPayloadV1 | PaymentPayloadV2;
4
- export type PaymentRequirements = PaymentRequirementsV1 | PaymentRequirementsV2;
5
- export type SettlementResponse = SettlementResponseV1 | SettlementResponseV2;
6
- export declare const isV1: (payload: PaymentPayload) => payload is PaymentPayloadV1;
7
- export declare const isV2: (payload: PaymentPayload) => payload is PaymentPayloadV2;
8
- export declare const getPaymentVersion: (payload: PaymentPayload) => 1 | 2;
9
- export declare const getRequirementsVersion: (requirements: PaymentRequirements) => 1 | 2;
10
- export declare const getSettlementVersion: (response: SettlementResponse) => 1 | 2;
11
- export declare const getPaymentHeaderName: (version: 1 | 2) => string;
12
- export declare const getPaymentResponseHeaderName: (version: 1 | 2) => string;
13
- export declare const getPaymentRequiredHeaderName: (version: 1 | 2) => string;
14
- export declare const isSettlementSuccessful: (response: SettlementResponse) => boolean;
15
- export declare const getTxHash: (response: SettlementResponse) => string | undefined;
1
+ /**
2
+ * Protocol Union Types and Version Detection
3
+ *
4
+ * Handles both x402 V1 and V2 formats along with legacy Armory formats
5
+ */
6
+ import type { X402PaymentPayloadV1, X402PaymentRequiredV1, X402PaymentRequirementsV1, X402SettlementResponseV1, LegacyPaymentPayloadV1, LegacyPaymentRequirementsV1, LegacySettlementResponseV1 } from "./v1";
7
+ import type { PaymentPayloadV2, PaymentRequirementsV2, SettlementResponseV2, PaymentRequiredV2 } from "./v2";
8
+ /**
9
+ * All x402 compatible payment payload types
10
+ */
11
+ export type PaymentPayload = X402PaymentPayloadV1 | PaymentPayloadV2 | LegacyPaymentPayloadV1;
12
+ /**
13
+ * All x402 compatible payment requirements types
14
+ */
15
+ export type PaymentRequirements = X402PaymentRequirementsV1 | PaymentRequirementsV2 | LegacyPaymentRequirementsV1;
16
+ /**
17
+ * All x402 compatible settlement response types
18
+ */
19
+ export type SettlementResponse = X402SettlementResponseV1 | SettlementResponseV2 | LegacySettlementResponseV1;
20
+ /**
21
+ * All x402 compatible payment required response types
22
+ */
23
+ export type PaymentRequired = X402PaymentRequiredV1 | PaymentRequiredV2;
24
+ /**
25
+ * Check if payload is x402 V1 format
26
+ */
27
+ export declare function isX402V1Payload(obj: unknown): obj is X402PaymentPayloadV1;
28
+ /**
29
+ * Check if payload is x402 V2 format
30
+ */
31
+ export declare function isX402V2Payload(obj: unknown): obj is PaymentPayloadV2;
32
+ /**
33
+ * Check if payload is legacy Armory V1 format
34
+ */
35
+ export declare function isLegacyV1Payload(obj: unknown): obj is LegacyPaymentPayloadV1;
36
+ /**
37
+ * Check if payload is legacy Armory V2 format
38
+ */
39
+ export declare function isLegacyV2Payload(obj: unknown): boolean;
40
+ /**
41
+ * Get x402 version from payload
42
+ */
43
+ export declare function getPaymentVersion(payload: PaymentPayload): 1 | 2;
44
+ /**
45
+ * Check if requirements is x402 V1 format
46
+ */
47
+ export declare function isX402V1Requirements(obj: unknown): obj is X402PaymentRequirementsV1;
48
+ /**
49
+ * Check if requirements is x402 V2 format
50
+ */
51
+ export declare function isX402V2Requirements(obj: unknown): obj is PaymentRequirementsV2;
52
+ /**
53
+ * Get x402 version from requirements
54
+ */
55
+ export declare function getRequirementsVersion(requirements: PaymentRequirements): 1 | 2;
56
+ /**
57
+ * Check if settlement response is x402 V1 format
58
+ */
59
+ export declare function isX402V1Settlement(obj: unknown): obj is X402SettlementResponseV1;
60
+ /**
61
+ * Check if settlement response is x402 V2 format
62
+ */
63
+ export declare function isX402V2Settlement(obj: unknown): obj is SettlementResponseV2;
64
+ /**
65
+ * Get x402 version from settlement response
66
+ */
67
+ export declare function getSettlementVersion(response: SettlementResponse): 1 | 2;
68
+ /**
69
+ * Check if payment required is x402 V1 format
70
+ */
71
+ export declare function isX402V1PaymentRequired(obj: unknown): obj is X402PaymentRequiredV1;
72
+ /**
73
+ * Check if payment required is x402 V2 format
74
+ */
75
+ export declare function isX402V2PaymentRequired(obj: unknown): obj is PaymentRequiredV2;
76
+ /**
77
+ * Get x402 version from payment required
78
+ */
79
+ export declare function getPaymentRequiredVersion(obj: PaymentRequired): 1 | 2;
80
+ /**
81
+ * Get payment header name for version
82
+ */
83
+ export declare function getPaymentHeaderName(version: 1 | 2): string;
84
+ /**
85
+ * Get payment response header name for version
86
+ */
87
+ export declare function getPaymentResponseHeaderName(version: 1 | 2): string;
88
+ /**
89
+ * Get payment required header name for version
90
+ */
91
+ export declare function getPaymentRequiredHeaderName(version: 1 | 2): string;
92
+ /**
93
+ * Check if settlement was successful
94
+ */
95
+ export declare function isSettlementSuccessful(response: SettlementResponse): boolean;
96
+ /**
97
+ * Get transaction hash from settlement response
98
+ */
99
+ export declare function getTxHash(response: SettlementResponse): string | undefined;
100
+ /**
101
+ * @deprecated Use isX402V1Payload or isLegacyV1Payload instead
102
+ */
103
+ export declare function isV1(obj: unknown): boolean;
104
+ /**
105
+ * @deprecated Use isX402V2Payload or isLegacyV2Payload instead
106
+ */
107
+ export declare function isV2(obj: unknown): boolean;
@@ -1,4 +1,84 @@
1
- export interface PaymentPayloadV1 {
1
+ /**
2
+ * Armory V1 Types - x402 Protocol V1 Compatible
3
+ *
4
+ * Supports both the x402 V1 specification format and legacy Armory V1 format.
5
+ *
6
+ * x402 V1 Specification: https://github.com/coinbase/x402
7
+ */
8
+ import type { Address } from "./v2";
9
+ /**
10
+ * Network name for x402 V1 (e.g., "base-sepolia", "ethereum-mainnet")
11
+ */
12
+ export type X402V1Network = string;
13
+ /**
14
+ * EIP-3009 authorization data (same format for V1 and V2)
15
+ */
16
+ export interface EIP3009AuthorizationV1 {
17
+ from: Address;
18
+ to: Address;
19
+ value: string;
20
+ validAfter: string;
21
+ validBefore: string;
22
+ nonce: `0x${string}`;
23
+ }
24
+ /**
25
+ * Payment requirements for a specific payment method (x402 V1)
26
+ */
27
+ export interface X402PaymentRequirementsV1 {
28
+ scheme: "exact";
29
+ network: X402V1Network;
30
+ maxAmountRequired: string;
31
+ asset: Address;
32
+ payTo: Address;
33
+ resource: string;
34
+ description: string;
35
+ mimeType?: string;
36
+ outputSchema?: object | null;
37
+ maxTimeoutSeconds: number;
38
+ extra?: Record<string, unknown>;
39
+ }
40
+ /**
41
+ * Payment requirements response (x402 V1)
42
+ * Matches x402 V1 PaymentRequirementsResponse spec
43
+ */
44
+ export interface X402PaymentRequiredV1 {
45
+ x402Version: 1;
46
+ error: string;
47
+ accepts: X402PaymentRequirementsV1[];
48
+ }
49
+ /**
50
+ * Scheme payload data (x402 V1)
51
+ */
52
+ export interface X402SchemePayloadV1 {
53
+ signature: `0x${string}`;
54
+ authorization: EIP3009AuthorizationV1;
55
+ }
56
+ /**
57
+ * Payment payload (x402 V1)
58
+ * Matches x402 V1 PaymentPayload spec
59
+ */
60
+ export interface X402PaymentPayloadV1 {
61
+ x402Version: 1;
62
+ scheme: "exact";
63
+ network: X402V1Network;
64
+ payload: X402SchemePayloadV1;
65
+ }
66
+ /**
67
+ * Settlement response (x402 V1)
68
+ * Matches x402 V1 SettlementResponse spec
69
+ */
70
+ export interface X402SettlementResponseV1 {
71
+ success: boolean;
72
+ errorReason?: string;
73
+ transaction: string;
74
+ network: X402V1Network;
75
+ payer: Address;
76
+ }
77
+ /**
78
+ * Legacy Armory V1 payment payload format
79
+ * @deprecated Use X402PaymentPayloadV1 for new code
80
+ */
81
+ export interface LegacyPaymentPayloadV1 {
2
82
  from: string;
3
83
  to: string;
4
84
  amount: string;
@@ -11,24 +91,66 @@ export interface PaymentPayloadV1 {
11
91
  contractAddress: string;
12
92
  network: string;
13
93
  }
14
- export interface PaymentRequirementsV1 {
94
+ /**
95
+ * Legacy Armory V1 payment requirements format
96
+ * @deprecated Use X402PaymentRequirementsV1 for new code
97
+ */
98
+ export interface LegacyPaymentRequirementsV1 {
15
99
  amount: string;
16
100
  network: string;
17
101
  contractAddress: string;
18
102
  payTo: string;
19
103
  expiry: number;
20
104
  }
21
- export interface SettlementResponseV1 {
105
+ /**
106
+ * Legacy Armory V1 settlement response format
107
+ * @deprecated Use X402SettlementResponseV1 for new code
108
+ */
109
+ export interface LegacySettlementResponseV1 {
22
110
  success: boolean;
23
111
  txHash?: string;
24
112
  error?: string;
25
113
  timestamp: number;
26
114
  }
115
+ export type PaymentPayloadV1 = X402PaymentPayloadV1 | LegacyPaymentPayloadV1;
116
+ export type PaymentRequirementsV1 = X402PaymentRequirementsV1 | LegacyPaymentRequirementsV1;
117
+ export type SettlementResponseV1 = X402SettlementResponseV1 | LegacySettlementResponseV1;
27
118
  export declare const V1_HEADERS: {
28
119
  readonly PAYMENT: "X-PAYMENT";
29
120
  readonly PAYMENT_RESPONSE: "X-PAYMENT-RESPONSE";
121
+ readonly PAYMENT_REQUIRED: "X-PAYMENT-REQUIRED";
30
122
  };
31
- export declare function encodePaymentPayload(payload: PaymentPayloadV1): string;
32
- export declare function decodePaymentPayload(encoded: string): PaymentPayloadV1;
33
- export declare function encodeSettlementResponse(response: SettlementResponseV1): string;
34
- export declare function decodeSettlementResponse(encoded: string): SettlementResponseV1;
123
+ export declare function encodeX402PaymentRequiredV1(data: X402PaymentRequiredV1): string;
124
+ export declare function decodeX402PaymentRequiredV1(encoded: string): X402PaymentRequiredV1;
125
+ export declare function encodeX402PaymentPayloadV1(data: X402PaymentPayloadV1): string;
126
+ export declare function decodeX402PaymentPayloadV1(encoded: string): X402PaymentPayloadV1;
127
+ export declare function encodeX402SettlementResponseV1(data: X402SettlementResponseV1): string;
128
+ export declare function decodeX402SettlementResponseV1(encoded: string): X402SettlementResponseV1;
129
+ /**
130
+ * @deprecated Use encodeX402PaymentPayloadV1 instead
131
+ */
132
+ export declare function encodePaymentPayloadLegacy(payload: LegacyPaymentPayloadV1): string;
133
+ /**
134
+ * @deprecated Use decodeX402PaymentPayloadV1 instead
135
+ */
136
+ export declare function decodePaymentPayloadLegacy(encoded: string): LegacyPaymentPayloadV1;
137
+ /**
138
+ * @deprecated Use encodeX402SettlementResponseV1 instead
139
+ */
140
+ export declare function encodeSettlementResponseLegacy(response: LegacySettlementResponseV1): string;
141
+ /**
142
+ * @deprecated Use decodeX402SettlementResponseV1 instead
143
+ */
144
+ export declare function decodeSettlementResponseLegacy(encoded: string): LegacySettlementResponseV1;
145
+ /**
146
+ * Check if value is x402 V1 PaymentRequired
147
+ */
148
+ export declare function isX402PaymentRequiredV1(obj: unknown): obj is X402PaymentRequiredV1;
149
+ /**
150
+ * Check if value is x402 V1 PaymentPayload
151
+ */
152
+ export declare function isX402PaymentPayloadV1(obj: unknown): obj is X402PaymentPayloadV1;
153
+ /**
154
+ * Check if value is legacy Armory V1 format
155
+ */
156
+ export declare function isLegacyPaymentPayloadV1(obj: unknown): obj is LegacyPaymentPayloadV1;
@@ -1,4 +1,12 @@
1
- export type CAIP2ChainId = `eip155:${string}`;
1
+ /**
2
+ * Armory V2 Types - x402 Protocol V2 Compatible
3
+ *
4
+ * These types match the Coinbase x402 V2 specification for full interoperability.
5
+ * No conversion needed - this is the native format.
6
+ *
7
+ * Specification: https://github.com/coinbase/x402
8
+ */
9
+ export type CAIP2Network = `eip155:${string}`;
2
10
  export type CAIPAssetId = `eip155:${string}/erc20:${string}`;
3
11
  export type Address = `0x${string}`;
4
12
  export interface Signature {
@@ -13,31 +21,112 @@ export type PayToV2 = Address | {
13
21
  export interface Extensions {
14
22
  [key: string]: unknown;
15
23
  }
16
- export interface PaymentPayloadV2 {
17
- from: Address;
18
- to: PayToV2;
19
- amount: string;
20
- nonce: string;
21
- expiry: number;
22
- signature: Signature;
23
- chainId: CAIP2ChainId;
24
- assetId: CAIPAssetId;
25
- extensions?: Extensions;
24
+ /**
25
+ * Resource information describing the protected resource
26
+ */
27
+ export interface ResourceInfo {
28
+ /** URL of the protected resource */
29
+ url: string;
30
+ /** Human-readable description of the resource */
31
+ description?: string;
32
+ /** MIME type of the expected response */
33
+ mimeType?: string;
26
34
  }
35
+ /**
36
+ * Payment requirements for a specific payment method
37
+ * Matches x402 V2 PaymentRequirements spec
38
+ */
27
39
  export interface PaymentRequirementsV2 {
40
+ /** Payment scheme identifier (e.g., "exact") */
41
+ scheme: "exact";
42
+ /** Blockchain network identifier in CAIP-2 format */
43
+ network: CAIP2Network;
44
+ /** Required payment amount in atomic token units */
28
45
  amount: string;
29
- to: PayToV2;
30
- chainId: CAIP2ChainId;
31
- assetId: CAIPAssetId;
32
- nonce: string;
33
- expiry: number;
46
+ /** Token contract address (extracted from CAIP asset ID) */
47
+ asset: Address;
48
+ /** Recipient wallet address or role constant */
49
+ payTo: Address;
50
+ /** Maximum time allowed for payment completion */
51
+ maxTimeoutSeconds: number;
52
+ /** Scheme-specific additional information (token name, version, etc.) */
53
+ extra?: Extensions;
54
+ }
55
+ /**
56
+ * Payment required response (PAYMENT-REQUIRED header)
57
+ * Matches x402 V2 PaymentRequired spec
58
+ */
59
+ export interface PaymentRequiredV2 {
60
+ /** Protocol version identifier (must be 2) */
61
+ x402Version: 2;
62
+ /** Human-readable error message */
63
+ error?: string;
64
+ /** Resource being accessed */
65
+ resource: ResourceInfo;
66
+ /** Array of acceptable payment methods */
67
+ accepts: PaymentRequirementsV2[];
68
+ /** Protocol extensions data */
69
+ extensions?: Extensions;
70
+ }
71
+ /**
72
+ * EIP-3009 TransferWithAuthorization authorization data
73
+ * Matches x402 V2 spec for exact scheme
74
+ */
75
+ export interface EIP3009Authorization {
76
+ /** Payer's wallet address */
77
+ from: Address;
78
+ /** Recipient's wallet address */
79
+ to: Address;
80
+ /** Payment amount in atomic units */
81
+ value: string;
82
+ /** Unix timestamp when authorization becomes valid */
83
+ validAfter: string;
84
+ /** Unix timestamp when authorization expires */
85
+ validBefore: string;
86
+ /** 32-byte random nonce (bytes32 hex string) */
87
+ nonce: `0x${string}`;
88
+ }
89
+ /**
90
+ * Scheme-specific payment payload data
91
+ */
92
+ export interface SchemePayloadV2 {
93
+ /** Signature for the authorization (0x-prefixed 65-byte hex) */
94
+ signature: `0x${string}`;
95
+ /** EIP-3009 authorization */
96
+ authorization: EIP3009Authorization;
97
+ }
98
+ /**
99
+ * Payment payload sent by client
100
+ * Matches x402 V2 PaymentPayload spec
101
+ */
102
+ export interface PaymentPayloadV2 {
103
+ /** Protocol version identifier */
104
+ x402Version: 2;
105
+ /** Resource being accessed (echoed from server) */
106
+ resource?: ResourceInfo;
107
+ /** Selected payment requirement from server's accepts array */
108
+ accepted: PaymentRequirementsV2;
109
+ /** Scheme-specific payment data */
110
+ payload: SchemePayloadV2;
111
+ /** Protocol extensions data */
34
112
  extensions?: Extensions;
35
113
  }
114
+ /**
115
+ * Settlement response after payment settlement
116
+ * Matches x402 V2 SettlementResponse spec
117
+ */
36
118
  export interface SettlementResponseV2 {
37
- status: "success" | "pending" | "failed";
38
- txHash?: string;
39
- txId?: string;
40
- timestamp: number;
119
+ /** Whether settlement was successful */
120
+ success: boolean;
121
+ /** Error reason if settlement failed */
122
+ errorReason?: string;
123
+ /** Address of the payer's wallet */
124
+ payer?: Address;
125
+ /** Blockchain transaction hash (empty if failed) */
126
+ transaction: string;
127
+ /** Blockchain network identifier in CAIP-2 format */
128
+ network: CAIP2Network;
129
+ /** Protocol extensions data */
41
130
  extensions?: Extensions;
42
131
  }
43
132
  export declare const V2_HEADERS: {
@@ -45,6 +134,24 @@ export declare const V2_HEADERS: {
45
134
  readonly PAYMENT_REQUIRED: "PAYMENT-REQUIRED";
46
135
  readonly PAYMENT_RESPONSE: "PAYMENT-RESPONSE";
47
136
  };
48
- export declare function isCAIP2ChainId(value: string): value is CAIP2ChainId;
137
+ export declare function isCAIP2ChainId(value: string): value is CAIP2Network;
49
138
  export declare function isCAIPAssetId(value: string): value is CAIPAssetId;
50
139
  export declare function isAddress(value: string): value is Address;
140
+ export declare function isPaymentRequiredV2(obj: unknown): obj is PaymentRequiredV2;
141
+ export declare function isPaymentPayloadV2(obj: unknown): obj is PaymentPayloadV2;
142
+ /**
143
+ * Extract token address from CAIP asset ID
144
+ */
145
+ export declare function assetIdToAddress(assetId: CAIPAssetId): Address;
146
+ /**
147
+ * Create CAIP asset ID from token address and chain ID
148
+ */
149
+ export declare function addressToAssetId(address: Address, chainId: string | number): CAIPAssetId;
150
+ /**
151
+ * Parse combined signature (0x + r + s + v) into components
152
+ */
153
+ export declare function parseSignature(signature: `0x${string}`): Signature;
154
+ /**
155
+ * Combine signature components into 65-byte hex string
156
+ */
157
+ export declare function combineSignature(v: number, r: string, s: string): `0x${string}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@armory-sh/base",
3
- "version": "0.2.9",
3
+ "version": "0.2.11",
4
4
  "license": "MIT",
5
5
  "author": "Sawyer Cutler <sawyer@dirtroad.dev>",
6
6
  "type": "module",