@integraledger/terms 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 (171) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +539 -0
  3. package/dist/agreement.d.ts +23 -0
  4. package/dist/agreement.js +264 -0
  5. package/dist/channels.d.ts +49 -0
  6. package/dist/channels.js +395 -0
  7. package/dist/gate.d.ts +69 -0
  8. package/dist/gate.js +347 -0
  9. package/dist/index.d.ts +5 -0
  10. package/dist/index.js +4 -0
  11. package/dist/pairings/ack-payment-request.d.ts +1 -0
  12. package/dist/pairings/ack-payment-request.js +12 -0
  13. package/dist/pairings/acp-checkout-delegated.d.ts +2 -0
  14. package/dist/pairings/acp-checkout-delegated.js +28 -0
  15. package/dist/pairings/acp-checkout-undelegated.d.ts +1 -0
  16. package/dist/pairings/acp-checkout-undelegated.js +11 -0
  17. package/dist/pairings/ap2-checkout-mandate.d.ts +2 -0
  18. package/dist/pairings/ap2-checkout-mandate.js +23 -0
  19. package/dist/pairings/batch.d.ts +29 -0
  20. package/dist/pairings/batch.js +83 -0
  21. package/dist/pairings/card-mastercard-vi-autonomous.d.ts +1 -0
  22. package/dist/pairings/card-mastercard-vi-autonomous.js +3 -0
  23. package/dist/pairings/card-mastercard-vi-immediate.d.ts +1 -0
  24. package/dist/pairings/card-mastercard-vi-immediate.js +3 -0
  25. package/dist/pairings/card-seller-reference.d.ts +1 -0
  26. package/dist/pairings/card-seller-reference.js +3 -0
  27. package/dist/pairings/card-visa-tap.d.ts +1 -0
  28. package/dist/pairings/card-visa-tap.js +3 -0
  29. package/dist/pairings/card.d.ts +7 -0
  30. package/dist/pairings/card.js +68 -0
  31. package/dist/pairings/common.d.ts +54 -0
  32. package/dist/pairings/common.js +134 -0
  33. package/dist/pairings/evm-x402.d.ts +3 -0
  34. package/dist/pairings/evm-x402.js +49 -0
  35. package/dist/pairings/index.d.ts +3 -0
  36. package/dist/pairings/index.js +134 -0
  37. package/dist/pairings/lightning.d.ts +17 -0
  38. package/dist/pairings/lightning.js +112 -0
  39. package/dist/pairings/lnbtc-request.d.ts +24 -0
  40. package/dist/pairings/lnbtc-request.js +175 -0
  41. package/dist/pairings/mpp-charge-card.d.ts +1 -0
  42. package/dist/pairings/mpp-charge-card.js +3 -0
  43. package/dist/pairings/mpp-charge-evm-authorization.d.ts +1 -0
  44. package/dist/pairings/mpp-charge-evm-authorization.js +3 -0
  45. package/dist/pairings/mpp-charge-evm-hash.d.ts +1 -0
  46. package/dist/pairings/mpp-charge-evm-hash.js +3 -0
  47. package/dist/pairings/mpp-charge-evm-permit2.d.ts +1 -0
  48. package/dist/pairings/mpp-charge-evm-permit2.js +3 -0
  49. package/dist/pairings/mpp-charge-evm-transaction.d.ts +1 -0
  50. package/dist/pairings/mpp-charge-evm-transaction.js +3 -0
  51. package/dist/pairings/mpp-charge-hedera.d.ts +23 -0
  52. package/dist/pairings/mpp-charge-hedera.js +137 -0
  53. package/dist/pairings/mpp-charge-nearintents.d.ts +2 -0
  54. package/dist/pairings/mpp-charge-nearintents.js +19 -0
  55. package/dist/pairings/mpp-charge-solana.d.ts +1 -0
  56. package/dist/pairings/mpp-charge-solana.js +25 -0
  57. package/dist/pairings/mpp-charge-stellar.d.ts +1 -0
  58. package/dist/pairings/mpp-charge-stellar.js +16 -0
  59. package/dist/pairings/mpp-charge-stripe.d.ts +1 -0
  60. package/dist/pairings/mpp-charge-stripe.js +3 -0
  61. package/dist/pairings/mpp-charge-tempo-memo.d.ts +9 -0
  62. package/dist/pairings/mpp-charge-tempo-memo.js +10 -0
  63. package/dist/pairings/mpp-charge-tempo-push.d.ts +12 -0
  64. package/dist/pairings/mpp-charge-tempo-push.js +21 -0
  65. package/dist/pairings/mpp-charge-usdc-evm.d.ts +1 -0
  66. package/dist/pairings/mpp-charge-usdc-evm.js +6 -0
  67. package/dist/pairings/mpp-charge-usdc-gateway.d.ts +2 -0
  68. package/dist/pairings/mpp-charge-usdc-gateway.js +33 -0
  69. package/dist/pairings/mpp-charge-usdc-solana.d.ts +1 -0
  70. package/dist/pairings/mpp-charge-usdc-solana.js +25 -0
  71. package/dist/pairings/mpp-charge-usdc-stacks.d.ts +1 -0
  72. package/dist/pairings/mpp-charge-usdc-stacks.js +17 -0
  73. package/dist/pairings/mpp-charge-xrpl.d.ts +1 -0
  74. package/dist/pairings/mpp-charge-xrpl.js +20 -0
  75. package/dist/pairings/mpp-confirm-only.d.ts +2 -0
  76. package/dist/pairings/mpp-confirm-only.js +21 -0
  77. package/dist/pairings/mpp-rails.d.ts +48 -0
  78. package/dist/pairings/mpp-rails.js +71 -0
  79. package/dist/pairings/mpp-session-evm.d.ts +1 -0
  80. package/dist/pairings/mpp-session-evm.js +3 -0
  81. package/dist/pairings/mpp-session-hedera.d.ts +2 -0
  82. package/dist/pairings/mpp-session-hedera.js +41 -0
  83. package/dist/pairings/mpp-session-solana.d.ts +1 -0
  84. package/dist/pairings/mpp-session-solana.js +60 -0
  85. package/dist/pairings/mpp-session-tempo.d.ts +1 -0
  86. package/dist/pairings/mpp-session-tempo.js +3 -0
  87. package/dist/pairings/mpp-session-xrpl.d.ts +1 -0
  88. package/dist/pairings/mpp-session-xrpl.js +42 -0
  89. package/dist/pairings/mpp-session.d.ts +5 -0
  90. package/dist/pairings/mpp-session.js +65 -0
  91. package/dist/pairings/mpp-subscription-stripe.d.ts +1 -0
  92. package/dist/pairings/mpp-subscription-stripe.js +6 -0
  93. package/dist/pairings/mpp-subscription-tempo.d.ts +2 -0
  94. package/dist/pairings/mpp-subscription-tempo.js +2 -0
  95. package/dist/pairings/mpp.d.ts +43 -0
  96. package/dist/pairings/mpp.js +194 -0
  97. package/dist/pairings/protocol-groups.d.ts +17 -0
  98. package/dist/pairings/protocol-groups.js +27 -0
  99. package/dist/pairings/ucp-booking-ap2-mandate.d.ts +1 -0
  100. package/dist/pairings/ucp-booking-ap2-mandate.js +3 -0
  101. package/dist/pairings/ucp-booking-unsigned.d.ts +1 -0
  102. package/dist/pairings/ucp-booking-unsigned.js +3 -0
  103. package/dist/pairings/ucp-checkout-ap2-mandate.d.ts +1 -0
  104. package/dist/pairings/ucp-checkout-ap2-mandate.js +3 -0
  105. package/dist/pairings/ucp-checkout-unsigned.d.ts +1 -0
  106. package/dist/pairings/ucp-checkout-unsigned.js +3 -0
  107. package/dist/pairings/ucp.d.ts +5 -0
  108. package/dist/pairings/ucp.js +35 -0
  109. package/dist/pairings/x402-account-rails.d.ts +25 -0
  110. package/dist/pairings/x402-account-rails.js +52 -0
  111. package/dist/pairings/x402-auth-capture-eip155-eip3009.d.ts +1 -0
  112. package/dist/pairings/x402-auth-capture-eip155-eip3009.js +3 -0
  113. package/dist/pairings/x402-auth-capture-eip155-permit2.d.ts +1 -0
  114. package/dist/pairings/x402-auth-capture-eip155-permit2.js +3 -0
  115. package/dist/pairings/x402-batch-settlement-cloudflare.d.ts +2 -0
  116. package/dist/pairings/x402-batch-settlement-cloudflare.js +21 -0
  117. package/dist/pairings/x402-batch-settlement-eip155.d.ts +2 -0
  118. package/dist/pairings/x402-batch-settlement-eip155.js +32 -0
  119. package/dist/pairings/x402-batch-settlement-solana.d.ts +2 -0
  120. package/dist/pairings/x402-batch-settlement-solana.js +55 -0
  121. package/dist/pairings/x402-exact-algorand.d.ts +1 -0
  122. package/dist/pairings/x402-exact-algorand.js +37 -0
  123. package/dist/pairings/x402-exact-aptos.d.ts +1 -0
  124. package/dist/pairings/x402-exact-aptos.js +9 -0
  125. package/dist/pairings/x402-exact-cardano.d.ts +1 -0
  126. package/dist/pairings/x402-exact-cardano.js +7 -0
  127. package/dist/pairings/x402-exact-casper.d.ts +2 -0
  128. package/dist/pairings/x402-exact-casper.js +21 -0
  129. package/dist/pairings/x402-exact-ccd.d.ts +2 -0
  130. package/dist/pairings/x402-exact-ccd.js +20 -0
  131. package/dist/pairings/x402-exact-eip155-eip3009.d.ts +1 -0
  132. package/dist/pairings/x402-exact-eip155-eip3009.js +3 -0
  133. package/dist/pairings/x402-exact-eip155-erc7710-salt.d.ts +1 -0
  134. package/dist/pairings/x402-exact-eip155-erc7710-salt.js +3 -0
  135. package/dist/pairings/x402-exact-eip155-erc7710.d.ts +1 -0
  136. package/dist/pairings/x402-exact-eip155-erc7710.js +3 -0
  137. package/dist/pairings/x402-exact-eip155-permit2.d.ts +1 -0
  138. package/dist/pairings/x402-exact-eip155-permit2.js +3 -0
  139. package/dist/pairings/x402-exact-hedera-transfer-executor.d.ts +1 -0
  140. package/dist/pairings/x402-exact-hedera-transfer-executor.js +23 -0
  141. package/dist/pairings/x402-exact-hedera.d.ts +3 -0
  142. package/dist/pairings/x402-exact-hedera.js +46 -0
  143. package/dist/pairings/x402-exact-near.d.ts +2 -0
  144. package/dist/pairings/x402-exact-near.js +34 -0
  145. package/dist/pairings/x402-exact-polkadot-lcp-assets-remark.d.ts +2 -0
  146. package/dist/pairings/x402-exact-polkadot-lcp-assets-remark.js +20 -0
  147. package/dist/pairings/x402-exact-solana.d.ts +11 -0
  148. package/dist/pairings/x402-exact-solana.js +36 -0
  149. package/dist/pairings/x402-exact-starknet.d.ts +2 -0
  150. package/dist/pairings/x402-exact-starknet.js +20 -0
  151. package/dist/pairings/x402-exact-stellar.d.ts +1 -0
  152. package/dist/pairings/x402-exact-stellar.js +17 -0
  153. package/dist/pairings/x402-exact-sui.d.ts +1 -0
  154. package/dist/pairings/x402-exact-sui.js +10 -0
  155. package/dist/pairings/x402-exact-tron-lcp-trc20-memo.d.ts +2 -0
  156. package/dist/pairings/x402-exact-tron-lcp-trc20-memo.js +45 -0
  157. package/dist/pairings/x402-exact-tvm.d.ts +2 -0
  158. package/dist/pairings/x402-exact-tvm.js +35 -0
  159. package/dist/pairings/x402-exact-xrpl.d.ts +1 -0
  160. package/dist/pairings/x402-exact-xrpl.js +27 -0
  161. package/dist/pairings/x402-rail.d.ts +31 -0
  162. package/dist/pairings/x402-rail.js +65 -0
  163. package/dist/pairings/x402-upto-eip155-permit2.d.ts +1 -0
  164. package/dist/pairings/x402-upto-eip155-permit2.js +3 -0
  165. package/dist/pairings/x402-upto-solana.d.ts +1 -0
  166. package/dist/pairings/x402-upto-solana.js +42 -0
  167. package/dist/transact.d.ts +20 -0
  168. package/dist/transact.js +53 -0
  169. package/dist/types.d.ts +169 -0
  170. package/dist/types.js +2 -0
  171. package/package.json +64 -0
