mppx 0.6.15 → 0.6.17

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 (70) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/Challenge.d.ts +5 -11
  3. package/dist/Challenge.d.ts.map +1 -1
  4. package/dist/Challenge.js +15 -9
  5. package/dist/Challenge.js.map +1 -1
  6. package/dist/Credential.d.ts +2 -2
  7. package/dist/Credential.d.ts.map +1 -1
  8. package/dist/Credential.js +31 -16
  9. package/dist/Credential.js.map +1 -1
  10. package/dist/Receipt.d.ts +0 -5
  11. package/dist/Receipt.d.ts.map +1 -1
  12. package/dist/server/Mppx.d.ts.map +1 -1
  13. package/dist/server/Mppx.js +19 -9
  14. package/dist/server/Mppx.js.map +1 -1
  15. package/dist/stripe/server/internal/html.gen.d.ts +1 -1
  16. package/dist/stripe/server/internal/html.gen.d.ts.map +1 -1
  17. package/dist/stripe/server/internal/html.gen.js +1 -1
  18. package/dist/stripe/server/internal/html.gen.js.map +1 -1
  19. package/dist/tempo/Proof.d.ts +5 -0
  20. package/dist/tempo/Proof.d.ts.map +1 -1
  21. package/dist/tempo/Proof.js +5 -1
  22. package/dist/tempo/Proof.js.map +1 -1
  23. package/dist/tempo/internal/fee-token.d.ts +7 -0
  24. package/dist/tempo/internal/fee-token.d.ts.map +1 -0
  25. package/dist/tempo/internal/fee-token.js +44 -0
  26. package/dist/tempo/internal/fee-token.js.map +1 -0
  27. package/dist/tempo/internal/proof.d.ts +2 -2
  28. package/dist/tempo/internal/proof.d.ts.map +1 -1
  29. package/dist/tempo/internal/proof.js +2 -2
  30. package/dist/tempo/internal/proof.js.map +1 -1
  31. package/dist/tempo/server/Charge.d.ts.map +1 -1
  32. package/dist/tempo/server/Charge.js +57 -17
  33. package/dist/tempo/server/Charge.js.map +1 -1
  34. package/dist/tempo/server/Session.d.ts.map +1 -1
  35. package/dist/tempo/server/Session.js +2 -0
  36. package/dist/tempo/server/Session.js.map +1 -1
  37. package/dist/tempo/server/internal/html.gen.d.ts +1 -1
  38. package/dist/tempo/server/internal/html.gen.d.ts.map +1 -1
  39. package/dist/tempo/server/internal/html.gen.js +1 -1
  40. package/dist/tempo/server/internal/html.gen.js.map +1 -1
  41. package/dist/tempo/session/Chain.d.ts +4 -0
  42. package/dist/tempo/session/Chain.d.ts.map +1 -1
  43. package/dist/tempo/session/Chain.js +19 -35
  44. package/dist/tempo/session/Chain.js.map +1 -1
  45. package/package.json +3 -3
  46. package/src/Challenge.test.ts +25 -9
  47. package/src/Challenge.ts +21 -10
  48. package/src/Credential.test.ts +64 -2
  49. package/src/Credential.ts +35 -19
  50. package/src/middlewares/hono.test.ts +2 -2
  51. package/src/proxy/Proxy.test.ts +2 -2
  52. package/src/server/Mppx.test.ts +28 -8
  53. package/src/server/Mppx.ts +23 -9
  54. package/src/stripe/server/internal/html/node_modules/.bin/mppx +22 -0
  55. package/src/stripe/server/internal/html/node_modules/.bin/mppx.src +3 -2
  56. package/src/stripe/server/internal/html.gen.ts +1 -1
  57. package/src/tempo/Proof.test-d.ts +4 -0
  58. package/src/tempo/Proof.test.ts +9 -0
  59. package/src/tempo/Proof.ts +6 -1
  60. package/src/tempo/internal/fee-token.test.ts +123 -0
  61. package/src/tempo/internal/fee-token.ts +51 -0
  62. package/src/tempo/internal/proof.test.ts +4 -4
  63. package/src/tempo/internal/proof.ts +2 -2
  64. package/src/tempo/server/Charge.test.ts +494 -2
  65. package/src/tempo/server/Charge.ts +61 -17
  66. package/src/tempo/server/Session.ts +2 -0
  67. package/src/tempo/server/internal/html/node_modules/.bin/mppx +22 -0
  68. package/src/tempo/server/internal/html/node_modules/.bin/mppx.src +3 -2
  69. package/src/tempo/server/internal/html.gen.ts +1 -1
  70. package/src/tempo/session/Chain.ts +54 -42
@@ -17,7 +17,6 @@ import {
17
17
  sendRawTransaction,
18
18
  sendRawTransactionSync,
19
19
  signTransaction,
20
- writeContract,
21
20
  } from 'viem/actions'
22
21
  import { Transaction } from 'viem/tempo'
23
22
 
@@ -25,6 +24,7 @@ import { BadRequestError, ChannelClosedError, VerificationFailedError } from '..
25
24
  import * as TempoAddress from '../internal/address.js'
26
25
  import * as defaults from '../internal/defaults.js'
27
26
  import * as FeePayer from '../internal/fee-payer.js'
27
+ import { resolveFeeToken } from '../internal/fee-token.js'
28
28
  import * as Channel from './Channel.js'
29
29
  import { escrowAbi } from './escrow.abi.js'
30
30
  import type { SignedVoucher } from './Types.js'
@@ -94,16 +94,14 @@ function assertUint128(amount: bigint): void {
94
94
  }
95
95
  }
96
96
 
