@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,194 @@
1
+ import { parseJson } from "@integraledger/lcp";
2
+ import { network, pairingsOfPlaced } from "@integraledger/lcp/mpp";
3
+ import { memoCalldata } from "@integraledger/lcp/tempo";
4
+ import { accountOf, bigintOf, broadcasts, choiceOf, inputsOf, isObject, isRefusal, refuse } from "./common.js";
5
+ const EVM_ADDRESS = /^0x[0-9a-fA-F]{40}$/;
6
+ /** The request a challenge carries, decoded from base64url JSON, or undefined. */
7
+ export function requestOf(c) {
8
+ if (typeof c.request !== "string")
9
+ return undefined;
10
+ try {
11
+ const b64 = c.request.replace(/-/g, "+").replace(/_/g, "/");
12
+ const binary = atob(b64 + "=".repeat((4 - (b64.length % 4)) % 4));
13
+ const bytes = Uint8Array.from(binary, (ch) => ch.charCodeAt(0));
14
+ const v = parseJson(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
15
+ return isObject(v) ? v : undefined;
16
+ }
17
+ catch {
18
+ return undefined;
19
+ }
20
+ }
21
+ /** Whether a placed challenge offers the pairing, by the protocol package's `pairingsOfPlaced`. */
22
+ export function offers(c, pairing) {
23
+ return pairingsOfPlaced(c).some((p) => p === pairing);
24
+ }
25
+ /**
26
+ * The first challenge offering `pairing` on the account's EVM network, as MPP's `network` reads it, with the choice
27
+ * MPP's build takes: the challenge, `from`, `now`, and the buyer's own inputs named in `spec`.
28
+ */
29
+ export function mppChoose(pairing, spec, accept) {
30
+ return (read, account, inputs, now, ref) => {
31
+ const a = accountOf(account);
32
+ if (a === undefined || a.namespace !== "eip155" || !EVM_ADDRESS.test(a.address))
33
+ return refuse("mpp/no-payable-option");
34
+ const offer = read.offer;
35
+ const challenges = isObject(offer) && Array.isArray(offer["challenges"]) ? offer["challenges"] : [];
36
+ const challenge = challenges.find((c) => offers(c, pairing) && network(c) === a.network);
37
+ if (challenge === undefined)
38
+ return refuse("mpp/no-payable-option");
39
+ const refused = accept?.(challenge) ?? null;
40
+ if (refused !== null)
41
+ return refused;
42
+ const given = inputsOf(inputs, pairing, spec);
43
+ if (isRefusal(given))
44
+ return given;
45
+ return { pairing, choice: { challenge: challenge, from: a.address, now, ...given }, ref };
46
+ };
47
+ }
48
+ /** The choice as MPP's build takes it, with `deposit` as a bigint where the choice carries one. */
49
+ export function mppChoice(chosen) {
50
+ const c = choiceOf(chosen);
51
+ if (c === undefined)
52
+ return refuse("mpp/choice-malformed");
53
+ if (c["deposit"] === undefined)
54
+ return c;
55
+ const deposit = bigintOf(c["deposit"]);
56
+ return deposit === undefined ? refuse("mpp/choice-malformed") : { ...c, deposit };
57
+ }
58
+ /** The build's request, exactly as built. */
59
+ export function mppRequest(unsigned) {
60
+ return unsigned.request;
61
+ }
62
+ /** The credential for a one-value answer: a signature, a signed transaction or a transaction hash, as `0x` hex. */
63
+ export async function mppComplete(unsigned, signature) {
64
+ if (typeof signature !== "string")
65
+ return refuse("mpp/credential-malformed");
66
+ return unsigned.complete(signature);
67
+ }
68
+ /** A buyer piece for an MPP charge pairing whose answer is one value. */
69
+ export function mppChargePiece(pairing, spec, accept) {
70
+ return Object.freeze({
71
+ choose: mppChoose(pairing, spec, accept),
72
+ choice: mppChoice,
73
+ request: mppRequest,
74
+ complete: mppComplete,
75
+ moves: broadcasts,
76
+ });
77
+ }
78
+ const MAX_SPLITS = 10;
79
+ const DECIMAL = /^[0-9]{1,78}$/;
80
+ const WORD = /^0x[0-9a-fA-F]{64}$/;
81
+ const TRANSFER_SELECTOR = "a9059cbb";
82
+ /**
83
+ * A Tempo challenge's `methodDetails.splits`: none, or 1–10 entries, each with a recipient address, an amount above
84
+ * zero and an optional 32-byte memo.
85
+ */
86
+ function tempoSplits(c) {
87
+ const details = requestOf(c)?.["methodDetails"];
88
+ const splits = isObject(details) ? details["splits"] : undefined;
89
+ if (splits === undefined)
90
+ return [];
91
+ if (!Array.isArray(splits) || splits.length < 1 || splits.length > MAX_SPLITS)
92
+ return refuse("mpp/splits-malformed");
93
+ const out = [];
94
+ for (const s of splits) {
95
+ if (!isObject(s))
96
+ return refuse("mpp/splits-malformed");
97
+ const { recipient, amount, memo } = s;
98
+ if (typeof recipient !== "string" || !EVM_ADDRESS.test(recipient))
99
+ return refuse("mpp/splits-malformed");
100
+ if (typeof amount !== "string" || !DECIMAL.test(amount) || BigInt(amount) === 0n)
101
+ return refuse("mpp/splits-malformed");
102
+ if (memo !== undefined && (typeof memo !== "string" || !WORD.test(memo)))
103
+ return refuse("mpp/splits-malformed");
104
+ out.push({ recipient: recipient, amount: BigInt(amount), ...(memo !== undefined ? { memo: memo } : {}) });
105
+ }
106
+ return out;
107
+ }
108
+ /** What the primary recipient receives: `amount` less the splits, which must leave more than zero. */
109
+ function primaryAmount(c, splits) {
110
+ const amount = requestOf(c)?.["amount"];
111
+ if (typeof amount !== "string" || !DECIMAL.test(amount))
112
+ return refuse("mpp/request-malformed");
113
+ const primary = BigInt(amount) - splits.reduce((sum, s) => sum + s.amount, 0n);
114
+ return primary > 0n ? primary : refuse("mpp/input-malformed");
115
+ }
116
+ /** Accepts a Tempo challenge whose splits are well formed and leave the primary recipient more than zero. */
117
+ export function acceptSplits(c) {
118
+ const splits = tempoSplits(c);
119
+ if (isRefusal(splits))
120
+ return splits;
121
+ const primary = primaryAmount(c, splits);
122
+ return isRefusal(primary) ? primary : null;
123
+ }
124
+ /** `transfer(address,uint256)` calldata. */
125
+ function transferCalldata(to, amount) {
126
+ return `0x${TRANSFER_SELECTOR}${"0".repeat(24)}${to.slice(2).toLowerCase()}${amount.toString(16).padStart(64, "0")}`;
127
+ }
128
+ /**
129
+ * A Tempo charge's build. Without splits it is the binding's own `tempo-call`. With splits it is `tempo-calls`: the
130
+ * binding's primary call, carrying its attribution memo, for `amount` less the splits, then one call per split in
131
+ * array order, `transferWithMemo` where the split names a memo and `transfer` otherwise, all on `currency`.
132
+ */
133
+ export function tempoBuild(binding) {
134
+ return async (choice, h) => {
135
+ const unsigned = await binding.build(choice, h);
136
+ if (isRefusal(unsigned))
137
+ return unsigned;
138
+ const challenge = choice.challenge;
139
+ const splits = tempoSplits(challenge);
140
+ if (isRefusal(splits) || splits.length === 0)
141
+ return isRefusal(splits) ? splits : unsigned;
142
+ const primary = primaryAmount(challenge, splits);
143
+ if (isRefusal(primary))
144
+ return primary;
145
+ const request = unsigned.request;
146
+ if (request.kind !== "tempo-call")
147
+ return refuse("mpp/request-malformed");
148
+ const recipient = requestOf(challenge)?.["recipient"];
149
+ const memo = `0x${request.call.data.slice(-64)}`;
150
+ const primaryCall = memoCalldata(recipient, primary, memo);
151
+ if (isRefusal(primaryCall))
152
+ return primaryCall;
153
+ const currency = request.call.to;
154
+ const calls = [{ to: currency, data: primaryCall }];
155
+ for (const s of splits) {
156
+ const data = s.memo !== undefined ? memoCalldata(s.recipient, s.amount, s.memo) : transferCalldata(s.recipient, s.amount);
157
+ if (isRefusal(data))
158
+ return data;
159
+ calls.push({ to: currency, data });
160
+ }
161
+ const tempoCalls = {
162
+ kind: "tempo-calls",
163
+ chainId: request.chainId,
164
+ calls,
165
+ validBefore: request.validBefore,
166
+ broadcast: request.broadcast,
167
+ };
168
+ return { request: tempoCalls, complete: (s) => unsigned.complete(s) };
169
+ };
170
+ }
171
+ /**
172
+ * The credential for a pushed Tempo transfer: the answer is `{hash, landed?: {transaction, blockNumber, logs}}`, the
173
+ * hash the signer broadcast and, where the signer gives it, its landed receipt, which carries the memo `bound` reads.
174
+ */
175
+ export async function pushComplete(unsigned, signature) {
176
+ if (!isObject(signature) || typeof signature["hash"] !== "string")
177
+ return refuse("mpp/credential-malformed");
178
+ const credential = unsigned.complete(signature["hash"]);
179
+ if ("refused" in credential)
180
+ return credential;
181
+ const landed = signature["landed"];
182
+ if (landed === undefined)
183
+ return credential;
184
+ if (!isObject(landed) || typeof landed["transaction"] !== "string" || !Array.isArray(landed["logs"])) {
185
+ return refuse("mpp/credential-malformed");
186
+ }
187
+ const blockNumber = bigintOf(landed["blockNumber"]);
188
+ if (blockNumber === undefined)
189
+ return refuse("mpp/credential-malformed");
190
+ return {
191
+ ...credential,
192
+ landed: { transaction: landed["transaction"], blockNumber, logs: landed["logs"] },
193
+ };
194
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * What the protocol-group pieces (card, AP2, UCP, ACP, ACK) share: the buyer's CAIP-10 account, which names the buyer,
3
+ * the object choice revived from `Chosen`, and the piece of a pairing whose build has nothing for the buyer to sign.
4
+ */
5
+ import type { Json, Refusal } from "@integraledger/lcp";
6
+ import type { BuyerPiece, Chosen, Inputs, Read } from "../types.js";
7
+ /** A refusal `<ns>/no-payable-option` unless the account is CAIP-10. */
8
+ export declare function unnamedBuyer(account: string, ns: string): Refusal | undefined;
9
+ /** The choice object of `chosen`, or `<ns>/choice-malformed`. */
10
+ export declare function objectChoice(chosen: Chosen, ns: string): Record<string, unknown> | Refusal;
11
+ /** The pairing's build input as the piece stores it in `Chosen`. */
12
+ export type Choose = (read: Read, account: string, inputs: Inputs, now: number, ref: string, doc?: unknown) => Record<string, Json> | Refusal;
13
+ /**
14
+ * The piece of a pairing whose build refuses `no-signed-place` or `nothing-to-sign`: it chooses, and the gate confirms
15
+ * only. Nothing is handed to a signer, and nothing completes.
16
+ */
17
+ export declare function confirmOnlyPiece(pairing: string, choose: Choose): BuyerPiece;
@@ -0,0 +1,27 @@
1
+ import { accountOf, choiceOf, isRefusal, refuse } from "./common.js";
2
+ /** A refusal `<ns>/no-payable-option` unless the account is CAIP-10. */
3
+ export function unnamedBuyer(account, ns) {
4
+ return accountOf(account) === undefined ? refuse(`${ns}/no-payable-option`) : undefined;
5
+ }
6
+ /** The choice object of `chosen`, or `<ns>/choice-malformed`. */
7
+ export function objectChoice(chosen, ns) {
8
+ return choiceOf(chosen) ?? refuse(`${ns}/choice-malformed`);
9
+ }
10
+ /**
11
+ * The piece of a pairing whose build refuses `no-signed-place` or `nothing-to-sign`: it chooses, and the gate confirms
12
+ * only. Nothing is handed to a signer, and nothing completes.
13
+ */
14
+ export function confirmOnlyPiece(pairing, choose) {
15
+ const ns = pairing.split("/")[0];
16
+ return Object.freeze({
17
+ choose(read, account, inputs, now, ref, doc) {
18
+ const choice = choose(read, account, inputs, now, ref, doc);
19
+ if (isRefusal(choice))
20
+ return choice;
21
+ return { pairing, choice, ref };
22
+ },
23
+ choice: (chosen) => objectChoice(chosen, ns),
24
+ request: () => refuse(`${ns}/nothing-to-sign`),
25
+ complete: async () => refuse(`${ns}/nothing-to-sign`),
26
+ });
27
+ }
@@ -0,0 +1 @@
1
+ export declare const ucpBookingAp2Mandate: import("../types.js").BuyerPiece;
@@ -0,0 +1,3 @@
1
+ /** The buyer piece for `ucp/booking/ap2-mandate`: the signed booking for the buyer's mandate issuer. */
2
+ import { ucpMandatePiece } from "./ucp.js";
3
+ export const ucpBookingAp2Mandate = ucpMandatePiece("ucp/booking/ap2-mandate");
@@ -0,0 +1 @@
1
+ export declare const ucpBookingUnsigned: import("../types.js").BuyerPiece;
@@ -0,0 +1,3 @@
1
+ /** The buyer piece for `ucp/booking/unsigned`: UCP defines no buyer signature here, so the gate confirms only. */
2
+ import { ucpUnsignedPiece } from "./ucp.js";
3
+ export const ucpBookingUnsigned = ucpUnsignedPiece("ucp/booking/unsigned");
@@ -0,0 +1 @@
1
+ export declare const ucpCheckoutAp2Mandate: import("../types.js").BuyerPiece;
@@ -0,0 +1,3 @@
1
+ /** The buyer piece for `ucp/checkout/ap2-mandate`: the signed checkout for the buyer's mandate issuer. */
2
+ import { ucpMandatePiece } from "./ucp.js";
3
+ export const ucpCheckoutAp2Mandate = ucpMandatePiece("ucp/checkout/ap2-mandate");
@@ -0,0 +1 @@
1
+ export declare const ucpCheckoutUnsigned: import("../types.js").BuyerPiece;
@@ -0,0 +1,3 @@
1
+ /** The buyer piece for `ucp/checkout/unsigned`: UCP defines no buyer signature here, so the gate confirms only. */
2
+ import { ucpUnsignedPiece } from "./ucp.js";
3
+ export const ucpCheckoutUnsigned = ucpUnsignedPiece("ucp/checkout/unsigned");
@@ -0,0 +1,5 @@
1
+ import type { BuyerPiece } from "../types.js";
2
+ /** The piece of a UCP pairing under the AP2 Mandates extension. */
3
+ export declare function ucpMandatePiece(pairing: string): BuyerPiece;
4
+ /** The piece of a UCP pairing without the AP2 Mandates extension: the gate confirms only. */
5
+ export declare function ucpUnsignedPiece(pairing: string): BuyerPiece;
@@ -0,0 +1,35 @@
1
+ import { isObject, isRefusal, refuse } from "./common.js";
2
+ import { confirmOnlyPiece, objectChoice, unnamedBuyer } from "./protocol-groups.js";
3
+ function chooseCheckout(read, account, _inputs, _now, _ref) {
4
+ const unnamed = unnamedBuyer(account, "ucp");
5
+ if (unnamed !== undefined)
6
+ return unnamed;
7
+ if (!isObject(read.offer))
8
+ return refuse("ucp/no-payable-option");
9
+ return read.offer;
10
+ }
11
+ /** The piece of a UCP pairing under the AP2 Mandates extension. */
12
+ export function ucpMandatePiece(pairing) {
13
+ return Object.freeze({
14
+ choose(read, account, inputs, now, ref) {
15
+ const choice = chooseCheckout(read, account, inputs, now, ref);
16
+ if (isRefusal(choice))
17
+ return choice;
18
+ return { pairing, choice, ref };
19
+ },
20
+ choice: (chosen) => objectChoice(chosen, "ucp"),
21
+ request: (unsigned) => ({
22
+ kind: "ucp-checkout",
23
+ checkout: unsigned.checkout,
24
+ }),
25
+ async complete(unsigned, signature) {
26
+ if (typeof signature !== "string" || signature === "")
27
+ return refuse("ucp/signature-malformed");
28
+ return unsigned.complete(signature);
29
+ },
30
+ });
31
+ }
32
+ /** The piece of a UCP pairing without the AP2 Mandates extension: the gate confirms only. */
33
+ export function ucpUnsignedPiece(pairing) {
34
+ return confirmOnlyPiece(pairing, chooseCheckout);
35
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * What the x402 pieces on non-EVM rails share: the option on the account's network, with the account's address
3
+ * percent-decoded as CAIP-10 writes it, and the piece for pairings where the payer's wallet builds and signs the
4
+ * rail's own payment around what the build hands it.
5
+ */
6
+ import type { Refusal } from "@integraledger/lcp";
7
+ import type { PaymentRequired, PaymentRequirements } from "@integraledger/lcp/x402";
8
+ import type { BuyerPiece, Chosen, Presented, Read } from "../types.js";
9
+ /**
10
+ * The first option on the account's network, for an account in one of `namespaces` whose decoded address passes
11
+ * `address`. The address is returned decoded, as `accountOf` gives it.
12
+ */
13
+ export declare function railOption(read: Read, account: string, namespaces: readonly string[], pairing: string, address: (a: string) => boolean): {
14
+ required: PaymentRequired;
15
+ accepted: PaymentRequirements;
16
+ address: string;
17
+ } | Refusal;
18
+ /** The x402 payment with the payment identifier appended where the challenge advertises it. */
19
+ export declare function identified(signed: unknown, chosen: Chosen): Presented | Refusal;
20
+ /**
21
+ * The buyer piece for an x402 pairing whose build takes only the challenge and the chosen option, and whose request
22
+ * the payer's wallet answers with the payment's own fields: `fields`, each a string, passed to the build's `complete`
23
+ * as one object.
24
+ */
25
+ export declare function walletBuiltPiece(pairing: string, namespaces: readonly string[], address: (a: string) => boolean, fields: readonly string[]): BuyerPiece;
@@ -0,0 +1,52 @@
1
+ import { accountOf, choiceOf, firstOption, isObject, isRefusal, refuse, withPaymentIdentifier, x402Chosen } from "./common.js";
2
+ const NO_OPTION = "x402/no-payable-option";
3
+ /**
4
+ * The first option on the account's network, for an account in one of `namespaces` whose decoded address passes
5
+ * `address`. The address is returned decoded, as `accountOf` gives it.
6
+ */
7
+ export function railOption(read, account, namespaces, pairing, address) {
8
+ const a = accountOf(account);
9
+ if (a === undefined || !namespaces.includes(a.namespace))
10
+ return refuse(NO_OPTION);
11
+ if (!address(a.address))
12
+ return refuse(NO_OPTION);
13
+ const o = firstOption(read, account, a.namespace, pairing);
14
+ if (isRefusal(o))
15
+ return refuse(NO_OPTION);
16
+ return { required: o.required, accepted: o.accepted, address: o.address };
17
+ }
18
+ /** The x402 payment with the payment identifier appended where the challenge advertises it. */
19
+ export function identified(signed, chosen) {
20
+ if (isRefusal(signed))
21
+ return signed;
22
+ return withPaymentIdentifier(signed, choiceOf(chosen)?.["required"], chosen.ref);
23
+ }
24
+ /**
25
+ * The buyer piece for an x402 pairing whose build takes only the challenge and the chosen option, and whose request
26
+ * the payer's wallet answers with the payment's own fields: `fields`, each a string, passed to the build's `complete`
27
+ * as one object.
28
+ */
29
+ export function walletBuiltPiece(pairing, namespaces, address, fields) {
30
+ return Object.freeze({
31
+ choose(read, account, _inputs, _now, ref) {
32
+ const o = railOption(read, account, namespaces, pairing, address);
33
+ if (isRefusal(o))
34
+ return o;
35
+ return x402Chosen(pairing, { required: o.required, accepted: o.accepted }, ref);
36
+ },
37
+ choice: (chosen) => choiceOf(chosen) ?? refuse("x402/choice-malformed"),
38
+ request: (unsigned) => unsigned.request,
39
+ async complete(unsigned, signature, chosen) {
40
+ if (!isObject(signature))
41
+ return refuse("x402/signature-malformed");
42
+ const signed = {};
43
+ for (const f of fields) {
44
+ const v = signature[f];
45
+ if (typeof v !== "string")
46
+ return refuse("x402/signature-malformed");
47
+ signed[f] = v;
48
+ }
49
+ return identified(await unsigned.complete(signed), chosen);
50
+ },
51
+ });
52
+ }
@@ -0,0 +1 @@
1
+ export declare const x402AuthCaptureEip155Eip3009: import("../types.js").BuyerPiece;
@@ -0,0 +1,3 @@
1
+ /** The buyer piece for `x402/auth-capture/eip155/eip3009`. */
2
+ import { evmX402Piece } from "./evm-x402.js";
3
+ export const x402AuthCaptureEip155Eip3009 = evmX402Piece("x402/auth-capture/eip155/eip3009");
@@ -0,0 +1 @@
1
+ export declare const x402AuthCaptureEip155Permit2: import("../types.js").BuyerPiece;
@@ -0,0 +1,3 @@
1
+ /** The buyer piece for `x402/auth-capture/eip155/permit2`. */
2
+ import { evmX402Piece } from "./evm-x402.js";
3
+ export const x402AuthCaptureEip155Permit2 = evmX402Piece("x402/auth-capture/eip155/permit2");
@@ -0,0 +1,2 @@
1
+ import type { BuyerPiece } from "../types.js";
2
+ export declare const x402BatchSettlementCloudflare: BuyerPiece;
@@ -0,0 +1,21 @@
1
+ import { choiceOf, isObject, refuse, withPaymentIdentifier, x402Chosen, x402OfferOf } from "./common.js";
2
+ const PAIRING = "x402/batch-settlement/cloudflare";
3
+ const NETWORK = "cloudflare:402";
4
+ /** `cloudflare:402:` and a CAIP-10 account reference naming the agent. */
5
+ const ACCOUNT = /^cloudflare:402:[-.%a-zA-Z0-9]{1,128}$/;
6
+ export const x402BatchSettlementCloudflare = Object.freeze({
7
+ choose(read, account, _inputs, _now, ref) {
8
+ const offer = x402OfferOf(read);
9
+ if (typeof account !== "string" || !ACCOUNT.test(account) || offer === undefined)
10
+ return refuse("x402/no-payable-option");
11
+ const accepted = offer.options.find((o) => isObject(o) && o.network === NETWORK);
12
+ if (accepted === undefined)
13
+ return refuse("x402/no-payable-option");
14
+ return x402Chosen(PAIRING, { required: offer.required, accepted }, ref);
15
+ },
16
+ choice: (chosen) => choiceOf(chosen) ?? refuse("x402/choice-malformed"),
17
+ request: () => null,
18
+ async complete(unsigned, _signature, chosen) {
19
+ return withPaymentIdentifier(unsigned, choiceOf(chosen)?.["required"], chosen.ref);
20
+ },
21
+ });
@@ -0,0 +1,2 @@
1
+ import type { BuyerPiece } from "../types.js";
2
+ export declare const x402BatchSettlementEip155: BuyerPiece;
@@ -0,0 +1,32 @@
1
+ import { batchComplete, batchRequest, depositOf, optionalInputs, randomSalt32, reviveDecimals } from "./batch.js";
2
+ import { firstOption, isRefusal, refuse, x402Chosen } from "./common.js";
3
+ const PAIRING = "x402/batch-settlement/eip155";
4
+ const EVM_ADDRESS = /^0x[0-9a-fA-F]{40}$/;
5
+ const HASH32 = /^0x[0-9a-fA-F]{64}$/;
6
+ export const x402BatchSettlementEip155 = Object.freeze({
7
+ choose(read, account, inputs, now, ref) {
8
+ const o = firstOption(read, account, "eip155", PAIRING, EVM_ADDRESS);
9
+ if ("refused" in o)
10
+ return refuse("x402/no-payable-option");
11
+ const given = optionalInputs(inputs, "x402", { payerAuthorizer: EVM_ADDRESS, authSalt: HASH32 });
12
+ if (isRefusal(given))
13
+ return given;
14
+ if (given["payerAuthorizer"] === undefined)
15
+ return refuse("x402/input-missing");
16
+ const deposit = depositOf(o.accepted, inputs);
17
+ if (isRefusal(deposit))
18
+ return deposit;
19
+ return x402Chosen(PAIRING, {
20
+ required: o.required,
21
+ accepted: o.accepted,
22
+ from: o.address,
23
+ now,
24
+ payerAuthorizer: given["payerAuthorizer"],
25
+ deposit,
26
+ authSalt: given["authSalt"] ?? randomSalt32(),
27
+ }, ref);
28
+ },
29
+ choice: (chosen) => reviveDecimals(chosen, ["deposit"]),
30
+ request: batchRequest,
31
+ complete: batchComplete,
32
+ });
@@ -0,0 +1,2 @@
1
+ import type { BuyerPiece } from "../types.js";
2
+ export declare const x402BatchSettlementSolana: BuyerPiece;
@@ -0,0 +1,55 @@
1
+ import { batchComplete, batchRequest, depositOf, optionalInputs, randomU64, reviveDecimals } from "./batch.js";
2
+ import { firstOption, inputsOf, isObject, isRefusal, refuse, uintOf, x402Chosen } from "./common.js";
3
+ const PAIRING = "x402/batch-settlement/solana";
4
+ const KEY = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
5
+ const U64 = /^[0-9]{1,20}$/;
6
+ const U64_LIMIT = 1n << 64n;
7
+ const isU64 = (v) => typeof v === "string" && U64.test(v) && BigInt(v) < U64_LIMIT;
8
+ export const x402BatchSettlementSolana = Object.freeze({
9
+ choose(read, account, inputs, now, ref) {
10
+ const o = firstOption(read, account, "solana", PAIRING, KEY);
11
+ if ("refused" in o)
12
+ return refuse("x402/no-payable-option");
13
+ const extra = isObject(o.accepted.extra) ? o.accepted.extra : {};
14
+ const offered = extra["recentBlockhash"];
15
+ const required = inputsOf(inputs, PAIRING, {
16
+ payerAuthorizer: "string",
17
+ openSlot: "decimal",
18
+ tokenProgram: "string",
19
+ ...(offered === undefined ? { recentBlockhash: "string" } : {}),
20
+ computeUnitLimit: "optional-uint",
21
+ computeUnitPrice: "optional-decimal",
22
+ });
23
+ if (isRefusal(required))
24
+ return required;
25
+ const salt = optionalInputs(inputs, "x402", { salt: U64 });
26
+ if (isRefusal(salt))
27
+ return salt;
28
+ const blockhash = offered ?? required["recentBlockhash"];
29
+ if (typeof blockhash !== "string" || !KEY.test(blockhash))
30
+ return refuse("x402/input-missing");
31
+ if (!KEY.test(String(required["payerAuthorizer"])) || !isU64(required["openSlot"]))
32
+ return refuse("x402/input-missing");
33
+ if (salt["salt"] !== undefined && !isU64(salt["salt"]))
34
+ return refuse("x402/input-missing");
35
+ if (required["computeUnitLimit"] !== undefined && uintOf(required["computeUnitLimit"]) === undefined) {
36
+ return refuse("x402/input-missing");
37
+ }
38
+ const deposit = depositOf(o.accepted, inputs);
39
+ if (isRefusal(deposit))
40
+ return deposit;
41
+ return x402Chosen(PAIRING, {
42
+ required: o.required,
43
+ accepted: o.accepted,
44
+ payer: o.address,
45
+ now,
46
+ ...required,
47
+ recentBlockhash: blockhash,
48
+ deposit,
49
+ salt: salt["salt"] ?? randomU64(),
50
+ }, ref);
51
+ },
52
+ choice: (chosen) => reviveDecimals(chosen, ["deposit", "salt", "openSlot", "computeUnitPrice"]),
53
+ request: batchRequest,
54
+ complete: batchComplete,
55
+ });
@@ -0,0 +1 @@
1
+ export declare const x402ExactAlgorand: import("../types.js").BuyerPiece;
@@ -0,0 +1,37 @@
1
+ import { bigintOf, inputsOf, isObject, isRefusal, refuse } from "./common.js";
2
+ import { railPiece, signature64 } from "./x402-rail.js";
3
+ const PAIRING = "x402/exact/algorand";
4
+ function inputs(_accepted, given) {
5
+ const read = inputsOf(given, PAIRING, { params: "object" });
6
+ if (isRefusal(read))
7
+ return read;
8
+ const params = inputsOf(read["params"], PAIRING, {
9
+ firstValid: "decimal",
10
+ genesisHash: "string",
11
+ genesisId: "string",
12
+ minFee: "decimal",
13
+ feePerByte: "decimal",
14
+ });
15
+ return isRefusal(params) ? params : { params };
16
+ }
17
+ function revive(c) {
18
+ const p = c["params"];
19
+ if (!isObject(p))
20
+ return refuse("x402/choice-malformed");
21
+ const firstValid = bigintOf(p["firstValid"]);
22
+ const minFee = bigintOf(p["minFee"]);
23
+ const feePerByte = bigintOf(p["feePerByte"]);
24
+ if (firstValid === undefined || minFee === undefined || feePerByte === undefined)
25
+ return refuse("x402/choice-malformed");
26
+ return { ...c, params: { ...p, firstValid, minFee, feePerByte } };
27
+ }
28
+ export const x402ExactAlgorand = railPiece({
29
+ pairing: PAIRING,
30
+ namespace: "algorand",
31
+ address: /^[A-Z2-7]{58}$/,
32
+ payer: "payer",
33
+ now: false,
34
+ inputs,
35
+ revive,
36
+ answer: signature64,
37
+ });
@@ -0,0 +1 @@
1
+ export declare const x402ExactAptos: import("../types.js").BuyerPiece;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * The buyer piece for `x402/exact/aptos`: the wallet builds and signs the scheme's own payment for the request's
3
+ * option and answers `{transaction}`.
4
+ */
5
+ import { walletBuiltPiece } from "./x402-account-rails.js";
6
+ const APTOS_ADDRESS = /^0x[0-9a-fA-F]{1,64}$/;
7
+ export const x402ExactAptos = walletBuiltPiece("x402/exact/aptos", ["aptos"], (a) => APTOS_ADDRESS.test(a), [
8
+ "transaction",
9
+ ]);
@@ -0,0 +1 @@
1
+ export declare const x402ExactCardano: import("../types.js").BuyerPiece;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * The buyer piece for `x402/exact/cardano`: the wallet builds the scheme's payment with a TTL, attaches the request's
3
+ * `auxiliaryData` and its hash, signs without broadcasting, and answers `{transaction, nonce}`.
4
+ */
5
+ import { walletBuiltPiece } from "./x402-account-rails.js";
6
+ const BECH32_ADDRESS = /^[a-z_]{1,83}1[02-9ac-hj-np-z]{6,}$/;
7
+ export const x402ExactCardano = walletBuiltPiece("x402/exact/cardano", ["cardano", "cip34"], (a) => BECH32_ADDRESS.test(a), ["transaction", "nonce"]);
@@ -0,0 +1,2 @@
1
+ import type { BuyerPiece } from "../types.js";
2
+ export declare const x402ExactCasper: BuyerPiece;
@@ -0,0 +1,21 @@
1
+ import { choiceOf, firstOption, isObject, refuse, withPaymentIdentifier, x402Chosen } from "./common.js";
2
+ const ID = "x402/exact/casper";
3
+ export const x402ExactCasper = Object.freeze({
4
+ choose(read, account, _inputs, now, ref) {
5
+ const o = firstOption(read, account, "casper", ID);
6
+ if ("refused" in o)
7
+ return refuse("x402/no-payable-option");
8
+ return x402Chosen(ID, { required: o.required, accepted: o.accepted, from: o.address, now }, ref);
9
+ },
10
+ choice: (chosen) => choiceOf(chosen) ?? refuse("x402/choice-malformed"),
11
+ request: (unsigned) => unsigned.request,
12
+ async complete(unsigned, signature, chosen) {
13
+ if (!isObject(signature) || typeof signature["publicKey"] !== "string" || typeof signature["signature"] !== "string") {
14
+ return refuse("x402/signature-malformed");
15
+ }
16
+ const signed = unsigned.complete(signature["publicKey"], signature["signature"]);
17
+ if ("refused" in signed)
18
+ return signed;
19
+ return withPaymentIdentifier(signed, choiceOf(chosen)?.["required"], chosen.ref);
20
+ },
21
+ });
@@ -0,0 +1,2 @@
1
+ import type { BuyerPiece } from "../types.js";
2
+ export declare const x402ExactCcd: BuyerPiece;