2019-08-25 02:39:20 -04:00
|
|
|
"use strict";
|
|
|
|
import { defineReadOnly } from "@ethersproject/properties";
|
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
import { JsonRpcProvider } from "./json-rpc-provider";
|
2020-04-27 06:31:43 -04:00
|
|
|
let _nextId = 1;
|
|
|
|
function buildWeb3LegacyFetcher(provider, sendFunc) {
|
|
|
|
return function (method, params) {
|
2019-08-25 02:39:20 -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-08-25 02:39:20 -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
|
|
|
const request = {
|
|
|
|
method: method,
|
|
|
|
params: params,
|
|
|
|
id: (_nextId++),
|
|
|
|
jsonrpc: "2.0"
|
|
|
|
};
|
2019-08-25 02:39:20 -04:00
|
|
|
return new Promise((resolve, reject) => {
|
2020-04-27 06:31:43 -04:00
|
|
|
sendFunc(request, function (error, result) {
|
2019-08-25 02:39:20 -04:00
|
|
|
if (error) {
|
2020-04-27 06:31:43 -04:00
|
|
|
return reject(error);
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
if (result.error) {
|
2019-11-20 18:57:38 +09:00
|
|
|
const error = new Error(result.error.message);
|
2019-08-25 02:39:20 -04:00
|
|
|
error.code = result.error.code;
|
|
|
|
error.data = result.error.data;
|
2020-04-27 06:31:43 -04:00
|
|
|
return reject(error);
|
2019-08-25 02:39:20 -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, params });
|
|
|
|
};
|
|
|
|
}
|
|
|
|
export class Web3Provider extends JsonRpcProvider {
|
|
|
|
constructor(provider, network) {
|
|
|
|
logger.checkNew(new.target, Web3Provider);
|
|
|
|
if (provider == null) {
|
|
|
|
logger.throwArgumentError("missing provider", "provider", provider);
|
|
|
|
}
|
|
|
|
let path = null;
|
|
|
|
let jsonRpcFetchFunc = null;
|
|
|
|
let 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:";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
super(path, network);
|
|
|
|
defineReadOnly(this, "jsonRpcFetchFunc", jsonRpcFetchFunc);
|
|
|
|
defineReadOnly(this, "provider", subprovider);
|
|
|
|
}
|
|
|
|
send(method, params) {
|
|
|
|
return this.jsonRpcFetchFunc(method, params);
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
}
|
2020-07-13 08:03:56 -04:00
|
|
|
//# sourceMappingURL=web3-provider.js.map
|