ethers.js/packages/providers/lib/web3-provider.js

118 lines
4.5 KiB
JavaScript
Raw Permalink Normal View History

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; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
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) {
return function (method, params) {
2019-05-14 18:48:48 -04:00
// Metamask complains about eth_sign (and on some versions hangs)
2020-04-27 06:31:43 -04:00
if (method == "eth_sign" && provider.isMetaMask) {
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) {
2020-04-27 06:31:43 -04:00
sendFunc(request, function (error, result) {
2019-05-14 18:48:48 -04:00
if (error) {
2020-04-27 06:31:43 -04:00
return reject(error);
2019-05-14 18:48:48 -04:00
}
if (result.error) {
var error_1 = new Error(result.error.message);
error_1.code = result.error.code;
error_1.data = result.error.data;
2020-04-27 06:31:43 -04:00
return reject(error_1);
2019-05-14 18:48:48 -04:00
}
resolve(result.result);
});
});
};
2020-04-27 06:31:43 -04:00
}
function buildEip1193Fetcher(provider) {
return function (method, params) {
if (params == null) {
params = [];
}
// Metamask complains about eth_sign (and on some versions hangs)
if (method == "eth_sign" && provider.isMetaMask) {
// https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_sign
method = "personal_sign";
params = [params[1], params[0]];
}
return provider.request({ method: method, params: params });
};
}
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