@xona-labs/xpay 0.2.14 → 0.2.16

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 (44) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +29 -1
  3. package/dist/cli/index.js +22 -0
  4. package/dist/cli/index.js.map +1 -1
  5. package/dist/cli/trade.d.ts +22 -0
  6. package/dist/cli/trade.d.ts.map +1 -0
  7. package/dist/cli/trade.js +148 -0
  8. package/dist/cli/trade.js.map +1 -0
  9. package/dist/index.d.ts +32 -0
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +8 -0
  12. package/dist/index.js.map +1 -1
  13. package/dist/profile/index.d.ts.map +1 -1
  14. package/dist/profile/index.js +11 -1
  15. package/dist/profile/index.js.map +1 -1
  16. package/dist/profile/types.d.ts +8 -0
  17. package/dist/profile/types.d.ts.map +1 -1
  18. package/dist/signers/raw-evm.d.ts.map +1 -1
  19. package/dist/signers/raw-evm.js +15 -0
  20. package/dist/signers/raw-evm.js.map +1 -1
  21. package/dist/tools/index.d.ts.map +1 -1
  22. package/dist/tools/index.js +74 -0
  23. package/dist/tools/index.js.map +1 -1
  24. package/dist/trading/discovery.d.ts +42 -0
  25. package/dist/trading/discovery.d.ts.map +1 -0
  26. package/dist/trading/discovery.js +109 -0
  27. package/dist/trading/discovery.js.map +1 -0
  28. package/dist/trading/index.d.ts +93 -0
  29. package/dist/trading/index.d.ts.map +1 -0
  30. package/dist/trading/index.js +348 -0
  31. package/dist/trading/index.js.map +1 -0
  32. package/dist/types.d.ts +23 -0
  33. package/dist/types.d.ts.map +1 -1
  34. package/dist/use/index.d.ts.map +1 -1
  35. package/dist/use/index.js +37 -10
  36. package/dist/use/index.js.map +1 -1
  37. package/dist/wallet/index.d.ts.map +1 -1
  38. package/dist/wallet/index.js +2 -0
  39. package/dist/wallet/index.js.map +1 -1
  40. package/dist/x402/evm-payment.d.ts +51 -0
  41. package/dist/x402/evm-payment.d.ts.map +1 -0
  42. package/dist/x402/evm-payment.js +121 -0
  43. package/dist/x402/evm-payment.js.map +1 -0
  44. package/package.json +1 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"evm-payment.d.ts","sourceRoot":"","sources":["../../src/x402/evm-payment.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAuBtD,MAAM,WAAW,mBAAmB;IAClC,uDAAuD;IACvD,OAAO,EAAE,MAAM,CAAC;IAChB,kEAAkE;IAClE,aAAa,EAAE,CAAC,SAAS,EAAE;QACzB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAChC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,IAAI,EAAE,MAAM,CAAA;SAAE,CAAC,CAAC,CAAC;QAC7D,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;KAClC,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IACtB,4CAA4C;IAC5C,WAAW,EAAE,kBAAkB,CAAC;IAChC,6DAA6D;IAC7D,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CASlD;AAED,0FAA0F;AAC1F,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAErD;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,kBAAkB,GAAG,OAAO,CAEnE;AAED;;;;GAIG;AACH,wBAAsB,qBAAqB,CAAC,IAAI,EAAE,mBAAmB,GAAG,OAAO,CAAC,MAAM,CAAC,CA8DtF"}
