@metamask-previews/eth-json-rpc-provider 6.0.1-preview-4f4c98e → 6.0.1-preview-eb263b0ee
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 +0 -1
- package/dist/index.cjs +23 -0
- package/dist/index.cjs.map +1 -0
- package/dist/{index.d.ts → index.d.cts} +3 -3
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +9 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +5 -0
- package/dist/index.mjs.map +1 -0
- package/dist/internal-provider.cjs +137 -0
- package/dist/internal-provider.cjs.map +1 -0
- package/dist/{internal-provider.d.ts → internal-provider.d.cts} +5 -5
- package/dist/internal-provider.d.cts.map +1 -0
- package/dist/internal-provider.d.mts +74 -0
- package/dist/internal-provider.d.mts.map +1 -0
- package/dist/{internal-provider.js → internal-provider.mjs} +9 -9
- package/dist/internal-provider.mjs.map +1 -0
- package/dist/provider-from-middleware.cjs +40 -0
- package/dist/provider-from-middleware.cjs.map +1 -0
- package/dist/{provider-from-middleware.d.ts → provider-from-middleware.d.cts} +5 -5
- package/dist/provider-from-middleware.d.cts.map +1 -0
- package/dist/provider-from-middleware.d.mts +20 -0
- package/dist/provider-from-middleware.d.mts.map +1 -0
- package/dist/{provider-from-middleware.js → provider-from-middleware.mjs} +4 -4
- package/dist/provider-from-middleware.mjs.map +1 -0
- package/package.json +16 -12
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -5
- package/dist/index.js.map +0 -1
- package/dist/internal-provider.d.ts.map +0 -1
- package/dist/internal-provider.js.map +0 -1
- package/dist/provider-from-middleware.d.ts.map +0 -1
- package/dist/provider-from-middleware.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,7 +9,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
9
9
|
|
|
10
10
|
### Changed
|
|
11
11
|
|
|
12
|
-
- **BREAKING:** Bump minimum Node.js version to 22 ([#9168](https://github.com/MetaMask/core/pull/9168))
|
|
13
12
|
- Bump `@metamask/utils` from `^11.9.0` to `^11.11.0` ([#9074](https://github.com/MetaMask/core/pull/9074))
|
|
14
13
|
- Bump `@metamask/json-rpc-engine` from `^10.2.4` to `^10.5.0` ([#8661](https://github.com/MetaMask/core/pull/8661), [#8746](https://github.com/MetaMask/core/pull/8746), [#8753](https://github.com/MetaMask/core/pull/8753))
|
|
15
14
|
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.SafeEventEmitterProvider = exports.InternalProvider = void 0;
|
|
18
|
+
const internal_provider_js_1 = require("./internal-provider.cjs");
|
|
19
|
+
Object.defineProperty(exports, "InternalProvider", { enumerable: true, get: function () { return internal_provider_js_1.InternalProvider; } });
|
|
20
|
+
__exportStar(require("./provider-from-middleware.cjs"), exports);
|
|
21
|
+
const SafeEventEmitterProvider = internal_provider_js_1.InternalProvider;
|
|
22
|
+
exports.SafeEventEmitterProvider = SafeEventEmitterProvider;
|
|
23
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,kEAA0D;AAUjD,iGAVA,uCAAgB,OAUA;AARzB,iEAA8C;AAM9C,MAAM,wBAAwB,GAAG,uCAAgB,CAAC;AAEvB,4DAAwB","sourcesContent":["import { InternalProvider } from './internal-provider.js';\n\nexport * from './provider-from-middleware.js';\n\n/**\n * @deprecated Use {@link InternalProvider} instead.\n */\ntype SafeEventEmitterProvider = InternalProvider;\nconst SafeEventEmitterProvider = InternalProvider;\n\nexport { InternalProvider, SafeEventEmitterProvider };\n"]}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { InternalProvider } from
|
|
2
|
-
export * from
|
|
1
|
+
import { InternalProvider } from "./internal-provider.cjs";
|
|
2
|
+
export * from "./provider-from-middleware.cjs";
|
|
3
3
|
/**
|
|
4
4
|
* @deprecated Use {@link InternalProvider} instead.
|
|
5
5
|
*/
|
|
6
6
|
type SafeEventEmitterProvider = InternalProvider;
|
|
7
7
|
declare const SafeEventEmitterProvider: typeof InternalProvider;
|
|
8
8
|
export { InternalProvider, SafeEventEmitterProvider };
|
|
9
|
-
//# sourceMappingURL=index.d.
|
|
9
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,gCAA+B;AAE1D,+CAA8C;AAE9C;;GAEG;AACH,KAAK,wBAAwB,GAAG,gBAAgB,CAAC;AACjD,QAAA,MAAM,wBAAwB,yBAAmB,CAAC;AAElD,OAAO,EAAE,gBAAgB,EAAE,wBAAwB,EAAE,CAAC"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { InternalProvider } from "./internal-provider.mjs";
|
|
2
|
+
export * from "./provider-from-middleware.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated Use {@link InternalProvider} instead.
|
|
5
|
+
*/
|
|
6
|
+
type SafeEventEmitterProvider = InternalProvider;
|
|
7
|
+
declare const SafeEventEmitterProvider: typeof InternalProvider;
|
|
8
|
+
export { InternalProvider, SafeEventEmitterProvider };
|
|
9
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,gCAA+B;AAE1D,+CAA8C;AAE9C;;GAEG;AACH,KAAK,wBAAwB,GAAG,gBAAgB,CAAC;AACjD,QAAA,MAAM,wBAAwB,yBAAmB,CAAC;AAElD,OAAO,EAAE,gBAAgB,EAAE,wBAAwB,EAAE,CAAC"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,gCAA+B;AAE1D,+CAA8C;AAM9C,MAAM,wBAAwB,GAAG,gBAAgB,CAAC;AAElD,OAAO,EAAE,gBAAgB,EAAE,wBAAwB,EAAE,CAAC","sourcesContent":["import { InternalProvider } from './internal-provider.js';\n\nexport * from './provider-from-middleware.js';\n\n/**\n * @deprecated Use {@link InternalProvider} instead.\n */\ntype SafeEventEmitterProvider = InternalProvider;\nconst SafeEventEmitterProvider = InternalProvider;\n\nexport { InternalProvider, SafeEventEmitterProvider };\n"]}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
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
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _InternalProvider_engine, _InternalProvider_handle, _InternalProvider_handleWithCallback;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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");
|
|
18
|
+
const nanoid_1 = require("nanoid");
|
|
19
|
+
/**
|
|
20
|
+
* An Ethereum provider.
|
|
21
|
+
*
|
|
22
|
+
* This provider loosely follows conventions that pre-date EIP-1193.
|
|
23
|
+
* It is not compliant with any Ethereum provider standard.
|
|
24
|
+
*/
|
|
25
|
+
class InternalProvider {
|
|
26
|
+
/**
|
|
27
|
+
* Construct a InternalProvider from a JSON-RPC server or legacy engine.
|
|
28
|
+
*
|
|
29
|
+
* @param options - Options.
|
|
30
|
+
* @param options.engine - The JSON-RPC engine used to process requests.
|
|
31
|
+
*/
|
|
32
|
+
constructor({ engine }) {
|
|
33
|
+
_InternalProvider_engine.set(this, void 0);
|
|
34
|
+
/**
|
|
35
|
+
* Send a provider request asynchronously.
|
|
36
|
+
*
|
|
37
|
+
* This method serves the same purpose as `request`. It only exists for
|
|
38
|
+
* legacy reasons.
|
|
39
|
+
*
|
|
40
|
+
* @param eip1193Request - The request to send.
|
|
41
|
+
* @param callback - A function that is called upon the success or failure of the request.
|
|
42
|
+
* @deprecated Use {@link request} instead. This method is retained solely for backwards
|
|
43
|
+
* compatibility with certain libraries.
|
|
44
|
+
*/
|
|
45
|
+
this.sendAsync = (eip1193Request,
|
|
46
|
+
// Non-polluting `any` that acts like a constraint.
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
48
|
+
callback) => {
|
|
49
|
+
const jsonRpcRequest = convertEip1193RequestToJsonRpcRequest(eip1193Request);
|
|
50
|
+
__classPrivateFieldGet(this, _InternalProvider_handleWithCallback, "f").call(this, jsonRpcRequest, callback);
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Send a provider request asynchronously.
|
|
54
|
+
*
|
|
55
|
+
* This method serves the same purpose as `request`. It only exists for
|
|
56
|
+
* legacy reasons.
|
|
57
|
+
*
|
|
58
|
+
* @param eip1193Request - The request to send.
|
|
59
|
+
* @param callback - A function that is called upon the success or failure of the request.
|
|
60
|
+
* @deprecated Use {@link request} instead. This method is retained solely for backwards
|
|
61
|
+
* compatibility with certain libraries.
|
|
62
|
+
*/
|
|
63
|
+
this.send = (eip1193Request,
|
|
64
|
+
// TODO: Replace `any` with type
|
|
65
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
66
|
+
callback) => {
|
|
67
|
+
if (typeof callback !== 'function') {
|
|
68
|
+
throw new Error('Must provide callback to "send" method.');
|
|
69
|
+
}
|
|
70
|
+
const jsonRpcRequest = convertEip1193RequestToJsonRpcRequest(eip1193Request);
|
|
71
|
+
__classPrivateFieldGet(this, _InternalProvider_handleWithCallback, "f").call(this, jsonRpcRequest, callback);
|
|
72
|
+
};
|
|
73
|
+
_InternalProvider_handle.set(this, async (jsonRpcRequest, options) => {
|
|
74
|
+
const { id, jsonrpc } = jsonRpcRequest;
|
|
75
|
+
// The `result` typecast is unsafe, but we need it to preserve the provider's
|
|
76
|
+
// public interface, which allows you to unsafely typecast results.
|
|
77
|
+
const result = (await __classPrivateFieldGet(this, _InternalProvider_engine, "f").handle(jsonRpcRequest, options));
|
|
78
|
+
return {
|
|
79
|
+
id,
|
|
80
|
+
jsonrpc,
|
|
81
|
+
result,
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
_InternalProvider_handleWithCallback.set(this, (jsonRpcRequest, callback) => {
|
|
85
|
+
/* eslint-disable promise/no-callback-in-promise */
|
|
86
|
+
__classPrivateFieldGet(this, _InternalProvider_handle, "f").call(this, jsonRpcRequest)
|
|
87
|
+
// A resolution will always be a successful response
|
|
88
|
+
.then((response) => callback(null, response))
|
|
89
|
+
.catch((error) => {
|
|
90
|
+
callback(error);
|
|
91
|
+
});
|
|
92
|
+
/* eslint-enable promise/no-callback-in-promise */
|
|
93
|
+
});
|
|
94
|
+
__classPrivateFieldSet(this, _InternalProvider_engine, 'push' in engine
|
|
95
|
+
? v2_1.JsonRpcEngineV2.create({
|
|
96
|
+
middleware: [(0, json_rpc_engine_1.asV2Middleware)(engine)],
|
|
97
|
+
})
|
|
98
|
+
: engine, "f");
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Send a provider request asynchronously.
|
|
102
|
+
*
|
|
103
|
+
* @param eip1193Request - The request to send.
|
|
104
|
+
* @param options - The options for the request operation.
|
|
105
|
+
* @param options.context - The context to include with the request.
|
|
106
|
+
* @returns The JSON-RPC response.
|
|
107
|
+
*/
|
|
108
|
+
async request(eip1193Request, options) {
|
|
109
|
+
const jsonRpcRequest = convertEip1193RequestToJsonRpcRequest(eip1193Request);
|
|
110
|
+
return (await __classPrivateFieldGet(this, _InternalProvider_handle, "f").call(this, jsonRpcRequest, options)).result;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
exports.InternalProvider = InternalProvider;
|
|
114
|
+
_InternalProvider_engine = 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
|
+
function convertEip1193RequestToJsonRpcRequest(eip1193Request) {
|
|
122
|
+
const { id = (0, nanoid_1.nanoid)(), 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
|
+
exports.convertEip1193RequestToJsonRpcRequest = convertEip1193RequestToJsonRpcRequest;
|
|
137
|
+
//# sourceMappingURL=internal-provider.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal-provider.cjs","sourceRoot":"","sources":["../src/internal-provider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+DAA2D;AAE3D,qDAA+D;AAc/D,mCAAgC;AAmBhC;;;;;GAKG;AACH,MAAa,gBAAgB;IAK3B;;;;;OAKG;IACH,YAAY,EAAE,MAAM,EAAoC;QAR/C,2CAAkD;QAkC3D;;;;;;;;;;WAUG;QACH,cAAS,GAAG,CACV,cAAsC;QACtC,mDAAmD;QACnD,8DAA8D;QAC9D,QAAqD,EAC/C,EAAE;YACR,MAAM,cAAc,GAClB,qCAAqC,CAAC,cAAc,CAAC,CAAC;YACxD,uBAAA,IAAI,4CAAoB,MAAxB,IAAI,EAAqB,cAAc,EAAE,QAAQ,CAAC,CAAC;QACrD,CAAC,CAAC;QAEF;;;;;;;;;;WAUG;QACH,SAAI,GAAG,CACL,cAAsC;QACtC,gCAAgC;QAChC,8DAA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AOG;IACH,KAAK,CAAC,OAAO,CACX,cAAsC,EACtC,OAAgC;QAEhC,MAAM,cAAc,GAClB,qCAAqC,CAAC,cAAc,CAAC,CAAC;QACxD,OAAO,CAAC,MAAM,uBAAA,IAAI,gCAAQ,MAAZ,IAAI,EAAiB,cAAc,EAAE,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC;IACtE,CAAC;CAiFF;AApHD,4CAoHC;;AAED;;;;;GAKG;AACH,SAAgB,qCAAqC,CACnD,cAA8B;IAE9B,MAAM,EAAE,EAAE,GAAG,IAAA,eAAM,GAAE,EAAE,OAAO,GAAG,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,cAAc,CAAC;IAE1E,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;AAjBD,sFAiBC","sourcesContent":["import { asV2Middleware } from '@metamask/json-rpc-engine';\nimport type { JsonRpcEngine } from '@metamask/json-rpc-engine';\nimport { JsonRpcEngineV2 } from '@metamask/json-rpc-engine/v2';\nimport type {\n HandleOptions,\n ContextConstraint,\n MiddlewareContext,\n} from '@metamask/json-rpc-engine/v2';\nimport type {\n Json,\n JsonRpcId,\n JsonRpcParams,\n JsonRpcSuccess,\n JsonRpcRequest,\n JsonRpcVersion2,\n} from '@metamask/utils';\nimport { nanoid } from 'nanoid';\n\n/**\n * A JSON-RPC request conforming to the EIP-1193 specification.\n */\ntype Eip1193Request<Params extends JsonRpcParams = JsonRpcParams> = {\n id?: JsonRpcId;\n jsonrpc?: JsonRpcVersion2;\n method: string;\n params?: Params;\n};\n\ntype Options<\n Request extends JsonRpcRequest = JsonRpcRequest,\n Context extends ContextConstraint = MiddlewareContext,\n> = {\n engine: JsonRpcEngine | JsonRpcEngineV2<Request, Context>;\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 Context extends ContextConstraint = MiddlewareContext,\n> {\n readonly #engine: JsonRpcEngineV2<JsonRpcRequest, Context>;\n\n /**\n * Construct a InternalProvider from a JSON-RPC server or legacy engine.\n *\n * @param options - Options.\n * @param options.engine - The JSON-RPC engine used to process requests.\n */\n constructor({ engine }: Options<JsonRpcRequest, Context>) {\n this.#engine =\n 'push' in engine\n ? JsonRpcEngineV2.create({\n middleware: [asV2Middleware<JsonRpcParams, JsonRpcRequest>(engine)],\n })\n : engine;\n }\n\n /**\n * Send a provider request asynchronously.\n *\n * @param eip1193Request - The request to send.\n * @param options - The options for the request operation.\n * @param options.context - The context to include with the request.\n * @returns The JSON-RPC response.\n */\n async request<Params extends JsonRpcParams, Result extends Json>(\n eip1193Request: Eip1193Request<Params>,\n options?: HandleOptions<Context>,\n ): Promise<Result> {\n const jsonRpcRequest =\n convertEip1193RequestToJsonRpcRequest(eip1193Request);\n return (await this.#handle<Result>(jsonRpcRequest, options)).result;\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. This method is retained solely for backwards\n * compatibility with certain libraries.\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 ): void => {\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. This method is retained solely for backwards\n * compatibility with certain libraries.\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 ): void => {\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 options?: HandleOptions<Context>,\n ): Promise<JsonRpcSuccess<Result>> => {\n const { id, jsonrpc } = jsonRpcRequest;\n // The `result` typecast is unsafe, but we need it to preserve the provider's\n // public interface, which allows you to unsafely typecast results.\n const result = (await this.#engine.handle(\n jsonRpcRequest,\n options,\n )) as unknown as Result;\n\n return {\n id,\n jsonrpc,\n result,\n };\n };\n\n readonly #handleWithCallback = (\n jsonRpcRequest: JsonRpcRequest,\n callback: (error: unknown, providerRes?: unknown) => void,\n ): void => {\n /* eslint-disable promise/no-callback-in-promise */\n this.#handle(jsonRpcRequest)\n // A resolution will always be a successful response\n .then((response) => callback(null, response))\n .catch((error) => {\n callback(error);\n });\n /* eslint-enable 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 eip1193Request: Eip1193Request,\n): JsonRpcRequest {\n const { id = nanoid(), jsonrpc = '2.0', method, params } = eip1193Request;\n\n return params\n ? {\n id,\n jsonrpc,\n method,\n params,\n }\n : {\n id,\n jsonrpc,\n method,\n };\n}\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { JsonRpcEngine } from
|
|
2
|
-
import { JsonRpcEngineV2 } from
|
|
3
|
-
import type { HandleOptions, ContextConstraint, MiddlewareContext } from
|
|
4
|
-
import type { Json, JsonRpcId, JsonRpcParams, JsonRpcRequest, JsonRpcVersion2 } from
|
|
1
|
+
import type { JsonRpcEngine } from "@metamask/json-rpc-engine";
|
|
2
|
+
import { JsonRpcEngineV2 } from "@metamask/json-rpc-engine/v2";
|
|
3
|
+
import type { HandleOptions, ContextConstraint, MiddlewareContext } from "@metamask/json-rpc-engine/v2";
|
|
4
|
+
import type { Json, JsonRpcId, JsonRpcParams, JsonRpcRequest, JsonRpcVersion2 } from "@metamask/utils";
|
|
5
5
|
/**
|
|
6
6
|
* A JSON-RPC request conforming to the EIP-1193 specification.
|
|
7
7
|
*/
|
|
@@ -71,4 +71,4 @@ export declare class InternalProvider<Context extends ContextConstraint = Middle
|
|
|
71
71
|
*/
|
|
72
72
|
export declare function convertEip1193RequestToJsonRpcRequest(eip1193Request: Eip1193Request): JsonRpcRequest;
|
|
73
73
|
export {};
|
|
74
|
-
//# sourceMappingURL=internal-provider.d.
|
|
74
|
+
//# sourceMappingURL=internal-provider.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal-provider.d.cts","sourceRoot":"","sources":["../src/internal-provider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,kCAAkC;AAC/D,OAAO,EAAE,eAAe,EAAE,qCAAqC;AAC/D,OAAO,KAAK,EACV,aAAa,EACb,iBAAiB,EACjB,iBAAiB,EAClB,qCAAqC;AACtC,OAAO,KAAK,EACV,IAAI,EACJ,SAAS,EACT,aAAa,EAEb,cAAc,EACd,eAAe,EAChB,wBAAwB;AAGzB;;GAEG;AACH,KAAK,cAAc,CAAC,MAAM,SAAS,aAAa,GAAG,aAAa,IAAI;IAClE,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,KAAK,OAAO,CACV,OAAO,SAAS,cAAc,GAAG,cAAc,EAC/C,OAAO,SAAS,iBAAiB,GAAG,iBAAiB,IACnD;IACF,MAAM,EAAE,aAAa,GAAG,eAAe,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;CAC3D,CAAC;AAEF;;;;;GAKG;AACH,qBAAa,gBAAgB,CAC3B,OAAO,SAAS,iBAAiB,GAAG,iBAAiB;;IAIrD;;;;;OAKG;gBACS,EAAE,MAAM,EAAE,EAAE,OAAO,CAAC,cAAc,EAAE,OAAO,CAAC;IASxD;;;;;;;OAOG;IACG,OAAO,CAAC,MAAM,SAAS,aAAa,EAAE,MAAM,SAAS,IAAI,EAC7D,cAAc,EAAE,cAAc,CAAC,MAAM,CAAC,EACtC,OAAO,CAAC,EAAE,aAAa,CAAC,OAAO,CAAC,GAC/B,OAAO,CAAC,MAAM,CAAC;IAMlB;;;;;;;;;;OAUG;IACH,SAAS,2FAIW,OAAO,gBAAgB,GAAG,KAAK,IAAI,KACpD,IAAI,CAIL;IAEF;;;;;;;;;;OAUG;IACH,IAAI,2FAIgB,OAAO,gBAAgB,GAAG,KAAK,IAAI,KACpD,IAAI,CAOL;CAkCH;AAED;;;;;GAKG;AACH,wBAAgB,qCAAqC,CACnD,cAAc,EAAE,cAAc,GAC7B,cAAc,CAehB"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { JsonRpcEngine } from "@metamask/json-rpc-engine";
|
|
2
|
+
import { JsonRpcEngineV2 } from "@metamask/json-rpc-engine/v2";
|
|
3
|
+
import type { HandleOptions, ContextConstraint, MiddlewareContext } from "@metamask/json-rpc-engine/v2";
|
|
4
|
+
import type { Json, JsonRpcId, JsonRpcParams, JsonRpcRequest, JsonRpcVersion2 } from "@metamask/utils";
|
|
5
|
+
/**
|
|
6
|
+
* A JSON-RPC request conforming to the EIP-1193 specification.
|
|
7
|
+
*/
|
|
8
|
+
type Eip1193Request<Params extends JsonRpcParams = JsonRpcParams> = {
|
|
9
|
+
id?: JsonRpcId;
|
|
10
|
+
jsonrpc?: JsonRpcVersion2;
|
|
11
|
+
method: string;
|
|
12
|
+
params?: Params;
|
|
13
|
+
};
|
|
14
|
+
type Options<Request extends JsonRpcRequest = JsonRpcRequest, Context extends ContextConstraint = MiddlewareContext> = {
|
|
15
|
+
engine: JsonRpcEngine | JsonRpcEngineV2<Request, Context>;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* An Ethereum provider.
|
|
19
|
+
*
|
|
20
|
+
* This provider loosely follows conventions that pre-date EIP-1193.
|
|
21
|
+
* It is not compliant with any Ethereum provider standard.
|
|
22
|
+
*/
|
|
23
|
+
export declare class InternalProvider<Context extends ContextConstraint = MiddlewareContext> {
|
|
24
|
+
#private;
|
|
25
|
+
/**
|
|
26
|
+
* Construct a InternalProvider from a JSON-RPC server or legacy engine.
|
|
27
|
+
*
|
|
28
|
+
* @param options - Options.
|
|
29
|
+
* @param options.engine - The JSON-RPC engine used to process requests.
|
|
30
|
+
*/
|
|
31
|
+
constructor({ engine }: Options<JsonRpcRequest, Context>);
|
|
32
|
+
/**
|
|
33
|
+
* Send a provider request asynchronously.
|
|
34
|
+
*
|
|
35
|
+
* @param eip1193Request - The request to send.
|
|
36
|
+
* @param options - The options for the request operation.
|
|
37
|
+
* @param options.context - The context to include with the request.
|
|
38
|
+
* @returns The JSON-RPC response.
|
|
39
|
+
*/
|
|
40
|
+
request<Params extends JsonRpcParams, Result extends Json>(eip1193Request: Eip1193Request<Params>, options?: HandleOptions<Context>): Promise<Result>;
|
|
41
|
+
/**
|
|
42
|
+
* Send a provider request asynchronously.
|
|
43
|
+
*
|
|
44
|
+
* This method serves the same purpose as `request`. It only exists for
|
|
45
|
+
* legacy reasons.
|
|
46
|
+
*
|
|
47
|
+
* @param eip1193Request - The request to send.
|
|
48
|
+
* @param callback - A function that is called upon the success or failure of the request.
|
|
49
|
+
* @deprecated Use {@link request} instead. This method is retained solely for backwards
|
|
50
|
+
* compatibility with certain libraries.
|
|
51
|
+
*/
|
|
52
|
+
sendAsync: <Params extends JsonRpcParams>(eip1193Request: Eip1193Request<Params>, callback: (error: unknown, providerRes?: any) => void) => void;
|
|
53
|
+
/**
|
|
54
|
+
* Send a provider request asynchronously.
|
|
55
|
+
*
|
|
56
|
+
* This method serves the same purpose as `request`. It only exists for
|
|
57
|
+
* legacy reasons.
|
|
58
|
+
*
|
|
59
|
+
* @param eip1193Request - The request to send.
|
|
60
|
+
* @param callback - A function that is called upon the success or failure of the request.
|
|
61
|
+
* @deprecated Use {@link request} instead. This method is retained solely for backwards
|
|
62
|
+
* compatibility with certain libraries.
|
|
63
|
+
*/
|
|
64
|
+
send: <Params extends JsonRpcParams>(eip1193Request: Eip1193Request<Params>, callback: (error: unknown, providerRes?: any) => void) => void;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Convert an EIP-1193 request to a JSON-RPC request.
|
|
68
|
+
*
|
|
69
|
+
* @param eip1193Request - The EIP-1193 request to convert.
|
|
70
|
+
* @returns The JSON-RPC request.
|
|
71
|
+
*/
|
|
72
|
+
export declare function convertEip1193RequestToJsonRpcRequest(eip1193Request: Eip1193Request): JsonRpcRequest;
|
|
73
|
+
export {};
|
|
74
|
+
//# sourceMappingURL=internal-provider.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal-provider.d.mts","sourceRoot":"","sources":["../src/internal-provider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,kCAAkC;AAC/D,OAAO,EAAE,eAAe,EAAE,qCAAqC;AAC/D,OAAO,KAAK,EACV,aAAa,EACb,iBAAiB,EACjB,iBAAiB,EAClB,qCAAqC;AACtC,OAAO,KAAK,EACV,IAAI,EACJ,SAAS,EACT,aAAa,EAEb,cAAc,EACd,eAAe,EAChB,wBAAwB;AAGzB;;GAEG;AACH,KAAK,cAAc,CAAC,MAAM,SAAS,aAAa,GAAG,aAAa,IAAI;IAClE,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,KAAK,OAAO,CACV,OAAO,SAAS,cAAc,GAAG,cAAc,EAC/C,OAAO,SAAS,iBAAiB,GAAG,iBAAiB,IACnD;IACF,MAAM,EAAE,aAAa,GAAG,eAAe,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;CAC3D,CAAC;AAEF;;;;;GAKG;AACH,qBAAa,gBAAgB,CAC3B,OAAO,SAAS,iBAAiB,GAAG,iBAAiB;;IAIrD;;;;;OAKG;gBACS,EAAE,MAAM,EAAE,EAAE,OAAO,CAAC,cAAc,EAAE,OAAO,CAAC;IASxD;;;;;;;OAOG;IACG,OAAO,CAAC,MAAM,SAAS,aAAa,EAAE,MAAM,SAAS,IAAI,EAC7D,cAAc,EAAE,cAAc,CAAC,MAAM,CAAC,EACtC,OAAO,CAAC,EAAE,aAAa,CAAC,OAAO,CAAC,GAC/B,OAAO,CAAC,MAAM,CAAC;IAMlB;;;;;;;;;;OAUG;IACH,SAAS,2FAIW,OAAO,gBAAgB,GAAG,KAAK,IAAI,KACpD,IAAI,CAIL;IAEF;;;;;;;;;;OAUG;IACH,IAAI,2FAIgB,OAAO,gBAAgB,GAAG,KAAK,IAAI,KACpD,IAAI,CAOL;CAkCH;AAED;;;;;GAKG;AACH,wBAAgB,qCAAqC,CACnD,cAAc,EAAE,cAAc,GAC7B,cAAc,CAehB"}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
-
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");
|
|
4
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
-
};
|
|
6
1
|
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
7
2
|
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
8
3
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
9
4
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
10
5
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
6
|
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
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
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
12
|
var _InternalProvider_engine, _InternalProvider_handle, _InternalProvider_handleWithCallback;
|
|
13
|
-
import { asV2Middleware } from
|
|
14
|
-
import { JsonRpcEngineV2 } from
|
|
15
|
-
import { nanoid } from
|
|
13
|
+
import { asV2Middleware } from "@metamask/json-rpc-engine";
|
|
14
|
+
import { JsonRpcEngineV2 } from "@metamask/json-rpc-engine/v2";
|
|
15
|
+
import { nanoid } from "nanoid";
|
|
16
16
|
/**
|
|
17
17
|
* An Ethereum provider.
|
|
18
18
|
*
|
|
@@ -129,4 +129,4 @@ export function convertEip1193RequestToJsonRpcRequest(eip1193Request) {
|
|
|
129
129
|
method,
|
|
130
130
|
};
|
|
131
131
|
}
|
|
132
|
-
//# sourceMappingURL=internal-provider.
|
|
132
|
+
//# sourceMappingURL=internal-provider.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal-provider.mjs","sourceRoot":"","sources":["../src/internal-provider.ts"],"names":[],"mappings":";;;;;;;;;;;;AAA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AyD,EACnD,EAAE;YACR,mDAAmD;YACnD,uBAAA,IAAI,gCAAQ,MAAZ,IAAI,EAAS,cAAc,CAAC;gBAC1B,oDAAoD;iBACnD,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;iBAC5C,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;gBACf,QAAQ,CAAC,KAAK,CAAC,CAAC;YAClB,CAAC,CAAC,CAAC;YACL,kDAAkD;QACpD,CAAC,EAAC;QAvGA,uBAAA,IAAI,4BACF,MAAM,IAAI,MAAM;YACd,CAAC,CAAC,eAAe,CAAC,MAAM,CAAC;gBACrB,UAAU,EAAE,CAAC,cAAc,CAAgC,MAAM,CAAC,CAAC;aACpE,CAAC;YACJ,CAAC,CAAC,MAAM,MAAA,CAAC;IACf,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,OAAO,CACX,cAAsC,EACtC,OAAgC;QAEhC,MAAM,cAAc,GAClB,qCAAqC,CAAC,cAAc,CAAC,CAAC;QACxD,OAAO,CAAC,MAAM,uBAAA,IAAI,gCAAQ,MAAZ,IAAI,EAAiB,cAAc,EAAE,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC;IACtE,CAAC;CAiFF;;AAED;;;;;GAKG;AACH,MAAM,UAAU,qCAAqC,CACnD,cAA8B;IAE9B,MAAM,EAAE,EAAE,GAAG,MAAM,EAAE,EAAE,OAAO,GAAG,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,cAAc,CAAC;IAE1E,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","sourcesContent":["import { asV2Middleware } from '@metamask/json-rpc-engine';\nimport type { JsonRpcEngine } from '@metamask/json-rpc-engine';\nimport { JsonRpcEngineV2 } from '@metamask/json-rpc-engine/v2';\nimport type {\n HandleOptions,\n ContextConstraint,\n MiddlewareContext,\n} from '@metamask/json-rpc-engine/v2';\nimport type {\n Json,\n JsonRpcId,\n JsonRpcParams,\n JsonRpcSuccess,\n JsonRpcRequest,\n JsonRpcVersion2,\n} from '@metamask/utils';\nimport { nanoid } from 'nanoid';\n\n/**\n * A JSON-RPC request conforming to the EIP-1193 specification.\n */\ntype Eip1193Request<Params extends JsonRpcParams = JsonRpcParams> = {\n id?: JsonRpcId;\n jsonrpc?: JsonRpcVersion2;\n method: string;\n params?: Params;\n};\n\ntype Options<\n Request extends JsonRpcRequest = JsonRpcRequest,\n Context extends ContextConstraint = MiddlewareContext,\n> = {\n engine: JsonRpcEngine | JsonRpcEngineV2<Request, Context>;\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 Context extends ContextConstraint = MiddlewareContext,\n> {\n readonly #engine: JsonRpcEngineV2<JsonRpcRequest, Context>;\n\n /**\n * Construct a InternalProvider from a JSON-RPC server or legacy engine.\n *\n * @param options - Options.\n * @param options.engine - The JSON-RPC engine used to process requests.\n */\n constructor({ engine }: Options<JsonRpcRequest, Context>) {\n this.#engine =\n 'push' in engine\n ? JsonRpcEngineV2.create({\n middleware: [asV2Middleware<JsonRpcParams, JsonRpcRequest>(engine)],\n })\n : engine;\n }\n\n /**\n * Send a provider request asynchronously.\n *\n * @param eip1193Request - The request to send.\n * @param options - The options for the request operation.\n * @param options.context - The context to include with the request.\n * @returns The JSON-RPC response.\n */\n async request<Params extends JsonRpcParams, Result extends Json>(\n eip1193Request: Eip1193Request<Params>,\n options?: HandleOptions<Context>,\n ): Promise<Result> {\n const jsonRpcRequest =\n convertEip1193RequestToJsonRpcRequest(eip1193Request);\n return (await this.#handle<Result>(jsonRpcRequest, options)).result;\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. This method is retained solely for backwards\n * compatibility with certain libraries.\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 ): void => {\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. This method is retained solely for backwards\n * compatibility with certain libraries.\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 ): void => {\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 options?: HandleOptions<Context>,\n ): Promise<JsonRpcSuccess<Result>> => {\n const { id, jsonrpc } = jsonRpcRequest;\n // The `result` typecast is unsafe, but we need it to preserve the provider's\n // public interface, which allows you to unsafely typecast results.\n const result = (await this.#engine.handle(\n jsonRpcRequest,\n options,\n )) as unknown as Result;\n\n return {\n id,\n jsonrpc,\n result,\n };\n };\n\n readonly #handleWithCallback = (\n jsonRpcRequest: JsonRpcRequest,\n callback: (error: unknown, providerRes?: unknown) => void,\n ): void => {\n /* eslint-disable promise/no-callback-in-promise */\n this.#handle(jsonRpcRequest)\n // A resolution will always be a successful response\n .then((response) => callback(null, response))\n .catch((error) => {\n callback(error);\n });\n /* eslint-enable 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 eip1193Request: Eip1193Request,\n): JsonRpcRequest {\n const { id = nanoid(), jsonrpc = '2.0', method, params } = eip1193Request;\n\n return params\n ? {\n id,\n jsonrpc,\n method,\n params,\n }\n : {\n id,\n jsonrpc,\n method,\n };\n}\n"]}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.providerFromMiddlewareV2 = exports.providerFromMiddleware = void 0;
|
|
4
|
+
const json_rpc_engine_1 = require("@metamask/json-rpc-engine");
|
|
5
|
+
const v2_1 = require("@metamask/json-rpc-engine/v2");
|
|
6
|
+
const internal_provider_js_1 = require("./internal-provider.cjs");
|
|
7
|
+
/**
|
|
8
|
+
* Construct an Ethereum provider from the given middleware.
|
|
9
|
+
*
|
|
10
|
+
* @param middleware - The middleware to construct a provider from.
|
|
11
|
+
* @returns An Ethereum provider.
|
|
12
|
+
* @deprecated Use `JsonRpcEngineV2` middleware and {@link providerFromMiddlewareV2} instead.
|
|
13
|
+
*/
|
|
14
|
+
function providerFromMiddleware(middleware) {
|
|
15
|
+
return providerFromMiddlewareV2(
|
|
16
|
+
// This function is generic on the Params and Result types to match the legacy JsonRpcMiddleware type.
|
|
17
|
+
// However, since the V2 JsonRpcMiddleware type is not generic on the Params, we need to elide this
|
|
18
|
+
// parameter by upcasting the request type to JsonRpcRequest, or we get an error due to contravariance
|
|
19
|
+
// since JsonRpcRequest<Params> is not assignable to JsonRpcRequest.
|
|
20
|
+
(0, json_rpc_engine_1.asV2Middleware)(middleware));
|
|
21
|
+
}
|
|
22
|
+
exports.providerFromMiddleware = providerFromMiddleware;
|
|
23
|
+
/**
|
|
24
|
+
* Construct an Ethereum provider from the given middleware.
|
|
25
|
+
*
|
|
26
|
+
* @param middleware - The middleware to construct a provider from.
|
|
27
|
+
* @returns An Ethereum provider.
|
|
28
|
+
*/
|
|
29
|
+
function providerFromMiddlewareV2(middleware) {
|
|
30
|
+
return new internal_provider_js_1.InternalProvider({
|
|
31
|
+
engine: v2_1.JsonRpcEngineV2.create({
|
|
32
|
+
// This function is generic in order to accept middleware functions with narrower types than
|
|
33
|
+
// the plain JsonRpcMiddleware<JsonRpcRequest> type. However, since InternalProvider is non-generic,
|
|
34
|
+
// we need to upcast the middleware to avoid a type error.
|
|
35
|
+
middleware: [middleware],
|
|
36
|
+
}),
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
exports.providerFromMiddlewareV2 = providerFromMiddlewareV2;
|
|
40
|
+
//# sourceMappingURL=provider-from-middleware.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-from-middleware.cjs","sourceRoot":"","sources":["../src/provider-from-middleware.ts"],"names":[],"mappings":";;;AAAA,+DAA2D;AAE3D,qDAA+D;AAQ/D,kEAA0D;AAE1D;;;;;;GAMG;AACH,SAAgB,sBAAsB,CAGpC,UAAmD;IACnD,OAAO,wBAAwB;IAC7B,sGAAsG;IACtG,mGAAmG;IACnG,sGAAsG;IACtG,oEAAoE;IACpE,IAAA,gCAAc,EAAC,UAAU,CAAsC,CAChE,CAAC;AACJ,CAAC;AAXD,wDAWC;AAED;;;;;GAKG;AACH,SAAgB,wBAAwB,CAOtC,UAAsB;IACtB,OAAO,IAAI,uCAAgB,CAAC;QAC1B,MAAM,EAAE,oBAAe,CAAC,MAAM,CAAC;YAC7B,4FAA4F;YAC5F,oGAAoG;YACpG,0DAA0D;YAC1D,UAAU,EAAE,CAAC,UAA+C,CAAC;SAC9D,CAAC;KACH,CAAC,CAAC;AACL,CAAC;AAhBD,4DAgBC","sourcesContent":["import { asV2Middleware } from '@metamask/json-rpc-engine';\nimport type { JsonRpcMiddleware as LegacyJsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport { JsonRpcEngineV2 } from '@metamask/json-rpc-engine/v2';\nimport type {\n ContextConstraint,\n JsonRpcMiddleware,\n ResultConstraint,\n} from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\n\nimport { InternalProvider } from './internal-provider.js';\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 `JsonRpcEngineV2` middleware and {@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 // This function is generic on the Params and Result types to match the legacy JsonRpcMiddleware type.\n // However, since the V2 JsonRpcMiddleware type is not generic on the Params, we need to elide this\n // parameter by upcasting the request type to JsonRpcRequest, or we get an error due to contravariance\n // since JsonRpcRequest<Params> is not assignable to JsonRpcRequest.\n asV2Middleware(middleware) as JsonRpcMiddleware<JsonRpcRequest>,\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 Request extends JsonRpcRequest,\n Middleware extends JsonRpcMiddleware<\n Request,\n ResultConstraint<Request>,\n ContextConstraint\n >,\n>(middleware: Middleware): InternalProvider {\n return new InternalProvider({\n engine: JsonRpcEngineV2.create({\n // This function is generic in order to accept middleware functions with narrower types than\n // the plain JsonRpcMiddleware<JsonRpcRequest> type. However, since InternalProvider is non-generic,\n // we need to upcast the middleware to avoid a type error.\n middleware: [middleware as JsonRpcMiddleware<JsonRpcRequest>],\n }),\n });\n}\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { JsonRpcMiddleware as LegacyJsonRpcMiddleware } from
|
|
2
|
-
import type { ContextConstraint, JsonRpcMiddleware, ResultConstraint } from
|
|
3
|
-
import type { Json, JsonRpcParams, JsonRpcRequest } from
|
|
4
|
-
import { InternalProvider } from
|
|
1
|
+
import type { JsonRpcMiddleware as LegacyJsonRpcMiddleware } from "@metamask/json-rpc-engine";
|
|
2
|
+
import type { ContextConstraint, JsonRpcMiddleware, ResultConstraint } from "@metamask/json-rpc-engine/v2";
|
|
3
|
+
import type { Json, JsonRpcParams, JsonRpcRequest } from "@metamask/utils";
|
|
4
|
+
import { InternalProvider } from "./internal-provider.cjs";
|
|
5
5
|
/**
|
|
6
6
|
* Construct an Ethereum provider from the given middleware.
|
|
7
7
|
*
|
|
@@ -17,4 +17,4 @@ export declare function providerFromMiddleware<Params extends JsonRpcParams, Res
|
|
|
17
17
|
* @returns An Ethereum provider.
|
|
18
18
|
*/
|
|
19
19
|
export declare function providerFromMiddlewareV2<Request extends JsonRpcRequest, Middleware extends JsonRpcMiddleware<Request, ResultConstraint<Request>, ContextConstraint>>(middleware: Middleware): InternalProvider;
|
|
20
|
-
//# sourceMappingURL=provider-from-middleware.d.
|
|
20
|
+
//# sourceMappingURL=provider-from-middleware.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
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,EACV,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EACjB,qCAAqC;AACtC,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,cAAc,EAAE,wBAAwB;AAE3E,OAAO,EAAE,gBAAgB,EAAE,gCAA+B;AAE1D;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,SAAS,aAAa,EAC5B,MAAM,SAAS,IAAI,EACnB,UAAU,EAAE,uBAAuB,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,gBAAgB,CAQvE;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,OAAO,SAAS,cAAc,EAC9B,UAAU,SAAS,iBAAiB,CAClC,OAAO,EACP,gBAAgB,CAAC,OAAO,CAAC,EACzB,iBAAiB,CAClB,EACD,UAAU,EAAE,UAAU,GAAG,gBAAgB,CAS1C"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { JsonRpcMiddleware as LegacyJsonRpcMiddleware } from "@metamask/json-rpc-engine";
|
|
2
|
+
import type { ContextConstraint, JsonRpcMiddleware, ResultConstraint } from "@metamask/json-rpc-engine/v2";
|
|
3
|
+
import type { Json, JsonRpcParams, JsonRpcRequest } from "@metamask/utils";
|
|
4
|
+
import { InternalProvider } from "./internal-provider.mjs";
|
|
5
|
+
/**
|
|
6
|
+
* Construct an Ethereum provider from the given middleware.
|
|
7
|
+
*
|
|
8
|
+
* @param middleware - The middleware to construct a provider from.
|
|
9
|
+
* @returns An Ethereum provider.
|
|
10
|
+
* @deprecated Use `JsonRpcEngineV2` middleware and {@link providerFromMiddlewareV2} instead.
|
|
11
|
+
*/
|
|
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<Request extends JsonRpcRequest, Middleware extends JsonRpcMiddleware<Request, ResultConstraint<Request>, ContextConstraint>>(middleware: Middleware): InternalProvider;
|
|
20
|
+
//# sourceMappingURL=provider-from-middleware.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
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,EACV,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EACjB,qCAAqC;AACtC,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,cAAc,EAAE,wBAAwB;AAE3E,OAAO,EAAE,gBAAgB,EAAE,gCAA+B;AAE1D;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,SAAS,aAAa,EAC5B,MAAM,SAAS,IAAI,EACnB,UAAU,EAAE,uBAAuB,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,gBAAgB,CAQvE;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,OAAO,SAAS,cAAc,EAC9B,UAAU,SAAS,iBAAiB,CAClC,OAAO,EACP,gBAAgB,CAAC,OAAO,CAAC,EACzB,iBAAiB,CAClB,EACD,UAAU,EAAE,UAAU,GAAG,gBAAgB,CAS1C"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { asV2Middleware } from
|
|
2
|
-
import { JsonRpcEngineV2 } from
|
|
3
|
-
import { InternalProvider } from
|
|
1
|
+
import { asV2Middleware } from "@metamask/json-rpc-engine";
|
|
2
|
+
import { JsonRpcEngineV2 } from "@metamask/json-rpc-engine/v2";
|
|
3
|
+
import { InternalProvider } from "./internal-provider.mjs";
|
|
4
4
|
/**
|
|
5
5
|
* Construct an Ethereum provider from the given middleware.
|
|
6
6
|
*
|
|
@@ -32,4 +32,4 @@ export function providerFromMiddlewareV2(middleware) {
|
|
|
32
32
|
}),
|
|
33
33
|
});
|
|
34
34
|
}
|
|
35
|
-
//# sourceMappingURL=provider-from-middleware.
|
|
35
|
+
//# sourceMappingURL=provider-from-middleware.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
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,eAAe,EAAE,qCAAqC;AAQ/D,OAAO,EAAE,gBAAgB,EAAE,gCAA+B;AAE1D;;;;;;GAMG;AACH,MAAM,UAAU,sBAAsB,CAGpC,UAAmD;IACnD,OAAO,wBAAwB;IAC7B,sGAAsG;IACtG,mGAAmG;IACnG,sGAAsG;IACtG,oEAAoE;IACpE,cAAc,CAAC,UAAU,CAAsC,CAChE,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,wBAAwB,CAOtC,UAAsB;IACtB,OAAO,IAAI,gBAAgB,CAAC;QAC1B,MAAM,EAAE,eAAe,CAAC,MAAM,CAAC;YAC7B,4FAA4F;YAC5F,oGAAoG;YACpG,0DAA0D;YAC1D,UAAU,EAAE,CAAC,UAA+C,CAAC;SAC9D,CAAC;KACH,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { asV2Middleware } from '@metamask/json-rpc-engine';\nimport type { JsonRpcMiddleware as LegacyJsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport { JsonRpcEngineV2 } from '@metamask/json-rpc-engine/v2';\nimport type {\n ContextConstraint,\n JsonRpcMiddleware,\n ResultConstraint,\n} from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\n\nimport { InternalProvider } from './internal-provider.js';\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 `JsonRpcEngineV2` middleware and {@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 // This function is generic on the Params and Result types to match the legacy JsonRpcMiddleware type.\n // However, since the V2 JsonRpcMiddleware type is not generic on the Params, we need to elide this\n // parameter by upcasting the request type to JsonRpcRequest, or we get an error due to contravariance\n // since JsonRpcRequest<Params> is not assignable to JsonRpcRequest.\n asV2Middleware(middleware) as JsonRpcMiddleware<JsonRpcRequest>,\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 Request extends JsonRpcRequest,\n Middleware extends JsonRpcMiddleware<\n Request,\n ResultConstraint<Request>,\n ContextConstraint\n >,\n>(middleware: Middleware): InternalProvider {\n return new InternalProvider({\n engine: JsonRpcEngineV2.create({\n // This function is generic in order to accept middleware functions with narrower types than\n // the plain JsonRpcMiddleware<JsonRpcRequest> type. However, since InternalProvider is non-generic,\n // we need to upcast the middleware to avoid a type error.\n middleware: [middleware as JsonRpcMiddleware<JsonRpcRequest>],\n }),\n });\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/eth-json-rpc-provider",
|
|
3
|
-
"version": "6.0.1-preview-
|
|
3
|
+
"version": "6.0.1-preview-eb263b0ee",
|
|
4
4
|
"description": "Create an Ethereum provider using a JSON-RPC engine or middleware",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Ethereum",
|
|
@@ -18,12 +18,19 @@
|
|
|
18
18
|
"files": [
|
|
19
19
|
"dist/"
|
|
20
20
|
],
|
|
21
|
-
"type": "module",
|
|
22
21
|
"sideEffects": false,
|
|
22
|
+
"main": "./dist/index.cjs",
|
|
23
|
+
"types": "./dist/index.d.cts",
|
|
23
24
|
"exports": {
|
|
24
25
|
".": {
|
|
25
|
-
"
|
|
26
|
-
|
|
26
|
+
"import": {
|
|
27
|
+
"types": "./dist/index.d.mts",
|
|
28
|
+
"default": "./dist/index.mjs"
|
|
29
|
+
},
|
|
30
|
+
"require": {
|
|
31
|
+
"types": "./dist/index.d.cts",
|
|
32
|
+
"default": "./dist/index.cjs"
|
|
33
|
+
}
|
|
27
34
|
},
|
|
28
35
|
"./package.json": "./package.json"
|
|
29
36
|
},
|
|
@@ -32,11 +39,9 @@
|
|
|
32
39
|
"registry": "https://registry.npmjs.org/"
|
|
33
40
|
},
|
|
34
41
|
"scripts": {
|
|
35
|
-
"build": "
|
|
36
|
-
"build:all": "
|
|
37
|
-
"build:clean": "yarn build:only-clean && yarn build",
|
|
42
|
+
"build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
|
|
43
|
+
"build:all": "ts-bridge --project tsconfig.build.json --verbose --clean",
|
|
38
44
|
"build:docs": "typedoc",
|
|
39
|
-
"build:only-clean": "rimraf './dist' './tsconfig.build.tsbuildinfo'",
|
|
40
45
|
"changelog:update": "../../scripts/update-changelog.sh @metamask/eth-json-rpc-provider",
|
|
41
46
|
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/eth-json-rpc-provider",
|
|
42
47
|
"lint:tsconfigs": "tsx ../../scripts/lint-tsconfigs/lint-tsconfigs.mts",
|
|
@@ -58,19 +63,18 @@
|
|
|
58
63
|
"@metamask/auto-changelog": "^6.1.0",
|
|
59
64
|
"@metamask/eth-query": "^4.0.0",
|
|
60
65
|
"@metamask/ethjs-query": "^0.5.3",
|
|
66
|
+
"@ts-bridge/cli": "^0.6.4",
|
|
61
67
|
"@types/jest": "^30.0.0",
|
|
62
|
-
"@typescript/native": "npm:typescript@^7.0.2",
|
|
63
68
|
"deepmerge": "^4.2.2",
|
|
64
69
|
"ethers": "^6.12.0",
|
|
65
70
|
"jest": "^30.4.2",
|
|
66
|
-
"rimraf": "^5.0.5",
|
|
67
71
|
"ts-jest": "^29.4.11",
|
|
68
72
|
"tsx": "^4.20.5",
|
|
69
73
|
"typedoc": "^0.25.13",
|
|
70
|
-
"typescript": "
|
|
74
|
+
"typescript": "~5.3.3"
|
|
71
75
|
},
|
|
72
76
|
"engines": {
|
|
73
|
-
"node": "^
|
|
77
|
+
"node": "^18.18 || >=20"
|
|
74
78
|
},
|
|
75
79
|
"lavamoat": {
|
|
76
80
|
"allowScripts": {
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D,cAAc,+BAA+B,CAAC;AAE9C;;GAEG;AACH,KAAK,wBAAwB,GAAG,gBAAgB,CAAC;AACjD,QAAA,MAAM,wBAAwB,yBAAmB,CAAC;AAElD,OAAO,EAAE,gBAAgB,EAAE,wBAAwB,EAAE,CAAC"}
|
package/dist/index.js
DELETED
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D,cAAc,+BAA+B,CAAC;AAM9C,MAAM,wBAAwB,GAAG,gBAAgB,CAAC;AAElD,OAAO,EAAE,gBAAgB,EAAE,wBAAwB,EAAE,CAAC","sourcesContent":["import { InternalProvider } from './internal-provider.js';\n\nexport * from './provider-from-middleware.js';\n\n/**\n * @deprecated Use {@link InternalProvider} instead.\n */\ntype SafeEventEmitterProvider = InternalProvider;\nconst SafeEventEmitterProvider = InternalProvider;\n\nexport { InternalProvider, SafeEventEmitterProvider };\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"internal-provider.d.ts","sourceRoot":"","sources":["../src/internal-provider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAC/D,OAAO,KAAK,EACV,aAAa,EACb,iBAAiB,EACjB,iBAAiB,EAClB,MAAM,8BAA8B,CAAC;AACtC,OAAO,KAAK,EACV,IAAI,EACJ,SAAS,EACT,aAAa,EAEb,cAAc,EACd,eAAe,EAChB,MAAM,iBAAiB,CAAC;A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;YACE,EAAE;YACF,OAAO;YACP,MAAM;SACP,CAAC;AACR,CAAC","sourcesContent":["import { asV2Middleware } from '@metamask/json-rpc-engine';\nimport type { JsonRpcEngine } from '@metamask/json-rpc-engine';\nimport { JsonRpcEngineV2 } from '@metamask/json-rpc-engine/v2';\nimport type {\n HandleOptions,\n ContextConstraint,\n MiddlewareContext,\n} from '@metamask/json-rpc-engine/v2';\nimport type {\n Json,\n JsonRpcId,\n JsonRpcParams,\n JsonRpcSuccess,\n JsonRpcRequest,\n JsonRpcVersion2,\n} from '@metamask/utils';\nimport { nanoid } from 'nanoid';\n\n/**\n * A JSON-RPC request conforming to the EIP-1193 specification.\n */\ntype Eip1193Request<Params extends JsonRpcParams = JsonRpcParams> = {\n id?: JsonRpcId;\n jsonrpc?: JsonRpcVersion2;\n method: string;\n params?: Params;\n};\n\ntype Options<\n Request extends JsonRpcRequest = JsonRpcRequest,\n Context extends ContextConstraint = MiddlewareContext,\n> = {\n engine: JsonRpcEngine | JsonRpcEngineV2<Request, Context>;\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 Context extends ContextConstraint = MiddlewareContext,\n> {\n readonly #engine: JsonRpcEngineV2<JsonRpcRequest, Context>;\n\n /**\n * Construct a InternalProvider from a JSON-RPC server or legacy engine.\n *\n * @param options - Options.\n * @param options.engine - The JSON-RPC engine used to process requests.\n */\n constructor({ engine }: Options<JsonRpcRequest, Context>) {\n this.#engine =\n 'push' in engine\n ? JsonRpcEngineV2.create({\n middleware: [asV2Middleware<JsonRpcParams, JsonRpcRequest>(engine)],\n })\n : engine;\n }\n\n /**\n * Send a provider request asynchronously.\n *\n * @param eip1193Request - The request to send.\n * @param options - The options for the request operation.\n * @param options.context - The context to include with the request.\n * @returns The JSON-RPC response.\n */\n async request<Params extends JsonRpcParams, Result extends Json>(\n eip1193Request: Eip1193Request<Params>,\n options?: HandleOptions<Context>,\n ): Promise<Result> {\n const jsonRpcRequest =\n convertEip1193RequestToJsonRpcRequest(eip1193Request);\n return (await this.#handle<Result>(jsonRpcRequest, options)).result;\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. This method is retained solely for backwards\n * compatibility with certain libraries.\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 ): void => {\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. This method is retained solely for backwards\n * compatibility with certain libraries.\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 ): void => {\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 options?: HandleOptions<Context>,\n ): Promise<JsonRpcSuccess<Result>> => {\n const { id, jsonrpc } = jsonRpcRequest;\n // The `result` typecast is unsafe, but we need it to preserve the provider's\n // public interface, which allows you to unsafely typecast results.\n const result = (await this.#engine.handle(\n jsonRpcRequest,\n options,\n )) as unknown as Result;\n\n return {\n id,\n jsonrpc,\n result,\n };\n };\n\n readonly #handleWithCallback = (\n jsonRpcRequest: JsonRpcRequest,\n callback: (error: unknown, providerRes?: unknown) => void,\n ): void => {\n /* eslint-disable promise/no-callback-in-promise */\n this.#handle(jsonRpcRequest)\n // A resolution will always be a successful response\n .then((response) => callback(null, response))\n .catch((error) => {\n callback(error);\n });\n /* eslint-enable 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 eip1193Request: Eip1193Request,\n): JsonRpcRequest {\n const { id = nanoid(), jsonrpc = '2.0', method, params } = eip1193Request;\n\n return params\n ? {\n id,\n jsonrpc,\n method,\n params,\n }\n : {\n id,\n jsonrpc,\n method,\n };\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"provider-from-middleware.d.ts","sourceRoot":"","sources":["../src/provider-from-middleware.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,IAAI,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AAE9F,OAAO,KAAK,EACV,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EACjB,MAAM,8BAA8B,CAAC;AACtC,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAE3E,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,SAAS,aAAa,EAC5B,MAAM,SAAS,IAAI,EACnB,UAAU,EAAE,uBAAuB,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,gBAAgB,CAQvE;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,OAAO,SAAS,cAAc,EAC9B,UAAU,SAAS,iBAAiB,CAClC,OAAO,EACP,gBAAgB,CAAC,OAAO,CAAC,EACzB,iBAAiB,CAClB,EACD,UAAU,EAAE,UAAU,GAAG,gBAAgB,CAS1C"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"provider-from-middleware.js","sourceRoot":"","sources":["../src/provider-from-middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAE3D,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAQ/D,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D;;;;;;GAMG;AACH,MAAM,UAAU,sBAAsB,CAGpC,UAAmD;IACnD,OAAO,wBAAwB;IAC7B,sGAAsG;IACtG,mGAAmG;IACnG,sGAAsG;IACtG,oEAAoE;IACpE,cAAc,CAAC,UAAU,CAAsC,CAChE,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,wBAAwB,CAOtC,UAAsB;IACtB,OAAO,IAAI,gBAAgB,CAAC;QAC1B,MAAM,EAAE,eAAe,CAAC,MAAM,CAAC;YAC7B,4FAA4F;YAC5F,oGAAoG;YACpG,0DAA0D;YAC1D,UAAU,EAAE,CAAC,UAA+C,CAAC;SAC9D,CAAC;KACH,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { asV2Middleware } from '@metamask/json-rpc-engine';\nimport type { JsonRpcMiddleware as LegacyJsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport { JsonRpcEngineV2 } from '@metamask/json-rpc-engine/v2';\nimport type {\n ContextConstraint,\n JsonRpcMiddleware,\n ResultConstraint,\n} from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\n\nimport { InternalProvider } from './internal-provider.js';\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 `JsonRpcEngineV2` middleware and {@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 // This function is generic on the Params and Result types to match the legacy JsonRpcMiddleware type.\n // However, since the V2 JsonRpcMiddleware type is not generic on the Params, we need to elide this\n // parameter by upcasting the request type to JsonRpcRequest, or we get an error due to contravariance\n // since JsonRpcRequest<Params> is not assignable to JsonRpcRequest.\n asV2Middleware(middleware) as JsonRpcMiddleware<JsonRpcRequest>,\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 Request extends JsonRpcRequest,\n Middleware extends JsonRpcMiddleware<\n Request,\n ResultConstraint<Request>,\n ContextConstraint\n >,\n>(middleware: Middleware): InternalProvider {\n return new InternalProvider({\n engine: JsonRpcEngineV2.create({\n // This function is generic in order to accept middleware functions with narrower types than\n // the plain JsonRpcMiddleware<JsonRpcRequest> type. However, since InternalProvider is non-generic,\n // we need to upcast the middleware to avoid a type error.\n middleware: [middleware as JsonRpcMiddleware<JsonRpcRequest>],\n }),\n });\n}\n"]}
|