@metamask-previews/eth-json-rpc-provider 5.0.1-preview-5e4e26a6 → 5.0.1-preview-c0feb25

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/CHANGELOG.md CHANGED
@@ -12,6 +12,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
12
12
  - **BREAKING:** Replace `SafeEventEmitterProvider` with `InternalProvider` ([#6796](https://github.com/MetaMask/core/pull/6796))
13
13
  - The new class is behaviorally equivalent to the previous version except it does not extend `SafeEventEmitter`.
14
14
  - `SafeEventEmitterProvider` is for now still exported as a deprecated alias of `InternalProvider` for backwards compatibility.
15
+ - **BREAKING:** Use `JsonRpcServer` instead of `JsonRpcEngine` ([#7001](https://github.com/MetaMask/core/pull/7001))
16
+ - Adds a new `server` constructor option to the `InternalProvider` class, mutually exclusive with the now deprecated `engine` option.
17
+ - Legacy `JsonRpcEngine` instances are wrapped in a `JsonRpcServer` internally
18
+ wherever they appear. Due to differences in error serialization, this may be
19
+ breaking for consumers.
15
20
 
16
21
  ## [5.0.1]
17
22
 
@@ -10,33 +10,14 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _InternalProvider_engine;
13
+ var _InternalProvider_server, _InternalProvider_handle, _InternalProvider_handleWithCallback;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.InternalProvider = exports.convertEip1193RequestToJsonRpcRequest = void 0;
15
+ exports.convertEip1193RequestToJsonRpcRequest = exports.InternalProvider = void 0;
16
+ const json_rpc_engine_1 = require("@metamask/json-rpc-engine");
17
+ const v2_1 = require("@metamask/json-rpc-engine/v2");
16
18
  const rpc_errors_1 = require("@metamask/rpc-errors");
19
+ const utils_1 = require("@metamask/utils");
17
20
  const uuid_1 = require("uuid");
18
- /**
19
- * Converts an EIP-1193 request to a JSON-RPC request.
20
- *
21
- * @param eip1193Request - The EIP-1193 request to convert.
22
- * @returns The corresponding JSON-RPC request.
23
- */
24
- function convertEip1193RequestToJsonRpcRequest(eip1193Request) {
25
- const { id = (0, uuid_1.v4)(), jsonrpc = '2.0', method, params } = eip1193Request;
26
- return params
27
- ? {
28
- id,
29
- jsonrpc,
30
- method,
31
- params,
32
- }
33
- : {
34
- id,
35
- jsonrpc,
36
- method,
37
- };
38
- }
39
- exports.convertEip1193RequestToJsonRpcRequest = convertEip1193RequestToJsonRpcRequest;
40
21
  /**
41
22
  * An Ethereum provider.
42
23
  *
@@ -45,13 +26,14 @@ exports.convertEip1193RequestToJsonRpcRequest = convertEip1193RequestToJsonRpcRe
45
26
  */
46
27
  class InternalProvider {
47
28
  /**
48
- * Construct a InternalProvider from a JSON-RPC engine.
29
+ * Construct a InternalProvider from a JSON-RPC server or legacy engine.
49
30
  *
50
31
  * @param options - Options.
51
- * @param options.engine - The JSON-RPC engine used to process requests.
32
+ * @param options.engine - **Deprecated:** The JSON-RPC engine used to process requests. Mutually exclusive with `server`.
33
+ * @param options.server - The JSON-RPC server used to process requests. Mutually exclusive with `engine`.
52
34
  */
53
- constructor({ engine }) {
54
- _InternalProvider_engine.set(this, void 0);
35
+ constructor(options) {
36
+ _InternalProvider_server.set(this, void 0);
55
37
  /**
56
38
  * Send a provider request asynchronously.
57
39
  *
@@ -60,14 +42,14 @@ class InternalProvider {
60
42
  *
61
43
  * @param eip1193Request - The request to send.
62
44
  * @param callback - A function that is called upon the success or failure of the request.
63
- * @deprecated Please use `request` instead.
45
+ * @deprecated Use {@link request} instead.
64
46
  */
65
47
  this.sendAsync = (eip1193Request,
66
- // TODO: Replace `any` with type
48
+ // Non-polluting `any` that acts like a constraint.
67
49
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
68
50
  callback) => {
69
51
  const jsonRpcRequest = convertEip1193RequestToJsonRpcRequest(eip1193Request);
70
- __classPrivateFieldGet(this, _InternalProvider_engine, "f").handle(jsonRpcRequest, callback);
52
+ __classPrivateFieldGet(this, _InternalProvider_handleWithCallback, "f").call(this, jsonRpcRequest, callback);
71
53
  };
72
54
  /**
73
55
  * Send a provider request asynchronously.
@@ -77,7 +59,7 @@ class InternalProvider {
77
59
  *
78
60
  * @param eip1193Request - The request to send.
79
61
  * @param callback - A function that is called upon the success or failure of the request.
80
- * @deprecated Please use `request` instead.
62
+ * @deprecated Use {@link request} instead.
81
63
  */
82
64
  this.send = (eip1193Request,
83
65
  // TODO: Replace `any` with type
@@ -87,9 +69,35 @@ class InternalProvider {
87
69
  throw new Error('Must provide callback to "send" method.');
88
70
  }
89
71
  const jsonRpcRequest = convertEip1193RequestToJsonRpcRequest(eip1193Request);
90
- __classPrivateFieldGet(this, _InternalProvider_engine, "f").handle(jsonRpcRequest, callback);
72
+ __classPrivateFieldGet(this, _InternalProvider_handleWithCallback, "f").call(this, jsonRpcRequest, callback);
91
73
  };
92
- __classPrivateFieldSet(this, _InternalProvider_engine, engine, "f");
74
+ _InternalProvider_handle.set(this, async (jsonRpcRequest) => {
75
+ // @ts-expect-error - The signatures are incompatible between the legacy engine
76
+ // and server, but this works at runtime.
77
+ return await __classPrivateFieldGet(this, _InternalProvider_server, "f").handle(jsonRpcRequest);
78
+ });
79
+ _InternalProvider_handleWithCallback.set(this, (jsonRpcRequest, callback) => {
80
+ /* eslint-disable promise/always-return,promise/no-callback-in-promise */
81
+ __classPrivateFieldGet(this, _InternalProvider_handle, "f").call(this, jsonRpcRequest)
82
+ .then((response) => {
83
+ if ((0, utils_1.hasProperty)(response, 'result')) {
84
+ callback(null, response);
85
+ }
86
+ else {
87
+ callback(deserializeError(response.error));
88
+ }
89
+ })
90
+ .catch((error) => {
91
+ callback(error);
92
+ });
93
+ /* eslint-enable promise/always-return,promise/no-callback-in-promise */
94
+ });
95
+ const serverOrLegacyEngine = 'server' in options ? options.server : options.engine;
96
+ __classPrivateFieldSet(this, _InternalProvider_server, 'push' in serverOrLegacyEngine
97
+ ? new v2_1.JsonRpcServer({
98
+ middleware: [(0, json_rpc_engine_1.asV2Middleware)(serverOrLegacyEngine)],
99
+ })
100
+ : serverOrLegacyEngine, "f");
93
101
  }
94
102
  /**
95
103
  * Send a provider request asynchronously.
@@ -99,17 +107,45 @@ class InternalProvider {
99
107
  */
100
108
  async request(eip1193Request) {
101
109
  const jsonRpcRequest = convertEip1193RequestToJsonRpcRequest(eip1193Request);
102
- const response = await __classPrivateFieldGet(this, _InternalProvider_engine, "f").handle(jsonRpcRequest);
110
+ const response = await __classPrivateFieldGet(this, _InternalProvider_handle, "f").call(this, jsonRpcRequest);
103
111
  if ('result' in response) {
104
112
  return response.result;
105
113
  }
106
- const error = new rpc_errors_1.JsonRpcError(response.error.code, response.error.message, response.error.data);
107
- if ('stack' in response.error) {
108
- error.stack = response.error.stack;
109
- }
110
- throw error;
114
+ throw deserializeError(response.error);
111
115
  }
112
116
  }
113
117
  exports.InternalProvider = InternalProvider;
114
- _InternalProvider_engine = new WeakMap();
118
+ _InternalProvider_server = new WeakMap(), _InternalProvider_handle = new WeakMap(), _InternalProvider_handleWithCallback = new WeakMap();
119
+ /**
120
+ * Convert an EIP-1193 request to a JSON-RPC request.
121
+ *
122
+ * @param eip1193Request - The EIP-1193 request to convert.
123
+ * @returns The JSON-RPC request.
124
+ */
125
+ function convertEip1193RequestToJsonRpcRequest(eip1193Request) {
126
+ const { id = (0, uuid_1.v4)(), jsonrpc = '2.0', method, params } = eip1193Request;
127
+ return params
128
+ ? {
129
+ id,
130
+ jsonrpc,
131
+ method,
132
+ params,
133
+ }
134
+ : {
135
+ id,
136
+ jsonrpc,
137
+ method,
138
+ };
139
+ }
140
+ exports.convertEip1193RequestToJsonRpcRequest = convertEip1193RequestToJsonRpcRequest;
141
+ /**
142
+ * Deserialize a JSON-RPC error. Ignores the possibility of `stack` property, since this is
143
+ * stripped by `JsonRpcServer`.
144
+ *
145
+ * @param error - The JSON-RPC error to deserialize.
146
+ * @returns The deserialized error.
147
+ */
148
+ function deserializeError(error) {
149
+ return new rpc_errors_1.JsonRpcError(error.code, error.message, error.data);
150
+ }
115
151
  //# sourceMappingURL=internal-provider.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"internal-provider.cjs","sourceRoot":"","sources":["../src/internal-provider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,qDAAoD;AAQpD,+BAAoC;AAYpC;;;;;GAKG;AACH,SAAgB,qCAAqC,CAGnD,cAAsC;IAEtC,MAAM,EAAE,EAAE,GAAG,IAAA,SAAM,GAAE,EAAE,OAAO,GAAG,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,cAAc,CAAC;IAC1E,OAAO,MAAM;QACX,CAAC,CAAC;YACE,EAAE;YACF,OAAO;YACP,MAAM;YACN,MAAM;SACP;QACH,CAAC,CAAC;YACE,EAAE;YACF,OAAO;YACP,MAAM;SACP,CAAC;AACR,CAAC;AAlBD,sFAkBC;AAED;;;;;GAKG;AACH,MAAa,gBAAgB;IAG3B;;;;;OAKG;IACH,YAAY,EAAE,MAAM,EAA6B;QARxC,2CAAuB;QA2ChC;;;;;;;;;WASG;QACH,cAAS,GAAG,CACV,cAAsC;QACtC,gCAAgC;QAChC,8DAA8D;QAC9D,QAAqD,EACrD,EAAE;YACF,MAAM,cAAc,GAClB,qCAAqC,CAAC,cAAc,CAAC,CAAC;YACxD,uBAAA,IAAI,gCAAQ,CAAC,MAAM,CAAC,cAAc,EAAE,QAAQ,CAAC,CAAC;QAChD,CAAC,CAAC;QAEF;;;;;;;;;WASG;QACH,SAAI,GAAG,CACL,cAAsC;QACtC,gCAAgC;QAChC,8DAA8D;QAC9D,QAAqD,EACrD,EAAE;YACF,IAAI,OAAO,QAAQ,KAAK,UAAU,EAAE;gBAClC,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;aAC5D;YACD,MAAM,cAAc,GAClB,qCAAqC,CAAC,cAAc,CAAC,CAAC;YACxD,uBAAA,IAAI,gCAAQ,CAAC,MAAM,CAAC,cAAc,EAAE,QAAQ,CAAC,CAAC;QAChD,CAAC,CAAC;QA7EA,uBAAA,IAAI,4BAAW,MAAM,MAAA,CAAC;IACxB,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,OAAO,CACX,cAAsC;QAEtC,MAAM,cAAc,GAClB,qCAAqC,CAAC,cAAc,CAAC,CAAC;QACxD,MAAM,QAAQ,GAAG,MAAM,uBAAA,IAAI,gCAAQ,CAAC,MAAM,CAGxC,cAAc,CAAC,CAAC;QAElB,IAAI,QAAQ,IAAI,QAAQ,EAAE;YACxB,OAAO,QAAQ,CAAC,MAAM,CAAC;SACxB;QAED,MAAM,KAAK,GAAG,IAAI,yBAAY,CAC5B,QAAQ,CAAC,KAAK,CAAC,IAAI,EACnB,QAAQ,CAAC,KAAK,CAAC,OAAO,EACtB,QAAQ,CAAC,KAAK,CAAC,IAAI,CACpB,CAAC;QACF,IAAI,OAAO,IAAI,QAAQ,CAAC,KAAK,EAAE;YAC7B,KAAK,CAAC,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC;SACpC;QACD,MAAM,KAAK,CAAC;IACd,CAAC;CA8CF;AAxFD,4CAwFC","sourcesContent":["import type { JsonRpcEngine } from '@metamask/json-rpc-engine';\nimport { JsonRpcError } from '@metamask/rpc-errors';\nimport type {\n Json,\n JsonRpcId,\n JsonRpcParams,\n JsonRpcRequest,\n JsonRpcVersion2,\n} from '@metamask/utils';\nimport { v4 as uuidV4 } from 'uuid';\n\n/**\n * A JSON-RPC request conforming to the EIP-1193 specification.\n */\ntype Eip1193Request<Params extends JsonRpcParams> = {\n id?: JsonRpcId;\n jsonrpc?: JsonRpcVersion2;\n method: string;\n params?: Params;\n};\n\n/**\n * Converts an EIP-1193 request to a JSON-RPC request.\n *\n * @param eip1193Request - The EIP-1193 request to convert.\n * @returns The corresponding JSON-RPC request.\n */\nexport function convertEip1193RequestToJsonRpcRequest<\n Params extends JsonRpcParams,\n>(\n eip1193Request: Eip1193Request<Params>,\n): JsonRpcRequest<Params | Record<never, never>> {\n const { id = uuidV4(), jsonrpc = '2.0', method, params } = eip1193Request;\n return params\n ? {\n id,\n jsonrpc,\n method,\n params,\n }\n : {\n id,\n jsonrpc,\n method,\n };\n}\n\n/**\n * An Ethereum provider.\n *\n * This provider loosely follows conventions that pre-date EIP-1193.\n * It is not compliant with any Ethereum provider standard.\n */\nexport class InternalProvider {\n readonly #engine: JsonRpcEngine;\n\n /**\n * Construct a InternalProvider from a JSON-RPC engine.\n *\n * @param options - Options.\n * @param options.engine - The JSON-RPC engine used to process requests.\n */\n constructor({ engine }: { engine: JsonRpcEngine }) {\n this.#engine = engine;\n }\n\n /**\n * Send a provider request asynchronously.\n *\n * @param eip1193Request - The request to send.\n * @returns The JSON-RPC response.\n */\n async request<Params extends JsonRpcParams, Result extends Json>(\n eip1193Request: Eip1193Request<Params>,\n ): Promise<Result> {\n const jsonRpcRequest =\n convertEip1193RequestToJsonRpcRequest(eip1193Request);\n const response = await this.#engine.handle<\n Params | Record<never, never>,\n Result\n >(jsonRpcRequest);\n\n if ('result' in response) {\n return response.result;\n }\n\n const error = new JsonRpcError(\n response.error.code,\n response.error.message,\n response.error.data,\n );\n if ('stack' in response.error) {\n error.stack = response.error.stack;\n }\n throw error;\n }\n\n /**\n * Send a provider request asynchronously.\n *\n * This method serves the same purpose as `request`. It only exists for\n * legacy reasons.\n *\n * @param eip1193Request - The request to send.\n * @param callback - A function that is called upon the success or failure of the request.\n * @deprecated Please use `request` instead.\n */\n sendAsync = <Params extends JsonRpcParams>(\n eip1193Request: Eip1193Request<Params>,\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n callback: (error: unknown, providerRes?: any) => void,\n ) => {\n const jsonRpcRequest =\n convertEip1193RequestToJsonRpcRequest(eip1193Request);\n this.#engine.handle(jsonRpcRequest, callback);\n };\n\n /**\n * Send a provider request asynchronously.\n *\n * This method serves the same purpose as `request`. It only exists for\n * legacy reasons.\n *\n * @param eip1193Request - The request to send.\n * @param callback - A function that is called upon the success or failure of the request.\n * @deprecated Please use `request` instead.\n */\n send = <Params extends JsonRpcParams>(\n eip1193Request: Eip1193Request<Params>,\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n callback: (error: unknown, providerRes?: any) => void,\n ) => {\n if (typeof callback !== 'function') {\n throw new Error('Must provide callback to \"send\" method.');\n }\n const jsonRpcRequest =\n convertEip1193RequestToJsonRpcRequest(eip1193Request);\n this.#engine.handle(jsonRpcRequest, callback);\n };\n}\n"]}
1
+ {"version":3,"file":"internal-provider.cjs","sourceRoot":"","sources":["../src/internal-provider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+DAA+E;AAE/E,qDAA6D;AAC7D,qDAAoD;AAEpD,2CAQyB;AACzB,+BAAoC;AAoCpC;;;;;GAKG;AACH,MAAa,gBAAgB;IAK3B;;;;;;OAMG;IACH,YAAY,OAA4B;QAT/B,2CAAmC;QAwC5C;;;;;;;;;WASG;QACH,cAAS,GAAG,CACV,cAAsC;QACtC,mDAAmD;QACnD,8DAA8D;QAC9D,QAAqD,EACrD,EAAE;YACF,MAAM,cAAc,GAClB,qCAAqC,CAAC,cAAc,CAAC,CAAC;YACxD,uBAAA,IAAI,4CAAoB,MAAxB,IAAI,EAAqB,cAAc,EAAE,QAAQ,CAAC,CAAC;QACrD,CAAC,CAAC;QAEF;;;;;;;;;WASG;QACH,SAAI,GAAG,CACL,cAAsC;QACtC,gCAAgC;QAChC,8DAA8D;QAC9D,QAAqD,EACrD,EAAE;YACF,IAAI,OAAO,QAAQ,KAAK,UAAU,EAAE;gBAClC,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;aAC5D;YACD,MAAM,cAAc,GAClB,qCAAqC,CAAC,cAAc,CAAC,CAAC;YACxD,uBAAA,IAAI,4CAAoB,MAAxB,IA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,MAAM,uBAAA,IAAI,gCAAQ,MAAZ,IAAI,EAAS,cAAc,CAAC,CAAC;QAErC,IAAI,QAAQ,IAAI,QAAQ,EAAE;YACxB,OAAO,QAAQ,CAAC,MAAM,CAAC;SACxB;QACD,MAAM,gBAAgB,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACzC,CAAC;CAyEF;AAlHD,4CAkHC;;AAED;;;;;GAKG;AACH,SAAgB,qCAAqC,CAGnD,cAAsC;IAEtC,MAAM,EAAE,EAAE,GAAG,IAAA,SAAM,GAAE,EAAE,OAAO,GAAG,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,cAAc,CAAC;IAC1E,OAAO,MAAM;QACX,CAAC,CAAC;YACE,EAAE;YACF,OAAO;YACP,MAAM;YACN,MAAM;SACP;QACH,CAAC,CAAC;YACE,EAAE;YACF,OAAO;YACP,MAAM;SACP,CAAC;AACR,CAAC;AAlBD,sFAkBC;AAED;;;;;;GAMG;AACH,SAAS,gBAAgB,CAAC,KAA8B;IACtD,OAAO,IAAI,yBAAY,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;AACjE,CAAC","sourcesContent":["import { asV2Middleware, type JsonRpcEngine } from '@metamask/json-rpc-engine';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine/v2';\nimport { JsonRpcServer } from '@metamask/json-rpc-engine/v2';\nimport { JsonRpcError } from '@metamask/rpc-errors';\nimport type { JsonRpcFailure } from '@metamask/utils';\nimport {\n hasProperty,\n type Json,\n type JsonRpcId,\n type JsonRpcParams,\n type JsonRpcRequest,\n type JsonRpcResponse,\n type JsonRpcVersion2,\n} from '@metamask/utils';\nimport { v4 as uuidV4 } from 'uuid';\n\n/**\n * A JSON-RPC request conforming to the EIP-1193 specification.\n */\ntype Eip1193Request<Params extends JsonRpcParams> = {\n id?: JsonRpcId;\n jsonrpc?: JsonRpcVersion2;\n method: string;\n params?: Params;\n};\n\n/**\n * The {@link JsonRpcMiddleware} constraint and default type for the {@link InternalProvider}.\n * We care that the middleware can handle JSON-RPC requests, but do not care about the context,\n * the validity of which is enforced by the {@link JsonRpcServer}.\n */\nexport type InternalProviderMiddleware = JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n // Non-polluting `any` constraint.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any\n>;\n\ntype Options<Middleware extends InternalProviderMiddleware> =\n | {\n /**\n * @deprecated Use `server` instead.\n */\n engine: JsonRpcEngine;\n }\n | {\n server: JsonRpcServer<Middleware>;\n };\n\n/**\n * An Ethereum provider.\n *\n * This provider loosely follows conventions that pre-date EIP-1193.\n * It is not compliant with any Ethereum provider standard.\n */\nexport class InternalProvider<\n Middleware extends InternalProviderMiddleware = InternalProviderMiddleware,\n> {\n readonly #server: JsonRpcServer<Middleware>;\n\n /**\n * Construct a InternalProvider from a JSON-RPC server or legacy engine.\n *\n * @param options - Options.\n * @param options.engine - **Deprecated:** The JSON-RPC engine used to process requests. Mutually exclusive with `server`.\n * @param options.server - The JSON-RPC server used to process requests. Mutually exclusive with `engine`.\n */\n constructor(options: Options<Middleware>) {\n const serverOrLegacyEngine =\n 'server' in options ? options.server : options.engine;\n this.#server =\n 'push' in serverOrLegacyEngine\n ? new JsonRpcServer({\n middleware: [asV2Middleware(serverOrLegacyEngine)],\n })\n : serverOrLegacyEngine;\n }\n\n /**\n * Send a provider request asynchronously.\n *\n * @param eip1193Request - The request to send.\n * @returns The JSON-RPC response.\n */\n async request<Params extends JsonRpcParams, Result extends Json>(\n eip1193Request: Eip1193Request<Params>,\n ): Promise<Result> {\n const jsonRpcRequest =\n convertEip1193RequestToJsonRpcRequest(eip1193Request);\n const response: JsonRpcResponse<Result> =\n await this.#handle(jsonRpcRequest);\n\n if ('result' in response) {\n return response.result;\n }\n throw deserializeError(response.error);\n }\n\n /**\n * Send a provider request asynchronously.\n *\n * This method serves the same purpose as `request`. It only exists for\n * legacy reasons.\n *\n * @param eip1193Request - The request to send.\n * @param callback - A function that is called upon the success or failure of the request.\n * @deprecated Use {@link request} instead.\n */\n sendAsync = <Params extends JsonRpcParams>(\n eip1193Request: Eip1193Request<Params>,\n // Non-polluting `any` that acts like a constraint.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n callback: (error: unknown, providerRes?: any) => void,\n ) => {\n const jsonRpcRequest =\n convertEip1193RequestToJsonRpcRequest(eip1193Request);\n this.#handleWithCallback(jsonRpcRequest, callback);\n };\n\n /**\n * Send a provider request asynchronously.\n *\n * This method serves the same purpose as `request`. It only exists for\n * legacy reasons.\n *\n * @param eip1193Request - The request to send.\n * @param callback - A function that is called upon the success or failure of the request.\n * @deprecated Use {@link request} instead.\n */\n send = <Params extends JsonRpcParams>(\n eip1193Request: Eip1193Request<Params>,\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n callback: (error: unknown, providerRes?: any) => void,\n ) => {\n if (typeof callback !== 'function') {\n throw new Error('Must provide callback to \"send\" method.');\n }\n const jsonRpcRequest =\n convertEip1193RequestToJsonRpcRequest(eip1193Request);\n this.#handleWithCallback(jsonRpcRequest, callback);\n };\n\n readonly #handle = async <Result extends Json>(\n jsonRpcRequest: JsonRpcRequest,\n ): Promise<JsonRpcResponse<Result>> => {\n // @ts-expect-error - The signatures are incompatible between the legacy engine\n // and server, but this works at runtime.\n return await this.#server.handle(jsonRpcRequest);\n };\n\n readonly #handleWithCallback = (\n jsonRpcRequest: JsonRpcRequest,\n callback: (error: unknown, providerRes?: unknown) => void,\n ): void => {\n /* eslint-disable promise/always-return,promise/no-callback-in-promise */\n this.#handle(jsonRpcRequest)\n .then((response) => {\n if (hasProperty(response, 'result')) {\n callback(null, response);\n } else {\n callback(deserializeError(response.error));\n }\n })\n .catch((error) => {\n callback(error);\n });\n /* eslint-enable promise/always-return,promise/no-callback-in-promise */\n };\n}\n\n/**\n * Convert an EIP-1193 request to a JSON-RPC request.\n *\n * @param eip1193Request - The EIP-1193 request to convert.\n * @returns The JSON-RPC request.\n */\nexport function convertEip1193RequestToJsonRpcRequest<\n Params extends JsonRpcParams,\n>(\n eip1193Request: Eip1193Request<Params>,\n): JsonRpcRequest<Params | Record<never, never>> {\n const { id = uuidV4(), jsonrpc = '2.0', method, params } = eip1193Request;\n return params\n ? {\n id,\n jsonrpc,\n method,\n params,\n }\n : {\n id,\n jsonrpc,\n method,\n };\n}\n\n/**\n * Deserialize a JSON-RPC error. Ignores the possibility of `stack` property, since this is\n * stripped by `JsonRpcServer`.\n *\n * @param error - The JSON-RPC error to deserialize.\n * @returns The deserialized error.\n */\nfunction deserializeError(error: JsonRpcFailure['error']): JsonRpcError<Json> {\n return new JsonRpcError(error.code, error.message, error.data);\n}\n"]}
@@ -1,5 +1,7 @@
1
- import type { JsonRpcEngine } from "@metamask/json-rpc-engine";
2
- import type { Json, JsonRpcId, JsonRpcParams, JsonRpcRequest, JsonRpcVersion2 } from "@metamask/utils";
1
+ import { type JsonRpcEngine } from "@metamask/json-rpc-engine";
2
+ import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine/v2";
3
+ import { JsonRpcServer } from "@metamask/json-rpc-engine/v2";
4
+ import { type Json, type JsonRpcId, type JsonRpcParams, type JsonRpcRequest, type JsonRpcVersion2 } from "@metamask/utils";
3
5
  /**
4
6
  * A JSON-RPC request conforming to the EIP-1193 specification.
5
7
  */
@@ -10,29 +12,35 @@ type Eip1193Request<Params extends JsonRpcParams> = {
10
12
  params?: Params;
11
13
  };
12
14
  /**
13
- * Converts an EIP-1193 request to a JSON-RPC request.
14
- *
15
- * @param eip1193Request - The EIP-1193 request to convert.
16
- * @returns The corresponding JSON-RPC request.
15
+ * The {@link JsonRpcMiddleware} constraint and default type for the {@link InternalProvider}.
16
+ * We care that the middleware can handle JSON-RPC requests, but do not care about the context,
17
+ * the validity of which is enforced by the {@link JsonRpcServer}.
17
18
  */
18
- export declare function convertEip1193RequestToJsonRpcRequest<Params extends JsonRpcParams>(eip1193Request: Eip1193Request<Params>): JsonRpcRequest<Params | Record<never, never>>;
19
+ export type InternalProviderMiddleware = JsonRpcMiddleware<JsonRpcRequest, Json, any>;
20
+ type Options<Middleware extends InternalProviderMiddleware> = {
21
+ /**
22
+ * @deprecated Use `server` instead.
23
+ */
24
+ engine: JsonRpcEngine;
25
+ } | {
26
+ server: JsonRpcServer<Middleware>;
27
+ };
19
28
  /**
20
29
  * An Ethereum provider.
21
30
  *
22
31
  * This provider loosely follows conventions that pre-date EIP-1193.
23
32
  * It is not compliant with any Ethereum provider standard.
24
33
  */
25
- export declare class InternalProvider {
34
+ export declare class InternalProvider<Middleware extends InternalProviderMiddleware = InternalProviderMiddleware> {
26
35
  #private;
27
36
  /**
28
- * Construct a InternalProvider from a JSON-RPC engine.
37
+ * Construct a InternalProvider from a JSON-RPC server or legacy engine.
29
38
  *
30
39
  * @param options - Options.
31
- * @param options.engine - The JSON-RPC engine used to process requests.
40
+ * @param options.engine - **Deprecated:** The JSON-RPC engine used to process requests. Mutually exclusive with `server`.
41
+ * @param options.server - The JSON-RPC server used to process requests. Mutually exclusive with `engine`.
32
42
  */
33
- constructor({ engine }: {
34
- engine: JsonRpcEngine;
35
- });
43
+ constructor(options: Options<Middleware>);
36
44
  /**
37
45
  * Send a provider request asynchronously.
38
46
  *
@@ -48,7 +56,7 @@ export declare class InternalProvider {
48
56
  *
49
57
  * @param eip1193Request - The request to send.
50
58
  * @param callback - A function that is called upon the success or failure of the request.
51
- * @deprecated Please use `request` instead.
59
+ * @deprecated Use {@link request} instead.
52
60
  */
53
61
  sendAsync: <Params extends JsonRpcParams>(eip1193Request: Eip1193Request<Params>, callback: (error: unknown, providerRes?: any) => void) => void;
54
62
  /**
@@ -59,9 +67,16 @@ export declare class InternalProvider {
59
67
  *
60
68
  * @param eip1193Request - The request to send.
61
69
  * @param callback - A function that is called upon the success or failure of the request.
62
- * @deprecated Please use `request` instead.
70
+ * @deprecated Use {@link request} instead.
63
71
  */
64
72
  send: <Params extends JsonRpcParams>(eip1193Request: Eip1193Request<Params>, callback: (error: unknown, providerRes?: any) => void) => void;
65
73
  }
74
+ /**
75
+ * Convert an EIP-1193 request to a JSON-RPC request.
76
+ *
77
+ * @param eip1193Request - The EIP-1193 request to convert.
78
+ * @returns The JSON-RPC request.
79
+ */
80
+ export declare function convertEip1193RequestToJsonRpcRequest<Params extends JsonRpcParams>(eip1193Request: Eip1193Request<Params>): JsonRpcRequest<Params | Record<never, never>>;
66
81
  export {};
67
82
  //# sourceMappingURL=internal-provider.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"internal-provider.d.cts","sourceRoot":"","sources":["../src/internal-provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,kCAAkC;AAE/D,OAAO,KAAK,EACV,IAAI,EACJ,SAAS,EACT,aAAa,EACb,cAAc,EACd,eAAe,EAChB,wBAAwB;AAGzB;;GAEG;AACH,KAAK,cAAc,CAAC,MAAM,SAAS,aAAa,IAAI;IAClD,EAAE,CAAC,EAAE,SAAS,CAAC;IACf,OAAO,CAAC,EAAE,eAAe,CAAC;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,qCAAqC,CACnD,MAAM,SAAS,aAAa,EAE5B,cAAc,EAAE,cAAc,CAAC,MAAM,CAAC,GACrC,cAAc,CAAC,MAAM,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAc/C;AAED;;;;;GAKG;AACH,qBAAa,gBAAgB;;IAG3B;;;;;OAKG;gBACS,EAAE,MAAM,EAAE,EAAE;QAAE,MAAM,EAAE,aAAa,CAAA;KAAE;IAIjD;;;;;OAKG;IACG,OAAO,CAAC,MAAM,SAAS,aAAa,EAAE,MAAM,SAAS,IAAI,EAC7D,cAAc,EAAE,cAAc,CAAC,MAAM,CAAC,GACrC,OAAO,CAAC,MAAM,CAAC;IAuBlB;;;;;;;;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AM,CAAC;IAYlB;;;;;;;;;OASG;IACH,SAAS,2FAIW,OAAO,gBAAgB,GAAG,KAAK,IAAI,UAKrD;IAEF;;;;;;;;;OASG;IACH,IAAI,2FAIgB,OAAO,gBAAgB,GAAG,KAAK,IAAI,UAQrD;CA4BH;AAED;;;;;GAKG;AACH,wBAAgB,qCAAqC,CACnD,MAAM,SAAS,aAAa,EAE5B,cAAc,EAAE,cAAc,CAAC,MAAM,CAAC,GACrC,cAAc,CAAC,MAAM,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAc/C"}
@@ -1,5 +1,7 @@
1
- import type { JsonRpcEngine } from "@metamask/json-rpc-engine";
2
- import type { Json, JsonRpcId, JsonRpcParams, JsonRpcRequest, JsonRpcVersion2 } from "@metamask/utils";
1
+ import { type JsonRpcEngine } from "@metamask/json-rpc-engine";
2
+ import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine/v2";
3
+ import { JsonRpcServer } from "@metamask/json-rpc-engine/v2";
4
+ import { type Json, type JsonRpcId, type JsonRpcParams, type JsonRpcRequest, type JsonRpcVersion2 } from "@metamask/utils";
3
5
  /**
4
6
  * A JSON-RPC request conforming to the EIP-1193 specification.
5
7
  */
@@ -10,29 +12,35 @@ type Eip1193Request<Params extends JsonRpcParams> = {
10
12
  params?: Params;
11
13
  };
12
14
  /**
13
- * Converts an EIP-1193 request to a JSON-RPC request.
14
- *
15
- * @param eip1193Request - The EIP-1193 request to convert.
16
- * @returns The corresponding JSON-RPC request.
15
+ * The {@link JsonRpcMiddleware} constraint and default type for the {@link InternalProvider}.
16
+ * We care that the middleware can handle JSON-RPC requests, but do not care about the context,
17
+ * the validity of which is enforced by the {@link JsonRpcServer}.
17
18
  */
18
- export declare function convertEip1193RequestToJsonRpcRequest<Params extends JsonRpcParams>(eip1193Request: Eip1193Request<Params>): JsonRpcRequest<Params | Record<never, never>>;
19
+ export type InternalProviderMiddleware = JsonRpcMiddleware<JsonRpcRequest, Json, any>;
20
+ type Options<Middleware extends InternalProviderMiddleware> = {
21
+ /**
22
+ * @deprecated Use `server` instead.
23
+ */
24
+ engine: JsonRpcEngine;
25
+ } | {
26
+ server: JsonRpcServer<Middleware>;
27
+ };
19
28
  /**
20
29
  * An Ethereum provider.
21
30
  *
22
31
  * This provider loosely follows conventions that pre-date EIP-1193.
23
32
  * It is not compliant with any Ethereum provider standard.
24
33
  */
25
- export declare class InternalProvider {
34
+ export declare class InternalProvider<Middleware extends InternalProviderMiddleware = InternalProviderMiddleware> {
26
35
  #private;
27
36
  /**
28
- * Construct a InternalProvider from a JSON-RPC engine.
37
+ * Construct a InternalProvider from a JSON-RPC server or legacy engine.
29
38
  *
30
39
  * @param options - Options.
31
- * @param options.engine - The JSON-RPC engine used to process requests.
40
+ * @param options.engine - **Deprecated:** The JSON-RPC engine used to process requests. Mutually exclusive with `server`.
41
+ * @param options.server - The JSON-RPC server used to process requests. Mutually exclusive with `engine`.
32
42
  */
33
- constructor({ engine }: {
34
- engine: JsonRpcEngine;
35
- });
43
+ constructor(options: Options<Middleware>);
36
44
  /**
37
45
  * Send a provider request asynchronously.
38
46
  *
@@ -48,7 +56,7 @@ export declare class InternalProvider {
48
56
  *
49
57
  * @param eip1193Request - The request to send.
50
58
  * @param callback - A function that is called upon the success or failure of the request.
51
- * @deprecated Please use `request` instead.
59
+ * @deprecated Use {@link request} instead.
52
60
  */
53
61
  sendAsync: <Params extends JsonRpcParams>(eip1193Request: Eip1193Request<Params>, callback: (error: unknown, providerRes?: any) => void) => void;
54
62
  /**
@@ -59,9 +67,16 @@ export declare class InternalProvider {
59
67
  *
60
68
  * @param eip1193Request - The request to send.
61
69
  * @param callback - A function that is called upon the success or failure of the request.
62
- * @deprecated Please use `request` instead.
70
+ * @deprecated Use {@link request} instead.
63
71
  */
64
72
  send: <Params extends JsonRpcParams>(eip1193Request: Eip1193Request<Params>, callback: (error: unknown, providerRes?: any) => void) => void;
65
73
  }
74
+ /**
75
+ * Convert an EIP-1193 request to a JSON-RPC request.
76
+ *
77
+ * @param eip1193Request - The EIP-1193 request to convert.
78
+ * @returns The JSON-RPC request.
79
+ */
80
+ export declare function convertEip1193RequestToJsonRpcRequest<Params extends JsonRpcParams>(eip1193Request: Eip1193Request<Params>): JsonRpcRequest<Params | Record<never, never>>;
66
81
  export {};
67
82
  //# sourceMappingURL=internal-provider.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"internal-provider.d.mts","sourceRoot":"","sources":["../src/internal-provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,kCAAkC;AAE/D,OAAO,KAAK,EACV,IAAI,EACJ,SAAS,EACT,aAAa,EACb,cAAc,EACd,eAAe,EAChB,wBAAwB;AAGzB;;GAEG;AACH,KAAK,cAAc,CAAC,MAAM,SAAS,aAAa,IAAI;IAClD,EAAE,CAAC,EAAE,SAAS,CAAC;IACf,OAAO,CAAC,EAAE,eAAe,CAAC;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,qCAAqC,CACnD,MAAM,SAAS,aAAa,EAE5B,cAAc,EAAE,cAAc,CAAC,MAAM,CAAC,GACrC,cAAc,CAAC,MAAM,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAc/C;AAED;;;;;GAKG;AACH,qBAAa,gBAAgB;;IAG3B;;;;;OAKG;gBACS,EAAE,MAAM,EAAE,EAAE;QAAE,MAAM,EAAE,aAAa,CAAA;KAAE;IAIjD;;;;;OAKG;IACG,OAAO,CAAC,MAAM,SAAS,aAAa,EAAE,MAAM,SAAS,IAAI,EAC7D,cAAc,EAAE,cAAc,CAAC,MAAM,CAAC,GACrC,OAAO,CAAC,MAAM,CAAC;IAuBlB;;;;;;;;;OASG;IACH,SAAS,2FAIW,OAAO,gBAAgB,GAAG,KAAK,IAAI,UAKrD;IAEF;;;;;;;;;OASG;IACH,IAAI,2FAIgB,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;OASG;IACH,IAAI,2FAIgB,OAAO,gBAAgB,GAAG,KAAK,IAAI,UAQrD;CA4BH;AAED;;;;;GAKG;AACH,wBAAgB,qCAAqC,CACnD,MAAM,SAAS,aAAa,EAE5B,cAAc,EAAE,cAAc,CAAC,MAAM,CAAC,GACrC,cAAc,CAAC,MAAM,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAc/C"}
@@ -9,30 +9,12 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _InternalProvider_engine;
12
+ var _InternalProvider_server, _InternalProvider_handle, _InternalProvider_handleWithCallback;
13
+ import { asV2Middleware } from "@metamask/json-rpc-engine";
14
+ import { JsonRpcServer } from "@metamask/json-rpc-engine/v2";
13
15
  import { JsonRpcError } from "@metamask/rpc-errors";
16
+ import { hasProperty } from "@metamask/utils";
14
17
  import { v4 as uuidV4 } from "uuid";
15
- /**
16
- * Converts an EIP-1193 request to a JSON-RPC request.
17
- *
18
- * @param eip1193Request - The EIP-1193 request to convert.
19
- * @returns The corresponding JSON-RPC request.
20
- */
21
- export function convertEip1193RequestToJsonRpcRequest(eip1193Request) {
22
- const { id = uuidV4(), jsonrpc = '2.0', method, params } = eip1193Request;
23
- return params
24
- ? {
25
- id,
26
- jsonrpc,
27
- method,
28
- params,
29
- }
30
- : {
31
- id,
32
- jsonrpc,
33
- method,
34
- };
35
- }
36
18
  /**
37
19
  * An Ethereum provider.
38
20
  *
@@ -41,13 +23,14 @@ export function convertEip1193RequestToJsonRpcRequest(eip1193Request) {
41
23
  */
42
24
  export class InternalProvider {
43
25
  /**
44
- * Construct a InternalProvider from a JSON-RPC engine.
26
+ * Construct a InternalProvider from a JSON-RPC server or legacy engine.
45
27
  *
46
28
  * @param options - Options.
47
- * @param options.engine - The JSON-RPC engine used to process requests.
29
+ * @param options.engine - **Deprecated:** The JSON-RPC engine used to process requests. Mutually exclusive with `server`.
30
+ * @param options.server - The JSON-RPC server used to process requests. Mutually exclusive with `engine`.
48
31
  */
49
- constructor({ engine }) {
50
- _InternalProvider_engine.set(this, void 0);
32
+ constructor(options) {
33
+ _InternalProvider_server.set(this, void 0);
51
34
  /**
52
35
  * Send a provider request asynchronously.
53
36
  *
@@ -56,14 +39,14 @@ export class InternalProvider {
56
39
  *
57
40
  * @param eip1193Request - The request to send.
58
41
  * @param callback - A function that is called upon the success or failure of the request.
59
- * @deprecated Please use `request` instead.
42
+ * @deprecated Use {@link request} instead.
60
43
  */
61
44
  this.sendAsync = (eip1193Request,
62
- // TODO: Replace `any` with type
45
+ // Non-polluting `any` that acts like a constraint.
63
46
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
64
47
  callback) => {
65
48
  const jsonRpcRequest = convertEip1193RequestToJsonRpcRequest(eip1193Request);
66
- __classPrivateFieldGet(this, _InternalProvider_engine, "f").handle(jsonRpcRequest, callback);
49
+ __classPrivateFieldGet(this, _InternalProvider_handleWithCallback, "f").call(this, jsonRpcRequest, callback);
67
50
  };
68
51
  /**
69
52
  * Send a provider request asynchronously.
@@ -73,7 +56,7 @@ export class InternalProvider {
73
56
  *
74
57
  * @param eip1193Request - The request to send.
75
58
  * @param callback - A function that is called upon the success or failure of the request.
76
- * @deprecated Please use `request` instead.
59
+ * @deprecated Use {@link request} instead.
77
60
  */
78
61
  this.send = (eip1193Request,
79
62
  // TODO: Replace `any` with type
@@ -83,9 +66,35 @@ export class InternalProvider {
83
66
  throw new Error('Must provide callback to "send" method.');
84
67
  }
85
68
  const jsonRpcRequest = convertEip1193RequestToJsonRpcRequest(eip1193Request);
86
- __classPrivateFieldGet(this, _InternalProvider_engine, "f").handle(jsonRpcRequest, callback);
69
+ __classPrivateFieldGet(this, _InternalProvider_handleWithCallback, "f").call(this, jsonRpcRequest, callback);
87
70
  };
88
- __classPrivateFieldSet(this, _InternalProvider_engine, engine, "f");
71
+ _InternalProvider_handle.set(this, async (jsonRpcRequest) => {
72
+ // @ts-expect-error - The signatures are incompatible between the legacy engine
73
+ // and server, but this works at runtime.
74
+ return await __classPrivateFieldGet(this, _InternalProvider_server, "f").handle(jsonRpcRequest);
75
+ });
76
+ _InternalProvider_handleWithCallback.set(this, (jsonRpcRequest, callback) => {
77
+ /* eslint-disable promise/always-return,promise/no-callback-in-promise */
78
+ __classPrivateFieldGet(this, _InternalProvider_handle, "f").call(this, jsonRpcRequest)
79
+ .then((response) => {
80
+ if (hasProperty(response, 'result')) {
81
+ callback(null, response);
82
+ }
83
+ else {
84
+ callback(deserializeError(response.error));
85
+ }
86
+ })
87
+ .catch((error) => {
88
+ callback(error);
89
+ });
90
+ /* eslint-enable promise/always-return,promise/no-callback-in-promise */
91
+ });
92
+ const serverOrLegacyEngine = 'server' in options ? options.server : options.engine;
93
+ __classPrivateFieldSet(this, _InternalProvider_server, 'push' in serverOrLegacyEngine
94
+ ? new JsonRpcServer({
95
+ middleware: [asV2Middleware(serverOrLegacyEngine)],
96
+ })
97
+ : serverOrLegacyEngine, "f");
89
98
  }
90
99
  /**
91
100
  * Send a provider request asynchronously.
@@ -95,16 +104,43 @@ export class InternalProvider {
95
104
  */
96
105
  async request(eip1193Request) {
97
106
  const jsonRpcRequest = convertEip1193RequestToJsonRpcRequest(eip1193Request);
98
- const response = await __classPrivateFieldGet(this, _InternalProvider_engine, "f").handle(jsonRpcRequest);
107
+ const response = await __classPrivateFieldGet(this, _InternalProvider_handle, "f").call(this, jsonRpcRequest);
99
108
  if ('result' in response) {
100
109
  return response.result;
101
110
  }
102
- const error = new JsonRpcError(response.error.code, response.error.message, response.error.data);
103
- if ('stack' in response.error) {
104
- error.stack = response.error.stack;
105
- }
106
- throw error;
111
+ throw deserializeError(response.error);
107
112
  }
108
113
  }
109
- _InternalProvider_engine = new WeakMap();
114
+ _InternalProvider_server = new WeakMap(), _InternalProvider_handle = new WeakMap(), _InternalProvider_handleWithCallback = new WeakMap();
115
+ /**
116
+ * Convert an EIP-1193 request to a JSON-RPC request.
117
+ *
118
+ * @param eip1193Request - The EIP-1193 request to convert.
119
+ * @returns The JSON-RPC request.
120
+ */
121
+ export function convertEip1193RequestToJsonRpcRequest(eip1193Request) {
122
+ const { id = uuidV4(), jsonrpc = '2.0', method, params } = eip1193Request;
123
+ return params
124
+ ? {
125
+ id,
126
+ jsonrpc,
127
+ method,
128
+ params,
129
+ }
130
+ : {
131
+ id,
132
+ jsonrpc,
133
+ method,
134
+ };
135
+ }
136
+ /**
137
+ * Deserialize a JSON-RPC error. Ignores the possibility of `stack` property, since this is
138
+ * stripped by `JsonRpcServer`.
139
+ *
140
+ * @param error - The JSON-RPC error to deserialize.
141
+ * @returns The deserialized error.
142
+ */
143
+ function deserializeError(error) {
144
+ return new JsonRpcError(error.code, error.message, error.data);
145
+ }
110
146
  //# sourceMappingURL=internal-provider.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"internal-provider.mjs","sourceRoot":"","sources":["../src/internal-provider.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,YAAY,EAAE,6BAA6B;AAQpD,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,aAAa;AAYpC;;;;;GAKG;AACH,MAAM,UAAU,qCAAqC,CAGnD,cAAsC;IAEtC,MAAM,EAAE,EAAE,GAAG,MAAM,EAAE,EAAE,OAAO,GAAG,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,cAAc,CAAC;IAC1E,OAAO,MAAM;QACX,CAAC,CAAC;YACE,EAAE;YACF,OAAO;YACP,MAAM;YACN,MAAM;SACP;QACH,CAAC,CAAC;YACE,EAAE;YACF,OAAO;YACP,MAAM;SACP,CAAC;AACR,CAAC;AAED;;;;;GAKG;AACH,MAAM,OAAO,gBAAgB;IAG3B;;;;;OAKG;IACH,YAAY,EAAE,MAAM,EAA6B;QARxC,2CAAuB;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C;QACD,MAAM,KAAK,CAAC;IACd,CAAC;CA8CF","sourcesContent":["import type { JsonRpcEngine } from '@metamask/json-rpc-engine';\nimport { JsonRpcError } from '@metamask/rpc-errors';\nimport type {\n Json,\n JsonRpcId,\n JsonRpcParams,\n JsonRpcRequest,\n JsonRpcVersion2,\n} from '@metamask/utils';\nimport { v4 as uuidV4 } from 'uuid';\n\n/**\n * A JSON-RPC request conforming to the EIP-1193 specification.\n */\ntype Eip1193Request<Params extends JsonRpcParams> = {\n id?: JsonRpcId;\n jsonrpc?: JsonRpcVersion2;\n method: string;\n params?: Params;\n};\n\n/**\n * Converts an EIP-1193 request to a JSON-RPC request.\n *\n * @param eip1193Request - The EIP-1193 request to convert.\n * @returns The corresponding JSON-RPC request.\n */\nexport function convertEip1193RequestToJsonRpcRequest<\n Params extends JsonRpcParams,\n>(\n eip1193Request: Eip1193Request<Params>,\n): JsonRpcRequest<Params | Record<never, never>> {\n const { id = uuidV4(), jsonrpc = '2.0', method, params } = eip1193Request;\n return params\n ? {\n id,\n jsonrpc,\n method,\n params,\n }\n : {\n id,\n jsonrpc,\n method,\n };\n}\n\n/**\n * An Ethereum provider.\n *\n * This provider loosely follows conventions that pre-date EIP-1193.\n * It is not compliant with any Ethereum provider standard.\n */\nexport class InternalProvider {\n readonly #engine: JsonRpcEngine;\n\n /**\n * Construct a InternalProvider from a JSON-RPC engine.\n *\n * @param options - Options.\n * @param options.engine - The JSON-RPC engine used to process requests.\n */\n constructor({ engine }: { engine: JsonRpcEngine }) {\n this.#engine = engine;\n }\n\n /**\n * Send a provider request asynchronously.\n *\n * @param eip1193Request - The request to send.\n * @returns The JSON-RPC response.\n */\n async request<Params extends JsonRpcParams, Result extends Json>(\n eip1193Request: Eip1193Request<Params>,\n ): Promise<Result> {\n const jsonRpcRequest =\n convertEip1193RequestToJsonRpcRequest(eip1193Request);\n const response = await this.#engine.handle<\n Params | Record<never, never>,\n Result\n >(jsonRpcRequest);\n\n if ('result' in response) {\n return response.result;\n }\n\n const error = new JsonRpcError(\n response.error.code,\n response.error.message,\n response.error.data,\n );\n if ('stack' in response.error) {\n error.stack = response.error.stack;\n }\n throw error;\n }\n\n /**\n * Send a provider request asynchronously.\n *\n * This method serves the same purpose as `request`. It only exists for\n * legacy reasons.\n *\n * @param eip1193Request - The request to send.\n * @param callback - A function that is called upon the success or failure of the request.\n * @deprecated Please use `request` instead.\n */\n sendAsync = <Params extends JsonRpcParams>(\n eip1193Request: Eip1193Request<Params>,\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n callback: (error: unknown, providerRes?: any) => void,\n ) => {\n const jsonRpcRequest =\n convertEip1193RequestToJsonRpcRequest(eip1193Request);\n this.#engine.handle(jsonRpcRequest, callback);\n };\n\n /**\n * Send a provider request asynchronously.\n *\n * This method serves the same purpose as `request`. It only exists for\n * legacy reasons.\n *\n * @param eip1193Request - The request to send.\n * @param callback - A function that is called upon the success or failure of the request.\n * @deprecated Please use `request` instead.\n */\n send = <Params extends JsonRpcParams>(\n eip1193Request: Eip1193Request<Params>,\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n callback: (error: unknown, providerRes?: any) => void,\n ) => {\n if (typeof callback !== 'function') {\n throw new Error('Must provide callback to \"send\" method.');\n }\n const jsonRpcRequest =\n convertEip1193RequestToJsonRpcRequest(eip1193Request);\n this.#engine.handle(jsonRpcRequest, callback);\n };\n}\n"]}
1
+ {"version":3,"file":"internal-provider.mjs","sourceRoot":"","sources":["../src/internal-provider.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,cAAc,EAAsB,kCAAkC;AAE/E,OAAO,EAAE,aAAa,EAAE,qCAAqC;AAC7D,OAAO,EAAE,YAAY,EAAE,6BAA6B;AAEpD,OAAO,EACL,WAAW,EAOZ,wBAAwB;AACzB,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,aAAa;AAoCpC;;;;;GAKG;AACH,MAAM,OAAO,gBAAgB;IA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AAC,IAAI,EAAE,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;AACjE,CAAC","sourcesContent":["import { asV2Middleware, type JsonRpcEngine } from '@metamask/json-rpc-engine';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine/v2';\nimport { JsonRpcServer } from '@metamask/json-rpc-engine/v2';\nimport { JsonRpcError } from '@metamask/rpc-errors';\nimport type { JsonRpcFailure } from '@metamask/utils';\nimport {\n hasProperty,\n type Json,\n type JsonRpcId,\n type JsonRpcParams,\n type JsonRpcRequest,\n type JsonRpcResponse,\n type JsonRpcVersion2,\n} from '@metamask/utils';\nimport { v4 as uuidV4 } from 'uuid';\n\n/**\n * A JSON-RPC request conforming to the EIP-1193 specification.\n */\ntype Eip1193Request<Params extends JsonRpcParams> = {\n id?: JsonRpcId;\n jsonrpc?: JsonRpcVersion2;\n method: string;\n params?: Params;\n};\n\n/**\n * The {@link JsonRpcMiddleware} constraint and default type for the {@link InternalProvider}.\n * We care that the middleware can handle JSON-RPC requests, but do not care about the context,\n * the validity of which is enforced by the {@link JsonRpcServer}.\n */\nexport type InternalProviderMiddleware = JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n // Non-polluting `any` constraint.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any\n>;\n\ntype Options<Middleware extends InternalProviderMiddleware> =\n | {\n /**\n * @deprecated Use `server` instead.\n */\n engine: JsonRpcEngine;\n }\n | {\n server: JsonRpcServer<Middleware>;\n };\n\n/**\n * An Ethereum provider.\n *\n * This provider loosely follows conventions that pre-date EIP-1193.\n * It is not compliant with any Ethereum provider standard.\n */\nexport class InternalProvider<\n Middleware extends InternalProviderMiddleware = InternalProviderMiddleware,\n> {\n readonly #server: JsonRpcServer<Middleware>;\n\n /**\n * Construct a InternalProvider from a JSON-RPC server or legacy engine.\n *\n * @param options - Options.\n * @param options.engine - **Deprecated:** The JSON-RPC engine used to process requests. Mutually exclusive with `server`.\n * @param options.server - The JSON-RPC server used to process requests. Mutually exclusive with `engine`.\n */\n constructor(options: Options<Middleware>) {\n const serverOrLegacyEngine =\n 'server' in options ? options.server : options.engine;\n this.#server =\n 'push' in serverOrLegacyEngine\n ? new JsonRpcServer({\n middleware: [asV2Middleware(serverOrLegacyEngine)],\n })\n : serverOrLegacyEngine;\n }\n\n /**\n * Send a provider request asynchronously.\n *\n * @param eip1193Request - The request to send.\n * @returns The JSON-RPC response.\n */\n async request<Params extends JsonRpcParams, Result extends Json>(\n eip1193Request: Eip1193Request<Params>,\n ): Promise<Result> {\n const jsonRpcRequest =\n convertEip1193RequestToJsonRpcRequest(eip1193Request);\n const response: JsonRpcResponse<Result> =\n await this.#handle(jsonRpcRequest);\n\n if ('result' in response) {\n return response.result;\n }\n throw deserializeError(response.error);\n }\n\n /**\n * Send a provider request asynchronously.\n *\n * This method serves the same purpose as `request`. It only exists for\n * legacy reasons.\n *\n * @param eip1193Request - The request to send.\n * @param callback - A function that is called upon the success or failure of the request.\n * @deprecated Use {@link request} instead.\n */\n sendAsync = <Params extends JsonRpcParams>(\n eip1193Request: Eip1193Request<Params>,\n // Non-polluting `any` that acts like a constraint.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n callback: (error: unknown, providerRes?: any) => void,\n ) => {\n const jsonRpcRequest =\n convertEip1193RequestToJsonRpcRequest(eip1193Request);\n this.#handleWithCallback(jsonRpcRequest, callback);\n };\n\n /**\n * Send a provider request asynchronously.\n *\n * This method serves the same purpose as `request`. It only exists for\n * legacy reasons.\n *\n * @param eip1193Request - The request to send.\n * @param callback - A function that is called upon the success or failure of the request.\n * @deprecated Use {@link request} instead.\n */\n send = <Params extends JsonRpcParams>(\n eip1193Request: Eip1193Request<Params>,\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n callback: (error: unknown, providerRes?: any) => void,\n ) => {\n if (typeof callback !== 'function') {\n throw new Error('Must provide callback to \"send\" method.');\n }\n const jsonRpcRequest =\n convertEip1193RequestToJsonRpcRequest(eip1193Request);\n this.#handleWithCallback(jsonRpcRequest, callback);\n };\n\n readonly #handle = async <Result extends Json>(\n jsonRpcRequest: JsonRpcRequest,\n ): Promise<JsonRpcResponse<Result>> => {\n // @ts-expect-error - The signatures are incompatible between the legacy engine\n // and server, but this works at runtime.\n return await this.#server.handle(jsonRpcRequest);\n };\n\n readonly #handleWithCallback = (\n jsonRpcRequest: JsonRpcRequest,\n callback: (error: unknown, providerRes?: unknown) => void,\n ): void => {\n /* eslint-disable promise/always-return,promise/no-callback-in-promise */\n this.#handle(jsonRpcRequest)\n .then((response) => {\n if (hasProperty(response, 'result')) {\n callback(null, response);\n } else {\n callback(deserializeError(response.error));\n }\n })\n .catch((error) => {\n callback(error);\n });\n /* eslint-enable promise/always-return,promise/no-callback-in-promise */\n };\n}\n\n/**\n * Convert an EIP-1193 request to a JSON-RPC request.\n *\n * @param eip1193Request - The EIP-1193 request to convert.\n * @returns The JSON-RPC request.\n */\nexport function convertEip1193RequestToJsonRpcRequest<\n Params extends JsonRpcParams,\n>(\n eip1193Request: Eip1193Request<Params>,\n): JsonRpcRequest<Params | Record<never, never>> {\n const { id = uuidV4(), jsonrpc = '2.0', method, params } = eip1193Request;\n return params\n ? {\n id,\n jsonrpc,\n method,\n params,\n }\n : {\n id,\n jsonrpc,\n method,\n };\n}\n\n/**\n * Deserialize a JSON-RPC error. Ignores the possibility of `stack` property, since this is\n * stripped by `JsonRpcServer`.\n *\n * @param error - The JSON-RPC error to deserialize.\n * @returns The deserialized error.\n */\nfunction deserializeError(error: JsonRpcFailure['error']): JsonRpcError<Json> {\n return new JsonRpcError(error.code, error.message, error.data);\n}\n"]}
@@ -3,10 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.providerFromEngine = void 0;
4
4
  const internal_provider_1 = require("./internal-provider.cjs");
5
5
  /**
6
- * Construct an Ethereum provider from the given JSON-RPC engine.
6
+ * Construct an Ethereum provider from a JSON-RPC engine.
7
7
  *
8
8
  * @param engine - The JSON-RPC engine to construct a provider from.
9
9
  * @returns An Ethereum provider.
10
+ * @deprecated Just use {@link InternalProvider} directly instead.
10
11
  */
11
12
  function providerFromEngine(engine) {
12
13
  return new internal_provider_1.InternalProvider({ engine });
@@ -1 +1 @@
1
- {"version":3,"file":"provider-from-engine.cjs","sourceRoot":"","sources":["../src/provider-from-engine.ts"],"names":[],"mappings":";;;AAEA,+DAAuD;AAEvD;;;;;GAKG;AACH,SAAgB,kBAAkB,CAAC,MAAqB;IACtD,OAAO,IAAI,oCAAgB,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;AAC1C,CAAC;AAFD,gDAEC","sourcesContent":["import type { JsonRpcEngine } from '@metamask/json-rpc-engine';\n\nimport { InternalProvider } from './internal-provider';\n\n/**\n * Construct an Ethereum provider from the given JSON-RPC engine.\n *\n * @param engine - The JSON-RPC engine to construct a provider from.\n * @returns An Ethereum provider.\n */\nexport function providerFromEngine(engine: JsonRpcEngine): InternalProvider {\n return new InternalProvider({ engine });\n}\n"]}
1
+ {"version":3,"file":"provider-from-engine.cjs","sourceRoot":"","sources":["../src/provider-from-engine.ts"],"names":[],"mappings":";;;AAEA,+DAAuD;AAEvD;;;;;;GAMG;AACH,SAAgB,kBAAkB,CAAC,MAAqB;IACtD,OAAO,IAAI,oCAAgB,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;AAC1C,CAAC;AAFD,gDAEC","sourcesContent":["import type { JsonRpcEngine } from '@metamask/json-rpc-engine';\n\nimport { InternalProvider } from './internal-provider';\n\n/**\n * Construct an Ethereum provider from a JSON-RPC engine.\n *\n * @param engine - The JSON-RPC engine to construct a provider from.\n * @returns An Ethereum provider.\n * @deprecated Just use {@link InternalProvider} directly instead.\n */\nexport function providerFromEngine(engine: JsonRpcEngine): InternalProvider {\n return new InternalProvider({ engine });\n}\n"]}
@@ -1,10 +1,11 @@
1
1
  import type { JsonRpcEngine } from "@metamask/json-rpc-engine";
2
2
  import { InternalProvider } from "./internal-provider.cjs";
3
3
  /**
4
- * Construct an Ethereum provider from the given JSON-RPC engine.
4
+ * Construct an Ethereum provider from a JSON-RPC engine.
5
5
  *
6
6
  * @param engine - The JSON-RPC engine to construct a provider from.
7
7
  * @returns An Ethereum provider.
8
+ * @deprecated Just use {@link InternalProvider} directly instead.
8
9
  */
9
10
  export declare function providerFromEngine(engine: JsonRpcEngine): InternalProvider;
10
11
  //# sourceMappingURL=provider-from-engine.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"provider-from-engine.d.cts","sourceRoot":"","sources":["../src/provider-from-engine.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,kCAAkC;AAE/D,OAAO,EAAE,gBAAgB,EAAE,gCAA4B;AAEvD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,aAAa,GAAG,gBAAgB,CAE1E"}
1
+ {"version":3,"file":"provider-from-engine.d.cts","sourceRoot":"","sources":["../src/provider-from-engine.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,kCAAkC;AAE/D,OAAO,EAAE,gBAAgB,EAAE,gCAA4B;AAEvD;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,aAAa,GAAG,gBAAgB,CAE1E"}
@@ -1,10 +1,11 @@
1
1
  import type { JsonRpcEngine } from "@metamask/json-rpc-engine";
2
2
  import { InternalProvider } from "./internal-provider.mjs";
3
3
  /**
4
- * Construct an Ethereum provider from the given JSON-RPC engine.
4
+ * Construct an Ethereum provider from a JSON-RPC engine.
5
5
  *
6
6
  * @param engine - The JSON-RPC engine to construct a provider from.
7
7
  * @returns An Ethereum provider.
8
+ * @deprecated Just use {@link InternalProvider} directly instead.
8
9
  */
9
10
  export declare function providerFromEngine(engine: JsonRpcEngine): InternalProvider;
10
11
  //# sourceMappingURL=provider-from-engine.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"provider-from-engine.d.mts","sourceRoot":"","sources":["../src/provider-from-engine.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,kCAAkC;AAE/D,OAAO,EAAE,gBAAgB,EAAE,gCAA4B;AAEvD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,aAAa,GAAG,gBAAgB,CAE1E"}
1
+ {"version":3,"file":"provider-from-engine.d.mts","sourceRoot":"","sources":["../src/provider-from-engine.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,kCAAkC;AAE/D,OAAO,EAAE,gBAAgB,EAAE,gCAA4B;AAEvD;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,aAAa,GAAG,gBAAgB,CAE1E"}
@@ -1,9 +1,10 @@
1
1
  import { InternalProvider } from "./internal-provider.mjs";
2
2
  /**
3
- * Construct an Ethereum provider from the given JSON-RPC engine.
3
+ * Construct an Ethereum provider from a JSON-RPC engine.
4
4
  *
5
5
  * @param engine - The JSON-RPC engine to construct a provider from.
6
6
  * @returns An Ethereum provider.
7
+ * @deprecated Just use {@link InternalProvider} directly instead.
7
8
  */
8
9
  export function providerFromEngine(engine) {
9
10
  return new InternalProvider({ engine });
@@ -1 +1 @@
1
- {"version":3,"file":"provider-from-engine.mjs","sourceRoot":"","sources":["../src/provider-from-engine.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,gCAA4B;AAEvD;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAqB;IACtD,OAAO,IAAI,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;AAC1C,CAAC","sourcesContent":["import type { JsonRpcEngine } from '@metamask/json-rpc-engine';\n\nimport { InternalProvider } from './internal-provider';\n\n/**\n * Construct an Ethereum provider from the given JSON-RPC engine.\n *\n * @param engine - The JSON-RPC engine to construct a provider from.\n * @returns An Ethereum provider.\n */\nexport function providerFromEngine(engine: JsonRpcEngine): InternalProvider {\n return new InternalProvider({ engine });\n}\n"]}
1
+ {"version":3,"file":"provider-from-engine.mjs","sourceRoot":"","sources":["../src/provider-from-engine.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,gCAA4B;AAEvD;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAqB;IACtD,OAAO,IAAI,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;AAC1C,CAAC","sourcesContent":["import type { JsonRpcEngine } from '@metamask/json-rpc-engine';\n\nimport { InternalProvider } from './internal-provider';\n\n/**\n * Construct an Ethereum provider from a JSON-RPC engine.\n *\n * @param engine - The JSON-RPC engine to construct a provider from.\n * @returns An Ethereum provider.\n * @deprecated Just use {@link InternalProvider} directly instead.\n */\nexport function providerFromEngine(engine: JsonRpcEngine): InternalProvider {\n return new InternalProvider({ engine });\n}\n"]}
@@ -1,19 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.providerFromMiddleware = void 0;
3
+ exports.providerFromMiddlewareV2 = exports.providerFromMiddleware = void 0;
4
4
  const json_rpc_engine_1 = require("@metamask/json-rpc-engine");
5
- const provider_from_engine_1 = require("./provider-from-engine.cjs");
5
+ const v2_1 = require("@metamask/json-rpc-engine/v2");
6
+ const internal_provider_1 = require("./internal-provider.cjs");
6
7
  /**
7
8
  * Construct an Ethereum provider from the given middleware.
8
9
  *
9
10
  * @param middleware - The middleware to construct a provider from.
10
11
  * @returns An Ethereum provider.
12
+ * @deprecated Use {@link providerFromMiddlewareV2} instead.
11
13
  */
12
14
  function providerFromMiddleware(middleware) {
13
- const engine = new json_rpc_engine_1.JsonRpcEngine();
14
- engine.push(middleware);
15
- const provider = (0, provider_from_engine_1.providerFromEngine)(engine);
16
- return provider;
15
+ return providerFromMiddlewareV2((0, json_rpc_engine_1.asV2Middleware)(middleware));
17
16
  }
18
17
  exports.providerFromMiddleware = providerFromMiddleware;
18
+ /**
19
+ * Construct an Ethereum provider from the given middleware.
20
+ *
21
+ * @param middleware - The middleware to construct a provider from.
22
+ * @returns An Ethereum provider.
23
+ */
24
+ function providerFromMiddlewareV2(middleware) {
25
+ return new internal_provider_1.InternalProvider({
26
+ server: new v2_1.JsonRpcServer({ middleware: [middleware] }),
27
+ });
28
+ }
29
+ exports.providerFromMiddlewareV2 = providerFromMiddlewareV2;
19
30
  //# sourceMappingURL=provider-from-middleware.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"provider-from-middleware.cjs","sourceRoot":"","sources":["../src/provider-from-middleware.ts"],"names":[],"mappings":";;;AAAA,+DAA0D;AAK1D,qEAA4D;AAE5D;;;;;GAKG;AACH,SAAgB,sBAAsB,CAGpC,UAA6C;IAC7C,MAAM,MAAM,GAAkB,IAAI,+BAAa,EAAE,CAAC;IAClD,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACxB,MAAM,QAAQ,GAAqB,IAAA,yCAAkB,EAAC,MAAM,CAAC,CAAC;IAC9D,OAAO,QAAQ,CAAC;AAClB,CAAC;AARD,wDAQC","sourcesContent":["import { JsonRpcEngine } from '@metamask/json-rpc-engine';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport type { Json, JsonRpcParams } from '@metamask/utils';\n\nimport type { InternalProvider } from './internal-provider';\nimport { providerFromEngine } from './provider-from-engine';\n\n/**\n * Construct an Ethereum provider from the given middleware.\n *\n * @param middleware - The middleware to construct a provider from.\n * @returns An Ethereum provider.\n */\nexport function providerFromMiddleware<\n Params extends JsonRpcParams,\n Result extends Json,\n>(middleware: JsonRpcMiddleware<Params, Result>): InternalProvider {\n const engine: JsonRpcEngine = new JsonRpcEngine();\n engine.push(middleware);\n const provider: InternalProvider = providerFromEngine(engine);\n return provider;\n}\n"]}
1
+ {"version":3,"file":"provider-from-middleware.cjs","sourceRoot":"","sources":["../src/provider-from-middleware.ts"],"names":[],"mappings":";;;AAAA,+DAA2D;AAE3D,qDAA6D;AAI7D,+DAAuD;AAEvD;;;;;;GAMG;AACH,SAAgB,sBAAsB,CAGpC,UAAmD;IACnD,OAAO,wBAAwB,CAC7B,IAAA,gCAAc,EAAC,UAAU,CAA+B,CACzD,CAAC;AACJ,CAAC;AAPD,wDAOC;AAED;;;;;GAKG;AACH,SAAgB,wBAAwB,CAEtC,UAAsB;IACtB,OAAO,IAAI,oCAAgB,CAAC;QAC1B,MAAM,EAAE,IAAI,kBAAa,CAAC,EAAE,UAAU,EAAE,CAAC,UAAU,CAAC,EAAE,CAAC;KACxD,CAAC,CAAC;AACL,CAAC;AAND,4DAMC","sourcesContent":["import { asV2Middleware } from '@metamask/json-rpc-engine';\nimport type { JsonRpcMiddleware as LegacyJsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport { JsonRpcServer } from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcParams } from '@metamask/utils';\n\nimport type { InternalProviderMiddleware } from './internal-provider';\nimport { InternalProvider } from './internal-provider';\n\n/**\n * Construct an Ethereum provider from the given middleware.\n *\n * @param middleware - The middleware to construct a provider from.\n * @returns An Ethereum provider.\n * @deprecated Use {@link providerFromMiddlewareV2} instead.\n */\nexport function providerFromMiddleware<\n Params extends JsonRpcParams,\n Result extends Json,\n>(middleware: LegacyJsonRpcMiddleware<Params, Result>): InternalProvider {\n return providerFromMiddlewareV2(\n asV2Middleware(middleware) as InternalProviderMiddleware,\n );\n}\n\n/**\n * Construct an Ethereum provider from the given middleware.\n *\n * @param middleware - The middleware to construct a provider from.\n * @returns An Ethereum provider.\n */\nexport function providerFromMiddlewareV2<\n Middleware extends InternalProviderMiddleware,\n>(middleware: Middleware): InternalProvider {\n return new InternalProvider({\n server: new JsonRpcServer({ middleware: [middleware] }),\n });\n}\n"]}
@@ -1,11 +1,20 @@
1
- import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
1
+ import type { JsonRpcMiddleware as LegacyJsonRpcMiddleware } from "@metamask/json-rpc-engine";
2
2
  import type { Json, JsonRpcParams } from "@metamask/utils";
3
- import type { InternalProvider } from "./internal-provider.cjs";
3
+ import type { InternalProviderMiddleware } from "./internal-provider.cjs";
4
+ import { InternalProvider } from "./internal-provider.cjs";
4
5
  /**
5
6
  * Construct an Ethereum provider from the given middleware.
6
7
  *
7
8
  * @param middleware - The middleware to construct a provider from.
8
9
  * @returns An Ethereum provider.
10
+ * @deprecated Use {@link providerFromMiddlewareV2} instead.
9
11
  */
10
- export declare function providerFromMiddleware<Params extends JsonRpcParams, Result extends Json>(middleware: JsonRpcMiddleware<Params, Result>): InternalProvider;
12
+ export declare function providerFromMiddleware<Params extends JsonRpcParams, Result extends Json>(middleware: LegacyJsonRpcMiddleware<Params, Result>): InternalProvider;
13
+ /**
14
+ * Construct an Ethereum provider from the given middleware.
15
+ *
16
+ * @param middleware - The middleware to construct a provider from.
17
+ * @returns An Ethereum provider.
18
+ */
19
+ export declare function providerFromMiddlewareV2<Middleware extends InternalProviderMiddleware>(middleware: Middleware): InternalProvider;
11
20
  //# sourceMappingURL=provider-from-middleware.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"provider-from-middleware.d.cts","sourceRoot":"","sources":["../src/provider-from-middleware.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,kCAAkC;AACnE,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,wBAAwB;AAE3D,OAAO,KAAK,EAAE,gBAAgB,EAAE,gCAA4B;AAG5D;;;;;GAKG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,SAAS,aAAa,EAC5B,MAAM,SAAS,IAAI,EACnB,UAAU,EAAE,iBAAiB,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,gBAAgB,CAKjE"}
1
+ {"version":3,"file":"provider-from-middleware.d.cts","sourceRoot":"","sources":["../src/provider-from-middleware.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,IAAI,uBAAuB,EAAE,kCAAkC;AAE9F,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,wBAAwB;AAE3D,OAAO,KAAK,EAAE,0BAA0B,EAAE,gCAA4B;AACtE,OAAO,EAAE,gBAAgB,EAAE,gCAA4B;AAEvD;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,SAAS,aAAa,EAC5B,MAAM,SAAS,IAAI,EACnB,UAAU,EAAE,uBAAuB,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,gBAAgB,CAIvE;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,UAAU,SAAS,0BAA0B,EAC7C,UAAU,EAAE,UAAU,GAAG,gBAAgB,CAI1C"}
@@ -1,11 +1,20 @@
1
- import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
1
+ import type { JsonRpcMiddleware as LegacyJsonRpcMiddleware } from "@metamask/json-rpc-engine";
2
2
  import type { Json, JsonRpcParams } from "@metamask/utils";
3
- import type { InternalProvider } from "./internal-provider.mjs";
3
+ import type { InternalProviderMiddleware } from "./internal-provider.mjs";
4
+ import { InternalProvider } from "./internal-provider.mjs";
4
5
  /**
5
6
  * Construct an Ethereum provider from the given middleware.
6
7
  *
7
8
  * @param middleware - The middleware to construct a provider from.
8
9
  * @returns An Ethereum provider.
10
+ * @deprecated Use {@link providerFromMiddlewareV2} instead.
9
11
  */
10
- export declare function providerFromMiddleware<Params extends JsonRpcParams, Result extends Json>(middleware: JsonRpcMiddleware<Params, Result>): InternalProvider;
12
+ export declare function providerFromMiddleware<Params extends JsonRpcParams, Result extends Json>(middleware: LegacyJsonRpcMiddleware<Params, Result>): InternalProvider;
13
+ /**
14
+ * Construct an Ethereum provider from the given middleware.
15
+ *
16
+ * @param middleware - The middleware to construct a provider from.
17
+ * @returns An Ethereum provider.
18
+ */
19
+ export declare function providerFromMiddlewareV2<Middleware extends InternalProviderMiddleware>(middleware: Middleware): InternalProvider;
11
20
  //# sourceMappingURL=provider-from-middleware.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"provider-from-middleware.d.mts","sourceRoot":"","sources":["../src/provider-from-middleware.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,kCAAkC;AACnE,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,wBAAwB;AAE3D,OAAO,KAAK,EAAE,gBAAgB,EAAE,gCAA4B;AAG5D;;;;;GAKG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,SAAS,aAAa,EAC5B,MAAM,SAAS,IAAI,EACnB,UAAU,EAAE,iBAAiB,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,gBAAgB,CAKjE"}
1
+ {"version":3,"file":"provider-from-middleware.d.mts","sourceRoot":"","sources":["../src/provider-from-middleware.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,IAAI,uBAAuB,EAAE,kCAAkC;AAE9F,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,wBAAwB;AAE3D,OAAO,KAAK,EAAE,0BAA0B,EAAE,gCAA4B;AACtE,OAAO,EAAE,gBAAgB,EAAE,gCAA4B;AAEvD;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,SAAS,aAAa,EAC5B,MAAM,SAAS,IAAI,EACnB,UAAU,EAAE,uBAAuB,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,gBAAgB,CAIvE;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,UAAU,SAAS,0BAA0B,EAC7C,UAAU,EAAE,UAAU,GAAG,gBAAgB,CAI1C"}
@@ -1,15 +1,25 @@
1
- import { JsonRpcEngine } from "@metamask/json-rpc-engine";
2
- import { providerFromEngine } from "./provider-from-engine.mjs";
1
+ import { asV2Middleware } from "@metamask/json-rpc-engine";
2
+ import { JsonRpcServer } from "@metamask/json-rpc-engine/v2";
3
+ import { InternalProvider } from "./internal-provider.mjs";
3
4
  /**
4
5
  * Construct an Ethereum provider from the given middleware.
5
6
  *
6
7
  * @param middleware - The middleware to construct a provider from.
7
8
  * @returns An Ethereum provider.
9
+ * @deprecated Use {@link providerFromMiddlewareV2} instead.
8
10
  */
9
11
  export function providerFromMiddleware(middleware) {
10
- const engine = new JsonRpcEngine();
11
- engine.push(middleware);
12
- const provider = providerFromEngine(engine);
13
- return provider;
12
+ return providerFromMiddlewareV2(asV2Middleware(middleware));
13
+ }
14
+ /**
15
+ * Construct an Ethereum provider from the given middleware.
16
+ *
17
+ * @param middleware - The middleware to construct a provider from.
18
+ * @returns An Ethereum provider.
19
+ */
20
+ export function providerFromMiddlewareV2(middleware) {
21
+ return new InternalProvider({
22
+ server: new JsonRpcServer({ middleware: [middleware] }),
23
+ });
14
24
  }
15
25
  //# sourceMappingURL=provider-from-middleware.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"provider-from-middleware.mjs","sourceRoot":"","sources":["../src/provider-from-middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,kCAAkC;AAK1D,OAAO,EAAE,kBAAkB,EAAE,mCAA+B;AAE5D;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB,CAGpC,UAA6C;IAC7C,MAAM,MAAM,GAAkB,IAAI,aAAa,EAAE,CAAC;IAClD,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACxB,MAAM,QAAQ,GAAqB,kBAAkB,CAAC,MAAM,CAAC,CAAC;IAC9D,OAAO,QAAQ,CAAC;AAClB,CAAC","sourcesContent":["import { JsonRpcEngine } from '@metamask/json-rpc-engine';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport type { Json, JsonRpcParams } from '@metamask/utils';\n\nimport type { InternalProvider } from './internal-provider';\nimport { providerFromEngine } from './provider-from-engine';\n\n/**\n * Construct an Ethereum provider from the given middleware.\n *\n * @param middleware - The middleware to construct a provider from.\n * @returns An Ethereum provider.\n */\nexport function providerFromMiddleware<\n Params extends JsonRpcParams,\n Result extends Json,\n>(middleware: JsonRpcMiddleware<Params, Result>): InternalProvider {\n const engine: JsonRpcEngine = new JsonRpcEngine();\n engine.push(middleware);\n const provider: InternalProvider = providerFromEngine(engine);\n return provider;\n}\n"]}
1
+ {"version":3,"file":"provider-from-middleware.mjs","sourceRoot":"","sources":["../src/provider-from-middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAE3D,OAAO,EAAE,aAAa,EAAE,qCAAqC;AAI7D,OAAO,EAAE,gBAAgB,EAAE,gCAA4B;AAEvD;;;;;;GAMG;AACH,MAAM,UAAU,sBAAsB,CAGpC,UAAmD;IACnD,OAAO,wBAAwB,CAC7B,cAAc,CAAC,UAAU,CAA+B,CACzD,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,wBAAwB,CAEtC,UAAsB;IACtB,OAAO,IAAI,gBAAgB,CAAC;QAC1B,MAAM,EAAE,IAAI,aAAa,CAAC,EAAE,UAAU,EAAE,CAAC,UAAU,CAAC,EAAE,CAAC;KACxD,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { asV2Middleware } from '@metamask/json-rpc-engine';\nimport type { JsonRpcMiddleware as LegacyJsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport { JsonRpcServer } from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcParams } from '@metamask/utils';\n\nimport type { InternalProviderMiddleware } from './internal-provider';\nimport { InternalProvider } from './internal-provider';\n\n/**\n * Construct an Ethereum provider from the given middleware.\n *\n * @param middleware - The middleware to construct a provider from.\n * @returns An Ethereum provider.\n * @deprecated Use {@link providerFromMiddlewareV2} instead.\n */\nexport function providerFromMiddleware<\n Params extends JsonRpcParams,\n Result extends Json,\n>(middleware: LegacyJsonRpcMiddleware<Params, Result>): InternalProvider {\n return providerFromMiddlewareV2(\n asV2Middleware(middleware) as InternalProviderMiddleware,\n );\n}\n\n/**\n * Construct an Ethereum provider from the given middleware.\n *\n * @param middleware - The middleware to construct a provider from.\n * @returns An Ethereum provider.\n */\nexport function providerFromMiddlewareV2<\n Middleware extends InternalProviderMiddleware,\n>(middleware: Middleware): InternalProvider {\n return new InternalProvider({\n server: new JsonRpcServer({ middleware: [middleware] }),\n });\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/eth-json-rpc-provider",
3
- "version": "5.0.1-preview-5e4e26a6",
3
+ "version": "5.0.1-preview-c0feb25",
4
4
  "description": "Create an Ethereum provider using a JSON-RPC engine or middleware",
5
5
  "keywords": [
6
6
  "MetaMask",