@metamask/snaps-rpc-methods 15.1.0 → 16.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (352) hide show
  1. package/CHANGELOG.md +34 -4
  2. package/dist/endowments/keyring.cjs +2 -5
  3. package/dist/endowments/keyring.cjs.map +1 -1
  4. package/dist/endowments/keyring.d.cts +0 -2
  5. package/dist/endowments/keyring.d.cts.map +1 -1
  6. package/dist/endowments/keyring.d.mts +0 -2
  7. package/dist/endowments/keyring.d.mts.map +1 -1
  8. package/dist/endowments/keyring.mjs +3 -6
  9. package/dist/endowments/keyring.mjs.map +1 -1
  10. package/dist/index.cjs +1 -4
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.cts +1 -2
  13. package/dist/index.d.cts.map +1 -1
  14. package/dist/index.d.mts +1 -2
  15. package/dist/index.d.mts.map +1 -1
  16. package/dist/index.mjs +1 -2
  17. package/dist/index.mjs.map +1 -1
  18. package/dist/permissions.cjs +18 -8
  19. package/dist/permissions.cjs.map +1 -1
  20. package/dist/permissions.d.cts +3 -2
  21. package/dist/permissions.d.cts.map +1 -1
  22. package/dist/permissions.d.mts +3 -2
  23. package/dist/permissions.d.mts.map +1 -1
  24. package/dist/permissions.mjs +17 -7
  25. package/dist/permissions.mjs.map +1 -1
  26. package/dist/permitted/cancelBackgroundEvent.cjs +10 -14
  27. package/dist/permitted/cancelBackgroundEvent.cjs.map +1 -1
  28. package/dist/permitted/cancelBackgroundEvent.d.cts +9 -12
  29. package/dist/permitted/cancelBackgroundEvent.d.cts.map +1 -1
  30. package/dist/permitted/cancelBackgroundEvent.d.mts +9 -12
  31. package/dist/permitted/cancelBackgroundEvent.d.mts.map +1 -1
  32. package/dist/permitted/cancelBackgroundEvent.mjs +10 -14
  33. package/dist/permitted/cancelBackgroundEvent.mjs.map +1 -1
  34. package/dist/permitted/clearState.cjs +10 -16
  35. package/dist/permitted/clearState.cjs.map +1 -1
  36. package/dist/permitted/clearState.d.cts +9 -23
  37. package/dist/permitted/clearState.d.cts.map +1 -1
  38. package/dist/permitted/clearState.d.mts +9 -23
  39. package/dist/permitted/clearState.d.mts.map +1 -1
  40. package/dist/permitted/clearState.mjs +10 -16
  41. package/dist/permitted/clearState.mjs.map +1 -1
  42. package/dist/permitted/closeWebSocket.cjs +7 -14
  43. package/dist/permitted/closeWebSocket.cjs.map +1 -1
  44. package/dist/permitted/closeWebSocket.d.cts +9 -12
  45. package/dist/permitted/closeWebSocket.d.cts.map +1 -1
  46. package/dist/permitted/closeWebSocket.d.mts +9 -12
  47. package/dist/permitted/closeWebSocket.d.mts.map +1 -1
  48. package/dist/permitted/closeWebSocket.mjs +7 -14
  49. package/dist/permitted/closeWebSocket.mjs.map +1 -1
  50. package/dist/permitted/createInterface.cjs +11 -15
  51. package/dist/permitted/createInterface.cjs.map +1 -1
  52. package/dist/permitted/createInterface.d.cts +23 -37
  53. package/dist/permitted/createInterface.d.cts.map +1 -1
  54. package/dist/permitted/createInterface.d.mts +23 -37
  55. package/dist/permitted/createInterface.d.mts.map +1 -1
  56. package/dist/permitted/createInterface.mjs +11 -15
  57. package/dist/permitted/createInterface.mjs.map +1 -1
  58. package/dist/permitted/endTrace.cjs +4 -6
  59. package/dist/permitted/endTrace.cjs.map +1 -1
  60. package/dist/permitted/endTrace.d.cts +8 -11
  61. package/dist/permitted/endTrace.d.cts.map +1 -1
  62. package/dist/permitted/endTrace.d.mts +8 -11
  63. package/dist/permitted/endTrace.d.mts.map +1 -1
  64. package/dist/permitted/endTrace.mjs +4 -6
  65. package/dist/permitted/endTrace.mjs.map +1 -1
  66. package/dist/permitted/getAllSnaps.cjs +5 -11
  67. package/dist/permitted/getAllSnaps.cjs.map +1 -1
  68. package/dist/permitted/getAllSnaps.d.cts +10 -14
  69. package/dist/permitted/getAllSnaps.d.cts.map +1 -1
  70. package/dist/permitted/getAllSnaps.d.mts +10 -14
  71. package/dist/permitted/getAllSnaps.d.mts.map +1 -1
  72. package/dist/permitted/getAllSnaps.mjs +5 -11
  73. package/dist/permitted/getAllSnaps.mjs.map +1 -1
  74. package/dist/permitted/getBackgroundEvents.cjs +8 -15
  75. package/dist/permitted/getBackgroundEvents.cjs.map +1 -1
  76. package/dist/permitted/getBackgroundEvents.d.cts +10 -13
  77. package/dist/permitted/getBackgroundEvents.d.cts.map +1 -1
  78. package/dist/permitted/getBackgroundEvents.d.mts +10 -13
  79. package/dist/permitted/getBackgroundEvents.d.mts.map +1 -1
  80. package/dist/permitted/getBackgroundEvents.mjs +8 -15
  81. package/dist/permitted/getBackgroundEvents.mjs.map +1 -1
  82. package/dist/permitted/getClientStatus.cjs +5 -6
  83. package/dist/permitted/getClientStatus.cjs.map +1 -1
  84. package/dist/permitted/getClientStatus.d.cts +17 -18
  85. package/dist/permitted/getClientStatus.d.cts.map +1 -1
  86. package/dist/permitted/getClientStatus.d.mts +17 -18
  87. package/dist/permitted/getClientStatus.d.mts.map +1 -1
  88. package/dist/permitted/getClientStatus.mjs +5 -6
  89. package/dist/permitted/getClientStatus.mjs.map +1 -1
  90. package/dist/permitted/getFile.cjs +7 -11
  91. package/dist/permitted/getFile.cjs.map +1 -1
  92. package/dist/permitted/getFile.d.cts +8 -10
  93. package/dist/permitted/getFile.d.cts.map +1 -1
  94. package/dist/permitted/getFile.d.mts +8 -10
  95. package/dist/permitted/getFile.d.mts.map +1 -1
  96. package/dist/permitted/getFile.mjs +7 -11
  97. package/dist/permitted/getFile.mjs.map +1 -1
  98. package/dist/permitted/getInterfaceContext.cjs +12 -15
  99. package/dist/permitted/getInterfaceContext.cjs.map +1 -1
  100. package/dist/permitted/getInterfaceContext.d.cts +9 -21
  101. package/dist/permitted/getInterfaceContext.d.cts.map +1 -1
  102. package/dist/permitted/getInterfaceContext.d.mts +9 -21
  103. package/dist/permitted/getInterfaceContext.d.mts.map +1 -1
  104. package/dist/permitted/getInterfaceContext.mjs +12 -15
  105. package/dist/permitted/getInterfaceContext.mjs.map +1 -1
  106. package/dist/permitted/getInterfaceState.cjs +11 -15
  107. package/dist/permitted/getInterfaceState.cjs.map +1 -1
  108. package/dist/permitted/getInterfaceState.d.cts +9 -21
  109. package/dist/permitted/getInterfaceState.d.cts.map +1 -1
  110. package/dist/permitted/getInterfaceState.d.mts +9 -21
  111. package/dist/permitted/getInterfaceState.d.mts.map +1 -1
  112. package/dist/permitted/getInterfaceState.mjs +11 -15
  113. package/dist/permitted/getInterfaceState.mjs.map +1 -1
  114. package/dist/permitted/getSnaps.cjs +6 -12
  115. package/dist/permitted/getSnaps.cjs.map +1 -1
  116. package/dist/permitted/getSnaps.d.cts +11 -14
  117. package/dist/permitted/getSnaps.d.cts.map +1 -1
  118. package/dist/permitted/getSnaps.d.mts +11 -14
  119. package/dist/permitted/getSnaps.d.mts.map +1 -1
  120. package/dist/permitted/getSnaps.mjs +6 -12
  121. package/dist/permitted/getSnaps.mjs.map +1 -1
  122. package/dist/permitted/getState.cjs +9 -11
  123. package/dist/permitted/getState.cjs.map +1 -1
  124. package/dist/permitted/getState.d.cts +17 -28
  125. package/dist/permitted/getState.d.cts.map +1 -1
  126. package/dist/permitted/getState.d.mts +17 -28
  127. package/dist/permitted/getState.d.mts.map +1 -1
  128. package/dist/permitted/getState.mjs +9 -11
  129. package/dist/permitted/getState.mjs.map +1 -1
  130. package/dist/permitted/getWebSockets.cjs +11 -14
  131. package/dist/permitted/getWebSockets.cjs.map +1 -1
  132. package/dist/permitted/getWebSockets.d.cts +10 -13
  133. package/dist/permitted/getWebSockets.d.cts.map +1 -1
  134. package/dist/permitted/getWebSockets.d.mts +10 -13
  135. package/dist/permitted/getWebSockets.d.mts.map +1 -1
  136. package/dist/permitted/getWebSockets.mjs +11 -14
  137. package/dist/permitted/getWebSockets.mjs.map +1 -1
  138. package/dist/permitted/index.cjs +0 -1
  139. package/dist/permitted/index.cjs.map +1 -1
  140. package/dist/permitted/index.d.cts +29 -25
  141. package/dist/permitted/index.d.cts.map +1 -1
  142. package/dist/permitted/index.d.mts +29 -25
  143. package/dist/permitted/index.d.mts.map +1 -1
  144. package/dist/permitted/index.mjs +0 -1
  145. package/dist/permitted/index.mjs.map +1 -1
  146. package/dist/permitted/invokeKeyring.cjs +12 -13
  147. package/dist/permitted/invokeKeyring.cjs.map +1 -1
  148. package/dist/permitted/invokeKeyring.d.cts +17 -16
  149. package/dist/permitted/invokeKeyring.d.cts.map +1 -1
  150. package/dist/permitted/invokeKeyring.d.mts +17 -16
  151. package/dist/permitted/invokeKeyring.d.mts.map +1 -1
  152. package/dist/permitted/invokeKeyring.mjs +12 -13
  153. package/dist/permitted/invokeKeyring.mjs.map +1 -1
  154. package/dist/permitted/invokeSnapSugar.cjs +6 -10
  155. package/dist/permitted/invokeSnapSugar.cjs.map +1 -1
  156. package/dist/permitted/invokeSnapSugar.d.cts +9 -12
  157. package/dist/permitted/invokeSnapSugar.d.cts.map +1 -1
  158. package/dist/permitted/invokeSnapSugar.d.mts +9 -12
  159. package/dist/permitted/invokeSnapSugar.d.mts.map +1 -1
  160. package/dist/permitted/invokeSnapSugar.mjs +6 -10
  161. package/dist/permitted/invokeSnapSugar.mjs.map +1 -1
  162. package/dist/permitted/listEntropySources.cjs +28 -12
  163. package/dist/permitted/listEntropySources.cjs.map +1 -1
  164. package/dist/permitted/listEntropySources.d.cts +12 -23
  165. package/dist/permitted/listEntropySources.d.cts.map +1 -1
  166. package/dist/permitted/listEntropySources.d.mts +12 -23
  167. package/dist/permitted/listEntropySources.d.mts.map +1 -1
  168. package/dist/permitted/listEntropySources.mjs +28 -12
  169. package/dist/permitted/listEntropySources.mjs.map +1 -1
  170. package/dist/permitted/middleware.cjs +70 -20
  171. package/dist/permitted/middleware.cjs.map +1 -1
  172. package/dist/permitted/middleware.d.cts +5 -2
  173. package/dist/permitted/middleware.d.cts.map +1 -1
  174. package/dist/permitted/middleware.d.mts +5 -2
  175. package/dist/permitted/middleware.d.mts.map +1 -1
  176. package/dist/permitted/middleware.mjs +70 -20
  177. package/dist/permitted/middleware.mjs.map +1 -1
  178. package/dist/permitted/openWebSocket.cjs +7 -14
  179. package/dist/permitted/openWebSocket.cjs.map +1 -1
  180. package/dist/permitted/openWebSocket.d.cts +9 -12
  181. package/dist/permitted/openWebSocket.d.cts.map +1 -1
  182. package/dist/permitted/openWebSocket.d.mts +9 -12
  183. package/dist/permitted/openWebSocket.d.mts.map +1 -1
  184. package/dist/permitted/openWebSocket.mjs +7 -14
  185. package/dist/permitted/openWebSocket.mjs.map +1 -1
  186. package/dist/permitted/requestSnaps.cjs +14 -19
  187. package/dist/permitted/requestSnaps.cjs.map +1 -1
  188. package/dist/permitted/requestSnaps.d.cts +9 -36
  189. package/dist/permitted/requestSnaps.d.cts.map +1 -1
  190. package/dist/permitted/requestSnaps.d.mts +9 -36
  191. package/dist/permitted/requestSnaps.d.mts.map +1 -1
  192. package/dist/permitted/requestSnaps.mjs +15 -20
  193. package/dist/permitted/requestSnaps.mjs.map +1 -1
  194. package/dist/permitted/resolveInterface.cjs +11 -15
  195. package/dist/permitted/resolveInterface.cjs.map +1 -1
  196. package/dist/permitted/resolveInterface.d.cts +12 -24
  197. package/dist/permitted/resolveInterface.d.cts.map +1 -1
  198. package/dist/permitted/resolveInterface.d.mts +12 -24
  199. package/dist/permitted/resolveInterface.d.mts.map +1 -1
  200. package/dist/permitted/resolveInterface.mjs +11 -15
  201. package/dist/permitted/resolveInterface.mjs.map +1 -1
  202. package/dist/permitted/scheduleBackgroundEvent.cjs +11 -14
  203. package/dist/permitted/scheduleBackgroundEvent.cjs.map +1 -1
  204. package/dist/permitted/scheduleBackgroundEvent.d.cts +10 -17
  205. package/dist/permitted/scheduleBackgroundEvent.d.cts.map +1 -1
  206. package/dist/permitted/scheduleBackgroundEvent.d.mts +10 -17
  207. package/dist/permitted/scheduleBackgroundEvent.d.mts.map +1 -1
  208. package/dist/permitted/scheduleBackgroundEvent.mjs +11 -14
  209. package/dist/permitted/scheduleBackgroundEvent.mjs.map +1 -1
  210. package/dist/permitted/sendWebSocketMessage.cjs +10 -14
  211. package/dist/permitted/sendWebSocketMessage.cjs.map +1 -1
  212. package/dist/permitted/sendWebSocketMessage.d.cts +9 -12
  213. package/dist/permitted/sendWebSocketMessage.d.cts.map +1 -1
  214. package/dist/permitted/sendWebSocketMessage.d.mts +9 -12
  215. package/dist/permitted/sendWebSocketMessage.d.mts.map +1 -1
  216. package/dist/permitted/sendWebSocketMessage.mjs +10 -14
  217. package/dist/permitted/sendWebSocketMessage.mjs.map +1 -1
  218. package/dist/permitted/setState.cjs +18 -21
  219. package/dist/permitted/setState.cjs.map +1 -1
  220. package/dist/permitted/setState.d.cts +18 -45
  221. package/dist/permitted/setState.d.cts.map +1 -1
  222. package/dist/permitted/setState.d.mts +18 -45
  223. package/dist/permitted/setState.d.mts.map +1 -1
  224. package/dist/permitted/setState.mjs +18 -21
  225. package/dist/permitted/setState.mjs.map +1 -1
  226. package/dist/permitted/startTrace.cjs +4 -6
  227. package/dist/permitted/startTrace.cjs.map +1 -1
  228. package/dist/permitted/startTrace.d.cts +8 -11
  229. package/dist/permitted/startTrace.d.cts.map +1 -1
  230. package/dist/permitted/startTrace.d.mts +8 -11
  231. package/dist/permitted/startTrace.d.mts.map +1 -1
  232. package/dist/permitted/startTrace.mjs +4 -6
  233. package/dist/permitted/startTrace.mjs.map +1 -1
  234. package/dist/permitted/trackError.cjs +4 -6
  235. package/dist/permitted/trackError.cjs.map +1 -1
  236. package/dist/permitted/trackError.d.cts +8 -11
  237. package/dist/permitted/trackError.d.cts.map +1 -1
  238. package/dist/permitted/trackError.d.mts +8 -11
  239. package/dist/permitted/trackError.d.mts.map +1 -1
  240. package/dist/permitted/trackError.mjs +4 -6
  241. package/dist/permitted/trackError.mjs.map +1 -1
  242. package/dist/permitted/trackEvent.cjs +4 -6
  243. package/dist/permitted/trackEvent.cjs.map +1 -1
  244. package/dist/permitted/trackEvent.d.cts +8 -11
  245. package/dist/permitted/trackEvent.d.cts.map +1 -1
  246. package/dist/permitted/trackEvent.d.mts +8 -11
  247. package/dist/permitted/trackEvent.d.mts.map +1 -1
  248. package/dist/permitted/trackEvent.mjs +4 -6
  249. package/dist/permitted/trackEvent.mjs.map +1 -1
  250. package/dist/permitted/updateInterface.cjs +11 -15
  251. package/dist/permitted/updateInterface.cjs.map +1 -1
  252. package/dist/permitted/updateInterface.d.cts +9 -22
  253. package/dist/permitted/updateInterface.d.cts.map +1 -1
  254. package/dist/permitted/updateInterface.d.mts +9 -22
  255. package/dist/permitted/updateInterface.d.mts.map +1 -1
  256. package/dist/permitted/updateInterface.mjs +11 -15
  257. package/dist/permitted/updateInterface.mjs.map +1 -1
  258. package/dist/restricted/dialog.cjs +23 -30
  259. package/dist/restricted/dialog.cjs.map +1 -1
  260. package/dist/restricted/dialog.d.cts +10 -58
  261. package/dist/restricted/dialog.d.cts.map +1 -1
  262. package/dist/restricted/dialog.d.mts +10 -58
  263. package/dist/restricted/dialog.d.mts.map +1 -1
  264. package/dist/restricted/dialog.mjs +23 -30
  265. package/dist/restricted/dialog.mjs.map +1 -1
  266. package/dist/restricted/getBip32Entropy.cjs +15 -12
  267. package/dist/restricted/getBip32Entropy.cjs.map +1 -1
  268. package/dist/restricted/getBip32Entropy.d.cts +11 -24
  269. package/dist/restricted/getBip32Entropy.d.cts.map +1 -1
  270. package/dist/restricted/getBip32Entropy.d.mts +11 -24
  271. package/dist/restricted/getBip32Entropy.d.mts.map +1 -1
  272. package/dist/restricted/getBip32Entropy.mjs +16 -13
  273. package/dist/restricted/getBip32Entropy.mjs.map +1 -1
  274. package/dist/restricted/getBip32PublicKey.cjs +15 -12
  275. package/dist/restricted/getBip32PublicKey.cjs.map +1 -1
  276. package/dist/restricted/getBip32PublicKey.d.cts +11 -24
  277. package/dist/restricted/getBip32PublicKey.d.cts.map +1 -1
  278. package/dist/restricted/getBip32PublicKey.d.mts +11 -24
  279. package/dist/restricted/getBip32PublicKey.d.mts.map +1 -1
  280. package/dist/restricted/getBip32PublicKey.mjs +16 -13
  281. package/dist/restricted/getBip32PublicKey.mjs.map +1 -1
  282. package/dist/restricted/getBip44Entropy.cjs +14 -10
  283. package/dist/restricted/getBip44Entropy.cjs.map +1 -1
  284. package/dist/restricted/getBip44Entropy.d.cts +11 -15
  285. package/dist/restricted/getBip44Entropy.d.cts.map +1 -1
  286. package/dist/restricted/getBip44Entropy.d.mts +11 -15
  287. package/dist/restricted/getBip44Entropy.d.mts.map +1 -1
  288. package/dist/restricted/getBip44Entropy.mjs +15 -11
  289. package/dist/restricted/getBip44Entropy.mjs.map +1 -1
  290. package/dist/restricted/getEntropy.cjs +10 -10
  291. package/dist/restricted/getEntropy.cjs.map +1 -1
  292. package/dist/restricted/getEntropy.d.cts +11 -15
  293. package/dist/restricted/getEntropy.d.cts.map +1 -1
  294. package/dist/restricted/getEntropy.d.mts +11 -15
  295. package/dist/restricted/getEntropy.d.mts.map +1 -1
  296. package/dist/restricted/getEntropy.mjs +11 -11
  297. package/dist/restricted/getEntropy.mjs.map +1 -1
  298. package/dist/restricted/index.cjs.map +1 -1
  299. package/dist/restricted/index.d.cts +20 -160
  300. package/dist/restricted/index.d.cts.map +1 -1
  301. package/dist/restricted/index.d.mts +20 -160
  302. package/dist/restricted/index.d.mts.map +1 -1
  303. package/dist/restricted/index.mjs.map +1 -1
  304. package/dist/restricted/invokeSnap.cjs +11 -12
  305. package/dist/restricted/invokeSnap.cjs.map +1 -1
  306. package/dist/restricted/invokeSnap.d.cts +9 -13
  307. package/dist/restricted/invokeSnap.d.cts.map +1 -1
  308. package/dist/restricted/invokeSnap.d.mts +9 -13
  309. package/dist/restricted/invokeSnap.d.mts.map +1 -1
  310. package/dist/restricted/invokeSnap.mjs +11 -12
  311. package/dist/restricted/invokeSnap.mjs.map +1 -1
  312. package/dist/restricted/manageState.cjs +21 -20
  313. package/dist/restricted/manageState.cjs.map +1 -1
  314. package/dist/restricted/manageState.d.cts +12 -34
  315. package/dist/restricted/manageState.d.cts.map +1 -1
  316. package/dist/restricted/manageState.d.mts +12 -34
  317. package/dist/restricted/manageState.d.mts.map +1 -1
  318. package/dist/restricted/manageState.mjs +21 -20
  319. package/dist/restricted/manageState.mjs.map +1 -1
  320. package/dist/restricted/notify.cjs +31 -24
  321. package/dist/restricted/notify.cjs.map +1 -1
  322. package/dist/restricted/notify.d.cts +16 -25
  323. package/dist/restricted/notify.d.cts.map +1 -1
  324. package/dist/restricted/notify.d.mts +16 -25
  325. package/dist/restricted/notify.d.mts.map +1 -1
  326. package/dist/restricted/notify.mjs +32 -25
  327. package/dist/restricted/notify.mjs.map +1 -1
  328. package/dist/types.cjs +3 -0
  329. package/dist/types.cjs.map +1 -0
  330. package/dist/types.d.cts +155 -0
  331. package/dist/types.d.cts.map +1 -0
  332. package/dist/types.d.mts +155 -0
  333. package/dist/types.d.mts.map +1 -0
  334. package/dist/types.mjs +2 -0
  335. package/dist/types.mjs.map +1 -0
  336. package/dist/utils.cjs +78 -24
  337. package/dist/utils.cjs.map +1 -1
  338. package/dist/utils.d.cts +21 -13
  339. package/dist/utils.d.cts.map +1 -1
  340. package/dist/utils.d.mts +21 -13
  341. package/dist/utils.d.mts.map +1 -1
  342. package/dist/utils.mjs +75 -22
  343. package/dist/utils.mjs.map +1 -1
  344. package/package.json +6 -6
  345. package/dist/permitted/handlers.cjs +0 -63
  346. package/dist/permitted/handlers.cjs.map +0 -1
  347. package/dist/permitted/handlers.d.cts +0 -299
  348. package/dist/permitted/handlers.d.cts.map +0 -1
  349. package/dist/permitted/handlers.d.mts +0 -299
  350. package/dist/permitted/handlers.d.mts.map +0 -1
  351. package/dist/permitted/handlers.mjs +0 -60
  352. package/dist/permitted/handlers.mjs.map +0 -1
