2019-05-14 18:25:46 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import { Networkish } from "@ethersproject/networks";
|
2021-05-13 22:27:13 -04:00
|
|
|
import { deepCopy, defineReadOnly } from "@ethersproject/properties";
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-08-01 18:04:06 -04:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
import { JsonRpcProvider } from "./json-rpc-provider";
|
|
|
|
|
|
|
|
// Exported Types
|
2020-04-27 06:15:44 -04:00
|
|
|
export type ExternalProvider = {
|
2019-05-14 18:25:46 -04:00
|
|
|
isMetaMask?: boolean;
|
2021-02-08 14:46:04 -05:00
|
|
|
isStatus?: boolean;
|
2019-05-14 18:25:46 -04:00
|
|
|
host?: string;
|
|
|
|
path?: string;
|
2020-04-27 06:15:44 -04:00
|
|
|
sendAsync?: (request: { method: string, params?: Array<any> }, callback: (error: any, response: any) => void) => void
|
|
|
|
send?: (request: { method: string, params?: Array<any> }, callback: (error: any, response: any) => void) => void
|
|
|
|
request?: (request: { method: string, params?: Array<any> }) => Promise<any>
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-04-27 06:15:44 -04:00
|
|
|
let _nextId = 1;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-04-27 06:15:44 -04:00
|
|
|
export type JsonRpcFetchFunc = (method: string, params?: Array<any>) => Promise<any>;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-04-27 06:15:44 -04:00
|
|
|
type Web3LegacySend = (request: any, callback: (error: Error, response: any) => void) => void;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-04-27 06:15:44 -04:00
|
|
|
function buildWeb3LegacyFetcher(provider: ExternalProvider, sendFunc: Web3LegacySend) : JsonRpcFetchFunc {
|
2021-05-13 22:27:13 -04:00
|
|
|
const fetcher = "Web3LegacyFetcher";
|
|
|
|
|
2020-04-27 06:15:44 -04:00
|
|
|
return function(method: string, params: Array<any>): Promise<any> {
|
|
|
|
const request = {
|
|
|
|
method: method,
|
|
|
|
params: params,
|
|
|
|
id: (_nextId++),
|
|
|
|
jsonrpc: "2.0"
|
|
|
|
};
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-05-13 22:27:13 -04:00
|
|
|
this.emit("debug", {
|
|
|
|
action: "request",
|
|
|
|
fetcher,
|
|
|
|
request: deepCopy(request),
|
|
|
|
provider: this
|
|
|
|
});
|
|
|
|
|
|
|
|
sendFunc(request, (error, response) => {
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
this.emit("debug", {
|
|
|
|
action: "response",
|
|
|
|
fetcher,
|
|
|
|
error,
|
|
|
|
request,
|
|
|
|
provider: this
|
|
|
|
});
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-04-27 06:15:44 -04:00
|
|
|
return reject(error);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2021-05-13 22:27:13 -04:00
|
|
|
this.emit("debug", {
|
|
|
|
action: "response",
|
|
|
|
fetcher,
|
|
|
|
request,
|
|
|
|
response,
|
|
|
|
provider: this
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.error) {
|
|
|
|
const error = new Error(response.error.message);
|
|
|
|
(<any>error).code = response.error.code;
|
|
|
|
(<any>error).data = response.error.data;
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(response.result);
|
2019-05-14 18:25:46 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-04-27 06:15:44 -04:00
|
|
|
|
|
|
|
function buildEip1193Fetcher(provider: ExternalProvider): JsonRpcFetchFunc {
|
|
|
|
return function(method: string, params: Array<any>): Promise<any> {
|
|
|
|
if (params == null) { params = [ ]; }
|
|
|
|
|
2021-05-13 22:27:13 -04:00
|
|
|
const request = { method, params };
|
|
|
|
|
|
|
|
this.emit("debug", {
|
|
|
|
action: "request",
|
|
|
|
fetcher: "Eip1193Fetcher",
|
|
|
|
request: deepCopy(request),
|
|
|
|
provider: this
|
|
|
|
});
|
|
|
|
|
|
|
|
return provider.request(request).then((response) => {
|
|
|
|
this.emit("debug", {
|
|
|
|
action: "response",
|
|
|
|
fetcher: "Eip1193Fetcher",
|
|
|
|
request,
|
|
|
|
response,
|
|
|
|
provider: this
|
|
|
|
});
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
|
|
|
}, (error) => {
|
|
|
|
this.emit("debug", {
|
|
|
|
action: "response",
|
|
|
|
fetcher: "Eip1193Fetcher",
|
|
|
|
request,
|
|
|
|
error,
|
|
|
|
provider: this
|
|
|
|
});
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
});
|
2020-04-27 06:15:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Web3Provider extends JsonRpcProvider {
|
|
|
|
readonly provider: ExternalProvider;
|
|
|
|
readonly jsonRpcFetchFunc: JsonRpcFetchFunc;
|
|
|
|
|
|
|
|
constructor(provider: ExternalProvider | JsonRpcFetchFunc, network?: Networkish) {
|
|
|
|
if (provider == null) {
|
|
|
|
logger.throwArgumentError("missing provider", "provider", provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
let path: string = null;
|
|
|
|
let jsonRpcFetchFunc: JsonRpcFetchFunc = null;
|
|
|
|
let subprovider: ExternalProvider = 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: string, params: Array<any>): Promise<any> {
|
|
|
|
return this.jsonRpcFetchFunc(method, params);
|
|
|
|
}
|
|
|
|
}
|