mppx 0.0.1 → 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.
- package/LICENSE +21 -0
- package/README.md +195 -0
- package/dist/BodyDigest.d.ts +42 -0
- package/dist/BodyDigest.d.ts.map +1 -0
- package/dist/BodyDigest.js +40 -0
- package/dist/BodyDigest.js.map +1 -0
- package/dist/Challenge.d.ts +271 -0
- package/dist/Challenge.d.ts.map +1 -0
- package/dist/Challenge.js +291 -0
- package/dist/Challenge.js.map +1 -0
- package/dist/Credential.d.ts +91 -0
- package/dist/Credential.d.ts.map +1 -0
- package/dist/Credential.js +122 -0
- package/dist/Credential.js.map +1 -0
- package/dist/Errors.d.ts +243 -0
- package/dist/Errors.d.ts.map +1 -0
- package/dist/Errors.js +201 -0
- package/dist/Errors.js.map +1 -0
- package/dist/Expires.d.ts +15 -0
- package/dist/Expires.d.ts.map +1 -0
- package/dist/Expires.js +29 -0
- package/dist/Expires.js.map +1 -0
- package/dist/Intent.d.ts +101 -0
- package/dist/Intent.d.ts.map +1 -0
- package/dist/Intent.js +83 -0
- package/dist/Intent.js.map +1 -0
- package/dist/Mcp.d.ts +74 -0
- package/dist/Mcp.d.ts.map +1 -0
- package/dist/Mcp.js +9 -0
- package/dist/Mcp.js.map +1 -0
- package/dist/MethodIntent.d.ts +225 -0
- package/dist/MethodIntent.d.ts.map +1 -0
- package/dist/MethodIntent.js +156 -0
- package/dist/MethodIntent.js.map +1 -0
- package/dist/PaymentRequest.d.ts +88 -0
- package/dist/PaymentRequest.d.ts.map +1 -0
- package/dist/PaymentRequest.js +81 -0
- package/dist/PaymentRequest.js.map +1 -0
- package/dist/Receipt.d.ts +110 -0
- package/dist/Receipt.d.ts.map +1 -0
- package/dist/Receipt.js +105 -0
- package/dist/Receipt.js.map +1 -0
- package/dist/Store.d.ts +28 -0
- package/dist/Store.d.ts.map +1 -0
- package/dist/Store.js +61 -0
- package/dist/Store.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +1219 -0
- package/dist/cli.js.map +1 -0
- package/dist/client/Methods.d.ts +4 -0
- package/dist/client/Methods.d.ts.map +1 -0
- package/dist/client/Methods.js +4 -0
- package/dist/client/Methods.js.map +1 -0
- package/dist/client/Mppx.d.ts +84 -0
- package/dist/client/Mppx.d.ts.map +1 -0
- package/dist/client/Mppx.js +64 -0
- package/dist/client/Mppx.js.map +1 -0
- package/dist/client/Transport.d.ts +56 -0
- package/dist/client/Transport.d.ts.map +1 -0
- package/dist/client/Transport.js +81 -0
- package/dist/client/Transport.js.map +1 -0
- package/dist/client/index.d.ts +5 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +5 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/internal/Fetch.d.ts +85 -0
- package/dist/client/internal/Fetch.d.ts.map +1 -0
- package/dist/client/internal/Fetch.js +95 -0
- package/dist/client/internal/Fetch.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/types.d.ts +302 -0
- package/dist/internal/types.d.ts.map +1 -0
- package/dist/internal/types.js +2 -0
- package/dist/internal/types.js.map +1 -0
- package/dist/mcp-sdk/client/McpClient.d.ts +78 -0
- package/dist/mcp-sdk/client/McpClient.d.ts.map +1 -0
- package/dist/mcp-sdk/client/McpClient.js +98 -0
- package/dist/mcp-sdk/client/McpClient.js.map +1 -0
- package/dist/mcp-sdk/client/index.d.ts +3 -0
- package/dist/mcp-sdk/client/index.d.ts.map +1 -0
- package/dist/mcp-sdk/client/index.js +3 -0
- package/dist/mcp-sdk/client/index.js.map +1 -0
- package/dist/mcp-sdk/server/Transport.d.ts +43 -0
- package/dist/mcp-sdk/server/Transport.d.ts.map +1 -0
- package/dist/mcp-sdk/server/Transport.js +71 -0
- package/dist/mcp-sdk/server/Transport.js.map +1 -0
- package/dist/mcp-sdk/server/index.d.ts +2 -0
- package/dist/mcp-sdk/server/index.d.ts.map +1 -0
- package/dist/mcp-sdk/server/index.js +2 -0
- package/dist/mcp-sdk/server/index.js.map +1 -0
- package/dist/middlewares/elysia.d.ts +51 -0
- package/dist/middlewares/elysia.d.ts.map +1 -0
- package/dist/middlewares/elysia.js +59 -0
- package/dist/middlewares/elysia.js.map +1 -0
- package/dist/middlewares/express.d.ts +46 -0
- package/dist/middlewares/express.d.ts.map +1 -0
- package/dist/middlewares/express.js +69 -0
- package/dist/middlewares/express.js.map +1 -0
- package/dist/middlewares/hono.d.ts +46 -0
- package/dist/middlewares/hono.d.ts.map +1 -0
- package/dist/middlewares/hono.js +57 -0
- package/dist/middlewares/hono.js.map +1 -0
- package/dist/middlewares/internal/mppx.d.ts +16 -0
- package/dist/middlewares/internal/mppx.d.ts.map +1 -0
- package/dist/middlewares/internal/mppx.js +16 -0
- package/dist/middlewares/internal/mppx.js.map +1 -0
- package/dist/middlewares/nextjs.d.ts +45 -0
- package/dist/middlewares/nextjs.d.ts.map +1 -0
- package/dist/middlewares/nextjs.js +57 -0
- package/dist/middlewares/nextjs.js.map +1 -0
- package/dist/proxy/Proxy.d.ts +47 -0
- package/dist/proxy/Proxy.d.ts.map +1 -0
- package/dist/proxy/Proxy.js +126 -0
- package/dist/proxy/Proxy.js.map +1 -0
- package/dist/proxy/Service.d.ts +100 -0
- package/dist/proxy/Service.d.ts.map +1 -0
- package/dist/proxy/Service.js +147 -0
- package/dist/proxy/Service.js.map +1 -0
- package/dist/proxy/index.d.ts +7 -0
- package/dist/proxy/index.d.ts.map +1 -0
- package/dist/proxy/index.js +7 -0
- package/dist/proxy/index.js.map +1 -0
- package/dist/proxy/internal/Headers.d.ts +3 -0
- package/dist/proxy/internal/Headers.d.ts.map +1 -0
- package/dist/proxy/internal/Headers.js +41 -0
- package/dist/proxy/internal/Headers.js.map +1 -0
- package/dist/proxy/internal/Route.d.ts +14 -0
- package/dist/proxy/internal/Route.d.ts.map +1 -0
- package/dist/proxy/internal/Route.js +47 -0
- package/dist/proxy/internal/Route.js.map +1 -0
- package/dist/proxy/services/anthropic.d.ts +29 -0
- package/dist/proxy/services/anthropic.d.ts.map +1 -0
- package/dist/proxy/services/anthropic.js +30 -0
- package/dist/proxy/services/anthropic.js.map +1 -0
- package/dist/proxy/services/openai.d.ts +29 -0
- package/dist/proxy/services/openai.d.ts.map +1 -0
- package/dist/proxy/services/openai.js +30 -0
- package/dist/proxy/services/openai.js.map +1 -0
- package/dist/proxy/services/stripe.d.ts +29 -0
- package/dist/proxy/services/stripe.d.ts.map +1 -0
- package/dist/proxy/services/stripe.js +30 -0
- package/dist/proxy/services/stripe.js.map +1 -0
- package/dist/server/Methods.d.ts +3 -0
- package/dist/server/Methods.d.ts.map +1 -0
- package/dist/server/Methods.js +3 -0
- package/dist/server/Methods.js.map +1 -0
- package/dist/server/Mppx.d.ts +116 -0
- package/dist/server/Mppx.d.ts.map +1 -0
- package/dist/server/Mppx.js +207 -0
- package/dist/server/Mppx.js.map +1 -0
- package/dist/server/NodeListener.d.ts +3 -0
- package/dist/server/NodeListener.d.ts.map +1 -0
- package/dist/server/NodeListener.js +3 -0
- package/dist/server/NodeListener.js.map +1 -0
- package/dist/server/Request.d.ts +24 -0
- package/dist/server/Request.d.ts.map +1 -0
- package/dist/server/Request.js +26 -0
- package/dist/server/Request.js.map +1 -0
- package/dist/server/Response.d.ts +10 -0
- package/dist/server/Response.d.ts.map +1 -0
- package/dist/server/Response.js +15 -0
- package/dist/server/Response.js.map +1 -0
- package/dist/server/Transport.d.ts +93 -0
- package/dist/server/Transport.d.ts.map +1 -0
- package/dist/server/Transport.js +132 -0
- package/dist/server/Transport.js.map +1 -0
- package/dist/server/index.d.ts +9 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +9 -0
- package/dist/server/index.js.map +1 -0
- package/dist/stripe/Intents.d.ts +54 -0
- package/dist/stripe/Intents.d.ts.map +1 -0
- package/dist/stripe/Intents.js +27 -0
- package/dist/stripe/Intents.js.map +1 -0
- package/dist/stripe/client/Charge.d.ts +114 -0
- package/dist/stripe/client/Charge.d.ts.map +1 -0
- package/dist/stripe/client/Charge.js +77 -0
- package/dist/stripe/client/Charge.js.map +1 -0
- package/dist/stripe/client/MethodIntents.d.ts +80 -0
- package/dist/stripe/client/MethodIntents.d.ts.map +1 -0
- package/dist/stripe/client/MethodIntents.js +34 -0
- package/dist/stripe/client/MethodIntents.js.map +1 -0
- package/dist/stripe/client/index.d.ts +3 -0
- package/dist/stripe/client/index.d.ts.map +1 -0
- package/dist/stripe/client/index.js +3 -0
- package/dist/stripe/client/index.js.map +1 -0
- package/dist/stripe/index.d.ts +2 -0
- package/dist/stripe/index.d.ts.map +1 -0
- package/dist/stripe/index.js +2 -0
- package/dist/stripe/index.js.map +1 -0
- package/dist/stripe/server/Charge.d.ts +74 -0
- package/dist/stripe/server/Charge.d.ts.map +1 -0
- package/dist/stripe/server/Charge.js +79 -0
- package/dist/stripe/server/Charge.js.map +1 -0
- package/dist/stripe/server/MethodIntents.d.ts +65 -0
- package/dist/stripe/server/MethodIntents.d.ts.map +1 -0
- package/dist/stripe/server/MethodIntents.js +21 -0
- package/dist/stripe/server/MethodIntents.js.map +1 -0
- package/dist/stripe/server/index.d.ts +3 -0
- package/dist/stripe/server/index.d.ts.map +1 -0
- package/dist/stripe/server/index.js +3 -0
- package/dist/stripe/server/index.js.map +1 -0
- package/dist/tempo/Attribution.d.ts +101 -0
- package/dist/tempo/Attribution.d.ts.map +1 -0
- package/dist/tempo/Attribution.js +124 -0
- package/dist/tempo/Attribution.js.map +1 -0
- package/dist/tempo/Intents.d.ts +132 -0
- package/dist/tempo/Intents.d.ts.map +1 -0
- package/dist/tempo/Intents.js +81 -0
- package/dist/tempo/Intents.js.map +1 -0
- package/dist/tempo/client/ChannelOps.d.ts +54 -0
- package/dist/tempo/client/ChannelOps.d.ts.map +1 -0
- package/dist/tempo/client/ChannelOps.js +138 -0
- package/dist/tempo/client/ChannelOps.js.map +1 -0
- package/dist/tempo/client/Charge.d.ts +76 -0
- package/dist/tempo/client/Charge.d.ts.map +1 -0
- package/dist/tempo/client/Charge.js +69 -0
- package/dist/tempo/client/Charge.js.map +1 -0
- package/dist/tempo/client/MethodIntents.d.ts +157 -0
- package/dist/tempo/client/MethodIntents.d.ts.map +1 -0
- package/dist/tempo/client/MethodIntents.js +25 -0
- package/dist/tempo/client/MethodIntents.js.map +1 -0
- package/dist/tempo/client/Session.d.ts +159 -0
- package/dist/tempo/client/Session.d.ts.map +1 -0
- package/dist/tempo/client/Session.js +263 -0
- package/dist/tempo/client/Session.js.map +1 -0
- package/dist/tempo/client/SessionManager.d.ts +62 -0
- package/dist/tempo/client/SessionManager.d.ts.map +1 -0
- package/dist/tempo/client/SessionManager.js +196 -0
- package/dist/tempo/client/SessionManager.js.map +1 -0
- package/dist/tempo/client/index.d.ts +6 -0
- package/dist/tempo/client/index.d.ts.map +1 -0
- package/dist/tempo/client/index.js +5 -0
- package/dist/tempo/client/index.js.map +1 -0
- package/dist/tempo/index.d.ts +3 -0
- package/dist/tempo/index.d.ts.map +1 -0
- package/dist/tempo/index.js +3 -0
- package/dist/tempo/index.js.map +1 -0
- package/dist/tempo/internal/account.d.ts +32 -0
- package/dist/tempo/internal/account.d.ts.map +1 -0
- package/dist/tempo/internal/account.js +33 -0
- package/dist/tempo/internal/account.js.map +1 -0
- package/dist/tempo/internal/defaults.d.ts +18 -0
- package/dist/tempo/internal/defaults.d.ts.map +1 -0
- package/dist/tempo/internal/defaults.js +18 -0
- package/dist/tempo/internal/defaults.js.map +1 -0
- package/dist/tempo/internal/types.d.ts +11 -0
- package/dist/tempo/internal/types.d.ts.map +1 -0
- package/dist/tempo/internal/types.js +2 -0
- package/dist/tempo/internal/types.js.map +1 -0
- package/dist/tempo/server/Charge.d.ts +77 -0
- package/dist/tempo/server/Charge.d.ts.map +1 -0
- package/dist/tempo/server/Charge.js +228 -0
- package/dist/tempo/server/Charge.js.map +1 -0
- package/dist/tempo/server/MethodIntents.d.ts +140 -0
- package/dist/tempo/server/MethodIntents.d.ts.map +1 -0
- package/dist/tempo/server/MethodIntents.js +26 -0
- package/dist/tempo/server/MethodIntents.js.map +1 -0
- package/dist/tempo/server/Session.d.ts +148 -0
- package/dist/tempo/server/Session.d.ts.map +1 -0
- package/dist/tempo/server/Session.js +529 -0
- package/dist/tempo/server/Session.js.map +1 -0
- package/dist/tempo/server/internal/transport.d.ts +47 -0
- package/dist/tempo/server/internal/transport.d.ts.map +1 -0
- package/dist/tempo/server/internal/transport.js +118 -0
- package/dist/tempo/server/internal/transport.js.map +1 -0
- package/dist/tempo/stream/Chain.d.ts +52 -0
- package/dist/tempo/stream/Chain.d.ts.map +1 -0
- package/dist/tempo/stream/Chain.js +215 -0
- package/dist/tempo/stream/Chain.js.map +1 -0
- package/dist/tempo/stream/Channel.d.ts +26 -0
- package/dist/tempo/stream/Channel.d.ts.map +1 -0
- package/dist/tempo/stream/Channel.js +27 -0
- package/dist/tempo/stream/Channel.js.map +1 -0
- package/dist/tempo/stream/ChannelStore.d.ts +103 -0
- package/dist/tempo/stream/ChannelStore.d.ts.map +1 -0
- package/dist/tempo/stream/ChannelStore.js +100 -0
- package/dist/tempo/stream/ChannelStore.js.map +1 -0
- package/dist/tempo/stream/Receipt.d.ts +22 -0
- package/dist/tempo/stream/Receipt.d.ts.map +1 -0
- package/dist/tempo/stream/Receipt.js +34 -0
- package/dist/tempo/stream/Receipt.js.map +1 -0
- package/dist/tempo/stream/Sse.d.ts +134 -0
- package/dist/tempo/stream/Sse.d.ts.map +1 -0
- package/dist/tempo/stream/Sse.js +288 -0
- package/dist/tempo/stream/Sse.js.map +1 -0
- package/dist/tempo/stream/Types.d.ts +78 -0
- package/dist/tempo/stream/Types.d.ts.map +1 -0
- package/dist/tempo/stream/Types.js +2 -0
- package/dist/tempo/stream/Types.js.map +1 -0
- package/dist/tempo/stream/Voucher.d.ts +20 -0
- package/dist/tempo/stream/Voucher.d.ts.map +1 -0
- package/dist/tempo/stream/Voucher.js +98 -0
- package/dist/tempo/stream/Voucher.js.map +1 -0
- package/dist/tempo/stream/escrow.abi.d.ts +598 -0
- package/dist/tempo/stream/escrow.abi.d.ts.map +1 -0
- package/dist/tempo/stream/escrow.abi.js +760 -0
- package/dist/tempo/stream/escrow.abi.js.map +1 -0
- package/dist/tempo/stream/index.d.ts +8 -0
- package/dist/tempo/stream/index.d.ts.map +1 -0
- package/dist/tempo/stream/index.js +8 -0
- package/dist/tempo/stream/index.js.map +1 -0
- package/dist/viem/Account.d.ts +12 -0
- package/dist/viem/Account.d.ts.map +1 -0
- package/dist/viem/Account.js +14 -0
- package/dist/viem/Account.js.map +1 -0
- package/dist/viem/Client.d.ts +21 -0
- package/dist/viem/Client.d.ts.map +1 -0
- package/dist/viem/Client.js +19 -0
- package/dist/viem/Client.js.map +1 -0
- package/dist/zod.d.ts +17 -0
- package/dist/zod.d.ts.map +1 -0
- package/dist/zod.js +35 -0
- package/dist/zod.js.map +1 -0
- package/package.json +117 -4
- package/src/BodyDigest.test.ts +43 -0
- package/src/BodyDigest.ts +53 -0
- package/src/Challenge.test-d.ts +81 -0
- package/src/Challenge.test.ts +414 -0
- package/src/Challenge.ts +429 -0
- package/src/Credential.test.ts +227 -0
- package/src/Credential.ts +154 -0
- package/src/Errors.test.ts +402 -0
- package/src/Errors.ts +348 -0
- package/src/Expires.ts +34 -0
- package/src/Intent.test.ts +180 -0
- package/src/Intent.ts +109 -0
- package/src/Mcp.ts +81 -0
- package/src/MethodIntent.test.ts +303 -0
- package/src/MethodIntent.ts +388 -0
- package/src/PaymentRequest.test.ts +152 -0
- package/src/PaymentRequest.ts +107 -0
- package/src/Receipt.test.ts +98 -0
- package/src/Receipt.ts +129 -0
- package/src/Store.ts +84 -0
- package/src/cli.test.ts +542 -0
- package/src/cli.ts +1319 -0
- package/src/client/Methods.ts +3 -0
- package/src/client/Mppx.test-d.ts +90 -0
- package/src/client/Mppx.test.ts +468 -0
- package/src/client/Mppx.ts +149 -0
- package/src/client/Transport.test.ts +283 -0
- package/src/client/Transport.ts +115 -0
- package/src/client/index.ts +4 -0
- package/src/client/internal/Fetch.test-d.ts +57 -0
- package/src/client/internal/Fetch.test.ts +281 -0
- package/src/client/internal/Fetch.ts +157 -0
- package/src/env.d.ts +11 -0
- package/src/index.ts +12 -0
- package/src/internal/types.ts +403 -0
- package/src/mcp-sdk/client/McpClient.test-d.ts +109 -0
- package/src/mcp-sdk/client/McpClient.test.ts +219 -0
- package/src/mcp-sdk/client/McpClient.ts +187 -0
- package/src/mcp-sdk/client/index.ts +2 -0
- package/src/mcp-sdk/server/Transport.ts +94 -0
- package/src/mcp-sdk/server/index.ts +1 -0
- package/src/middlewares/elysia.ts +66 -0
- package/src/middlewares/express.test.ts +155 -0
- package/src/middlewares/express.ts +82 -0
- package/src/middlewares/hono.test.ts +148 -0
- package/src/middlewares/hono.ts +62 -0
- package/src/middlewares/internal/mppx.ts +30 -0
- package/src/middlewares/nextjs.test.ts +164 -0
- package/src/middlewares/nextjs.ts +66 -0
- package/src/proxy/Proxy.test.ts +472 -0
- package/src/proxy/Proxy.ts +175 -0
- package/src/proxy/Service.test.ts +125 -0
- package/src/proxy/Service.ts +227 -0
- package/src/proxy/index.ts +6 -0
- package/src/proxy/internal/Headers.test.ts +100 -0
- package/src/proxy/internal/Headers.ts +40 -0
- package/src/proxy/internal/Route.test.ts +143 -0
- package/src/proxy/internal/Route.ts +54 -0
- package/src/proxy/services/anthropic.ts +45 -0
- package/src/proxy/services/openai.test.ts +97 -0
- package/src/proxy/services/openai.ts +48 -0
- package/src/proxy/services/stripe.ts +49 -0
- package/src/server/Methods.ts +2 -0
- package/src/server/Mppx.test-d.ts +343 -0
- package/src/server/Mppx.test.ts +342 -0
- package/src/server/Mppx.ts +378 -0
- package/src/server/NodeListener.test.ts +188 -0
- package/src/server/NodeListener.ts +3 -0
- package/src/server/Request.test.ts +102 -0
- package/src/server/Request.ts +33 -0
- package/src/server/Response.test.ts +31 -0
- package/src/server/Response.ts +27 -0
- package/src/server/Transport.test.ts +294 -0
- package/src/server/Transport.ts +222 -0
- package/src/server/index.ts +8 -0
- package/src/stripe/Charge.integration.test.ts +326 -0
- package/src/stripe/Intents.test.ts +52 -0
- package/src/stripe/Intents.ts +27 -0
- package/src/stripe/client/Charge.ts +119 -0
- package/src/stripe/client/MethodIntents.ts +37 -0
- package/src/stripe/client/index.ts +2 -0
- package/src/stripe/index.ts +1 -0
- package/src/stripe/server/Charge.ts +121 -0
- package/src/stripe/server/MethodIntents.ts +24 -0
- package/src/stripe/server/index.ts +2 -0
- package/src/tempo/Attribution.test.ts +187 -0
- package/src/tempo/Attribution.ts +156 -0
- package/src/tempo/Intents.test.ts +84 -0
- package/src/tempo/Intents.ts +93 -0
- package/src/tempo/client/ChannelOps.ts +233 -0
- package/src/tempo/client/Charge.ts +84 -0
- package/src/tempo/client/MethodIntents.ts +28 -0
- package/src/tempo/client/Session.ts +369 -0
- package/src/tempo/client/SessionManager.test.ts +223 -0
- package/src/tempo/client/SessionManager.ts +270 -0
- package/src/tempo/client/index.ts +5 -0
- package/src/tempo/index.ts +2 -0
- package/src/tempo/internal/account.ts +47 -0
- package/src/tempo/internal/defaults.ts +20 -0
- package/src/tempo/internal/types.ts +8 -0
- package/src/tempo/server/Charge.test.ts +847 -0
- package/src/tempo/server/Charge.ts +309 -0
- package/src/tempo/server/MethodIntents.ts +29 -0
- package/src/tempo/server/Session.test.ts +1349 -0
- package/src/tempo/server/Session.ts +773 -0
- package/src/tempo/server/Sse.test.ts +289 -0
- package/src/tempo/server/index.ts +5 -0
- package/src/tempo/server/internal/transport.ts +153 -0
- package/src/tempo/stream/Chain.ts +333 -0
- package/src/tempo/stream/Channel.ts +50 -0
- package/src/tempo/stream/ChannelStore.test.ts +473 -0
- package/src/tempo/stream/ChannelStore.ts +202 -0
- package/src/tempo/stream/Receipt.test.ts +84 -0
- package/src/tempo/stream/Receipt.ts +45 -0
- package/src/tempo/stream/Sse.test.ts +401 -0
- package/src/tempo/stream/Sse.ts +375 -0
- package/src/tempo/stream/Types.ts +86 -0
- package/src/tempo/stream/Voucher.test.ts +134 -0
- package/src/tempo/stream/Voucher.ts +123 -0
- package/src/tempo/stream/escrow.abi.ts +759 -0
- package/src/tempo/stream/index.ts +7 -0
- package/src/tsconfig.json +10 -0
- package/src/viem/Account.test.ts +71 -0
- package/src/viem/Account.ts +30 -0
- package/src/viem/Client.test.ts +58 -0
- package/src/viem/Client.ts +33 -0
- package/src/zod.ts +47 -0
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSE (Server-Sent Events) utilities for metered streaming payments.
|
|
3
|
+
*
|
|
4
|
+
* Provides event formatting/parsing, balance polling, the core
|
|
5
|
+
* `serve()` loop that meters an async iterable into a ReadableStream
|
|
6
|
+
* of SSE events, and helpers (`toResponse`, `fromRequest`) for
|
|
7
|
+
* building HTTP responses from the stream.
|
|
8
|
+
*/
|
|
9
|
+
import type { Hex } from 'viem'
|
|
10
|
+
import * as Credential from '../../Credential.js'
|
|
11
|
+
import * as ChannelStore from './ChannelStore.js'
|
|
12
|
+
import { createStreamReceipt } from './Receipt.js'
|
|
13
|
+
import type { NeedVoucherEvent, StreamCredentialPayload, StreamReceipt } from './Types.js'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Format a stream receipt as a Server-Sent Event.
|
|
17
|
+
*
|
|
18
|
+
* Produces a valid SSE event string with `event: payment-receipt`
|
|
19
|
+
* and the receipt JSON as the `data` field.
|
|
20
|
+
*/
|
|
21
|
+
export function formatReceiptEvent(receipt: StreamReceipt): string {
|
|
22
|
+
return `event: payment-receipt\ndata: ${JSON.stringify(receipt)}\n\n`
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Format a need-voucher event as a Server-Sent Event.
|
|
27
|
+
*
|
|
28
|
+
* Emitted when the channel balance is exhausted mid-stream.
|
|
29
|
+
* The client responds by sending a new voucher credential to
|
|
30
|
+
* any mppx-protected endpoint.
|
|
31
|
+
*/
|
|
32
|
+
export function formatNeedVoucherEvent(params: NeedVoucherEvent): string {
|
|
33
|
+
return `event: payment-need-voucher\ndata: ${JSON.stringify(params)}\n\n`
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Parsed SSE event (discriminated union by `type`).
|
|
38
|
+
*/
|
|
39
|
+
export type SseEvent =
|
|
40
|
+
| { type: 'message'; data: string }
|
|
41
|
+
| { type: 'payment-need-voucher'; data: NeedVoucherEvent }
|
|
42
|
+
| { type: 'payment-receipt'; data: StreamReceipt }
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Parse a raw SSE event string into a typed event.
|
|
46
|
+
*
|
|
47
|
+
* Handles the three event types used by mppx streaming:
|
|
48
|
+
* - `message` (default / no event field) — application data
|
|
49
|
+
* - `payment-need-voucher` — balance exhausted, client should send voucher
|
|
50
|
+
* - `payment-receipt` — final receipt
|
|
51
|
+
*/
|
|
52
|
+
export function parseEvent(raw: string): SseEvent | null {
|
|
53
|
+
let eventType = 'message'
|
|
54
|
+
const dataLines: string[] = []
|
|
55
|
+
|
|
56
|
+
for (const line of raw.split('\n')) {
|
|
57
|
+
if (line.startsWith('event: ')) {
|
|
58
|
+
eventType = line.slice(7).trim()
|
|
59
|
+
} else if (line.startsWith('data: ')) {
|
|
60
|
+
dataLines.push(line.slice(6))
|
|
61
|
+
} else if (line === 'data:') {
|
|
62
|
+
dataLines.push('')
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (dataLines.length === 0) return null
|
|
67
|
+
const data = dataLines.join('\n')
|
|
68
|
+
|
|
69
|
+
switch (eventType) {
|
|
70
|
+
case 'message':
|
|
71
|
+
return { type: 'message', data }
|
|
72
|
+
case 'payment-need-voucher':
|
|
73
|
+
return { type: 'payment-need-voucher', data: JSON.parse(data) as NeedVoucherEvent }
|
|
74
|
+
case 'payment-receipt':
|
|
75
|
+
return { type: 'payment-receipt', data: JSON.parse(data) as StreamReceipt }
|
|
76
|
+
default:
|
|
77
|
+
return { type: 'message', data }
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export type StreamController = {
|
|
82
|
+
charge(): Promise<void>
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Wrap an async iterable with payment metering, producing an SSE stream.
|
|
87
|
+
*
|
|
88
|
+
* `generate` may be either:
|
|
89
|
+
* - An `AsyncIterable<string>` — each yielded value is automatically charged
|
|
90
|
+
* (one `tickCost` per value).
|
|
91
|
+
* - A callback `(stream: StreamController) => AsyncIterable<string>` — the
|
|
92
|
+
* generator controls when charges happen by calling `stream.charge()`.
|
|
93
|
+
*
|
|
94
|
+
* For each emitted value the stream:
|
|
95
|
+
* 1. Deducts `tickCost` from the channel balance atomically (auto or manual).
|
|
96
|
+
* 2. If balance is sufficient, emits `event: message` with the value.
|
|
97
|
+
* 3. If balance is exhausted, emits `event: payment-need-voucher`
|
|
98
|
+
* and polls store until the client tops up the channel.
|
|
99
|
+
* 4. On generator completion, emits a final `event: payment-receipt`.
|
|
100
|
+
*
|
|
101
|
+
* Returns a `ReadableStream<Uint8Array>` suitable for use as an HTTP response body.
|
|
102
|
+
*/
|
|
103
|
+
export function serve(options: serve.Options): ReadableStream<Uint8Array> {
|
|
104
|
+
const {
|
|
105
|
+
store,
|
|
106
|
+
channelId,
|
|
107
|
+
challengeId,
|
|
108
|
+
tickCost,
|
|
109
|
+
generate,
|
|
110
|
+
pollIntervalMs = 100,
|
|
111
|
+
signal,
|
|
112
|
+
} = options
|
|
113
|
+
|
|
114
|
+
const encoder = new TextEncoder()
|
|
115
|
+
|
|
116
|
+
return new ReadableStream<Uint8Array>({
|
|
117
|
+
async start(controller) {
|
|
118
|
+
const aborted = () => signal?.aborted ?? false
|
|
119
|
+
const emit = (event: string) => controller.enqueue(encoder.encode(event))
|
|
120
|
+
|
|
121
|
+
const charge = () =>
|
|
122
|
+
chargeOrWait({
|
|
123
|
+
store,
|
|
124
|
+
channelId,
|
|
125
|
+
amount: tickCost,
|
|
126
|
+
emit,
|
|
127
|
+
pollIntervalMs,
|
|
128
|
+
signal,
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
const iterable: AsyncIterable<string> =
|
|
132
|
+
typeof generate === 'function' ? generate({ charge }) : generate
|
|
133
|
+
|
|
134
|
+
try {
|
|
135
|
+
for await (const value of iterable) {
|
|
136
|
+
if (aborted()) break
|
|
137
|
+
|
|
138
|
+
if (typeof generate !== 'function') await charge()
|
|
139
|
+
|
|
140
|
+
controller.enqueue(encoder.encode(`event: message\ndata: ${value}\n\n`))
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (!aborted()) {
|
|
144
|
+
const channel = await store.getChannel(channelId)
|
|
145
|
+
if (channel) {
|
|
146
|
+
const receipt = createStreamReceipt({
|
|
147
|
+
challengeId,
|
|
148
|
+
channelId,
|
|
149
|
+
acceptedCumulative: channel.highestVoucherAmount,
|
|
150
|
+
spent: channel.spent,
|
|
151
|
+
units: channel.units,
|
|
152
|
+
})
|
|
153
|
+
controller.enqueue(encoder.encode(formatReceiptEvent(receipt)))
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
} catch (e) {
|
|
157
|
+
if (!aborted()) controller.error(e)
|
|
158
|
+
} finally {
|
|
159
|
+
controller.close()
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export declare namespace serve {
|
|
166
|
+
type Options = {
|
|
167
|
+
store: ChannelStore.ChannelStore
|
|
168
|
+
channelId: Hex
|
|
169
|
+
challengeId: string
|
|
170
|
+
tickCost: bigint
|
|
171
|
+
generate: AsyncIterable<string> | ((stream: StreamController) => AsyncIterable<string>)
|
|
172
|
+
pollIntervalMs?: number | undefined
|
|
173
|
+
signal?: AbortSignal | undefined
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Wrap a `ReadableStream<Uint8Array>` (from {@link serve}) in an HTTP
|
|
179
|
+
* `Response` with the correct SSE headers.
|
|
180
|
+
*/
|
|
181
|
+
export function toResponse(body: ReadableStream<Uint8Array>): Response {
|
|
182
|
+
return new Response(body, {
|
|
183
|
+
headers: {
|
|
184
|
+
'Cache-Control': 'no-cache, no-transform',
|
|
185
|
+
Connection: 'keep-alive',
|
|
186
|
+
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
187
|
+
},
|
|
188
|
+
})
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Extract `channelId`, `challengeId`, and `tickCost` from a `Request`'s
|
|
193
|
+
* `Authorization: Payment …` header.
|
|
194
|
+
*
|
|
195
|
+
* This is a convenience for callers that receive a raw `Request` and need
|
|
196
|
+
* the parameters required by {@link serve}.
|
|
197
|
+
*/
|
|
198
|
+
export function fromRequest(request: Request): fromRequest.Context {
|
|
199
|
+
const header = request.headers.get('Authorization')
|
|
200
|
+
if (!header) throw new Error('Missing Authorization header.')
|
|
201
|
+
|
|
202
|
+
const payment = Credential.extractPaymentScheme(header)
|
|
203
|
+
if (!payment) throw new Error('Missing Payment credential in Authorization header.')
|
|
204
|
+
|
|
205
|
+
const credential = Credential.deserialize(payment)
|
|
206
|
+
const payload = credential.payload as StreamCredentialPayload
|
|
207
|
+
return {
|
|
208
|
+
challengeId: credential.challenge.id,
|
|
209
|
+
channelId: payload.channelId,
|
|
210
|
+
tickCost: BigInt(credential.challenge.request.amount as string),
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export declare namespace fromRequest {
|
|
215
|
+
type Context = {
|
|
216
|
+
challengeId: string
|
|
217
|
+
channelId: Hex
|
|
218
|
+
tickCost: bigint
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Atomically deduct `amount` from a channel, retrying when balance is
|
|
224
|
+
* insufficient. Uses `store.waitForUpdate()` when available for
|
|
225
|
+
* event-driven wakeups, falling back to polling otherwise. Emits
|
|
226
|
+
* `payment-need-voucher` events via `emit` while waiting.
|
|
227
|
+
*/
|
|
228
|
+
async function chargeOrWait(options: {
|
|
229
|
+
store: ChannelStore.ChannelStore
|
|
230
|
+
channelId: Hex
|
|
231
|
+
amount: bigint
|
|
232
|
+
emit: (event: string) => void
|
|
233
|
+
pollIntervalMs: number
|
|
234
|
+
signal?: AbortSignal | undefined
|
|
235
|
+
}): Promise<void> {
|
|
236
|
+
const { store, channelId, amount, emit, pollIntervalMs, signal } = options
|
|
237
|
+
|
|
238
|
+
let result = await ChannelStore.deductFromChannel(store, channelId, amount)
|
|
239
|
+
|
|
240
|
+
if (!result.ok) {
|
|
241
|
+
// Emit a single need-voucher event, then poll/wait until the client
|
|
242
|
+
// sends an updated voucher. The requiredCumulative is constant here:
|
|
243
|
+
// `spent` only changes on successful deduction (which exits the loop),
|
|
244
|
+
// so re-emitting on every poll cycle would just cause redundant
|
|
245
|
+
// voucher POSTs from the client.
|
|
246
|
+
emit(
|
|
247
|
+
formatNeedVoucherEvent({
|
|
248
|
+
channelId,
|
|
249
|
+
requiredCumulative: (result.channel.spent + amount).toString(),
|
|
250
|
+
acceptedCumulative: result.channel.highestVoucherAmount.toString(),
|
|
251
|
+
deposit: result.channel.deposit.toString(),
|
|
252
|
+
}),
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
while (!result.ok) {
|
|
256
|
+
await waitForUpdate(store, channelId, pollIntervalMs, signal)
|
|
257
|
+
result = await ChannelStore.deductFromChannel(store, channelId, amount)
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async function waitForUpdate(
|
|
263
|
+
store: ChannelStore.ChannelStore,
|
|
264
|
+
channelId: Hex,
|
|
265
|
+
pollIntervalMs: number,
|
|
266
|
+
signal?: AbortSignal,
|
|
267
|
+
): Promise<void> {
|
|
268
|
+
if (signal?.aborted) throw new Error('Aborted while waiting for voucher')
|
|
269
|
+
if (store.waitForUpdate) {
|
|
270
|
+
await Promise.race([store.waitForUpdate(channelId), ...(signal ? [abortPromise(signal)] : [])])
|
|
271
|
+
} else {
|
|
272
|
+
await sleep(pollIntervalMs)
|
|
273
|
+
}
|
|
274
|
+
if (signal?.aborted) throw new Error('Aborted while waiting for voucher')
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function abortPromise(signal: AbortSignal): Promise<void> {
|
|
278
|
+
return new Promise((resolve) => {
|
|
279
|
+
if (signal.aborted) return resolve()
|
|
280
|
+
signal.addEventListener('abort', () => resolve(), { once: true })
|
|
281
|
+
})
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function sleep(ms: number): Promise<void> {
|
|
285
|
+
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Check whether a `Response` carries an SSE event stream.
|
|
290
|
+
*
|
|
291
|
+
* Returns `true` when the `Content-Type` header starts with
|
|
292
|
+
* `text/event-stream` (case-insensitive, ignoring charset params).
|
|
293
|
+
*/
|
|
294
|
+
export function isEventStream(response: Response): boolean {
|
|
295
|
+
const ct = response.headers.get('content-type')
|
|
296
|
+
return ct?.toLowerCase().startsWith('text/event-stream') ?? false
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Parse an SSE `Response` body into an async iterable of `data:` payloads.
|
|
301
|
+
*
|
|
302
|
+
* Yields the raw `data:` field content for each SSE event in the stream.
|
|
303
|
+
* Events whose data matches the `skip` predicate are silently dropped
|
|
304
|
+
* (e.g. `[DONE]` sentinels used by OpenAI-compatible APIs).
|
|
305
|
+
*
|
|
306
|
+
* Each yielded value typically becomes one charge tick when fed to
|
|
307
|
+
* {@link serve} via the SSE transport's auto-charge mode.
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
310
|
+
* ```ts
|
|
311
|
+
* const upstream = await fetch('https://api.example.com/stream')
|
|
312
|
+
* for await (const data of Sse.iterateData(upstream)) {
|
|
313
|
+
* console.log(data)
|
|
314
|
+
* }
|
|
315
|
+
* ```
|
|
316
|
+
*/
|
|
317
|
+
export async function* iterateData(
|
|
318
|
+
response: Response,
|
|
319
|
+
options?: iterateData.Options,
|
|
320
|
+
): AsyncGenerator<string> {
|
|
321
|
+
const skip = options?.skip
|
|
322
|
+
const body = response.body
|
|
323
|
+
if (!body) return
|
|
324
|
+
|
|
325
|
+
const reader = body.getReader()
|
|
326
|
+
const decoder = new TextDecoder()
|
|
327
|
+
let buffer = ''
|
|
328
|
+
|
|
329
|
+
try {
|
|
330
|
+
while (true) {
|
|
331
|
+
const { value, done } = await reader.read()
|
|
332
|
+
if (done) break
|
|
333
|
+
|
|
334
|
+
buffer += decoder.decode(value, { stream: true })
|
|
335
|
+
|
|
336
|
+
// Split on double-newline SSE event boundaries.
|
|
337
|
+
const events = buffer.split('\n\n')
|
|
338
|
+
// Last element may be incomplete — keep in buffer.
|
|
339
|
+
buffer = events.pop() ?? ''
|
|
340
|
+
|
|
341
|
+
for (const event of events) {
|
|
342
|
+
if (!event.trim()) continue
|
|
343
|
+
const data = extractData(event)
|
|
344
|
+
if (data === null) continue
|
|
345
|
+
if (skip?.(data)) continue
|
|
346
|
+
yield data
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// Flush remaining buffer.
|
|
351
|
+
if (buffer.trim()) {
|
|
352
|
+
const data = extractData(buffer)
|
|
353
|
+
if (data !== null && !skip?.(data)) yield data
|
|
354
|
+
}
|
|
355
|
+
} finally {
|
|
356
|
+
reader.releaseLock()
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
export declare namespace iterateData {
|
|
361
|
+
type Options = {
|
|
362
|
+
/** Predicate to skip specific data payloads (e.g. `d => d === '[DONE]'`). */
|
|
363
|
+
skip?: ((data: string) => boolean) | undefined
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** Extract the `data:` field value from a single SSE event block. */
|
|
368
|
+
function extractData(event: string): string | null {
|
|
369
|
+
const dataLines: string[] = []
|
|
370
|
+
for (const line of event.split('\n')) {
|
|
371
|
+
if (line.startsWith('data: ')) dataLines.push(line.slice(6))
|
|
372
|
+
else if (line === 'data:') dataLines.push('')
|
|
373
|
+
}
|
|
374
|
+
return dataLines.length > 0 ? dataLines.join('\n') : null
|
|
375
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { Address, Hex } from 'viem'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Voucher for cumulative payment.
|
|
5
|
+
* Cumulative monotonicity prevents replay attacks.
|
|
6
|
+
*/
|
|
7
|
+
export interface Voucher {
|
|
8
|
+
channelId: Hex
|
|
9
|
+
cumulativeAmount: bigint
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Signed voucher with EIP-712 signature.
|
|
14
|
+
*/
|
|
15
|
+
export interface SignedVoucher extends Voucher {
|
|
16
|
+
signature: Hex
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Stream credential payload (discriminated union).
|
|
21
|
+
*/
|
|
22
|
+
export type StreamCredentialPayload =
|
|
23
|
+
| {
|
|
24
|
+
action: 'open'
|
|
25
|
+
type: 'transaction'
|
|
26
|
+
channelId: Hex
|
|
27
|
+
transaction: Hex
|
|
28
|
+
signature: Hex
|
|
29
|
+
authorizedSigner?: Address | undefined
|
|
30
|
+
cumulativeAmount: string
|
|
31
|
+
}
|
|
32
|
+
| {
|
|
33
|
+
action: 'topUp'
|
|
34
|
+
type: 'transaction'
|
|
35
|
+
channelId: Hex
|
|
36
|
+
transaction: Hex
|
|
37
|
+
additionalDeposit: string
|
|
38
|
+
}
|
|
39
|
+
| {
|
|
40
|
+
action: 'voucher'
|
|
41
|
+
channelId: Hex
|
|
42
|
+
cumulativeAmount: string
|
|
43
|
+
signature: Hex
|
|
44
|
+
}
|
|
45
|
+
| {
|
|
46
|
+
action: 'close'
|
|
47
|
+
channelId: Hex
|
|
48
|
+
cumulativeAmount: string
|
|
49
|
+
signature: Hex
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* SSE event emitted when session balance is exhausted mid-stream.
|
|
54
|
+
* The client responds by sending a new voucher credential.
|
|
55
|
+
*
|
|
56
|
+
* Per spec §11.6, the event data contains:
|
|
57
|
+
* - `channelId` — channel identifier
|
|
58
|
+
* - `requiredCumulative` — minimum cumulative amount the next voucher must authorize
|
|
59
|
+
* - `acceptedCumulative` — current highest accepted voucher amount
|
|
60
|
+
* - `deposit` — current on-chain deposit ceiling; when `requiredCumulative > deposit`
|
|
61
|
+
* the client must top up the channel before sending a new voucher
|
|
62
|
+
*/
|
|
63
|
+
export interface NeedVoucherEvent {
|
|
64
|
+
channelId: Hex
|
|
65
|
+
requiredCumulative: string
|
|
66
|
+
acceptedCumulative: string
|
|
67
|
+
deposit: string
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Stream receipt returned in Payment-Receipt header.
|
|
72
|
+
*/
|
|
73
|
+
export interface StreamReceipt {
|
|
74
|
+
method: 'tempo'
|
|
75
|
+
intent: 'session'
|
|
76
|
+
status: 'success'
|
|
77
|
+
timestamp: string
|
|
78
|
+
/** Payment reference (channelId). Satisfies Receipt.Receipt contract. */
|
|
79
|
+
reference: string
|
|
80
|
+
challengeId: string
|
|
81
|
+
channelId: Hex
|
|
82
|
+
acceptedCumulative: string
|
|
83
|
+
spent: string
|
|
84
|
+
units?: number | undefined
|
|
85
|
+
txHash?: Hex | undefined
|
|
86
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { createClient, http } from 'viem'
|
|
2
|
+
import { privateKeyToAccount } from 'viem/accounts'
|
|
3
|
+
import { describe, expect, test } from 'vitest'
|
|
4
|
+
import { parseVoucherFromPayload, signVoucher, verifyVoucher } from './Voucher.js'
|
|
5
|
+
|
|
6
|
+
const account = privateKeyToAccount(
|
|
7
|
+
'0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80',
|
|
8
|
+
)
|
|
9
|
+
const escrowContract = '0x1234567890abcdef1234567890abcdef12345678' as const
|
|
10
|
+
const chainId = 42431
|
|
11
|
+
|
|
12
|
+
const client = createClient({
|
|
13
|
+
account,
|
|
14
|
+
transport: http('http://127.0.0.1'), // only used for local signTypedData
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
const channelId = '0x0000000000000000000000000000000000000000000000000000000000000001' as const
|
|
18
|
+
const cumulativeAmount = 1000000n
|
|
19
|
+
|
|
20
|
+
describe('Voucher', () => {
|
|
21
|
+
test('signVoucher and verifyVoucher round-trip', async () => {
|
|
22
|
+
const signature = await signVoucher(
|
|
23
|
+
client,
|
|
24
|
+
account,
|
|
25
|
+
{ channelId, cumulativeAmount },
|
|
26
|
+
escrowContract,
|
|
27
|
+
chainId,
|
|
28
|
+
)
|
|
29
|
+
expect(signature).toMatch(/^0x/)
|
|
30
|
+
expect(signature.length).toBe(132)
|
|
31
|
+
|
|
32
|
+
const isValid = await verifyVoucher(
|
|
33
|
+
escrowContract,
|
|
34
|
+
chainId,
|
|
35
|
+
{ channelId, cumulativeAmount, signature },
|
|
36
|
+
account.address,
|
|
37
|
+
)
|
|
38
|
+
expect(isValid).toBe(true)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
test('verifyVoucher rejects wrong signer', async () => {
|
|
42
|
+
const signature = await signVoucher(
|
|
43
|
+
client,
|
|
44
|
+
account,
|
|
45
|
+
{ channelId, cumulativeAmount },
|
|
46
|
+
escrowContract,
|
|
47
|
+
chainId,
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
const wrongAddress = '0x0000000000000000000000000000000000000001' as const
|
|
51
|
+
const isValid = await verifyVoucher(
|
|
52
|
+
escrowContract,
|
|
53
|
+
chainId,
|
|
54
|
+
{ channelId, cumulativeAmount, signature },
|
|
55
|
+
wrongAddress,
|
|
56
|
+
)
|
|
57
|
+
expect(isValid).toBe(false)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
test('verifyVoucher rejects tampered amount', async () => {
|
|
61
|
+
const signature = await signVoucher(
|
|
62
|
+
client,
|
|
63
|
+
account,
|
|
64
|
+
{ channelId, cumulativeAmount },
|
|
65
|
+
escrowContract,
|
|
66
|
+
chainId,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
const isValid = await verifyVoucher(
|
|
70
|
+
escrowContract,
|
|
71
|
+
chainId,
|
|
72
|
+
{ channelId, cumulativeAmount: 9999999n, signature },
|
|
73
|
+
account.address,
|
|
74
|
+
)
|
|
75
|
+
expect(isValid).toBe(false)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
test('verifyVoucher rejects tampered channelId', async () => {
|
|
79
|
+
const signature = await signVoucher(
|
|
80
|
+
client,
|
|
81
|
+
account,
|
|
82
|
+
{ channelId, cumulativeAmount },
|
|
83
|
+
escrowContract,
|
|
84
|
+
chainId,
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
const wrongChannelId =
|
|
88
|
+
'0x0000000000000000000000000000000000000000000000000000000000000099' as const
|
|
89
|
+
const isValid = await verifyVoucher(
|
|
90
|
+
escrowContract,
|
|
91
|
+
chainId,
|
|
92
|
+
{ channelId: wrongChannelId, cumulativeAmount, signature },
|
|
93
|
+
account.address,
|
|
94
|
+
)
|
|
95
|
+
expect(isValid).toBe(false)
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
test('verifyVoucher rejects wrong chain ID', async () => {
|
|
99
|
+
const signature = await signVoucher(
|
|
100
|
+
client,
|
|
101
|
+
account,
|
|
102
|
+
{ channelId, cumulativeAmount },
|
|
103
|
+
escrowContract,
|
|
104
|
+
chainId,
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
const isValid = await verifyVoucher(
|
|
108
|
+
escrowContract,
|
|
109
|
+
99999,
|
|
110
|
+
{ channelId, cumulativeAmount, signature },
|
|
111
|
+
account.address,
|
|
112
|
+
)
|
|
113
|
+
expect(isValid).toBe(false)
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
test('verifyVoucher returns false for invalid signature', async () => {
|
|
117
|
+
const isValid = await verifyVoucher(
|
|
118
|
+
escrowContract,
|
|
119
|
+
chainId,
|
|
120
|
+
{ channelId, cumulativeAmount, signature: '0xdeadbeef' },
|
|
121
|
+
account.address,
|
|
122
|
+
)
|
|
123
|
+
expect(isValid).toBe(false)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
test('parseVoucherFromPayload', () => {
|
|
127
|
+
const sig =
|
|
128
|
+
'0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890ab' as const
|
|
129
|
+
const voucher = parseVoucherFromPayload(channelId, '5000000', sig)
|
|
130
|
+
expect(voucher.channelId).toBe(channelId)
|
|
131
|
+
expect(voucher.cumulativeAmount).toBe(5000000n)
|
|
132
|
+
expect(voucher.signature).toBe(sig)
|
|
133
|
+
})
|
|
134
|
+
})
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { type Address, Signature } from 'ox'
|
|
2
|
+
import { SignatureEnvelope } from 'ox/tempo'
|
|
3
|
+
import type { Account, Client, Hex } from 'viem'
|
|
4
|
+
import { isAddressEqual, recoverTypedDataAddress } from 'viem'
|
|
5
|
+
import { signTypedData } from 'viem/actions'
|
|
6
|
+
import type { SignedVoucher, Voucher } from './Types.js'
|
|
7
|
+
|
|
8
|
+
/** Must match the on-chain TempoStreamChannel DOMAIN_SEPARATOR name. */
|
|
9
|
+
const DOMAIN_NAME = 'Tempo Stream Channel'
|
|
10
|
+
/** Must match the on-chain TempoStreamChannel DOMAIN_SEPARATOR version. */
|
|
11
|
+
const DOMAIN_VERSION = '1'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* EIP-712 domain for voucher signing.
|
|
15
|
+
*/
|
|
16
|
+
function getVoucherDomain(escrowContract: Address.Address, chainId: number) {
|
|
17
|
+
return {
|
|
18
|
+
name: DOMAIN_NAME,
|
|
19
|
+
version: DOMAIN_VERSION,
|
|
20
|
+
chainId,
|
|
21
|
+
verifyingContract: escrowContract,
|
|
22
|
+
} as const
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* EIP-712 types for voucher signing.
|
|
27
|
+
* Matches @tempo/stream-channels/voucher and on-chain VOUCHER_TYPEHASH.
|
|
28
|
+
*/
|
|
29
|
+
const voucherTypes = {
|
|
30
|
+
Voucher: [
|
|
31
|
+
{ name: 'channelId', type: 'bytes32' },
|
|
32
|
+
{ name: 'cumulativeAmount', type: 'uint128' },
|
|
33
|
+
],
|
|
34
|
+
} as const
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Sign a voucher with an account.
|
|
38
|
+
*/
|
|
39
|
+
export async function signVoucher(
|
|
40
|
+
client: Client,
|
|
41
|
+
account: Account,
|
|
42
|
+
message: Voucher,
|
|
43
|
+
escrowContract: Address.Address,
|
|
44
|
+
chainId: number,
|
|
45
|
+
authorizedSigner?: Address.Address | undefined,
|
|
46
|
+
): Promise<Hex> {
|
|
47
|
+
const signature = await signTypedData(client, {
|
|
48
|
+
account,
|
|
49
|
+
domain: getVoucherDomain(escrowContract, chainId),
|
|
50
|
+
types: voucherTypes,
|
|
51
|
+
primaryType: 'Voucher',
|
|
52
|
+
message: {
|
|
53
|
+
channelId: message.channelId,
|
|
54
|
+
cumulativeAmount: message.cumulativeAmount,
|
|
55
|
+
},
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
// When a separate authorizedSigner is used (e.g. access key), unwrap the
|
|
59
|
+
// keychain envelope — the escrow contract verifies raw ECDSA signatures
|
|
60
|
+
// against authorizedSigner, not keychain-wrapped ones.
|
|
61
|
+
// TODO: when TIP-1020 is implemented, we can remove this.
|
|
62
|
+
if (authorizedSigner) {
|
|
63
|
+
try {
|
|
64
|
+
const envelope = SignatureEnvelope.from(signature as SignatureEnvelope.Serialized)
|
|
65
|
+
if (envelope.type === 'keychain' && envelope.inner.type === 'secp256k1')
|
|
66
|
+
return Signature.toHex(envelope.inner.signature)
|
|
67
|
+
} catch {}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return signature
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Verify a voucher signature matches the expected signer.
|
|
75
|
+
*
|
|
76
|
+
* Supports both direct signatures (secp256k1/p256/webAuthn) and
|
|
77
|
+
* Tempo access key (keychain) signatures. For keychain signatures,
|
|
78
|
+
* the envelope's `userAddress` is compared to `expectedSigner`.
|
|
79
|
+
*/
|
|
80
|
+
export async function verifyVoucher(
|
|
81
|
+
escrowContract: Address.Address,
|
|
82
|
+
chainId: number,
|
|
83
|
+
voucher: SignedVoucher,
|
|
84
|
+
expectedSigner: Address.Address,
|
|
85
|
+
): Promise<boolean> {
|
|
86
|
+
try {
|
|
87
|
+
const domain = getVoucherDomain(escrowContract, chainId)
|
|
88
|
+
const message = {
|
|
89
|
+
channelId: voucher.channelId,
|
|
90
|
+
cumulativeAmount: voucher.cumulativeAmount,
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const envelope = SignatureEnvelope.from(voucher.signature as SignatureEnvelope.Serialized)
|
|
94
|
+
|
|
95
|
+
if (envelope.type === 'keychain') return isAddressEqual(envelope.userAddress, expectedSigner)
|
|
96
|
+
|
|
97
|
+
const signer = await recoverTypedDataAddress({
|
|
98
|
+
domain,
|
|
99
|
+
types: voucherTypes,
|
|
100
|
+
primaryType: 'Voucher',
|
|
101
|
+
message,
|
|
102
|
+
signature: voucher.signature,
|
|
103
|
+
})
|
|
104
|
+
return isAddressEqual(signer, expectedSigner)
|
|
105
|
+
} catch {
|
|
106
|
+
return false
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Parse a voucher from credential payload.
|
|
112
|
+
*/
|
|
113
|
+
export function parseVoucherFromPayload(
|
|
114
|
+
channelId: Hex,
|
|
115
|
+
cumulativeAmount: string,
|
|
116
|
+
signature: Hex,
|
|
117
|
+
): SignedVoucher {
|
|
118
|
+
return {
|
|
119
|
+
channelId,
|
|
120
|
+
cumulativeAmount: BigInt(cumulativeAmount),
|
|
121
|
+
signature,
|
|
122
|
+
}
|
|
123
|
+
}
|