mppx 0.5.11 → 0.5.12

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 (100) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/cli/cli.d.ts.map +1 -1
  3. package/dist/cli/cli.js +41 -16
  4. package/dist/cli/cli.js.map +1 -1
  5. package/dist/cli/config.d.ts +6 -4
  6. package/dist/cli/config.d.ts.map +1 -1
  7. package/dist/cli/config.js.map +1 -1
  8. package/dist/cli/internal.d.ts +8 -0
  9. package/dist/cli/internal.d.ts.map +1 -1
  10. package/dist/cli/internal.js +33 -3
  11. package/dist/cli/internal.js.map +1 -1
  12. package/dist/cli/plugins/plugin.d.ts +2 -0
  13. package/dist/cli/plugins/plugin.d.ts.map +1 -1
  14. package/dist/cli/plugins/stripe.d.ts.map +1 -1
  15. package/dist/cli/plugins/stripe.js +3 -0
  16. package/dist/cli/plugins/stripe.js.map +1 -1
  17. package/dist/cli/plugins/tempo.d.ts.map +1 -1
  18. package/dist/cli/plugins/tempo.js +3 -0
  19. package/dist/cli/plugins/tempo.js.map +1 -1
  20. package/dist/client/Mppx.d.ts +10 -1
  21. package/dist/client/Mppx.d.ts.map +1 -1
  22. package/dist/client/Mppx.js +17 -5
  23. package/dist/client/Mppx.js.map +1 -1
  24. package/dist/client/Transport.d.ts +2 -0
  25. package/dist/client/Transport.d.ts.map +1 -1
  26. package/dist/client/Transport.js +11 -0
  27. package/dist/client/Transport.js.map +1 -1
  28. package/dist/client/internal/Fetch.d.ts +3 -0
  29. package/dist/client/internal/Fetch.d.ts.map +1 -1
  30. package/dist/client/internal/Fetch.js +65 -19
  31. package/dist/client/internal/Fetch.js.map +1 -1
  32. package/dist/internal/AcceptPayment.d.ts +72 -0
  33. package/dist/internal/AcceptPayment.d.ts.map +1 -0
  34. package/dist/internal/AcceptPayment.js +185 -0
  35. package/dist/internal/AcceptPayment.js.map +1 -0
  36. package/dist/mcp-sdk/client/McpClient.d.ts.map +1 -1
  37. package/dist/mcp-sdk/client/McpClient.js +8 -4
  38. package/dist/mcp-sdk/client/McpClient.js.map +1 -1
  39. package/dist/server/Mppx.d.ts +1 -1
  40. package/dist/server/Mppx.d.ts.map +1 -1
  41. package/dist/server/Mppx.js +33 -24
  42. package/dist/server/Mppx.js.map +1 -1
  43. package/dist/stripe/internal/constants.d.ts +8 -0
  44. package/dist/stripe/internal/constants.d.ts.map +1 -0
  45. package/dist/stripe/internal/constants.js +8 -0
  46. package/dist/stripe/internal/constants.js.map +1 -0
  47. package/dist/stripe/server/Charge.d.ts.map +1 -1
  48. package/dist/stripe/server/Charge.js +23 -5
  49. package/dist/stripe/server/Charge.js.map +1 -1
  50. package/dist/tempo/Proof.d.ts +12 -0
  51. package/dist/tempo/Proof.d.ts.map +1 -0
  52. package/dist/tempo/Proof.js +10 -0
  53. package/dist/tempo/Proof.js.map +1 -0
  54. package/dist/tempo/index.d.ts +1 -0
  55. package/dist/tempo/index.d.ts.map +1 -1
  56. package/dist/tempo/index.js +1 -0
  57. package/dist/tempo/index.js.map +1 -1
  58. package/dist/tempo/internal/fee-payer.d.ts.map +1 -1
  59. package/dist/tempo/internal/fee-payer.js +5 -1
  60. package/dist/tempo/internal/fee-payer.js.map +1 -1
  61. package/dist/tempo/server/Charge.d.ts.map +1 -1
  62. package/dist/tempo/server/Charge.js +62 -3
  63. package/dist/tempo/server/Charge.js.map +1 -1
  64. package/dist/tempo/server/internal/html.gen.d.ts +1 -1
  65. package/dist/tempo/server/internal/html.gen.d.ts.map +1 -1
  66. package/dist/tempo/server/internal/html.gen.js +1 -1
  67. package/dist/tempo/server/internal/html.gen.js.map +1 -1
  68. package/package.json +2 -2
  69. package/src/cli/cli.test.ts +278 -0
  70. package/src/cli/cli.ts +47 -16
  71. package/src/cli/config.ts +10 -4
  72. package/src/cli/internal.ts +59 -3
  73. package/src/cli/plugins/plugin.ts +3 -0
  74. package/src/cli/plugins/stripe.ts +3 -0
  75. package/src/cli/plugins/tempo.ts +3 -0
  76. package/src/client/Mppx.test-d.ts +33 -0
  77. package/src/client/Mppx.test.ts +130 -1
  78. package/src/client/Mppx.ts +35 -5
  79. package/src/client/Transport.test.ts +88 -55
  80. package/src/client/Transport.ts +13 -0
  81. package/src/client/internal/Fetch.browser.test.ts +16 -13
  82. package/src/client/internal/Fetch.test.ts +307 -10
  83. package/src/client/internal/Fetch.ts +85 -19
  84. package/src/internal/AcceptPayment.test.ts +211 -0
  85. package/src/internal/AcceptPayment.ts +304 -0
  86. package/src/mcp-sdk/client/McpClient.ts +11 -5
  87. package/src/server/Mppx.test.ts +141 -44
  88. package/src/server/Mppx.ts +43 -23
  89. package/src/stripe/internal/constants.ts +7 -0
  90. package/src/stripe/server/Charge.ts +22 -4
  91. package/src/tempo/Proof.test-d.ts +13 -0
  92. package/src/tempo/Proof.test.ts +31 -0
  93. package/src/tempo/Proof.ts +13 -0
  94. package/src/tempo/client/SessionManager.test.ts +4 -7
  95. package/src/tempo/index.ts +1 -0
  96. package/src/tempo/internal/fee-payer.test.ts +1 -1
  97. package/src/tempo/internal/fee-payer.ts +5 -1
  98. package/src/tempo/server/Charge.test.ts +123 -0
  99. package/src/tempo/server/Charge.ts +74 -1
  100. package/src/tempo/server/internal/html.gen.ts +1 -1