@@ -1,27 +1,31 @@
1
- import type { CancelBackgroundEventMethodHooks } from "./cancelBackgroundEvent.mjs";
2
- import type { ClearStateHooks } from "./clearState.mjs";
3
- import type { CloseWebSocketMethodHooks } from "./closeWebSocket.mjs";
4
- import type { CreateInterfaceMethodHooks } from "./createInterface.mjs";
5
- import type { EndTraceMethodHooks } from "./endTrace.mjs";
6
- import type { GetAllSnapsHooks } from "./getAllSnaps.mjs";
7
- import type { GetBackgroundEventsMethodHooks } from "./getBackgroundEvents.mjs";
8
- import type { GetClientStatusHooks } from "./getClientStatus.mjs";
9
- import type { GetInterfaceStateMethodHooks } from "./getInterfaceState.mjs";
10
- import type { GetSnapsHooks } from "./getSnaps.mjs";
11
- import type { GetStateHooks } from "./getState.mjs";
12
- import type { GetWebSocketsMethodHooks } from "./getWebSockets.mjs";
13
- import type { ListEntropySourcesHooks } from "./listEntropySources.mjs";
14
- import type { OpenWebSocketMethodHooks } from "./openWebSocket.mjs";
15
- import type { RequestSnapsHooks } from "./requestSnaps.mjs";
16
- import type { ResolveInterfaceMethodHooks } from "./resolveInterface.mjs";
17
- import type { ScheduleBackgroundEventMethodHooks } from "./scheduleBackgroundEvent.mjs";
18
- import type { SendWebSocketMessageMethodHooks } from "./sendWebSocketMessage.mjs";
19
- import type { SetStateHooks } from "./setState.mjs";
20
- import type { StartTraceMethodHooks } from "./startTrace.mjs";
21
- import type { TrackErrorMethodHooks } from "./trackError.mjs";
22
- import type { TrackEventMethodHooks } from "./trackEvent.mjs";
23
- import type { UpdateInterfaceMethodHooks } from "./updateInterface.mjs";
24
- export type PermittedRpcMethodHooks = ClearStateHooks & GetAllSnapsHooks & GetClientStatusHooks & GetSnapsHooks & GetStateHooks & ListEntropySourcesHooks & RequestSnapsHooks & CreateInterfaceMethodHooks & UpdateInterfaceMethodHooks & GetInterfaceStateMethodHooks & ResolveInterfaceMethodHooks & ScheduleBackgroundEventMethodHooks & CancelBackgroundEventMethodHooks & GetBackgroundEventsMethodHooks & SetStateHooks & OpenWebSocketMethodHooks & CloseWebSocketMethodHooks & SendWebSocketMessageMethodHooks & GetWebSocketsMethodHooks & TrackEventMethodHooks & TrackErrorMethodHooks & StartTraceMethodHooks & EndTraceMethodHooks;
25
- export * from "./handlers.mjs";
1
+ import type { CancelBackgroundEventMethodActions } from "./cancelBackgroundEvent.mjs";
2
+ import type { ClearStateMethodActions } from "./clearState.mjs";
3
+ import type { CloseWebSocketMethodActions } from "./closeWebSocket.mjs";
4
+ import type { CreateInterfaceMethodActions } from "./createInterface.mjs";
5
+ import type { EndTraceMethodActions, EndTraceMethodHooks } from "./endTrace.mjs";
6
+ import type { GetAllSnapsMethodActions } from "./getAllSnaps.mjs";
7
+ import type { GetBackgroundEventsMethodActions } from "./getBackgroundEvents.mjs";
8
+ import type { GetClientStatusMethodActions, GetClientStatusMethodHooks } from "./getClientStatus.mjs";
9
+ import type { GetFileMethodActions } from "./getFile.mjs";
10
+ import type { GetInterfaceContextMethodActions } from "./getInterfaceContext.mjs";
11
+ import type { GetInterfaceStateMethodActions } from "./getInterfaceState.mjs";
12
+ import type { GetSnapsMethodActions } from "./getSnaps.mjs";
13
+ import type { GetStateMethodActions, GetStateMethodHooks } from "./getState.mjs";
14
+ import type { GetWebSocketsMethodActions } from "./getWebSockets.mjs";
15
+ import type { InvokeKeyringMethodActions, InvokeKeyringMethodHooks } from "./invokeKeyring.mjs";
16
+ import type { InvokeSnapSugarMethodActions } from "./invokeSnapSugar.mjs";
17
+ import type { ListEntropySourcesMethodActions, ListEntropySourcesMethodHooks } from "./listEntropySources.mjs";
18
+ import type { OpenWebSocketMethodActions } from "./openWebSocket.mjs";
19
+ import type { RequestSnapsMethodActions } from "./requestSnaps.mjs";
20
+ import type { ResolveInterfaceMethodActions } from "./resolveInterface.mjs";
21
+ import type { ScheduleBackgroundEventMethodActions } from "./scheduleBackgroundEvent.mjs";
22
+ import type { SendWebSocketMessageMethodActions } from "./sendWebSocketMessage.mjs";
23
+ import type { SetStateMethodActions, SetStateMethodHooks } from "./setState.mjs";
24
+ import type { StartTraceMethodActions, StartTraceMethodHooks } from "./startTrace.mjs";
25
+ import type { TrackErrorMethodActions, TrackErrorMethodHooks } from "./trackError.mjs";
26
+ import type { TrackEventMethodActions, TrackEventMethodHooks } from "./trackEvent.mjs";
27
+ import type { UpdateInterfaceMethodActions } from "./updateInterface.mjs";
28
+ export type PermittedRpcMethodActions = CancelBackgroundEventMethodActions | ClearStateMethodActions | CloseWebSocketMethodActions | CreateInterfaceMethodActions | EndTraceMethodActions | GetAllSnapsMethodActions | GetBackgroundEventsMethodActions | GetClientStatusMethodActions | GetFileMethodActions | GetInterfaceContextMethodActions | GetInterfaceStateMethodActions | GetSnapsMethodActions | GetStateMethodActions | GetWebSocketsMethodActions | InvokeKeyringMethodActions | InvokeSnapSugarMethodActions | ListEntropySourcesMethodActions | OpenWebSocketMethodActions | RequestSnapsMethodActions | ResolveInterfaceMethodActions | ScheduleBackgroundEventMethodActions | SendWebSocketMessageMethodActions | SetStateMethodActions | StartTraceMethodActions | TrackErrorMethodActions | TrackEventMethodActions | UpdateInterfaceMethodActions;
29
+ export type PermittedRpcMethodHooks = GetClientStatusMethodHooks & GetStateMethodHooks & ListEntropySourcesMethodHooks & SetStateMethodHooks & TrackEventMethodHooks & TrackErrorMethodHooks & StartTraceMethodHooks & EndTraceMethodHooks & InvokeKeyringMethodHooks;
26
30
  export * from "./middleware.mjs";