@@ -0,0 +1,121 @@
1
+ /**
2
+ * Build the canonical x402 EVM v2 `X-Payment` header value (gasless).
3
+ *
4
+ * Spec flow (`exact` scheme on eip155 networks):
5
+ * client signs an EIP-3009 `TransferWithAuthorization` typed-data payload
6
+ * (an off-chain signature — USDC supports it natively) → header carries
7
+ * the authorization + signature → facilitator submits the transfer
8
+ * on-chain and pays the gas.
9
+ *
10
+ * The payer wallet therefore needs ZERO native ETH: this replaces the legacy
11
+ * `signer.pay()` path, which broadcast a plain `erc20.transfer()` from the
12
+ * wallet and failed with "insufficient funds for intrinsic transaction cost"
13
+ * on gasless agent wallets.
14
+ *
15
+ * Payload shape mirrors `@x402/evm`'s `exact` client scheme, which is what
16
+ * `ExactEvmScheme` servers + facilitators (PayAI, CDP) verify against.
17
+ */
18
+ /** EIP-712 types for EIP-3009 transferWithAuthorization. */
19
+ const AUTHORIZATION_TYPES = {
20
+ TransferWithAuthorization: [
21
+ { name: "from", type: "address" },
22
+ { name: "to", type: "address" },
23
+ { name: "value", type: "uint256" },
24
+ { name: "validAfter", type: "uint256" },
25
+ { name: "validBefore", type: "uint256" },
26
+ { name: "nonce", type: "bytes32" },
27
+ ],
28
+ };
29
+ /** Chain ids for the named networks xpay profiles use. */
30
+ const NAMED_CHAIN_IDS = {
31
+ base: 8453,
32
+ ethereum: 1,
33
+ arbitrum: 42161,
34
+ optimism: 10,
35
+ robinhood: 4663,
36
+ };
37
+ export function evmChainId(network) {
38
+ if (network.startsWith("eip155:")) {
39
+ const id = Number(network.slice("eip155:".length));
40
+ if (Number.isInteger(id) && id > 0)
41
+ return id;
42
+ throw new Error(`buildEvmPaymentHeader: bad eip155 network "${network}"`);
43
+ }
44
+ const id = NAMED_CHAIN_IDS[network];
45
+ if (!id)
46
+ throw new Error(`buildEvmPaymentHeader: unknown EVM network "${network}"`);
47
+ return id;
48
+ }
49
+ /** True for networks paid via the EVM x402 path (eip155 CAIP ids or named EVM chains). */
50
+ export function isEvmNetwork(network) {
51
+ return network.startsWith("eip155:") || network in NAMED_CHAIN_IDS;
52
+ }
53
+ /**
54
+ * True when this requirement carries the EIP-712 domain params (`extra.name`
55
+ * + `extra.version`) the gasless signature needs. Catalog snapshots usually
56
+ * strip `extra` — a fresh 402 challenge always has it.
57
+ */
58
+ export function hasEvmDomainParams(req) {
59
+ return Boolean(req.extra?.name && req.extra?.version);
60
+ }
61
+ /**
62
+ * Returns the value of the `X-Payment` header (base64 of canonical JSON
63
+ * envelope), ready to attach to the retry request. Nothing is broadcast —
64
+ * the facilitator settles server-side.
65
+ */
66
+ export async function buildEvmPaymentHeader(args) {
67
+ const req = args.requirement;
68
+ // The EIP-3009 payload shape only exists in x402 v2 — never emit a v1
69
+ // envelope here (v1 catalog entries without a version reach us as 1).
70
+ const version = Math.max(args.x402Version ?? 2, 2);
71
+ if (!hasEvmDomainParams(req)) {
72
+ throw new Error(`buildEvmPaymentHeader: EIP-712 domain params (extra.name/extra.version) missing for asset ${req.asset} — re-fetch the live 402 challenge`);
73
+ }
74
+ const now = Math.floor(Date.now() / 1000);
75
+ const nonceBytes = globalThis.crypto.getRandomValues(new Uint8Array(32));
76
+ const nonce = "0x" + Buffer.from(nonceBytes).toString("hex");
77
+ // Authorization values travel as decimal strings in the header payload but
78
+ // are signed as uint256 — same split @x402/evm's client makes.
79
+ const authorization = {
80
+ from: args.address,
81
+ to: req.payTo,
82
+ value: req.amount ?? "0",
83
+ validAfter: String(now - 600), // small back-date absorbs clock skew
84
+ validBefore: String(now + (req.maxTimeoutSeconds ?? 300)),
85
+ nonce,
86
+ };
87
+ const signature = await args.signTypedData({
88
+ domain: {
89
+ name: String(req.extra.name),
90
+ version: String(req.extra.version),
91
+ chainId: evmChainId(req.network),
92
+ verifyingContract: req.asset,
93
+ },
94
+ types: AUTHORIZATION_TYPES,
95
+ message: {
96
+ from: authorization.from,
97
+ to: authorization.to,
98
+ value: BigInt(authorization.value),
99
+ validAfter: BigInt(authorization.validAfter),
100
+ validBefore: BigInt(authorization.validBefore),
101
+ nonce: authorization.nonce,
102
+ },
103
+ });
104
+ // Full canonical PaymentPayloadV2 envelope, same assembly as the SVM
105
+ // builder: `accepted` echoes which of the server's accepts[] we picked.
106
+ const envelope = {
107
+ x402Version: version,
108
+ accepted: {
109
+ payTo: req.payTo,
110
+ amount: req.amount ?? "0",
111
+ asset: req.asset,
112
+ network: req.network,
113
+ scheme: req.scheme || "exact",
114
+ maxTimeoutSeconds: req.maxTimeoutSeconds ?? 300,
115
+ extra: req.extra ?? undefined,
116
+ },
117
+ payload: { signature, authorization },
118
+ };
119
+ return Buffer.from(JSON.stringify(envelope), "utf8").toString("base64");
120
+ }
121
+ //# sourceMappingURL=evm-payment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"evm-payment.js","sourceRoot":"","sources":["../../src/x402/evm-payment.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAIH,4DAA4D;AAC5D,MAAM,mBAAmB,GAAG;IAC1B,yBAAyB,EAAE;QACzB,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE;QACjC,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE;QAC/B,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE;QAClC,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,SAAS,EAAE;QACvC,EAAE,IAAI,EAAE,aAAa,EAAE,IAAI,EAAE,SAAS,EAAE;QACxC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE;KACnC;CACF,CAAC;AAEF,0DAA0D;AAC1D,MAAM,eAAe,GAA2B;IAC9C,IAAI,EAAE,IAAI;IACV,QAAQ,EAAE,CAAC;IACX,QAAQ,EAAE,KAAK;IACf,QAAQ,EAAE,EAAE;IACZ,SAAS,EAAE,IAAI;CAChB,CAAC;AAiBF,MAAM,UAAU,UAAU,CAAC,OAAe;IACxC,IAAI,OAAO,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;QAClC,MAAM,EAAE,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,MA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ACpB,QAAQ,EAAE;YACR,KAAK,EAAE,GAAG,CAAC,KAAK;YAChB,MAAM,EAAE,GAAG,CAAC,MAAM,IAAI,GAAG;YACzB,KAAK,EAAE,GAAG,CAAC,KAAK;YAChB,OAAO,EAAE,GAAG,CAAC,OAAO;YACpB,MAAM,EAAE,GAAG,CAAC,MAAM,IAAI,OAAO;YAC7B,iBAAiB,EAAE,GAAG,CAAC,iBAAiB,IAAI,GAAG;YAC/C,KAAK,EAAE,GAAG,CAAC,KAAK,IAAI,SAAS;SAC9B;QACD,OAAO,EAAE,EAAE,SAAS,EAAE,aAAa,EAAE;KACtC,CAAC;IAEF,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AAC1E,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xona-labs/xpay",
3
- "version": "0.2.14",
3
+ "version": "0.2.16",
4
4
  "description": "Discovery and usage layer for agentic commerce — multi-network wallet, x402 payments, AgenC marketplace hires, 20k+ services via PayAI and other catalogs",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",