UNPKG

3.25 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 };
9 return function (d, b) {
10 if (typeof b !== "function" && b !== null)
11 throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12 extendStatics(d, b);
13 function __() { this.constructor = d; }
14 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15 };
16})();
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.IpcProvider = void 0;
19var net_1 = require("net");
20var properties_1 = require("@ethersproject/properties");
21var logger_1 = require("@ethersproject/logger");
22var _version_1 = require("./_version");
23var logger = new logger_1.Logger(_version_1.version);
24var json_rpc_provider_1 = require("./json-rpc-provider");
25var IpcProvider = /** @class */ (function (_super) {
26 __extends(IpcProvider, _super);
27 function IpcProvider(path, network) {
28 var _this = this;
29 if (path == null) {
30 logger.throwError("missing path", logger_1.Logger.errors.MISSING_ARGUMENT, { arg: "path" });
31 }
32 _this = _super.call(this, "ipc://" + path, network) || this;
33 (0, properties_1.defineReadOnly)(_this, "path", path);
34 return _this;
35 }
36 // @TODO: Create a connection to the IPC path and use filters instead of polling for block
37 IpcProvider.prototype.send = function (method, params) {
38 // This method is very simple right now. We create a new socket
39 // connection each time, which may be slower, but the main
40 // advantage we are aiming for now is security. This simplifies
41 // multiplexing requests (since we do not need to multiplex).
42 var _this = this;
43 var payload = JSON.stringify({
44 method: method,
45 params: params,
46 id: 42,
47 jsonrpc: "2.0"
48 });
49 return new Promise(function (resolve, reject) {
50 var response = Buffer.alloc(0);
51 var stream = (0, net_1.connect)(_this.path);
52 stream.on("data", function (data) {
53 response = Buffer.concat([response, data]);
54 });
55 stream.on("end", function () {
56 try {
57 resolve(JSON.parse(response.toString()).result);
58 // @TODO: Better pull apart the error
59 stream.destroy();
60 }
61 catch (error) {
62 reject(error);
63 stream.destroy();
64 }
65 });
66 stream.on("error", function (error) {
67 reject(error);
68 stream.destroy();
69 });
70 stream.write(payload);
71 stream.end();
72 });
73 };
74 return IpcProvider;
75}(json_rpc_provider_1.JsonRpcProvider));
76exports.IpcProvider = IpcProvider;
77//# sourceMappingURL=ipc-provider.js.map
\No newline at end of file