27
31
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/permitted/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gCAAgC,EAAE,oCAAgC;AAChF,OAAO,KAAK,EAAE,eAAe,EAAE,yBAAqB;AACpD,OAAO,KAAK,EAAE,yBAAyB,EAAE,6BAAyB;AAClE,OAAO,KAAK,EAAE,0BAA0B,EAAE,8BAA0B;AACpE,OAAO,KAAK,EAAE,mBAAmB,EAAE,uBAAmB;AACtD,OAAO,KAAK,EAAE,gBAAgB,EAAE,0BAAsB;AACtD,OAAO,KAAK,EAAE,8BAA8B,EAAE,kCAA8B;AAC5E,OAAO,KAAK,EAAE,oBAAoB,EAAE,8BAA0B;AAC9D,OAAO,KAAK,EAAE,4BAA4B,EAAE,gCAA4B;AACxE,OAAO,KAAK,EAAE,aAAa,EAAE,uBAAmB;AAChD,OAAO,KAAK,EAA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tE,MAAM,MAAM,yBAAyB,GACjC,kCAAkC,GAClC,uBAAuB,GACvB,2BAA2B,GAC3B,4BAA4B,GAC5B,qBAAqB,GACrB,wBAAwB,GACxB,gCAAgC,GAChC,4BAA4B,GAC5B,oBAAoB,GACpB,gCAAgC,GAChC,8BAA8B,GAC9B,qBAAqB,GACrB,qBAAqB,GACrB,0BAA0B,GAC1B,0BAA0B,GAC1B,4BAA4B,GAC5B,+BAA+B,GAC/B,0BAA0B,GAC1B,yBAAyB,GACzB,6BAA6B,GAC7B,oCAAoC,GACpC,iCAAiC,GACjC,qBAAqB,GACrB,uBAAuB,GACvB,uBAAuB,GACvB,uBAAuB,GACvB,4BAA4B,CAAC;AAEjC,MAAM,MAAM,uBAAuB,GAAG,0BAA0B,GAC9D,mBAAmB,GACnB,6BAA6B,GAC7B,mBAAmB,GACnB,qBAAqB,GACrB,qBAAqB,GACrB,qBAAqB,GACrB,mBAAmB,GACnB,wBAAwB,CAAC;AAE3B,iCAA6B"}
@@ -1,3 +1,2 @@
1
- export * from "./handlers.mjs";
2
1
  export * from "./middleware.mjs";
3
2
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/permitted/index.ts"],"names":[],"mappings":"AAgDA,+BAA2B;AAC3B,iCAA6B","sourcesContent":["import type { CancelBackgroundEventMethodHooks } from './cancelBackgroundEvent';\nimport type { ClearStateHooks } from './clearState';\nimport type { CloseWebSocketMethodHooks } from './closeWebSocket';\nimport type { CreateInterfaceMethodHooks } from './createInterface';\nimport type { EndTraceMethodHooks } from './endTrace';\nimport type { GetAllSnapsHooks } from './getAllSnaps';\nimport type { GetBackgroundEventsMethodHooks } from './getBackgroundEvents';\nimport type { GetClientStatusHooks } from './getClientStatus';\nimport type { GetInterfaceStateMethodHooks } from './getInterfaceState';\nimport type { GetSnapsHooks } from './getSnaps';\nimport type { GetStateHooks } from './getState';\nimport type { GetWebSocketsMethodHooks } from './getWebSockets';\nimport type { ListEntropySourcesHooks } from './listEntropySources';\nimport type { OpenWebSocketMethodHooks } from './openWebSocket';\nimport type { RequestSnapsHooks } from './requestSnaps';\nimport type { ResolveInterfaceMethodHooks } from './resolveInterface';\nimport type { ScheduleBackgroundEventMethodHooks } from './scheduleBackgroundEvent';\nimport type { SendWebSocketMessageMethodHooks } from './sendWebSocketMessage';\nimport type { SetStateHooks } from './setState';\nimport type { StartTraceMethodHooks } from './startTrace';\nimport type { TrackErrorMethodHooks } from './trackError';\nimport type { TrackEventMethodHooks } from './trackEvent';\nimport type { UpdateInterfaceMethodHooks } from './updateInterface';\n\nexport type PermittedRpcMethodHooks = ClearStateHooks &\n GetAllSnapsHooks &\n GetClientStatusHooks &\n GetSnapsHooks &\n GetStateHooks &\n ListEntropySourcesHooks &\n RequestSnapsHooks &\n CreateInterfaceMethodHooks &\n UpdateInterfaceMethodHooks &\n GetInterfaceStateMethodHooks &\n ResolveInterfaceMethodHooks &\n ScheduleBackgroundEventMethodHooks &\n CancelBackgroundEventMethodHooks &\n GetBackgroundEventsMethodHooks &\n SetStateHooks &\n OpenWebSocketMethodHooks &\n CloseWebSocketMethodHooks &\n SendWebSocketMessageMethodHooks &\n GetWebSocketsMethodHooks &\n TrackEventMethodHooks &\n TrackErrorMethodHooks &\n StartTraceMethodHooks &\n EndTraceMethodHooks;\n\nexport * from './handlers';\nexport * from './middleware';\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/permitted/index.ts"],"names":[],"mappings":"AAqFA,iCAA6B","sourcesContent":["import type { CancelBackgroundEventMethodActions } from './cancelBackgroundEvent';\nimport type { ClearStateMethodActions } from './clearState';\nimport type { CloseWebSocketMethodActions } from './closeWebSocket';\nimport type { CreateInterfaceMethodActions } from './createInterface';\nimport type { EndTraceMethodActions, EndTraceMethodHooks } from './endTrace';\nimport type { GetAllSnapsMethodActions } from './getAllSnaps';\nimport type { GetBackgroundEventsMethodActions } from './getBackgroundEvents';\nimport type {\n GetClientStatusMethodActions,\n GetClientStatusMethodHooks,\n} from './getClientStatus';\nimport type { GetFileMethodActions } from './getFile';\nimport type { GetInterfaceContextMethodActions } from './getInterfaceContext';\nimport type { GetInterfaceStateMethodActions } from './getInterfaceState';\nimport type { GetSnapsMethodActions } from './getSnaps';\nimport type { GetStateMethodActions, GetStateMethodHooks } from './getState';\nimport type { GetWebSocketsMethodActions } from './getWebSockets';\nimport type {\n InvokeKeyringMethodActions,\n InvokeKeyringMethodHooks,\n} from './invokeKeyring';\nimport type { InvokeSnapSugarMethodActions } from './invokeSnapSugar';\nimport type {\n ListEntropySourcesMethodActions,\n ListEntropySourcesMethodHooks,\n} from './listEntropySources';\nimport type { OpenWebSocketMethodActions } from './openWebSocket';\nimport type { RequestSnapsMethodActions } from './requestSnaps';\nimport type { ResolveInterfaceMethodActions } from './resolveInterface';\nimport type { ScheduleBackgroundEventMethodActions } from './scheduleBackgroundEvent';\nimport type { SendWebSocketMessageMethodActions } from './sendWebSocketMessage';\nimport type { SetStateMethodActions, SetStateMethodHooks } from './setState';\nimport type {\n StartTraceMethodActions,\n StartTraceMethodHooks,\n} from './startTrace';\nimport type {\n TrackErrorMethodActions,\n TrackErrorMethodHooks,\n} from './trackError';\nimport type {\n TrackEventMethodActions,\n TrackEventMethodHooks,\n} from './trackEvent';\nimport type { UpdateInterfaceMethodActions } from './updateInterface';\n\nexport type PermittedRpcMethodActions =\n | CancelBackgroundEventMethodActions\n | ClearStateMethodActions\n | CloseWebSocketMethodActions\n | CreateInterfaceMethodActions\n | EndTraceMethodActions\n | GetAllSnapsMethodActions\n | GetBackgroundEventsMethodActions\n | GetClientStatusMethodActions\n | GetFileMethodActions\n | GetInterfaceContextMethodActions\n | GetInterfaceStateMethodActions\n | GetSnapsMethodActions\n | GetStateMethodActions\n | GetWebSocketsMethodActions\n | InvokeKeyringMethodActions\n | InvokeSnapSugarMethodActions\n | ListEntropySourcesMethodActions\n | OpenWebSocketMethodActions\n | RequestSnapsMethodActions\n | ResolveInterfaceMethodActions\n | ScheduleBackgroundEventMethodActions\n | SendWebSocketMessageMethodActions\n | SetStateMethodActions\n | StartTraceMethodActions\n | TrackErrorMethodActions\n | TrackEventMethodActions\n | UpdateInterfaceMethodActions;\n\nexport type PermittedRpcMethodHooks = GetClientStatusMethodHooks &\n GetStateMethodHooks &\n ListEntropySourcesMethodHooks &\n SetStateMethodHooks &\n TrackEventMethodHooks &\n TrackErrorMethodHooks &\n StartTraceMethodHooks &\n EndTraceMethodHooks &\n InvokeKeyringMethodHooks;\n\nexport * from './middleware';\n"]}
@@ -5,11 +5,7 @@ const rpc_errors_1 = require("@metamask/rpc-errors");
5
5
  const snaps_utils_1 = require("@metamask/snaps-utils");