97
- function isTempoAccessKeyAccount(
98
- account: Account,
99
- ): account is Account & { accessKeyAddress: Address } {
100
- return 'accessKeyAddress' in account && typeof account.accessKeyAddress === 'string'
101
- }
102
-
103
97
  /** Options for {@link settleOnChain}. */
104
98
  export type SettleOptions =
105
- | { feePayer: Account; account: Account }
106
- | { feePayer?: undefined; account?: Account | undefined }
99
+ | { candidateFeeTokens?: readonly Address[] | undefined; feePayer: Account; account: Account }
100
+ | {
101
+ candidateFeeTokens?: readonly Address[] | undefined
102
+ feePayer?: undefined
103
+ account?: Account | undefined
104
+ }
107
105
 
108
106
  /**
109
107
  * Submit a settle transaction on-chain.
@@ -123,31 +121,38 @@ export async function settleOnChain(
123
121
  const args = [voucher.channelId, voucher.cumulativeAmount, voucher.signature] as const
124
122
  if (options?.feePayer) {
125
123
  const data = encodeFunctionData({ abi: escrowAbi, functionName: 'settle', args })
126
- return sendFeePayerTx(client, resolved, options.feePayer, escrowContract, data, 'settle')
127
- }
128
- if (isTempoAccessKeyAccount(resolved)) {
129
- return sendAccountTx(
124
+ return sendFeePayerTx(
130
125
  client,
131
126
  resolved,
127
+ options.feePayer,
132
128
  escrowContract,
133
- encodeFunctionData({ abi: escrowAbi, functionName: 'settle', args }),
129
+ data,
134
130
  'settle',
131
+ options.candidateFeeTokens,
135
132
  )
136
133
  }
137
- return writeContract(client, {
138
- account: resolved,
139
- chain: client.chain,
140
- address: escrowContract,
141
- abi: escrowAbi,
142
- functionName: 'settle',
143
- args,
144
- })
134
+ return sendAccountTx(
135
+ client,
136
+ resolved,
137
+ escrowContract,
138
+ encodeFunctionData({ abi: escrowAbi, functionName: 'settle', args }),
139
+ 'settle',
140
+ options?.candidateFeeTokens,
141
+ )
145
142
  }
146
143
 
147
144
  /** Options for {@link closeOnChain}. */
148
145
  export type CloseOptions =
149
- | { feePayer: Account; account: Account }
150
- | { feePayer?: undefined; account?: Account | undefined }
146
+ | {
147
+ candidateFeeTokens?: readonly Address[] | undefined
148
+ feePayer: Account
149
+ account: Account
150
+ }
151
+ | {
152
+ candidateFeeTokens?: readonly Address[] | undefined
153
+ feePayer?: undefined
154
+ account?: Account | undefined
155
+ }
151
156
 
152
157
  /**
153
158
  * Submit a close transaction on-chain.
@@ -167,25 +172,24 @@ export async function closeOnChain(
167
172
  const args = [voucher.channelId, voucher.cumulativeAmount, voucher.signature] as const
168
173
  if (options?.feePayer) {
169
174
  const data = encodeFunctionData({ abi: escrowAbi, functionName: 'close', args })
170
- return sendFeePayerTx(client, resolved, options.feePayer, escrowContract, data, 'close')
171
- }
172
- if (isTempoAccessKeyAccount(resolved)) {
173
- return sendAccountTx(
175
+ return sendFeePayerTx(
174
176
  client,
175
177
  resolved,
178
+ options.feePayer,
176
179
  escrowContract,
177
- encodeFunctionData({ abi: escrowAbi, functionName: 'close', args }),
180
+ data,
178
181
  'close',
182
+ options.candidateFeeTokens,
179
183
  )
180
184
  }
181
- return writeContract(client, {
182
- account: resolved,
183
- chain: client.chain,
184
- address: escrowContract,
185
- abi: escrowAbi,
186
- functionName: 'close',
187
- args,
188
- })
185
+ return sendAccountTx(
186
+ client,
187
+ resolved,
188
+ escrowContract,
189
+ encodeFunctionData({ abi: escrowAbi, functionName: 'close', args }),
190
+ 'close',
191
+ options?.candidateFeeTokens,
192
+ )
189
193
  }
190
194
 
191
195
  async function sendAccountTx(
@@ -194,10 +198,17 @@ async function sendAccountTx(
194
198
  to: Address,
195
199
  data: Hex,
196
200
  label: string,
201
+ candidateFeeTokens?: readonly Address[] | undefined,
197
202
  ): Promise<Hex> {
203
+ const feeToken = await resolveFeeToken({
204
+ account: account.address,
205
+ candidateTokens: candidateFeeTokens,
206
+ client,
207
+ })
198
208
  const prepared = await prepareTransactionRequest(client, {
199
209
  account,
200
210
  calls: [{ to, data }],
211
+ ...(feeToken ? { feeToken } : {}),
201
212
  } as never)
202
213
  prepared.gas = prepared.gas! + 5_000n
203
214
 
@@ -236,12 +247,13 @@ async function sendFeePayerTx(
236
247
  to: Address,
237
248
  data: Hex,
238
249
  label: string,
250
+ candidateFeeTokens?: readonly Address[] | undefined,
239
251
  ): Promise<Hex> {
240
- // Resolve the fee token for this chain so the tx pays gas in the correct
241
- // token. `feePayer: true` tells the prepare hook to use expiring nonces but
242
- // does NOT set feeToken automatically, so we must provide it explicitly.
243
- const chainId = client.chain?.id
244
- const feeToken = chainId ? defaults.resolveCurrency({ chainId }) : undefined
252
+ const feeToken = await resolveFeeToken({
253
+ account: feePayer.address,
254
+ candidateTokens: candidateFeeTokens,
255
+ client,
256
+ })
245
257
 
246
258
  const prepared = await prepareTransactionRequest(client, {
247
259
  account,