@@ -127,6 +127,50 @@ describe('request handler', () => {
127
127
  expect(body.detail).not.toContain('rpc.example.com')
128
128
  })
129
129
 
130
+ test('returns 402 when challenge ID mismatch', async () => {
131
+ const wrongChallenge = Challenge.from({
132
+ id: 'wrong-id',
133
+ intent: 'charge',
134
+ method: 'tempo',
135
+ realm,
136
+ request: { amount: '1000', currency: asset, recipient: accounts[0].address },
137
+ })
138
+ const credential = Credential.from({
139
+ challenge: wrongChallenge,
140
+ payload: { signature: '0x123', type: 'transaction' },
141
+ })
142
+
143
+ const request = new Request('https://example.com/resource', {
144
+ headers: { Authorization: Credential.serialize(credential) },
145
+ })
146
+
147
+ const result = await Mppx.create({ methods: [method], realm, secretKey }).charge({
148
+ amount: '1000',
149
+ currency: asset,
150
+ expires: new Date(Date.now() + 60_000).toISOString(),
151
+ recipient: accounts[0].address,
152
+ })(request)
153
+
154
+ expect(result.status).toBe(402)
155
+ if (result.status !== 402) throw new Error()
156
+
157
+ const body = (await result.challenge.json()) as object
158
+ expect({
159
+ ...body,
160
+ challengeId: '[challengeId]',
161
+ instance: '[instance]',
162
+ }).toMatchInlineSnapshot(`
163
+ {
164
+ "challengeId": "[challengeId]",
165
+ "detail": "Challenge "wrong-id" is invalid: challenge was not issued by this server.",
166
+ "instance": "[instance]",
167
+ "status": 402,
168
+ "title": "Invalid Challenge",
169
+ "type": "https://paymentauth.org/problems/invalid-challenge",
170
+ }
171
+ `)
172
+ })
173
+
130
174
  test('captures each transport request once and threads the verified envelope additively', async () => {
131
175
  const requestMethod = Method.from({
132
176
  name: 'mock',
@@ -232,50 +276,6 @@ describe('request handler', () => {
232
276
  expect(receiptEnvelope?.challenge.id).toBe(credential.challenge.id)
233
277
  })
234
278
 
235
- test('returns 402 when challenge ID mismatch', async () => {
236
- const wrongChallenge = Challenge.from({
237
- id: 'wrong-id',
238
- intent: 'charge',
239
- method: 'tempo',
240
- realm,
241
- request: { amount: '1000', currency: asset, recipient: accounts[0].address },
242
- })
243
- const credential = Credential.from({
244
- challenge: wrongChallenge,
245
- payload: { signature: '0x123', type: 'transaction' },
246
- })
247
-
248
- const request = new Request('https://example.com/resource', {
249
- headers: { Authorization: Credential.serialize(credential) },
250
- })
251
-
252
- const result = await Mppx.create({ methods: [method], realm, secretKey }).charge({
253
- amount: '1000',
254
- currency: asset,
255
- expires: new Date(Date.now() + 60_000).toISOString(),
256
- recipient: accounts[0].address,
257
- })(request)
258
-
259
- expect(result.status).toBe(402)
260
- if (result.status !== 402) throw new Error()
261
-
262
- const body = (await result.challenge.json()) as object
263
- expect({
264
- ...body,
265
- challengeId: '[challengeId]',
266
- instance: '[instance]',
267
- }).toMatchInlineSnapshot(`
268
- {
269
- "challengeId": "[challengeId]",
270
- "detail": "Challenge "wrong-id" is invalid: challenge was not issued by this server.",
271
- "instance": "[instance]",
272
- "status": 402,
273
- "title": "Invalid Challenge",
274
- "type": "https://paymentauth.org/problems/invalid-challenge",
275
- }
276
- `)
277
- })
278
-
279
279
  test('returns 402 when credential is from a different route (cross-route scope confusion)', async () => {
280
280
  const handler = Mppx.create({ methods: [method], realm, secretKey })
281
281
 
@@ -982,6 +982,103 @@ describe('compose', () => {
982
982
  expect(wwwAuth).toContain('method="beta"')
983
983
  })
984
984
 
985
+ test('filters compose challenges using Accept-Payment', async () => {
986
+ const mppx = Mppx.create({ methods: [alphaMethod, betaMethod], realm, secretKey })
987
+
988
+ const result = await mppx.compose(
989
+ [alphaMethod, challengeOpts],
990
+ [betaMethod, challengeOpts],
991
+ )(
992
+ new Request('https://example.com/resource', {
993
+ headers: { 'Accept-Payment': 'beta/charge' },
994
+ }),
995
+ )
996
+
997
+ expect(result.status).toBe(402)
998
+ if (result.status !== 402) throw new Error()
999
+
1000
+ const challenges = Challenge.fromResponseList(result.challenge)
1001
+ expect(challenges).toHaveLength(1)
1002
+ expect(challenges[0]?.method).toBe('beta')
1003
+ })
1004
+
1005
+ test('orders compose challenges by Accept-Payment q-value', async () => {
1006
+ const mppx = Mppx.create({ methods: [alphaMethod, betaMethod], realm, secretKey })
1007
+
1008
+ const result = await mppx.compose(
1009
+ [alphaMethod, challengeOpts],
1010
+ [betaMethod, challengeOpts],
1011
+ )(
1012
+ new Request('https://example.com/resource', {
1013
+ headers: { 'Accept-Payment': 'beta/charge;q=0.9, alpha/charge;q=0.3' },
1014
+ }),
1015
+ )
1016
+
1017
+ expect(result.status).toBe(402)
1018
+ if (result.status !== 402) throw new Error()
1019
+
1020
+ const challenges = Challenge.fromResponseList(result.challenge)
1021
+ expect(challenges.map((challenge) => challenge.method)).toEqual(['beta', 'alpha'])
1022
+ })
1023
+
1024
+ test('applies a specific Accept-Payment opt-out before broader wildcards', async () => {
1025
+ const mppx = Mppx.create({ methods: [alphaMethod, betaMethod], realm, secretKey })
1026
+
1027
+ const result = await mppx.compose(
1028
+ [alphaMethod, challengeOpts],
1029
+ [betaMethod, challengeOpts],
1030
+ )(
1031
+ new Request('https://example.com/resource', {
1032
+ headers: { 'Accept-Payment': 'alpha/*;q=1, alpha/charge;q=0, beta/*;q=0.5' },
1033
+ }),
1034
+ )
1035
+
1036
+ expect(result.status).toBe(402)
1037
+ if (result.status !== 402) throw new Error()
1038
+
1039
+ const challenges = Challenge.fromResponseList(result.challenge)
1040
+ expect(challenges).toHaveLength(1)
1041
+ expect(challenges[0]?.method).toBe('beta')
1042
+ })
1043
+
1044
+ test('falls back to all compose challenges when Accept-Payment has no matches', async () => {
1045
+ const mppx = Mppx.create({ methods: [alphaMethod, betaMethod], realm, secretKey })
1046
+
1047
+ const result = await mppx.compose(
1048
+ [alphaMethod, challengeOpts],
1049
+ [betaMethod, challengeOpts],
1050
+ )(
1051
+ new Request('https://example.com/resource', {
1052
+ headers: { 'Accept-Payment': 'gamma/charge' },
1053
+ }),
1054
+ )
1055
+
1056
+ expect(result.status).toBe(402)
1057
+ if (result.status !== 402) throw new Error()
1058
+
1059
+ const challenges = Challenge.fromResponseList(result.challenge)
1060
+ expect(challenges.map((challenge) => challenge.method)).toEqual(['alpha', 'beta'])
1061
+ })
1062
+
1063
+ test('falls back to all compose challenges when Accept-Payment is invalid', async () => {
1064
+ const mppx = Mppx.create({ methods: [alphaMethod, betaMethod], realm, secretKey })
1065
+
1066
+ const result = await mppx.compose(
1067
+ [alphaMethod, challengeOpts],
1068
+ [betaMethod, challengeOpts],
1069
+ )(
1070
+ new Request('https://example.com/resource', {
1071
+ headers: { 'Accept-Payment': 'not a valid header' },
1072
+ }),
1073
+ )
1074
+
1075
+ expect(result.status).toBe(402)
1076
+ if (result.status !== 402) throw new Error()
1077
+
1078
+ const challenges = Challenge.fromResponseList(result.challenge)
1079
+ expect(challenges.map((challenge) => challenge.method)).toEqual(['alpha', 'beta'])
1080
+ })
1081
+
985
1082
  test('dispatches to matching handler when credential matches alpha', async () => {
986
1083
  const mppx = Mppx.create({ methods: [alphaMethod, betaMethod], realm, secretKey })
987
1084
 
@@ -5,8 +5,9 @@ import * as Challenge from '../Challenge.js'
5
5
  import * as Credential from '../Credential.js'
6
6
  import * as Errors from '../Errors.js'
7
7
  import * as Expires from '../Expires.js'
8
+ import * as AcceptPayment from '../internal/AcceptPayment.js'
8
9
  import * as Env from '../internal/env.js'
9
- import * as Method from '../Method.js'
10
+ import type * as Method from '../Method.js'
10
11
  import * as PaymentRequest from '../PaymentRequest.js'
11
12
  import type * as Receipt from '../Receipt.js'
12
13
  import type * as z from '../zod.js'
@@ -447,22 +448,22 @@ function createMethodFn(parameters: createMethodFn.Parameters): createMethodFn.R
447
448
  withReceipt<response>(response?: response) {
448
449
  if (managementResponse) {
449
450
  return transport.respondReceipt({
451
+ challengeId: credential.challenge.id,
450
452
  credential,
451
453
  envelope,
452
454
  input,
453
455
  receipt: receiptData,
454
456
  response: managementResponse as never,
455
- challengeId: credential.challenge.id,
456
457
  }) as response
457
458
  }
458
459
  if (!response) throw new Error('withReceipt() requires a response argument')
459
460
  return transport.respondReceipt({
461
+ challengeId: credential.challenge.id,
460
462
  credential,
461
463
  envelope,
462
464
  input,
463
465
  receipt: receiptData,
464
466
  response: response as never,
465
- challengeId: credential.challenge.id,
466
467
  }) as response
467
468
  },
468
469
  }
@@ -883,38 +884,58 @@ export function compose(
883
884
  // No credential — call all handlers and merge 402 challenges.
884
885
  const results = await Promise.all(handlers.map((h) => h(input)))
885
886
 
886
- // Merge WWW-Authenticate headers from all 402 responses.
887
- const mergedHeaders = new Headers()
888
- mergedHeaders.set('Cache-Control', 'no-store')
889
-
890
- for (const result of results) {
891
- if (result.status !== 402) continue
892
- const response = result.challenge as Response
893
- const wwwAuth = response.headers.get('WWW-Authenticate')
894
- if (wwwAuth) mergedHeaders.append('WWW-Authenticate', wwwAuth)
895
- }
896
-
897
- // Collect html-enabled handlers and their challenges
898
- const htmlEntries = (() => {
887
+ const challengeEntries = (() => {
899
888
  const entries: {
900
889
  handler: ConfiguredHandler
901
890
  challenge: Challenge.Challenge
891
+ result: Extract<MethodFn.Response<Transport.Http>, { status: 402 }>
902
892
  }[] = []
893
+
903
894
  for (let i = 0; i < handlers.length; i++) {
904
- const meta = (handlers[i] as ConfiguredHandler)._internal
905
- if (!meta?.html) continue
906
895
  const result = results[i]
907
896
  if (result?.status !== 402) continue
908
- const wwwAuth = result.challenge.headers.get('WWW-Authenticate')
897
+
898
+ const response = result.challenge as Response
899
+ const wwwAuth = response.headers.get('WWW-Authenticate')
909
900
  if (!wwwAuth) continue
901
+
910
902
  entries.push({
911
903
  handler: handlers[i] as ConfiguredHandler,
912
904
  challenge: Challenge.deserialize(wwwAuth),
905
+ result,
913
906
  })
914
907
  }
915
- return entries
908
+
909
+ const acceptPayment = input.headers.get('Accept-Payment')
910
+ if (!acceptPayment) return entries
911
+
912
+ try {
913
+ const ranked = AcceptPayment.rank(
914
+ entries.map((entry) => entry.challenge),
915
+ AcceptPayment.parse(acceptPayment),
916
+ )
917
+ if (ranked.length === 0) return entries
918
+
919
+ const entriesById = new Map(entries.map((entry) => [entry.challenge.id, entry] as const))
920
+ return ranked.map((challenge) => entriesById.get(challenge.id)!)
921
+ } catch {
922
+ return entries
923
+ }
916
924
  })()
917
925
 
926
+ // Merge WWW-Authenticate headers from all 402 responses.
927
+ const mergedHeaders = new Headers()
928
+ mergedHeaders.set('Cache-Control', 'no-store')
929
+
930
+ for (const entry of challengeEntries) {
931
+ const response = entry.result.challenge as Response
932
+ const wwwAuth = response.headers.get('WWW-Authenticate')
933
+ if (wwwAuth) mergedHeaders.append('WWW-Authenticate', wwwAuth)
934
+ }
935
+
936
+ // Collect html-enabled handlers and their challenges
937
+ const htmlEntries = challengeEntries.filter((entry) => entry.handler._internal?.html)
938
+
918
939
  const wantsHtml = input.headers.get('Accept')?.includes('text/html')
919
940
  if (wantsHtml && htmlEntries.length > 0) {
920
941
  const { theme, text } = Html.resolveOptions(
@@ -962,10 +983,9 @@ export function compose(
962
983
 
963
984
  // Non-HTML fallback: use first handler's body
964
985
  let body: string | null = null
965
- for (const result of results) {
966
- if (result.status !== 402) continue
986
+ for (const entry of challengeEntries) {
967
987
  if (!body) {
968
- const response = result.challenge as Response
988
+ const response = entry.result.challenge as Response
969
989
  const contentType = response.headers.get('Content-Type')
970
990
  if (contentType) mergedHeaders.set('Content-Type', contentType)
971
991
  body = await response.text()
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Stripe API version with `.preview` suffix.
3
+ *
4
+ * Required for `shared_payment_granted_token` (SPTs are in private preview).
5
+ * Bump this when upgrading to a newer Stripe API version.
6
+ */
7
+ export const stripePreviewVersion = '2026-02-25.preview'
@@ -5,6 +5,7 @@ import type { LooseOmit, OneOf } from '../../internal/types.js'
5
5
  import * as Method from '../../Method.js'
6
6
  import type * as Html from '../../server/internal/html/config.ts'
7
7
  import type * as z from '../../zod.js'
8
+ import { stripePreviewVersion } from '../internal/constants.js'
8
9
  import type {
9
10
  StripeClient,
10
11
  CreatePaymentMethodFromElements,
@@ -202,13 +203,16 @@ async function createWithClient(parameters: {
202
203
  // `shared_payment_granted_token` is not yet in the Stripe SDK types (SPTs are in private preview).
203
204
  shared_payment_granted_token: spt,
204
205
  } as any,
205
- { idempotencyKey: `mppx_${challenge.id}_${spt}` },
206
+ { idempotencyKey: `mppx_${challenge.id}_${spt}`, apiVersion: stripePreviewVersion },
206
207
  )
207
208
  // https://docs.stripe.com/error-low-level#idempotency
208
209
  const replayed = result.lastResponse?.headers?.['idempotent-replayed'] === 'true'
209
210
  return { id: result.id, status: result.status, replayed }
210
- } catch {
211
- throw new VerificationFailedError({ reason: 'Stripe PaymentIntent failed' })
211
+ } catch (error) {
212
+ const detail = error instanceof Error ? error.message : String(error)
213
+ throw new VerificationFailedError({
214
+ reason: `Stripe PaymentIntent failed: ${detail}`,
215
+ })
212
216
  }
213
217
  }
214
218
 
@@ -240,11 +244,25 @@ async function createWithSecretKey(parameters: {
240
244
  Authorization: `Basic ${btoa(`${secretKey}:`)}`,
241
245
  'Content-Type': 'application/x-www-form-urlencoded',
242
246
  'Idempotency-Key': `mppx_${challenge.id}_${spt}`,
247
+ 'Stripe-Version': stripePreviewVersion,
243
248
  },
244
249
  body,
245
250
  })
246
251
 
247
- if (!response.ok) throw new VerificationFailedError({ reason: 'Stripe PaymentIntent failed' })
252
+ if (!response.ok) {
253
+ const body = await response.text().catch(() => '')
254
+ const detail = (() => {
255
+ try {
256
+ const parsed = JSON.parse(body) as { error?: { message?: string } }
257
+ return parsed.error?.message ?? body
258
+ } catch {
259
+ return body
260
+ }
261
+ })()
262
+ throw new VerificationFailedError({
263
+ reason: `Stripe PaymentIntent failed: ${detail}`,
264
+ })
265
+ }
248
266
  // https://docs.stripe.com/error-low-level#idempotency
249
267
  const replayed = response.headers.get('idempotent-replayed') === 'true'
250
268
  const result = (await response.json()) as { id: string; status: string }
@@ -0,0 +1,13 @@
1
+ import { expectTypeOf, test } from 'vp/test'
2
+
3
+ import { Proof } from './index.js'
4
+
5
+ test('Proof exports public proof source helpers', () => {
6
+ expectTypeOf(Proof.proofSource).toEqualTypeOf<
7
+ (parameters: { address: string; chainId: number }) => string
8
+ >()
9
+
10
+ expectTypeOf(Proof.parseProofSource).toEqualTypeOf<
11
+ (source: string) => { address: `0x${string}`; chainId: number } | null
12
+ >()
13
+ })
@@ -0,0 +1,31 @@
1
+ import { describe, expect, test } from 'vp/test'
2
+
3
+ import * as tempo from './index.js'
4
+
5
+ describe('tempo.Proof', () => {
6
+ test('proofSource constructs a did:pkh:eip155 source', () => {
7
+ expect(
8
+ tempo.Proof.proofSource({
9
+ address: '0xAbCdEf1234567890AbCdEf1234567890AbCdEf12',
10
+ chainId: 42431,
11
+ }),
12
+ ).toBe('did:pkh:eip155:42431:0xAbCdEf1234567890AbCdEf1234567890AbCdEf12')
13
+ })
14
+
15
+ test('parseProofSource parses a valid did:pkh:eip155 source', () => {
16
+ expect(
17
+ tempo.Proof.parseProofSource(
18
+ 'did:pkh:eip155:42431:0xa5cc3c03994db5b0d9ba5e4f6d2efbd9f213b141',
19
+ ),
20
+ ).toEqual({
21
+ address: '0xa5cc3c03994db5b0d9ba5e4f6d2efbd9f213b141',
22
+ chainId: 42431,
23
+ })
24
+ })
25
+
26
+ test('parseProofSource rejects invalid source values', () => {
27
+ expect(
28
+ tempo.Proof.parseProofSource('did:pkh:eip155:01:0xa5cc3c03994db5b0d9ba5e4f6d2efbd9f213b141'),
29
+ ).toBe(null)
30
+ })
31
+ })
@@ -0,0 +1,13 @@
1
+ import type { Address } from 'viem'
2
+
3
+ import * as Proof_internal from './internal/proof.js'
4
+
5
+ /** Constructs the canonical `did:pkh:eip155` source DID for Tempo proof credentials. */
6
+ export function proofSource(parameters: { address: string; chainId: number }): string {
7
+ return Proof_internal.proofSource(parameters)
8
+ }
9
+
10
+ /** Parses a Tempo proof credential source DID into its chain ID and wallet address. */
11
+ export function parseProofSource(source: string): { address: Address; chainId: number } | null {
12
+ return Proof_internal.parseProofSource(source)
13
+ }
@@ -225,13 +225,10 @@ describe('Session', () => {
225
225
  // drain
226
226
  }
227
227
 
228
- const calledHeaders = (mockFetch.mock.calls[0]![1] as RequestInit).headers as Record<
229
- string,
230
- string
231
- >
232
- expect(calledHeaders['content-type']).toBe('application/json')
233
- expect(calledHeaders['x-custom']).toBe('value')
234
- expect(calledHeaders.Accept).toBe('text/event-stream')
228
+ const calledHeaders = new Headers((mockFetch.mock.calls[0]![1] as RequestInit).headers)
229
+ expect(calledHeaders.get('content-type')).toBe('application/json')
230
+ expect(calledHeaders.get('x-custom')).toBe('value')
231
+ expect(calledHeaders.get('accept')).toBe('text/event-stream')
235
232
  })
236
233
  })
237
234
 
@@ -1,2 +1,3 @@
1
+ export * as Proof from './Proof.js'
1
2
  export * as Methods from './Methods.js'
2
3
  export * as Session from './session/index.js'
@@ -322,7 +322,7 @@ describe('prepareSponsoredTransaction', () => {
322
322
  transaction: {
323
323
  ...baseTransaction,
324
324
  gas: 1_500_000n,
325
- maxFeePerGas: 10_000_000_000n,
325
+ maxFeePerGas: 50_000_000_000n,
326
326
  } as any,
327
327
  }),
328
328
  ).toThrow('total fee budget exceeds sponsor policy')
@@ -26,11 +26,15 @@ export const callScopes = [
26
26
  [Selectors.approve, Selectors.swapExactAmountOut, Selectors.transferWithMemo],
27
27
  ]
28
28
 
29
+ /**
30
+ * maxTotalFee must be high enough to cover `transferWithMemo` and
31
+ * swap transactions at peak gas prices. Bumped from 0.01 ETH in #327.
32
+ */
29
33
  const policy = {
30
34
  maxGas: 2_000_000n,
31
35
  maxFeePerGas: 100_000_000_000n,
32
36
  maxPriorityFeePerGas: 10_000_000_000n,
33
- maxTotalFee: 10_000_000_000_000_000n,
37
+ maxTotalFee: 50_000_000_000_000_000n,
34
38
  maxValidityWindowSeconds: 15 * 60,
35
39
  } as const
36
40
 
@@ -25,6 +25,11 @@ import { signVoucher } from '../session/Voucher.js'
25
25
  const realm = 'api.example.com'
26
26
  const secretKey = 'test-secret-key'
27
27
 
28
+ type ProofAccessKeyContext = {
29
+ accessKey: ReturnType<typeof Account.fromSecp256k1>
30
+ rootAccount: (typeof accounts)[number]
31
+ }
32
+
28
33
  const server = Mppx_server.create({
29
34
  methods: [
30
35
  tempo_server.charge({
@@ -2114,6 +2119,124 @@ describe('tempo', () => {
2114
2119
  httpServer.close()
2115
2120
  })
2116
2121
 
2122
+ for (const testCase of [
2123
+ {
2124
+ name: 'accepts proof signed by an authorized access key for the root source',
2125
+ expectedStatus: 200,
2126
+ async prepare({ accessKey, rootAccount }: ProofAccessKeyContext) {
2127
+ await Actions.accessKey.authorizeSync(client, {
2128
+ account: rootAccount,
2129
+ accessKey,
2130
+ feeToken: asset,
2131
+ })
2132
+ },
2133
+ },
2134
+ {
2135
+ name: 'rejects proof signed by an unauthorized access key for the root source',
2136
+ expectedDetail: 'Proof signature does not match source.',
2137
+ expectedStatus: 402,
2138
+ },
2139
+ {
2140
+ name: 'rejects proof signed by a revoked access key for the root source',
2141
+ expectedDetail: 'Proof signature does not match source.',
2142
+ expectedStatus: 402,
2143
+ async prepare({ accessKey, rootAccount }: ProofAccessKeyContext) {
2144
+ await Actions.accessKey.authorizeSync(client, {
2145
+ account: rootAccount,
2146
+ accessKey,
2147
+ feeToken: asset,
2148
+ })
2149
+ await fundAccount({ address: rootAccount.address, token: asset })
2150
+ await Actions.accessKey.revokeSync(client, {
2151
+ account: rootAccount,
2152
+ accessKey,
2153
+ feeToken: asset,
2154
+ })
2155
+ },
2156
+ },
2157
+ {
2158
+ name: 'rejects proof signed by an expired access key for the root source',
2159
+ expectedDetail: 'Proof signature does not match source.',
2160
+ expectedStatus: 402,
2161
+ async prepare({ accessKey, rootAccount }: ProofAccessKeyContext) {
2162
+ await Actions.accessKey.authorizeSync(client, {
2163
+ account: rootAccount,
2164
+ accessKey,
2165
+ expiry: Math.floor(Date.now() / 1000) + 10,
2166
+ feeToken: asset,
2167
+ })
2168
+
2169
+ const metadata = await Actions.accessKey.getMetadata(client, {
2170
+ account: rootAccount.address,
2171
+ accessKey,
2172
+ })
2173
+ const originalNow = Date.now
2174
+ Date.now = () => (Number(metadata.expiry) + 5) * 1000
2175
+
2176
+ return () => {
2177
+ Date.now = originalNow
2178
+ }
2179
+ },
2180
+ },
2181
+ ] as const) {
2182
+ test(`behavior: ${testCase.name}`, async () => {
2183
+ const rootAccount = accounts[1]
2184
+ const accessKey = Account.fromSecp256k1(Secp256k1.randomPrivateKey(), {
2185
+ access: rootAccount,
2186
+ })
2187
+
2188
+ let cleanup: (() => void) | undefined
2189
+ let httpServer: Awaited<ReturnType<typeof Http.createServer>> | undefined
2190
+
2191
+ try {
2192
+ const maybeCleanup = await testCase.prepare?.({ accessKey, rootAccount })
2193
+ cleanup = typeof maybeCleanup === 'function' ? maybeCleanup : undefined
2194
+
2195
+ httpServer = await Http.createServer(async (req, res) => {
2196
+ const result = await Mppx_server.toNodeListener(
2197
+ server.charge({ amount: '0', decimals: 6 }),
2198
+ )(req, res)
2199
+ if (result.status === 402) return
2200
+ res.end('OK')
2201
+ })
2202
+
2203
+ const response1 = await fetch(httpServer.url)
2204
+ expect(response1.status).toBe(402)
2205
+
2206
+ const challenge = Challenge.fromResponse(response1, {
2207
+ methods: [tempo_client.charge()],
2208
+ })
2209
+
2210
+ const signature = await signTypedData(client, {
2211
+ account: accessKey,
2212
+ domain: Proof.domain(chain.id),
2213
+ types: Proof.types,
2214
+ primaryType: 'Proof',
2215
+ message: Proof.message(challenge.id),
2216
+ })
2217
+
2218
+ const credential = Credential.from({
2219
+ challenge,
2220
+ payload: { signature, type: 'proof' as const },
2221
+ source: `did:pkh:eip155:${chain.id}:${rootAccount.address}`,
2222
+ })
2223
+
2224
+ const response2 = await fetch(httpServer.url, {
2225
+ headers: { Authorization: Credential.serialize(credential) },
2226
+ })
2227
+ expect(response2.status).toBe(testCase.expectedStatus)
2228
+
2229
+ if (testCase.expectedDetail) {
2230
+ const body = (await response2.json()) as { detail: string }
2231
+ expect(body.detail).toContain(testCase.expectedDetail)
2232
+ }
2233
+ } finally {
2234
+ cleanup?.()
2235
+ httpServer?.close()
2236
+ }
2237
+ })
2238
+ }
2239
+
2117
2240
  test('behavior: rejects replayed proof credential when store is configured', async () => {
2118
2241
  const replayStore = Store.memory()
2119
2242
  const server_ = Mppx_server.create({