6
6
  const utils_1 = require("@metamask/utils");
7
7
  const invokeSnapSugar_1 = require("./invokeSnapSugar.cjs");
8
- const methodName = 'wallet_invokeKeyring';
9
8
  const hookNames = {
10
- hasPermission: true,
11
- handleSnapRpcRequest: true,
12
- getSnap: true,
13
9
  getAllowedKeyringMethods: true,
14
10
  };
15
11
  /**
@@ -21,9 +17,13 @@ const hookNames = {
21
17
  * request permission to communicate with it using [`wallet_requestSnaps`](https://docs.metamask.io/snaps/reference/snaps-api/wallet_requestsnaps).
22
18
  */
23
19
  exports.invokeKeyringHandler = {
24
- methodNames: [methodName],
25
20
  implementation: invokeKeyringImplementation,
26
21
  hookNames,
22
+ actionNames: [
23
+ 'PermissionController:hasPermission',
24
+ 'SnapController:handleRequest',
25
+ 'SnapController:getSnap',
26
+ ],
27
27
  };
28
28
  /**
29
29
  * The `wallet_invokeKeyring` method implementation.
@@ -35,11 +35,9 @@ exports.invokeKeyringHandler = {
35
35
  * function.
36
36
  * @param end - The `json-rpc-engine` "end" callback.
37
37
  * @param hooks - The RPC method hooks.
38
- * @param hooks.handleSnapRpcRequest - Invokes a snap with a given RPC request.
39
- * @param hooks.hasPermission - Checks whether a given origin has a given permission.
40
- * @param hooks.getSnap - Gets information about a given snap.
41
38
  * @param hooks.getAllowedKeyringMethods - Get the list of allowed Keyring
42
39
  * methods for a given origin.
40
+ * @param messenger - The messenger used to call controller actions.
43
41
  * @returns Nothing.
44
42
  */
