401 lines
16 KiB
JavaScript
401 lines
16 KiB
JavaScript
"use strict";
|
|
import { Signer } from "@ethersproject/abstract-signer";
|
|
import { BigNumber } from "@ethersproject/bignumber";
|
|
import { hexlify, hexValue } from "@ethersproject/bytes";
|
|
import { getNetwork } from "@ethersproject/networks";
|
|
import { checkProperties, deepCopy, defineReadOnly, resolveProperties, shallowCopy } from "@ethersproject/properties";
|
|
import { toUtf8Bytes } from "@ethersproject/strings";
|
|
import { fetchJson, poll } from "@ethersproject/web";
|
|
import { Logger } from "@ethersproject/logger";
|
|
import { version } from "./_version";
|
|
const logger = new Logger(version);
|
|
import { BaseProvider } from "./base-provider";
|
|
function timer(timeout) {
|
|
return new Promise(function (resolve) {
|
|
setTimeout(function () {
|
|
resolve();
|
|
}, timeout);
|
|
});
|
|
}
|
|
function getResult(payload) {
|
|
if (payload.error) {
|
|
// @TODO: not any
|
|
let error = new Error(payload.error.message);
|
|
error.code = payload.error.code;
|
|
error.data = payload.error.data;
|
|
throw error;
|
|
}
|
|
return payload.result;
|
|
}
|
|
function getLowerCase(value) {
|
|
if (value) {
|
|
return value.toLowerCase();
|
|
}
|
|
return value;
|
|
}
|
|
const _constructorGuard = {};
|
|
export class JsonRpcSigner extends Signer {
|
|
constructor(constructorGuard, provider, addressOrIndex) {
|
|
logger.checkNew(new.target, JsonRpcSigner);
|
|
super();
|
|
if (constructorGuard !== _constructorGuard) {
|
|
throw new Error("do not call the JsonRpcSigner constructor directly; use provider.getSigner");
|
|
}
|
|
defineReadOnly(this, "provider", provider);
|
|
if (addressOrIndex == null) {
|
|
addressOrIndex = 0;
|
|
}
|
|
if (typeof (addressOrIndex) === "string") {
|
|
defineReadOnly(this, "_address", this.provider.formatter.address(addressOrIndex));
|
|
defineReadOnly(this, "_index", null);
|
|
}
|
|
else if (typeof (addressOrIndex) === "number") {
|
|
defineReadOnly(this, "_index", addressOrIndex);
|
|
defineReadOnly(this, "_address", null);
|
|
}
|
|
else {
|
|
logger.throwArgumentError("invalid address or index", "addressOrIndex", addressOrIndex);
|
|
}
|
|
}
|
|
connect(provider) {
|
|
return logger.throwError("cannot alter JSON-RPC Signer connection", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
operation: "connect"
|
|
});
|
|
}
|
|
connectUnchecked() {
|
|
return new UncheckedJsonRpcSigner(_constructorGuard, this.provider, this._address || this._index);
|
|
}
|
|
getAddress() {
|
|
if (this._address) {
|
|
return Promise.resolve(this._address);
|
|
}
|
|
return this.provider.send("eth_accounts", []).then((accounts) => {
|
|
if (accounts.length <= this._index) {
|
|
logger.throwError("unknown account #" + this._index, Logger.errors.UNSUPPORTED_OPERATION, {
|
|
operation: "getAddress"
|
|
});
|
|
}
|
|
return this.provider.formatter.address(accounts[this._index]);
|
|
});
|
|
}
|
|
sendUncheckedTransaction(transaction) {
|
|
transaction = shallowCopy(transaction);
|
|
let fromAddress = this.getAddress().then((address) => {
|
|
if (address) {
|
|
address = address.toLowerCase();
|
|
}
|
|
return address;
|
|
});
|
|
// The JSON-RPC for eth_sendTransaction uses 90000 gas; if the user
|
|
// wishes to use this, it is easy to specify explicitly, otherwise
|
|
// we look it up for them.
|
|
if (transaction.gasLimit == null) {
|
|
let estimate = shallowCopy(transaction);
|
|
estimate.from = fromAddress;
|
|
transaction.gasLimit = this.provider.estimateGas(estimate);
|
|
}
|
|
return Promise.all([
|
|
resolveProperties(transaction),
|
|
fromAddress
|
|
]).then((results) => {
|
|
let tx = results[0];
|
|
let hexTx = this.provider.constructor.hexlifyTransaction(tx);
|
|
hexTx.from = results[1];
|
|
return this.provider.send("eth_sendTransaction", [hexTx]).then((hash) => {
|
|
return hash;
|
|
}, (error) => {
|
|
if (error.responseText) {
|
|
// See: JsonRpcProvider.sendTransaction (@TODO: Expose a ._throwError??)
|
|
if (error.responseText.indexOf("insufficient funds") >= 0) {
|
|
logger.throwError("insufficient funds", Logger.errors.INSUFFICIENT_FUNDS, {
|
|
transaction: tx
|
|
});
|
|
}
|
|
if (error.responseText.indexOf("nonce too low") >= 0) {
|
|
logger.throwError("nonce has already been used", Logger.errors.NONCE_EXPIRED, {
|
|
transaction: tx
|
|
});
|
|
}
|
|
if (error.responseText.indexOf("replacement transaction underpriced") >= 0) {
|
|
logger.throwError("replacement fee too low", Logger.errors.REPLACEMENT_UNDERPRICED, {
|
|
transaction: tx
|
|
});
|
|
}
|
|
}
|
|
throw error;
|
|
});
|
|
});
|
|
}
|
|
signTransaction(transaction) {
|
|
return logger.throwError("signing transactions is unsupported", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
operation: "signTransaction"
|
|
});
|
|
}
|
|
sendTransaction(transaction) {
|
|
return this.sendUncheckedTransaction(transaction).then((hash) => {
|
|
return poll(() => {
|
|
return this.provider.getTransaction(hash).then((tx) => {
|
|
if (tx === null) {
|
|
return undefined;
|
|
}
|
|
return this.provider._wrapTransaction(tx, hash);
|
|
});
|
|
}, { onceBlock: this.provider }).catch((error) => {
|
|
error.transactionHash = hash;
|
|
throw error;
|
|
});
|
|
});
|
|
}
|
|
signMessage(message) {
|
|
let data = ((typeof (message) === "string") ? toUtf8Bytes(message) : message);
|
|
return this.getAddress().then((address) => {
|
|
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign
|
|
return this.provider.send("eth_sign", [address.toLowerCase(), hexlify(data)]);
|
|
});
|
|
}
|
|
unlock(password) {
|
|
let provider = this.provider;
|
|
return this.getAddress().then(function (address) {
|
|
return provider.send("personal_unlockAccount", [address.toLowerCase(), password, null]);
|
|
});
|
|
}
|
|
}
|
|
class UncheckedJsonRpcSigner extends JsonRpcSigner {
|
|
sendTransaction(transaction) {
|
|
return this.sendUncheckedTransaction(transaction).then((hash) => {
|
|
return {
|
|
hash: hash,
|
|
nonce: null,
|
|
gasLimit: null,
|
|
gasPrice: null,
|
|
data: null,
|
|
value: null,
|
|
chainId: null,
|
|
confirmations: 0,
|
|
from: null,
|
|
wait: (confirmations) => { return this.provider.waitForTransaction(hash, confirmations); }
|
|
};
|
|
});
|
|
}
|
|
}
|
|
const allowedTransactionKeys = {
|
|
chainId: true, data: true, gasLimit: true, gasPrice: true, nonce: true, to: true, value: true
|
|
};
|
|
export class JsonRpcProvider extends BaseProvider {
|
|
constructor(url, network) {
|
|
logger.checkNew(new.target, JsonRpcProvider);
|
|
// One parameter, but it is a network name, so swap it with the URL
|
|
if (typeof (url) === "string") {
|
|
if (network === null && getNetwork(url)) {
|
|
network = url;
|
|
url = null;
|
|
}
|
|
}
|
|
if (network) {
|
|
// The network has been specified explicitly, we can use it
|
|
super(network);
|
|
}
|
|
else {
|
|
// The network is unknown, query the JSON-RPC for it
|
|
let ready = new Promise((resolve, reject) => {
|
|
setTimeout(() => {
|
|
this.send("eth_chainId", []).then((result) => {
|
|
resolve(getNetwork(BigNumber.from(result).toNumber()));
|
|
}).catch((error) => {
|
|
this.send("net_version", []).then((result) => {
|
|
resolve(getNetwork(BigNumber.from(result).toNumber()));
|
|
}).catch((error) => {
|
|
reject(logger.makeError("could not detect network", Logger.errors.NETWORK_ERROR));
|
|
});
|
|
});
|
|
});
|
|
});
|
|
super(ready);
|
|
}
|
|
// Default URL
|
|
if (!url) {
|
|
url = "http:/" + "/localhost:8545";
|
|
}
|
|
if (typeof (url) === "string") {
|
|
this.connection = {
|
|
url: url
|
|
};
|
|
}
|
|
else {
|
|
this.connection = url;
|
|
}
|
|
this._nextId = 42;
|
|
}
|
|
getSigner(addressOrIndex) {
|
|
return new JsonRpcSigner(_constructorGuard, this, addressOrIndex);
|
|
}
|
|
getUncheckedSigner(addressOrIndex) {
|
|
return this.getSigner(addressOrIndex).connectUnchecked();
|
|
}
|
|
listAccounts() {
|
|
return this.send("eth_accounts", []).then((accounts) => {
|
|
return accounts.map((a) => this.formatter.address(a));
|
|
});
|
|
}
|
|
send(method, params) {
|
|
let request = {
|
|
method: method,
|
|
params: params,
|
|
id: (this._nextId++),
|
|
jsonrpc: "2.0"
|
|
};
|
|
this.emit("debug", {
|
|
action: "request",
|
|
request: deepCopy(request),
|
|
provider: this
|
|
});
|
|
return fetchJson(this.connection, JSON.stringify(request), getResult).then((result) => {
|
|
this.emit("debug", {
|
|
action: "response",
|
|
request: request,
|
|
response: result,
|
|
provider: this
|
|
});
|
|
return result;
|
|
});
|
|
}
|
|
perform(method, params) {
|
|
switch (method) {
|
|
case "getBlockNumber":
|
|
return this.send("eth_blockNumber", []);
|
|
case "getGasPrice":
|
|
return this.send("eth_gasPrice", []);
|
|
case "getBalance":
|
|
return this.send("eth_getBalance", [getLowerCase(params.address), params.blockTag]);
|
|
case "getTransactionCount":
|
|
return this.send("eth_getTransactionCount", [getLowerCase(params.address), params.blockTag]);
|
|
case "getCode":
|
|
return this.send("eth_getCode", [getLowerCase(params.address), params.blockTag]);
|
|
case "getStorageAt":
|
|
return this.send("eth_getStorageAt", [getLowerCase(params.address), params.position, params.blockTag]);
|
|
case "sendTransaction":
|
|
return this.send("eth_sendRawTransaction", [params.signedTransaction]).catch((error) => {
|
|
if (error.responseText) {
|
|
// "insufficient funds for gas * price + value"
|
|
if (error.responseText.indexOf("insufficient funds") > 0) {
|
|
logger.throwError("insufficient funds", Logger.errors.INSUFFICIENT_FUNDS, {});
|
|
}
|
|
// "nonce too low"
|
|
if (error.responseText.indexOf("nonce too low") > 0) {
|
|
logger.throwError("nonce has already been used", Logger.errors.NONCE_EXPIRED, {});
|
|
}
|
|
// "replacement transaction underpriced"
|
|
if (error.responseText.indexOf("replacement transaction underpriced") > 0) {
|
|
logger.throwError("replacement fee too low", Logger.errors.REPLACEMENT_UNDERPRICED, {});
|
|
}
|
|
}
|
|
throw error;
|
|
});
|
|
case "getBlock":
|
|
if (params.blockTag) {
|
|
return this.send("eth_getBlockByNumber", [params.blockTag, !!params.includeTransactions]);
|
|
}
|
|
else if (params.blockHash) {
|
|
return this.send("eth_getBlockByHash", [params.blockHash, !!params.includeTransactions]);
|
|
}
|
|
return logger.throwArgumentError("invalid block tag or block hash", "params", params);
|
|
case "getTransaction":
|
|
return this.send("eth_getTransactionByHash", [params.transactionHash]);
|
|
case "getTransactionReceipt":
|
|
return this.send("eth_getTransactionReceipt", [params.transactionHash]);
|
|
case "call":
|
|
return this.send("eth_call", [this.constructor.hexlifyTransaction(params.transaction, { from: true }), params.blockTag]);
|
|
case "estimateGas":
|
|
return this.send("eth_estimateGas", [this.constructor.hexlifyTransaction(params.transaction, { from: true })]);
|
|
case "getLogs":
|
|
if (params.filter && params.filter.address != null) {
|
|
params.filter.address = getLowerCase(params.filter.address);
|
|
}
|
|
return this.send("eth_getLogs", [params.filter]);
|
|
default:
|
|
break;
|
|
}
|
|
return logger.throwError(method + " not implemented", Logger.errors.NOT_IMPLEMENTED, { operation: method });
|
|
}
|
|
_startPending() {
|
|
if (this._pendingFilter != null) {
|
|
return;
|
|
}
|
|
let self = this;
|
|
let pendingFilter = this.send("eth_newPendingTransactionFilter", []);
|
|
this._pendingFilter = pendingFilter;
|
|
pendingFilter.then(function (filterId) {
|
|
function poll() {
|
|
self.send("eth_getFilterChanges", [filterId]).then(function (hashes) {
|
|
if (self._pendingFilter != pendingFilter) {
|
|
return null;
|
|
}
|
|
let seq = Promise.resolve();
|
|
hashes.forEach(function (hash) {
|
|
// @TODO: This should be garbage collected at some point... How? When?
|
|
self._emitted["t:" + hash.toLowerCase()] = "pending";
|
|
seq = seq.then(function () {
|
|
return self.getTransaction(hash).then(function (tx) {
|
|
self.emit("pending", tx);
|
|
return null;
|
|
});
|
|
});
|
|
});
|
|
return seq.then(function () {
|
|
return timer(1000);
|
|
});
|
|
}).then(function () {
|
|
if (self._pendingFilter != pendingFilter) {
|
|
self.send("eth_uninstallFilter", [filterId]);
|
|
return;
|
|
}
|
|
setTimeout(function () { poll(); }, 0);
|
|
return null;
|
|
}).catch((error) => { });
|
|
}
|
|
poll();
|
|
return filterId;
|
|
}).catch((error) => { });
|
|
}
|
|
_stopPending() {
|
|
this._pendingFilter = null;
|
|
}
|
|
// Convert an ethers.js transaction into a JSON-RPC transaction
|
|
// - gasLimit => gas
|
|
// - All values hexlified
|
|
// - All numeric values zero-striped
|
|
// NOTE: This allows a TransactionRequest, but all values should be resolved
|
|
// before this is called
|
|
static hexlifyTransaction(transaction, allowExtra) {
|
|
// Check only allowed properties are given
|
|
let allowed = shallowCopy(allowedTransactionKeys);
|
|
if (allowExtra) {
|
|
for (let key in allowExtra) {
|
|
if (allowExtra[key]) {
|
|
allowed[key] = true;
|
|
}
|
|
}
|
|
}
|
|
checkProperties(transaction, allowed);
|
|
let result = {};
|
|
// Some nodes (INFURA ropsten; INFURA mainnet is fine) do not like leading zeros.
|
|
["gasLimit", "gasPrice", "nonce", "value"].forEach(function (key) {
|
|
if (transaction[key] == null) {
|
|
return;
|
|
}
|
|
let value = hexValue(transaction[key]);
|
|
if (key === "gasLimit") {
|
|
key = "gas";
|
|
}
|
|
result[key] = value;
|
|
});
|
|
["from", "to", "data"].forEach(function (key) {
|
|
if (transaction[key] == null) {
|
|
return;
|
|
}
|
|
result[key] = hexlify(transaction[key]);
|
|
});
|
|
return result;
|
|
}
|
|
}
|