2019-08-25 09:39:20 +03: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";
|
|
|
|
/*
|
|
|
|
@TODO
|
|
|
|
utils.defineProperty(Web3Signer, "onchange", {
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
*/
|
|
|
|
export class Web3Provider extends JsonRpcProvider {
|
|
|
|
constructor(web3Provider, network) {
|
|
|
|
logger.checkNew(new.target, Web3Provider);
|
|
|
|
// HTTP has a host; IPC has a path.
|
|
|
|
super(web3Provider.host || web3Provider.path || "", network);
|
|
|
|
if (web3Provider) {
|
|
|
|
if (web3Provider.sendAsync) {
|
|
|
|
this._sendAsync = web3Provider.sendAsync.bind(web3Provider);
|
|
|
|
}
|
|
|
|
else if (web3Provider.send) {
|
|
|
|
this._sendAsync = web3Provider.send.bind(web3Provider);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!web3Provider || !this._sendAsync) {
|
|
|
|
logger.throwArgumentError("invalid web3Provider", "web3Provider", web3Provider);
|
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
defineReadOnly(this, "provider", web3Provider);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
send(method, params) {
|
|
|
|
// Metamask complains about eth_sign (and on some versions hangs)
|
2019-11-20 12:57:38 +03:00
|
|
|
if (method == "eth_sign" && this.provider.isMetaMask) {
|
2019-08-25 09:39:20 +03:00
|
|
|
// https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_sign
|
|
|
|
method = "personal_sign";
|
|
|
|
params = [params[1], params[0]];
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-11-20 12:57:38 +03:00
|
|
|
const request = {
|
2019-08-25 09:39:20 +03:00
|
|
|
method: method,
|
|
|
|
params: params,
|
|
|
|
id: 42,
|
|
|
|
jsonrpc: "2.0"
|
|
|
|
};
|
|
|
|
this._sendAsync(request, function (error, result) {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (result.error) {
|
|
|
|
// @TODO: not any
|
2019-11-20 12:57:38 +03:00
|
|
|
const error = new Error(result.error.message);
|
2019-08-25 09:39:20 +03:00
|
|
|
error.code = result.error.code;
|
|
|
|
error.data = result.error.data;
|
|
|
|
reject(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve(result.result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|