tenjin-cli 0.1.0-alpha.33 → 0.1.0-alpha.35
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/NOTICE.md +110 -0
- package/README.md +87 -81
- package/dist/{_esm-BIN7VY2L.js → _esm-2KST7HQ7.js} +7 -7
- package/dist/alive-5TV22PNF.js +51 -0
- package/dist/auth-XI7BJFLW.js +47 -0
- package/dist/{auto-update-Y24UL56V.js → auto-update-7L24KDWA.js} +4 -5
- package/dist/balance-DFHC6XEU.js +71 -0
- package/dist/{ccip-DHDFKTQV.js → ccip-HOW3DZYF.js} +4 -4
- package/dist/chunk-24XKUN2S.js +202 -0
- package/dist/chunk-24XKUN2S.js.map +1 -0
- package/dist/chunk-2L7TDGLV.js +56 -0
- package/dist/chunk-2L7TDGLV.js.map +1 -0
- package/dist/{chunk-C2Q76BVM.js → chunk-2U7H35IP.js} +29 -7
- package/dist/chunk-2U7H35IP.js.map +1 -0
- package/dist/chunk-2W4DQQUU.js +176 -0
- package/dist/chunk-2W4DQQUU.js.map +1 -0
- package/dist/{chunk-LRW6S53P.js → chunk-34SAECKZ.js} +35 -11
- package/dist/chunk-34SAECKZ.js.map +1 -0
- package/dist/{chunk-2I4QTOLB.js → chunk-3MZ7DYRO.js} +1 -1
- package/dist/chunk-3MZ7DYRO.js.map +1 -0
- package/dist/{chunk-RVPYTUP4.js → chunk-3Y2EHT4F.js} +13 -9
- package/dist/chunk-3Y2EHT4F.js.map +1 -0
- package/dist/chunk-3Y6BHIN6.js +169 -0
- package/dist/chunk-3Y6BHIN6.js.map +1 -0
- package/dist/{chunk-4K7TQHNP.js → chunk-3YPL6T3P.js} +17 -33
- package/dist/chunk-3YPL6T3P.js.map +1 -0
- package/dist/{chunk-E7SW44LM.js → chunk-44MVIRZH.js} +121 -6
- package/dist/chunk-44MVIRZH.js.map +1 -0
- package/dist/chunk-45MMSBHA.js +321 -0
- package/dist/chunk-45MMSBHA.js.map +1 -0
- package/dist/{chunk-3FMZGI7I.js → chunk-4CE6TMI5.js} +1 -1
- package/dist/chunk-4CE6TMI5.js.map +1 -0
- package/dist/{chunk-QXP3Z3JN.js → chunk-4MBCJFY4.js} +35 -3
- package/dist/chunk-4MBCJFY4.js.map +1 -0
- package/dist/chunk-4TPL54KW.js +227 -0
- package/dist/chunk-4TPL54KW.js.map +1 -0
- package/dist/chunk-54ELBTKE.js +1615 -0
- package/dist/chunk-54ELBTKE.js.map +1 -0
- package/dist/{chunk-NDGZDTSJ.js → chunk-5F4OB5B3.js} +115 -57
- package/dist/chunk-5F4OB5B3.js.map +1 -0
- package/dist/chunk-5HN4KVQU.js +3348 -0
- package/dist/chunk-5HN4KVQU.js.map +1 -0
- package/dist/{chunk-ZDZERB6L.js → chunk-5J4E5GYI.js} +572 -407
- package/dist/chunk-5J4E5GYI.js.map +1 -0
- package/dist/{chunk-ZNNXRNFD.js → chunk-5N4GGUBN.js} +48 -6
- package/dist/chunk-5N4GGUBN.js.map +1 -0
- package/dist/chunk-5USBZ2MU.js +58 -0
- package/dist/chunk-5USBZ2MU.js.map +1 -0
- package/dist/chunk-64WOMNYR.js +183 -0
- package/dist/chunk-64WOMNYR.js.map +1 -0
- package/dist/{chunk-ILNIZ5I2.js → chunk-65RS4LQS.js} +1825 -1715
- package/dist/chunk-65RS4LQS.js.map +1 -0
- package/dist/{chunk-IE7P2BZE.js → chunk-6QMDUDF2.js} +3 -3
- package/dist/{chunk-IADZOXKN.js → chunk-7SLOTLO7.js} +19 -697
- package/dist/chunk-7SLOTLO7.js.map +1 -0
- package/dist/{chunk-WFSPCCVW.js → chunk-7V25UAQO.js} +99 -99
- package/dist/{chunk-WFSPCCVW.js.map → chunk-7V25UAQO.js.map} +1 -1
- package/dist/chunk-BGA4DO5M.js +719 -0
- package/dist/chunk-BGA4DO5M.js.map +1 -0
- package/dist/chunk-BVP7YK2J.js +706 -0
- package/dist/chunk-BVP7YK2J.js.map +1 -0
- package/dist/chunk-C265QIMJ.js +83 -0
- package/dist/chunk-C265QIMJ.js.map +1 -0
- package/dist/{chunk-XQJXNMSB.js → chunk-CRG54LEB.js} +1 -1
- package/dist/chunk-CRG54LEB.js.map +1 -0
- package/dist/{chunk-KG3D7FIF.js → chunk-CZJOMUBP.js} +2 -2
- package/dist/{chunk-36RVGZNY.js → chunk-D6M3ZK66.js} +22 -39
- package/dist/chunk-D6M3ZK66.js.map +1 -0
- package/dist/{chunk-3UCIG4TG.js → chunk-DI4QAGRZ.js} +1 -11
- package/dist/chunk-DI4QAGRZ.js.map +1 -0
- package/dist/{chunk-JKKAIPTU.js → chunk-DIH2G3YG.js} +47 -10
- package/dist/chunk-DIH2G3YG.js.map +1 -0
- package/dist/chunk-DJ36BM62.js +78 -0
- package/dist/chunk-DJ36BM62.js.map +1 -0
- package/dist/chunk-DTEWAVNE.js +114 -0
- package/dist/chunk-DTEWAVNE.js.map +1 -0
- package/dist/chunk-F2KJAJ7T.js +13 -0
- package/dist/chunk-F2KJAJ7T.js.map +1 -0
- package/dist/{chunk-BF4XHEQK.js → chunk-F3QPGYOY.js} +6 -6
- package/dist/chunk-F3QPGYOY.js.map +1 -0
- package/dist/{chunk-3GNJXKIT.js → chunk-F43CV6UC.js} +2 -2
- package/dist/{chunk-J57UC3DD.js → chunk-FKVJKLPI.js} +3 -3
- package/dist/{chunk-V2LQLN5H.js → chunk-FYFHH7V5.js} +59 -8
- package/dist/{chunk-V2LQLN5H.js.map → chunk-FYFHH7V5.js.map} +1 -1
- package/dist/chunk-GAJC4BQZ.js +1096 -0
- package/dist/chunk-GAJC4BQZ.js.map +1 -0
- package/dist/{chunk-DOQGNFHM.js → chunk-GH5UKMZD.js} +2 -12
- package/dist/{chunk-DOQGNFHM.js.map → chunk-GH5UKMZD.js.map} +1 -1
- package/dist/{chunk-WYXUQGOL.js → chunk-HAZAN4UM.js} +7 -6
- package/dist/chunk-HAZAN4UM.js.map +1 -0
- package/dist/chunk-HI5XQOX2.js +393 -0
- package/dist/chunk-HI5XQOX2.js.map +1 -0
- package/dist/chunk-HKGUKAQW.js +488 -0
- package/dist/chunk-HKGUKAQW.js.map +1 -0
- package/dist/{chunk-SO3XRZPX.js → chunk-HPRVFJPM.js} +6 -6
- package/dist/{chunk-L6JEXB3E.js → chunk-HQR4BWKX.js} +3 -131
- package/dist/chunk-HQR4BWKX.js.map +1 -0
- package/dist/chunk-HZKANST2.js +177 -0
- package/dist/chunk-HZKANST2.js.map +1 -0
- package/dist/{chunk-UUGOUOVT.js → chunk-I557SAOM.js} +8 -2
- package/dist/chunk-I557SAOM.js.map +1 -0
- package/dist/{chunk-TC2WZLSC.js → chunk-IGLWLH73.js} +8 -7
- package/dist/chunk-IGLWLH73.js.map +1 -0
- package/dist/chunk-ILWCKCOI.js +58 -0
- package/dist/chunk-ILWCKCOI.js.map +1 -0
- package/dist/{chunk-2GP3HRYK.js → chunk-INSIW65E.js} +182 -182
- package/dist/{chunk-MPZUYCAP.js → chunk-IULCDMVG.js} +12 -1
- package/dist/chunk-IULCDMVG.js.map +1 -0
- package/dist/{chunk-INTFHMU4.js → chunk-IVNSUAVC.js} +25 -26
- package/dist/chunk-IVNSUAVC.js.map +1 -0
- package/dist/chunk-JDQZCREI.js +87 -0
- package/dist/chunk-JDQZCREI.js.map +1 -0
- package/dist/chunk-JHFFUVON.js +1050 -0
- package/dist/chunk-JHFFUVON.js.map +1 -0
- package/dist/{chunk-RL5UUDT4.js → chunk-JNUKLSMZ.js} +3 -3
- package/dist/chunk-JTILUN4W.js +2933 -0
- package/dist/chunk-JTILUN4W.js.map +1 -0
- package/dist/{chunk-OJNTCYQQ.js → chunk-JTX3BUDK.js} +37 -53
- package/dist/chunk-JTX3BUDK.js.map +1 -0
- package/dist/{chunk-X5ZBP3IY.js → chunk-JUUBBBY5.js} +2 -2
- package/dist/chunk-JUUBBBY5.js.map +1 -0
- package/dist/chunk-JWAK7JQB.js +15 -0
- package/dist/chunk-JWAK7JQB.js.map +1 -0
- package/dist/{chunk-UXZONNEQ.js → chunk-K5KNGC4I.js} +34 -31
- package/dist/chunk-K5KNGC4I.js.map +1 -0
- package/dist/chunk-KAQ4Q3TO.js +135 -0
- package/dist/chunk-KAQ4Q3TO.js.map +1 -0
- package/dist/chunk-KFVJYLVL.js +125 -0
- package/dist/chunk-KFVJYLVL.js.map +1 -0
- package/dist/chunk-KGUE5AVX.js +69 -0
- package/dist/chunk-KGUE5AVX.js.map +1 -0
- package/dist/{chunk-FVIGCDSV.js → chunk-KLDMLWXT.js} +11 -85
- package/dist/chunk-KLDMLWXT.js.map +1 -0
- package/dist/chunk-KNKDMTHU.js +453 -0
- package/dist/chunk-KNKDMTHU.js.map +1 -0
- package/dist/chunk-LDA6TAYQ.js +95 -0
- package/dist/chunk-LDA6TAYQ.js.map +1 -0
- package/dist/chunk-LWSSIZOY.js +347 -0
- package/dist/chunk-LWSSIZOY.js.map +1 -0
- package/dist/{chunk-ZURZHELF.js → chunk-MAHNZA6C.js} +158 -41
- package/dist/chunk-MAHNZA6C.js.map +1 -0
- package/dist/chunk-MEWZCIA6.js +220 -0
- package/dist/chunk-MEWZCIA6.js.map +1 -0
- package/dist/{chunk-2SNUOHAT.js → chunk-MEZJ3I63.js} +49 -1
- package/dist/chunk-MEZJ3I63.js.map +1 -0
- package/dist/chunk-MJDO4EXD.js +353 -0
- package/dist/chunk-MJDO4EXD.js.map +1 -0
- package/dist/chunk-NJA6NT3A.js +6595 -0
- package/dist/chunk-NJA6NT3A.js.map +1 -0
- package/dist/{chunk-LHHMV4GU.js → chunk-NVSKJPRC.js} +1 -1
- package/dist/{chunk-LHHMV4GU.js.map → chunk-NVSKJPRC.js.map} +1 -1
- package/dist/{chunk-4YOZGGTZ.js → chunk-OC7K2DVZ.js} +5 -5
- package/dist/chunk-OC7K2DVZ.js.map +1 -0
- package/dist/chunk-ODRPASFP.js +2078 -0
- package/dist/chunk-ODRPASFP.js.map +1 -0
- package/dist/chunk-OJJFANO6.js +133 -0
- package/dist/chunk-OJJFANO6.js.map +1 -0
- package/dist/{chunk-JNJWBTU2.js → chunk-OQWPRFAC.js} +332 -882
- package/dist/chunk-OQWPRFAC.js.map +1 -0
- package/dist/chunk-P4I7BFNJ.js +11 -0
- package/dist/chunk-P4I7BFNJ.js.map +1 -0
- package/dist/{chunk-SNNSAGMT.js → chunk-P6YQWIT2.js} +4058 -4039
- package/dist/chunk-P6YQWIT2.js.map +1 -0
- package/dist/{chunk-JRGFKVAZ.js → chunk-PETQ5WEA.js} +2 -2
- package/dist/{chunk-HMWYAUA6.js → chunk-Q6RSJCXB.js} +29 -69
- package/dist/chunk-Q6RSJCXB.js.map +1 -0
- package/dist/{chunk-U3IKUGRP.js → chunk-QKEUKBRK.js} +17 -23
- package/dist/chunk-QKEUKBRK.js.map +1 -0
- package/dist/{chunk-2RWB7RKE.js → chunk-QKYUCITR.js} +174 -5814
- package/dist/chunk-QKYUCITR.js.map +1 -0
- package/dist/chunk-QQDPRTLP.js +127 -0
- package/dist/chunk-QQDPRTLP.js.map +1 -0
- package/dist/chunk-QQYCYAKI.js +79 -0
- package/dist/chunk-QQYCYAKI.js.map +1 -0
- package/dist/chunk-R4KDH477.js +1064 -0
- package/dist/chunk-R4KDH477.js.map +1 -0
- package/dist/chunk-R5TR2DIK.js +119 -0
- package/dist/chunk-R5TR2DIK.js.map +1 -0
- package/dist/chunk-R62PKTAG.js +66 -0
- package/dist/chunk-R62PKTAG.js.map +1 -0
- package/dist/{chunk-BFDQ4WNW.js → chunk-RFZA7PSM.js} +4 -5
- package/dist/chunk-RFZA7PSM.js.map +1 -0
- package/dist/chunk-RGCPELQN.js +219 -0
- package/dist/chunk-RGCPELQN.js.map +1 -0
- package/dist/chunk-RGVCOG2S.js +910 -0
- package/dist/chunk-RGVCOG2S.js.map +1 -0
- package/dist/chunk-SOLNJ3AZ.js +209 -0
- package/dist/chunk-SOLNJ3AZ.js.map +1 -0
- package/dist/chunk-TFT2X7SH.js +77 -0
- package/dist/chunk-TFT2X7SH.js.map +1 -0
- package/dist/chunk-TUCKEEXE.js +2 -0
- package/dist/chunk-TVWCLUBI.js +293 -0
- package/dist/chunk-TVWCLUBI.js.map +1 -0
- package/dist/chunk-U4TRWSVE.js +78 -0
- package/dist/chunk-U4TRWSVE.js.map +1 -0
- package/dist/chunk-UIB33QQP.js +379 -0
- package/dist/chunk-UIB33QQP.js.map +1 -0
- package/dist/chunk-V6GIVALQ.js +1889 -0
- package/dist/chunk-V6GIVALQ.js.map +1 -0
- package/dist/chunk-VHXCPXUS.js +366 -0
- package/dist/chunk-VHXCPXUS.js.map +1 -0
- package/dist/{chunk-HGZEPQUT.js → chunk-VU6GMWF6.js} +17 -8
- package/dist/chunk-VU6GMWF6.js.map +1 -0
- package/dist/chunk-VWZYE3NC.js +8415 -0
- package/dist/chunk-VWZYE3NC.js.map +1 -0
- package/dist/{chunk-AGMVASXZ.js → chunk-W2GXIARJ.js} +88 -11
- package/dist/chunk-W2GXIARJ.js.map +1 -0
- package/dist/chunk-W4G3K3V7.js +288 -0
- package/dist/chunk-W4G3K3V7.js.map +1 -0
- package/dist/chunk-W74LJ5OT.js +588 -0
- package/dist/chunk-W74LJ5OT.js.map +1 -0
- package/dist/chunk-W7ELYLYK.js +233 -0
- package/dist/chunk-W7ELYLYK.js.map +1 -0
- package/dist/{chunk-BPMWXTFX.js → chunk-WFZX352D.js} +7 -7
- package/dist/chunk-WXA3WTPF.js +20 -0
- package/dist/chunk-WXA3WTPF.js.map +1 -0
- package/dist/chunk-XRGFH7FI.js +454 -0
- package/dist/chunk-XRGFH7FI.js.map +1 -0
- package/dist/chunk-XSCGUO2U.js +330 -0
- package/dist/chunk-XSCGUO2U.js.map +1 -0
- package/dist/chunk-XWROSMI5.js +1039 -0
- package/dist/chunk-XWROSMI5.js.map +1 -0
- package/dist/chunk-Y2EXZ47H.js +40 -0
- package/dist/chunk-Y2EXZ47H.js.map +1 -0
- package/dist/chunk-YJGEVBKZ.js +47 -0
- package/dist/chunk-YJGEVBKZ.js.map +1 -0
- package/dist/{chunk-6CMM6YCB.js → chunk-YWWMISDY.js} +2 -2
- package/dist/{chunk-WXA2BWTE.js → chunk-ZCT5DJ3G.js} +2 -48
- package/dist/chunk-ZCT5DJ3G.js.map +1 -0
- package/dist/chunk-ZEYA2WWX.js +982 -0
- package/dist/chunk-ZEYA2WWX.js.map +1 -0
- package/dist/chunk-ZRC7Z5X3.js +82 -0
- package/dist/chunk-ZRC7Z5X3.js.map +1 -0
- package/dist/{chunk-3AIHRXKO.js → chunk-ZS7JIUJ3.js} +12 -10
- package/dist/chunk-ZS7JIUJ3.js.map +1 -0
- package/dist/{clack-EZ7TDWQU.js → clack-Q4RNC5VE.js} +2 -6
- package/dist/{claude-cli-2YKABJJZ.js → claude-cli-GIIHOBGT.js} +4 -2
- package/dist/claude-install-BAUSJMW3.js +64 -0
- package/dist/{cli-E36SPKBW.js → cli-QY4KKA3D.js} +632 -150
- package/dist/cli-QY4KKA3D.js.map +1 -0
- package/dist/client-3YQKORVK.js +710 -0
- package/dist/client-3YQKORVK.js.map +1 -0
- package/dist/{codex-event-ZO2GEFE6.js → codex-event-EUTCAOTQ.js} +3 -5
- package/dist/{codex-host-KECJVTA2.js → codex-host-ZON3S6HU.js} +7 -7
- package/dist/{codex-install-HQZDGFXW.js → codex-install-HZV2DOAB.js} +242 -88
- package/dist/codex-install-HZV2DOAB.js.map +1 -0
- package/dist/config-QZOBMTEL.js +57 -0
- package/dist/config-names-Z5NZD5R2.js +224 -0
- package/dist/config-names-Z5NZD5R2.js.map +1 -0
- package/dist/config-rules-YOYHS6YL.js +55 -0
- package/dist/config-subagents-WQV2ZAD5.js +362 -0
- package/dist/config-subagents-WQV2ZAD5.js.map +1 -0
- package/dist/connect-YDWTYU4B.js +1020 -0
- package/dist/connect-YDWTYU4B.js.map +1 -0
- package/dist/core-0a66n3e7-TTP554XV.js +127 -0
- package/dist/core-0a66n3e7-TTP554XV.js.map +1 -0
- package/dist/core-0bmnpbz2-C2JIRVZO.js +10790 -0
- package/dist/core-0bmnpbz2-C2JIRVZO.js.map +1 -0
- package/dist/core-9je8ee1j-74DGHWPV.js +30 -0
- package/dist/core-9je8ee1j-74DGHWPV.js.map +1 -0
- package/dist/core-wadgcjfh-TSZFQZGH.js +666 -0
- package/dist/core-wadgcjfh-TSZFQZGH.js.map +1 -0
- package/dist/core-zg882d22-LKV5DZYW.js +37 -0
- package/dist/core-zg882d22-LKV5DZYW.js.map +1 -0
- package/dist/decision-QSKHOGCO.js +145 -0
- package/dist/{doctor-I6XQEKWD.js → doctor-ZWJXRD75.js} +162 -148
- package/dist/doctor-ZWJXRD75.js.map +1 -0
- package/dist/{env-proxy-R6ZMLQTT.js → env-proxy-XLSLHBKQ.js} +2 -2
- package/dist/{ethersCompat-Df4a6QIZ-X6YZWAML.js → ethersCompat-Df4a6QIZ-K3DMHR6Y.js} +5 -5
- package/dist/explicit-ME2NRADQ.js +371 -0
- package/dist/explicit-ME2NRADQ.js.map +1 -0
- package/dist/flush-stdio-NYIJ3QXT.js +18 -0
- package/dist/fund-VF7TLDJH.js +722 -0
- package/dist/fund-VF7TLDJH.js.map +1 -0
- package/dist/hook-command-VZ27MGWO.js +49 -0
- package/dist/hosts/claude/.claude-plugin/marketplace.json +2 -2
- package/dist/hosts/claude/tenjin/.claude-plugin/plugin.json +2 -2
- package/dist/hosts/claude/tenjin/hooks/hook-data.ts +152 -0
- package/dist/hosts/claude/tenjin/hooks/hooks.json +7 -0
- package/dist/hosts/claude/tenjin/hooks/model-viewer.tsx +2987 -0
- package/dist/hosts/claude/tenjin/hooks/register.tsx +527 -56
- package/dist/hosts/claude/tenjin/skills/tenjin-pay/SKILL.md +14 -4
- package/dist/hosts/claude/tenjin/types/index.d.ts +144 -1
- package/dist/{hosts-DVQJ32QC.js → hosts-3EMTVM4H.js} +7 -6
- package/dist/index.js +18 -15
- package/dist/index.js.map +1 -1
- package/dist/install-ZA7L2UF3.js +115 -0
- package/dist/install-funding-JDGYTF72.js +30 -0
- package/dist/install-identity-FFAYEFLX.js +30 -0
- package/dist/{install-wallet-5M4OTYLX.js → install-wallet-2SFOVE3Q.js} +68 -53
- package/dist/install-wallet-2SFOVE3Q.js.map +1 -0
- package/dist/mcp-36S6MK3C.js +67642 -0
- package/dist/mcp-36S6MK3C.js.map +1 -0
- package/dist/new-install-5L4GY3LN.js +51 -0
- package/dist/{package-IJFGMWEV.js → package-BONDXSV3.js} +2 -2
- package/dist/{passphrase-IFTDS3CU.js → passphrase-T4ULBN4M.js} +16 -12
- package/dist/{paths-XCSAKISU.js → paths-O2KDWZ76.js} +18 -28
- package/dist/pay-CMV5KL74.js +74 -0
- package/dist/{payments-HFPPWHTV.js → payments-Z6UBZL4Z.js} +36 -26
- package/dist/payments-Z6UBZL4Z.js.map +1 -0
- package/dist/reclaim-subagent-credit-ABSF67SJ.js +105 -0
- package/dist/reclaim-subagent-credit-ABSF67SJ.js.map +1 -0
- package/dist/{redact-D7ASVWKL.js → redact-LOTHE5TS.js} +2 -8
- package/dist/redact-LOTHE5TS.js.map +1 -0
- package/dist/routing-quote-AHLAREXR.js +40 -0
- package/dist/routing-quote-AHLAREXR.js.map +1 -0
- package/dist/run-JH7ELYAM.js +88 -0
- package/dist/run-JH7ELYAM.js.map +1 -0
- package/dist/runtime-2GX4ALJP.js +410 -0
- package/dist/runtime-2GX4ALJP.js.map +1 -0
- package/dist/runtime-activate-IDQ2NCSP.js +39 -0
- package/dist/runtime-activate-IDQ2NCSP.js.map +1 -0
- package/dist/{runtime-fetch-ZSSSK72Y.js → runtime-fetch-6LOBHBEN.js} +4 -4
- package/dist/runtime-fetch-6LOBHBEN.js.map +1 -0
- package/dist/runtime-launchers-YAPKCBHV.js +31 -0
- package/dist/runtime-launchers-YAPKCBHV.js.map +1 -0
- package/dist/{runtime-layout-WIDILZ6K.js → runtime-layout-QHZQJV7C.js} +5 -4
- package/dist/runtime-layout-QHZQJV7C.js.map +1 -0
- package/dist/runtime-place-4POL4KK4.js +37 -0
- package/dist/runtime-place-4POL4KK4.js.map +1 -0
- package/dist/runtime-provenance-MNJGVBV2.js +12 -0
- package/dist/runtime-provenance-MNJGVBV2.js.map +1 -0
- package/dist/{secp256k1-ZZZT2CAX.js → secp256k1-N5P2GVGF.js} +3 -3
- package/dist/secp256k1-N5P2GVGF.js.map +1 -0
- package/dist/{send-GCVEAQKL.js → send-ELXFKEKY.js} +35 -32
- package/dist/send-ELXFKEKY.js.map +1 -0
- package/dist/status-FXUFWJDM.js +404 -0
- package/dist/status-FXUFWJDM.js.map +1 -0
- package/dist/{status-line-OPCXDHL2.js → status-line-IH5JBWBC.js} +51 -43
- package/dist/status-line-IH5JBWBC.js.map +1 -0
- package/dist/streamableHttp-CP2ZQBIK.js +768 -0
- package/dist/streamableHttp-CP2ZQBIK.js.map +1 -0
- package/dist/{uninstall-BAWRBXXQ.js → uninstall-LJZUKM4D.js} +76 -76
- package/dist/uninstall-LJZUKM4D.js.map +1 -0
- package/dist/update-EUIX72IX.js +68 -0
- package/dist/update-EUIX72IX.js.map +1 -0
- package/dist/usage-EKDC7LUC.js +180 -0
- package/dist/usage-EKDC7LUC.js.map +1 -0
- package/dist/{usdc-VG4CIOUK.js → usdc-YIHK2OMC.js} +8 -12
- package/dist/usdc-YIHK2OMC.js.map +1 -0
- package/dist/{viemAdapter-By13KInL-DOFTEIJV.js → viemAdapter-By13KInL-PRLSIESK.js} +5 -5
- package/dist/viemAdapter-By13KInL-PRLSIESK.js.map +1 -0
- package/dist/wallet-HSW757W6.js +80 -0
- package/dist/wallet-HSW757W6.js.map +1 -0
- package/dist/wallet-kdf-worker.mjs +1433 -818
- package/docs/agent-permissions.md +88 -155
- package/docs/router-reference.md +236 -0
- package/docs/safety-model.md +31 -33
- package/docs/subagents-internals.md +97 -0
- package/docs/subagents.md +348 -0
- package/docs/troubleshooting.md +10 -6
- package/package.json +12 -8
- package/scripts/installers/install.sh +7 -1
- package/dist/alive-GSOWKUOY.js +0 -38
- package/dist/chunk-2I4QTOLB.js.map +0 -1
- package/dist/chunk-2M2VPI2E.js +0 -560
- package/dist/chunk-2M2VPI2E.js.map +0 -1
- package/dist/chunk-2RWB7RKE.js.map +0 -1
- package/dist/chunk-2SNUOHAT.js.map +0 -1
- package/dist/chunk-3623BLPV.js +0 -184
- package/dist/chunk-3623BLPV.js.map +0 -1
- package/dist/chunk-36RVGZNY.js.map +0 -1
- package/dist/chunk-3AIHRXKO.js.map +0 -1
- package/dist/chunk-3FMZGI7I.js.map +0 -1
- package/dist/chunk-3UCIG4TG.js.map +0 -1
- package/dist/chunk-4K7TQHNP.js.map +0 -1
- package/dist/chunk-4LMUQMBH.js +0 -29
- package/dist/chunk-4LMUQMBH.js.map +0 -1
- package/dist/chunk-4USCZ2NI.js +0 -228
- package/dist/chunk-4USCZ2NI.js.map +0 -1
- package/dist/chunk-4YOZGGTZ.js.map +0 -1
- package/dist/chunk-62KNUYWD.js +0 -111
- package/dist/chunk-62KNUYWD.js.map +0 -1
- package/dist/chunk-A5GKIG5O.js +0 -2037
- package/dist/chunk-A5GKIG5O.js.map +0 -1
- package/dist/chunk-AGMVASXZ.js.map +0 -1
- package/dist/chunk-BF4XHEQK.js.map +0 -1
- package/dist/chunk-BFDQ4WNW.js.map +0 -1
- package/dist/chunk-C2Q76BVM.js.map +0 -1
- package/dist/chunk-CEXSOI5R.js +0 -808
- package/dist/chunk-CEXSOI5R.js.map +0 -1
- package/dist/chunk-E7SW44LM.js.map +0 -1
- package/dist/chunk-EDJWHDDL.js +0 -33
- package/dist/chunk-EDJWHDDL.js.map +0 -1
- package/dist/chunk-FVIGCDSV.js.map +0 -1
- package/dist/chunk-GTTUCOPD.js +0 -105
- package/dist/chunk-GTTUCOPD.js.map +0 -1
- package/dist/chunk-HDPUXS3Z.js +0 -122
- package/dist/chunk-HDPUXS3Z.js.map +0 -1
- package/dist/chunk-HGGWCP7W.js +0 -120
- package/dist/chunk-HGGWCP7W.js.map +0 -1
- package/dist/chunk-HGZEPQUT.js.map +0 -1
- package/dist/chunk-HMWYAUA6.js.map +0 -1
- package/dist/chunk-IADZOXKN.js.map +0 -1
- package/dist/chunk-ILNIZ5I2.js.map +0 -1
- package/dist/chunk-INTFHMU4.js.map +0 -1
- package/dist/chunk-JKKAIPTU.js.map +0 -1
- package/dist/chunk-JNJWBTU2.js.map +0 -1
- package/dist/chunk-KM7RY2ZS.js +0 -246
- package/dist/chunk-KM7RY2ZS.js.map +0 -1
- package/dist/chunk-KOGZDMHO.js +0 -21
- package/dist/chunk-KOGZDMHO.js.map +0 -1
- package/dist/chunk-L6JEXB3E.js.map +0 -1
- package/dist/chunk-LRW6S53P.js.map +0 -1
- package/dist/chunk-LXAYUOXI.js +0 -511
- package/dist/chunk-LXAYUOXI.js.map +0 -1
- package/dist/chunk-MEFB5KHA.js +0 -59
- package/dist/chunk-MEFB5KHA.js.map +0 -1
- package/dist/chunk-MPZUYCAP.js.map +0 -1
- package/dist/chunk-NDGZDTSJ.js.map +0 -1
- package/dist/chunk-OJNTCYQQ.js.map +0 -1
- package/dist/chunk-P3LFTKE7.js +0 -58
- package/dist/chunk-P3LFTKE7.js.map +0 -1
- package/dist/chunk-QXP3Z3JN.js.map +0 -1
- package/dist/chunk-RVPYTUP4.js.map +0 -1
- package/dist/chunk-SNNSAGMT.js.map +0 -1
- package/dist/chunk-TC2WZLSC.js.map +0 -1
- package/dist/chunk-TKPF35W6.js +0 -29
- package/dist/chunk-TKPF35W6.js.map +0 -1
- package/dist/chunk-U3IKUGRP.js.map +0 -1
- package/dist/chunk-UUGOUOVT.js.map +0 -1
- package/dist/chunk-UXZONNEQ.js.map +0 -1
- package/dist/chunk-WXA2BWTE.js.map +0 -1
- package/dist/chunk-WYXUQGOL.js.map +0 -1
- package/dist/chunk-X5ZBP3IY.js.map +0 -1
- package/dist/chunk-XKESD27X.js +0 -10
- package/dist/chunk-XKESD27X.js.map +0 -1
- package/dist/chunk-XQJXNMSB.js.map +0 -1
- package/dist/chunk-Y527D4TV.js +0 -56
- package/dist/chunk-Y527D4TV.js.map +0 -1
- package/dist/chunk-ZDZERB6L.js.map +0 -1
- package/dist/chunk-ZNNXRNFD.js.map +0 -1
- package/dist/chunk-ZURZHELF.js.map +0 -1
- package/dist/cli-E36SPKBW.js.map +0 -1
- package/dist/codex-install-HQZDGFXW.js.map +0 -1
- package/dist/config-HY2E4GGW.js +0 -55
- package/dist/doctor-I6XQEKWD.js.map +0 -1
- package/dist/fund-2QRO7OJL.js +0 -378
- package/dist/fund-2QRO7OJL.js.map +0 -1
- package/dist/hook-command-2UBCSZG6.js +0 -43
- package/dist/install-ILUX5C4A.js +0 -107
- package/dist/install-identity-TZKGY6LQ.js +0 -31
- package/dist/install-wallet-5M4OTYLX.js.map +0 -1
- package/dist/mcp-LE4DUS22.js +0 -14127
- package/dist/mcp-LE4DUS22.js.map +0 -1
- package/dist/pay-WCG3IH4R.js +0 -55
- package/dist/payments-HFPPWHTV.js.map +0 -1
- package/dist/plugin-registration-PZFI5GZW.js +0 -68
- package/dist/runtime-UZOKSUZX.js +0 -1321
- package/dist/runtime-UZOKSUZX.js.map +0 -1
- package/dist/send-GCVEAQKL.js.map +0 -1
- package/dist/status-HCVAOCN7.js +0 -150
- package/dist/status-HCVAOCN7.js.map +0 -1
- package/dist/status-line-OPCXDHL2.js.map +0 -1
- package/dist/uninstall-BAWRBXXQ.js.map +0 -1
- package/dist/update-CACXLQLR.js +0 -56
- package/dist/wallet-QL4UUXEZ.js +0 -55
- /package/dist/{_esm-BIN7VY2L.js.map → _esm-2KST7HQ7.js.map} +0 -0
- /package/dist/{alive-GSOWKUOY.js.map → alive-5TV22PNF.js.map} +0 -0
- /package/dist/{auto-update-Y24UL56V.js.map → auth-XI7BJFLW.js.map} +0 -0
- /package/dist/{ccip-DHDFKTQV.js.map → auto-update-7L24KDWA.js.map} +0 -0
- /package/dist/{clack-EZ7TDWQU.js.map → balance-DFHC6XEU.js.map} +0 -0
- /package/dist/{claude-cli-2YKABJJZ.js.map → ccip-HOW3DZYF.js.map} +0 -0
- /package/dist/{chunk-IE7P2BZE.js.map → chunk-6QMDUDF2.js.map} +0 -0
- /package/dist/{chunk-KG3D7FIF.js.map → chunk-CZJOMUBP.js.map} +0 -0
- /package/dist/{chunk-3GNJXKIT.js.map → chunk-F43CV6UC.js.map} +0 -0
- /package/dist/{chunk-J57UC3DD.js.map → chunk-FKVJKLPI.js.map} +0 -0
- /package/dist/{chunk-SO3XRZPX.js.map → chunk-HPRVFJPM.js.map} +0 -0
- /package/dist/{chunk-2GP3HRYK.js.map → chunk-INSIW65E.js.map} +0 -0
- /package/dist/{chunk-RL5UUDT4.js.map → chunk-JNUKLSMZ.js.map} +0 -0
- /package/dist/{chunk-JRGFKVAZ.js.map → chunk-PETQ5WEA.js.map} +0 -0
- /package/dist/{codex-event-ZO2GEFE6.js.map → chunk-TUCKEEXE.js.map} +0 -0
- /package/dist/{chunk-BPMWXTFX.js.map → chunk-WFZX352D.js.map} +0 -0
- /package/dist/{chunk-6CMM6YCB.js.map → chunk-YWWMISDY.js.map} +0 -0
- /package/dist/{codex-host-KECJVTA2.js.map → clack-Q4RNC5VE.js.map} +0 -0
- /package/dist/{config-HY2E4GGW.js.map → claude-cli-GIIHOBGT.js.map} +0 -0
- /package/dist/{env-proxy-R6ZMLQTT.js.map → claude-install-BAUSJMW3.js.map} +0 -0
- /package/dist/{ethersCompat-Df4a6QIZ-X6YZWAML.js.map → codex-event-EUTCAOTQ.js.map} +0 -0
- /package/dist/{hook-command-2UBCSZG6.js.map → codex-host-ZON3S6HU.js.map} +0 -0
- /package/dist/{hosts-DVQJ32QC.js.map → config-QZOBMTEL.js.map} +0 -0
- /package/dist/{install-ILUX5C4A.js.map → config-rules-YOYHS6YL.js.map} +0 -0
- /package/dist/{install-identity-TZKGY6LQ.js.map → decision-QSKHOGCO.js.map} +0 -0
- /package/dist/{package-IJFGMWEV.js.map → env-proxy-XLSLHBKQ.js.map} +0 -0
- /package/dist/{passphrase-IFTDS3CU.js.map → ethersCompat-Df4a6QIZ-K3DMHR6Y.js.map} +0 -0
- /package/dist/{paths-XCSAKISU.js.map → flush-stdio-NYIJ3QXT.js.map} +0 -0
- /package/dist/{pay-WCG3IH4R.js.map → hook-command-VZ27MGWO.js.map} +0 -0
- /package/dist/{plugin-registration-PZFI5GZW.js.map → hosts-3EMTVM4H.js.map} +0 -0
- /package/dist/{redact-D7ASVWKL.js.map → install-ZA7L2UF3.js.map} +0 -0
- /package/dist/{runtime-fetch-ZSSSK72Y.js.map → install-funding-JDGYTF72.js.map} +0 -0
- /package/dist/{runtime-layout-WIDILZ6K.js.map → install-identity-FFAYEFLX.js.map} +0 -0
- /package/dist/{secp256k1-ZZZT2CAX.js.map → new-install-5L4GY3LN.js.map} +0 -0
- /package/dist/{update-CACXLQLR.js.map → package-BONDXSV3.js.map} +0 -0
- /package/dist/{usdc-VG4CIOUK.js.map → passphrase-T4ULBN4M.js.map} +0 -0
- /package/dist/{viemAdapter-By13KInL-DOFTEIJV.js.map → paths-O2KDWZ76.js.map} +0 -0
- /package/dist/{wallet-QL4UUXEZ.js.map → pay-CMV5KL74.js.map} +0 -0
|
@@ -0,0 +1,2933 @@
|
|
|
1
|
+
import { createRequire as __tenjinCreateRequire } from 'node:module'; const require = __tenjinCreateRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
BATCH_SETTLEMENT_ADDRESS,
|
|
4
|
+
BATCH_SETTLEMENT_SCHEME,
|
|
5
|
+
ERC3009_DEPOSIT_COLLECTOR_ADDRESS,
|
|
6
|
+
ErrCumulativeAmountBelowClaimed,
|
|
7
|
+
ErrCumulativeAmountMismatch,
|
|
8
|
+
ErrRefundAmountInvalid,
|
|
9
|
+
ErrRefundNoBalance,
|
|
10
|
+
MIN_WITHDRAW_DELAY,
|
|
11
|
+
PERMIT2_DEPOSIT_COLLECTOR_ADDRESS,
|
|
12
|
+
batchPermit2WitnessTypes,
|
|
13
|
+
computeChannelId,
|
|
14
|
+
createNonce,
|
|
15
|
+
createPermit2Nonce,
|
|
16
|
+
getBatchSettlementEip712Domain,
|
|
17
|
+
getEvmChainId,
|
|
18
|
+
normalizeChannelId,
|
|
19
|
+
receiveAuthorizationTypes,
|
|
20
|
+
voucherTypes
|
|
21
|
+
} from "./chunk-6QMDUDF2.js";
|
|
22
|
+
import {
|
|
23
|
+
isRecord
|
|
24
|
+
} from "./chunk-P4I7BFNJ.js";
|
|
25
|
+
import {
|
|
26
|
+
ADDITIVE_ARRAY_INFO_FIELDS,
|
|
27
|
+
convertToTokenAmount,
|
|
28
|
+
decodePaymentRequiredHeader,
|
|
29
|
+
decodePaymentResponseHeader,
|
|
30
|
+
deepEqual,
|
|
31
|
+
findByNetworkAndScheme,
|
|
32
|
+
findSchemesByNetwork,
|
|
33
|
+
networkMatchesPattern,
|
|
34
|
+
parseMoney,
|
|
35
|
+
toComparableArray,
|
|
36
|
+
x402HTTPClient,
|
|
37
|
+
x402Version
|
|
38
|
+
} from "./chunk-RGVCOG2S.js";
|
|
39
|
+
import {
|
|
40
|
+
USDC_ADDRESS
|
|
41
|
+
} from "./chunk-IGLWLH73.js";
|
|
42
|
+
import {
|
|
43
|
+
createPublicClient,
|
|
44
|
+
http,
|
|
45
|
+
recoverTypedDataAddress
|
|
46
|
+
} from "./chunk-7V25UAQO.js";
|
|
47
|
+
import {
|
|
48
|
+
encodeAbiParameters,
|
|
49
|
+
encodeFunctionData,
|
|
50
|
+
getAddress,
|
|
51
|
+
keccak256,
|
|
52
|
+
maxUint256,
|
|
53
|
+
toBytes
|
|
54
|
+
} from "./chunk-INSIW65E.js";
|
|
55
|
+
import {
|
|
56
|
+
ADDRESS_RE
|
|
57
|
+
} from "./chunk-JWAK7JQB.js";
|
|
58
|
+
import {
|
|
59
|
+
sanitizeForTerminal
|
|
60
|
+
} from "./chunk-KLDMLWXT.js";
|
|
61
|
+
import {
|
|
62
|
+
CliError
|
|
63
|
+
} from "./chunk-DI4QAGRZ.js";
|
|
64
|
+
|
|
65
|
+
// node_modules/.pnpm/@x402+core@2.28.0/node_modules/@x402/core/dist/esm/client/index.mjs
|
|
66
|
+
var DEFAULT_MAX_AMOUNT_PER_PAYMENT = "$1";
|
|
67
|
+
function isAtomicAmount(amount) {
|
|
68
|
+
return /^\d+$/.test(amount);
|
|
69
|
+
}
|
|
70
|
+
function findSpendControlAssetEntry(controls, requirement, defaultAsset) {
|
|
71
|
+
if (controls.allowedAssets === true) {
|
|
72
|
+
return void 0;
|
|
73
|
+
}
|
|
74
|
+
return controls.allowedAssets?.find((entry) => {
|
|
75
|
+
if (!networkMatchesPattern(entry.network, requirement.network)) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
if (entry.asset.toLowerCase() === requirement.asset.toLowerCase()) {
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
return defaultAsset != null && defaultAsset.symbol.toLowerCase() === entry.asset.toLowerCase();
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
function resolveAtomicSpendCap(controls, requirement, defaultAsset) {
|
|
85
|
+
if (controls === false) {
|
|
86
|
+
return void 0;
|
|
87
|
+
}
|
|
88
|
+
const assetEntry = findSpendControlAssetEntry(controls, requirement, defaultAsset);
|
|
89
|
+
if (assetEntry?.maxAmountPerPayment != null) {
|
|
90
|
+
if (!isAtomicAmount(assetEntry.maxAmountPerPayment)) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
`spendControls.allowedAssets[].maxAmountPerPayment must be an integer atomic amount, not a dollar value; got ${JSON.stringify(assetEntry.maxAmountPerPayment)}`
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
return assetEntry.maxAmountPerPayment;
|
|
96
|
+
}
|
|
97
|
+
if (!defaultAsset || controls.maxAmountPerPayment === false) {
|
|
98
|
+
return void 0;
|
|
99
|
+
}
|
|
100
|
+
const usdLimit = controls.maxAmountPerPayment ?? DEFAULT_MAX_AMOUNT_PER_PAYMENT;
|
|
101
|
+
return convertToTokenAmount(parseMoney(usdLimit).amount, defaultAsset.decimals);
|
|
102
|
+
}
|
|
103
|
+
var x402Client = class _x402Client {
|
|
104
|
+
/**
|
|
105
|
+
* Creates a new x402Client instance.
|
|
106
|
+
*
|
|
107
|
+
* @param paymentRequirementsSelector - Function to select payment requirements from available options
|
|
108
|
+
*/
|
|
109
|
+
constructor(paymentRequirementsSelector) {
|
|
110
|
+
this.registeredClientSchemes = /* @__PURE__ */ new Map();
|
|
111
|
+
this.schemeClientHookAdapters = /* @__PURE__ */ new Map();
|
|
112
|
+
this.policies = [];
|
|
113
|
+
this.registeredExtensions = /* @__PURE__ */ new Map();
|
|
114
|
+
this.spendControls = {};
|
|
115
|
+
this.beforePaymentCreationHooks = [];
|
|
116
|
+
this.afterPaymentCreationHooks = [];
|
|
117
|
+
this.onPaymentCreationFailureHooks = [];
|
|
118
|
+
this.paymentResponseHooks = [];
|
|
119
|
+
this.paymentRequirementsSelector = paymentRequirementsSelector || ((x402Version2, accepts) => accepts[0]);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Creates a new x402Client instance from a configuration object.
|
|
123
|
+
*
|
|
124
|
+
* @param config - The client configuration including schemes, policies, and payment requirements selector
|
|
125
|
+
* @returns A configured x402Client instance
|
|
126
|
+
*/
|
|
127
|
+
static fromConfig(config) {
|
|
128
|
+
const client = new _x402Client(config.paymentRequirementsSelector);
|
|
129
|
+
config.schemes.forEach((scheme) => {
|
|
130
|
+
if (scheme.x402Version === 1) {
|
|
131
|
+
client.registerV1(scheme.network, scheme.client);
|
|
132
|
+
} else {
|
|
133
|
+
client.register(scheme.network, scheme.client);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
config.policies?.forEach((policy) => {
|
|
137
|
+
client.registerPolicy(policy);
|
|
138
|
+
});
|
|
139
|
+
if (config.spendControls !== void 0) {
|
|
140
|
+
client.setSpendControls(config.spendControls);
|
|
141
|
+
}
|
|
142
|
+
return client;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Registers a scheme client for the current x402 version.
|
|
146
|
+
*
|
|
147
|
+
* @param network - The network to register the client for
|
|
148
|
+
* @param client - The scheme network client to register
|
|
149
|
+
* @returns The x402Client instance for chaining
|
|
150
|
+
*/
|
|
151
|
+
register(network, client) {
|
|
152
|
+
return this._registerScheme(x402Version, network, client);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Registers a scheme client for x402 version 1.
|
|
156
|
+
*
|
|
157
|
+
* @param network - The v1 network identifier (e.g., 'base-sepolia', 'solana-devnet')
|
|
158
|
+
* @param client - The scheme network client to register
|
|
159
|
+
* @returns The x402Client instance for chaining
|
|
160
|
+
*/
|
|
161
|
+
registerV1(network, client) {
|
|
162
|
+
return this._registerScheme(1, network, client);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Registers a policy to filter or transform payment requirements.
|
|
166
|
+
*
|
|
167
|
+
* Policies are applied in order after filtering by registered schemes
|
|
168
|
+
* and before the selector chooses the final payment requirement.
|
|
169
|
+
*
|
|
170
|
+
* @param policy - Function to filter/transform payment requirements
|
|
171
|
+
* @returns The x402Client instance for chaining
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```typescript
|
|
175
|
+
* // Prefer cheaper options
|
|
176
|
+
* client.registerPolicy((version, reqs) =>
|
|
177
|
+
* reqs.filter(r => BigInt(r.value) < BigInt('1000000'))
|
|
178
|
+
* );
|
|
179
|
+
*
|
|
180
|
+
* // Prefer specific networks
|
|
181
|
+
* client.registerPolicy((version, reqs) =>
|
|
182
|
+
* reqs.filter(r => r.network.startsWith('eip155:'))
|
|
183
|
+
* );
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
registerPolicy(policy) {
|
|
187
|
+
this.policies.push(policy);
|
|
188
|
+
return this;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Replace spend controls. Pass `false` to disable all spend controls.
|
|
192
|
+
* When an object is passed, omitted `maxAmountPerPayment` still defaults to
|
|
193
|
+
* {@link DEFAULT_MAX_AMOUNT_PER_PAYMENT}.
|
|
194
|
+
*
|
|
195
|
+
* @param controls - Spend control configuration, or `false` to disable
|
|
196
|
+
* @returns This client for chaining
|
|
197
|
+
*/
|
|
198
|
+
setSpendControls(controls) {
|
|
199
|
+
this.spendControls = controls;
|
|
200
|
+
return this;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Registers a client extension that can enrich payment payloads.
|
|
204
|
+
*
|
|
205
|
+
* Extensions are invoked after the scheme creates the base payload and the
|
|
206
|
+
* payload is wrapped with extensions/resource/accepted data. Every registered
|
|
207
|
+
* extension's `enrichPaymentPayload` hook is called to modify the payload.
|
|
208
|
+
* Server-declared fields are preserved via merge after enrichment.
|
|
209
|
+
*
|
|
210
|
+
* @param extension - The client extension to register
|
|
211
|
+
* @returns The x402Client instance for chaining
|
|
212
|
+
*/
|
|
213
|
+
registerExtension(extension) {
|
|
214
|
+
this.registeredExtensions.set(extension.key, extension);
|
|
215
|
+
return this;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Get all registered client extensions.
|
|
219
|
+
*
|
|
220
|
+
* @returns Array of registered extensions
|
|
221
|
+
*/
|
|
222
|
+
getExtensions() {
|
|
223
|
+
return Array.from(this.registeredExtensions.values());
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Register a hook to execute before payment payload creation.
|
|
227
|
+
* Can abort creation by returning { abort: true, reason: string }
|
|
228
|
+
*
|
|
229
|
+
* @param hook - The hook function to register
|
|
230
|
+
* @returns The x402Client instance for chaining
|
|
231
|
+
*/
|
|
232
|
+
onBeforePaymentCreation(hook) {
|
|
233
|
+
this.beforePaymentCreationHooks.push(hook);
|
|
234
|
+
return this;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Register a hook to execute after successful payment payload creation.
|
|
238
|
+
*
|
|
239
|
+
* @param hook - The hook function to register
|
|
240
|
+
* @returns The x402Client instance for chaining
|
|
241
|
+
*/
|
|
242
|
+
onAfterPaymentCreation(hook) {
|
|
243
|
+
this.afterPaymentCreationHooks.push(hook);
|
|
244
|
+
return this;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Register a hook to execute when payment payload creation fails.
|
|
248
|
+
* Can recover from failure by returning { recovered: true, payload: PaymentPayload }
|
|
249
|
+
*
|
|
250
|
+
* @param hook - The hook function to register
|
|
251
|
+
* @returns The x402Client instance for chaining
|
|
252
|
+
*/
|
|
253
|
+
onPaymentCreationFailure(hook) {
|
|
254
|
+
this.onPaymentCreationFailureHooks.push(hook);
|
|
255
|
+
return this;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Register a hook to execute after a paid request completes.
|
|
259
|
+
* Can signal recovery by returning { recovered: true }, causing the transport to retry.
|
|
260
|
+
*
|
|
261
|
+
* @param hook - The hook function to register
|
|
262
|
+
* @returns The x402Client instance for chaining
|
|
263
|
+
*/
|
|
264
|
+
onPaymentResponse(hook) {
|
|
265
|
+
this.paymentResponseHooks.push(hook);
|
|
266
|
+
return this;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Fires all registered payment response hooks in order.
|
|
270
|
+
* Returns `{ recovered: true }` if any hook signals recovery (first wins).
|
|
271
|
+
*
|
|
272
|
+
* @param ctx - The payment response context
|
|
273
|
+
* @returns Recovery signal or undefined
|
|
274
|
+
*/
|
|
275
|
+
async handlePaymentResponse(ctx) {
|
|
276
|
+
for (const hook of this.getLabeledHooks(
|
|
277
|
+
"onPaymentResponse",
|
|
278
|
+
ctx.paymentPayload.x402Version,
|
|
279
|
+
ctx.requirements,
|
|
280
|
+
ctx.paymentRequired?.extensions ?? ctx.paymentPayload.extensions
|
|
281
|
+
)) {
|
|
282
|
+
const result = await hook(ctx);
|
|
283
|
+
if (result && "recovered" in result && result.recovered) {
|
|
284
|
+
return { recovered: true };
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return void 0;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Creates a payment payload based on a PaymentRequired response.
|
|
291
|
+
*
|
|
292
|
+
* Automatically extracts x402Version, resource, and extensions from the PaymentRequired
|
|
293
|
+
* response and constructs a complete PaymentPayload with the accepted requirements.
|
|
294
|
+
*
|
|
295
|
+
* @param paymentRequired - The PaymentRequired response from the server
|
|
296
|
+
* @returns Promise resolving to the complete payment payload
|
|
297
|
+
*/
|
|
298
|
+
async createPaymentPayload(paymentRequired) {
|
|
299
|
+
const clientSchemesByNetwork = this.registeredClientSchemes.get(paymentRequired.x402Version);
|
|
300
|
+
if (!clientSchemesByNetwork) {
|
|
301
|
+
throw new Error(`No client registered for x402 version: ${paymentRequired.x402Version}`);
|
|
302
|
+
}
|
|
303
|
+
const requirements = this.selectPaymentRequirements(paymentRequired.x402Version, paymentRequired.accepts);
|
|
304
|
+
const context = {
|
|
305
|
+
paymentRequired,
|
|
306
|
+
selectedRequirements: requirements
|
|
307
|
+
};
|
|
308
|
+
for (const hook of this.getLabeledHooks(
|
|
309
|
+
"beforePaymentCreation",
|
|
310
|
+
paymentRequired.x402Version,
|
|
311
|
+
requirements,
|
|
312
|
+
paymentRequired.extensions
|
|
313
|
+
)) {
|
|
314
|
+
const result = await hook(context);
|
|
315
|
+
if (result && "abort" in result && result.abort) {
|
|
316
|
+
throw new Error(`Payment creation aborted: ${result.reason}`);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
try {
|
|
320
|
+
const schemeNetworkClient = findByNetworkAndScheme(clientSchemesByNetwork, requirements.scheme, requirements.network);
|
|
321
|
+
if (!schemeNetworkClient) {
|
|
322
|
+
throw new Error(`No client registered for scheme: ${requirements.scheme} and network: ${requirements.network}`);
|
|
323
|
+
}
|
|
324
|
+
const partialPayload = await schemeNetworkClient.createPaymentPayload(
|
|
325
|
+
paymentRequired.x402Version,
|
|
326
|
+
requirements,
|
|
327
|
+
{
|
|
328
|
+
extensions: paymentRequired.extensions,
|
|
329
|
+
maxAmountPerPayment: resolveAtomicSpendCap(
|
|
330
|
+
this.spendControls,
|
|
331
|
+
requirements,
|
|
332
|
+
schemeNetworkClient.findDefaultAsset?.(requirements.asset, requirements.network)
|
|
333
|
+
)
|
|
334
|
+
}
|
|
335
|
+
);
|
|
336
|
+
let paymentPayload;
|
|
337
|
+
if (partialPayload.x402Version == 1) {
|
|
338
|
+
paymentPayload = partialPayload;
|
|
339
|
+
} else {
|
|
340
|
+
const mergedExtensions = this.mergeExtensions(
|
|
341
|
+
paymentRequired.extensions,
|
|
342
|
+
partialPayload.extensions
|
|
343
|
+
);
|
|
344
|
+
paymentPayload = {
|
|
345
|
+
x402Version: partialPayload.x402Version,
|
|
346
|
+
payload: partialPayload.payload,
|
|
347
|
+
extensions: mergedExtensions,
|
|
348
|
+
resource: paymentRequired.resource,
|
|
349
|
+
accepted: requirements
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
paymentPayload = await this.enrichPaymentPayloadWithExtensions(paymentPayload, paymentRequired);
|
|
353
|
+
const createdContext = {
|
|
354
|
+
...context,
|
|
355
|
+
paymentPayload
|
|
356
|
+
};
|
|
357
|
+
for (const hook of this.getLabeledHooks(
|
|
358
|
+
"afterPaymentCreation",
|
|
359
|
+
paymentRequired.x402Version,
|
|
360
|
+
requirements,
|
|
361
|
+
paymentRequired.extensions
|
|
362
|
+
)) {
|
|
363
|
+
await hook(createdContext);
|
|
364
|
+
}
|
|
365
|
+
return paymentPayload;
|
|
366
|
+
} catch (error) {
|
|
367
|
+
const failureContext = {
|
|
368
|
+
...context,
|
|
369
|
+
error
|
|
370
|
+
};
|
|
371
|
+
for (const hook of this.getLabeledHooks(
|
|
372
|
+
"onPaymentCreationFailure",
|
|
373
|
+
paymentRequired.x402Version,
|
|
374
|
+
requirements,
|
|
375
|
+
paymentRequired.extensions
|
|
376
|
+
)) {
|
|
377
|
+
const result = await hook(failureContext);
|
|
378
|
+
if (result && "recovered" in result && result.recovered) {
|
|
379
|
+
return result.payload;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
throw error;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Merges server-declared extensions with client extension echoes.
|
|
387
|
+
* Client extension data may add fields, but server-declared fields remain intact.
|
|
388
|
+
* For fields listed in `ADDITIVE_ARRAY_INFO_FIELDS` (e.g. builder-code `s`), a
|
|
389
|
+
* conflicting array is concatenated with client entries first (so a downstream
|
|
390
|
+
* length cap trims server entries rather than the client's) and duplicates
|
|
391
|
+
* removed; a scalar on either side is treated as a single-element array. Every
|
|
392
|
+
* other conflicting array keeps the server's value, same as any other scalar.
|
|
393
|
+
*
|
|
394
|
+
* @param serverExtensions - Extensions declared by the server in the 402 response
|
|
395
|
+
* @param clientExtensions - Extensions provided by the client or scheme
|
|
396
|
+
* @returns The merged extensions object, or undefined if both inputs are undefined
|
|
397
|
+
*/
|
|
398
|
+
mergeExtensions(serverExtensions, clientExtensions) {
|
|
399
|
+
if (!clientExtensions) return serverExtensions;
|
|
400
|
+
if (!serverExtensions) return clientExtensions;
|
|
401
|
+
const merged = { ...serverExtensions };
|
|
402
|
+
for (const [key, clientValue] of Object.entries(clientExtensions)) {
|
|
403
|
+
const serverValue = merged[key];
|
|
404
|
+
if (serverValue === null || typeof serverValue !== "object" || Array.isArray(serverValue) || clientValue === null || typeof clientValue !== "object" || Array.isArray(clientValue)) {
|
|
405
|
+
merged[key] = clientValue;
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
const serverRecord = serverValue;
|
|
409
|
+
const clientRecord = clientValue;
|
|
410
|
+
const additiveFields = ADDITIVE_ARRAY_INFO_FIELDS[key];
|
|
411
|
+
const extensionValue = { ...serverRecord };
|
|
412
|
+
const pending = [{ target: extensionValue, source: clientRecord }];
|
|
413
|
+
for (const item of pending) {
|
|
414
|
+
for (const [fieldKey, clientFieldValue] of Object.entries(item.source)) {
|
|
415
|
+
const serverFieldValue = item.target[fieldKey];
|
|
416
|
+
if (serverFieldValue !== null && typeof serverFieldValue === "object" && !Array.isArray(serverFieldValue) && clientFieldValue !== null && typeof clientFieldValue === "object" && !Array.isArray(clientFieldValue)) {
|
|
417
|
+
const nestedValue = { ...serverFieldValue };
|
|
418
|
+
item.target[fieldKey] = nestedValue;
|
|
419
|
+
pending.push({
|
|
420
|
+
target: nestedValue,
|
|
421
|
+
source: clientFieldValue
|
|
422
|
+
});
|
|
423
|
+
continue;
|
|
424
|
+
}
|
|
425
|
+
if (additiveFields?.has(fieldKey) && (Array.isArray(serverFieldValue) || Array.isArray(clientFieldValue))) {
|
|
426
|
+
const serverArray = toComparableArray(serverFieldValue);
|
|
427
|
+
const clientArray = toComparableArray(clientFieldValue);
|
|
428
|
+
if (serverArray && clientArray) {
|
|
429
|
+
item.target[fieldKey] = mergeArraysUnique(clientArray, serverArray);
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
if (!Object.prototype.hasOwnProperty.call(item.target, fieldKey)) {
|
|
434
|
+
item.target[fieldKey] = clientFieldValue;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
merged[key] = extensionValue;
|
|
439
|
+
}
|
|
440
|
+
return merged;
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Enriches a payment payload by calling registered extension hooks.
|
|
444
|
+
* Invokes enrichPaymentPayload for every registered extension, then merges
|
|
445
|
+
* server-declared extension fields back into the result.
|
|
446
|
+
*
|
|
447
|
+
* @param paymentPayload - The payment payload to enrich with extension data
|
|
448
|
+
* @param paymentRequired - The PaymentRequired response containing extension declarations
|
|
449
|
+
* @returns The enriched payment payload with extension data applied
|
|
450
|
+
*/
|
|
451
|
+
async enrichPaymentPayloadWithExtensions(paymentPayload, paymentRequired) {
|
|
452
|
+
if (this.registeredExtensions.size === 0) {
|
|
453
|
+
return paymentPayload;
|
|
454
|
+
}
|
|
455
|
+
let enriched = paymentPayload;
|
|
456
|
+
for (const [, extension] of this.registeredExtensions) {
|
|
457
|
+
if (extension.enrichPaymentPayload) {
|
|
458
|
+
enriched = await extension.enrichPaymentPayload(enriched, paymentRequired);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
return {
|
|
462
|
+
...enriched,
|
|
463
|
+
extensions: this.mergeExtensions(paymentRequired.extensions, enriched.extensions)
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Selects appropriate payment requirements based on registered clients and policies.
|
|
468
|
+
*
|
|
469
|
+
* Selection process:
|
|
470
|
+
* 1. Filter by registered schemes (network + scheme support)
|
|
471
|
+
* 2. Drop accepts with unrecognized `extra.paymentFlow`
|
|
472
|
+
* 3. Enforce spend controls (allowlist, per-asset caps, USD cap on default assets)
|
|
473
|
+
* 4. Apply all registered policies in order
|
|
474
|
+
* 5. Prefer authorization (omit or explicit) over upfront/escrow when both remain
|
|
475
|
+
* 6. Use selector to choose final requirement
|
|
476
|
+
*
|
|
477
|
+
* @param x402Version - The x402 protocol version
|
|
478
|
+
* @param paymentRequirements - Array of available payment requirements
|
|
479
|
+
* @returns The selected payment requirements
|
|
480
|
+
*/
|
|
481
|
+
selectPaymentRequirements(x402Version2, paymentRequirements) {
|
|
482
|
+
const clientSchemesByNetwork = this.registeredClientSchemes.get(x402Version2);
|
|
483
|
+
if (!clientSchemesByNetwork) {
|
|
484
|
+
throw new Error(`No client registered for x402 version: ${x402Version2}`);
|
|
485
|
+
}
|
|
486
|
+
const supportedPaymentRequirements = paymentRequirements.filter((requirement) => {
|
|
487
|
+
let clientSchemes = findSchemesByNetwork(clientSchemesByNetwork, requirement.network);
|
|
488
|
+
if (!clientSchemes) {
|
|
489
|
+
return false;
|
|
490
|
+
}
|
|
491
|
+
return clientSchemes.has(requirement.scheme);
|
|
492
|
+
});
|
|
493
|
+
if (supportedPaymentRequirements.length === 0) {
|
|
494
|
+
throw new Error(`No network/scheme registered for x402 version: ${x402Version2} which comply with the payment requirements. ${JSON.stringify({
|
|
495
|
+
x402Version: x402Version2,
|
|
496
|
+
paymentRequirements,
|
|
497
|
+
x402Versions: Array.from(this.registeredClientSchemes.keys()),
|
|
498
|
+
networks: Array.from(clientSchemesByNetwork.keys()),
|
|
499
|
+
schemes: Array.from(clientSchemesByNetwork.values()).map((schemes) => Array.from(schemes.keys())).flat()
|
|
500
|
+
})}`);
|
|
501
|
+
}
|
|
502
|
+
const recognizedFlowRequirements = supportedPaymentRequirements.filter((requirement) => {
|
|
503
|
+
const flow = requirement.extra?.paymentFlow;
|
|
504
|
+
return flow == null || flow === "authorization" || flow === "upfront" || flow === "escrow";
|
|
505
|
+
});
|
|
506
|
+
if (recognizedFlowRequirements.length === 0) {
|
|
507
|
+
throw new Error(
|
|
508
|
+
`No payment requirements with a recognized paymentFlow for x402 version: ${x402Version2}`
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
let filteredRequirements = this.applySpendControls(
|
|
512
|
+
x402Version2,
|
|
513
|
+
recognizedFlowRequirements,
|
|
514
|
+
clientSchemesByNetwork
|
|
515
|
+
);
|
|
516
|
+
for (const policy of this.policies) {
|
|
517
|
+
filteredRequirements = policy(x402Version2, filteredRequirements);
|
|
518
|
+
if (filteredRequirements.length === 0) {
|
|
519
|
+
throw new Error(`All payment requirements were filtered out by policies for x402 version: ${x402Version2}`);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
const authorizationAccepts = filteredRequirements.filter(
|
|
523
|
+
(requirement) => requirement.extra?.paymentFlow == null || requirement.extra?.paymentFlow === "authorization"
|
|
524
|
+
);
|
|
525
|
+
if (authorizationAccepts.length > 0) {
|
|
526
|
+
filteredRequirements = authorizationAccepts;
|
|
527
|
+
}
|
|
528
|
+
return this.paymentRequirementsSelector(x402Version2, filteredRequirements);
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Filter by spend controls (default-asset allowlist → opt-in assets → caps).
|
|
532
|
+
* Keeps any accept that fits so a mixed offer can still pay the affordable option.
|
|
533
|
+
*
|
|
534
|
+
* @param x402Version - Protocol version (v1 uses `maxAmountRequired`)
|
|
535
|
+
* @param requirements - Post scheme/flow filter
|
|
536
|
+
* @param clientSchemesByNetwork - Registered clients for this version
|
|
537
|
+
* @returns Requirements that pass spend controls
|
|
538
|
+
*/
|
|
539
|
+
applySpendControls(x402Version2, requirements, clientSchemesByNetwork) {
|
|
540
|
+
const controls = this.spendControls;
|
|
541
|
+
if (controls === false) {
|
|
542
|
+
return requirements;
|
|
543
|
+
}
|
|
544
|
+
const rawAmountOf = (requirement) => x402Version2 === 1 ? requirement.maxAmountRequired : requirement.amount;
|
|
545
|
+
const schemeFor = (requirement) => findByNetworkAndScheme(
|
|
546
|
+
clientSchemesByNetwork,
|
|
547
|
+
requirement.scheme,
|
|
548
|
+
requirement.network
|
|
549
|
+
);
|
|
550
|
+
const defaultAssetFor = (requirement) => schemeFor(requirement)?.findDefaultAsset?.(requirement.asset, requirement.network);
|
|
551
|
+
const findAssetEntry = (requirement) => findSpendControlAssetEntry(controls, requirement, defaultAssetFor(requirement));
|
|
552
|
+
const allowAnyAsset = controls.allowedAssets === true;
|
|
553
|
+
let filtered = allowAnyAsset ? requirements : requirements.filter((requirement) => {
|
|
554
|
+
if (defaultAssetFor(requirement) != null) {
|
|
555
|
+
return true;
|
|
556
|
+
}
|
|
557
|
+
return findAssetEntry(requirement) != null;
|
|
558
|
+
});
|
|
559
|
+
if (filtered.length === 0) {
|
|
560
|
+
throw new Error(
|
|
561
|
+
`All payment requirements were rejected by spendControls: only default assets or entries in spendControls.allowedAssets are allowed. Add an allowedAssets entry for non-default tokens, set allowedAssets: true, or set spendControls: false.`
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
const usdLimit = controls.maxAmountPerPayment === false ? false : controls.maxAmountPerPayment ?? DEFAULT_MAX_AMOUNT_PER_PAYMENT;
|
|
565
|
+
const beforeAmountCaps = filtered;
|
|
566
|
+
let rejectedByAssetCap = false;
|
|
567
|
+
let rejectedUsdSymbol;
|
|
568
|
+
filtered = filtered.filter((requirement) => {
|
|
569
|
+
const defaultAsset = defaultAssetFor(requirement);
|
|
570
|
+
const assetEntry = findSpendControlAssetEntry(controls, requirement, defaultAsset);
|
|
571
|
+
const cap = resolveAtomicSpendCap(controls, requirement, defaultAsset);
|
|
572
|
+
if (cap === void 0) {
|
|
573
|
+
return true;
|
|
574
|
+
}
|
|
575
|
+
const rawAmount = rawAmountOf(requirement);
|
|
576
|
+
if (!isAtomicAmount(rawAmount)) {
|
|
577
|
+
if (assetEntry?.maxAmountPerPayment != null) {
|
|
578
|
+
rejectedByAssetCap = true;
|
|
579
|
+
return false;
|
|
580
|
+
}
|
|
581
|
+
if (usdLimit === false) {
|
|
582
|
+
return true;
|
|
583
|
+
}
|
|
584
|
+
const valueScaled = BigInt(convertToTokenAmount(rawAmount, 18));
|
|
585
|
+
const capScaled = BigInt(convertToTokenAmount(parseMoney(usdLimit).amount, 18));
|
|
586
|
+
const ok2 = valueScaled <= capScaled;
|
|
587
|
+
if (!ok2) rejectedUsdSymbol = defaultAsset?.symbol;
|
|
588
|
+
return ok2;
|
|
589
|
+
}
|
|
590
|
+
const ok = BigInt(rawAmount) <= BigInt(cap);
|
|
591
|
+
if (!ok) {
|
|
592
|
+
if (assetEntry?.maxAmountPerPayment != null) {
|
|
593
|
+
rejectedByAssetCap = true;
|
|
594
|
+
} else {
|
|
595
|
+
rejectedUsdSymbol = defaultAsset?.symbol;
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
return ok;
|
|
599
|
+
});
|
|
600
|
+
if (filtered.length === 0) {
|
|
601
|
+
if (rejectedByAssetCap && beforeAmountCaps.every((requirement) => {
|
|
602
|
+
const entry = findAssetEntry(requirement);
|
|
603
|
+
return entry?.maxAmountPerPayment != null;
|
|
604
|
+
})) {
|
|
605
|
+
throw new Error(
|
|
606
|
+
`All payment requirements were rejected by spendControls.allowedAssets maxAmountPerPayment. Raise the per-asset cap, or omit maxAmountPerPayment to allow uncapped (default assets then fall back to the top-level USD cap).`
|
|
607
|
+
);
|
|
608
|
+
}
|
|
609
|
+
throw new Error(
|
|
610
|
+
`All payment requirements were rejected by spendControls.maxAmountPerPayment (${String(usdLimit)}${rejectedUsdSymbol ? `, including ${rejectedUsdSymbol}` : ""}). Raise maxAmountPerPayment, set it to false to disable, set allowedAssets[].maxAmountPerPayment for a per-asset atomic cap, or set spendControls: false to disable all spend controls.`
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
return filtered;
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* Internal method to register a scheme client.
|
|
617
|
+
*
|
|
618
|
+
* @param x402Version - The x402 protocol version
|
|
619
|
+
* @param network - The network to register the client for
|
|
620
|
+
* @param client - The scheme network client to register
|
|
621
|
+
* @returns The x402Client instance for chaining
|
|
622
|
+
*/
|
|
623
|
+
_registerScheme(x402Version2, network, client) {
|
|
624
|
+
if (!this.registeredClientSchemes.has(x402Version2)) {
|
|
625
|
+
this.registeredClientSchemes.set(x402Version2, /* @__PURE__ */ new Map());
|
|
626
|
+
}
|
|
627
|
+
const clientSchemesByNetwork = this.registeredClientSchemes.get(x402Version2);
|
|
628
|
+
if (!clientSchemesByNetwork.has(network)) {
|
|
629
|
+
clientSchemesByNetwork.set(network, /* @__PURE__ */ new Map());
|
|
630
|
+
}
|
|
631
|
+
const clientByScheme = clientSchemesByNetwork.get(network);
|
|
632
|
+
clientByScheme.set(client.scheme, client);
|
|
633
|
+
if (!this.schemeClientHookAdapters.has(x402Version2)) {
|
|
634
|
+
this.schemeClientHookAdapters.set(x402Version2, /* @__PURE__ */ new Map());
|
|
635
|
+
}
|
|
636
|
+
const adaptersByNetwork = this.schemeClientHookAdapters.get(x402Version2);
|
|
637
|
+
if (!adaptersByNetwork.has(network)) {
|
|
638
|
+
adaptersByNetwork.set(network, /* @__PURE__ */ new Map());
|
|
639
|
+
}
|
|
640
|
+
const adaptersByScheme = adaptersByNetwork.get(network);
|
|
641
|
+
const hooks = client.schemeHooks;
|
|
642
|
+
if (!hooks) {
|
|
643
|
+
adaptersByScheme.delete(client.scheme);
|
|
644
|
+
return this;
|
|
645
|
+
}
|
|
646
|
+
const handles = {};
|
|
647
|
+
if (hooks.onBeforePaymentCreation) {
|
|
648
|
+
handles.beforePaymentCreation = hooks.onBeforePaymentCreation;
|
|
649
|
+
}
|
|
650
|
+
if (hooks.onAfterPaymentCreation) {
|
|
651
|
+
handles.afterPaymentCreation = hooks.onAfterPaymentCreation;
|
|
652
|
+
}
|
|
653
|
+
if (hooks.onPaymentCreationFailure) {
|
|
654
|
+
handles.onPaymentCreationFailure = hooks.onPaymentCreationFailure;
|
|
655
|
+
}
|
|
656
|
+
if (hooks.onPaymentResponse) {
|
|
657
|
+
handles.onPaymentResponse = hooks.onPaymentResponse;
|
|
658
|
+
}
|
|
659
|
+
if (Object.keys(handles).length > 0) {
|
|
660
|
+
adaptersByScheme.set(client.scheme, handles);
|
|
661
|
+
} else {
|
|
662
|
+
adaptersByScheme.delete(client.scheme);
|
|
663
|
+
}
|
|
664
|
+
return this;
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* Returns manual hooks followed by the selected scheme hook and declared extension hooks.
|
|
668
|
+
*
|
|
669
|
+
* @param phase - Hook slot to collect
|
|
670
|
+
* @param x402Version - Protocol version for the selected requirement
|
|
671
|
+
* @param requirements - Selected payment requirement
|
|
672
|
+
* @param declaredExtensions - Extension declarations that scope extension hooks
|
|
673
|
+
* @returns Hooks in invocation order
|
|
674
|
+
*/
|
|
675
|
+
getLabeledHooks(phase, x402Version2, requirements, declaredExtensions) {
|
|
676
|
+
let manual;
|
|
677
|
+
switch (phase) {
|
|
678
|
+
case "beforePaymentCreation":
|
|
679
|
+
manual = this.beforePaymentCreationHooks;
|
|
680
|
+
break;
|
|
681
|
+
case "afterPaymentCreation":
|
|
682
|
+
manual = this.afterPaymentCreationHooks;
|
|
683
|
+
break;
|
|
684
|
+
case "onPaymentCreationFailure":
|
|
685
|
+
manual = this.onPaymentCreationFailureHooks;
|
|
686
|
+
break;
|
|
687
|
+
case "onPaymentResponse":
|
|
688
|
+
manual = this.paymentResponseHooks;
|
|
689
|
+
break;
|
|
690
|
+
}
|
|
691
|
+
const out = [...manual];
|
|
692
|
+
const adaptersByNetwork = this.schemeClientHookAdapters.get(x402Version2);
|
|
693
|
+
const schemeAdapter = adaptersByNetwork ? findByNetworkAndScheme(adaptersByNetwork, requirements.scheme, requirements.network) : void 0;
|
|
694
|
+
const hook = schemeAdapter?.[phase];
|
|
695
|
+
if (hook !== void 0) {
|
|
696
|
+
out.push(hook);
|
|
697
|
+
}
|
|
698
|
+
if (!declaredExtensions) {
|
|
699
|
+
return out;
|
|
700
|
+
}
|
|
701
|
+
const extensionHookKey = this.getClientExtensionHookKey(phase);
|
|
702
|
+
for (const [extensionKey, extension] of this.registeredExtensions) {
|
|
703
|
+
if (!(extensionKey in declaredExtensions)) continue;
|
|
704
|
+
const extensionHook = extension.hooks?.[extensionHookKey];
|
|
705
|
+
if (!extensionHook) continue;
|
|
706
|
+
out.push((async (ctx) => {
|
|
707
|
+
return extensionHook(declaredExtensions[extensionKey], ctx);
|
|
708
|
+
}));
|
|
709
|
+
}
|
|
710
|
+
return out;
|
|
711
|
+
}
|
|
712
|
+
/**
|
|
713
|
+
* Maps internal hook phases to extension hook names.
|
|
714
|
+
*
|
|
715
|
+
* @param phase - Internal hook phase
|
|
716
|
+
* @returns Extension hook key for the phase
|
|
717
|
+
*/
|
|
718
|
+
getClientExtensionHookKey(phase) {
|
|
719
|
+
switch (phase) {
|
|
720
|
+
case "beforePaymentCreation":
|
|
721
|
+
return "onBeforePaymentCreation";
|
|
722
|
+
case "afterPaymentCreation":
|
|
723
|
+
return "onAfterPaymentCreation";
|
|
724
|
+
case "onPaymentCreationFailure":
|
|
725
|
+
return "onPaymentCreationFailure";
|
|
726
|
+
case "onPaymentResponse":
|
|
727
|
+
return "onPaymentResponse";
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
};
|
|
731
|
+
function mergeArraysUnique(client, server) {
|
|
732
|
+
const merged = [];
|
|
733
|
+
for (const item of [...client, ...server]) {
|
|
734
|
+
if (!merged.some((existing) => deepEqual(existing, item))) {
|
|
735
|
+
merged.push(item);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
return merged;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
// node_modules/.pnpm/@x402+evm@2.28.0_typescript@6.0.3/node_modules/@x402/evm/dist/esm/chunk-GPMYUGHX.mjs
|
|
742
|
+
var EVM_NETWORK_CHAIN_ID_MAP = {
|
|
743
|
+
ethereum: 1,
|
|
744
|
+
sepolia: 11155111,
|
|
745
|
+
abstract: 2741,
|
|
746
|
+
"abstract-testnet": 11124,
|
|
747
|
+
"base-sepolia": 84532,
|
|
748
|
+
base: 8453,
|
|
749
|
+
"avalanche-fuji": 43113,
|
|
750
|
+
avalanche: 43114,
|
|
751
|
+
iotex: 4689,
|
|
752
|
+
sei: 1329,
|
|
753
|
+
"sei-testnet": 1328,
|
|
754
|
+
polygon: 137,
|
|
755
|
+
"polygon-amoy": 80002,
|
|
756
|
+
peaq: 3338,
|
|
757
|
+
story: 1514,
|
|
758
|
+
educhain: 41923,
|
|
759
|
+
"skale-base-sepolia": 324705682,
|
|
760
|
+
megaeth: 4326,
|
|
761
|
+
monad: 143,
|
|
762
|
+
"monad-testnet": 10143,
|
|
763
|
+
stable: 988,
|
|
764
|
+
"stable-testnet": 2201,
|
|
765
|
+
celo: 42220,
|
|
766
|
+
flare: 14
|
|
767
|
+
};
|
|
768
|
+
var NETWORKS = Object.keys(EVM_NETWORK_CHAIN_ID_MAP);
|
|
769
|
+
var authorizationTypes = {
|
|
770
|
+
TransferWithAuthorization: [
|
|
771
|
+
{ name: "from", type: "address" },
|
|
772
|
+
{ name: "to", type: "address" },
|
|
773
|
+
{ name: "value", type: "uint256" },
|
|
774
|
+
{ name: "validAfter", type: "uint256" },
|
|
775
|
+
{ name: "validBefore", type: "uint256" },
|
|
776
|
+
{ name: "nonce", type: "bytes32" }
|
|
777
|
+
]
|
|
778
|
+
};
|
|
779
|
+
var permit2WitnessTypes = {
|
|
780
|
+
PermitWitnessTransferFrom: [
|
|
781
|
+
{ name: "permitted", type: "TokenPermissions" },
|
|
782
|
+
{ name: "spender", type: "address" },
|
|
783
|
+
{ name: "nonce", type: "uint256" },
|
|
784
|
+
{ name: "deadline", type: "uint256" },
|
|
785
|
+
{ name: "witness", type: "Witness" }
|
|
786
|
+
],
|
|
787
|
+
TokenPermissions: [
|
|
788
|
+
{ name: "token", type: "address" },
|
|
789
|
+
{ name: "amount", type: "uint256" }
|
|
790
|
+
],
|
|
791
|
+
Witness: [
|
|
792
|
+
{ name: "to", type: "address" },
|
|
793
|
+
{ name: "validAfter", type: "uint256" }
|
|
794
|
+
]
|
|
795
|
+
};
|
|
796
|
+
var eip2612PermitTypes = {
|
|
797
|
+
Permit: [
|
|
798
|
+
{ name: "owner", type: "address" },
|
|
799
|
+
{ name: "spender", type: "address" },
|
|
800
|
+
{ name: "value", type: "uint256" },
|
|
801
|
+
{ name: "nonce", type: "uint256" },
|
|
802
|
+
{ name: "deadline", type: "uint256" }
|
|
803
|
+
]
|
|
804
|
+
};
|
|
805
|
+
var eip2612NoncesAbi = [
|
|
806
|
+
{
|
|
807
|
+
type: "function",
|
|
808
|
+
name: "nonces",
|
|
809
|
+
inputs: [{ name: "owner", type: "address" }],
|
|
810
|
+
outputs: [{ type: "uint256" }],
|
|
811
|
+
stateMutability: "view"
|
|
812
|
+
}
|
|
813
|
+
];
|
|
814
|
+
var erc20ApproveAbi = [
|
|
815
|
+
{
|
|
816
|
+
type: "function",
|
|
817
|
+
name: "approve",
|
|
818
|
+
inputs: [
|
|
819
|
+
{ name: "spender", type: "address" },
|
|
820
|
+
{ name: "amount", type: "uint256" }
|
|
821
|
+
],
|
|
822
|
+
outputs: [{ type: "bool" }],
|
|
823
|
+
stateMutability: "nonpayable"
|
|
824
|
+
}
|
|
825
|
+
];
|
|
826
|
+
var erc20AllowanceAbi = [
|
|
827
|
+
{
|
|
828
|
+
type: "function",
|
|
829
|
+
name: "allowance",
|
|
830
|
+
inputs: [
|
|
831
|
+
{ name: "owner", type: "address" },
|
|
832
|
+
{ name: "spender", type: "address" }
|
|
833
|
+
],
|
|
834
|
+
outputs: [{ type: "uint256" }],
|
|
835
|
+
stateMutability: "view"
|
|
836
|
+
}
|
|
837
|
+
];
|
|
838
|
+
var ERC20_APPROVE_GAS_LIMIT = 70000n;
|
|
839
|
+
var DEFAULT_MAX_FEE_PER_GAS = 1000000000n;
|
|
840
|
+
var DEFAULT_MAX_PRIORITY_FEE_PER_GAS = 100000000n;
|
|
841
|
+
var PERMIT2_ADDRESS = "0x000000000022D473030F116dDEE9F6B43aC78BA3";
|
|
842
|
+
var x402ExactPermit2ProxyAddress = "0x402085c248EeA27D92E8b30b2C58ed07f9E20001";
|
|
843
|
+
|
|
844
|
+
// node_modules/.pnpm/@x402+evm@2.28.0_typescript@6.0.3/node_modules/@x402/evm/dist/esm/chunk-LQQ3M4DU.mjs
|
|
845
|
+
var DEFAULT_ASSETS = {
|
|
846
|
+
"eip155:8453": [
|
|
847
|
+
{
|
|
848
|
+
asset: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
|
|
849
|
+
name: "USD Coin",
|
|
850
|
+
version: "2",
|
|
851
|
+
decimals: 6,
|
|
852
|
+
symbol: "USDC"
|
|
853
|
+
}
|
|
854
|
+
],
|
|
855
|
+
// Base mainnet USDC
|
|
856
|
+
"eip155:84532": [
|
|
857
|
+
{
|
|
858
|
+
asset: "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
|
|
859
|
+
name: "USDC",
|
|
860
|
+
version: "2",
|
|
861
|
+
decimals: 6,
|
|
862
|
+
symbol: "USDC"
|
|
863
|
+
}
|
|
864
|
+
],
|
|
865
|
+
// Base Sepolia USDC
|
|
866
|
+
"eip155:1": [
|
|
867
|
+
{
|
|
868
|
+
asset: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
|
|
869
|
+
name: "USD Coin",
|
|
870
|
+
version: "2",
|
|
871
|
+
decimals: 6,
|
|
872
|
+
symbol: "USDC"
|
|
873
|
+
}
|
|
874
|
+
],
|
|
875
|
+
// Ethereum mainnet USDC
|
|
876
|
+
"eip155:43114": [
|
|
877
|
+
{
|
|
878
|
+
asset: "0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",
|
|
879
|
+
name: "USD Coin",
|
|
880
|
+
version: "2",
|
|
881
|
+
decimals: 6,
|
|
882
|
+
symbol: "USDC"
|
|
883
|
+
}
|
|
884
|
+
],
|
|
885
|
+
// Avalanche C-Chain USDC
|
|
886
|
+
"eip155:4326": [
|
|
887
|
+
{
|
|
888
|
+
asset: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
|
|
889
|
+
name: "MegaUSD",
|
|
890
|
+
version: "1",
|
|
891
|
+
decimals: 18,
|
|
892
|
+
symbol: "MegaUSD",
|
|
893
|
+
assetTransferMethod: "permit2",
|
|
894
|
+
supportsEip2612: true
|
|
895
|
+
}
|
|
896
|
+
],
|
|
897
|
+
// MegaETH mainnet MegaUSD (no EIP-3009, supports EIP-2612)
|
|
898
|
+
"eip155:143": [
|
|
899
|
+
{
|
|
900
|
+
asset: "0x754704Bc059F8C67012fEd69BC8A327a5aafb603",
|
|
901
|
+
name: "USDC",
|
|
902
|
+
version: "2",
|
|
903
|
+
decimals: 6,
|
|
904
|
+
symbol: "USDC"
|
|
905
|
+
}
|
|
906
|
+
],
|
|
907
|
+
// Monad mainnet USDC
|
|
908
|
+
"eip155:10143": [
|
|
909
|
+
{
|
|
910
|
+
asset: "0x534b2f3A21130d7a60830c2Df862319e593943A3",
|
|
911
|
+
name: "USDC",
|
|
912
|
+
version: "2",
|
|
913
|
+
decimals: 6,
|
|
914
|
+
symbol: "USDC"
|
|
915
|
+
}
|
|
916
|
+
],
|
|
917
|
+
// Monad testnet USDC
|
|
918
|
+
"eip155:988": [
|
|
919
|
+
{
|
|
920
|
+
asset: "0x779Ded0c9e1022225f8E0630b35a9b54bE713736",
|
|
921
|
+
name: "USDT0",
|
|
922
|
+
version: "1",
|
|
923
|
+
decimals: 6,
|
|
924
|
+
symbol: "USDT0"
|
|
925
|
+
}
|
|
926
|
+
],
|
|
927
|
+
// Stable mainnet USDT0
|
|
928
|
+
"eip155:2201": [
|
|
929
|
+
{
|
|
930
|
+
asset: "0x78Cf24370174180738C5B8E352B6D14c83a6c9A9",
|
|
931
|
+
name: "USDT0",
|
|
932
|
+
version: "1",
|
|
933
|
+
decimals: 6,
|
|
934
|
+
symbol: "USDT0"
|
|
935
|
+
}
|
|
936
|
+
],
|
|
937
|
+
// Stable testnet USDT0
|
|
938
|
+
"eip155:137": [
|
|
939
|
+
{
|
|
940
|
+
asset: "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359",
|
|
941
|
+
name: "USD Coin",
|
|
942
|
+
version: "2",
|
|
943
|
+
decimals: 6,
|
|
944
|
+
symbol: "USDC"
|
|
945
|
+
}
|
|
946
|
+
],
|
|
947
|
+
// Polygon mainnet USDC
|
|
948
|
+
"eip155:42161": [
|
|
949
|
+
{
|
|
950
|
+
asset: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
|
951
|
+
name: "USD Coin",
|
|
952
|
+
version: "2",
|
|
953
|
+
decimals: 6,
|
|
954
|
+
symbol: "USDC"
|
|
955
|
+
}
|
|
956
|
+
],
|
|
957
|
+
// Arbitrum One USDC
|
|
958
|
+
"eip155:421614": [
|
|
959
|
+
{
|
|
960
|
+
asset: "0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d",
|
|
961
|
+
name: "USD Coin",
|
|
962
|
+
version: "2",
|
|
963
|
+
decimals: 6,
|
|
964
|
+
symbol: "USDC"
|
|
965
|
+
}
|
|
966
|
+
],
|
|
967
|
+
// Arbitrum Sepolia USDC
|
|
968
|
+
"eip155:31612": [
|
|
969
|
+
{
|
|
970
|
+
asset: "0xdD468A1DDc392dcdbEf6db6e34E89AA338F9F186",
|
|
971
|
+
name: "Mezo USD",
|
|
972
|
+
version: "1",
|
|
973
|
+
decimals: 18,
|
|
974
|
+
symbol: "mUSD",
|
|
975
|
+
assetTransferMethod: "permit2",
|
|
976
|
+
supportsEip2612: true
|
|
977
|
+
}
|
|
978
|
+
],
|
|
979
|
+
// Mezo mainnet mUSD (no EIP-3009, supports EIP-2612)
|
|
980
|
+
"eip155:31611": [
|
|
981
|
+
{
|
|
982
|
+
asset: "0x118917a40FAF1CD7a13dB0Ef56C86De7973Ac503",
|
|
983
|
+
name: "Mezo USD",
|
|
984
|
+
version: "1",
|
|
985
|
+
decimals: 18,
|
|
986
|
+
symbol: "mUSD",
|
|
987
|
+
assetTransferMethod: "permit2",
|
|
988
|
+
supportsEip2612: true
|
|
989
|
+
}
|
|
990
|
+
],
|
|
991
|
+
// Mezo Testnet mUSD (no EIP-3009, supports EIP-2612)
|
|
992
|
+
"eip155:723487": [
|
|
993
|
+
{
|
|
994
|
+
asset: "0x33ad9e4BD16B69B5BFdED37D8B5D9fF9aba014Fb",
|
|
995
|
+
name: "Stable Coin",
|
|
996
|
+
version: "1",
|
|
997
|
+
decimals: 6,
|
|
998
|
+
symbol: "SBC",
|
|
999
|
+
assetTransferMethod: "permit2",
|
|
1000
|
+
supportsEip2612: true
|
|
1001
|
+
}
|
|
1002
|
+
],
|
|
1003
|
+
// Radius Network SBC (no EIP-3009, supports EIP-2612)
|
|
1004
|
+
"eip155:72344": [
|
|
1005
|
+
{
|
|
1006
|
+
asset: "0x33ad9e4BD16B69B5BFdED37D8B5D9fF9aba014Fb",
|
|
1007
|
+
name: "Stable Coin",
|
|
1008
|
+
version: "1",
|
|
1009
|
+
decimals: 6,
|
|
1010
|
+
symbol: "SBC",
|
|
1011
|
+
assetTransferMethod: "permit2",
|
|
1012
|
+
supportsEip2612: true
|
|
1013
|
+
}
|
|
1014
|
+
],
|
|
1015
|
+
// Radius Testnet SBC (no EIP-3009, supports EIP-2612)
|
|
1016
|
+
"eip155:36900": [
|
|
1017
|
+
{
|
|
1018
|
+
asset: "0x9cb8142aEBBcdc60AF7c97Af897A67A8f3CA71C2",
|
|
1019
|
+
name: "USDC.e",
|
|
1020
|
+
version: "2",
|
|
1021
|
+
decimals: 6,
|
|
1022
|
+
symbol: "USDC.e"
|
|
1023
|
+
}
|
|
1024
|
+
],
|
|
1025
|
+
// ADI Chain USDC.e (EIP-3009 supported)
|
|
1026
|
+
"eip155:190415": [
|
|
1027
|
+
{
|
|
1028
|
+
asset: "0x401eCb1D350407f13ba348573E5630B83638E30D",
|
|
1029
|
+
name: "Bridged USDC",
|
|
1030
|
+
version: "2",
|
|
1031
|
+
decimals: 6,
|
|
1032
|
+
symbol: "USDC.e"
|
|
1033
|
+
}
|
|
1034
|
+
],
|
|
1035
|
+
// HPP mainnet USDC.e
|
|
1036
|
+
"eip155:181228": [
|
|
1037
|
+
{
|
|
1038
|
+
asset: "0x401eCb1D350407f13ba348573E5630B83638E30D",
|
|
1039
|
+
name: "Bridged USDC",
|
|
1040
|
+
version: "2",
|
|
1041
|
+
decimals: 6,
|
|
1042
|
+
symbol: "USDC.e"
|
|
1043
|
+
}
|
|
1044
|
+
],
|
|
1045
|
+
// HPP Sepolia USDC.e
|
|
1046
|
+
"eip155:50": [
|
|
1047
|
+
{
|
|
1048
|
+
asset: "0xfA2958CB79b0491CC627c1557F441eF849Ca8eb1",
|
|
1049
|
+
name: "USDC",
|
|
1050
|
+
version: "2",
|
|
1051
|
+
decimals: 6,
|
|
1052
|
+
symbol: "USDC"
|
|
1053
|
+
}
|
|
1054
|
+
],
|
|
1055
|
+
// XDC Network mainnet USDC (Bridged USDC Standard, EIP-3009 supported)
|
|
1056
|
+
"eip155:51": [
|
|
1057
|
+
{
|
|
1058
|
+
asset: "0xb5AB69F7bBada22B28e79C8FFAECe55eF1c771D4",
|
|
1059
|
+
name: "USDC",
|
|
1060
|
+
version: "2",
|
|
1061
|
+
decimals: 6,
|
|
1062
|
+
symbol: "USDC"
|
|
1063
|
+
}
|
|
1064
|
+
],
|
|
1065
|
+
// XDC Apothem testnet USDC (Bridged USDC Standard, EIP-3009 supported)
|
|
1066
|
+
"eip155:38833": [
|
|
1067
|
+
{
|
|
1068
|
+
asset: "0xA5b8BF902b2844dA17d4506cc827F7F1681735E7",
|
|
1069
|
+
name: "USDC",
|
|
1070
|
+
version: "1",
|
|
1071
|
+
decimals: 6,
|
|
1072
|
+
symbol: "USDC",
|
|
1073
|
+
assetTransferMethod: "permit2"
|
|
1074
|
+
}
|
|
1075
|
+
],
|
|
1076
|
+
// Igra mainnet USDC (no EIP-3009, no EIP-2612)
|
|
1077
|
+
"eip155:14": [
|
|
1078
|
+
{
|
|
1079
|
+
asset: "0xe7cd86e13AC4309349F30B3435a9d337750fC82D",
|
|
1080
|
+
name: "USD\u20AE0",
|
|
1081
|
+
version: "1",
|
|
1082
|
+
decimals: 6,
|
|
1083
|
+
symbol: "USDT0"
|
|
1084
|
+
}
|
|
1085
|
+
],
|
|
1086
|
+
// Flare mainnet USD₮0 (EIP-3009 supported)
|
|
1087
|
+
"eip155:42220": [
|
|
1088
|
+
{
|
|
1089
|
+
asset: "0xcebA9300f2b948710d2653dD7B07f33A8B32118C",
|
|
1090
|
+
name: "USDC",
|
|
1091
|
+
version: "2",
|
|
1092
|
+
decimals: 6,
|
|
1093
|
+
symbol: "USDC"
|
|
1094
|
+
},
|
|
1095
|
+
{
|
|
1096
|
+
asset: "0x48065fbBE25f71C9282ddf5e1cD6D6A887483D5e",
|
|
1097
|
+
name: "Tether USD",
|
|
1098
|
+
version: "1",
|
|
1099
|
+
decimals: 6,
|
|
1100
|
+
symbol: "USDT"
|
|
1101
|
+
},
|
|
1102
|
+
{
|
|
1103
|
+
asset: "0xD2ab3C9A02DBBAB236BfEC45D1d755DF4267F771",
|
|
1104
|
+
name: "Tether America USD",
|
|
1105
|
+
version: "1",
|
|
1106
|
+
decimals: 6,
|
|
1107
|
+
symbol: "USAT"
|
|
1108
|
+
}
|
|
1109
|
+
],
|
|
1110
|
+
// Celo mainnet USDC, USDT, USAT (EIP-3009 supported)
|
|
1111
|
+
"eip155:11142220": [
|
|
1112
|
+
{
|
|
1113
|
+
asset: "0x01C5C0122039549AD1493B8220cABEdD739BC44E",
|
|
1114
|
+
name: "USDC",
|
|
1115
|
+
version: "2",
|
|
1116
|
+
decimals: 6,
|
|
1117
|
+
symbol: "USDC"
|
|
1118
|
+
}
|
|
1119
|
+
],
|
|
1120
|
+
// Celo Sepolia testnet USDC (EIP-3009 supported)
|
|
1121
|
+
"eip155:1329": [
|
|
1122
|
+
{
|
|
1123
|
+
asset: "0xe15fC38F6D8c56aF07bbCBe3BAf5708A2Bf42392",
|
|
1124
|
+
name: "USDC",
|
|
1125
|
+
version: "2",
|
|
1126
|
+
decimals: 6,
|
|
1127
|
+
symbol: "USDC"
|
|
1128
|
+
}
|
|
1129
|
+
],
|
|
1130
|
+
// Sei mainnet USDC (EIP-3009 supported)
|
|
1131
|
+
"eip155:1328": [
|
|
1132
|
+
{
|
|
1133
|
+
asset: "0x4fCF1784B31630811181f670Aea7A7bEF803eaED",
|
|
1134
|
+
name: "USDC",
|
|
1135
|
+
version: "2",
|
|
1136
|
+
decimals: 6,
|
|
1137
|
+
symbol: "USDC"
|
|
1138
|
+
}
|
|
1139
|
+
],
|
|
1140
|
+
// Sei testnet USDC (EIP-3009 supported)
|
|
1141
|
+
"eip155:5042": [
|
|
1142
|
+
{
|
|
1143
|
+
asset: "0x3600000000000000000000000000000000000000",
|
|
1144
|
+
name: "USDC",
|
|
1145
|
+
version: "2",
|
|
1146
|
+
decimals: 6,
|
|
1147
|
+
symbol: "USDC"
|
|
1148
|
+
}
|
|
1149
|
+
],
|
|
1150
|
+
// Arc mainnet USDC (EIP-3009 supported)
|
|
1151
|
+
"eip155:5042002": [
|
|
1152
|
+
{
|
|
1153
|
+
asset: "0x3600000000000000000000000000000000000000",
|
|
1154
|
+
name: "USDC",
|
|
1155
|
+
version: "2",
|
|
1156
|
+
decimals: 6,
|
|
1157
|
+
symbol: "USDC"
|
|
1158
|
+
}
|
|
1159
|
+
]
|
|
1160
|
+
// Arc Testnet USDC (EIP-3009 supported)
|
|
1161
|
+
};
|
|
1162
|
+
function resolveNetworkKey(network) {
|
|
1163
|
+
if (network in DEFAULT_ASSETS) {
|
|
1164
|
+
return network;
|
|
1165
|
+
}
|
|
1166
|
+
const chainId = EVM_NETWORK_CHAIN_ID_MAP[network];
|
|
1167
|
+
if (chainId !== void 0) {
|
|
1168
|
+
return `eip155:${chainId}`;
|
|
1169
|
+
}
|
|
1170
|
+
return network;
|
|
1171
|
+
}
|
|
1172
|
+
var findDefaultAsset = (asset, network) => {
|
|
1173
|
+
const key = resolveNetworkKey(network);
|
|
1174
|
+
const assets = DEFAULT_ASSETS[key];
|
|
1175
|
+
if (!assets) {
|
|
1176
|
+
return void 0;
|
|
1177
|
+
}
|
|
1178
|
+
const normalized = asset.toLowerCase();
|
|
1179
|
+
return assets.find((entry) => entry.asset.toLowerCase() === normalized);
|
|
1180
|
+
};
|
|
1181
|
+
|
|
1182
|
+
// node_modules/.pnpm/@x402+evm@2.28.0_typescript@6.0.3/node_modules/@x402/evm/dist/esm/chunk-KPBDMMIU.mjs
|
|
1183
|
+
var SALT_BINDING_TYPEHASH = keccak256(
|
|
1184
|
+
toBytes(
|
|
1185
|
+
"x402AuthCaptureSaltBinding(address receiverAuthorizer,address policy,uint256 saltNonce)"
|
|
1186
|
+
)
|
|
1187
|
+
);
|
|
1188
|
+
var PAYMENT_INFO_TYPEHASH = keccak256(
|
|
1189
|
+
new TextEncoder().encode(
|
|
1190
|
+
"PaymentInfo(address operator,address payer,address receiver,address token,uint120 maxAmount,uint48 preApprovalExpiry,uint48 authorizationExpiry,uint48 refundExpiry,uint16 minFeeBps,uint16 maxFeeBps,address feeReceiver,uint256 salt)"
|
|
1191
|
+
)
|
|
1192
|
+
);
|
|
1193
|
+
|
|
1194
|
+
// node_modules/.pnpm/@x402+evm@2.28.0_typescript@6.0.3/node_modules/@x402/evm/dist/esm/chunk-W6ON4LG2.mjs
|
|
1195
|
+
function buildErc3009DepositNonce(channelId, salt) {
|
|
1196
|
+
return keccak256(
|
|
1197
|
+
encodeAbiParameters([{ type: "bytes32" }, { type: "uint256" }], [channelId, BigInt(salt)])
|
|
1198
|
+
);
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
// node_modules/.pnpm/@x402+evm@2.28.0_typescript@6.0.3/node_modules/@x402/evm/dist/esm/chunk-H25OEB2U.mjs
|
|
1202
|
+
var channelConfigComponents = [
|
|
1203
|
+
{ name: "payer", type: "address" },
|
|
1204
|
+
{ name: "payerAuthorizer", type: "address" },
|
|
1205
|
+
{ name: "receiver", type: "address" },
|
|
1206
|
+
{ name: "receiverAuthorizer", type: "address" },
|
|
1207
|
+
{ name: "token", type: "address" },
|
|
1208
|
+
{ name: "withdrawDelay", type: "uint40" },
|
|
1209
|
+
{ name: "salt", type: "bytes32" }
|
|
1210
|
+
];
|
|
1211
|
+
var voucherClaimComponents = [
|
|
1212
|
+
{
|
|
1213
|
+
name: "voucher",
|
|
1214
|
+
type: "tuple",
|
|
1215
|
+
components: [
|
|
1216
|
+
{
|
|
1217
|
+
name: "channel",
|
|
1218
|
+
type: "tuple",
|
|
1219
|
+
components: channelConfigComponents
|
|
1220
|
+
},
|
|
1221
|
+
{ name: "maxClaimableAmount", type: "uint128" }
|
|
1222
|
+
]
|
|
1223
|
+
},
|
|
1224
|
+
{ name: "signature", type: "bytes" },
|
|
1225
|
+
{ name: "totalClaimed", type: "uint128" }
|
|
1226
|
+
];
|
|
1227
|
+
var batchSettlementABI = [
|
|
1228
|
+
{
|
|
1229
|
+
type: "function",
|
|
1230
|
+
name: "multicall",
|
|
1231
|
+
inputs: [{ name: "data", type: "bytes[]" }],
|
|
1232
|
+
outputs: [{ name: "results", type: "bytes[]" }],
|
|
1233
|
+
stateMutability: "nonpayable"
|
|
1234
|
+
},
|
|
1235
|
+
{
|
|
1236
|
+
type: "function",
|
|
1237
|
+
name: "deposit",
|
|
1238
|
+
inputs: [
|
|
1239
|
+
{ name: "config", type: "tuple", components: channelConfigComponents },
|
|
1240
|
+
{ name: "amount", type: "uint128" },
|
|
1241
|
+
{ name: "collector", type: "address" },
|
|
1242
|
+
{ name: "collectorData", type: "bytes" }
|
|
1243
|
+
],
|
|
1244
|
+
outputs: [],
|
|
1245
|
+
stateMutability: "nonpayable"
|
|
1246
|
+
},
|
|
1247
|
+
{
|
|
1248
|
+
type: "function",
|
|
1249
|
+
name: "claim",
|
|
1250
|
+
inputs: [{ name: "voucherClaims", type: "tuple[]", components: voucherClaimComponents }],
|
|
1251
|
+
outputs: [],
|
|
1252
|
+
stateMutability: "nonpayable"
|
|
1253
|
+
},
|
|
1254
|
+
{
|
|
1255
|
+
type: "function",
|
|
1256
|
+
name: "claimWithSignature",
|
|
1257
|
+
inputs: [
|
|
1258
|
+
{ name: "voucherClaims", type: "tuple[]", components: voucherClaimComponents },
|
|
1259
|
+
{ name: "authorizerSignature", type: "bytes" }
|
|
1260
|
+
],
|
|
1261
|
+
outputs: [],
|
|
1262
|
+
stateMutability: "nonpayable"
|
|
1263
|
+
},
|
|
1264
|
+
{
|
|
1265
|
+
type: "function",
|
|
1266
|
+
name: "settle",
|
|
1267
|
+
inputs: [
|
|
1268
|
+
{ name: "receiver", type: "address" },
|
|
1269
|
+
{ name: "token", type: "address" }
|
|
1270
|
+
],
|
|
1271
|
+
outputs: [],
|
|
1272
|
+
stateMutability: "nonpayable"
|
|
1273
|
+
},
|
|
1274
|
+
{
|
|
1275
|
+
type: "function",
|
|
1276
|
+
name: "initiateWithdraw",
|
|
1277
|
+
inputs: [
|
|
1278
|
+
{ name: "config", type: "tuple", components: channelConfigComponents },
|
|
1279
|
+
{ name: "amount", type: "uint128" }
|
|
1280
|
+
],
|
|
1281
|
+
outputs: [],
|
|
1282
|
+
stateMutability: "nonpayable"
|
|
1283
|
+
},
|
|
1284
|
+
{
|
|
1285
|
+
type: "function",
|
|
1286
|
+
name: "finalizeWithdraw",
|
|
1287
|
+
inputs: [{ name: "config", type: "tuple", components: channelConfigComponents }],
|
|
1288
|
+
outputs: [],
|
|
1289
|
+
stateMutability: "nonpayable"
|
|
1290
|
+
},
|
|
1291
|
+
{
|
|
1292
|
+
type: "function",
|
|
1293
|
+
name: "refund",
|
|
1294
|
+
inputs: [
|
|
1295
|
+
{ name: "config", type: "tuple", components: channelConfigComponents },
|
|
1296
|
+
{ name: "amount", type: "uint128" }
|
|
1297
|
+
],
|
|
1298
|
+
outputs: [],
|
|
1299
|
+
stateMutability: "nonpayable"
|
|
1300
|
+
},
|
|
1301
|
+
{
|
|
1302
|
+
type: "function",
|
|
1303
|
+
name: "refundWithSignature",
|
|
1304
|
+
inputs: [
|
|
1305
|
+
{ name: "config", type: "tuple", components: channelConfigComponents },
|
|
1306
|
+
{ name: "amount", type: "uint128" },
|
|
1307
|
+
{ name: "nonce", type: "uint256" },
|
|
1308
|
+
{ name: "receiverAuthorizerSignature", type: "bytes" }
|
|
1309
|
+
],
|
|
1310
|
+
outputs: [],
|
|
1311
|
+
stateMutability: "nonpayable"
|
|
1312
|
+
},
|
|
1313
|
+
{
|
|
1314
|
+
type: "function",
|
|
1315
|
+
name: "getChannelId",
|
|
1316
|
+
inputs: [{ name: "config", type: "tuple", components: channelConfigComponents }],
|
|
1317
|
+
outputs: [{ name: "", type: "bytes32" }],
|
|
1318
|
+
stateMutability: "view"
|
|
1319
|
+
},
|
|
1320
|
+
{
|
|
1321
|
+
type: "function",
|
|
1322
|
+
name: "CHANNEL_CONFIG_TYPEHASH",
|
|
1323
|
+
inputs: [],
|
|
1324
|
+
outputs: [{ name: "", type: "bytes32" }],
|
|
1325
|
+
stateMutability: "view"
|
|
1326
|
+
},
|
|
1327
|
+
{
|
|
1328
|
+
type: "function",
|
|
1329
|
+
name: "channels",
|
|
1330
|
+
inputs: [{ name: "channelId", type: "bytes32" }],
|
|
1331
|
+
outputs: [
|
|
1332
|
+
{ name: "balance", type: "uint128" },
|
|
1333
|
+
{ name: "totalClaimed", type: "uint128" }
|
|
1334
|
+
],
|
|
1335
|
+
stateMutability: "view"
|
|
1336
|
+
},
|
|
1337
|
+
{
|
|
1338
|
+
type: "function",
|
|
1339
|
+
name: "pendingWithdrawals",
|
|
1340
|
+
inputs: [{ name: "channelId", type: "bytes32" }],
|
|
1341
|
+
outputs: [
|
|
1342
|
+
{ name: "amount", type: "uint128" },
|
|
1343
|
+
{ name: "initiatedAt", type: "uint40" }
|
|
1344
|
+
],
|
|
1345
|
+
stateMutability: "view"
|
|
1346
|
+
},
|
|
1347
|
+
{
|
|
1348
|
+
type: "function",
|
|
1349
|
+
name: "receivers",
|
|
1350
|
+
inputs: [
|
|
1351
|
+
{ name: "receiver", type: "address" },
|
|
1352
|
+
{ name: "token", type: "address" }
|
|
1353
|
+
],
|
|
1354
|
+
outputs: [
|
|
1355
|
+
{ name: "totalClaimed", type: "uint128" },
|
|
1356
|
+
{ name: "totalSettled", type: "uint128" }
|
|
1357
|
+
],
|
|
1358
|
+
stateMutability: "view"
|
|
1359
|
+
},
|
|
1360
|
+
{
|
|
1361
|
+
type: "function",
|
|
1362
|
+
name: "getVoucherDigest",
|
|
1363
|
+
inputs: [
|
|
1364
|
+
{ name: "channelId", type: "bytes32" },
|
|
1365
|
+
{ name: "maxClaimableAmount", type: "uint128" }
|
|
1366
|
+
],
|
|
1367
|
+
outputs: [{ name: "", type: "bytes32" }],
|
|
1368
|
+
stateMutability: "view"
|
|
1369
|
+
},
|
|
1370
|
+
{
|
|
1371
|
+
type: "function",
|
|
1372
|
+
name: "getRefundDigest",
|
|
1373
|
+
inputs: [
|
|
1374
|
+
{ name: "channelId", type: "bytes32" },
|
|
1375
|
+
{ name: "nonce", type: "uint256" },
|
|
1376
|
+
{ name: "amount", type: "uint128" }
|
|
1377
|
+
],
|
|
1378
|
+
outputs: [{ name: "", type: "bytes32" }],
|
|
1379
|
+
stateMutability: "view"
|
|
1380
|
+
},
|
|
1381
|
+
{
|
|
1382
|
+
type: "function",
|
|
1383
|
+
name: "refundNonce",
|
|
1384
|
+
inputs: [{ name: "channelId", type: "bytes32" }],
|
|
1385
|
+
outputs: [{ name: "", type: "uint256" }],
|
|
1386
|
+
stateMutability: "view"
|
|
1387
|
+
},
|
|
1388
|
+
{
|
|
1389
|
+
type: "function",
|
|
1390
|
+
name: "getClaimBatchDigest",
|
|
1391
|
+
inputs: [{ name: "voucherClaims", type: "tuple[]", components: voucherClaimComponents }],
|
|
1392
|
+
outputs: [{ name: "", type: "bytes32" }],
|
|
1393
|
+
stateMutability: "view"
|
|
1394
|
+
},
|
|
1395
|
+
{
|
|
1396
|
+
type: "event",
|
|
1397
|
+
name: "Settled",
|
|
1398
|
+
inputs: [
|
|
1399
|
+
{ name: "receiver", type: "address", indexed: true },
|
|
1400
|
+
{ name: "token", type: "address", indexed: true },
|
|
1401
|
+
{ name: "sender", type: "address", indexed: true },
|
|
1402
|
+
{ name: "amount", type: "uint128", indexed: false }
|
|
1403
|
+
],
|
|
1404
|
+
anonymous: false
|
|
1405
|
+
}
|
|
1406
|
+
];
|
|
1407
|
+
|
|
1408
|
+
// node_modules/.pnpm/@x402+evm@2.28.0_typescript@6.0.3/node_modules/@x402/evm/dist/esm/chunk-U4HCGTLU.mjs
|
|
1409
|
+
function isObject(payload) {
|
|
1410
|
+
return typeof payload === "object" && payload !== null;
|
|
1411
|
+
}
|
|
1412
|
+
function isVoucherFields(payload) {
|
|
1413
|
+
return isObject(payload) && "channelId" in payload && "maxClaimableAmount" in payload && "signature" in payload;
|
|
1414
|
+
}
|
|
1415
|
+
function isBatchSettlementDepositPayload(payload) {
|
|
1416
|
+
return isObject(payload) && payload.type === "deposit" && "channelConfig" in payload && isVoucherFields(payload.voucher) && isObject(payload.deposit) && typeof payload.deposit.amount === "string" && isObject(payload.deposit.authorization);
|
|
1417
|
+
}
|
|
1418
|
+
function isBatchSettlementRefundPayload(payload) {
|
|
1419
|
+
return isObject(payload) && payload.type === "refund" && "channelConfig" in payload && isVoucherFields(payload.voucher);
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1422
|
+
// node_modules/.pnpm/@x402+evm@2.28.0_typescript@6.0.3/node_modules/@x402/evm/dist/esm/chunk-X4BYNI7R.mjs
|
|
1423
|
+
var EIP2612_GAS_SPONSORING_KEY = "eip2612GasSponsoring";
|
|
1424
|
+
var ERC20_APPROVAL_GAS_SPONSORING_KEY = "erc20ApprovalGasSponsoring";
|
|
1425
|
+
var ERC20_APPROVAL_GAS_SPONSORING_VERSION = "1";
|
|
1426
|
+
async function signEip2612Permit(signer, tokenAddress, tokenName, tokenVersion, chainId, deadline, permittedAmount) {
|
|
1427
|
+
const owner = signer.address;
|
|
1428
|
+
const spender = getAddress(PERMIT2_ADDRESS);
|
|
1429
|
+
const nonce = await signer.readContract({
|
|
1430
|
+
address: tokenAddress,
|
|
1431
|
+
abi: eip2612NoncesAbi,
|
|
1432
|
+
functionName: "nonces",
|
|
1433
|
+
args: [owner]
|
|
1434
|
+
});
|
|
1435
|
+
const domain = {
|
|
1436
|
+
name: tokenName,
|
|
1437
|
+
version: tokenVersion,
|
|
1438
|
+
chainId,
|
|
1439
|
+
verifyingContract: tokenAddress
|
|
1440
|
+
};
|
|
1441
|
+
const approvalAmount = BigInt(permittedAmount);
|
|
1442
|
+
const message = {
|
|
1443
|
+
owner,
|
|
1444
|
+
spender,
|
|
1445
|
+
value: approvalAmount,
|
|
1446
|
+
nonce,
|
|
1447
|
+
deadline: BigInt(deadline)
|
|
1448
|
+
};
|
|
1449
|
+
const signature = await signer.signTypedData({
|
|
1450
|
+
domain,
|
|
1451
|
+
types: eip2612PermitTypes,
|
|
1452
|
+
primaryType: "Permit",
|
|
1453
|
+
message
|
|
1454
|
+
});
|
|
1455
|
+
return {
|
|
1456
|
+
from: owner,
|
|
1457
|
+
asset: tokenAddress,
|
|
1458
|
+
spender,
|
|
1459
|
+
amount: approvalAmount.toString(),
|
|
1460
|
+
nonce: nonce.toString(),
|
|
1461
|
+
deadline,
|
|
1462
|
+
signature,
|
|
1463
|
+
version: "1"
|
|
1464
|
+
};
|
|
1465
|
+
}
|
|
1466
|
+
async function signErc20ApprovalTransaction(signer, tokenAddress, chainId) {
|
|
1467
|
+
const from = signer.address;
|
|
1468
|
+
const spender = getAddress(PERMIT2_ADDRESS);
|
|
1469
|
+
const data = encodeFunctionData({
|
|
1470
|
+
abi: erc20ApproveAbi,
|
|
1471
|
+
functionName: "approve",
|
|
1472
|
+
args: [spender, maxUint256]
|
|
1473
|
+
});
|
|
1474
|
+
const nonce = await signer.getTransactionCount({ address: from });
|
|
1475
|
+
let maxFeePerGas;
|
|
1476
|
+
let maxPriorityFeePerGas;
|
|
1477
|
+
try {
|
|
1478
|
+
const fees = await signer.estimateFeesPerGas?.();
|
|
1479
|
+
if (!fees) {
|
|
1480
|
+
throw new Error("no fee estimates available");
|
|
1481
|
+
}
|
|
1482
|
+
maxFeePerGas = fees.maxFeePerGas;
|
|
1483
|
+
maxPriorityFeePerGas = fees.maxPriorityFeePerGas;
|
|
1484
|
+
} catch {
|
|
1485
|
+
maxFeePerGas = DEFAULT_MAX_FEE_PER_GAS;
|
|
1486
|
+
maxPriorityFeePerGas = DEFAULT_MAX_PRIORITY_FEE_PER_GAS;
|
|
1487
|
+
}
|
|
1488
|
+
const signedTransaction = await signer.signTransaction({
|
|
1489
|
+
to: tokenAddress,
|
|
1490
|
+
data,
|
|
1491
|
+
nonce,
|
|
1492
|
+
gas: ERC20_APPROVE_GAS_LIMIT,
|
|
1493
|
+
maxFeePerGas,
|
|
1494
|
+
maxPriorityFeePerGas,
|
|
1495
|
+
chainId
|
|
1496
|
+
});
|
|
1497
|
+
return {
|
|
1498
|
+
from,
|
|
1499
|
+
asset: tokenAddress,
|
|
1500
|
+
spender,
|
|
1501
|
+
amount: maxUint256.toString(),
|
|
1502
|
+
signedTransaction,
|
|
1503
|
+
version: ERC20_APPROVAL_GAS_SPONSORING_VERSION
|
|
1504
|
+
};
|
|
1505
|
+
}
|
|
1506
|
+
var rpcClientCache = /* @__PURE__ */ new Map();
|
|
1507
|
+
function isConfigByChainId(options) {
|
|
1508
|
+
const keys = Object.keys(options);
|
|
1509
|
+
return keys.length > 0 && keys.every((key) => /^\d+$/.test(key));
|
|
1510
|
+
}
|
|
1511
|
+
function getRpcClient(rpcUrl) {
|
|
1512
|
+
const existing = rpcClientCache.get(rpcUrl);
|
|
1513
|
+
if (existing) {
|
|
1514
|
+
return existing;
|
|
1515
|
+
}
|
|
1516
|
+
const client = createPublicClient({
|
|
1517
|
+
transport: http(rpcUrl)
|
|
1518
|
+
});
|
|
1519
|
+
rpcClientCache.set(rpcUrl, client);
|
|
1520
|
+
return client;
|
|
1521
|
+
}
|
|
1522
|
+
function resolveRpcUrl(network, options) {
|
|
1523
|
+
if (!options) {
|
|
1524
|
+
return void 0;
|
|
1525
|
+
}
|
|
1526
|
+
if (isConfigByChainId(options)) {
|
|
1527
|
+
const chainId = getEvmChainId(network);
|
|
1528
|
+
const optionsByChainId = options;
|
|
1529
|
+
return optionsByChainId[chainId]?.rpcUrl;
|
|
1530
|
+
}
|
|
1531
|
+
return options.rpcUrl;
|
|
1532
|
+
}
|
|
1533
|
+
function resolveExtensionRpcCapabilities(network, signer, options) {
|
|
1534
|
+
const capabilities = {
|
|
1535
|
+
signTransaction: signer.signTransaction,
|
|
1536
|
+
readContract: signer.readContract,
|
|
1537
|
+
getTransactionCount: signer.getTransactionCount,
|
|
1538
|
+
estimateFeesPerGas: signer.estimateFeesPerGas
|
|
1539
|
+
};
|
|
1540
|
+
const needsRpcBackfill = !capabilities.readContract || !capabilities.getTransactionCount || !capabilities.estimateFeesPerGas;
|
|
1541
|
+
if (!needsRpcBackfill) {
|
|
1542
|
+
return capabilities;
|
|
1543
|
+
}
|
|
1544
|
+
const rpcUrl = resolveRpcUrl(network, options);
|
|
1545
|
+
if (!rpcUrl) {
|
|
1546
|
+
return capabilities;
|
|
1547
|
+
}
|
|
1548
|
+
const rpcClient = getRpcClient(rpcUrl);
|
|
1549
|
+
if (!capabilities.readContract) {
|
|
1550
|
+
capabilities.readContract = (args) => rpcClient.readContract(args);
|
|
1551
|
+
}
|
|
1552
|
+
if (!capabilities.getTransactionCount) {
|
|
1553
|
+
capabilities.getTransactionCount = async (args) => rpcClient.getTransactionCount({ address: args.address });
|
|
1554
|
+
}
|
|
1555
|
+
if (!capabilities.estimateFeesPerGas) {
|
|
1556
|
+
capabilities.estimateFeesPerGas = async () => rpcClient.estimateFeesPerGas();
|
|
1557
|
+
}
|
|
1558
|
+
return capabilities;
|
|
1559
|
+
}
|
|
1560
|
+
async function trySignEip2612PermitExtension(signer, options, requirements, result, context, approvalAmount) {
|
|
1561
|
+
const capabilities = resolveExtensionRpcCapabilities(requirements.network, signer, options);
|
|
1562
|
+
if (!capabilities.readContract) {
|
|
1563
|
+
return void 0;
|
|
1564
|
+
}
|
|
1565
|
+
if (!context?.extensions?.[EIP2612_GAS_SPONSORING_KEY]) {
|
|
1566
|
+
return void 0;
|
|
1567
|
+
}
|
|
1568
|
+
const tokenName = requirements.extra?.name;
|
|
1569
|
+
const tokenVersion = requirements.extra?.version;
|
|
1570
|
+
if (!tokenName || !tokenVersion) {
|
|
1571
|
+
return void 0;
|
|
1572
|
+
}
|
|
1573
|
+
const chainId = getEvmChainId(requirements.network);
|
|
1574
|
+
const tokenAddress = getAddress(requirements.asset);
|
|
1575
|
+
const requiredAllowance = approvalAmount ?? requirements.amount;
|
|
1576
|
+
try {
|
|
1577
|
+
const allowance = await capabilities.readContract({
|
|
1578
|
+
address: tokenAddress,
|
|
1579
|
+
abi: erc20AllowanceAbi,
|
|
1580
|
+
functionName: "allowance",
|
|
1581
|
+
args: [signer.address, PERMIT2_ADDRESS]
|
|
1582
|
+
});
|
|
1583
|
+
if (allowance >= BigInt(requiredAllowance)) {
|
|
1584
|
+
return void 0;
|
|
1585
|
+
}
|
|
1586
|
+
} catch {
|
|
1587
|
+
}
|
|
1588
|
+
const permit2Auth = result.payload?.permit2Authorization;
|
|
1589
|
+
const deadline = permit2Auth?.deadline ?? Math.floor(Date.now() / 1e3 + requirements.maxTimeoutSeconds).toString();
|
|
1590
|
+
const info = await signEip2612Permit(
|
|
1591
|
+
{
|
|
1592
|
+
address: signer.address,
|
|
1593
|
+
signTypedData: (msg) => signer.signTypedData(msg),
|
|
1594
|
+
readContract: capabilities.readContract
|
|
1595
|
+
},
|
|
1596
|
+
tokenAddress,
|
|
1597
|
+
tokenName,
|
|
1598
|
+
tokenVersion,
|
|
1599
|
+
chainId,
|
|
1600
|
+
deadline,
|
|
1601
|
+
requiredAllowance
|
|
1602
|
+
);
|
|
1603
|
+
return {
|
|
1604
|
+
[EIP2612_GAS_SPONSORING_KEY]: { info }
|
|
1605
|
+
};
|
|
1606
|
+
}
|
|
1607
|
+
async function trySignErc20ApprovalExtension(signer, options, requirements, context, approvalAmount) {
|
|
1608
|
+
const capabilities = resolveExtensionRpcCapabilities(requirements.network, signer, options);
|
|
1609
|
+
if (!capabilities.readContract) {
|
|
1610
|
+
return void 0;
|
|
1611
|
+
}
|
|
1612
|
+
if (!context?.extensions?.[ERC20_APPROVAL_GAS_SPONSORING_KEY]) {
|
|
1613
|
+
return void 0;
|
|
1614
|
+
}
|
|
1615
|
+
if (!capabilities.signTransaction || !capabilities.getTransactionCount) {
|
|
1616
|
+
return void 0;
|
|
1617
|
+
}
|
|
1618
|
+
const chainId = getEvmChainId(requirements.network);
|
|
1619
|
+
const tokenAddress = getAddress(requirements.asset);
|
|
1620
|
+
const requiredAllowance = approvalAmount ?? requirements.amount;
|
|
1621
|
+
try {
|
|
1622
|
+
const allowance = await capabilities.readContract({
|
|
1623
|
+
address: tokenAddress,
|
|
1624
|
+
abi: erc20AllowanceAbi,
|
|
1625
|
+
functionName: "allowance",
|
|
1626
|
+
args: [signer.address, PERMIT2_ADDRESS]
|
|
1627
|
+
});
|
|
1628
|
+
if (allowance >= BigInt(requiredAllowance)) {
|
|
1629
|
+
return void 0;
|
|
1630
|
+
}
|
|
1631
|
+
} catch {
|
|
1632
|
+
}
|
|
1633
|
+
const info = await signErc20ApprovalTransaction(
|
|
1634
|
+
{
|
|
1635
|
+
address: signer.address,
|
|
1636
|
+
signTransaction: capabilities.signTransaction,
|
|
1637
|
+
getTransactionCount: capabilities.getTransactionCount,
|
|
1638
|
+
estimateFeesPerGas: capabilities.estimateFeesPerGas
|
|
1639
|
+
},
|
|
1640
|
+
tokenAddress,
|
|
1641
|
+
chainId
|
|
1642
|
+
);
|
|
1643
|
+
return {
|
|
1644
|
+
[ERC20_APPROVAL_GAS_SPONSORING_KEY]: { info }
|
|
1645
|
+
};
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
// node_modules/.pnpm/@x402+evm@2.28.0_typescript@6.0.3/node_modules/@x402/evm/dist/esm/chunk-QJHO4KEV.mjs
|
|
1649
|
+
async function signVoucher(signer, channelId, maxClaimableAmount, network) {
|
|
1650
|
+
const chainId = getEvmChainId(network);
|
|
1651
|
+
const signature = await signer.signTypedData({
|
|
1652
|
+
domain: getBatchSettlementEip712Domain(chainId),
|
|
1653
|
+
types: voucherTypes,
|
|
1654
|
+
primaryType: "Voucher",
|
|
1655
|
+
message: {
|
|
1656
|
+
channelId,
|
|
1657
|
+
maxClaimableAmount: BigInt(maxClaimableAmount)
|
|
1658
|
+
}
|
|
1659
|
+
});
|
|
1660
|
+
return {
|
|
1661
|
+
channelId,
|
|
1662
|
+
maxClaimableAmount,
|
|
1663
|
+
signature
|
|
1664
|
+
};
|
|
1665
|
+
}
|
|
1666
|
+
async function createBatchSettlementEIP3009DepositPayload(signer, x402Version2, paymentRequirements, channelConfig, depositAmount, maxClaimableAmount, voucherSigner) {
|
|
1667
|
+
const salt = createNonce();
|
|
1668
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
1669
|
+
const chainId = getEvmChainId(paymentRequirements.network);
|
|
1670
|
+
if (!paymentRequirements.extra?.name || !paymentRequirements.extra?.version) {
|
|
1671
|
+
throw new Error(
|
|
1672
|
+
`EIP-712 domain parameters (name, version) are required in payment requirements for asset ${paymentRequirements.asset}`
|
|
1673
|
+
);
|
|
1674
|
+
}
|
|
1675
|
+
const { name, version } = paymentRequirements.extra;
|
|
1676
|
+
const channelId = computeChannelId(channelConfig, paymentRequirements.network);
|
|
1677
|
+
const erc3009Nonce = buildErc3009DepositNonce(channelId, salt);
|
|
1678
|
+
const signature = await signer.signTypedData({
|
|
1679
|
+
domain: {
|
|
1680
|
+
name,
|
|
1681
|
+
version,
|
|
1682
|
+
chainId,
|
|
1683
|
+
verifyingContract: getAddress(paymentRequirements.asset)
|
|
1684
|
+
},
|
|
1685
|
+
types: receiveAuthorizationTypes,
|
|
1686
|
+
primaryType: "ReceiveWithAuthorization",
|
|
1687
|
+
message: {
|
|
1688
|
+
from: getAddress(signer.address),
|
|
1689
|
+
to: getAddress(ERC3009_DEPOSIT_COLLECTOR_ADDRESS),
|
|
1690
|
+
value: BigInt(depositAmount),
|
|
1691
|
+
validAfter: BigInt(0),
|
|
1692
|
+
validBefore: BigInt(now + paymentRequirements.maxTimeoutSeconds),
|
|
1693
|
+
nonce: erc3009Nonce
|
|
1694
|
+
}
|
|
1695
|
+
});
|
|
1696
|
+
const vSigner = voucherSigner ?? signer;
|
|
1697
|
+
const voucher = await signVoucher(
|
|
1698
|
+
vSigner,
|
|
1699
|
+
channelId,
|
|
1700
|
+
maxClaimableAmount,
|
|
1701
|
+
paymentRequirements.network
|
|
1702
|
+
);
|
|
1703
|
+
const payload = {
|
|
1704
|
+
type: "deposit",
|
|
1705
|
+
channelConfig,
|
|
1706
|
+
voucher,
|
|
1707
|
+
deposit: {
|
|
1708
|
+
amount: depositAmount,
|
|
1709
|
+
authorization: {
|
|
1710
|
+
erc3009Authorization: {
|
|
1711
|
+
validAfter: "0",
|
|
1712
|
+
validBefore: (now + paymentRequirements.maxTimeoutSeconds).toString(),
|
|
1713
|
+
salt,
|
|
1714
|
+
signature
|
|
1715
|
+
}
|
|
1716
|
+
}
|
|
1717
|
+
}
|
|
1718
|
+
};
|
|
1719
|
+
return {
|
|
1720
|
+
x402Version: x402Version2,
|
|
1721
|
+
payload
|
|
1722
|
+
};
|
|
1723
|
+
}
|
|
1724
|
+
var InMemoryClientChannelStorage = class {
|
|
1725
|
+
constructor() {
|
|
1726
|
+
this.channels = /* @__PURE__ */ new Map();
|
|
1727
|
+
}
|
|
1728
|
+
/**
|
|
1729
|
+
* Returns the channel record for `key` if present.
|
|
1730
|
+
*
|
|
1731
|
+
* @param key - Channel storage key (a canonical `bytes32` channel id).
|
|
1732
|
+
* @returns Persisted context or undefined.
|
|
1733
|
+
*/
|
|
1734
|
+
async get(key) {
|
|
1735
|
+
return this.channels.get(normalizeChannelId(key));
|
|
1736
|
+
}
|
|
1737
|
+
/**
|
|
1738
|
+
* Stores or replaces the channel record for `key`.
|
|
1739
|
+
*
|
|
1740
|
+
* @param key - Channel storage key (a canonical `bytes32` channel id).
|
|
1741
|
+
* @param context - Channel fields to persist.
|
|
1742
|
+
* @returns Resolves when stored.
|
|
1743
|
+
*/
|
|
1744
|
+
async set(key, context) {
|
|
1745
|
+
this.channels.set(normalizeChannelId(key), context);
|
|
1746
|
+
}
|
|
1747
|
+
/**
|
|
1748
|
+
* Removes the channel record for `key` if it exists.
|
|
1749
|
+
*
|
|
1750
|
+
* @param key - Channel storage key (a canonical `bytes32` channel id).
|
|
1751
|
+
* @returns Resolves when removed.
|
|
1752
|
+
*/
|
|
1753
|
+
async delete(key) {
|
|
1754
|
+
this.channels.delete(normalizeChannelId(key));
|
|
1755
|
+
}
|
|
1756
|
+
};
|
|
1757
|
+
var DEFAULT_SALT = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
|
1758
|
+
function isBatchSettlementEvmSchemeOptions(o) {
|
|
1759
|
+
return o !== void 0 && typeof o === "object" && ("storage" in o || "depositPolicy" in o || "depositStrategy" in o || "salt" in o || "payerAuthorizer" in o || "rpcUrl" in o || "voucherSigner" in o);
|
|
1760
|
+
}
|
|
1761
|
+
function resolveClientOptions(second) {
|
|
1762
|
+
if (second === void 0) {
|
|
1763
|
+
return { storage: new InMemoryClientChannelStorage(), salt: DEFAULT_SALT };
|
|
1764
|
+
}
|
|
1765
|
+
if (isBatchSettlementEvmSchemeOptions(second)) {
|
|
1766
|
+
return {
|
|
1767
|
+
storage: second.storage ?? new InMemoryClientChannelStorage(),
|
|
1768
|
+
depositPolicy: second.depositPolicy,
|
|
1769
|
+
depositStrategy: second.depositStrategy,
|
|
1770
|
+
salt: second.salt ?? DEFAULT_SALT,
|
|
1771
|
+
payerAuthorizer: second.payerAuthorizer,
|
|
1772
|
+
voucherSigner: second.voucherSigner,
|
|
1773
|
+
extensionRpcOptions: second.rpcUrl ? { rpcUrl: second.rpcUrl } : void 0
|
|
1774
|
+
};
|
|
1775
|
+
}
|
|
1776
|
+
return {
|
|
1777
|
+
storage: new InMemoryClientChannelStorage(),
|
|
1778
|
+
depositPolicy: second,
|
|
1779
|
+
salt: DEFAULT_SALT
|
|
1780
|
+
};
|
|
1781
|
+
}
|
|
1782
|
+
function validateDepositPolicy(policy) {
|
|
1783
|
+
if (!policy) return;
|
|
1784
|
+
const m = policy.depositMultiplier;
|
|
1785
|
+
if (m !== void 0 && (!Number.isInteger(m) || m < 3)) {
|
|
1786
|
+
throw new Error("depositMultiplier must be an integer >= 3");
|
|
1787
|
+
}
|
|
1788
|
+
}
|
|
1789
|
+
function parseAnnouncedMinDeposit(value, requestAmount) {
|
|
1790
|
+
if (typeof value !== "string" || !/^\d+$/.test(value)) {
|
|
1791
|
+
return void 0;
|
|
1792
|
+
}
|
|
1793
|
+
const parsed = BigInt(value);
|
|
1794
|
+
if (parsed <= 0n || parsed < requestAmount) {
|
|
1795
|
+
return void 0;
|
|
1796
|
+
}
|
|
1797
|
+
return parsed;
|
|
1798
|
+
}
|
|
1799
|
+
function maxDepositFromSpendCap(maxAmountPerPayment, depositMultiplier = 5) {
|
|
1800
|
+
if (typeof maxAmountPerPayment !== "string" || !/^\d+$/.test(maxAmountPerPayment) || BigInt(maxAmountPerPayment) <= 0n) {
|
|
1801
|
+
return void 0;
|
|
1802
|
+
}
|
|
1803
|
+
return BigInt(maxAmountPerPayment) * BigInt(depositMultiplier);
|
|
1804
|
+
}
|
|
1805
|
+
function applyMaxDeposit(deposit, needed, maxDeposit) {
|
|
1806
|
+
if (maxDeposit === void 0) {
|
|
1807
|
+
return deposit.toString();
|
|
1808
|
+
}
|
|
1809
|
+
if (needed > maxDeposit) {
|
|
1810
|
+
throw new Error(
|
|
1811
|
+
`Required deposit ${needed.toString()} exceeds depositMultiplier \xD7 spendControls.maxAmountPerPayment (${maxDeposit.toString()}). Raise maxAmountPerPayment or depositMultiplier.`
|
|
1812
|
+
);
|
|
1813
|
+
}
|
|
1814
|
+
return (deposit > maxDeposit ? maxDeposit : deposit).toString();
|
|
1815
|
+
}
|
|
1816
|
+
function depositAmountForRequest(policy, requestAmount, needed, extra, maxDeposit) {
|
|
1817
|
+
const announced = parseAnnouncedMinDeposit(extra?.minDeposit, requestAmount);
|
|
1818
|
+
const multiplier = BigInt(policy?.depositMultiplier ?? 5);
|
|
1819
|
+
const target = announced ?? multiplier * requestAmount;
|
|
1820
|
+
const deposit = needed > target ? needed : target;
|
|
1821
|
+
return applyMaxDeposit(deposit, needed, maxDeposit);
|
|
1822
|
+
}
|
|
1823
|
+
function buildChannelConfig(deps, paymentRequirements) {
|
|
1824
|
+
const extra = paymentRequirements.extra;
|
|
1825
|
+
const receiverAuthorizer = extra?.receiverAuthorizer;
|
|
1826
|
+
if (!receiverAuthorizer || getAddress(receiverAuthorizer) === "0x0000000000000000000000000000000000000000") {
|
|
1827
|
+
throw new Error("Payment requirements must include a non-zero extra.receiverAuthorizer");
|
|
1828
|
+
}
|
|
1829
|
+
return {
|
|
1830
|
+
payer: deps.signer.address,
|
|
1831
|
+
payerAuthorizer: getAddress(
|
|
1832
|
+
deps.payerAuthorizer ?? deps.voucherSigner?.address ?? deps.signer.address
|
|
1833
|
+
),
|
|
1834
|
+
receiver: paymentRequirements.payTo,
|
|
1835
|
+
receiverAuthorizer: getAddress(receiverAuthorizer),
|
|
1836
|
+
token: paymentRequirements.asset,
|
|
1837
|
+
withdrawDelay: typeof extra?.withdrawDelay === "number" ? extra.withdrawDelay : MIN_WITHDRAW_DELAY,
|
|
1838
|
+
salt: deps.salt
|
|
1839
|
+
};
|
|
1840
|
+
}
|
|
1841
|
+
async function updateChannelFromSettle(storage, input) {
|
|
1842
|
+
const { server, local } = input;
|
|
1843
|
+
let chargedAmount = 0n;
|
|
1844
|
+
if (server.chargedAmount !== void 0) {
|
|
1845
|
+
if (!/^\d+$/.test(server.chargedAmount)) {
|
|
1846
|
+
throw new Error("invalid chargedAmount: not a non-negative integer");
|
|
1847
|
+
}
|
|
1848
|
+
chargedAmount = BigInt(server.chargedAmount);
|
|
1849
|
+
}
|
|
1850
|
+
const requestAmount = BigInt(local.requestAmount);
|
|
1851
|
+
if (chargedAmount > requestAmount) {
|
|
1852
|
+
throw new Error("settle response chargedAmount exceeds PaymentRequirements.amount");
|
|
1853
|
+
}
|
|
1854
|
+
const key = local.channelId.toLowerCase();
|
|
1855
|
+
const previous = await storage.get(key);
|
|
1856
|
+
const depositAmount = local.depositAmount === void 0 ? void 0 : BigInt(local.depositAmount);
|
|
1857
|
+
if (!previous && chargedAmount === 0n && depositAmount === void 0) {
|
|
1858
|
+
return;
|
|
1859
|
+
}
|
|
1860
|
+
const nextChargedCumulative = BigInt(previous?.chargedCumulativeAmount ?? "0") + chargedAmount;
|
|
1861
|
+
if (server.chargedCumulativeAmount !== void 0) {
|
|
1862
|
+
if (!/^\d+$/.test(server.chargedCumulativeAmount) || BigInt(server.chargedCumulativeAmount) !== nextChargedCumulative) {
|
|
1863
|
+
return;
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
const next = { ...previous ?? {} };
|
|
1867
|
+
next.chargedCumulativeAmount = nextChargedCumulative.toString();
|
|
1868
|
+
if (depositAmount !== void 0) {
|
|
1869
|
+
next.balance = (BigInt(previous?.balance ?? "0") + depositAmount).toString();
|
|
1870
|
+
}
|
|
1871
|
+
await storage.set(key, next);
|
|
1872
|
+
}
|
|
1873
|
+
async function updateChannelAfterRefund(storage, channelKey, refundAmount) {
|
|
1874
|
+
if (refundAmount === void 0) {
|
|
1875
|
+
await storage.delete(channelKey);
|
|
1876
|
+
return;
|
|
1877
|
+
}
|
|
1878
|
+
const amount = BigInt(refundAmount);
|
|
1879
|
+
const previous = await storage.get(channelKey);
|
|
1880
|
+
const previousBalance = BigInt(previous?.balance ?? "0");
|
|
1881
|
+
const chargedCumulativeAmount = BigInt(previous?.chargedCumulativeAmount ?? "0");
|
|
1882
|
+
const refundableBalance = previousBalance > chargedCumulativeAmount ? previousBalance - chargedCumulativeAmount : 0n;
|
|
1883
|
+
if (amount >= refundableBalance) {
|
|
1884
|
+
await storage.delete(channelKey);
|
|
1885
|
+
return;
|
|
1886
|
+
}
|
|
1887
|
+
const next = { ...previous ?? {} };
|
|
1888
|
+
next.balance = (previousBalance - amount).toString();
|
|
1889
|
+
await storage.set(channelKey, next);
|
|
1890
|
+
}
|
|
1891
|
+
async function recoverChannel(deps, paymentRequirements) {
|
|
1892
|
+
if (!deps.signer.readContract) {
|
|
1893
|
+
throw new Error("recoverChannel requires ClientEvmSigner.readContract");
|
|
1894
|
+
}
|
|
1895
|
+
const config = buildChannelConfig(deps, paymentRequirements);
|
|
1896
|
+
const channelId = computeChannelId(config, paymentRequirements.network);
|
|
1897
|
+
const [chBalance, chTotalClaimed] = await readChannelBalanceAndTotalClaimed(
|
|
1898
|
+
deps.signer,
|
|
1899
|
+
channelId
|
|
1900
|
+
);
|
|
1901
|
+
const ctx = {
|
|
1902
|
+
chargedCumulativeAmount: chTotalClaimed.toString(),
|
|
1903
|
+
balance: chBalance.toString(),
|
|
1904
|
+
totalClaimed: chTotalClaimed.toString()
|
|
1905
|
+
};
|
|
1906
|
+
await deps.storage.set(channelId.toLowerCase(), ctx);
|
|
1907
|
+
return ctx;
|
|
1908
|
+
}
|
|
1909
|
+
async function readChannelBalanceAndTotalClaimed(signer, channelId) {
|
|
1910
|
+
if (!signer.readContract) {
|
|
1911
|
+
throw new Error("readChannelBalanceAndTotalClaimed requires ClientEvmSigner.readContract");
|
|
1912
|
+
}
|
|
1913
|
+
return await signer.readContract({
|
|
1914
|
+
address: BATCH_SETTLEMENT_ADDRESS,
|
|
1915
|
+
abi: batchSettlementABI,
|
|
1916
|
+
functionName: "channels",
|
|
1917
|
+
args: [channelId]
|
|
1918
|
+
});
|
|
1919
|
+
}
|
|
1920
|
+
async function processCorrectivePaymentRequired(deps, paymentRequired) {
|
|
1921
|
+
if (paymentRequired.error !== ErrCumulativeAmountMismatch && paymentRequired.error !== ErrCumulativeAmountBelowClaimed) {
|
|
1922
|
+
return false;
|
|
1923
|
+
}
|
|
1924
|
+
const accept = paymentRequired.accepts.find((a) => a.scheme === BATCH_SETTLEMENT_SCHEME);
|
|
1925
|
+
if (!accept) {
|
|
1926
|
+
return false;
|
|
1927
|
+
}
|
|
1928
|
+
const channelState = accept.extra.channelState;
|
|
1929
|
+
const voucherState = accept.extra.voucherState;
|
|
1930
|
+
const hasSig = channelState?.chargedCumulativeAmount !== void 0 && voucherState?.signedMaxClaimable !== void 0 && voucherState.signature !== void 0;
|
|
1931
|
+
if (!hasSig) {
|
|
1932
|
+
return recoverFromOnChainState(deps, accept);
|
|
1933
|
+
}
|
|
1934
|
+
return recoverFromSignature(deps, accept, channelState, voucherState);
|
|
1935
|
+
}
|
|
1936
|
+
async function recoverFromSignature(deps, accept, channelState, voucherState) {
|
|
1937
|
+
const chargedRaw = channelState.chargedCumulativeAmount;
|
|
1938
|
+
const signedRaw = voucherState.signedMaxClaimable;
|
|
1939
|
+
const sig = voucherState.signature;
|
|
1940
|
+
const charged = BigInt(String(chargedRaw));
|
|
1941
|
+
const signed = BigInt(String(signedRaw));
|
|
1942
|
+
if (charged > signed) {
|
|
1943
|
+
return false;
|
|
1944
|
+
}
|
|
1945
|
+
const config = buildChannelConfig(deps, accept);
|
|
1946
|
+
const channelId = computeChannelId(config, accept.network);
|
|
1947
|
+
if (!deps.signer.readContract) {
|
|
1948
|
+
return false;
|
|
1949
|
+
}
|
|
1950
|
+
const [chBalance, chTotalClaimed] = await readChannelBalanceAndTotalClaimed(
|
|
1951
|
+
deps.signer,
|
|
1952
|
+
channelId
|
|
1953
|
+
);
|
|
1954
|
+
if (charged < chTotalClaimed) {
|
|
1955
|
+
return false;
|
|
1956
|
+
}
|
|
1957
|
+
const chainId = getEvmChainId(accept.network);
|
|
1958
|
+
const recovered = await recoverTypedDataAddress({
|
|
1959
|
+
domain: getBatchSettlementEip712Domain(chainId),
|
|
1960
|
+
types: voucherTypes,
|
|
1961
|
+
primaryType: "Voucher",
|
|
1962
|
+
message: {
|
|
1963
|
+
channelId,
|
|
1964
|
+
maxClaimableAmount: signed
|
|
1965
|
+
},
|
|
1966
|
+
signature: sig
|
|
1967
|
+
});
|
|
1968
|
+
const expectedSigner = getAddress(
|
|
1969
|
+
deps.payerAuthorizer ?? deps.voucherSigner?.address ?? deps.signer.address
|
|
1970
|
+
);
|
|
1971
|
+
if (recovered.toLowerCase() !== expectedSigner.toLowerCase()) {
|
|
1972
|
+
return false;
|
|
1973
|
+
}
|
|
1974
|
+
const ctx = {
|
|
1975
|
+
chargedCumulativeAmount: charged.toString(),
|
|
1976
|
+
signedMaxClaimable: signed.toString(),
|
|
1977
|
+
signature: sig,
|
|
1978
|
+
balance: chBalance.toString(),
|
|
1979
|
+
totalClaimed: chTotalClaimed.toString()
|
|
1980
|
+
};
|
|
1981
|
+
await deps.storage.set(channelId.toLowerCase(), ctx);
|
|
1982
|
+
return true;
|
|
1983
|
+
}
|
|
1984
|
+
async function recoverFromOnChainState(deps, accept) {
|
|
1985
|
+
if (!deps.signer.readContract) {
|
|
1986
|
+
return false;
|
|
1987
|
+
}
|
|
1988
|
+
const config = buildChannelConfig(deps, accept);
|
|
1989
|
+
const channelId = computeChannelId(config, accept.network);
|
|
1990
|
+
const [chBalance, chTotalClaimed] = await readChannelBalanceAndTotalClaimed(
|
|
1991
|
+
deps.signer,
|
|
1992
|
+
channelId
|
|
1993
|
+
);
|
|
1994
|
+
const ctx = {
|
|
1995
|
+
chargedCumulativeAmount: chTotalClaimed.toString(),
|
|
1996
|
+
balance: chBalance.toString(),
|
|
1997
|
+
totalClaimed: chTotalClaimed.toString()
|
|
1998
|
+
};
|
|
1999
|
+
await deps.storage.set(channelId.toLowerCase(), ctx);
|
|
2000
|
+
return true;
|
|
2001
|
+
}
|
|
2002
|
+
function createBatchSettlementClientHooks(deps) {
|
|
2003
|
+
return {
|
|
2004
|
+
onPaymentResponse: async (ctx) => {
|
|
2005
|
+
const settleResponse = ctx.settleResponse;
|
|
2006
|
+
if (settleResponse) {
|
|
2007
|
+
if (!settleResponse.success) {
|
|
2008
|
+
return;
|
|
2009
|
+
}
|
|
2010
|
+
const payload = ctx.paymentPayload.payload;
|
|
2011
|
+
const channelId = computeChannelId(
|
|
2012
|
+
buildChannelConfig(deps, ctx.requirements),
|
|
2013
|
+
ctx.requirements.network
|
|
2014
|
+
);
|
|
2015
|
+
if (isBatchSettlementRefundPayload(payload)) {
|
|
2016
|
+
await updateChannelAfterRefund(deps.storage, channelId.toLowerCase(), payload.amount);
|
|
2017
|
+
return;
|
|
2018
|
+
}
|
|
2019
|
+
const chargedAmount = settleResponse.extra?.chargedAmount;
|
|
2020
|
+
if (chargedAmount !== void 0 && typeof chargedAmount !== "string") {
|
|
2021
|
+
throw new Error("invalid chargedAmount: not a non-negative integer");
|
|
2022
|
+
}
|
|
2023
|
+
const channelState = settleResponse.extra?.channelState;
|
|
2024
|
+
await updateChannelFromSettle(deps.storage, {
|
|
2025
|
+
server: {
|
|
2026
|
+
chargedAmount,
|
|
2027
|
+
chargedCumulativeAmount: channelState?.chargedCumulativeAmount
|
|
2028
|
+
},
|
|
2029
|
+
local: {
|
|
2030
|
+
channelId,
|
|
2031
|
+
requestAmount: ctx.requirements.amount,
|
|
2032
|
+
depositAmount: isBatchSettlementDepositPayload(payload) ? payload.deposit.amount : void 0
|
|
2033
|
+
}
|
|
2034
|
+
});
|
|
2035
|
+
return;
|
|
2036
|
+
}
|
|
2037
|
+
if (ctx.paymentRequired) {
|
|
2038
|
+
const recovered = await processCorrectivePaymentRequired(deps, ctx.paymentRequired);
|
|
2039
|
+
return recovered ? { recovered: true } : void 0;
|
|
2040
|
+
}
|
|
2041
|
+
}
|
|
2042
|
+
};
|
|
2043
|
+
}
|
|
2044
|
+
var NON_RECOVERABLE_REFUND_ERRORS = /* @__PURE__ */ new Set([
|
|
2045
|
+
ErrRefundNoBalance,
|
|
2046
|
+
ErrRefundAmountInvalid
|
|
2047
|
+
]);
|
|
2048
|
+
async function refundChannel(ctx, url, options) {
|
|
2049
|
+
const fetchImpl = options?.fetch ?? globalThis.fetch;
|
|
2050
|
+
if (!fetchImpl) {
|
|
2051
|
+
throw new Error("refund requires a fetch implementation (globalThis.fetch unavailable)");
|
|
2052
|
+
}
|
|
2053
|
+
const refundAmount = normalizeRefundAmount(options?.amount);
|
|
2054
|
+
const probe = await probeRefundRequirements(url, fetchImpl);
|
|
2055
|
+
return executeRefund(ctx, url, probe, refundAmount, fetchImpl);
|
|
2056
|
+
}
|
|
2057
|
+
async function probeRefundRequirements(url, fetchImpl) {
|
|
2058
|
+
const probe = await fetchImpl(url, { method: "GET" });
|
|
2059
|
+
if (probe.status !== 402) {
|
|
2060
|
+
throw new Error(`Refund probe expected 402, got ${probe.status}`);
|
|
2061
|
+
}
|
|
2062
|
+
const header = probe.headers.get("PAYMENT-REQUIRED");
|
|
2063
|
+
if (!header) {
|
|
2064
|
+
throw new Error("Refund probe response missing PAYMENT-REQUIRED header");
|
|
2065
|
+
}
|
|
2066
|
+
const paymentRequired = decodePaymentRequiredHeader(header);
|
|
2067
|
+
const requirements = paymentRequired.accepts.find((a) => a.scheme === BATCH_SETTLEMENT_SCHEME);
|
|
2068
|
+
if (!requirements) {
|
|
2069
|
+
throw new Error(`No ${BATCH_SETTLEMENT_SCHEME} payment option at ${url}`);
|
|
2070
|
+
}
|
|
2071
|
+
const extra = requirements.extra;
|
|
2072
|
+
if (!extra?.receiverAuthorizer) {
|
|
2073
|
+
throw new Error("Refund requires a configured receiverAuthorizer on the receiver");
|
|
2074
|
+
}
|
|
2075
|
+
return { paymentRequired, requirements };
|
|
2076
|
+
}
|
|
2077
|
+
async function executeRefund(ctx, url, probe, refundAmount, fetchImpl) {
|
|
2078
|
+
const maxAttempts = 2;
|
|
2079
|
+
const { paymentRequired, requirements } = probe;
|
|
2080
|
+
const httpClient = createRefundHttpClient(ctx, requirements);
|
|
2081
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
|
2082
|
+
const paymentPayload = await buildRefundPaymentPayload(
|
|
2083
|
+
ctx,
|
|
2084
|
+
paymentRequired,
|
|
2085
|
+
requirements,
|
|
2086
|
+
refundAmount
|
|
2087
|
+
);
|
|
2088
|
+
const headers = httpClient.encodePaymentSignatureHeader(paymentPayload);
|
|
2089
|
+
const response = await fetchImpl(url, { method: "GET", headers });
|
|
2090
|
+
if (response.status === 402) {
|
|
2091
|
+
const nonRecoverable = getNonRecoverableRefundFailure(response);
|
|
2092
|
+
if (nonRecoverable) {
|
|
2093
|
+
throw new Error(nonRecoverable);
|
|
2094
|
+
}
|
|
2095
|
+
}
|
|
2096
|
+
const result = await httpClient.processPaymentResult(
|
|
2097
|
+
paymentPayload,
|
|
2098
|
+
(name) => response.headers.get(name),
|
|
2099
|
+
response.status
|
|
2100
|
+
);
|
|
2101
|
+
if (response.status === 402) {
|
|
2102
|
+
if (result.recovered && attempt < maxAttempts) {
|
|
2103
|
+
continue;
|
|
2104
|
+
}
|
|
2105
|
+
if (result.recovered) {
|
|
2106
|
+
throw new Error(`Refund failed: server returned 402 after ${attempt} attempt(s)`);
|
|
2107
|
+
}
|
|
2108
|
+
const corrective = getRefundPaymentRequired(response);
|
|
2109
|
+
throw new Error(`Refund failed: ${corrective.error ?? "unknown"}`);
|
|
2110
|
+
}
|
|
2111
|
+
if (!result.settleResponse) {
|
|
2112
|
+
throw new Error(
|
|
2113
|
+
`Refund response missing PAYMENT-RESPONSE header (status ${response.status})`
|
|
2114
|
+
);
|
|
2115
|
+
}
|
|
2116
|
+
return result.settleResponse;
|
|
2117
|
+
}
|
|
2118
|
+
throw new Error("Refund failed: retry budget exhausted");
|
|
2119
|
+
}
|
|
2120
|
+
async function buildRefundPaymentPayload(ctx, paymentRequired, requirements, refundAmount) {
|
|
2121
|
+
const config = buildChannelConfig(ctx, requirements);
|
|
2122
|
+
const channelId = computeChannelId(config, requirements.network);
|
|
2123
|
+
const key = channelId.toLowerCase();
|
|
2124
|
+
let channel = await ctx.storage.get(key);
|
|
2125
|
+
if (channel === void 0 && ctx.signer.readContract) {
|
|
2126
|
+
channel = await recoverChannel(ctx, requirements);
|
|
2127
|
+
}
|
|
2128
|
+
if (channel === void 0) {
|
|
2129
|
+
throw new Error(
|
|
2130
|
+
"Refund requires an existing channel record; deposit first or call from a context with an EVM RPC"
|
|
2131
|
+
);
|
|
2132
|
+
}
|
|
2133
|
+
const charged = channel.chargedCumulativeAmount ?? "0";
|
|
2134
|
+
if (channel.balance !== void 0 && BigInt(channel.balance) <= BigInt(charged)) {
|
|
2135
|
+
throw new Error(
|
|
2136
|
+
`Refund failed: channel has no remaining balance (balance=${channel.balance}, chargedCumulativeAmount=${charged})`
|
|
2137
|
+
);
|
|
2138
|
+
}
|
|
2139
|
+
const voucherSigner = ctx.voucherSigner ?? ctx.signer;
|
|
2140
|
+
const voucher = await signVoucher(voucherSigner, channelId, charged, requirements.network);
|
|
2141
|
+
const payload = {
|
|
2142
|
+
type: "refund",
|
|
2143
|
+
channelConfig: config,
|
|
2144
|
+
voucher,
|
|
2145
|
+
...refundAmount !== void 0 ? { amount: refundAmount } : {}
|
|
2146
|
+
};
|
|
2147
|
+
return {
|
|
2148
|
+
x402Version: 2,
|
|
2149
|
+
accepted: requirements,
|
|
2150
|
+
payload,
|
|
2151
|
+
...paymentRequired.resource ? { resource: paymentRequired.resource } : {},
|
|
2152
|
+
...paymentRequired.extensions ? { extensions: paymentRequired.extensions } : {}
|
|
2153
|
+
};
|
|
2154
|
+
}
|
|
2155
|
+
function createRefundHttpClient(ctx, requirements) {
|
|
2156
|
+
const client = new x402Client().register(requirements.network, {
|
|
2157
|
+
scheme: BATCH_SETTLEMENT_SCHEME,
|
|
2158
|
+
schemeHooks: createBatchSettlementClientHooks(ctx),
|
|
2159
|
+
createPaymentPayload: async () => {
|
|
2160
|
+
throw new Error("Refund payloads are built by refundChannel");
|
|
2161
|
+
}
|
|
2162
|
+
});
|
|
2163
|
+
return new x402HTTPClient(client);
|
|
2164
|
+
}
|
|
2165
|
+
function getNonRecoverableRefundFailure(response) {
|
|
2166
|
+
const settleHeader = response.headers.get("PAYMENT-RESPONSE");
|
|
2167
|
+
if (settleHeader) {
|
|
2168
|
+
return formatRefundFailure(decodePaymentResponseHeader(settleHeader));
|
|
2169
|
+
}
|
|
2170
|
+
const paymentRequired = getRefundPaymentRequired(response);
|
|
2171
|
+
const errorCode = paymentRequired.error;
|
|
2172
|
+
if (errorCode && NON_RECOVERABLE_REFUND_ERRORS.has(errorCode)) {
|
|
2173
|
+
return `Refund failed: ${errorCode}`;
|
|
2174
|
+
}
|
|
2175
|
+
}
|
|
2176
|
+
function getRefundPaymentRequired(response) {
|
|
2177
|
+
const requiredHeader = response.headers.get("PAYMENT-REQUIRED");
|
|
2178
|
+
if (!requiredHeader) {
|
|
2179
|
+
throw new Error("Refund 402 missing PAYMENT-REQUIRED header");
|
|
2180
|
+
}
|
|
2181
|
+
return decodePaymentRequiredHeader(requiredHeader);
|
|
2182
|
+
}
|
|
2183
|
+
function formatRefundFailure(settle) {
|
|
2184
|
+
const reason = settle.errorReason ?? "unknown_settlement_error";
|
|
2185
|
+
const message = settle.errorMessage;
|
|
2186
|
+
if (message && message !== reason) {
|
|
2187
|
+
return `Refund failed: ${reason}: ${message}`;
|
|
2188
|
+
}
|
|
2189
|
+
return `Refund failed: ${reason}`;
|
|
2190
|
+
}
|
|
2191
|
+
function normalizeRefundAmount(amount) {
|
|
2192
|
+
if (amount === void 0) return void 0;
|
|
2193
|
+
if (!/^\d+$/.test(amount) || amount === "0") {
|
|
2194
|
+
throw new Error(`Invalid refund amount "${amount}": must be a positive integer string`);
|
|
2195
|
+
}
|
|
2196
|
+
return amount;
|
|
2197
|
+
}
|
|
2198
|
+
async function createBatchSettlementPermit2DepositPayload(signer, x402Version2, paymentRequirements, channelConfig, depositAmount, maxClaimableAmount, voucherSigner) {
|
|
2199
|
+
const chainId = getEvmChainId(paymentRequirements.network);
|
|
2200
|
+
const nonce = createPermit2Nonce();
|
|
2201
|
+
const deadline = Math.floor(Date.now() / 1e3 + paymentRequirements.maxTimeoutSeconds).toString();
|
|
2202
|
+
const channelId = computeChannelId(channelConfig, paymentRequirements.network);
|
|
2203
|
+
const permit2Authorization = {
|
|
2204
|
+
from: signer.address,
|
|
2205
|
+
permitted: {
|
|
2206
|
+
token: getAddress(paymentRequirements.asset),
|
|
2207
|
+
amount: depositAmount
|
|
2208
|
+
},
|
|
2209
|
+
spender: getAddress(PERMIT2_DEPOSIT_COLLECTOR_ADDRESS),
|
|
2210
|
+
nonce,
|
|
2211
|
+
deadline,
|
|
2212
|
+
witness: {
|
|
2213
|
+
channelId
|
|
2214
|
+
}
|
|
2215
|
+
};
|
|
2216
|
+
const signature = await signer.signTypedData({
|
|
2217
|
+
domain: { name: "Permit2", chainId, verifyingContract: PERMIT2_ADDRESS },
|
|
2218
|
+
types: batchPermit2WitnessTypes,
|
|
2219
|
+
primaryType: "PermitWitnessTransferFrom",
|
|
2220
|
+
message: {
|
|
2221
|
+
permitted: {
|
|
2222
|
+
token: permit2Authorization.permitted.token,
|
|
2223
|
+
amount: BigInt(permit2Authorization.permitted.amount)
|
|
2224
|
+
},
|
|
2225
|
+
spender: permit2Authorization.spender,
|
|
2226
|
+
nonce: BigInt(permit2Authorization.nonce),
|
|
2227
|
+
deadline: BigInt(permit2Authorization.deadline),
|
|
2228
|
+
witness: {
|
|
2229
|
+
channelId
|
|
2230
|
+
}
|
|
2231
|
+
}
|
|
2232
|
+
});
|
|
2233
|
+
const voucher = await signVoucher(
|
|
2234
|
+
voucherSigner ?? signer,
|
|
2235
|
+
channelId,
|
|
2236
|
+
maxClaimableAmount,
|
|
2237
|
+
paymentRequirements.network
|
|
2238
|
+
);
|
|
2239
|
+
const payload = {
|
|
2240
|
+
type: "deposit",
|
|
2241
|
+
channelConfig,
|
|
2242
|
+
voucher,
|
|
2243
|
+
deposit: {
|
|
2244
|
+
amount: depositAmount,
|
|
2245
|
+
authorization: {
|
|
2246
|
+
permit2Authorization: {
|
|
2247
|
+
...permit2Authorization,
|
|
2248
|
+
signature
|
|
2249
|
+
}
|
|
2250
|
+
}
|
|
2251
|
+
}
|
|
2252
|
+
};
|
|
2253
|
+
return { x402Version: x402Version2, payload };
|
|
2254
|
+
}
|
|
2255
|
+
var BatchSettlementEvmScheme = class {
|
|
2256
|
+
/**
|
|
2257
|
+
* Constructs a batched client scheme.
|
|
2258
|
+
*
|
|
2259
|
+
* @param signer - Client EVM wallet used for signing vouchers and ERC-3009 authorizations.
|
|
2260
|
+
* @param optionsOrPolicy - Either a full options object or a bare deposit-policy.
|
|
2261
|
+
*/
|
|
2262
|
+
constructor(signer, optionsOrPolicy) {
|
|
2263
|
+
this.signer = signer;
|
|
2264
|
+
this.scheme = BATCH_SETTLEMENT_SCHEME;
|
|
2265
|
+
this.findDefaultAsset = findDefaultAsset;
|
|
2266
|
+
const {
|
|
2267
|
+
storage,
|
|
2268
|
+
depositPolicy,
|
|
2269
|
+
depositStrategy,
|
|
2270
|
+
salt,
|
|
2271
|
+
payerAuthorizer,
|
|
2272
|
+
voucherSigner,
|
|
2273
|
+
extensionRpcOptions
|
|
2274
|
+
} = resolveClientOptions(optionsOrPolicy);
|
|
2275
|
+
this.storage = storage;
|
|
2276
|
+
this.depositPolicy = depositPolicy;
|
|
2277
|
+
this.depositStrategy = depositStrategy;
|
|
2278
|
+
this.salt = salt;
|
|
2279
|
+
this.payerAuthorizer = payerAuthorizer;
|
|
2280
|
+
this.voucherSigner = voucherSigner;
|
|
2281
|
+
this.extensionRpcOptions = extensionRpcOptions;
|
|
2282
|
+
if (payerAuthorizer !== void 0 && voucherSigner !== void 0 && getAddress(payerAuthorizer) !== getAddress(voucherSigner.address)) {
|
|
2283
|
+
throw new Error("payerAuthorizer address must match voucherSigner.address");
|
|
2284
|
+
}
|
|
2285
|
+
validateDepositPolicy(depositPolicy);
|
|
2286
|
+
this.schemeHooks = createBatchSettlementClientHooks(this.deps());
|
|
2287
|
+
}
|
|
2288
|
+
/**
|
|
2289
|
+
* Creates the payment payload for a batched request.
|
|
2290
|
+
*
|
|
2291
|
+
* If the channel has no onchain deposit (or needs a top-up), builds an
|
|
2292
|
+
* ERC-3009 deposit payload bundled with a voucher. Otherwise, signs and
|
|
2293
|
+
* returns a voucher-only payload.
|
|
2294
|
+
*
|
|
2295
|
+
* @param x402Version - Protocol version for the payload envelope.
|
|
2296
|
+
* @param paymentRequirements - Server payment requirements (scheme, network, asset, amount).
|
|
2297
|
+
* @param context - Optional extensions and the resolved atomic spend cap.
|
|
2298
|
+
* @returns A {@link PaymentPayloadResult} ready to be sent as the `X-PAYMENT` header.
|
|
2299
|
+
*/
|
|
2300
|
+
async createPaymentPayload(x402Version2, paymentRequirements, context) {
|
|
2301
|
+
const deps = this.deps();
|
|
2302
|
+
const config = buildChannelConfig(deps, paymentRequirements);
|
|
2303
|
+
const channelId = computeChannelId(config, paymentRequirements.network);
|
|
2304
|
+
const key = channelId.toLowerCase();
|
|
2305
|
+
let batchedCtx = await this.storage.get(key);
|
|
2306
|
+
if (batchedCtx === void 0 && this.signer.readContract) {
|
|
2307
|
+
batchedCtx = await recoverChannel(deps, paymentRequirements);
|
|
2308
|
+
}
|
|
2309
|
+
batchedCtx = batchedCtx ?? {};
|
|
2310
|
+
const needsInitialDeposit = !batchedCtx.balance || batchedCtx.balance === "0";
|
|
2311
|
+
const baseCumulative = BigInt(batchedCtx.chargedCumulativeAmount ?? "0");
|
|
2312
|
+
const requestAmount = BigInt(paymentRequirements.amount);
|
|
2313
|
+
const maxClaimableAmount = (baseCumulative + requestAmount).toString();
|
|
2314
|
+
const currentBalance = BigInt(batchedCtx.balance ?? "0");
|
|
2315
|
+
const needsTopUp = !needsInitialDeposit && BigInt(maxClaimableAmount) > currentBalance;
|
|
2316
|
+
if (needsInitialDeposit || needsTopUp) {
|
|
2317
|
+
const minimumDepositAmount = BigInt(maxClaimableAmount) - currentBalance;
|
|
2318
|
+
const maxDeposit = maxDepositFromSpendCap(
|
|
2319
|
+
context?.maxAmountPerPayment,
|
|
2320
|
+
this.depositPolicy?.depositMultiplier ?? 5
|
|
2321
|
+
);
|
|
2322
|
+
const computedDeposit = depositAmountForRequest(
|
|
2323
|
+
this.depositPolicy,
|
|
2324
|
+
requestAmount,
|
|
2325
|
+
minimumDepositAmount,
|
|
2326
|
+
paymentRequirements.extra,
|
|
2327
|
+
maxDeposit
|
|
2328
|
+
);
|
|
2329
|
+
const depositAmount = await this.resolveDepositAmount({
|
|
2330
|
+
paymentRequirements,
|
|
2331
|
+
channelConfig: config,
|
|
2332
|
+
channelId,
|
|
2333
|
+
clientContext: batchedCtx,
|
|
2334
|
+
requestAmount: requestAmount.toString(),
|
|
2335
|
+
maxClaimableAmount,
|
|
2336
|
+
currentBalance: currentBalance.toString(),
|
|
2337
|
+
minimumDepositAmount: minimumDepositAmount.toString(),
|
|
2338
|
+
depositAmount: computedDeposit,
|
|
2339
|
+
...maxDeposit !== void 0 ? { maxDeposit: maxDeposit.toString() } : {}
|
|
2340
|
+
});
|
|
2341
|
+
if (depositAmount === false) {
|
|
2342
|
+
return this.createVoucherPayload(
|
|
2343
|
+
x402Version2,
|
|
2344
|
+
channelId,
|
|
2345
|
+
maxClaimableAmount,
|
|
2346
|
+
paymentRequirements.network,
|
|
2347
|
+
config
|
|
2348
|
+
);
|
|
2349
|
+
}
|
|
2350
|
+
const assetTransferMethod = paymentRequirements.extra?.assetTransferMethod ?? "eip3009";
|
|
2351
|
+
if (assetTransferMethod === "eip3009") {
|
|
2352
|
+
return createBatchSettlementEIP3009DepositPayload(
|
|
2353
|
+
this.signer,
|
|
2354
|
+
x402Version2,
|
|
2355
|
+
paymentRequirements,
|
|
2356
|
+
config,
|
|
2357
|
+
depositAmount,
|
|
2358
|
+
maxClaimableAmount,
|
|
2359
|
+
this.voucherSigner
|
|
2360
|
+
);
|
|
2361
|
+
}
|
|
2362
|
+
if (assetTransferMethod !== "permit2") {
|
|
2363
|
+
throw new Error(`unsupported batch-settlement assetTransferMethod: ${assetTransferMethod}`);
|
|
2364
|
+
}
|
|
2365
|
+
const result = await createBatchSettlementPermit2DepositPayload(
|
|
2366
|
+
this.signer,
|
|
2367
|
+
x402Version2,
|
|
2368
|
+
paymentRequirements,
|
|
2369
|
+
config,
|
|
2370
|
+
depositAmount,
|
|
2371
|
+
maxClaimableAmount,
|
|
2372
|
+
this.voucherSigner
|
|
2373
|
+
);
|
|
2374
|
+
const eip2612Extensions = await trySignEip2612PermitExtension(
|
|
2375
|
+
this.signer,
|
|
2376
|
+
this.extensionRpcOptions,
|
|
2377
|
+
paymentRequirements,
|
|
2378
|
+
result,
|
|
2379
|
+
context,
|
|
2380
|
+
depositAmount
|
|
2381
|
+
);
|
|
2382
|
+
if (eip2612Extensions) {
|
|
2383
|
+
return { ...result, extensions: eip2612Extensions };
|
|
2384
|
+
}
|
|
2385
|
+
const erc20Extensions = await trySignErc20ApprovalExtension(
|
|
2386
|
+
this.signer,
|
|
2387
|
+
this.extensionRpcOptions,
|
|
2388
|
+
paymentRequirements,
|
|
2389
|
+
context,
|
|
2390
|
+
depositAmount
|
|
2391
|
+
);
|
|
2392
|
+
if (erc20Extensions) {
|
|
2393
|
+
return { ...result, extensions: erc20Extensions };
|
|
2394
|
+
}
|
|
2395
|
+
return result;
|
|
2396
|
+
}
|
|
2397
|
+
return this.createVoucherPayload(
|
|
2398
|
+
x402Version2,
|
|
2399
|
+
channelId,
|
|
2400
|
+
maxClaimableAmount,
|
|
2401
|
+
paymentRequirements.network,
|
|
2402
|
+
config
|
|
2403
|
+
);
|
|
2404
|
+
}
|
|
2405
|
+
/**
|
|
2406
|
+
* Sends a cooperative refund request.
|
|
2407
|
+
*
|
|
2408
|
+
* @param url - The route URL backing the channel to refund.
|
|
2409
|
+
* @param options - Optional `amount` (partial refund) and `fetch` override.
|
|
2410
|
+
* @returns The settle response describing the refund outcome.
|
|
2411
|
+
*/
|
|
2412
|
+
async refund(url, options) {
|
|
2413
|
+
return refundChannel(this.deps(), url, options);
|
|
2414
|
+
}
|
|
2415
|
+
/**
|
|
2416
|
+
* Resyncs local channel state from a corrective 402 response.
|
|
2417
|
+
*
|
|
2418
|
+
* @param paymentRequired - The decoded 402 response body.
|
|
2419
|
+
* @returns `true` if local state was successfully resynced and a retry is warranted.
|
|
2420
|
+
*/
|
|
2421
|
+
async processCorrectivePaymentRequired(paymentRequired) {
|
|
2422
|
+
return processCorrectivePaymentRequired(this.deps(), paymentRequired);
|
|
2423
|
+
}
|
|
2424
|
+
/**
|
|
2425
|
+
* Builds the immutable {@link ChannelConfig} for a given set of payment
|
|
2426
|
+
* requirements, using the scheme's own signer and salt.
|
|
2427
|
+
*
|
|
2428
|
+
* @param paymentRequirements - Server payment requirements for the channel.
|
|
2429
|
+
* @returns The channel config that uniquely identifies the payment channel.
|
|
2430
|
+
*/
|
|
2431
|
+
buildChannelConfig(paymentRequirements) {
|
|
2432
|
+
return buildChannelConfig(this.deps(), paymentRequirements);
|
|
2433
|
+
}
|
|
2434
|
+
/**
|
|
2435
|
+
* Resolves the deposit amount after applying the optional custom strategy.
|
|
2436
|
+
*
|
|
2437
|
+
* @param context - Deposit attempt context exposed to the strategy.
|
|
2438
|
+
* @returns The deposit amount to sign, or `false` to skip this deposit attempt.
|
|
2439
|
+
*/
|
|
2440
|
+
async resolveDepositAmount(context) {
|
|
2441
|
+
const strategyResult = await this.depositStrategy?.(context);
|
|
2442
|
+
if (strategyResult === false) return false;
|
|
2443
|
+
if (strategyResult === void 0) return context.depositAmount;
|
|
2444
|
+
const depositAmount = this.normalizeStrategyDepositAmount(strategyResult);
|
|
2445
|
+
if (BigInt(depositAmount) < BigInt(context.minimumDepositAmount)) {
|
|
2446
|
+
throw new Error(
|
|
2447
|
+
`depositStrategy returned ${depositAmount}, below required top-up ${context.minimumDepositAmount}`
|
|
2448
|
+
);
|
|
2449
|
+
}
|
|
2450
|
+
return applyMaxDeposit(
|
|
2451
|
+
BigInt(depositAmount),
|
|
2452
|
+
BigInt(context.minimumDepositAmount),
|
|
2453
|
+
context.maxDeposit === void 0 ? void 0 : BigInt(context.maxDeposit)
|
|
2454
|
+
);
|
|
2455
|
+
}
|
|
2456
|
+
/**
|
|
2457
|
+
* Normalizes and validates a strategy-provided base-unit deposit amount.
|
|
2458
|
+
*
|
|
2459
|
+
* @param value - Strategy-provided string or bigint amount.
|
|
2460
|
+
* @returns Normalized decimal string.
|
|
2461
|
+
*/
|
|
2462
|
+
normalizeStrategyDepositAmount(value) {
|
|
2463
|
+
if (typeof value === "bigint") {
|
|
2464
|
+
if (value <= 0n) {
|
|
2465
|
+
throw new Error("depositStrategy must return a positive integer deposit amount");
|
|
2466
|
+
}
|
|
2467
|
+
return value.toString();
|
|
2468
|
+
}
|
|
2469
|
+
if (/^\d+$/.test(value) && BigInt(value) > 0n) {
|
|
2470
|
+
return BigInt(value).toString();
|
|
2471
|
+
}
|
|
2472
|
+
throw new Error("depositStrategy must return a positive integer deposit amount");
|
|
2473
|
+
}
|
|
2474
|
+
/**
|
|
2475
|
+
* Signs a voucher-only payment payload for the current channel.
|
|
2476
|
+
*
|
|
2477
|
+
* @param x402Version - Protocol version for the payload envelope.
|
|
2478
|
+
* @param channelId - Channel identifier for the voucher.
|
|
2479
|
+
* @param maxClaimableAmount - Cumulative ceiling for the voucher.
|
|
2480
|
+
* @param network - CAIP-2 network identifier.
|
|
2481
|
+
* @param config - Immutable channel configuration.
|
|
2482
|
+
* @returns Voucher-only payment payload.
|
|
2483
|
+
*/
|
|
2484
|
+
async createVoucherPayload(x402Version2, channelId, maxClaimableAmount, network, config) {
|
|
2485
|
+
const voucherSigner = this.voucherSigner ?? this.signer;
|
|
2486
|
+
const voucher = await signVoucher(voucherSigner, channelId, maxClaimableAmount, network);
|
|
2487
|
+
const payload = {
|
|
2488
|
+
type: "voucher",
|
|
2489
|
+
channelConfig: config,
|
|
2490
|
+
voucher
|
|
2491
|
+
};
|
|
2492
|
+
return {
|
|
2493
|
+
x402Version: x402Version2,
|
|
2494
|
+
payload
|
|
2495
|
+
};
|
|
2496
|
+
}
|
|
2497
|
+
/**
|
|
2498
|
+
* Bundles the class state into the {@link BatchSettlementClientDeps} shape
|
|
2499
|
+
* consumed by the `channel`, `recovery`, and `refund` modules.
|
|
2500
|
+
*
|
|
2501
|
+
* @returns Client deps wrapping the scheme's own signer and storage.
|
|
2502
|
+
*/
|
|
2503
|
+
deps() {
|
|
2504
|
+
return {
|
|
2505
|
+
signer: this.signer,
|
|
2506
|
+
storage: this.storage,
|
|
2507
|
+
salt: this.salt,
|
|
2508
|
+
payerAuthorizer: this.payerAuthorizer,
|
|
2509
|
+
voucherSigner: this.voucherSigner
|
|
2510
|
+
};
|
|
2511
|
+
}
|
|
2512
|
+
};
|
|
2513
|
+
|
|
2514
|
+
// node_modules/.pnpm/@x402+evm@2.28.0_typescript@6.0.3/node_modules/@x402/evm/dist/esm/chunk-XP6HEYCL.mjs
|
|
2515
|
+
async function createPermit2PayloadForProxy(proxyAddress, signer, x402Version2, paymentRequirements) {
|
|
2516
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
2517
|
+
const nonce = createPermit2Nonce();
|
|
2518
|
+
const validAfter = "0";
|
|
2519
|
+
const deadline = (now + paymentRequirements.maxTimeoutSeconds).toString();
|
|
2520
|
+
const permit2Authorization = {
|
|
2521
|
+
from: signer.address,
|
|
2522
|
+
permitted: {
|
|
2523
|
+
token: getAddress(paymentRequirements.asset),
|
|
2524
|
+
amount: paymentRequirements.amount
|
|
2525
|
+
},
|
|
2526
|
+
spender: proxyAddress,
|
|
2527
|
+
nonce,
|
|
2528
|
+
deadline,
|
|
2529
|
+
witness: {
|
|
2530
|
+
to: getAddress(paymentRequirements.payTo),
|
|
2531
|
+
validAfter
|
|
2532
|
+
}
|
|
2533
|
+
};
|
|
2534
|
+
const signature = await signPermit2Authorization(
|
|
2535
|
+
signer,
|
|
2536
|
+
permit2Authorization,
|
|
2537
|
+
paymentRequirements
|
|
2538
|
+
);
|
|
2539
|
+
return {
|
|
2540
|
+
x402Version: x402Version2,
|
|
2541
|
+
payload: { signature, permit2Authorization }
|
|
2542
|
+
};
|
|
2543
|
+
}
|
|
2544
|
+
async function signPermit2Authorization(signer, permit2Authorization, requirements) {
|
|
2545
|
+
const chainId = getEvmChainId(requirements.network);
|
|
2546
|
+
return await signer.signTypedData({
|
|
2547
|
+
domain: { name: "Permit2", chainId, verifyingContract: PERMIT2_ADDRESS },
|
|
2548
|
+
types: permit2WitnessTypes,
|
|
2549
|
+
primaryType: "PermitWitnessTransferFrom",
|
|
2550
|
+
message: {
|
|
2551
|
+
permitted: {
|
|
2552
|
+
token: getAddress(permit2Authorization.permitted.token),
|
|
2553
|
+
amount: BigInt(permit2Authorization.permitted.amount)
|
|
2554
|
+
},
|
|
2555
|
+
spender: getAddress(permit2Authorization.spender),
|
|
2556
|
+
nonce: BigInt(permit2Authorization.nonce),
|
|
2557
|
+
deadline: BigInt(permit2Authorization.deadline),
|
|
2558
|
+
witness: {
|
|
2559
|
+
to: getAddress(permit2Authorization.witness.to),
|
|
2560
|
+
validAfter: BigInt(permit2Authorization.witness.validAfter)
|
|
2561
|
+
}
|
|
2562
|
+
}
|
|
2563
|
+
});
|
|
2564
|
+
}
|
|
2565
|
+
|
|
2566
|
+
// node_modules/.pnpm/@x402+evm@2.28.0_typescript@6.0.3/node_modules/@x402/evm/dist/esm/chunk-OR6TZGCV.mjs
|
|
2567
|
+
var MAX_UINT256 = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
2568
|
+
async function createPermit2Payload(signer, x402Version2, paymentRequirements) {
|
|
2569
|
+
return createPermit2PayloadForProxy(
|
|
2570
|
+
x402ExactPermit2ProxyAddress,
|
|
2571
|
+
signer,
|
|
2572
|
+
x402Version2,
|
|
2573
|
+
paymentRequirements
|
|
2574
|
+
);
|
|
2575
|
+
}
|
|
2576
|
+
|
|
2577
|
+
// node_modules/.pnpm/@x402+evm@2.28.0_typescript@6.0.3/node_modules/@x402/evm/dist/esm/chunk-DLPUE5VK.mjs
|
|
2578
|
+
async function createEIP3009Payload(signer, x402Version2, paymentRequirements) {
|
|
2579
|
+
const nonce = createNonce();
|
|
2580
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
2581
|
+
const authorization = {
|
|
2582
|
+
from: signer.address,
|
|
2583
|
+
to: getAddress(paymentRequirements.payTo),
|
|
2584
|
+
value: paymentRequirements.amount,
|
|
2585
|
+
validAfter: "0",
|
|
2586
|
+
validBefore: (now + paymentRequirements.maxTimeoutSeconds).toString(),
|
|
2587
|
+
nonce
|
|
2588
|
+
};
|
|
2589
|
+
const signature = await signEIP3009Authorization(signer, authorization, paymentRequirements);
|
|
2590
|
+
const payload = {
|
|
2591
|
+
authorization,
|
|
2592
|
+
signature
|
|
2593
|
+
};
|
|
2594
|
+
return {
|
|
2595
|
+
x402Version: x402Version2,
|
|
2596
|
+
payload
|
|
2597
|
+
};
|
|
2598
|
+
}
|
|
2599
|
+
async function signEIP3009Authorization(signer, authorization, requirements) {
|
|
2600
|
+
const chainId = getEvmChainId(requirements.network);
|
|
2601
|
+
if (!requirements.extra?.name || !requirements.extra?.version) {
|
|
2602
|
+
throw new Error(
|
|
2603
|
+
`EIP-712 domain parameters (name, version) are required in payment requirements for asset ${requirements.asset}`
|
|
2604
|
+
);
|
|
2605
|
+
}
|
|
2606
|
+
const { name, version } = requirements.extra;
|
|
2607
|
+
const domain = {
|
|
2608
|
+
name,
|
|
2609
|
+
version,
|
|
2610
|
+
chainId,
|
|
2611
|
+
verifyingContract: getAddress(requirements.asset)
|
|
2612
|
+
};
|
|
2613
|
+
const message = {
|
|
2614
|
+
from: getAddress(authorization.from),
|
|
2615
|
+
to: getAddress(authorization.to),
|
|
2616
|
+
value: BigInt(authorization.value),
|
|
2617
|
+
validAfter: BigInt(authorization.validAfter),
|
|
2618
|
+
validBefore: BigInt(authorization.validBefore),
|
|
2619
|
+
nonce: authorization.nonce
|
|
2620
|
+
};
|
|
2621
|
+
return await signer.signTypedData({
|
|
2622
|
+
domain,
|
|
2623
|
+
types: authorizationTypes,
|
|
2624
|
+
primaryType: "TransferWithAuthorization",
|
|
2625
|
+
message
|
|
2626
|
+
});
|
|
2627
|
+
}
|
|
2628
|
+
var ExactEvmScheme = class {
|
|
2629
|
+
/**
|
|
2630
|
+
* Creates a new ExactEvmClient instance.
|
|
2631
|
+
*
|
|
2632
|
+
* @param signer - The EVM signer for client operations.
|
|
2633
|
+
* Base flow only requires `address` + `signTypedData`.
|
|
2634
|
+
* Extension enrichment (EIP-2612 / ERC-20 approval sponsoring) additionally
|
|
2635
|
+
* requires optional capabilities like `readContract` and tx signing helpers.
|
|
2636
|
+
* @param options - Optional RPC configuration used to backfill extension capabilities.
|
|
2637
|
+
*/
|
|
2638
|
+
constructor(signer, options) {
|
|
2639
|
+
this.signer = signer;
|
|
2640
|
+
this.options = options;
|
|
2641
|
+
this.scheme = "exact";
|
|
2642
|
+
this.findDefaultAsset = findDefaultAsset;
|
|
2643
|
+
}
|
|
2644
|
+
/**
|
|
2645
|
+
* Creates a payment payload for the Exact scheme.
|
|
2646
|
+
* Routes to EIP-3009 or Permit2 based on requirements.extra.assetTransferMethod.
|
|
2647
|
+
*
|
|
2648
|
+
* For Permit2 flows, if the server advertises `eip2612GasSponsoring` and the
|
|
2649
|
+
* signer supports `readContract`, automatically signs an EIP-2612 permit
|
|
2650
|
+
* when Permit2 allowance is insufficient.
|
|
2651
|
+
*
|
|
2652
|
+
* @param x402Version - The x402 protocol version
|
|
2653
|
+
* @param paymentRequirements - The payment requirements
|
|
2654
|
+
* @param context - Optional context with server-declared extensions
|
|
2655
|
+
* @returns Promise resolving to a payment payload result (with optional extensions)
|
|
2656
|
+
*/
|
|
2657
|
+
async createPaymentPayload(x402Version2, paymentRequirements, context) {
|
|
2658
|
+
const assetTransferMethod = paymentRequirements.extra?.assetTransferMethod ?? "eip3009";
|
|
2659
|
+
if (assetTransferMethod === "permit2") {
|
|
2660
|
+
const result = await createPermit2Payload(this.signer, x402Version2, paymentRequirements);
|
|
2661
|
+
const eip2612Extensions = await trySignEip2612PermitExtension(
|
|
2662
|
+
this.signer,
|
|
2663
|
+
this.options,
|
|
2664
|
+
paymentRequirements,
|
|
2665
|
+
result,
|
|
2666
|
+
context
|
|
2667
|
+
);
|
|
2668
|
+
if (eip2612Extensions) {
|
|
2669
|
+
return {
|
|
2670
|
+
...result,
|
|
2671
|
+
extensions: eip2612Extensions
|
|
2672
|
+
};
|
|
2673
|
+
}
|
|
2674
|
+
const erc20Extensions = await trySignErc20ApprovalExtension(
|
|
2675
|
+
this.signer,
|
|
2676
|
+
this.options,
|
|
2677
|
+
paymentRequirements,
|
|
2678
|
+
context
|
|
2679
|
+
);
|
|
2680
|
+
if (erc20Extensions) {
|
|
2681
|
+
return {
|
|
2682
|
+
...result,
|
|
2683
|
+
extensions: erc20Extensions
|
|
2684
|
+
};
|
|
2685
|
+
}
|
|
2686
|
+
return result;
|
|
2687
|
+
}
|
|
2688
|
+
return createEIP3009Payload(this.signer, x402Version2, paymentRequirements);
|
|
2689
|
+
}
|
|
2690
|
+
};
|
|
2691
|
+
|
|
2692
|
+
// node_modules/.pnpm/@x402+evm@2.28.0_typescript@6.0.3/node_modules/@x402/evm/dist/esm/chunk-BPTXPSEK.mjs
|
|
2693
|
+
var DEFAULT_ASSET_CONTRACT_CACHE_TTL_MS = 15 * 60 * 1e3;
|
|
2694
|
+
|
|
2695
|
+
// node_modules/.pnpm/@x402+evm@2.28.0_typescript@6.0.3/node_modules/@x402/evm/dist/esm/index.mjs
|
|
2696
|
+
function toClientEvmSigner(signer, publicClient) {
|
|
2697
|
+
const readContract = signer.readContract ?? publicClient?.readContract.bind(publicClient);
|
|
2698
|
+
const result = {
|
|
2699
|
+
address: signer.address,
|
|
2700
|
+
signTypedData: (msg) => signer.signTypedData(msg)
|
|
2701
|
+
};
|
|
2702
|
+
if (readContract) {
|
|
2703
|
+
result.readContract = readContract;
|
|
2704
|
+
}
|
|
2705
|
+
const signTransaction = signer.signTransaction;
|
|
2706
|
+
if (signTransaction) {
|
|
2707
|
+
result.signTransaction = (args) => signTransaction(args);
|
|
2708
|
+
}
|
|
2709
|
+
const getTransactionCount = signer.getTransactionCount ?? publicClient?.getTransactionCount?.bind(publicClient);
|
|
2710
|
+
if (getTransactionCount) {
|
|
2711
|
+
result.getTransactionCount = (args) => getTransactionCount(args);
|
|
2712
|
+
}
|
|
2713
|
+
const estimateFeesPerGas = signer.estimateFeesPerGas ?? publicClient?.estimateFeesPerGas?.bind(publicClient);
|
|
2714
|
+
if (estimateFeesPerGas) {
|
|
2715
|
+
result.estimateFeesPerGas = () => estimateFeesPerGas();
|
|
2716
|
+
}
|
|
2717
|
+
return result;
|
|
2718
|
+
}
|
|
2719
|
+
|
|
2720
|
+
// node_modules/.pnpm/@x402+extensions@2.28.0_typescript@6.0.3/node_modules/@x402/extensions/dist/esm/chunk-DGKLKGT2.mjs
|
|
2721
|
+
var BUILDER_CODE = "builder-code";
|
|
2722
|
+
var BUILDER_CODE_PATTERN = /^[a-z0-9_]{1,32}$/;
|
|
2723
|
+
var MAX_CLIENT_SERVICE_CODES = 5;
|
|
2724
|
+
var MAX_SERVER_SERVICE_CODES = 5;
|
|
2725
|
+
var MAX_FACILITATOR_SERVICE_CODES = 1;
|
|
2726
|
+
var MAX_SERVICE_CODES = MAX_CLIENT_SERVICE_CODES + MAX_SERVER_SERVICE_CODES + MAX_FACILITATOR_SERVICE_CODES;
|
|
2727
|
+
var BuilderCodeClientExtension = class {
|
|
2728
|
+
/**
|
|
2729
|
+
* Creates a client extension that attaches the given service code(s) to payments.
|
|
2730
|
+
*
|
|
2731
|
+
* Accepts a single code or an array of codes so layered clients (e.g. an MCP
|
|
2732
|
+
* middleware) can attribute multiple participants. Codes are normalized to an
|
|
2733
|
+
* array and sent as the `s` field.
|
|
2734
|
+
*
|
|
2735
|
+
* @param serviceCodes - Client service code(s) (`s`), each 1-32 lowercase alphanumeric/underscore characters
|
|
2736
|
+
*/
|
|
2737
|
+
constructor(serviceCodes) {
|
|
2738
|
+
this.key = BUILDER_CODE;
|
|
2739
|
+
const codes = Array.isArray(serviceCodes) ? serviceCodes : [serviceCodes];
|
|
2740
|
+
if (codes.length > MAX_CLIENT_SERVICE_CODES) {
|
|
2741
|
+
throw new Error(
|
|
2742
|
+
`Too many service codes: ${codes.length} exceeds the maximum of ${MAX_CLIENT_SERVICE_CODES}.`
|
|
2743
|
+
);
|
|
2744
|
+
}
|
|
2745
|
+
for (const code of codes) {
|
|
2746
|
+
if (!BUILDER_CODE_PATTERN.test(code)) {
|
|
2747
|
+
throw new Error(
|
|
2748
|
+
`Invalid builder code: "${code}". Must be 1-32 characters, lowercase alphanumeric and underscores only.`
|
|
2749
|
+
);
|
|
2750
|
+
}
|
|
2751
|
+
}
|
|
2752
|
+
this.serviceCodes = codes;
|
|
2753
|
+
}
|
|
2754
|
+
/**
|
|
2755
|
+
* Attaches this client's service code(s) (`s`).
|
|
2756
|
+
*
|
|
2757
|
+
* @param payload - Payment payload to enrich
|
|
2758
|
+
* @param _ - Server payment requirements; core merges server extension data
|
|
2759
|
+
* @returns Payment payload with builder-code extension data
|
|
2760
|
+
*/
|
|
2761
|
+
async enrichPaymentPayload(payload, _) {
|
|
2762
|
+
return {
|
|
2763
|
+
...payload,
|
|
2764
|
+
extensions: {
|
|
2765
|
+
...payload.extensions,
|
|
2766
|
+
[BUILDER_CODE]: { info: { s: this.serviceCodes } }
|
|
2767
|
+
}
|
|
2768
|
+
};
|
|
2769
|
+
}
|
|
2770
|
+
};
|
|
2771
|
+
var MAX_ECHOED_SERVICE_CODES = MAX_CLIENT_SERVICE_CODES + MAX_SERVER_SERVICE_CODES;
|
|
2772
|
+
|
|
2773
|
+
// src/lib/x402-pay.ts
|
|
2774
|
+
function authorizationOf(payload, requirement) {
|
|
2775
|
+
const inner = payload?.payload;
|
|
2776
|
+
const auth = inner?.authorization;
|
|
2777
|
+
if (auth === null || typeof auth !== "object") return void 0;
|
|
2778
|
+
const { from, nonce, validBefore } = auth;
|
|
2779
|
+
if (typeof from !== "string" || !ADDRESS_RE.test(from)) return void 0;
|
|
2780
|
+
if (typeof nonce !== "string" || !/^0x[0-9a-fA-F]{64}$/.test(nonce)) return void 0;
|
|
2781
|
+
const before = typeof validBefore === "number" ? String(validBefore) : validBefore;
|
|
2782
|
+
if (typeof before !== "string" || !/^\d+$/.test(before)) return void 0;
|
|
2783
|
+
return {
|
|
2784
|
+
from,
|
|
2785
|
+
nonce,
|
|
2786
|
+
validBefore: before,
|
|
2787
|
+
network: requirement.network,
|
|
2788
|
+
asset: requirement.asset
|
|
2789
|
+
};
|
|
2790
|
+
}
|
|
2791
|
+
var ALLOWED_USDC_BY_NETWORK = {
|
|
2792
|
+
"eip155:8453": USDC_ADDRESS,
|
|
2793
|
+
"eip155:84532": "0x036CbD53842c5426634e7929541eC2318f3dCF7e"
|
|
2794
|
+
};
|
|
2795
|
+
var TENJIN_CLI_BUILDER_CODE = "bc_kc0altv3";
|
|
2796
|
+
function withoutSellerServiceCodes(extensions, key) {
|
|
2797
|
+
const entry = extensions?.[key];
|
|
2798
|
+
if (!isRecord(entry) || !isRecord(entry.info)) return extensions;
|
|
2799
|
+
const info = { ...entry.info };
|
|
2800
|
+
delete info.s;
|
|
2801
|
+
return { ...extensions, [key]: { ...entry, info } };
|
|
2802
|
+
}
|
|
2803
|
+
function createPayerClient(getSigner) {
|
|
2804
|
+
const lazy = {
|
|
2805
|
+
get address() {
|
|
2806
|
+
return getSigner().address;
|
|
2807
|
+
},
|
|
2808
|
+
signTypedData: (message) => getSigner().signTypedData(message)
|
|
2809
|
+
};
|
|
2810
|
+
const core = new x402Client();
|
|
2811
|
+
for (const network of Object.keys(ALLOWED_USDC_BY_NETWORK)) {
|
|
2812
|
+
core.register(network, new ExactEvmScheme(lazy));
|
|
2813
|
+
}
|
|
2814
|
+
core.registerPolicy(canonicalUsdcOnly);
|
|
2815
|
+
core.registerPolicy(eip3009Only);
|
|
2816
|
+
const builderCode = new BuilderCodeClientExtension(TENJIN_CLI_BUILDER_CODE);
|
|
2817
|
+
core.registerExtension(builderCode);
|
|
2818
|
+
return { core, http: new x402HTTPClient(core), builderCodeKey: builderCode.key };
|
|
2819
|
+
}
|
|
2820
|
+
var canonicalUsdcOnly = (_version, requirements) => requirements.filter(isCanonicalUsdc);
|
|
2821
|
+
var eip3009Only = (_version, requirements) => requirements.filter(signsEip3009);
|
|
2822
|
+
function transferMethod(entry) {
|
|
2823
|
+
return entry.extra?.assetTransferMethod;
|
|
2824
|
+
}
|
|
2825
|
+
function signsEip3009(entry) {
|
|
2826
|
+
const method = transferMethod(entry);
|
|
2827
|
+
return method === void 0 || method === "eip3009";
|
|
2828
|
+
}
|
|
2829
|
+
function isCanonicalUsdc(entry) {
|
|
2830
|
+
const allowed = ALLOWED_USDC_BY_NETWORK[entry.network];
|
|
2831
|
+
if (allowed === void 0) return false;
|
|
2832
|
+
try {
|
|
2833
|
+
return getAddress(entry.asset) === getAddress(allowed);
|
|
2834
|
+
} catch {
|
|
2835
|
+
return false;
|
|
2836
|
+
}
|
|
2837
|
+
}
|
|
2838
|
+
function selectPayableRequirement(core, paymentRequired, want) {
|
|
2839
|
+
const approved = supportedEntries(paymentRequired);
|
|
2840
|
+
if (want === void 0) return sdkSelect(core, paymentRequired) ?? approved[0];
|
|
2841
|
+
return approved.find(
|
|
2842
|
+
(r) => (want.network === void 0 || r.network === want.network) && (want.asset === void 0 || sameAsset(r.asset, want.asset))
|
|
2843
|
+
);
|
|
2844
|
+
}
|
|
2845
|
+
function supportedEntries(paymentRequired) {
|
|
2846
|
+
return canonicalUsdcOnly(paymentRequired.x402Version, paymentRequired.accepts).filter(
|
|
2847
|
+
(r) => r.scheme === "exact" && ALLOWED_USDC_BY_NETWORK[r.network] !== void 0 && signsEip3009(r)
|
|
2848
|
+
);
|
|
2849
|
+
}
|
|
2850
|
+
function sdkSelect(core, paymentRequired) {
|
|
2851
|
+
const select = core.selectPaymentRequirements;
|
|
2852
|
+
if (typeof select !== "function") return void 0;
|
|
2853
|
+
try {
|
|
2854
|
+
return select.call(core, paymentRequired.x402Version, paymentRequired.accepts);
|
|
2855
|
+
} catch {
|
|
2856
|
+
return void 0;
|
|
2857
|
+
}
|
|
2858
|
+
}
|
|
2859
|
+
function sameAsset(a, b) {
|
|
2860
|
+
try {
|
|
2861
|
+
return getAddress(a) === getAddress(b);
|
|
2862
|
+
} catch {
|
|
2863
|
+
return a.toLowerCase() === b.toLowerCase();
|
|
2864
|
+
}
|
|
2865
|
+
}
|
|
2866
|
+
function noPayableRequirement(accepts) {
|
|
2867
|
+
const advertised = accepts.map((a) => `${a.scheme}/${a.network}/${a.asset}`);
|
|
2868
|
+
const unsigned = accepts.find(
|
|
2869
|
+
(a) => a.scheme === "exact" && isCanonicalUsdc(a) && !signsEip3009(a)
|
|
2870
|
+
);
|
|
2871
|
+
const method = unsigned !== void 0 ? transferMethod(unsigned) : void 0;
|
|
2872
|
+
const permit2 = method === "permit2";
|
|
2873
|
+
return new CliError(
|
|
2874
|
+
"PAYMENT_FAILED",
|
|
2875
|
+
accepts.length === 0 ? "The 402 advertised no payment requirements." : permit2 ? "The 402 asks for a Permit2 transfer, which this CLI does not sign: it pays USDC on Base by EIP-3009 only." : unsigned !== void 0 ? `This payment method is not supported: the 402 asks for the transfer method "${sanitizeForTerminal(String(method)).slice(0, 100)}", and this CLI pays USDC on Base by EIP-3009 only.` : "The 402 advertises nothing this CLI can pay: it lists no exact-scheme entry in canonical USDC on Base.",
|
|
2876
|
+
{
|
|
2877
|
+
fix: "Only USDC on Base (eip155:8453) by EIP-3009, or Base Sepolia for previews, is supported. Nothing was signed.",
|
|
2878
|
+
...accepts.length > 0 ? { details: { advertised, ...permit2 ? { reason: "permit2" } : {} } } : {}
|
|
2879
|
+
}
|
|
2880
|
+
);
|
|
2881
|
+
}
|
|
2882
|
+
async function buildExactPayment(paymentRequired, signer, only) {
|
|
2883
|
+
const { core, http: http2, builderCodeKey } = createPayerClient(() => signer);
|
|
2884
|
+
const requirement = only ?? selectPayableRequirement(core, paymentRequired);
|
|
2885
|
+
if (requirement === void 0) throw noPayableRequirement(paymentRequired.accepts);
|
|
2886
|
+
core.setSpendControls({
|
|
2887
|
+
allowedAssets: [
|
|
2888
|
+
{
|
|
2889
|
+
network: requirement.network,
|
|
2890
|
+
asset: requirement.asset,
|
|
2891
|
+
maxAmountPerPayment: requirement.amount
|
|
2892
|
+
}
|
|
2893
|
+
]
|
|
2894
|
+
});
|
|
2895
|
+
const bound = {
|
|
2896
|
+
...paymentRequired,
|
|
2897
|
+
accepts: [requirement],
|
|
2898
|
+
extensions: withoutSellerServiceCodes(paymentRequired.extensions, builderCodeKey)
|
|
2899
|
+
};
|
|
2900
|
+
let headers;
|
|
2901
|
+
let authorization;
|
|
2902
|
+
try {
|
|
2903
|
+
const payload = await http2.createPaymentPayload(bound);
|
|
2904
|
+
headers = http2.encodePaymentSignatureHeader(payload);
|
|
2905
|
+
authorization = authorizationOf(payload, requirement);
|
|
2906
|
+
} catch (err) {
|
|
2907
|
+
throw new CliError("PAYMENT_FAILED", "Could not build the x402 payment authorization.", {
|
|
2908
|
+
fix: "Confirm the wallet is a supported EVM account on the advertised network.",
|
|
2909
|
+
cause: err
|
|
2910
|
+
});
|
|
2911
|
+
}
|
|
2912
|
+
return {
|
|
2913
|
+
headers,
|
|
2914
|
+
amountAtomic: BigInt(requirement.amount),
|
|
2915
|
+
payer: signer.address,
|
|
2916
|
+
...authorization !== void 0 ? { authorization } : {}
|
|
2917
|
+
};
|
|
2918
|
+
}
|
|
2919
|
+
|
|
2920
|
+
export {
|
|
2921
|
+
x402Client,
|
|
2922
|
+
readChannelBalanceAndTotalClaimed,
|
|
2923
|
+
BatchSettlementEvmScheme,
|
|
2924
|
+
toClientEvmSigner,
|
|
2925
|
+
createPayerClient,
|
|
2926
|
+
canonicalUsdcOnly,
|
|
2927
|
+
signsEip3009,
|
|
2928
|
+
isCanonicalUsdc,
|
|
2929
|
+
selectPayableRequirement,
|
|
2930
|
+
noPayableRequirement,
|
|
2931
|
+
buildExactPayment
|
|
2932
|
+
};
|
|
2933
|
+
//# sourceMappingURL=chunk-JTILUN4W.js.map
|