45
43
  async function invokeKeyringImplementation(req,
@@ -48,7 +46,7 @@ async function invokeKeyringImplementation(req,
48
46
  // future. We use `InvokeKeyringResult` here to make it clear that this is the
49
47
  // expected type of the result.
50
48
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments
51
- res, _next, end, { handleSnapRpcRequest, hasPermission, getSnap, getAllowedKeyringMethods, }) {
49
+ res, _next, end, { getAllowedKeyringMethods }, messenger) {
52
50
  let params;
53
51
  try {
54
52
  params = (0, invokeSnapSugar_1.getValidatedParams)(req.params);
@@ -56,15 +54,15 @@ res, _next, end, { handleSnapRpcRequest, hasPermission, getSnap, getAllowedKeyri
56
54
  catch (error) {
57
55
  return end(error);
58
56
  }
59
- // We expect the MM middleware stack to always add the origin to requests
60
57
  const { origin } = req;
61
58
  const { snapId, request } = params;
62
- if (!origin || !hasPermission(snaps_utils_1.WALLET_SNAP_PERMISSION_KEY)) {
59
+ if (!origin ||
60
+ !messenger.call('PermissionController:hasPermission', origin, snaps_utils_1.WALLET_SNAP_PERMISSION_KEY)) {
63
61
  return end(rpc_errors_1.rpcErrors.invalidRequest({
64
62
  message: `The snap "${snapId}" is not connected to "${origin}". Please connect before invoking the snap.`,
65
63
  }));
66
64
  }
67
- if (!getSnap(snapId)) {
65
+ if (!messenger.call('SnapController:getSnap', snapId)) {
68
66
  return end(
69
67
  // Mirror error message from SnapController.
70
68
  rpc_errors_1.rpcErrors.invalidRequest({
@@ -83,7 +81,8 @@ res, _next, end, { handleSnapRpcRequest, hasPermission, getSnap, getAllowedKeyri
83
81
  }));
84
82
  }
85
83
  try {
86
- res.result = (await handleSnapRpcRequest({
84
+ res.result = (await messenger.call('SnapController:handleRequest', {
85
+ origin,
87
86
  snapId,
88
87
  request,
89
88
  handler: snaps_utils_1.HandlerType.OnKeyringRequest,
@@ -1 +1 @@
1
- {"version":3,"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J,CAAC;IAED,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QACrB,OAAO,GAAG;QACR,4CAA4C;QAC5C,sBAAS,CAAC,cAAc,CAAC;YACvB,OAAO,EAAE,aAAa,MAAM,2DAA2D;SACxF,CAAC,CACH,CAAC;IACJ,CAAC;IAED,IAAI,CAAC,IAAA,mBAAW,EAAC,OAAO,EAAE,QAAQ,CAAC,IAAI,OAAO,OAAO,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC1E,OAAO,GAAG,CACR,sBAAS,CAAC,cAAc,CAAC;YACvB,OAAO,EAAE,iCAAiC;SAC3C,CAAC,CACH,CAAC;IACJ,CAAC;IAED,MAAM,cAAc,GAAG,wBAAwB,EAAE,CAAC;IAClD,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAC7C,OAAO,GAAG,CACR,sBAAS,CAAC,cAAc,CAAC;YACvB,OAAO,EAAE,eAAe,MAAM,0CAA0C,OAAO,CAAC,MAAM,IAAI;SAC3F,CAAC,CACH,CAAC;IACJ,CAAC;IAED,IAAI,CAAC;QACH,GAAG,CAAC,MAAM,GAAG,CAAC,MAAM,oBAAoB,CAAC;YACvC,MAAM;YACN,OAAO;YACP,OAAO,EAAE,yBAAW,CAAC,gBAAgB;SACtC,CAAC,CAAS,CAAC;IACd,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,OAAO,GAAG,EAAE,CAAC;AACf,CAAC","sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n InvokeKeyringParams,\n InvokeKeyringResult,\n InvokeSnapParams,\n} from '@metamask/snaps-sdk';\nimport type { Snap, SnapRpcHookArgs } from '@metamask/snaps-utils';\nimport { HandlerType, WALLET_SNAP_PERMISSION_KEY } from '@metamask/snaps-utils';\nimport type {\n PendingJsonRpcResponse,\n Json,\n JsonRpcRequest,\n} from '@metamask/utils';\nimport { hasProperty } from '@metamask/utils';\n\nimport { getValidatedParams } from './invokeSnapSugar';\nimport type { MethodHooksObject } from '../utils';\n\nconst methodName = 'wallet_invokeKeyring';\n\nconst hookNames: MethodHooksObject<InvokeKeyringHooks> = {\n hasPermission: true,\n handleSnapRpcRequest: true,\n getSnap: true,\n getAllowedKeyringMethods: true,\n};\n\n/**\n * Invoke a keyring method of a Snap. This calls the `onKeyringRequest` handler\n * of the Snap.\n *\n * The Snap must be installed and the dapp must have permission to communicate\n * with the Snap, or the request is rejected. The dapp can install the Snap and\n * request permission to communicate with it using [`wallet_requestSnaps`](https://docs.metamask.io/snaps/reference/snaps-api/wallet_requestsnaps).\n */\nexport const invokeKeyringHandler = {\n methodNames: [methodName] as const,\n implementation: invokeKeyringImplementation,\n hookNames,\n} satisfies PermittedHandlerExport<\n InvokeKeyringHooks,\n InvokeSnapParams,\n InvokeKeyringResult\n>;\n\nexport type InvokeKeyringHooks = {\n hasPermission: (permissionName: string) => boolean;\n\n handleSnapRpcRequest: ({\n snapId,\n handler,\n request,\n }: Omit<SnapRpcHookArgs, 'origin'> & { snapId: string }) => Promise<unknown>;\n\n getSnap: (snapId: string) => Snap | undefined;\n\n getAllowedKeyringMethods: () => string[];\n};\n\n/**\n * The `wallet_invokeKeyring` method implementation.\n * Invokes onKeyringRequest if the snap requested is installed and connected to the dapp.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.handleSnapRpcRequest - Invokes a snap with a given RPC request.\n * @param hooks.hasPermission - Checks whether a given origin has a given permission.\n * @param hooks.getSnap - Gets information about a given snap.\n * @param hooks.getAllowedKeyringMethods - Get the list of allowed Keyring\n * methods for a given origin.\n * @returns Nothing.\n */\nasync function invokeKeyringImplementation(\n req: JsonRpcRequest<InvokeKeyringParams>,\n // `InvokeKeyringResult` is an alias for `Json` (which is the default type\n // argument for `PendingJsonRpcResponse`), but that may not be the case in the\n // future. We use `InvokeKeyringResult` here to make it clear that this is the\n // expected type of the result.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments\n res: PendingJsonRpcResponse<InvokeKeyringResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n {\n handleSnapRpcRequest,\n hasPermission,\n getSnap,\n getAllowedKeyringMethods,\n }: InvokeKeyringHooks,\n): Promise<void> {\n let params: InvokeSnapParams;\n try {\n params = getValidatedParams(req.params);\n } catch (error) {\n return end(error);\n }\n\n // We expect the MM middleware stack to always add the origin to requests\n const { origin } = req as JsonRpcRequest & { origin: string };\n const { snapId, request } = params;\n\n if (!origin || !hasPermission(WALLET_SNAP_PERMISSION_KEY)) {\n return end(\n rpcErrors.invalidRequest({\n message: `The snap \"${snapId}\" is not connected to \"${origin}\". Please connect before invoking the snap.`,\n }),\n );\n }\n\n if (!getSnap(snapId)) {\n return end(\n // Mirror error message from SnapController.\n rpcErrors.invalidRequest({\n message: `The Snap \"${snapId}\" is not installed. Please install it before invoking it.`,\n }),\n );\n }\n\n if (!hasProperty(request, 'method') || typeof request.method !== 'string') {\n return end(\n rpcErrors.invalidRequest({\n message: 'The request must have a method.',\n }),\n );\n }\n\n const allowedMethods = getAllowedKeyringMethods();\n if (!allowedMethods.includes(request.method)) {\n return end(\n rpcErrors.invalidRequest({\n message: `The origin \"${origin}\" is not allowed to invoke the method \"${request.method}\".`,\n }),\n );\n }\n\n try {\n res.result = (await handleSnapRpcRequest({\n snapId,\n request,\n handler: HandlerType.OnKeyringRequest,\n })) as Json;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n"]}
1
+ {"version":3,"file":"invokeKeyring.cjs","sourceRoot":"","sources":["../../src/permitted/invokeKeyring.ts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CAAC;YACvB,OAAO,EAAE,aAAa,MAAM,2DAA2D;SACxF,CAAC,CACH,CAAC;IACJ,CAAC;IAED,IAAI,CAAC,IAAA,mBAAW,EAAC,OAAO,EAAE,QAAQ,CAAC,IAAI,OAAO,OAAO,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC1E,OAAO,GAAG,CACR,sBAAS,CAAC,cAAc,CAAC;YACvB,OAAO,EAAE,iCAAiC;SAC3C,CAAC,CACH,CAAC;IACJ,CAAC;IAED,MAAM,cAAc,GAAG,wBAAwB,EAAE,CAAC;IAClD,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAC7C,OAAO,GAAG,CACR,sBAAS,CAAC,cAAc,CAAC;YACvB,OAAO,EAAE,eAAe,MAAM,0CAA0C,OAAO,CAAC,MAAM,IAAI;SAC3F,CAAC,CACH,CAAC;IACJ,CAAC;IAED,IAAI,CAAC;QACH,GAAG,CAAC,MAAM,GAAG,CAAC,MAAM,SAAS,CAAC,IAAI,CAAC,8BAA8B,EAAE;YACjE,MAAM;YACN,MAAM;YACN,OAAO;YACP,OAAO,EAAE,yBAAW,CAAC,gBAAgB;SACtC,CAAC,CAAS,CAAC;IACd,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,OAAO,GAAG,EAAE,CAAC;AACf,CAAC","sourcesContent":["import type {\n JsonRpcEngineEndCallback,\n MethodHandler,\n} from '@metamask/json-rpc-engine';\nimport type { Messenger } from '@metamask/messenger';\nimport type { PermissionControllerHasPermissionAction } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n InvokeKeyringParams,\n InvokeKeyringResult,\n InvokeSnapParams,\n} from '@metamask/snaps-sdk';\nimport { HandlerType, WALLET_SNAP_PERMISSION_KEY } from '@metamask/snaps-utils';\nimport type { PendingJsonRpcResponse, Json } from '@metamask/utils';\nimport { hasProperty } from '@metamask/utils';\n\nimport { getValidatedParams } from './invokeSnapSugar';\nimport type {\n JsonRpcRequestWithOrigin,\n SnapControllerGetSnapAction,\n SnapControllerHandleRequestAction,\n} from '../types';\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<InvokeKeyringMethodHooks> = {\n getAllowedKeyringMethods: true,\n};\n\nexport type InvokeKeyringMethodHooks = {\n /**\n * Get the list of allowed Keyring methods for a given origin.\n *\n * @returns The list of allowed Keyring methods.\n */\n getAllowedKeyringMethods: () => string[];\n};\n\nexport type InvokeKeyringMethodActions =\n | PermissionControllerHasPermissionAction\n | SnapControllerHandleRequestAction\n | SnapControllerGetSnapAction;\n\n/**\n * Invoke a keyring method of a Snap. This calls the `onKeyringRequest` handler\n * of the Snap.\n *\n * The Snap must be installed and the dapp must have permission to communicate\n * with the Snap, or the request is rejected. The dapp can install the Snap and\n * request permission to communicate with it using [`wallet_requestSnaps`](https://docs.metamask.io/snaps/reference/snaps-api/wallet_requestsnaps).\n */\nexport const invokeKeyringHandler = {\n implementation: invokeKeyringImplementation,\n hookNames,\n actionNames: [\n 'PermissionController:hasPermission',\n 'SnapController:handleRequest',\n 'SnapController:getSnap',\n ],\n} satisfies MethodHandler<\n InvokeKeyringMethodHooks,\n InvokeKeyringMethodActions,\n InvokeSnapParams,\n InvokeKeyringResult,\n { origin: string }\n>;\n\n/**\n * The `wallet_invokeKeyring` method implementation.\n * Invokes onKeyringRequest if the snap requested is installed and connected to the dapp.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.getAllowedKeyringMethods - Get the list of allowed Keyring\n * methods for a given origin.\n * @param messenger - The messenger used to call controller actions.\n * @returns Nothing.\n */\nasync function invokeKeyringImplementation(\n req: JsonRpcRequestWithOrigin<InvokeKeyringParams>,\n // `InvokeKeyringResult` is an alias for `Json` (which is the default type\n // argument for `PendingJsonRpcResponse`), but that may not be the case in the\n // future. We use `InvokeKeyringResult` here to make it clear that this is the\n // expected type of the result.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments\n res: PendingJsonRpcResponse<InvokeKeyringResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { getAllowedKeyringMethods }: InvokeKeyringMethodHooks,\n messenger: Messenger<string, InvokeKeyringMethodActions>,\n): Promise<void> {\n let params: InvokeSnapParams;\n try {\n params = getValidatedParams(req.params);\n } catch (error) {\n return end(error);\n }\n\n const { origin } = req;\n const { snapId, request } = params;\n\n if (\n !origin ||\n !messenger.call(\n 'PermissionController:hasPermission',\n origin,\n WALLET_SNAP_PERMISSION_KEY,\n )\n ) {\n return end(\n rpcErrors.invalidRequest({\n message: `The snap \"${snapId}\" is not connected to \"${origin}\". Please connect before invoking the snap.`,\n }),\n );\n }\n\n if (!messenger.call('SnapController:getSnap', snapId)) {\n return end(\n // Mirror error message from SnapController.\n rpcErrors.invalidRequest({\n message: `The Snap \"${snapId}\" is not installed. Please install it before invoking it.`,\n }),\n );\n }\n\n if (!hasProperty(request, 'method') || typeof request.method !== 'string') {\n return end(\n rpcErrors.invalidRequest({\n message: 'The request must have a method.',\n }),\n );\n }\n\n const allowedMethods = getAllowedKeyringMethods();\n if (!allowedMethods.includes(request.method)) {\n return end(\n rpcErrors.invalidRequest({\n message: `The origin \"${origin}\" is not allowed to invoke the method \"${request.method}\".`,\n }),\n );\n }\n\n try {\n res.result = (await messenger.call('SnapController:handleRequest', {\n origin,\n snapId,\n request,\n handler: HandlerType.OnKeyringRequest,\n })) as Json;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n"]}
@@ -1,8 +1,19 @@
1
1
  import type { JsonRpcEngineEndCallback } from "@metamask/json-rpc-engine";
2
+ import type { Messenger } from "@metamask/messenger";
3
+ import type { PermissionControllerHasPermissionAction } from "@metamask/permission-controller";
2
4
  import type { InvokeKeyringParams, InvokeKeyringResult } from "@metamask/snaps-sdk";
3
- import type { Snap, SnapRpcHookArgs } from "@metamask/snaps-utils";
4
- import type { PendingJsonRpcResponse, JsonRpcRequest } from "@metamask/utils";
5
+ import type { PendingJsonRpcResponse } from "@metamask/utils";
6
+ import type { JsonRpcRequestWithOrigin, SnapControllerGetSnapAction, SnapControllerHandleRequestAction } from "../types.cjs";
5
7
  import type { MethodHooksObject } from "../utils.cjs";
8
+ export type InvokeKeyringMethodHooks = {
9
+ /**
10
+ * Get the list of allowed Keyring methods for a given origin.
11
+ *
12
+ * @returns The list of allowed Keyring methods.
13
+ */
14
+ getAllowedKeyringMethods: () => string[];
15
+ };
16
+ export type InvokeKeyringMethodActions = PermissionControllerHasPermissionAction | SnapControllerHandleRequestAction | SnapControllerGetSnapAction;
6
17
  /**
7
18
  * Invoke a keyring method of a Snap. This calls the `onKeyringRequest` handler
8
19
  * of the Snap.
@@ -12,17 +23,9 @@ import type { MethodHooksObject } from "../utils.cjs";
12
23
  * request permission to communicate with it using [`wallet_requestSnaps`](https://docs.metamask.io/snaps/reference/snaps-api/wallet_requestsnaps).
13
24
  */
14
25
  export declare const invokeKeyringHandler: {
15
- methodNames: ["wallet_invokeKeyring"];
16
26
  implementation: typeof invokeKeyringImplementation;
17
- hookNames: MethodHooksObject<InvokeKeyringHooks>;
18
- };
19
- export type InvokeKeyringHooks = {
20
- hasPermission: (permissionName: string) => boolean;
21
- handleSnapRpcRequest: ({ snapId, handler, request, }: Omit<SnapRpcHookArgs, 'origin'> & {
22
- snapId: string;
23
- }) => Promise<unknown>;
24
- getSnap: (snapId: string) => Snap | undefined;
25
- getAllowedKeyringMethods: () => string[];
27
+ hookNames: MethodHooksObject<InvokeKeyringMethodHooks>;
28
+ actionNames: ("SnapController:handleRequest" | "SnapController:getSnap" | "PermissionController:hasPermission")[];
26
29
  };
27
30
  /**
28
31
  * The `wallet_invokeKeyring` method implementation.
@@ -34,13 +37,11 @@ export type InvokeKeyringHooks = {
34
37
  * function.
35
38
  * @param end - The `json-rpc-engine` "end" callback.
36
39
  * @param hooks - The RPC method hooks.
37
- * @param hooks.handleSnapRpcRequest - Invokes a snap with a given RPC request.
38
- * @param hooks.hasPermission - Checks whether a given origin has a given permission.
39
- * @param hooks.getSnap - Gets information about a given snap.
40
40
  * @param hooks.getAllowedKeyringMethods - Get the list of allowed Keyring
41
41
  * methods for a given origin.
42
+ * @param messenger - The messenger used to call controller actions.
42
43
  * @returns Nothing.
43
44
  */
44
- declare function invokeKeyringImplementation(req: JsonRpcRequest<InvokeKeyringParams>, res: PendingJsonRpcResponse<InvokeKeyringResult>, _next: unknown, end: JsonRpcEngineEndCallback, { handleSnapRpcRequest, hasPermission, getSnap, getAllowedKeyringMethods, }: InvokeKeyringHooks): Promise<void>;
45
+ declare function invokeKeyringImplementation(req: JsonRpcRequestWithOrigin<InvokeKeyringParams>, res: PendingJsonRpcResponse<InvokeKeyringResult>, _next: unknown, end: JsonRpcEngineEndCallback, { getAllowedKeyringMethods }: InvokeKeyringMethodHooks, messenger: Messenger<string, InvokeKeyringMethodActions>): Promise<void>;
45
46
  export {};
46
47
  //# sourceMappingURL=invokeKeyring.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"invokeKeyring.d.cts","sourceRoot":"","sources":["../../src/permitted/invokeKeyring.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,kCAAkC;AAG1E,OAAO,KAAK,EACV,mBAAmB,EACnB,mBAAmB,EAEpB,4BAA4B;AAC7B,OAAO,KAAK,EAAE,IAAI,EAAE,eAAe,EAAE,8BAA8B;AAEnE,OAAO,KAAK,EACV,sBAAsB,EAEtB,cAAc,EACf,wBAAwB;AAIzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAiB;AAWlD;;;;;;;GAOG;AACH,eAAO,MAAM,oBAAoB;;;;CAQhC,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,aAAa,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AC1C,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAClC,uCAAuC,GACvC,iCAAiC,GACjC,2BAA2B,CAAC;AAEhC;;;;;;;GAOG;AACH,eAAO,MAAM,oBAAoB;;;;CAchC,CAAC;AAEF;;;;;;;;;;;;;;GAcG;AACH,iBAAe,2BAA2B,CACxC,GAAG,EAAE,wBAAwB,CAAC,mBAAmB,CAAC,EAMlD,GAAG,EAAE,sBAAsB,CAAC,mBAAmB,CAAC,EAChD,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,wBAAwB,EAC7B,EAAE,wBAAwB,EAAE,EAAE,wBAAwB,EACtD,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,0BAA0B,CAAC,GACvD,OAAO,CAAC,IAAI,CAAC,CAgEf"}
@@ -1,8 +1,19 @@
1
1
  import type { JsonRpcEngineEndCallback } from "@metamask/json-rpc-engine";
2
+ import type { Messenger } from "@metamask/messenger";
3
+ import type { PermissionControllerHasPermissionAction } from "@metamask/permission-controller";
2
4
  import type { InvokeKeyringParams, InvokeKeyringResult } from "@metamask/snaps-sdk";
3
- import type { Snap, SnapRpcHookArgs } from "@metamask/snaps-utils";
4
- import type { PendingJsonRpcResponse, JsonRpcRequest } from "@metamask/utils";
5
+ import type { PendingJsonRpcResponse } from "@metamask/utils";
6
+ import type { JsonRpcRequestWithOrigin, SnapControllerGetSnapAction, SnapControllerHandleRequestAction } from "../types.mjs";
5
7
  import type { MethodHooksObject } from "../utils.mjs";
8
+ export type InvokeKeyringMethodHooks = {
9
+ /**
10
+ * Get the list of allowed Keyring methods for a given origin.
11
+ *
12
+ * @returns The list of allowed Keyring methods.
13
+ */
14
+ getAllowedKeyringMethods: () => string[];
15
+ };
16
+ export type InvokeKeyringMethodActions = PermissionControllerHasPermissionAction | SnapControllerHandleRequestAction | SnapControllerGetSnapAction;
6
17
  /**
7
18
  * Invoke a keyring method of a Snap. This calls the `onKeyringRequest` handler
8
19
  * of the Snap.
@@ -12,17 +23,9 @@ import type { MethodHooksObject } from "../utils.mjs";
12
23
  * request permission to communicate with it using [`wallet_requestSnaps`](https://docs.metamask.io/snaps/reference/snaps-api/wallet_requestsnaps).
13
24
  */
14
25
  export declare const invokeKeyringHandler: {
15
- methodNames: ["wallet_invokeKeyring"];
16
26
  implementation: typeof invokeKeyringImplementation;
17
- hookNames: MethodHooksObject<InvokeKeyringHooks>;
18
- };
19
- export type InvokeKeyringHooks = {
20
- hasPermission: (permissionName: string) => boolean;
21
- handleSnapRpcRequest: ({ snapId, handler, request, }: Omit<SnapRpcHookArgs, 'origin'> & {
22
- snapId: string;
23
- }) => Promise<unknown>;
24
- getSnap: (snapId: string) => Snap | undefined;
25
- getAllowedKeyringMethods: () => string[];
27
+ hookNames: MethodHooksObject<InvokeKeyringMethodHooks>;
28
+ actionNames: ("SnapController:handleRequest" | "SnapController:getSnap" | "PermissionController:hasPermission")[];
26
29
  };
27
30
  /**
28
31
  * The `wallet_invokeKeyring` method implementation.
@@ -34,13 +37,11 @@ export type InvokeKeyringHooks = {
34
37
  * function.
35
38
  * @param end - The `json-rpc-engine` "end" callback.
36
39
  * @param hooks - The RPC method hooks.
37
- * @param hooks.handleSnapRpcRequest - Invokes a snap with a given RPC request.
38
- * @param hooks.hasPermission - Checks whether a given origin has a given permission.
39
- * @param hooks.getSnap - Gets information about a given snap.
40
40
  * @param hooks.getAllowedKeyringMethods - Get the list of allowed Keyring
41
41
  * methods for a given origin.
42
+ * @param messenger - The messenger used to call controller actions.
42
43
  * @returns Nothing.
43
44
  */
44
- declare function invokeKeyringImplementation(req: JsonRpcRequest<InvokeKeyringParams>, res: PendingJsonRpcResponse<InvokeKeyringResult>, _next: unknown, end: JsonRpcEngineEndCallback, { handleSnapRpcRequest, hasPermission, getSnap, getAllowedKeyringMethods, }: InvokeKeyringHooks): Promise<void>;
45
+ declare function invokeKeyringImplementation(req: JsonRpcRequestWithOrigin<InvokeKeyringParams>, res: PendingJsonRpcResponse<InvokeKeyringResult>, _next: unknown, end: JsonRpcEngineEndCallback, { getAllowedKeyringMethods }: InvokeKeyringMethodHooks, messenger: Messenger<string, InvokeKeyringMethodActions>): Promise<void>;
45
46
  export {};
46
47
  //# sourceMappingURL=invokeKeyring.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"invokeKeyring.d.mts","sourceRoot":"","sources":["../../src/permitted/invokeKeyring.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,kCAAkC;AAG1E,OAAO,KAAK,EACV,mBAAmB,EACnB,mBAAmB,EAEpB,4BAA4B;AAC7B,OAAO,KAAK,EAAE,IAAI,EAAE,eAAe,EAAE,8BAA8B;AAEnE,OAAO,KAAK,EACV,sBAAsB,EAEtB,cAAc,EACf,wBAAwB;AAIzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAiB;AAWlD;;;;;;;GAOG;AACH,eAAO,MAAM,oBAAoB;;;;CAQhC,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,aAAa,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,OAAO,CAAC;IAEnD,oBAAoB,EAAE,CAAC,EACrB,MAAM,EACN,OAAO,EACP,OAAO,GACR,EAAE,IAAI,CAAC,eAAe,EAAE,QAAQ,CAAC,GAAG;QAAE,MAAM,EAAE,MAAM,CAAA;KAAE,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IAE7E,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,GAAG,SAAS,CAAC;IAE9C,wBAAwB,EAAE,MAAM,MAAM,EAAE,CAAC;CAC1C,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,iBAAe,2BAA2B,CACxC,GAAG,EAAE,cAAc,CAAC,mBAAmB,CAAC,EAMxC,GAAG,EAAE,sBAAsB,CAAC,mBAAmB,CAAC,EAChD,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,wBAAwB,EAC7B,EACE,oBAAoB,EACpB,aAAa,EACb,OAAO,EACP,wBAAwB,GACzB,EAAE,kBAAkB,GACpB,OAAO,CAAC,IAAI,CAAC,CAyDf"}
1
+ {"version":3,"file":"invokeKeyring.d.mts","sourceRoot":"","sources":["../../src/permitted/invokeKeyring.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EAEzB,kCAAkC;AACnC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,uCAAuC,EAAE,wCAAwC;AAE/F,OAAO,KAAK,EACV,mBAAmB,EACnB,mBAAmB,EAEpB,4BAA4B;AAE7B,OAAO,KAAK,EAAE,sBAAsB,EAAQ,wBAAwB;AAIpE,OAAO,KAAK,EACV,wBAAwB,EACxB,2BAA2B,EAC3B,iCAAiC,EAClC,qBAAiB;AAClB,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAiB;AAMlD,MAAM,MAAM,wBAAwB,GAAG;IACrC;;;;OAIG;IACH,wBAAwB,EAAE,MAAM,MAAM,EAAE,CAAC;CAC1C,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAClC,uCAAuC,GACvC,iCAAiC,GACjC,2BAA2B,CAAC;AAEhC;;;;;;;GAOG;AACH,eAAO,MAAM,oBAAoB;;;;CAchC,CAAC;AAEF;;;;;;;;;;;;;;GAcG;AACH,iBAAe,2BAA2B,CACxC,GAAG,EAAE,wBAAwB,CAAC,mBAAmB,CAAC,EAMlD,GAAG,EAAE,sBAAsB,CAAC,mBAAmB,CAAC,EAChD,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,wBAAwB,EAC7B,EAAE,wBAAwB,EAAE,EAAE,wBAAwB,EACtD,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,0BAA0B,CAAC,GACvD,OAAO,CAAC,IAAI,CAAC,CAgEf"}
@@ -2,11 +2,7 @@ import { rpcErrors } from "@metamask/rpc-errors";
2
2
  import { HandlerType, WALLET_SNAP_PERMISSION_KEY } from "@metamask/snaps-utils";
3
3
  import { hasProperty } from "@metamask/utils";
4
4
  import { getValidatedParams } from "./invokeSnapSugar.mjs";
5
- const methodName = 'wallet_invokeKeyring';
6
5
  const hookNames = {
7
- hasPermission: true,
8
- handleSnapRpcRequest: true,
9
- getSnap: true,
10
6
  getAllowedKeyringMethods: true,
11
7
  };
12
8
  /**
@@ -18,9 +14,13 @@ const hookNames = {
18
14
  * request permission to communicate with it using [`wallet_requestSnaps`](https://docs.metamask.io/snaps/reference/snaps-api/wallet_requestsnaps).
19
15
  */
20
16
  export const invokeKeyringHandler = {
21
- methodNames: [methodName],
22
17
  implementation: invokeKeyringImplementation,
23
18
  hookNames,
19
+ actionNames: [
20
+ 'PermissionController:hasPermission',
21
+ 'SnapController:handleRequest',
22
+ 'SnapController:getSnap',
23
+ ],
24
24
  };
25
25
  /**
26
26
  * The `wallet_invokeKeyring` method implementation.
@@ -32,11 +32,9 @@ export const invokeKeyringHandler = {
32
32
  * function.
33
33
  * @param end - The `json-rpc-engine` "end" callback.
34
34
  * @param hooks - The RPC method hooks.
35
- * @param hooks.handleSnapRpcRequest - Invokes a snap with a given RPC request.
36
- * @param hooks.hasPermission - Checks whether a given origin has a given permission.
37
- * @param hooks.getSnap - Gets information about a given snap.
38
35
  * @param hooks.getAllowedKeyringMethods - Get the list of allowed Keyring
39
36
  * methods for a given origin.
37
+ * @param messenger - The messenger used to call controller actions.
40
38
  * @returns Nothing.
41
39
  */
42
40
  async function invokeKeyringImplementation(req,
@@ -45,7 +43,7 @@ async function invokeKeyringImplementation(req,
45
43
  // future. We use `InvokeKeyringResult` here to make it clear that this is the
46
44
  // expected type of the result.
47
45
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments
48
- res, _next, end, { handleSnapRpcRequest, hasPermission, getSnap, getAllowedKeyringMethods, }) {
46
+ res, _next, end, { getAllowedKeyringMethods }, messenger) {
49
47
  let params;
50
48
  try {
51
49
  params = getValidatedParams(req.params);
@@ -53,15 +51,15 @@ res, _next, end, { handleSnapRpcRequest, hasPermission, getSnap, getAllowedKeyri
53
51
  catch (error) {
54
52
  return end(error);
55
53
  }
56
- // We expect the MM middleware stack to always add the origin to requests
57
54
  const { origin } = req;
58
55
  const { snapId, request } = params;
59
- if (!origin || !hasPermission(WALLET_SNAP_PERMISSION_KEY)) {
56
+ if (!origin ||
57
+ !messenger.call('PermissionController:hasPermission', origin, WALLET_SNAP_PERMISSION_KEY)) {
60
58
  return end(rpcErrors.invalidRequest({
61
59
  message: `The snap "${snapId}" is not connected to "${origin}". Please connect before invoking the snap.`,
62
60
  }));
63
61
  }
64
- if (!getSnap(snapId)) {
62
+ if (!messenger.call('SnapController:getSnap', snapId)) {
65
63
  return end(
66
64
  // Mirror error message from SnapController.
67
65
  rpcErrors.invalidRequest({
@@ -80,7 +78,8 @@ res, _next, end, { handleSnapRpcRequest, hasPermission, getSnap, getAllowedKeyri
80
78
  }));
81
79
  }
82
80
  try {
83
- res.result = (await handleSnapRpcRequest({
81
+ res.result = (await messenger.call('SnapController:handleRequest', {
82
+ origin,
84
83
  snapId,
85
84
  request,
86
85
  handler: HandlerType.OnKeyringRequest,
@@ -1 +1 @@
1
- {"version":3,"file":"invokeKeyring.mjs","sourceRoot":"","sources":["../../src/permitted/invokeKeyring.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAOjD,OAAO,EAAE,WAAW,EAAE,0BAA0B,EAAE,8BAA8B;AAMhF,OAAO,EAAE,WAAW,EAAE,wBAAwB;AAE9C,OAAO,EAAE,kBAAkB,EAAE,8BAA0B;AAGvD,MAAM,UAAU,GAAG,sBAAsB,CAAC;AAE1C,MAAM,SAAS,GAA0C;IACvD,aAAa,EAAE,IAAI;IACnB,oBAAoB,EAAE,IAAI;IAC1B,OAAO,EAAE,IAAI;IACb,wBAAwB,EAAE,IAAI;CAC/B,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,WAAW,EAAE,CAAC,UAAU,CAAU;IAClC,cAAc,EAAE,2BAA2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M,cAAc,GAAG,wBAAwB,EAAE,CAAC;IAClD,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAC7C,OAAO,GAAG,CACR,SAAS,CAAC,cAAc,CAAC;YACvB,OAAO,EAAE,eAAe,MAAM,0CAA0C,OAAO,CAAC,MAAM,IAAI;SAC3F,CAAC,CACH,CAAC;IACJ,CAAC;IAED,IAAI,CAAC;QACH,GAAG,CAAC,MAAM,GAAG,CAAC,MAAM,oBAAoB,CAAC;YACvC,MAAM;YACN,OAAO;YACP,OAAO,EAAE,WAAW,CAAC,gBAAgB;SACtC,CAAC,CAAS,CAAC;IACd,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,OAAO,GAAG,EAAE,CAAC;AACf,CAAC","sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n InvokeKeyringParams,\n InvokeKeyringResult,\n InvokeSnapParams,\n} from '@metamask/snaps-sdk';\nimport type { Snap, SnapRpcHookArgs } from '@metamask/snaps-utils';\nimport { HandlerType, WALLET_SNAP_PERMISSION_KEY } from '@metamask/snaps-utils';\nimport type {\n PendingJsonRpcResponse,\n Json,\n JsonRpcRequest,\n} from '@metamask/utils';\nimport { hasProperty } from '@metamask/utils';\n\nimport { getValidatedParams } from './invokeSnapSugar';\nimport type { MethodHooksObject } from '../utils';\n\nconst methodName = 'wallet_invokeKeyring';\n\nconst hookNames: MethodHooksObject<InvokeKeyringHooks> = {\n hasPermission: true,\n handleSnapRpcRequest: true,\n getSnap: true,\n getAllowedKeyringMethods: true,\n};\n\n/**\n * Invoke a keyring method of a Snap. This calls the `onKeyringRequest` handler\n * of the Snap.\n *\n * The Snap must be installed and the dapp must have permission to communicate\n * with the Snap, or the request is rejected. The dapp can install the Snap and\n * request permission to communicate with it using [`wallet_requestSnaps`](https://docs.metamask.io/snaps/reference/snaps-api/wallet_requestsnaps).\n */\nexport const invokeKeyringHandler = {\n methodNames: [methodName] as const,\n implementation: invokeKeyringImplementation,\n hookNames,\n} satisfies PermittedHandlerExport<\n InvokeKeyringHooks,\n InvokeSnapParams,\n InvokeKeyringResult\n>;\n\nexport type InvokeKeyringHooks = {\n hasPermission: (permissionName: string) => boolean;\n\n handleSnapRpcRequest: ({\n snapId,\n handler,\n request,\n }: Omit<SnapRpcHookArgs, 'origin'> & { snapId: string }) => Promise<unknown>;\n\n getSnap: (snapId: string) => Snap | undefined;\n\n getAllowedKeyringMethods: () => string[];\n};\n\n/**\n * The `wallet_invokeKeyring` method implementation.\n * Invokes onKeyringRequest if the snap requested is installed and connected to the dapp.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.handleSnapRpcRequest - Invokes a snap with a given RPC request.\n * @param hooks.hasPermission - Checks whether a given origin has a given permission.\n * @param hooks.getSnap - Gets information about a given snap.\n * @param hooks.getAllowedKeyringMethods - Get the list of allowed Keyring\n * methods for a given origin.\n * @returns Nothing.\n */\nasync function invokeKeyringImplementation(\n req: JsonRpcRequest<InvokeKeyringParams>,\n // `InvokeKeyringResult` is an alias for `Json` (which is the default type\n // argument for `PendingJsonRpcResponse`), but that may not be the case in the\n // future. We use `InvokeKeyringResult` here to make it clear that this is the\n // expected type of the result.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments\n res: PendingJsonRpcResponse<InvokeKeyringResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n {\n handleSnapRpcRequest,\n hasPermission,\n getSnap,\n getAllowedKeyringMethods,\n }: InvokeKeyringHooks,\n): Promise<void> {\n let params: InvokeSnapParams;\n try {\n params = getValidatedParams(req.params);\n } catch (error) {\n return end(error);\n }\n\n // We expect the MM middleware stack to always add the origin to requests\n const { origin } = req as JsonRpcRequest & { origin: string };\n const { snapId, request } = params;\n\n if (!origin || !hasPermission(WALLET_SNAP_PERMISSION_KEY)) {\n return end(\n rpcErrors.invalidRequest({\n message: `The snap \"${snapId}\" is not connected to \"${origin}\". Please connect before invoking the snap.`,\n }),\n );\n }\n\n if (!getSnap(snapId)) {\n return end(\n // Mirror error message from SnapController.\n rpcErrors.invalidRequest({\n message: `The Snap \"${snapId}\" is not installed. Please install it before invoking it.`,\n }),\n );\n }\n\n if (!hasProperty(request, 'method') || typeof request.method !== 'string') {\n return end(\n rpcErrors.invalidRequest({\n message: 'The request must have a method.',\n }),\n );\n }\n\n const allowedMethods = getAllowedKeyringMethods();\n if (!allowedMethods.includes(request.method)) {\n return end(\n rpcErrors.invalidRequest({\n message: `The origin \"${origin}\" is not allowed to invoke the method \"${request.method}\".`,\n }),\n );\n }\n\n try {\n res.result = (await handleSnapRpcRequest({\n snapId,\n request,\n handler: HandlerType.OnKeyringRequest,\n })) as Json;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n"]}
1
+ {"version":3,"file":"invokeKeyring.mjs","sourceRoot":"","sources":["../../src/permitted/invokeKeyring.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAMjD,OAAO,EAAE,WAAW,EAAE,0BAA0B,EAAE,8BAA8B;AAEhF,OAAO,EAAE,WAAW,EAAE,wBAAwB;AAE9C,OAAO,EAAE,kBAAkB,EAAE,8BAA0B;AAQvD,MAAM,SAAS,GAAgD;IAC7D,wBAAwB,EAAE,IAAI;CAC/B,CAAC;AAgBF;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,cAAc,EAAE,2BAA2B;IAC3C,SAAS;IACT,WAAW,EAAE;QACX,oCAAoC;QACpC,8BAA8B;QAC9B,wBAAwB;KACzB;CAOF,CAAC;AAEF;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AAwB,EAAE,CAAC;IAClD,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAC7C,OAAO,GAAG,CACR,SAAS,CAAC,cAAc,CAAC;YACvB,OAAO,EAAE,eAAe,MAAM,0CAA0C,OAAO,CAAC,MAAM,IAAI;SAC3F,CAAC,CACH,CAAC;IACJ,CAAC;IAED,IAAI,CAAC;QACH,GAAG,CAAC,MAAM,GAAG,CAAC,MAAM,SAAS,CAAC,IAAI,CAAC,8BAA8B,EAAE;YACjE,MAAM;YACN,MAAM;YACN,OAAO;YACP,OAAO,EAAE,WAAW,CAAC,gBAAgB;SACtC,CAAC,CAAS,CAAC;IACd,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,OAAO,GAAG,EAAE,CAAC;AACf,CAAC","sourcesContent":["import type {\n JsonRpcEngineEndCallback,\n MethodHandler,\n} from '@metamask/json-rpc-engine';\nimport type { Messenger } from '@metamask/messenger';\nimport type { PermissionControllerHasPermissionAction } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n InvokeKeyringParams,\n InvokeKeyringResult,\n InvokeSnapParams,\n} from '@metamask/snaps-sdk';\nimport { HandlerType, WALLET_SNAP_PERMISSION_KEY } from '@metamask/snaps-utils';\nimport type { PendingJsonRpcResponse, Json } from '@metamask/utils';\nimport { hasProperty } from '@metamask/utils';\n\nimport { getValidatedParams } from './invokeSnapSugar';\nimport type {\n JsonRpcRequestWithOrigin,\n SnapControllerGetSnapAction,\n SnapControllerHandleRequestAction,\n} from '../types';\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<InvokeKeyringMethodHooks> = {\n getAllowedKeyringMethods: true,\n};\n\nexport type InvokeKeyringMethodHooks = {\n /**\n * Get the list of allowed Keyring methods for a given origin.\n *\n * @returns The list of allowed Keyring methods.\n */\n getAllowedKeyringMethods: () => string[];\n};\n\nexport type InvokeKeyringMethodActions =\n | PermissionControllerHasPermissionAction\n | SnapControllerHandleRequestAction\n | SnapControllerGetSnapAction;\n\n/**\n * Invoke a keyring method of a Snap. This calls the `onKeyringRequest` handler\n * of the Snap.\n *\n * The Snap must be installed and the dapp must have permission to communicate\n * with the Snap, or the request is rejected. The dapp can install the Snap and\n * request permission to communicate with it using [`wallet_requestSnaps`](https://docs.metamask.io/snaps/reference/snaps-api/wallet_requestsnaps).\n */\nexport const invokeKeyringHandler = {\n implementation: invokeKeyringImplementation,\n hookNames,\n actionNames: [\n 'PermissionController:hasPermission',\n 'SnapController:handleRequest',\n 'SnapController:getSnap',\n ],\n} satisfies MethodHandler<\n InvokeKeyringMethodHooks,\n InvokeKeyringMethodActions,\n InvokeSnapParams,\n InvokeKeyringResult,\n { origin: string }\n>;\n\n/**\n * The `wallet_invokeKeyring` method implementation.\n * Invokes onKeyringRequest if the snap requested is installed and connected to the dapp.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.getAllowedKeyringMethods - Get the list of allowed Keyring\n * methods for a given origin.\n * @param messenger - The messenger used to call controller actions.\n * @returns Nothing.\n */\nasync function invokeKeyringImplementation(\n req: JsonRpcRequestWithOrigin<InvokeKeyringParams>,\n // `InvokeKeyringResult` is an alias for `Json` (which is the default type\n // argument for `PendingJsonRpcResponse`), but that may not be the case in the\n // future. We use `InvokeKeyringResult` here to make it clear that this is the\n // expected type of the result.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments\n res: PendingJsonRpcResponse<InvokeKeyringResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { getAllowedKeyringMethods }: InvokeKeyringMethodHooks,\n messenger: Messenger<string, InvokeKeyringMethodActions>,\n): Promise<void> {\n let params: InvokeSnapParams;\n try {\n params = getValidatedParams(req.params);\n } catch (error) {\n return end(error);\n }\n\n const { origin } = req;\n const { snapId, request } = params;\n\n if (\n !origin ||\n !messenger.call(\n 'PermissionController:hasPermission',\n origin,\n WALLET_SNAP_PERMISSION_KEY,\n )\n ) {\n return end(\n rpcErrors.invalidRequest({\n message: `The snap \"${snapId}\" is not connected to \"${origin}\". Please connect before invoking the snap.`,\n }),\n );\n }\n\n if (!messenger.call('SnapController:getSnap', snapId)) {\n return end(\n // Mirror error message from SnapController.\n rpcErrors.invalidRequest({\n message: `The Snap \"${snapId}\" is not installed. Please install it before invoking it.`,\n }),\n );\n }\n\n if (!hasProperty(request, 'method') || typeof request.method !== 'string') {\n return end(\n rpcErrors.invalidRequest({\n message: 'The request must have a method.',\n }),\n );\n }\n\n const allowedMethods = getAllowedKeyringMethods();\n if (!allowedMethods.includes(request.method)) {\n return end(\n rpcErrors.invalidRequest({\n message: `The origin \"${origin}\" is not allowed to invoke the method \"${request.method}\".`,\n }),\n );\n }\n\n try {\n res.result = (await messenger.call('SnapController:handleRequest', {\n origin,\n snapId,\n request,\n handler: HandlerType.OnKeyringRequest,\n })) as Json;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n"]}
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getValidatedParams = exports.invokeSnapSugar = exports.invokeSnapSugarHandler = void 0;
4
4
  const rpc_errors_1 = require("@metamask/rpc-errors");
5
5
  const utils_1 = require("@metamask/utils");
6
- const methodName = 'wallet_invokeSnap';
6
+ const invokeSnap_1 = require("../restricted/invokeSnap.cjs");
7
7
  /**
8
8
  * Invoke a method of a Snap, designated by the `snapId` parameter, with a
9
9
  * JSON-RPC request specified in the `request` parameter. This is effectively a
@@ -30,11 +30,8 @@ const methodName = 'wallet_invokeSnap';
30
30
  * ```
31
31
  */
32
32
  exports.invokeSnapSugarHandler = {
33
- methodNames: [methodName],
34
33
  implementation: invokeSnapSugar,
35
- hookNames: {
36
- invokeSnap: true,
37
- },
34
+ actionNames: ['PermissionController:executeRestrictedMethod'],
38
35
  };
39
36
  /**
40
37
  * The `wallet_invokeSnap` method implementation.
@@ -44,9 +41,8 @@ exports.invokeSnapSugarHandler = {
44
41
  * @param res - The JSON-RPC response object.
45
42
  * @param _next - The `json-rpc-engine` "next" callback.
46
43
  * @param end - The `json-rpc-engine` "end" callback.
47
- * @param hooks - The RPC method hooks.
48
- * @param hooks.invokeSnap - A function to invoke a snap designated by its parameters,
49
- * bound to the requesting origin.
44
+ * @param _hooks - The RPC method hooks. Not used by this function.
45
+ * @param messenger - The messenger used to call controller actions.
50
46
  * @returns Nothing.
51
47
  * @throws If the params are invalid.
52
48
  */
@@ -56,10 +52,10 @@ async function invokeSnapSugar(req,
56
52
  // future. We use `InvokeSnapResult` here to make it clear that this is the
57
53
  // expected type of the result.
58
54
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments
59
- res, _next, end, { invokeSnap }) {
55
+ res, _next, end, _hooks, messenger) {
60
56
  try {
61
57
  const params = getValidatedParams(req.params);
62
- res.result = await invokeSnap(params);
58
+ res.result = await messenger.call('PermissionController:executeRestrictedMethod', req.origin, invokeSnap_1.WALLET_SNAP_PERMISSION_KEY, params);
63
59
  }
64
60
  catch (error) {
65
61
  return end(error);
@@ -1 +1 @@
1
- {"version":3,"file":"invokeSnapSugar.cjs","sourceRoot":"","sources":["../../src/permitted/invokeSnapSugar.ts"],"names":[],"mappings":";;;AAKA,qDAAiD;AAGjD,2CAA2C;AAE3C,MAAM,UAAU,GAAG,mBAAmB,CAAC;AAEvC;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACU,QAAA,sBAAsB,GAAG;IACpC,WAAW,EAAE,CAAC,UAAU,CAAU;IAClC,cAAc,EAAE,eAAe;IAC/B,SAAS,EAAE;QACT,UAAU,EAAE,IAAI;KACjB;CAKF,CAAC;AAMF;;;;;;;;;;;;;GAaG;AACI,KAAK,UAAU,eAAe,CACnC,GAAqC;AACrC,uEAAuE;AACvE,8EAA8E;AAC9E,2EAA2E;AAC3E,+BAA+B;AAC/B,4EAA4E;AAC5E,GAA6C,EAC7C,KAAgC,EAChC,GAA6B,EAC7B,EAAE,UAAU,EAAwB;IAEpC,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,kBAAkB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAC9C,GAAG,CAAC,MAAM,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,CAAC;IACxC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IACD,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAnBD,0CAmBC;AAED;;;;;;GAMG;AACH,SAAgB,kBAAkB,CAAC,MAAe;IAChD,IAAI,CAAC,IAAA,gBAAQ,EAAC,MAAM,CAAC,EAAE,CAAC;QACtB,MAAM,sBAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EAAE,wCAAwC;SAClD,CAAC,CAAC;IACL,CAAC;IAED,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC;IAEnC,IAAI,CAAC,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,EAAE,EAAE,CAAC;QAC3D,MAAM,sBAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EAAE,+BAA+B;SACzC,CAAC,CAAC;IACL,CAAC;IAED,IAAI,CAAC,IAAA,gBAAQ,EAAC,OAAO,CAAC,EAAE,CAAC;QACvB,MAAM,sBAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EAAE,yCAAyC;SACnD,CAAC,CAAC;IACL,CAAC;IAED,OAAO,MAA0B,CAAC;AACpC,CAAC;AAtBD,gDAsBC","sourcesContent":["import type {\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n} from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { InvokeSnapParams, InvokeSnapResult } from '@metamask/snaps-sdk';\nimport type { JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils';\nimport { isObject } from '@metamask/utils';\n\nconst methodName = 'wallet_invokeSnap';\n\n/**\n * Invoke a method of a Snap, designated by the `snapId` parameter, with a\n * JSON-RPC request specified in the `request` parameter. This is effectively a\n * wrapper around [`wallet_snap`](https://docs.metamask.io/snaps/reference/snaps-api/wallet_snap)\n * that allows for more convenient invocation of Snap methods without needing to\n * specify the full `wallet_snap` parameters.\n *\n * The Snap must be installed and the dapp must have permission to communicate\n * with the Snap, or the request is rejected. The dapp can install the Snap and\n * request permission to communicate with it using [`wallet_requestSnaps`](https://docs.metamask.io/snaps/reference/snaps-api/wallet_requestsnaps).\n *\n * @example\n * ```ts\n * const result = await snap.request({\n * method: 'wallet_invokeSnap',\n * params: {\n * snapId: 'npm:@metamask/example-snap',\n * request: {\n * method: 'someMethod',\n * params: { some: 'params' },\n * },\n * },\n * });\n * ```\n */\nexport const invokeSnapSugarHandler = {\n methodNames: [methodName] as const,\n implementation: invokeSnapSugar,\n hookNames: {\n invokeSnap: true,\n },\n} satisfies PermittedHandlerExport<\n InvokeSnapSugarHooks,\n InvokeSnapParams,\n InvokeSnapResult\n>;\n\nexport type InvokeSnapSugarHooks = {\n invokeSnap: (params: InvokeSnapParams) => Promise<InvokeSnapResult>;\n};\n\n/**\n * The `wallet_invokeSnap` method implementation.\n * Effectively calls `wallet_snap` under the hood.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.invokeSnap - A function to invoke a snap designated by its parameters,\n * bound to the requesting origin.\n * @returns Nothing.\n * @throws If the params are invalid.\n */\nexport async function invokeSnapSugar(\n req: JsonRpcRequest<InvokeSnapParams>,\n // `InvokeSnapResult` is an alias for `Json` (which is the default type\n // argument for `PendingJsonRpcResponse`), but that may not be the case in the\n // future. We use `InvokeSnapResult` here to make it clear that this is the\n // expected type of the result.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments\n res: PendingJsonRpcResponse<InvokeSnapResult>,\n _next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n { invokeSnap }: InvokeSnapSugarHooks,\n): Promise<void> {\n try {\n const params = getValidatedParams(req.params);\n res.result = await invokeSnap(params);\n } catch (error) {\n return end(error);\n }\n return end();\n}\n\n/**\n * Validates the wallet_invokeSnap method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @returns The validated method parameter object.\n */\nexport function getValidatedParams(params: unknown): InvokeSnapParams {\n if (!isObject(params)) {\n throw rpcErrors.invalidParams({\n message: 'Expected params to be a single object.',\n });\n }\n\n const { snapId, request } = params;\n\n if (!snapId || typeof snapId !== 'string' || snapId === '') {\n throw rpcErrors.invalidParams({\n message: 'Must specify a valid snap ID.',\n });\n }\n\n if (!isObject(request)) {\n throw rpcErrors.invalidParams({\n message: 'Expected request to be a single object.',\n });\n }\n\n return params as InvokeSnapParams;\n}\n"]}
1
+ {"version":3,"file":"invokeSnapSugar.cjs","sourceRoot":"","sources":["../../src/permitted/invokeSnapSugar.ts"],"names":[],"mappings":";;;AAOA,qDAAiD;AAGjD,2CAA2C;AAE3C,6DAAsE;AAMtE;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACU,QAAA,sBAAsB,GAAG;IACpC,cAAc,EAAE,eAAe;IAC/B,WAAW,EAAE,CAAC,8CAA8C,CAAC;CAO9D,CAAC;AAEF;;;;;;;;;;;;GAYG;AACI,KAAK,UAAU,eAAe,CACnC,GAA+C;AAC/C,uEAAuE;AACvE,8EAA8E;AAC9E,2EAA2E;AAC3E,+BAA+B;AAC/B,4EAA4E;AAC5E,GAA6C,EAC7C,KAAgC,EAChC,GAA6B,EAC7B,MAAa,EACb,SAA0D;IAE1D,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,kBAAkB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAC9C,GAAG,CAAC,MAAM,GAAG,MAAM,SAAS,CAAC,IAAI,CAC/B,8CAA8C,EAC9C,GAAG,CAAC,MAAM,EACV,uCAA0B,EAC1B,MAAM,CACP,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IACD,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAzBD,0CAyBC;AAED;;;;;;GAMG;AACH,SAAgB,kBAAkB,CAAC,MAAe;IAChD,IAAI,CAAC,IAAA,gBAAQ,EAAC,MAAM,CAAC,EAAE,CAAC;QACtB,MAAM,sBAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EAAE,wCAAwC;SAClD,CAAC,CAAC;IACL,CAAC;IAED,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC;IAEnC,IAAI,CAAC,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,EAAE,EAAE,CAAC;QAC3D,MAAM,sBAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EAAE,+BAA+B;SACzC,CAAC,CAAC;IACL,CAAC;IAED,IAAI,CAAC,IAAA,gBAAQ,EAAC,OAAO,CAAC,EAAE,CAAC;QACvB,MAAM,sBAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EAAE,yCAAyC;SACnD,CAAC,CAAC;IACL,CAAC;IAED,OAAO,MAA0B,CAAC;AACpC,CAAC;AAtBD,gDAsBC","sourcesContent":["import type {\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n MethodHandler,\n} from '@metamask/json-rpc-engine';\nimport type { Messenger } from '@metamask/messenger';\nimport type { PermissionControllerExecuteRestrictedMethodAction } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { InvokeSnapParams, InvokeSnapResult } from '@metamask/snaps-sdk';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\nimport { isObject } from '@metamask/utils';\n\nimport { WALLET_SNAP_PERMISSION_KEY } from '../restricted/invokeSnap';\nimport type { JsonRpcRequestWithOrigin } from '../types';\n\nexport type InvokeSnapSugarMethodActions =\n PermissionControllerExecuteRestrictedMethodAction;\n\n/**\n * Invoke a method of a Snap, designated by the `snapId` parameter, with a\n * JSON-RPC request specified in the `request` parameter. This is effectively a\n * wrapper around [`wallet_snap`](https://docs.metamask.io/snaps/reference/snaps-api/wallet_snap)\n * that allows for more convenient invocation of Snap methods without needing to\n * specify the full `wallet_snap` parameters.\n *\n * The Snap must be installed and the dapp must have permission to communicate\n * with the Snap, or the request is rejected. The dapp can install the Snap and\n * request permission to communicate with it using [`wallet_requestSnaps`](https://docs.metamask.io/snaps/reference/snaps-api/wallet_requestsnaps).\n *\n * @example\n * ```ts\n * const result = await snap.request({\n * method: 'wallet_invokeSnap',\n * params: {\n * snapId: 'npm:@metamask/example-snap',\n * request: {\n * method: 'someMethod',\n * params: { some: 'params' },\n * },\n * },\n * });\n * ```\n */\nexport const invokeSnapSugarHandler = {\n implementation: invokeSnapSugar,\n actionNames: ['PermissionController:executeRestrictedMethod'],\n} satisfies MethodHandler<\n never,\n InvokeSnapSugarMethodActions,\n InvokeSnapParams,\n InvokeSnapResult,\n { origin: string }\n>;\n\n/**\n * The `wallet_invokeSnap` method implementation.\n * Effectively calls `wallet_snap` under the hood.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param _hooks - The RPC method hooks. Not used by this function.\n * @param messenger - The messenger used to call controller actions.\n * @returns Nothing.\n * @throws If the params are invalid.\n */\nexport async function invokeSnapSugar(\n req: JsonRpcRequestWithOrigin<InvokeSnapParams>,\n // `InvokeSnapResult` is an alias for `Json` (which is the default type\n // argument for `PendingJsonRpcResponse`), but that may not be the case in the\n // future. We use `InvokeSnapResult` here to make it clear that this is the\n // expected type of the result.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments\n res: PendingJsonRpcResponse<InvokeSnapResult>,\n _next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n _hooks: never,\n messenger: Messenger<string, InvokeSnapSugarMethodActions>,\n): Promise<void> {\n try {\n const params = getValidatedParams(req.params);\n res.result = await messenger.call(\n 'PermissionController:executeRestrictedMethod',\n req.origin,\n WALLET_SNAP_PERMISSION_KEY,\n params,\n );\n } catch (error) {\n return end(error);\n }\n return end();\n}\n\n/**\n * Validates the wallet_invokeSnap method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @returns The validated method parameter object.\n */\nexport function getValidatedParams(params: unknown): InvokeSnapParams {\n if (!isObject(params)) {\n throw rpcErrors.invalidParams({\n message: 'Expected params to be a single object.',\n });\n }\n\n const { snapId, request } = params;\n\n if (!snapId || typeof snapId !== 'string' || snapId === '') {\n throw rpcErrors.invalidParams({\n message: 'Must specify a valid snap ID.',\n });\n }\n\n if (!isObject(request)) {\n throw rpcErrors.invalidParams({\n message: 'Expected request to be a single object.',\n });\n }\n\n return params as InvokeSnapParams;\n}\n"]}
@@ -1,6 +1,10 @@
1
1
  import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback } from "@metamask/json-rpc-engine";
2
+ import type { Messenger } from "@metamask/messenger";
3
+ import type { PermissionControllerExecuteRestrictedMethodAction } from "@metamask/permission-controller";
2
4
  import type { InvokeSnapParams, InvokeSnapResult } from "@metamask/snaps-sdk";
3
- import type { JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
5
+ import type { PendingJsonRpcResponse } from "@metamask/utils";
6
+ import type { JsonRpcRequestWithOrigin } from "../types.cjs";
7
+ export type InvokeSnapSugarMethodActions = PermissionControllerExecuteRestrictedMethodAction;
4
8
  /**
5
9
  * Invoke a method of a Snap, designated by the `snapId` parameter, with a
6
10
  * JSON-RPC request specified in the `request` parameter. This is effectively a
@@ -27,14 +31,8 @@ import type { JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
27
31
  * ```
28
32
  */
29
33
  export declare const invokeSnapSugarHandler: {
30
- methodNames: ["wallet_invokeSnap"];
31
34
  implementation: typeof invokeSnapSugar;
32
- hookNames: {
33
- invokeSnap: true;
34
- };
35
- };
36
- export type InvokeSnapSugarHooks = {
37
- invokeSnap: (params: InvokeSnapParams) => Promise<InvokeSnapResult>;
35
+ actionNames: "PermissionController:executeRestrictedMethod"[];
38
36
  };
39
37
  /**
40
38
  * The `wallet_invokeSnap` method implementation.
@@ -44,13 +42,12 @@ export type InvokeSnapSugarHooks = {
44
42
  * @param res - The JSON-RPC response object.
45
43
  * @param _next - The `json-rpc-engine` "next" callback.
46
44
  * @param end - The `json-rpc-engine` "end" callback.
47
- * @param hooks - The RPC method hooks.
48
- * @param hooks.invokeSnap - A function to invoke a snap designated by its parameters,
49
- * bound to the requesting origin.
45
+ * @param _hooks - The RPC method hooks. Not used by this function.
46
+ * @param messenger - The messenger used to call controller actions.
50
47
  * @returns Nothing.
51
48
  * @throws If the params are invalid.
52
49
  */
53
- export declare function invokeSnapSugar(req: JsonRpcRequest<InvokeSnapParams>, res: PendingJsonRpcResponse<InvokeSnapResult>, _next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, { invokeSnap }: InvokeSnapSugarHooks): Promise<void>;
50
+ export declare function invokeSnapSugar(req: JsonRpcRequestWithOrigin<InvokeSnapParams>, res: PendingJsonRpcResponse<InvokeSnapResult>, _next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, _hooks: never, messenger: Messenger<string, InvokeSnapSugarMethodActions>): Promise<void>;
54
51
  /**
55
52
  * Validates the wallet_invokeSnap method `params` and returns them cast to the correct
56
53
  * type. Throws if validation fails.