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.
Files changed (446) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +195 -0
  3. package/dist/BodyDigest.d.ts +42 -0
  4. package/dist/BodyDigest.d.ts.map +1 -0
  5. package/dist/BodyDigest.js +40 -0
  6. package/dist/BodyDigest.js.map +1 -0
  7. package/dist/Challenge.d.ts +271 -0
  8. package/dist/Challenge.d.ts.map +1 -0
  9. package/dist/Challenge.js +291 -0
  10. package/dist/Challenge.js.map +1 -0
  11. package/dist/Credential.d.ts +91 -0
  12. package/dist/Credential.d.ts.map +1 -0
  13. package/dist/Credential.js +122 -0
  14. package/dist/Credential.js.map +1 -0
  15. package/dist/Errors.d.ts +243 -0
  16. package/dist/Errors.d.ts.map +1 -0
  17. package/dist/Errors.js +201 -0
  18. package/dist/Errors.js.map +1 -0
  19. package/dist/Expires.d.ts +15 -0
  20. package/dist/Expires.d.ts.map +1 -0
  21. package/dist/Expires.js +29 -0
  22. package/dist/Expires.js.map +1 -0
  23. package/dist/Intent.d.ts +101 -0
  24. package/dist/Intent.d.ts.map +1 -0
  25. package/dist/Intent.js +83 -0
  26. package/dist/Intent.js.map +1 -0
  27. package/dist/Mcp.d.ts +74 -0
  28. package/dist/Mcp.d.ts.map +1 -0
  29. package/dist/Mcp.js +9 -0
  30. package/dist/Mcp.js.map +1 -0
  31. package/dist/MethodIntent.d.ts +225 -0
  32. package/dist/MethodIntent.d.ts.map +1 -0
  33. package/dist/MethodIntent.js +156 -0
  34. package/dist/MethodIntent.js.map +1 -0
  35. package/dist/PaymentRequest.d.ts +88 -0
  36. package/dist/PaymentRequest.d.ts.map +1 -0
  37. package/dist/PaymentRequest.js +81 -0
  38. package/dist/PaymentRequest.js.map +1 -0
  39. package/dist/Receipt.d.ts +110 -0
  40. package/dist/Receipt.d.ts.map +1 -0
  41. package/dist/Receipt.js +105 -0
  42. package/dist/Receipt.js.map +1 -0
  43. package/dist/Store.d.ts +28 -0
  44. package/dist/Store.d.ts.map +1 -0
  45. package/dist/Store.js +61 -0
  46. package/dist/Store.js.map +1 -0
  47. package/dist/cli.d.ts +3 -0
  48. package/dist/cli.d.ts.map +1 -0
  49. package/dist/cli.js +1219 -0
  50. package/dist/cli.js.map +1 -0
  51. package/dist/client/Methods.d.ts +4 -0
  52. package/dist/client/Methods.d.ts.map +1 -0
  53. package/dist/client/Methods.js +4 -0
  54. package/dist/client/Methods.js.map +1 -0
  55. package/dist/client/Mppx.d.ts +84 -0
  56. package/dist/client/Mppx.d.ts.map +1 -0
  57. package/dist/client/Mppx.js +64 -0
  58. package/dist/client/Mppx.js.map +1 -0
  59. package/dist/client/Transport.d.ts +56 -0
  60. package/dist/client/Transport.d.ts.map +1 -0
  61. package/dist/client/Transport.js +81 -0
  62. package/dist/client/Transport.js.map +1 -0
  63. package/dist/client/index.d.ts +5 -0
  64. package/dist/client/index.d.ts.map +1 -0
  65. package/dist/client/index.js +5 -0
  66. package/dist/client/index.js.map +1 -0
  67. package/dist/client/internal/Fetch.d.ts +85 -0
  68. package/dist/client/internal/Fetch.d.ts.map +1 -0
  69. package/dist/client/internal/Fetch.js +95 -0
  70. package/dist/client/internal/Fetch.js.map +1 -0
  71. package/dist/index.d.ts +13 -0
  72. package/dist/index.d.ts.map +1 -0
  73. package/dist/index.js +13 -0
  74. package/dist/index.js.map +1 -0
  75. package/dist/internal/types.d.ts +302 -0
  76. package/dist/internal/types.d.ts.map +1 -0
  77. package/dist/internal/types.js +2 -0
  78. package/dist/internal/types.js.map +1 -0
  79. package/dist/mcp-sdk/client/McpClient.d.ts +78 -0
  80. package/dist/mcp-sdk/client/McpClient.d.ts.map +1 -0
  81. package/dist/mcp-sdk/client/McpClient.js +98 -0
  82. package/dist/mcp-sdk/client/McpClient.js.map +1 -0
  83. package/dist/mcp-sdk/client/index.d.ts +3 -0
  84. package/dist/mcp-sdk/client/index.d.ts.map +1 -0
  85. package/dist/mcp-sdk/client/index.js +3 -0
  86. package/dist/mcp-sdk/client/index.js.map +1 -0
  87. package/dist/mcp-sdk/server/Transport.d.ts +43 -0
  88. package/dist/mcp-sdk/server/Transport.d.ts.map +1 -0
  89. package/dist/mcp-sdk/server/Transport.js +71 -0
  90. package/dist/mcp-sdk/server/Transport.js.map +1 -0
  91. package/dist/mcp-sdk/server/index.d.ts +2 -0
  92. package/dist/mcp-sdk/server/index.d.ts.map +1 -0
  93. package/dist/mcp-sdk/server/index.js +2 -0
  94. package/dist/mcp-sdk/server/index.js.map +1 -0
  95. package/dist/middlewares/elysia.d.ts +51 -0
  96. package/dist/middlewares/elysia.d.ts.map +1 -0
  97. package/dist/middlewares/elysia.js +59 -0
  98. package/dist/middlewares/elysia.js.map +1 -0
  99. package/dist/middlewares/express.d.ts +46 -0
  100. package/dist/middlewares/express.d.ts.map +1 -0
  101. package/dist/middlewares/express.js +69 -0
  102. package/dist/middlewares/express.js.map +1 -0
  103. package/dist/middlewares/hono.d.ts +46 -0
  104. package/dist/middlewares/hono.d.ts.map +1 -0
  105. package/dist/middlewares/hono.js +57 -0
  106. package/dist/middlewares/hono.js.map +1 -0
  107. package/dist/middlewares/internal/mppx.d.ts +16 -0
  108. package/dist/middlewares/internal/mppx.d.ts.map +1 -0
  109. package/dist/middlewares/internal/mppx.js +16 -0
  110. package/dist/middlewares/internal/mppx.js.map +1 -0
  111. package/dist/middlewares/nextjs.d.ts +45 -0
  112. package/dist/middlewares/nextjs.d.ts.map +1 -0
  113. package/dist/middlewares/nextjs.js +57 -0
  114. package/dist/middlewares/nextjs.js.map +1 -0
  115. package/dist/proxy/Proxy.d.ts +47 -0
  116. package/dist/proxy/Proxy.d.ts.map +1 -0
  117. package/dist/proxy/Proxy.js +126 -0
  118. package/dist/proxy/Proxy.js.map +1 -0
  119. package/dist/proxy/Service.d.ts +100 -0
  120. package/dist/proxy/Service.d.ts.map +1 -0
  121. package/dist/proxy/Service.js +147 -0
  122. package/dist/proxy/Service.js.map +1 -0
  123. package/dist/proxy/index.d.ts +7 -0
  124. package/dist/proxy/index.d.ts.map +1 -0
  125. package/dist/proxy/index.js +7 -0
  126. package/dist/proxy/index.js.map +1 -0
  127. package/dist/proxy/internal/Headers.d.ts +3 -0
  128. package/dist/proxy/internal/Headers.d.ts.map +1 -0
  129. package/dist/proxy/internal/Headers.js +41 -0
  130. package/dist/proxy/internal/Headers.js.map +1 -0
  131. package/dist/proxy/internal/Route.d.ts +14 -0
  132. package/dist/proxy/internal/Route.d.ts.map +1 -0
  133. package/dist/proxy/internal/Route.js +47 -0
  134. package/dist/proxy/internal/Route.js.map +1 -0
  135. package/dist/proxy/services/anthropic.d.ts +29 -0
  136. package/dist/proxy/services/anthropic.d.ts.map +1 -0
  137. package/dist/proxy/services/anthropic.js +30 -0
  138. package/dist/proxy/services/anthropic.js.map +1 -0
  139. package/dist/proxy/services/openai.d.ts +29 -0
  140. package/dist/proxy/services/openai.d.ts.map +1 -0
  141. package/dist/proxy/services/openai.js +30 -0
  142. package/dist/proxy/services/openai.js.map +1 -0
  143. package/dist/proxy/services/stripe.d.ts +29 -0
  144. package/dist/proxy/services/stripe.d.ts.map +1 -0
  145. package/dist/proxy/services/stripe.js +30 -0
  146. package/dist/proxy/services/stripe.js.map +1 -0
  147. package/dist/server/Methods.d.ts +3 -0
  148. package/dist/server/Methods.d.ts.map +1 -0
  149. package/dist/server/Methods.js +3 -0
  150. package/dist/server/Methods.js.map +1 -0
  151. package/dist/server/Mppx.d.ts +116 -0
  152. package/dist/server/Mppx.d.ts.map +1 -0
  153. package/dist/server/Mppx.js +207 -0
  154. package/dist/server/Mppx.js.map +1 -0
  155. package/dist/server/NodeListener.d.ts +3 -0
  156. package/dist/server/NodeListener.d.ts.map +1 -0
  157. package/dist/server/NodeListener.js +3 -0
  158. package/dist/server/NodeListener.js.map +1 -0
  159. package/dist/server/Request.d.ts +24 -0
  160. package/dist/server/Request.d.ts.map +1 -0
  161. package/dist/server/Request.js +26 -0
  162. package/dist/server/Request.js.map +1 -0
  163. package/dist/server/Response.d.ts +10 -0
  164. package/dist/server/Response.d.ts.map +1 -0
  165. package/dist/server/Response.js +15 -0
  166. package/dist/server/Response.js.map +1 -0
  167. package/dist/server/Transport.d.ts +93 -0
  168. package/dist/server/Transport.d.ts.map +1 -0
  169. package/dist/server/Transport.js +132 -0
  170. package/dist/server/Transport.js.map +1 -0
  171. package/dist/server/index.d.ts +9 -0
  172. package/dist/server/index.d.ts.map +1 -0
  173. package/dist/server/index.js +9 -0
  174. package/dist/server/index.js.map +1 -0
  175. package/dist/stripe/Intents.d.ts +54 -0
  176. package/dist/stripe/Intents.d.ts.map +1 -0
  177. package/dist/stripe/Intents.js +27 -0
  178. package/dist/stripe/Intents.js.map +1 -0
  179. package/dist/stripe/client/Charge.d.ts +114 -0
  180. package/dist/stripe/client/Charge.d.ts.map +1 -0
  181. package/dist/stripe/client/Charge.js +77 -0
  182. package/dist/stripe/client/Charge.js.map +1 -0
  183. package/dist/stripe/client/MethodIntents.d.ts +80 -0
  184. package/dist/stripe/client/MethodIntents.d.ts.map +1 -0
  185. package/dist/stripe/client/MethodIntents.js +34 -0
  186. package/dist/stripe/client/MethodIntents.js.map +1 -0
  187. package/dist/stripe/client/index.d.ts +3 -0
  188. package/dist/stripe/client/index.d.ts.map +1 -0
  189. package/dist/stripe/client/index.js +3 -0
  190. package/dist/stripe/client/index.js.map +1 -0
  191. package/dist/stripe/index.d.ts +2 -0
  192. package/dist/stripe/index.d.ts.map +1 -0
  193. package/dist/stripe/index.js +2 -0
  194. package/dist/stripe/index.js.map +1 -0
  195. package/dist/stripe/server/Charge.d.ts +74 -0
  196. package/dist/stripe/server/Charge.d.ts.map +1 -0
  197. package/dist/stripe/server/Charge.js +79 -0
  198. package/dist/stripe/server/Charge.js.map +1 -0
  199. package/dist/stripe/server/MethodIntents.d.ts +65 -0
  200. package/dist/stripe/server/MethodIntents.d.ts.map +1 -0
  201. package/dist/stripe/server/MethodIntents.js +21 -0
  202. package/dist/stripe/server/MethodIntents.js.map +1 -0
  203. package/dist/stripe/server/index.d.ts +3 -0
  204. package/dist/stripe/server/index.d.ts.map +1 -0
  205. package/dist/stripe/server/index.js +3 -0
  206. package/dist/stripe/server/index.js.map +1 -0
  207. package/dist/tempo/Attribution.d.ts +101 -0
  208. package/dist/tempo/Attribution.d.ts.map +1 -0
  209. package/dist/tempo/Attribution.js +124 -0
  210. package/dist/tempo/Attribution.js.map +1 -0
  211. package/dist/tempo/Intents.d.ts +132 -0
  212. package/dist/tempo/Intents.d.ts.map +1 -0
  213. package/dist/tempo/Intents.js +81 -0
  214. package/dist/tempo/Intents.js.map +1 -0
  215. package/dist/tempo/client/ChannelOps.d.ts +54 -0
  216. package/dist/tempo/client/ChannelOps.d.ts.map +1 -0
  217. package/dist/tempo/client/ChannelOps.js +138 -0
  218. package/dist/tempo/client/ChannelOps.js.map +1 -0
  219. package/dist/tempo/client/Charge.d.ts +76 -0
  220. package/dist/tempo/client/Charge.d.ts.map +1 -0
  221. package/dist/tempo/client/Charge.js +69 -0
  222. package/dist/tempo/client/Charge.js.map +1 -0
  223. package/dist/tempo/client/MethodIntents.d.ts +157 -0
  224. package/dist/tempo/client/MethodIntents.d.ts.map +1 -0
  225. package/dist/tempo/client/MethodIntents.js +25 -0
  226. package/dist/tempo/client/MethodIntents.js.map +1 -0
  227. package/dist/tempo/client/Session.d.ts +159 -0
  228. package/dist/tempo/client/Session.d.ts.map +1 -0
  229. package/dist/tempo/client/Session.js +263 -0
  230. package/dist/tempo/client/Session.js.map +1 -0
  231. package/dist/tempo/client/SessionManager.d.ts +62 -0
  232. package/dist/tempo/client/SessionManager.d.ts.map +1 -0
  233. package/dist/tempo/client/SessionManager.js +196 -0
  234. package/dist/tempo/client/SessionManager.js.map +1 -0
  235. package/dist/tempo/client/index.d.ts +6 -0
  236. package/dist/tempo/client/index.d.ts.map +1 -0
  237. package/dist/tempo/client/index.js +5 -0
  238. package/dist/tempo/client/index.js.map +1 -0
  239. package/dist/tempo/index.d.ts +3 -0
  240. package/dist/tempo/index.d.ts.map +1 -0
  241. package/dist/tempo/index.js +3 -0
  242. package/dist/tempo/index.js.map +1 -0
  243. package/dist/tempo/internal/account.d.ts +32 -0
  244. package/dist/tempo/internal/account.d.ts.map +1 -0
  245. package/dist/tempo/internal/account.js +33 -0
  246. package/dist/tempo/internal/account.js.map +1 -0
  247. package/dist/tempo/internal/defaults.d.ts +18 -0
  248. package/dist/tempo/internal/defaults.d.ts.map +1 -0
  249. package/dist/tempo/internal/defaults.js +18 -0
  250. package/dist/tempo/internal/defaults.js.map +1 -0
  251. package/dist/tempo/internal/types.d.ts +11 -0
  252. package/dist/tempo/internal/types.d.ts.map +1 -0
  253. package/dist/tempo/internal/types.js +2 -0
  254. package/dist/tempo/internal/types.js.map +1 -0
  255. package/dist/tempo/server/Charge.d.ts +77 -0
  256. package/dist/tempo/server/Charge.d.ts.map +1 -0
  257. package/dist/tempo/server/Charge.js +228 -0
  258. package/dist/tempo/server/Charge.js.map +1 -0
  259. package/dist/tempo/server/MethodIntents.d.ts +140 -0
  260. package/dist/tempo/server/MethodIntents.d.ts.map +1 -0
  261. package/dist/tempo/server/MethodIntents.js +26 -0
  262. package/dist/tempo/server/MethodIntents.js.map +1 -0
  263. package/dist/tempo/server/Session.d.ts +148 -0
  264. package/dist/tempo/server/Session.d.ts.map +1 -0
  265. package/dist/tempo/server/Session.js +529 -0
  266. package/dist/tempo/server/Session.js.map +1 -0
  267. package/dist/tempo/server/internal/transport.d.ts +47 -0
  268. package/dist/tempo/server/internal/transport.d.ts.map +1 -0
  269. package/dist/tempo/server/internal/transport.js +118 -0
  270. package/dist/tempo/server/internal/transport.js.map +1 -0
  271. package/dist/tempo/stream/Chain.d.ts +52 -0
  272. package/dist/tempo/stream/Chain.d.ts.map +1 -0
  273. package/dist/tempo/stream/Chain.js +215 -0
  274. package/dist/tempo/stream/Chain.js.map +1 -0
  275. package/dist/tempo/stream/Channel.d.ts +26 -0
  276. package/dist/tempo/stream/Channel.d.ts.map +1 -0
  277. package/dist/tempo/stream/Channel.js +27 -0
  278. package/dist/tempo/stream/Channel.js.map +1 -0
  279. package/dist/tempo/stream/ChannelStore.d.ts +103 -0
  280. package/dist/tempo/stream/ChannelStore.d.ts.map +1 -0
  281. package/dist/tempo/stream/ChannelStore.js +100 -0
  282. package/dist/tempo/stream/ChannelStore.js.map +1 -0
  283. package/dist/tempo/stream/Receipt.d.ts +22 -0
  284. package/dist/tempo/stream/Receipt.d.ts.map +1 -0
  285. package/dist/tempo/stream/Receipt.js +34 -0
  286. package/dist/tempo/stream/Receipt.js.map +1 -0
  287. package/dist/tempo/stream/Sse.d.ts +134 -0
  288. package/dist/tempo/stream/Sse.d.ts.map +1 -0
  289. package/dist/tempo/stream/Sse.js +288 -0
  290. package/dist/tempo/stream/Sse.js.map +1 -0
  291. package/dist/tempo/stream/Types.d.ts +78 -0
  292. package/dist/tempo/stream/Types.d.ts.map +1 -0
  293. package/dist/tempo/stream/Types.js +2 -0
  294. package/dist/tempo/stream/Types.js.map +1 -0
  295. package/dist/tempo/stream/Voucher.d.ts +20 -0
  296. package/dist/tempo/stream/Voucher.d.ts.map +1 -0
  297. package/dist/tempo/stream/Voucher.js +98 -0
  298. package/dist/tempo/stream/Voucher.js.map +1 -0
  299. package/dist/tempo/stream/escrow.abi.d.ts +598 -0
  300. package/dist/tempo/stream/escrow.abi.d.ts.map +1 -0
  301. package/dist/tempo/stream/escrow.abi.js +760 -0
  302. package/dist/tempo/stream/escrow.abi.js.map +1 -0
  303. package/dist/tempo/stream/index.d.ts +8 -0
  304. package/dist/tempo/stream/index.d.ts.map +1 -0
  305. package/dist/tempo/stream/index.js +8 -0
  306. package/dist/tempo/stream/index.js.map +1 -0
  307. package/dist/viem/Account.d.ts +12 -0
  308. package/dist/viem/Account.d.ts.map +1 -0
  309. package/dist/viem/Account.js +14 -0
  310. package/dist/viem/Account.js.map +1 -0
  311. package/dist/viem/Client.d.ts +21 -0
  312. package/dist/viem/Client.d.ts.map +1 -0
  313. package/dist/viem/Client.js +19 -0
  314. package/dist/viem/Client.js.map +1 -0
  315. package/dist/zod.d.ts +17 -0
  316. package/dist/zod.d.ts.map +1 -0
  317. package/dist/zod.js +35 -0
  318. package/dist/zod.js.map +1 -0
  319. package/package.json +117 -4
  320. package/src/BodyDigest.test.ts +43 -0
  321. package/src/BodyDigest.ts +53 -0
  322. package/src/Challenge.test-d.ts +81 -0
  323. package/src/Challenge.test.ts +414 -0
  324. package/src/Challenge.ts +429 -0
  325. package/src/Credential.test.ts +227 -0
  326. package/src/Credential.ts +154 -0
  327. package/src/Errors.test.ts +402 -0
  328. package/src/Errors.ts +348 -0
  329. package/src/Expires.ts +34 -0
  330. package/src/Intent.test.ts +180 -0
  331. package/src/Intent.ts +109 -0
  332. package/src/Mcp.ts +81 -0
  333. package/src/MethodIntent.test.ts +303 -0
  334. package/src/MethodIntent.ts +388 -0
  335. package/src/PaymentRequest.test.ts +152 -0
  336. package/src/PaymentRequest.ts +107 -0
  337. package/src/Receipt.test.ts +98 -0
  338. package/src/Receipt.ts +129 -0
  339. package/src/Store.ts +84 -0
  340. package/src/cli.test.ts +542 -0
  341. package/src/cli.ts +1319 -0
  342. package/src/client/Methods.ts +3 -0
  343. package/src/client/Mppx.test-d.ts +90 -0
  344. package/src/client/Mppx.test.ts +468 -0
  345. package/src/client/Mppx.ts +149 -0
  346. package/src/client/Transport.test.ts +283 -0
  347. package/src/client/Transport.ts +115 -0
  348. package/src/client/index.ts +4 -0
  349. package/src/client/internal/Fetch.test-d.ts +57 -0
  350. package/src/client/internal/Fetch.test.ts +281 -0
  351. package/src/client/internal/Fetch.ts +157 -0
  352. package/src/env.d.ts +11 -0
  353. package/src/index.ts +12 -0
  354. package/src/internal/types.ts +403 -0
  355. package/src/mcp-sdk/client/McpClient.test-d.ts +109 -0
  356. package/src/mcp-sdk/client/McpClient.test.ts +219 -0
  357. package/src/mcp-sdk/client/McpClient.ts +187 -0
  358. package/src/mcp-sdk/client/index.ts +2 -0
  359. package/src/mcp-sdk/server/Transport.ts +94 -0
  360. package/src/mcp-sdk/server/index.ts +1 -0
  361. package/src/middlewares/elysia.ts +66 -0
  362. package/src/middlewares/express.test.ts +155 -0
  363. package/src/middlewares/express.ts +82 -0
  364. package/src/middlewares/hono.test.ts +148 -0
  365. package/src/middlewares/hono.ts +62 -0
  366. package/src/middlewares/internal/mppx.ts +30 -0
  367. package/src/middlewares/nextjs.test.ts +164 -0
  368. package/src/middlewares/nextjs.ts +66 -0
  369. package/src/proxy/Proxy.test.ts +472 -0
  370. package/src/proxy/Proxy.ts +175 -0
  371. package/src/proxy/Service.test.ts +125 -0
  372. package/src/proxy/Service.ts +227 -0
  373. package/src/proxy/index.ts +6 -0
  374. package/src/proxy/internal/Headers.test.ts +100 -0
  375. package/src/proxy/internal/Headers.ts +40 -0
  376. package/src/proxy/internal/Route.test.ts +143 -0
  377. package/src/proxy/internal/Route.ts +54 -0
  378. package/src/proxy/services/anthropic.ts +45 -0
  379. package/src/proxy/services/openai.test.ts +97 -0
  380. package/src/proxy/services/openai.ts +48 -0
  381. package/src/proxy/services/stripe.ts +49 -0
  382. package/src/server/Methods.ts +2 -0
  383. package/src/server/Mppx.test-d.ts +343 -0
  384. package/src/server/Mppx.test.ts +342 -0
  385. package/src/server/Mppx.ts +378 -0
  386. package/src/server/NodeListener.test.ts +188 -0
  387. package/src/server/NodeListener.ts +3 -0
  388. package/src/server/Request.test.ts +102 -0
  389. package/src/server/Request.ts +33 -0
  390. package/src/server/Response.test.ts +31 -0
  391. package/src/server/Response.ts +27 -0
  392. package/src/server/Transport.test.ts +294 -0
  393. package/src/server/Transport.ts +222 -0
  394. package/src/server/index.ts +8 -0
  395. package/src/stripe/Charge.integration.test.ts +326 -0
  396. package/src/stripe/Intents.test.ts +52 -0
  397. package/src/stripe/Intents.ts +27 -0
  398. package/src/stripe/client/Charge.ts +119 -0
  399. package/src/stripe/client/MethodIntents.ts +37 -0
  400. package/src/stripe/client/index.ts +2 -0
  401. package/src/stripe/index.ts +1 -0
  402. package/src/stripe/server/Charge.ts +121 -0
  403. package/src/stripe/server/MethodIntents.ts +24 -0
  404. package/src/stripe/server/index.ts +2 -0
  405. package/src/tempo/Attribution.test.ts +187 -0
  406. package/src/tempo/Attribution.ts +156 -0
  407. package/src/tempo/Intents.test.ts +84 -0
  408. package/src/tempo/Intents.ts +93 -0
  409. package/src/tempo/client/ChannelOps.ts +233 -0
  410. package/src/tempo/client/Charge.ts +84 -0
  411. package/src/tempo/client/MethodIntents.ts +28 -0
  412. package/src/tempo/client/Session.ts +369 -0
  413. package/src/tempo/client/SessionManager.test.ts +223 -0
  414. package/src/tempo/client/SessionManager.ts +270 -0
  415. package/src/tempo/client/index.ts +5 -0
  416. package/src/tempo/index.ts +2 -0
  417. package/src/tempo/internal/account.ts +47 -0
  418. package/src/tempo/internal/defaults.ts +20 -0
  419. package/src/tempo/internal/types.ts +8 -0
  420. package/src/tempo/server/Charge.test.ts +847 -0
  421. package/src/tempo/server/Charge.ts +309 -0
  422. package/src/tempo/server/MethodIntents.ts +29 -0
  423. package/src/tempo/server/Session.test.ts +1349 -0
  424. package/src/tempo/server/Session.ts +773 -0
  425. package/src/tempo/server/Sse.test.ts +289 -0
  426. package/src/tempo/server/index.ts +5 -0
  427. package/src/tempo/server/internal/transport.ts +153 -0
  428. package/src/tempo/stream/Chain.ts +333 -0
  429. package/src/tempo/stream/Channel.ts +50 -0
  430. package/src/tempo/stream/ChannelStore.test.ts +473 -0
  431. package/src/tempo/stream/ChannelStore.ts +202 -0
  432. package/src/tempo/stream/Receipt.test.ts +84 -0
  433. package/src/tempo/stream/Receipt.ts +45 -0
  434. package/src/tempo/stream/Sse.test.ts +401 -0
  435. package/src/tempo/stream/Sse.ts +375 -0
  436. package/src/tempo/stream/Types.ts +86 -0
  437. package/src/tempo/stream/Voucher.test.ts +134 -0
  438. package/src/tempo/stream/Voucher.ts +123 -0
  439. package/src/tempo/stream/escrow.abi.ts +759 -0
  440. package/src/tempo/stream/index.ts +7 -0
  441. package/src/tsconfig.json +10 -0
  442. package/src/viem/Account.test.ts +71 -0
  443. package/src/viem/Account.ts +30 -0
  444. package/src/viem/Client.test.ts +58 -0
  445. package/src/viem/Client.ts +33 -0
  446. package/src/zod.ts +47 -0
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Tempo-specific SSE transport that wraps the base HTTP transport
3
+ * with metering logic (context capture from credentials, per-token
4
+ * charging via Sse.serve).
5
+ *
6
+ * @internal
7
+ */
8
+ import * as Transport from '../../../server/Transport.js';
9
+ import * as Sse_core from '../../stream/Sse.js';
10
+ /**
11
+ * Creates a Tempo-metered SSE transport.
12
+ *
13
+ * Wraps an HTTP transport with:
14
+ * - Context capture from credentials (channelId, tickCost)
15
+ * - Per-token charging via Sse.serve for generator/iterable responses
16
+ * - Auto-detection of upstream SSE responses
17
+ * - Fallback to standard HTTP receipt handling for plain Response
18
+ */
19
+ export function sse(options) {
20
+ const { pollingInterval, poll } = options;
21
+ // When `poll` is true, strip `waitForUpdate` so the SSE charge loop
22
+ // falls back to polling. This is needed for runtimes like Cloudflare Workers
23
+ // where resolving promises across request contexts is not supported.
24
+ const store = (() => {
25
+ if (!poll)
26
+ return options.store;
27
+ const { waitForUpdate: _, ...store } = options.store;
28
+ return store;
29
+ })();
30
+ const contextMap = new Map();
31
+ const base = Transport.http();
32
+ return Transport.from({
33
+ name: 'sse',
34
+ getCredential(request) {
35
+ const credential = base.getCredential(request);
36
+ if (credential) {
37
+ try {
38
+ const ctx = Sse_core.fromRequest(request);
39
+ contextMap.set(ctx.challengeId, ctx);
40
+ }
41
+ catch {
42
+ // ignore — non-SSE credentials won't have stream context
43
+ }
44
+ }
45
+ return credential;
46
+ },
47
+ respondChallenge(options) {
48
+ return base.respondChallenge(options);
49
+ },
50
+ respondReceipt({ receipt, response, challengeId }) {
51
+ // Auto-detect upstream SSE responses and parse them into an
52
+ // AsyncIterable so they flow through the metered pipeline.
53
+ // This lets proxy consumers simply pass `result.withReceipt(upstreamRes)`
54
+ // and get per-event charging automatically.
55
+ const resolved = response instanceof Response && Sse_core.isEventStream(response) && response.body
56
+ ? Sse_core.iterateData(response, { skip: (d) => d === '[DONE]' })
57
+ : response;
58
+ if (isAsyncGeneratorFunction(resolved) || isAsyncIterable(resolved)) {
59
+ const ctx = contextMap.get(challengeId);
60
+ if (!ctx)
61
+ throw new Error('No SSE context available — credential was not parsed');
62
+ contextMap.delete(challengeId);
63
+ // Pass async generator functions directly so Sse.serve gives them
64
+ // a StreamController for manual charge(). Pass raw AsyncIterables
65
+ // as-is so Sse.serve auto-charges per yielded value.
66
+ const generate = isAsyncGeneratorFunction(resolved)
67
+ ? resolved
68
+ : resolved;
69
+ const stream = Sse_core.serve({
70
+ store,
71
+ channelId: ctx.channelId,
72
+ challengeId,
73
+ tickCost: ctx.tickCost,
74
+ pollIntervalMs: pollingInterval,
75
+ generate,
76
+ });
77
+ return Sse_core.toResponse(stream);
78
+ }
79
+ return base.respondReceipt({ receipt, response: response, challengeId });
80
+ },
81
+ });
82
+ }
83
+ /** Default SSE serve: iterates values and emits `event: message` per value. */
84
+ export function defaultServe(options) {
85
+ const iterable = typeof options.generate === 'function' ? options.generate(undefined) : options.generate;
86
+ const encoder = new TextEncoder();
87
+ const stream = new ReadableStream({
88
+ async start(controller) {
89
+ try {
90
+ for await (const value of iterable) {
91
+ controller.enqueue(encoder.encode(`event: message\ndata: ${value}\n\n`));
92
+ }
93
+ }
94
+ catch (e) {
95
+ controller.error(e);
96
+ }
97
+ finally {
98
+ controller.close();
99
+ }
100
+ },
101
+ });
102
+ return new Response(stream, {
103
+ headers: {
104
+ 'Content-Type': 'text/event-stream; charset=utf-8',
105
+ 'Cache-Control': 'no-cache, no-transform',
106
+ Connection: 'keep-alive',
107
+ },
108
+ });
109
+ }
110
+ function isAsyncGeneratorFunction(value) {
111
+ if (typeof value !== 'function')
112
+ return false;
113
+ return value.constructor?.name === 'AsyncGeneratorFunction';
114
+ }
115
+ function isAsyncIterable(value) {
116
+ return value !== null && typeof value === 'object' && Symbol.asyncIterator in value;
117
+ }
118
+ //# sourceMappingURL=transport.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transport.js","sourceRoot":"","sources":["../../../../src/tempo/server/internal/transport.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,SAAS,MAAM,8BAA8B,CAAA;AAEzD,OAAO,KAAK,QAAQ,MAAM,qBAAqB,CAAA;AAK/C;;;;;;;;GAQG;AACH,MAAM,UAAU,GAAG,CAAC,OAA2D;IAC7E,MAAM,EAAE,eAAe,EAAE,IAAI,EAAE,GAAG,OAAO,CAAA;IAEzC,oEAAoE;IACpE,6EAA6E;IAC7E,qEAAqE;IACrE,MAAM,KAAK,GAAG,CAAC,GAAG,EAAE;QAClB,IAAI,CAAC,IAAI;YAAE,OAAO,OAAO,CAAC,KAAK,CAAA;QAC/B,MAAM,EAAE,aAAa,EAAE,CAAC,EAAE,GAAG,KAAK,EAAE,GAAG,OAAO,CAAC,KAAK,CAAA;QACpD,OAAO,KAAK,CAAA;IACd,CAAC,CAAC,EAAE,CAAA;IAEJ,MAAM,UAAU,GAAG,IAAI,GAAG,EAAwC,CAAA;IAElE,MAAM,IAAI,GAAG,SAAS,CAAC,IAAI,EAAE,CAAA;IAC7B,OAAO,SAAS,CAAC,IAAI,CAAgE;QACnF,IAAI,EAAE,KAAK;QAEX,aAAa,CAAC,OAAO;YACnB,MAAM,UAAU,GAAG,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,CAAA;YAC9C,IAAI,UAAU,EAAE,CAAC;gBACf,IAAI,CAAC;oBACH,MAAM,GAAG,GAAG,QAAQ,CAAC,WAAW,CAAC,OAAO,CAAC,CAAA;oBACzC,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,WAAW,EAAE,GAAG,CAAC,CAAA;gBACtC,CAAC;gBAAC,MAAM,CAAC;oBACP,yDAAyD;gBAC3D,CAAC;YACH,CAAC;YACD,OAAO,UAAU,CAAA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,KAAc;IACrC,OAAO,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,aAAa,IAAK,KAAgB,CAAA;AACjG,CAAC"}
@@ -0,0 +1,52 @@
1
+ import { type Account, type Address, type Client, type Hex, type ReadContractReturnType } from 'viem';
2
+ import { escrowAbi } from './escrow.abi.js';
3
+ import type { SignedVoucher } from './Types.js';
4
+ export { escrowAbi };
5
+ /**
6
+ * On-chain channel state from the escrow contract.
7
+ */
8
+ export type OnChainChannel = ReadContractReturnType<typeof escrowAbi, 'getChannel'>;
9
+ /**
10
+ * Read channel state from the escrow contract.
11
+ */
12
+ export declare function getOnChainChannel(client: Client, escrowContract: Address, channelId: Hex): Promise<OnChainChannel>;
13
+ /**
14
+ * Verify a topUp by re-reading on-chain channel state.
15
+ */
16
+ export declare function verifyTopUpTransaction(client: Client, escrowContract: Address, channelId: Hex, previousDeposit: bigint): Promise<{
17
+ deposit: bigint;
18
+ }>;
19
+ /**
20
+ * Submit a settle transaction on-chain.
21
+ */
22
+ export declare function settleOnChain(client: Client, escrowContract: Address, voucher: SignedVoucher): Promise<Hex>;
23
+ /**
24
+ * Submit a close transaction on-chain.
25
+ */
26
+ export declare function closeOnChain(client: Client, escrowContract: Address, voucher: SignedVoucher, account?: Account): Promise<Hex>;
27
+ export type BroadcastResult = {
28
+ txHash: Hex | undefined;
29
+ onChain: OnChainChannel;
30
+ };
31
+ export declare function broadcastOpenTransaction(parameters: {
32
+ client: Client;
33
+ serializedTransaction: Hex;
34
+ escrowContract: Address;
35
+ channelId: Hex;
36
+ recipient: Address;
37
+ currency: Address;
38
+ feePayer?: Account | undefined;
39
+ }): Promise<BroadcastResult>;
40
+ export declare function broadcastTopUpTransaction(parameters: {
41
+ client: Client;
42
+ serializedTransaction: Hex;
43
+ escrowContract: Address;
44
+ channelId: Hex;
45
+ declaredDeposit: bigint;
46
+ previousDeposit: bigint;
47
+ feePayer?: Account | undefined;
48
+ }): Promise<{
49
+ txHash: Hex;
50
+ newDeposit: bigint;
51
+ }>;
52
+ //# sourceMappingURL=Chain.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Chain.d.ts","sourceRoot":"","sources":["../../../src/tempo/stream/Chain.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,OAAO,EACZ,KAAK,OAAO,EACZ,KAAK,MAAM,EAGX,KAAK,GAAG,EAER,KAAK,sBAAsB,EAE5B,MAAM,MAAM,CAAA;AAIb,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAE/C,OAAO,EAAE,SAAS,EAAE,CAAA;AAIpB;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,sBAAsB,CAAC,OAAO,SAAS,EAAE,YAAY,CAAC,CAAA;AAEnF;;GAEG;AACH,wBAAsB,iBAAiB,CACrC,MAAM,EAAE,MAAM,EACd,cAAc,EAAE,OAAO,EACvB,SAAS,EAAE,GAAG,GACb,OAAO,CAAC,cAAc,CAAC,CAOzB;AAED;;GAEG;AACH,wBAAsB,sBAAsB,CAC1C,MAAM,EAAE,MAAM,EACd,cAAc,EAAE,OAAO,EACvB,SAAS,EAAE,GAAG,EACd,eAAe,EAAE,MAAM,GACtB,OAAO,CAAC;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC,CAY9B;AAQD;;GAEG;AACH,wBAAsB,aAAa,CACjC,MAAM,EAAE,MAAM,EACd,cAAc,EAAE,OAAO,EACvB,OAAO,EAAE,aAAa,GACrB,OAAO,CAAC,GAAG,CAAC,CAUd;AAED;;GAEG;AACH,wBAAsB,YAAY,CAChC,MAAM,EAAE,MAAM,EACd,cAAc,EAAE,OAAO,EACvB,OAAO,EAAE,aAAa,EACtB,OAAO,CAAC,EAAE,OAAO,GAChB,OAAO,CAAC,GAAG,CAAC,CAed;AAcD,MAAM,MAAM,eAAe,GAAG;IAC5B,MAAM,EAAE,GAAG,GAAG,SAAS,CAAA;IACvB,OAAO,EAAE,cAAc,CAAA;CACxB,CAAA;AAED,wBAAsB,wBAAwB,CAAC,UAAU,EAAE;IACzD,MAAM,EAAE,MAAM,CAAA;IACd,qBAAqB,EAAE,GAAG,CAAA;IAC1B,cAAc,EAAE,OAAO,CAAA;IACvB,SAAS,EAAE,GAAG,CAAA;IACd,SAAS,EAAE,OAAO,CAAA;IAClB,QAAQ,EAAE,OAAO,CAAA;IACjB,QAAQ,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;CAC/B,GAAG,OAAO,CAAC,eAAe,CAAC,CA+F3B;AAED,wBAAsB,yBAAyB,CAAC,UAAU,EAAE;IAC1D,MAAM,EAAE,MAAM,CAAA;IACd,qBAAqB,EAAE,GAAG,CAAA;IAC1B,cAAc,EAAE,OAAO,CAAA;IACvB,SAAS,EAAE,GAAG,CAAA;IACd,eAAe,EAAE,MAAM,CAAA;IACvB,eAAe,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;CAC/B,GAAG,OAAO,CAAC;IAAE,MAAM,EAAE,GAAG,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAAC,CAwF/C"}
@@ -0,0 +1,215 @@
1
+ import { decodeFunctionData, getAbiItem, isAddressEqual, toFunctionSelector, } from 'viem';
2
+ import { readContract, sendRawTransactionSync, signTransaction, writeContract } from 'viem/actions';
3
+ import { Transaction } from 'viem/tempo';
4
+ import { BadRequestError, ChannelClosedError, VerificationFailedError } from '../../Errors.js';
5
+ import { escrowAbi } from './escrow.abi.js';
6
+ export { escrowAbi };
7
+ const UINT128_MAX = 2n ** 128n - 1n;
8
+ /**
9
+ * Read channel state from the escrow contract.
10
+ */
11
+ export async function getOnChainChannel(client, escrowContract, channelId) {
12
+ return readContract(client, {
13
+ address: escrowContract,
14
+ abi: escrowAbi,
15
+ functionName: 'getChannel',
16
+ args: [channelId],
17
+ });
18
+ }
19
+ /**
20
+ * Verify a topUp by re-reading on-chain channel state.
21
+ */
22
+ export async function verifyTopUpTransaction(client, escrowContract, channelId, previousDeposit) {
23
+ const channel = await getOnChainChannel(client, escrowContract, channelId);
24
+ if (channel.finalized) {
25
+ throw new ChannelClosedError({ reason: 'channel is finalized on-chain' });
26
+ }
27
+ if (channel.deposit <= previousDeposit) {
28
+ throw new VerificationFailedError({ reason: 'channel deposit did not increase' });
29
+ }
30
+ return { deposit: channel.deposit };
31
+ }
32
+ function assertUint128(amount) {
33
+ if (amount < 0n || amount > UINT128_MAX) {
34
+ throw new VerificationFailedError({ reason: 'cumulativeAmount exceeds uint128 range' });
35
+ }
36
+ }
37
+ /**
38
+ * Submit a settle transaction on-chain.
39
+ */
40
+ export async function settleOnChain(client, escrowContract, voucher) {
41
+ assertUint128(voucher.cumulativeAmount);
42
+ return writeContract(client, {
43
+ account: client.account,
44
+ chain: client.chain,
45
+ address: escrowContract,
46
+ abi: escrowAbi,
47
+ functionName: 'settle',
48
+ args: [voucher.channelId, voucher.cumulativeAmount, voucher.signature],
49
+ });
50
+ }
51
+ /**
52
+ * Submit a close transaction on-chain.
53
+ */
54
+ export async function closeOnChain(client, escrowContract, voucher, account) {
55
+ assertUint128(voucher.cumulativeAmount);
56
+ const resolved = account ?? client.account;
57
+ if (!resolved)
58
+ throw new Error('Cannot close channel: no account available. Provide an `account` in the session config or a `getClient` that returns an account-bearing client.');
59
+ return writeContract(client, {
60
+ account: resolved,
61
+ chain: client.chain,
62
+ address: escrowContract,
63
+ abi: escrowAbi,
64
+ functionName: 'close',
65
+ args: [voucher.channelId, voucher.cumulativeAmount, voucher.signature],
66
+ });
67
+ }
68
+ const escrowOpenSelector = /*#__PURE__*/ toFunctionSelector(getAbiItem({ abi: escrowAbi, name: 'open' }));
69
+ const escrowTopUpSelector = /*#__PURE__*/ toFunctionSelector(getAbiItem({ abi: escrowAbi, name: 'topUp' }));
70
+ const erc20ApproveSelector = /*#__PURE__*/ toFunctionSelector('function approve(address spender, uint256 amount)');
71
+ export async function broadcastOpenTransaction(parameters) {
72
+ const { client, serializedTransaction, escrowContract, channelId, recipient, currency, feePayer, } = parameters;
73
+ const transaction = Transaction.deserialize(serializedTransaction);
74
+ const calls = transaction.calls ?? [];
75
+ const openCall = calls.find((call) => {
76
+ if (!call.to || !isAddressEqual(call.to, escrowContract))
77
+ return false;
78
+ if (!call.data)
79
+ return false;
80
+ return call.data.slice(0, 10) === escrowOpenSelector;
81
+ });
82
+ if (!openCall)
83
+ throw new BadRequestError({
84
+ reason: 'transaction does not contain a valid escrow open call',
85
+ });
86
+ if (feePayer) {
87
+ for (const call of calls) {
88
+ if (!call.to || !call.data) {
89
+ throw new BadRequestError({
90
+ reason: 'fee-sponsored transactions must not contain calls without target or data',
91
+ });
92
+ }
93
+ const selector = call.data.slice(0, 10);
94
+ const isEscrowOpen = isAddressEqual(call.to, escrowContract) && selector === escrowOpenSelector;
95
+ const isTokenApprove = isAddressEqual(call.to, currency) && selector === erc20ApproveSelector;
96
+ if (!isEscrowOpen && !isTokenApprove) {
97
+ throw new BadRequestError({
98
+ reason: 'fee-sponsored open transaction contains an unauthorized call',
99
+ });
100
+ }
101
+ }
102
+ }
103
+ const { args: openArgs } = decodeFunctionData({ abi: escrowAbi, data: openCall.data });
104
+ const [payee, token] = openArgs;
105
+ if (!isAddressEqual(payee, recipient)) {
106
+ throw new VerificationFailedError({
107
+ reason: 'open transaction payee does not match server recipient',
108
+ });
109
+ }
110
+ if (!isAddressEqual(token, currency)) {
111
+ throw new VerificationFailedError({
112
+ reason: 'open transaction token does not match server currency',
113
+ });
114
+ }
115
+ const serializedTransaction_final = await (async () => {
116
+ if (feePayer) {
117
+ return signTransaction(client, {
118
+ ...transaction,
119
+ account: feePayer,
120
+ feePayer,
121
+ });
122
+ }
123
+ return serializedTransaction;
124
+ })();
125
+ let txHash;
126
+ try {
127
+ const receipt = await sendRawTransactionSync(client, {
128
+ serializedTransaction: serializedTransaction_final,
129
+ });
130
+ if (receipt.status !== 'success') {
131
+ throw new VerificationFailedError({
132
+ reason: `open transaction reverted: ${receipt.transactionHash}`,
133
+ });
134
+ }
135
+ txHash = receipt.transactionHash;
136
+ }
137
+ catch (error) {
138
+ const onChain = await getOnChainChannel(client, escrowContract, channelId);
139
+ if (onChain.deposit > 0n) {
140
+ return { txHash: undefined, onChain };
141
+ }
142
+ throw error;
143
+ }
144
+ const onChain = await getOnChainChannel(client, escrowContract, channelId);
145
+ return { txHash, onChain };
146
+ }
147
+ export async function broadcastTopUpTransaction(parameters) {
148
+ const { client, serializedTransaction, escrowContract, channelId, declaredDeposit, previousDeposit, feePayer, } = parameters;
149
+ const transaction = Transaction.deserialize(serializedTransaction);
150
+ const calls = transaction.calls ?? [];
151
+ const topUpCall = calls.find((call) => {
152
+ if (!call.to || !isAddressEqual(call.to, escrowContract))
153
+ return false;
154
+ if (!call.data)
155
+ return false;
156
+ return call.data.slice(0, 10) === escrowTopUpSelector;
157
+ });
158
+ if (!topUpCall)
159
+ throw new BadRequestError({
160
+ reason: 'transaction does not contain a valid escrow topUp call',
161
+ });
162
+ if (feePayer) {
163
+ for (const call of calls) {
164
+ if (!call.to || !call.data) {
165
+ throw new BadRequestError({
166
+ reason: 'fee-sponsored transactions must not contain calls without target or data',
167
+ });
168
+ }
169
+ const selector = call.data.slice(0, 10);
170
+ const isEscrowTopUp = isAddressEqual(call.to, escrowContract) && selector === escrowTopUpSelector;
171
+ const isTokenApprove = selector === erc20ApproveSelector;
172
+ if (!isEscrowTopUp && !isTokenApprove) {
173
+ throw new BadRequestError({
174
+ reason: 'fee-sponsored topUp transaction contains an unauthorized call',
175
+ });
176
+ }
177
+ }
178
+ }
179
+ const { args: topUpArgs } = decodeFunctionData({ abi: escrowAbi, data: topUpCall.data });
180
+ const [txChannelId, txAmount] = topUpArgs;
181
+ if (txChannelId.toLowerCase() !== channelId.toLowerCase()) {
182
+ throw new VerificationFailedError({
183
+ reason: 'topUp transaction channelId does not match payload channelId',
184
+ });
185
+ }
186
+ if (BigInt(txAmount) !== declaredDeposit) {
187
+ throw new VerificationFailedError({
188
+ reason: `topUp transaction amount (${txAmount}) does not match declared additionalDeposit (${declaredDeposit})`,
189
+ });
190
+ }
191
+ const serializedTransaction_final = await (async () => {
192
+ if (feePayer) {
193
+ return signTransaction(client, {
194
+ ...transaction,
195
+ account: feePayer,
196
+ feePayer,
197
+ });
198
+ }
199
+ return serializedTransaction;
200
+ })();
201
+ const receipt = await sendRawTransactionSync(client, {
202
+ serializedTransaction: serializedTransaction_final,
203
+ });
204
+ if (receipt.status !== 'success') {
205
+ throw new VerificationFailedError({
206
+ reason: `topUp transaction reverted: ${receipt.transactionHash}`,
207
+ });
208
+ }
209
+ const onChain = await getOnChainChannel(client, escrowContract, channelId);
210
+ if (onChain.deposit <= previousDeposit) {
211
+ throw new VerificationFailedError({ reason: 'channel deposit did not increase after topUp' });
212
+ }
213
+ return { txHash: receipt.transactionHash, newDeposit: onChain.deposit };
214
+ }
215
+ //# sourceMappingURL=Chain.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Chain.js","sourceRoot":"","sources":["../../../src/tempo/stream/Chain.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,kBAAkB,EAClB,UAAU,EAEV,cAAc,EAEd,kBAAkB,GACnB,MAAM,MAAM,CAAA;AACb,OAAO,EAAE,YAAY,EAAE,sBAAsB,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AACnG,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AACxC,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAA;AAC9F,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAG3C,OAAO,EAAE,SAAS,EAAE,CAAA;AAEpB,MAAM,WAAW,GAAG,EAAE,IAAI,IAAI,GAAG,EAAE,CAAA;AAOnC;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,MAAc,EACd,cAAuB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E;QACpD,IAAI,QAAQ,EAAE,CAAC;YACb,OAAO,eAAe,CAAC,MAAM,EAAE;gBAC7B,GAAG,WAAW;gBACd,OAAO,EAAE,QAAQ;gBACjB,QAAQ;aACA,CAAC,CAAA;QACb,CAAC;QACD,OAAO,qBAAqB,CAAA;IAC9B,CAAC,CAAC,EAAE,CAAA;IAEJ,MAAM,OAAO,GAAG,MAAM,sBAAsB,CAAC,MAAM,EAAE;QACnD,qBAAqB,EAAE,2BAAqE;KAC7F,CAAC,CAAA;IAEF,IAAI,OAAO,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;QACjC,MAAM,IAAI,uBAAuB,CAAC;YAChC,MAAM,EAAE,+BAA+B,OAAO,CAAC,eAAe,EAAE;SACjE,CAAC,CAAA;IACJ,CAAC;IAED,MAAM,OAAO,GAAG,MAAM,iBAAiB,CAAC,MAAM,EAAE,cAAc,EAAE,SAAS,CAAC,CAAA;IAE1E,IAAI,OAAO,CAAC,OAAO,IAAI,eAAe,EAAE,CAAC;QACvC,MAAM,IAAI,uBAAuB,CAAC,EAAE,MAAM,EAAE,8CAA8C,EAAE,CAAC,CAAA;IAC/F,CAAC;IAED,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,eAAe,EAAE,UAAU,EAAE,OAAO,CAAC,OAAO,EAAE,CAAA;AACzE,CAAC"}
@@ -0,0 +1,26 @@
1
+ import type * as Hex from 'ox/Hex';
2
+ /**
3
+ * Computes a channel ID from its parameters.
4
+ *
5
+ * Mirrors the onchain `computeChannelId` function: `keccak256(abi.encode(payer, payee, token, salt, authorizedSigner, escrowContract, chainId))`.
6
+ */
7
+ export declare function computeId(parameters: computeId.Parameters): Hex.Hex;
8
+ export declare namespace computeId {
9
+ type Parameters = {
10
+ /** Address authorized to sign vouchers on behalf of the payer. */
11
+ authorizedSigner: Hex.Hex;
12
+ /** Chain ID of the network the escrow contract is deployed on. */
13
+ chainId: number;
14
+ /** Address of the escrow contract. */
15
+ escrowContract: Hex.Hex;
16
+ /** Address of the payee (recipient). */
17
+ payee: Hex.Hex;
18
+ /** Address of the payer (sender). */
19
+ payer: Hex.Hex;
20
+ /** Unique salt to differentiate channels with the same parameters. */
21
+ salt: Hex.Hex;
22
+ /** Address of the token used for payment. */
23
+ token: Hex.Hex;
24
+ };
25
+ }
26
+ //# sourceMappingURL=Channel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Channel.d.ts","sourceRoot":"","sources":["../../../src/tempo/stream/Channel.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,GAAG,MAAM,QAAQ,CAAA;AAElC;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,UAAU,EAAE,SAAS,CAAC,UAAU,GAAG,GAAG,CAAC,GAAG,CAsBnE;AAED,MAAM,CAAC,OAAO,WAAW,SAAS,CAAC;IACjC,KAAK,UAAU,GAAG;QAChB,kEAAkE;QAClE,gBAAgB,EAAE,GAAG,CAAC,GAAG,CAAA;QACzB,kEAAkE;QAClE,OAAO,EAAE,MAAM,CAAA;QACf,sCAAsC;QACtC,cAAc,EAAE,GAAG,CAAC,GAAG,CAAA;QACvB,wCAAwC;QACxC,KAAK,EAAE,GAAG,CAAC,GAAG,CAAA;QACd,qCAAqC;QACrC,KAAK,EAAE,GAAG,CAAC,GAAG,CAAA;QACd,sEAAsE;QACtE,IAAI,EAAE,GAAG,CAAC,GAAG,CAAA;QACb,6CAA6C;QAC7C,KAAK,EAAE,GAAG,CAAC,GAAG,CAAA;KACf,CAAA;CACF"}
@@ -0,0 +1,27 @@
1
+ import { AbiParameters, Hash } from 'ox';
2
+ /**
3
+ * Computes a channel ID from its parameters.
4
+ *
5
+ * Mirrors the onchain `computeChannelId` function: `keccak256(abi.encode(payer, payee, token, salt, authorizedSigner, escrowContract, chainId))`.
6
+ */
7
+ export function computeId(parameters) {
8
+ const encoded = AbiParameters.encode(AbiParameters.from([
9
+ 'address payer',
10
+ 'address payee',
11
+ 'address token',
12
+ 'bytes32 salt',
13
+ 'address authorizedSigner',
14
+ 'address escrowContract',
15
+ 'uint256 chainId',
16
+ ]), [
17
+ parameters.payer,
18
+ parameters.payee,
19
+ parameters.token,
20
+ parameters.salt,
21
+ parameters.authorizedSigner,
22
+ parameters.escrowContract,
23
+ BigInt(parameters.chainId),
24
+ ]);
25
+ return Hash.keccak256(encoded);
26
+ }
27
+ //# sourceMappingURL=Channel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Channel.js","sourceRoot":"","sources":["../../../src/tempo/stream/Channel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,IAAI,EAAE,MAAM,IAAI,CAAA;AAGxC;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAAC,UAAgC;IACxD,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAClC,aAAa,CAAC,IAAI,CAAC;QACjB,eAAe;QACf,eAAe;QACf,eAAe;QACf,cAAc;QACd,0BAA0B;QAC1B,wBAAwB;QACxB,iBAAiB;KAClB,CAAC,EACF;QACE,UAAU,CAAC,KAAK;QAChB,UAAU,CAAC,KAAK;QAChB,UAAU,CAAC,KAAK;QAChB,UAAU,CAAC,IAAI;QACf,UAAU,CAAC,gBAAgB;QAC3B,UAAU,CAAC,cAAc;QACzB,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC;KAC3B,CACF,CAAA;IACD,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA;AAChC,CAAC"}
@@ -0,0 +1,103 @@
1
+ import type { Address, Hex } from 'viem';
2
+ import type * as Store from '../../Store.js';
3
+ import type { SignedVoucher } from './Types.js';
4
+ /**
5
+ * State for an on-chain payment channel, including per-session accounting.
6
+ *
7
+ * Tracks the channel's identity, on-chain balance, the highest voucher
8
+ * the server has accepted, and the current session's spend counters.
9
+ * A channel is created when a payer opens an escrow on-chain and persists
10
+ * until the channel is finalized (closed/settled).
11
+ *
12
+ * One channel = one session. The client owns the key and can't race with
13
+ * itself, so concurrent session support is unnecessary.
14
+ *
15
+ * Monotonicity invariants (enforced by update callbacks):
16
+ * - `highestVoucherAmount` only increases
17
+ * - `settledOnChain` only increases
18
+ * - `deposit` reflects the latest on-chain value
19
+ */
20
+ export interface State {
21
+ /** Address authorized to sign vouchers on behalf of the payer. */
22
+ authorizedSigner: Address;
23
+ /** Chain ID the channel was opened on. */
24
+ chainId: number;
25
+ /** Escrow contract address the channel was opened on. */
26
+ escrowContract: Address;
27
+ /** Unique identifier for this payment channel. */
28
+ channelId: Hex;
29
+ /** ISO 8601 timestamp when the channel was created. */
30
+ createdAt: string;
31
+ /** Current on-chain deposit in the escrow contract. */
32
+ deposit: bigint;
33
+ /** Whether the channel has been finalized (closed) on-chain. */
34
+ finalized: boolean;
35
+ /** The signed voucher corresponding to `highestVoucherAmount`. */
36
+ highestVoucher: SignedVoucher | null;
37
+ /** Highest cumulative voucher amount accepted by the server. */
38
+ highestVoucherAmount: bigint;
39
+ /** Address of the payment recipient. */
40
+ payee: Address;
41
+ /** Address of the payment sender. */
42
+ payer: Address;
43
+ /** Cumulative amount settled on-chain so far. */
44
+ settledOnChain: bigint;
45
+ /** Cumulative amount spent (charged) against this channel's current session. */
46
+ spent: bigint;
47
+ /** Token contract address used for payments. */
48
+ token: Address;
49
+ /** Number of charge operations (API requests) fulfilled in the current session. */
50
+ units: number;
51
+ }
52
+ /**
53
+ * Internal store interface for channel state persistence.
54
+ *
55
+ * ## Atomicity contract
56
+ *
57
+ * The `updateChannel` method uses an atomic read-modify-write callback.
58
+ * The callback receives the current state (or `null` if none exists), and
59
+ * returns the next state (or `null` to delete). Implementations must
60
+ * guarantee that no concurrent mutation occurs between reading `current`
61
+ * and writing the return value.
62
+ *
63
+ * Backends implement this via their native mechanisms:
64
+ * - **In-memory / JS single-thread**: Synchronous callback execution
65
+ * - **Durable Objects**: Single-threaded execution model
66
+ * - **D1 / SQL**: Database transactions
67
+ */
68
+ export type ChannelStore = {
69
+ getChannel(channelId: Hex): Promise<State | null>;
70
+ /**
71
+ * Atomic read-modify-write for channel state.
72
+ * Return `null` from `fn` to delete the channel.
73
+ */
74
+ updateChannel(channelId: Hex, fn: (current: State | null) => State | null): Promise<State | null>;
75
+ /**
76
+ * Wait for the next update to a channel.
77
+ *
78
+ * Returns a `Promise` that resolves once `updateChannel` is called for
79
+ * `channelId`. Implementations should resolve immediately if the channel
80
+ * was updated between the call to `waitForUpdate` and the `Promise`
81
+ * being awaited.
82
+ *
83
+ * When not implemented, callers fall back to polling.
84
+ */
85
+ waitForUpdate?(channelId: Hex): Promise<void>;
86
+ };
87
+ export type DeductResult = {
88
+ ok: true;
89
+ channel: State;
90
+ } | {
91
+ ok: false;
92
+ channel: State;
93
+ };
94
+ /**
95
+ * Atomically deduct `amount` from a channel's available balance.
96
+ *
97
+ * Returns `{ ok: true, channel }` if the deduction succeeded, or
98
+ * `{ ok: false, channel }` with the unchanged state if balance is
99
+ * insufficient. Throws if the channel does not exist.
100
+ */
101
+ export declare function deductFromChannel(store: ChannelStore, channelId: Hex, amount: bigint): Promise<DeductResult>;
102
+ export declare function fromStore(store: Store.Store): ChannelStore;
103
+ //# sourceMappingURL=ChannelStore.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ChannelStore.d.ts","sourceRoot":"","sources":["../../../src/tempo/stream/ChannelStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,MAAM,CAAA;AACxC,OAAO,KAAK,KAAK,KAAK,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAE/C;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,KAAK;IACpB,kEAAkE;IAClE,gBAAgB,EAAE,OAAO,CAAA;IACzB,0CAA0C;IAC1C,OAAO,EAAE,MAAM,CAAA;IACf,yDAAyD;IACzD,cAAc,EAAE,OAAO,CAAA;IACvB,kDAAkD;IAClD,SAAS,EAAE,GAAG,CAAA;IACd,uDAAuD;IACvD,SAA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