@@ -0,0 +1,68 @@
1
+ import { isObject, isRefusal, refuse } from "./common.js";
2
+ import { confirmOnlyPiece, objectChoice, unnamedBuyer } from "./protocol-groups.js";
3
+ const NS = "card";
4
+ /** `{doc}` for a named buyer; the document is the one the binding's `read` was given. */
5
+ function chooseDoc(_read, account, _inputs, _now, _ref, doc) {
6
+ const unnamed = unnamedBuyer(account, NS);
7
+ if (unnamed !== undefined)
8
+ return unnamed;
9
+ if (doc === undefined)
10
+ return refuse("card/document-missing");
11
+ return { doc: doc };
12
+ }
13
+ function docOf(chosen) {
14
+ const c = objectChoice(chosen, NS);
15
+ if (isRefusal(c))
16
+ return c;
17
+ return Object.hasOwn(c, "doc") ? c["doc"] : refuse("card/choice-malformed");
18
+ }
19
+ function strings(signature, keys) {
20
+ if (!isObject(signature))
21
+ return undefined;
22
+ const out = {};
23
+ for (const k of keys) {
24
+ const v = signature[k];
25
+ if (typeof v !== "string" || v === "")
26
+ return undefined;
27
+ out[k] = v;
28
+ }
29
+ return out;
30
+ }
31
+ function signedPiece(pairing, request, complete) {
32
+ return Object.freeze({
33
+ choose(read, account, inputs, now, ref, doc) {
34
+ const choice = chooseDoc(read, account, inputs, now, ref, doc);
35
+ if (isRefusal(choice))
36
+ return choice;
37
+ return { pairing, choice, ref };
38
+ },
39
+ choice: docOf,
40
+ request,
41
+ complete: async (unsigned, signature) => complete(unsigned, signature),
42
+ });
43
+ }
44
+ /** The TAP field exactly as built, for the agent's signer to add and list in its `agent-payer-auth` signature. */
45
+ export function tapPiece(pairing) {
46
+ return signedPiece(pairing, (unsigned) => ({ kind: "tap-field", ...unsigned }), (_unsigned, signature) => {
47
+ const s = strings(signature, ["signatureInput", "signature"]);
48
+ const lines = isObject(signature) ? signature["lcpHash"] : undefined;
49
+ if (s === undefined || !Array.isArray(lines))
50
+ return refuse("card/signature-malformed");
51
+ return { signatureInput: s.signatureInput, signature: s.signature, lcpHash: lines };
52
+ });
53
+ }
54
+ /** The checkout mandate exactly as built, for the wallet (Immediate) or the agent (Autonomous) to sign. */
55
+ export function viPiece(pairing, mode) {
56
+ return signedPiece(pairing, (unsigned) => ({ kind: "vi-checkout-mandate", ...unsigned }), (_unsigned, signature) => {
57
+ if (mode === "immediate") {
58
+ const s = strings(signature, ["l2"]);
59
+ return s === undefined ? refuse("card/signature-malformed") : { l2: s.l2 };
60
+ }
61
+ const s = strings(signature, ["l1", "l2", "l3b"]);
62
+ return s === undefined ? refuse("card/signature-malformed") : { l1: s.l1, l2: s.l2, l3b: s.l3b };
63
+ });
64
+ }
65
+ /** The plain card checkout: the gate confirms only. */
66
+ export function sellerReferencePiece(pairing) {
67
+ return confirmOnlyPiece(pairing, chooseDoc);
68
+ }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * What the buyer pieces share: the CAIP-10 account, the first x402 option on the account's network, the payment
3
+ * identifier the x402 client writes, and the JSON forms of byte strings and integers.
4
+ */
5
+ import type { Json, Refusal } from "@integraledger/lcp";
6
+ import type { PaymentRequired, PaymentRequirements, X402Offer } from "@integraledger/lcp/x402";
7
+ import type { Chosen, Inputs, Read } from "../types.js";
8
+ export declare function refuse(code: string): Refusal;
9
+ export declare function isRefusal(v: unknown): v is Refusal;
10
+ export declare function isObject(v: unknown): v is Record<string, unknown>;
11
+ /**
12
+ * A CAIP-10 account's namespace, its CAIP-2 network and its address, percent-decoded (CAIP-10's escape). An `eip155`
13
+ * address is taken as written: the gate's account form for EVM is `^eip155:([0-9]+):(0x[0-9a-fA-F]{40})$`, which has no escape.
14
+ */
15
+ export declare function accountOf(account: unknown): {
16
+ namespace: string;
17
+ network: string;
18
+ address: string;
19
+ } | undefined;
20
+ /** True when the request tells the signer to broadcast what it signs. */
21
+ export declare function broadcasts(request: unknown): boolean;
22
+ /** The offer a binding's `read` gives for x402, or undefined. */
23
+ export declare function x402OfferOf(read: Read): X402Offer | undefined;
24
+ /**
25
+ * The first of the offer's options, in document order, whose network is the account's, with the account parsed. The
26
+ * buyer chooses among options by removing the others from `accepts` first.
27
+ */
28
+ export declare function firstOption(read: Read, account: string, ns: string, pairing: string, address?: RegExp): {
29
+ required: PaymentRequired;
30
+ accepted: PaymentRequirements;
31
+ address: string;
32
+ } | Refusal;
33
+ /** `chosen` for an x402 pairing, with the choice as plain JSON. */
34
+ export declare function x402Chosen(pairing: string, choice: Record<string, Json>, ref: string): Chosen;
35
+ /**
36
+ * The completed payment with `id` = `ref` appended to the echoed `payment-identifier` info, where the challenge
37
+ * advertises that extension. An `info` that is not an object, or that already holds `id`, cannot take it without
38
+ * overwriting, and is refused.
39
+ */
40
+ export declare function withPaymentIdentifier<P>(signed: P, required: unknown, ref: string): P | Refusal;
41
+ /** The bytes of `0x` hex, of the given length when one is named, or undefined. */
42
+ export declare function bytesOf(s: unknown, length?: number): Uint8Array | undefined;
43
+ /** `0x` and the lowercase hex of the bytes. */
44
+ export declare function hexOf(b: Uint8Array): `0x${string}`;
45
+ /** A decimal string of at most 78 digits as a bigint, or undefined. */
46
+ export declare function bigintOf(v: unknown): bigint | undefined;
47
+ /** A non-negative safe integer, or undefined. */
48
+ export declare function uintOf(v: unknown): number | undefined;
49
+ /** The named inputs, present and of the given kinds, as JSON; or the refusal naming the first missing one. */
50
+ export declare function inputsOf(inputs: Inputs, pairing: string, spec: {
51
+ readonly [k: string]: "string" | "decimal" | "uint" | "hex" | "object" | "optional-string" | "optional-uint" | "optional-decimal" | "optional-hex" | "optional-object";
52
+ }): Record<string, Json> | Refusal;
53
+ /** The choice object of `chosen`, or undefined. */
54
+ export declare function choiceOf(chosen: Chosen): Record<string, unknown> | undefined;
@@ -0,0 +1,134 @@
1
+ const CAIP10 = /^([-a-z0-9]{3,8}):([-_a-zA-Z0-9]{1,32}):([-.%a-zA-Z0-9]{1,128})$/;
2
+ const PAYMENT_IDENTIFIER = "payment-identifier";
3
+ const HEX = /^0x(?:[0-9a-fA-F]{2})*$/;
4
+ const DECIMAL = /^[0-9]{1,78}$/;
5
+ export function refuse(code) {
6
+ return { refused: true, code };
7
+ }
8
+ export function isRefusal(v) {
9
+ return typeof v === "object" && v !== null && v.refused === true;
10
+ }
11
+ export function isObject(v) {
12
+ return typeof v === "object" && v !== null && !Array.isArray(v);
13
+ }
14
+ /**
15
+ * A CAIP-10 account's namespace, its CAIP-2 network and its address, percent-decoded (CAIP-10's escape). An `eip155`
16
+ * address is taken as written: the gate's account form for EVM is `^eip155:([0-9]+):(0x[0-9a-fA-F]{40})$`, which has no escape.
17
+ */
18
+ export function accountOf(account) {
19
+ const m = typeof account === "string" ? CAIP10.exec(account) : null;
20
+ if (m === null)
21
+ return undefined;
22
+ if (m[1] === "eip155")
23
+ return { namespace: m[1], network: `${m[1]}:${m[2]}`, address: m[3] };
24
+ let address;
25
+ try {
26
+ address = decodeURIComponent(m[3]);
27
+ }
28
+ catch {
29
+ return undefined;
30
+ }
31
+ return { namespace: m[1], network: `${m[1]}:${m[2]}`, address };
32
+ }
33
+ /** True when the request tells the signer to broadcast what it signs. */
34
+ export function broadcasts(request) {
35
+ return isObject(request) && request["broadcast"] === true;
36
+ }
37
+ /** The offer a binding's `read` gives for x402, or undefined. */
38
+ export function x402OfferOf(read) {
39
+ const offer = read.offer;
40
+ if (!isObject(offer) || !isObject(offer["required"]) || !Array.isArray(offer["options"]))
41
+ return undefined;
42
+ return offer;
43
+ }
44
+ /**
45
+ * The first of the offer's options, in document order, whose network is the account's, with the account parsed. The
46
+ * buyer chooses among options by removing the others from `accepts` first.
47
+ */
48
+ export function firstOption(read, account, ns, pairing, address) {
49
+ const a = accountOf(account);
50
+ const offer = x402OfferOf(read);
51
+ if (a === undefined || a.namespace !== ns || (address !== undefined && !address.test(a.address))) {
52
+ return refuse(`${pairing.split("/")[0]}/no-payable-option`);
53
+ }
54
+ if (offer === undefined)
55
+ return refuse("x402/no-payable-option");
56
+ const accepted = offer.options.find((o) => isObject(o) && o.network === a.network);
57
+ if (accepted === undefined)
58
+ return refuse("x402/no-payable-option");
59
+ return { required: offer.required, accepted, address: a.address };
60
+ }
61
+ /** `chosen` for an x402 pairing, with the choice as plain JSON. */
62
+ export function x402Chosen(pairing, choice, ref) {
63
+ return { pairing, choice, ref };
64
+ }
65
+ /**
66
+ * The completed payment with `id` = `ref` appended to the echoed `payment-identifier` info, where the challenge
67
+ * advertises that extension. An `info` that is not an object, or that already holds `id`, cannot take it without
68
+ * overwriting, and is refused.
69
+ */
70
+ export function withPaymentIdentifier(signed, required, ref) {
71
+ const advertised = isObject(required) ? required["extensions"] : undefined;
72
+ if (!isObject(advertised) || !Object.hasOwn(advertised, PAYMENT_IDENTIFIER))
73
+ return signed;
74
+ const extensions = isObject(signed) ? signed["extensions"] : undefined;
75
+ const extension = isObject(extensions) ? extensions[PAYMENT_IDENTIFIER] : undefined;
76
+ const info = isObject(extension) ? extension["info"] : undefined;
77
+ if (!isObject(extensions) || !isObject(extension) || !isObject(info) || Object.hasOwn(info, "id")) {
78
+ return refuse("x402/payment-identifier-unwritable");
79
+ }
80
+ return {
81
+ ...signed,
82
+ extensions: { ...extensions, [PAYMENT_IDENTIFIER]: { ...extension, info: { ...info, id: ref } } },
83
+ };
84
+ }
85
+ /** The bytes of `0x` hex, of the given length when one is named, or undefined. */
86
+ export function bytesOf(s, length) {
87
+ if (typeof s !== "string" || !HEX.test(s))
88
+ return undefined;
89
+ const out = new Uint8Array((s.length - 2) / 2);
90
+ if (length !== undefined && out.length !== length)
91
+ return undefined;
92
+ for (let i = 0; i < out.length; i++)
93
+ out[i] = Number.parseInt(s.slice(2 + 2 * i, 4 + 2 * i), 16);
94
+ return out;
95
+ }
96
+ /** `0x` and the lowercase hex of the bytes. */
97
+ export function hexOf(b) {
98
+ let s = "0x";
99
+ for (const x of b)
100
+ s += (x < 16 ? "0" : "") + x.toString(16);
101
+ return s;
102
+ }
103
+ /** A decimal string of at most 78 digits as a bigint, or undefined. */
104
+ export function bigintOf(v) {
105
+ return typeof v === "string" && DECIMAL.test(v) ? BigInt(v) : undefined;
106
+ }
107
+ /** A non-negative safe integer, or undefined. */
108
+ export function uintOf(v) {
109
+ return typeof v === "number" && Number.isSafeInteger(v) && v >= 0 ? v : undefined;
110
+ }
111
+ /** The named inputs, present and of the given kinds, as JSON; or the refusal naming the first missing one. */
112
+ export function inputsOf(inputs, pairing, spec) {
113
+ const out = {};
114
+ for (const [k, kind] of Object.entries(spec)) {
115
+ const v = inputs[k];
116
+ const optional = kind.startsWith("optional-");
117
+ if (v === undefined && optional)
118
+ continue;
119
+ const base = optional ? kind.slice("optional-".length) : kind;
120
+ const ok = (base === "string" && typeof v === "string" && v !== "") ||
121
+ (base === "decimal" && bigintOf(v) !== undefined) ||
122
+ (base === "uint" && uintOf(v) !== undefined) ||
123
+ (base === "hex" && typeof v === "string" && HEX.test(v)) ||
124
+ (base === "object" && isObject(v));
125
+ if (!ok)
126
+ return refuse(`${pairing.split("/")[0]}/input-missing`);
127
+ out[k] = v;
128
+ }
129
+ return out;
130
+ }
131
+ /** The choice object of `chosen`, or undefined. */
132
+ export function choiceOf(chosen) {
133
+ return isObject(chosen) && isObject(chosen.choice) ? chosen.choice : undefined;
134
+ }
@@ -0,0 +1,3 @@
1
+ import type { BuyerPiece } from "../types.js";
2
+ /** The buyer piece for one x402 EVM pairing. */
3
+ export declare function evmX402Piece(pairing: string): BuyerPiece;
@@ -0,0 +1,49 @@
1
+ import { choiceOf, firstOption, isObject, refuse, withPaymentIdentifier, x402Chosen } from "./common.js";
2
+ const EVM_ADDRESS = /^0x[0-9a-fA-F]{40}$/;
3
+ /** An EIP-712 signing request, from either form of an EVM x402 build. */
4
+ function request(unsigned) {
5
+ const u = unsigned;
6
+ if ("typedData" in u)
7
+ return { kind: "eip712", typedData: u.typedData };
8
+ return u.request;
9
+ }
10
+ async function complete(unsigned, signature, chosen) {
11
+ const u = unsigned;
12
+ let signed;
13
+ if ("typedData" in u || u.request.kind === "eip712") {
14
+ if (typeof signature !== "string")
15
+ return refuse("x402/signature-malformed");
16
+ signed = u.complete(signature);
17
+ }
18
+ else {
19
+ if (!isObject(signature))
20
+ return refuse("x402/signature-malformed");
21
+ const { delegationManager, permissionContext, delegator } = signature;
22
+ if (typeof delegationManager !== "string" || typeof permissionContext !== "string" || typeof delegator !== "string") {
23
+ return refuse("x402/signature-malformed");
24
+ }
25
+ const delegated = u;
26
+ signed = delegated.complete({
27
+ delegationManager: delegationManager,
28
+ permissionContext: permissionContext,
29
+ delegator: delegator,
30
+ });
31
+ }
32
+ if ("refused" in signed)
33
+ return signed;
34
+ return withPaymentIdentifier(signed, choiceOf(chosen)?.["required"], chosen.ref);
35
+ }
36
+ /** The buyer piece for one x402 EVM pairing. */
37
+ export function evmX402Piece(pairing) {
38
+ return Object.freeze({
39
+ choose(read, account, _inputs, now, ref) {
40
+ const o = firstOption(read, account, "eip155", pairing, EVM_ADDRESS);
41
+ if ("refused" in o)
42
+ return refuse("x402/no-payable-option");
43
+ return x402Chosen(pairing, { required: o.required, accepted: o.accepted, from: o.address, now }, ref);
44
+ },
45
+ choice: (chosen) => choiceOf(chosen) ?? refuse("x402/choice-malformed"),
46
+ request,
47
+ complete,
48
+ });
49
+ }
@@ -0,0 +1,3 @@
1
+ import type { BuyerPiece } from "../types.js";
2
+ /** The buyer piece for each pairing the gate pays, by pairing id. */
3
+ export declare const PIECES: ReadonlyMap<string, BuyerPiece>;
@@ -0,0 +1,134 @@
1
+ import { mppChargeLightning, mppSessionLightning, x402ExactLnbtc, x402ExactLnbtcInvoiceNamed } from "./lightning.js";
2
+ import { x402ExactCasper } from "./x402-exact-casper.js";
3
+ import { x402ExactCcd } from "./x402-exact-ccd.js";
4
+ import { ackPaymentRequest } from "./ack-payment-request.js";
5
+ import { acpCheckoutDelegated } from "./acp-checkout-delegated.js";
6
+ import { acpCheckoutUndelegated } from "./acp-checkout-undelegated.js";
7
+ import { ap2CheckoutMandate } from "./ap2-checkout-mandate.js";
8
+ import { cardMastercardViAutonomous } from "./card-mastercard-vi-autonomous.js";
9
+ import { cardMastercardViImmediate } from "./card-mastercard-vi-immediate.js";
10
+ import { cardSellerReference } from "./card-seller-reference.js";
11
+ import { cardVisaTap } from "./card-visa-tap.js";
12
+ import { mppChargeEvmAuthorization } from "./mpp-charge-evm-authorization.js";
13
+ import { mppChargeEvmHash } from "./mpp-charge-evm-hash.js";
14
+ import { mppChargeEvmPermit2 } from "./mpp-charge-evm-permit2.js";
15
+ import { mppChargeEvmTransaction } from "./mpp-charge-evm-transaction.js";
16
+ import { mppChargeTempoMemo } from "./mpp-charge-tempo-memo.js";
17
+ import { mppChargeTempoPush } from "./mpp-charge-tempo-push.js";
18
+ import { mppChargeHedera } from "./mpp-charge-hedera.js";
19
+ import { mppChargeCard } from "./mpp-charge-card.js";
20
+ import { mppChargeNearIntents } from "./mpp-charge-nearintents.js";
21
+ import { mppChargeStripe } from "./mpp-charge-stripe.js";
22
+ import { mppChargeUsdcEvm } from "./mpp-charge-usdc-evm.js";
23
+ import { mppChargeUsdcGateway } from "./mpp-charge-usdc-gateway.js";
24
+ import { mppChargeUsdcSolana } from "./mpp-charge-usdc-solana.js";
25
+ import { mppChargeUsdcStacks } from "./mpp-charge-usdc-stacks.js";
26
+ import { mppSubscriptionStripe } from "./mpp-subscription-stripe.js";
27
+ import { mppChargeSolana } from "./mpp-charge-solana.js";
28
+ import { mppChargeStellar } from "./mpp-charge-stellar.js";
29
+ import { mppChargeXrpl } from "./mpp-charge-xrpl.js";
30
+ import { mppSessionHedera } from "./mpp-session-hedera.js";
31
+ import { mppSessionSolana } from "./mpp-session-solana.js";
32
+ import { mppSessionXrpl } from "./mpp-session-xrpl.js";
33
+ import { ucpBookingAp2Mandate } from "./ucp-booking-ap2-mandate.js";
34
+ import { ucpBookingUnsigned } from "./ucp-booking-unsigned.js";
35
+ import { ucpCheckoutAp2Mandate } from "./ucp-checkout-ap2-mandate.js";
36
+ import { ucpCheckoutUnsigned } from "./ucp-checkout-unsigned.js";
37
+ import { x402AuthCaptureEip155Eip3009 } from "./x402-auth-capture-eip155-eip3009.js";
38
+ import { x402AuthCaptureEip155Permit2 } from "./x402-auth-capture-eip155-permit2.js";
39
+ import { x402ExactAlgorand } from "./x402-exact-algorand.js";
40
+ import { mppSessionEvm } from "./mpp-session-evm.js";
41
+ import { mppSessionTempo } from "./mpp-session-tempo.js";
42
+ import { mppSubscriptionTempo } from "./mpp-subscription-tempo.js";
43
+ import { x402BatchSettlementCloudflare } from "./x402-batch-settlement-cloudflare.js";
44
+ import { x402BatchSettlementEip155 } from "./x402-batch-settlement-eip155.js";
45
+ import { x402BatchSettlementSolana } from "./x402-batch-settlement-solana.js";
46
+ import { x402ExactEip155Eip3009 } from "./x402-exact-eip155-eip3009.js";
47
+ import { x402ExactEip155Erc7710 } from "./x402-exact-eip155-erc7710.js";
48
+ import { x402ExactEip155Erc7710Salt } from "./x402-exact-eip155-erc7710-salt.js";
49
+ import { x402ExactEip155Permit2 } from "./x402-exact-eip155-permit2.js";
50
+ import { x402ExactHedera } from "./x402-exact-hedera.js";
51
+ import { x402ExactHederaTransferExecutor } from "./x402-exact-hedera-transfer-executor.js";
52
+ import { x402ExactSolana } from "./x402-exact-solana.js";
53
+ import { x402ExactStellar } from "./x402-exact-stellar.js";
54
+ import { x402ExactXrpl } from "./x402-exact-xrpl.js";
55
+ import { x402UptoEip155Permit2 } from "./x402-upto-eip155-permit2.js";
56
+ import { x402UptoSolana } from "./x402-upto-solana.js";
57
+ import { x402ExactAptos } from "./x402-exact-aptos.js";
58
+ import { x402ExactCardano } from "./x402-exact-cardano.js";
59
+ import { x402ExactNear } from "./x402-exact-near.js";
60
+ import { x402ExactPolkadotLcpAssetsRemark } from "./x402-exact-polkadot-lcp-assets-remark.js";
61
+ import { x402ExactStarknet } from "./x402-exact-starknet.js";
62
+ import { x402ExactSui } from "./x402-exact-sui.js";
63
+ import { x402ExactTronLcpTrc20Memo } from "./x402-exact-tron-lcp-trc20-memo.js";
64
+ import { x402ExactTvm } from "./x402-exact-tvm.js";
65
+ /** The buyer piece for each pairing the gate pays, by pairing id. */
66
+ export const PIECES = new Map([
67
+ ["x402/exact/eip155/eip3009", x402ExactEip155Eip3009],
68
+ ["x402/exact/eip155/permit2", x402ExactEip155Permit2],
69
+ ["x402/exact/eip155/erc7710", x402ExactEip155Erc7710],
70
+ ["x402/exact/eip155/erc7710-salt", x402ExactEip155Erc7710Salt],
71
+ ["x402/upto/eip155/permit2", x402UptoEip155Permit2],
72
+ ["x402/auth-capture/eip155/eip3009", x402AuthCaptureEip155Eip3009],
73
+ ["x402/auth-capture/eip155/permit2", x402AuthCaptureEip155Permit2],
74
+ ["x402/exact/solana", x402ExactSolana],
75
+ ["x402/upto/solana", x402UptoSolana],
76
+ ["x402/exact/stellar", x402ExactStellar],
77
+ ["x402/exact/xrpl", x402ExactXrpl],
78
+ ["x402/exact/hedera", x402ExactHedera],
79
+ ["x402/exact/hedera/transfer-executor", x402ExactHederaTransferExecutor],
80
+ ["x402/exact/algorand", x402ExactAlgorand],
81
+ ["mpp/charge/evm/authorization", mppChargeEvmAuthorization],
82
+ ["mpp/charge/evm/permit2", mppChargeEvmPermit2],
83
+ ["mpp/charge/evm/transaction", mppChargeEvmTransaction],
84
+ ["mpp/charge/evm/hash", mppChargeEvmHash],
85
+ ["mpp/charge/tempo/memo", mppChargeTempoMemo],
86
+ ["mpp/charge/tempo/push", mppChargeTempoPush],
87
+ ["mpp/charge/hedera", mppChargeHedera],
88
+ ["mpp/charge/solana", mppChargeSolana],
89
+ ["mpp/charge/stellar", mppChargeStellar],
90
+ ["mpp/charge/xrpl", mppChargeXrpl],
91
+ ["mpp/charge/nearintents", mppChargeNearIntents],
92
+ ["mpp/session/hedera", mppSessionHedera],
93
+ ["mpp/session/solana", mppSessionSolana],
94
+ ["mpp/session/xrpl", mppSessionXrpl],
95
+ ["x402/exact/near", x402ExactNear],
96
+ ["x402/exact/tron/lcp-trc20-memo", x402ExactTronLcpTrc20Memo],
97
+ ["x402/exact/tvm", x402ExactTvm],
98
+ ["x402/exact/starknet", x402ExactStarknet],
99
+ ["x402/exact/polkadot/lcp-assets-remark", x402ExactPolkadotLcpAssetsRemark],
100
+ ["x402/exact/sui", x402ExactSui],
101
+ ["x402/exact/aptos", x402ExactAptos],
102
+ ["x402/exact/cardano", x402ExactCardano],
103
+ ["x402/exact/casper", x402ExactCasper],
104
+ ["x402/exact/ccd", x402ExactCcd],
105
+ ["x402/exact/lnbtc", x402ExactLnbtc],
106
+ ["x402/exact/lnbtc/invoice-named", x402ExactLnbtcInvoiceNamed],
107
+ ["mpp/charge/lightning", mppChargeLightning],
108
+ ["mpp/session/lightning", mppSessionLightning],
109
+ ["card/visa-tap", cardVisaTap],
110
+ ["card/mastercard-vi/immediate", cardMastercardViImmediate],
111
+ ["card/mastercard-vi/autonomous", cardMastercardViAutonomous],
112
+ ["card/seller-reference", cardSellerReference],
113
+ ["ap2/checkout-mandate", ap2CheckoutMandate],
114
+ ["ucp/checkout/ap2-mandate", ucpCheckoutAp2Mandate],
115
+ ["ucp/checkout/unsigned", ucpCheckoutUnsigned],
116
+ ["ucp/booking/ap2-mandate", ucpBookingAp2Mandate],
117
+ ["ucp/booking/unsigned", ucpBookingUnsigned],
118
+ ["acp/checkout/delegated", acpCheckoutDelegated],
119
+ ["acp/checkout/undelegated", acpCheckoutUndelegated],
120
+ ["ack/payment-request", ackPaymentRequest],
121
+ ["x402/batch-settlement/eip155", x402BatchSettlementEip155],
122
+ ["x402/batch-settlement/solana", x402BatchSettlementSolana],
123
+ ["x402/batch-settlement/cloudflare", x402BatchSettlementCloudflare],
124
+ ["mpp/session/evm", mppSessionEvm],
125
+ ["mpp/session/tempo", mppSessionTempo],
126
+ ["mpp/subscription/tempo", mppSubscriptionTempo],
127
+ ["mpp/charge/card", mppChargeCard],
128
+ ["mpp/charge/stripe", mppChargeStripe],
129
+ ["mpp/subscription/stripe", mppSubscriptionStripe],
130
+ ["mpp/charge/usdc/evm", mppChargeUsdcEvm],
131
+ ["mpp/charge/usdc/solana", mppChargeUsdcSolana],
132
+ ["mpp/charge/usdc/stacks", mppChargeUsdcStacks],
133
+ ["mpp/charge/usdc/gateway", mppChargeUsdcGateway],
134
+ ]);
@@ -0,0 +1,17 @@
1
+ import type { BuyerPiece } from "../types.js";
2
+ /**
3
+ * The x402 Lightning piece: the first option on the account's `lnbtc` network, whose request hash, recomputed from the
4
+ * buyer's own request (the `request` input), equals both the option's `extra.requestHash` and the invoice's description
5
+ * hash. With `named`, the build reads the ATR the gate compared, which rides beside the choice as the bytes the gate
6
+ * passes.
7
+ */
8
+ export declare function lnbtcPiece(pairing: string, named: boolean): BuyerPiece;
9
+ /**
10
+ * The MPP Lightning piece: the first challenge offering the pairing whose invoice's BOLT11 currency is the account's
11
+ * `lnbtc` network's. A session's return invoice is the buyer's input, and rides in the build's choice.
12
+ */
13
+ export declare function lnMppPiece(pairing: string, session: boolean): BuyerPiece;
14
+ export declare const x402ExactLnbtc: BuyerPiece;
15
+ export declare const x402ExactLnbtcInvoiceNamed: BuyerPiece;
16
+ export declare const mppChargeLightning: BuyerPiece;
17
+ export declare const mppSessionLightning: BuyerPiece;
@@ -0,0 +1,112 @@
1
+ import { decodeBolt11 } from "@integraledger/lcp/lightning";
2
+ import { accountOf, choiceOf, firstOption, inputsOf, isObject, isRefusal, refuse, withPaymentIdentifier, x402Chosen } from "./common.js";
3
+ import { checkRequestHash } from "./lnbtc-request.js";
4
+ import { offers, requestOf } from "./mpp.js";
5
+ /**
6
+ * The BOLT11 currency of each `lnbtc` network, the CAIP-2 reference being the first 32 hex characters of the network's
7
+ * genesis block hash: `bc` mainnet; `tb` testnet3 and testnet4; `tbs` signet; `bcrt` regtest.
8
+ */
9
+ const LN_CURRENCY = {
10
+ "lnbtc:000000000019d6689c085ae165831e93": "bc",
11
+ "lnbtc:000000000933ea01ad0ee984209779ba": "tb",
12
+ "lnbtc:00000000da84f2bafbbc53dee25a72ae": "tb",
13
+ "lnbtc:00000008819873e925422c1ff0f99f7c": "tbs",
14
+ "lnbtc:0f9188f13cb7b2c71f2a335e3a4fc328": "bcrt",
15
+ };
16
+ /** The invoice a Lightning challenge asks the node to pay: a charge's `methodDetails.invoice`, a session's deposit. */
17
+ function invoiceOf(c, session) {
18
+ const request = requestOf(c);
19
+ if (request === undefined)
20
+ return undefined;
21
+ if (session)
22
+ return request["depositInvoice"];
23
+ const d = request["methodDetails"];
24
+ return isObject(d) ? d["invoice"] : undefined;
25
+ }
26
+ async function preimageComplete(unsigned, signature, chosen) {
27
+ if (typeof signature !== "string")
28
+ return refuse("ln/preimage-malformed");
29
+ const signed = unsigned.complete(signature);
30
+ if (isRefusal(signed))
31
+ return signed;
32
+ return withPaymentIdentifier(signed, choiceOf(chosen)?.["required"], chosen.ref);
33
+ }
34
+ /**
35
+ * The x402 Lightning piece: the first option on the account's `lnbtc` network, whose request hash, recomputed from the
36
+ * buyer's own request (the `request` input), equals both the option's `extra.requestHash` and the invoice's description
37
+ * hash. With `named`, the build reads the ATR the gate compared, which rides beside the choice as the bytes the gate
38
+ * passes.
39
+ */
40
+ export function lnbtcPiece(pairing, named) {
41
+ return Object.freeze({
42
+ async choose(read, account, inputs, _now, ref) {
43
+ const o = firstOption(read, account, "lnbtc", pairing);
44
+ if ("refused" in o)
45
+ return refuse("x402/no-payable-option");
46
+ const request = isObject(inputs) ? inputs["request"] : undefined;
47
+ if (request === undefined)
48
+ return refuse("x402/input-missing");
49
+ const resource = isObject(o.required) && isObject(o.required.resource) ? o.required.resource["url"] : undefined;
50
+ const checked = await checkRequestHash(o.accepted, resource, request);
51
+ if (isRefusal(checked))
52
+ return checked;
53
+ return x402Chosen(pairing, { required: o.required, accepted: o.accepted }, ref);
54
+ },
55
+ choice(chosen, bytes) {
56
+ const c = choiceOf(chosen);
57
+ if (c === undefined)
58
+ return refuse("x402/choice-malformed");
59
+ return named ? { ...c, atr: bytes } : c;
60
+ },
61
+ request: (unsigned) => unsigned.request,
62
+ complete: preimageComplete,
63
+ moves: () => true,
64
+ });
65
+ }
66
+ /**
67
+ * The MPP Lightning piece: the first challenge offering the pairing whose invoice's BOLT11 currency is the account's
68
+ * `lnbtc` network's. A session's return invoice is the buyer's input, and rides in the build's choice.
69
+ */
70
+ export function lnMppPiece(pairing, session) {
71
+ return Object.freeze({
72
+ async choose(read, account, inputs, _now, ref) {
73
+ const a = accountOf(account);
74
+ const currency = a === undefined ? undefined : LN_CURRENCY[a.network];
75
+ if (a === undefined || a.namespace !== "lnbtc" || currency === undefined)
76
+ return refuse("mpp/no-payable-option");
77
+ const offer = read.offer;
78
+ const challenges = isObject(offer) && Array.isArray(offer["challenges"]) ? offer["challenges"] : [];
79
+ let challenge;
80
+ for (const c of challenges) {
81
+ if (!offers(c, pairing))
82
+ continue;
83
+ const invoice = invoiceOf(c, session);
84
+ if (typeof invoice !== "string")
85
+ continue;
86
+ const b = await decodeBolt11(invoice);
87
+ if (!isRefusal(b) && b.currency === currency) {
88
+ challenge = c;
89
+ break;
90
+ }
91
+ }
92
+ if (challenge === undefined)
93
+ return refuse("mpp/no-payable-option");
94
+ const given = inputsOf(inputs, pairing, session ? { returnInvoice: "string" } : {});
95
+ if (isRefusal(given))
96
+ return given;
97
+ return { pairing, choice: { challenge: challenge, ...given }, ref };
98
+ },
99
+ choice: (chosen) => choiceOf(chosen) ?? refuse("mpp/choice-malformed"),
100
+ request: (unsigned) => unsigned.request,
101
+ async complete(unsigned, signature) {
102
+ if (typeof signature !== "string")
103
+ return refuse("ln/preimage-malformed");
104
+ return unsigned.complete(signature);
105
+ },
106
+ moves: () => true,
107
+ });
108
+ }
109
+ export const x402ExactLnbtc = lnbtcPiece("x402/exact/lnbtc", false);
110
+ export const x402ExactLnbtcInvoiceNamed = lnbtcPiece("x402/exact/lnbtc/invoice-named", true);
111
+ export const mppChargeLightning = lnMppPiece("mpp/charge/lightning", false);
112
+ export const mppSessionLightning = lnMppPiece("mpp/session/lightning", true);
@@ -0,0 +1,24 @@
1
+ /**
2
+ * x402 `lnbtc`'s request binding, computed by the buyer from its own request: the option's profile and parameters are
3
+ * validated, the binding object is built from the request the buyer gives, and `requestHash` is SHA-256 over the UTF-8
4
+ * of its RFC 8785 form. The option's `extra.requestHash` and the invoice's description hash must both equal it.
5
+ *
6
+ * The buyer's request, the `request` input:
7
+ * - `http:1`: `{method, url, body?, headers?}`, `body` the content bytes as `0x` hex (absent is empty), `headers` the
8
+ * value of each present header by lower-case name, each field line's values joined as RFC 9421 §2.1 joins them.
9
+ * - `mcp:1`: `{server, name, arguments?, meta?}`, `server` the MCP server's URI as the buyer's own configuration spells
10
+ * it, `name` and `arguments` the `tools/call` params, `meta` its `params._meta`.
11
+ */
12
+ import { type Json, type Refusal } from "@integraledger/lcp";
13
+ /**
14
+ * The binding object of the buyer's own `request` under a profile and its parameters, the profile being the one the
15
+ * request's transport uses: `http:1` for an HTTP request, `mcp:1` for a `tools/call`.
16
+ */
17
+ export declare function bindingOf(profile: unknown, params: unknown, request: unknown, resourceUrl: unknown): Promise<Json | Refusal>;
18
+ /** `requestHash`: lower-case hex of SHA-256 over the UTF-8 of the binding object's RFC 8785 form. */
19
+ export declare function requestHashOf(profile: unknown, params: unknown, request: unknown, resourceUrl: unknown): Promise<string | Refusal>;
20
+ /**
21
+ * The request hash of the buyer's own `request` under the option's profile, when the option's `extra.requestHash` and
22
+ * the invoice's description hash both equal it; otherwise the refusal naming what differs.
23
+ */
24
+ export declare function checkRequestHash(option: unknown, resourceUrl: unknown, request: unknown): Promise<string | Refusal>;