2019-05-14 18:48:48 -04:00
|
|
|
"use strict";
|
|
|
|
var __extends = (this && this.__extends) || (function () {
|
|
|
|
var extendStatics = function (d, b) {
|
|
|
|
extendStatics = Object.setPrototypeOf ||
|
|
|
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
2021-03-07 18:24:04 -05:00
|
|
|
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
2019-05-14 18:48:48 -04:00
|
|
|
return extendStatics(d, b);
|
|
|
|
};
|
|
|
|
return function (d, b) {
|
2021-03-07 18:24:04 -05:00
|
|
|
if (typeof b !== "function" && b !== null)
|
|
|
|
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
2019-05-14 18:48:48 -04:00
|
|
|
extendStatics(d, b);
|
|
|
|
function __() { this.constructor = d; }
|
|
|
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2021-03-07 18:24:04 -05:00
|
|
|
exports.Web3Provider = void 0;
|
2019-05-14 18:48:48 -04:00
|
|
|
var properties_1 = require("@ethersproject/properties");
|
2019-08-02 02:10:58 -04:00
|
|
|
var logger_1 = require("@ethersproject/logger");
|
|
|
|
var _version_1 = require("./_version");
|
|
|
|
var logger = new logger_1.Logger(_version_1.version);
|
2019-05-14 18:48:48 -04:00
|
|
|
var json_rpc_provider_1 = require("./json-rpc-provider");
|
2020-04-27 06:31:43 -04:00
|
|
|
var _nextId = 1;
|
|
|
|
function buildWeb3LegacyFetcher(provider, sendFunc) {
|
2021-05-17 16:19:36 -04:00
|
|
|
var fetcher = "Web3LegacyFetcher";
|
2020-04-27 06:31:43 -04:00
|
|
|
return function (method, params) {
|
2021-05-17 16:19:36 -04:00
|
|
|
var _this = this;
|
2019-05-14 18:48:48 -04:00
|
|
|
// Metamask complains about eth_sign (and on some versions hangs)
|
2021-02-08 15:24:45 -05:00
|
|
|
if (method == "eth_sign" && (provider.isMetaMask || provider.isStatus)) {
|
2019-05-14 18:48:48 -04:00
|
|
|
// https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_sign
|
|
|
|
method = "personal_sign";
|
|
|
|
params = [params[1], params[0]];
|
|
|
|
}
|
2020-04-27 06:31:43 -04:00
|
|
|
var request = {
|
|
|
|
method: method,
|
|
|
|
params: params,
|
|
|
|
id: (_nextId++),
|
|
|
|
jsonrpc: "2.0"
|
|
|
|
};
|
2019-05-14 18:48:48 -04:00
|
|
|
return new Promise(function (resolve, reject) {
|
2021-05-17 16:19:36 -04:00
|
|
|
_this.emit("debug", {
|
|
|
|
action: "request",
|
|
|
|
fetcher: fetcher,
|
|
|
|
request: properties_1.deepCopy(request),
|
|
|
|
provider: _this
|
|
|
|
});
|
|
|
|
sendFunc(request, function (error, response) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (error) {
|
2021-05-17 16:19:36 -04:00
|
|
|
_this.emit("debug", {
|
|
|
|
action: "response",
|
|
|
|
fetcher: fetcher,
|
|
|
|
error: error,
|
|
|
|
request: request,
|
|
|
|
provider: _this
|
|
|
|
});
|
2020-04-27 06:31:43 -04:00
|
|
|
return reject(error);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2021-05-17 16:19:36 -04:00
|
|
|
_this.emit("debug", {
|
|
|
|
action: "response",
|
|
|
|
fetcher: fetcher,
|
|
|
|
request: request,
|
|
|
|
response: response,
|
|
|
|
provider: _this
|
|
|
|
});
|
|
|
|
if (response.error) {
|
|
|
|
var error_1 = new Error(response.error.message);
|
|
|
|
error_1.code = response.error.code;
|
|
|
|
error_1.data = response.error.data;
|
2020-04-27 06:31:43 -04:00
|
|
|
return reject(error_1);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2021-05-17 16:19:36 -04:00
|
|
|
resolve(response.result);
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2020-04-27 06:31:43 -04:00
|
|
|
}
|
|
|
|
function buildEip1193Fetcher(provider) {
|
|
|
|
return function (method, params) {
|
2021-05-17 16:19:36 -04:00
|
|
|
var _this = this;
|
2020-04-27 06:31:43 -04:00
|
|
|
if (params == null) {
|
|
|
|
params = [];
|
|
|
|
}
|
|
|
|
// Metamask complains about eth_sign (and on some versions hangs)
|
2021-02-08 15:24:45 -05:00
|
|
|
if (method == "eth_sign" && (provider.isMetaMask || provider.isStatus)) {
|
2020-04-27 06:31:43 -04:00
|
|
|
// https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_sign
|
|
|
|
method = "personal_sign";
|
|
|
|
params = [params[1], params[0]];
|
|
|
|
}
|
2021-05-17 16:19:36 -04:00
|
|
|
var request = { method: method, params: params };
|
|
|
|
this.emit("debug", {
|
|
|
|
action: "request",
|
|
|
|
fetcher: "Eip1193Fetcher",
|
|
|
|
request: properties_1.deepCopy(request),
|
|
|
|
provider: this
|
|
|
|
});
|
|
|
|
return provider.request(request).then(function (response) {
|
|
|
|
_this.emit("debug", {
|
|
|
|
action: "response",
|
|
|
|
fetcher: "Eip1193Fetcher",
|
|
|
|
request: request,
|
|
|
|
response: response,
|
|
|
|
provider: _this
|
|
|
|
});
|
|
|
|
return response;
|
|
|
|
}, function (error) {
|
|
|
|
_this.emit("debug", {
|
|
|
|
action: "response",
|
|
|
|
fetcher: "Eip1193Fetcher",
|
|
|
|
request: request,
|
|
|
|
error: error,
|
|
|
|
provider: _this
|
|
|
|
});
|
|
|
|
throw error;
|
|
|
|
});
|
2020-04-27 06:31:43 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
var Web3Provider = /** @class */ (function (_super) {
|
|
|
|
__extends(Web3Provider, _super);
|
|
|
|
function Web3Provider(provider, network) {
|
|
|
|
var _newTarget = this.constructor;
|
|
|
|
var _this = this;
|
|
|
|
logger.checkNew(_newTarget, Web3Provider);
|
|
|
|
if (provider == null) {
|
|
|
|
logger.throwArgumentError("missing provider", "provider", provider);
|
|
|
|
}
|
|
|
|
var path = null;
|
|
|
|
var jsonRpcFetchFunc = null;
|
|
|
|
var subprovider = null;
|
|
|
|
if (typeof (provider) === "function") {
|
|
|
|
path = "unknown:";
|
|
|
|
jsonRpcFetchFunc = provider;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
path = provider.host || provider.path || "";
|
|
|
|
if (!path && provider.isMetaMask) {
|
|
|
|
path = "metamask";
|
|
|
|
}
|
|
|
|
subprovider = provider;
|
|
|
|
if (provider.request) {
|
|
|
|
if (path === "") {
|
|
|
|
path = "eip-1193:";
|
|
|
|
}
|
|
|
|
jsonRpcFetchFunc = buildEip1193Fetcher(provider);
|
|
|
|
}
|
|
|
|
else if (provider.sendAsync) {
|
|
|
|
jsonRpcFetchFunc = buildWeb3LegacyFetcher(provider, provider.sendAsync.bind(provider));
|
|
|
|
}
|
|
|
|
else if (provider.send) {
|
|
|
|
jsonRpcFetchFunc = buildWeb3LegacyFetcher(provider, provider.send.bind(provider));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logger.throwArgumentError("unsupported provider", "provider", provider);
|
|
|
|
}
|
|
|
|
if (!path) {
|
|
|
|
path = "unknown:";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_this = _super.call(this, path, network) || this;
|
|
|
|
properties_1.defineReadOnly(_this, "jsonRpcFetchFunc", jsonRpcFetchFunc);
|
|
|
|
properties_1.defineReadOnly(_this, "provider", subprovider);
|
|
|
|
return _this;
|
|
|
|
}
|
|
|
|
Web3Provider.prototype.send = function (method, params) {
|
|
|
|
return this.jsonRpcFetchFunc(method, params);
|
|
|
|
};
|
2019-05-14 18:48:48 -04:00
|
|
|
return Web3Provider;
|
|
|
|
}(json_rpc_provider_1.JsonRpcProvider));
|
|
|
|
exports.Web3Provider = Web3Provider;
|
2020-07-13 08:03:56 -04:00
|
|
|
//# sourceMappingURL=web3-provider.js.map
|