PHP WebShell
Текущая директория: /usr/lib/node_modules/bitgo/node_modules/@celo/connect/lib
Просмотр файла: connection.js
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Connection = void 0;
var address_1 = require("@celo/utils/lib/address");
var sign_typed_data_utils_1 = require("@celo/utils/lib/sign-typed-data-utils");
var signatureUtils_1 = require("@celo/utils/lib/signatureUtils");
var debug_1 = __importDefault(require("debug"));
var celo_provider_1 = require("./celo-provider");
var abi_utils_1 = require("./utils/abi-utils");
var formatter_1 = require("./utils/formatter");
var provider_utils_1 = require("./utils/provider-utils");
var rpc_caller_1 = require("./utils/rpc-caller");
var tx_params_normalizer_1 = require("./utils/tx-params-normalizer");
var tx_result_1 = require("./utils/tx-result");
var debugGasEstimation = (0, debug_1.default)('connection:gas-estimation');
/**
* Connection is a Class for connecting to Celo, sending Transactions, etc
* @param web3 an instance of web3
* @optional wallet a child class of {@link WalletBase}
* @optional handleRevert sets handleRevert on the web3.eth instance passed in
*/
var Connection = /** @class */ (function () {
function Connection(web3, wallet, handleRevert) {
if (handleRevert === void 0) { handleRevert = true; }
var _this = this;
var _a;
this.web3 = web3;
this.wallet = wallet;
/** @deprecated no longer needed since gasPrice is available on minimumClientVersion node rpc */
this.currencyGasPrice = new Map();
this.keccak256 = function (value) {
return _this.web3.utils.keccak256(value);
};
this.hexToAscii = function (hex) {
return _this.web3.utils.hexToAscii(hex);
};
/**
* Send a transaction to celo-blockchain.
*
* Similar to `web3.eth.sendTransaction()` but with following differences:
* - applies connections tx's defaults
* - estimatesGas before sending
* - returns a `TransactionResult` instead of `PromiEvent`
*/
this.sendTransaction = function (tx) { return __awaiter(_this, void 0, void 0, function () {
var gas;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
tx = this.fillTxDefaults(tx);
tx = this.fillGasPrice(tx);
gas = tx.gas;
if (!(gas == null)) return [3 /*break*/, 2];
return [4 /*yield*/, this.estimateGasWithInflationFactor(tx)];
case 1:
gas = _a.sent();
_a.label = 2;
case 2: return [2 /*return*/, (0, tx_result_1.toTxResult)(this.web3.eth.sendTransaction(__assign(__assign({}, tx), { gas: gas })))];
}
});
}); };
this.sendTransactionObject = function (txObj, tx) { return __awaiter(_this, void 0, void 0, function () {
var gas, gasEstimator, getCallTx_1, caller;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
tx = this.fillTxDefaults(tx);
tx = this.fillGasPrice(tx);
gas = tx.gas;
if (!(gas == null)) return [3 /*break*/, 2];
gasEstimator = function (_tx) { return txObj.estimateGas(__assign({}, _tx)); };
getCallTx_1 = function (_tx) {
// @ts-ignore missing _parent property from TransactionObject type.
return __assign(__assign({}, _tx), { data: txObj.encodeABI(), to: txObj._parent._address });
};
caller = function (_tx) { return _this.web3.eth.call(getCallTx_1(_tx)); };
return [4 /*yield*/, this.estimateGasWithInflationFactor(tx, gasEstimator, caller)];
case 1:
gas = _a.sent();
_a.label = 2;
case 2: return [2 /*return*/, (0, tx_result_1.toTxResult)(txObj.send(__assign(__assign({}, tx), { gas: gas })))];
}
});
}); };
/*
* @param signer - The address of account signing this data
* @param typedData - Structured data to be signed
* @param version - Optionally provide a version which will be appended to the method. E.G. (4) becomes 'eth_signTypedData_v4'
* @remarks Some providers like Metamask treat eth_signTypedData differently from versioned method eth_signTypedData_v4
* @see [Metamask info in signing Typed Data](https://docs.metamask.io/guide/signing-data.html)
*/
this.signTypedData = function (signer, typedData, version) { return __awaiter(_this, void 0, void 0, function () {
var shouldStringify, signature, messageHash;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
shouldStringify = version === 3 || version === 4;
return [4 /*yield*/, new Promise(function (resolve, reject) {
var method = version ? "eth_signTypedData_v".concat(version) : 'eth_signTypedData';
_this.web3.currentProvider.send({
id: (0, rpc_caller_1.getRandomId)(),
jsonrpc: '2.0',
method: method,
params: [
(0, formatter_1.inputAddressFormatter)(signer),
shouldStringify ? JSON.stringify(typedData) : typedData,
],
}, function (error, resp) {
if (error) {
reject(error);
}
else if (resp) {
resolve(resp.result);
}
else {
reject(new Error('empty-response'));
}
});
})];
case 1:
signature = _a.sent();
messageHash = (0, address_1.ensureLeading0x)((0, sign_typed_data_utils_1.generateTypedDataHash)(typedData).toString('hex'));
return [2 /*return*/, (0, signatureUtils_1.parseSignatureWithoutPrefix)(messageHash, signature, signer)];
}
});
}); };
this.sign = function (dataToSign, address) { return __awaiter(_this, void 0, void 0, function () {
var signature;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, new Promise(function (resolve, reject) {
;
_this.web3.currentProvider.send({
id: (0, rpc_caller_1.getRandomId)(),
jsonrpc: '2.0',
method: 'eth_sign',
params: [(0, formatter_1.inputAddressFormatter)(address.toString()), (0, formatter_1.inputSignFormatter)(dataToSign)],
}, function (error, resp) {
if (error) {
reject(error);
}
else if (resp) {
resolve(resp.result);
}
else {
reject(new Error('empty-response'));
}
});
})];
case 1:
signature = _a.sent();
return [2 /*return*/, signature];
}
});
}); };
this.sendSignedTransaction = function (signedTransactionData) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, (0, tx_result_1.toTxResult)(this.web3.eth.sendSignedTransaction(signedTransactionData))];
});
}); };
this.estimateGas = function (tx, gasEstimator, caller) {
if (gasEstimator === void 0) { gasEstimator = _this.web3.eth.estimateGas; }
if (caller === void 0) { caller = _this.web3.eth.call; }
return __awaiter(_this, void 0, void 0, function () {
var gas, e_1, called, revertReason;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 4]);
return [4 /*yield*/, gasEstimator(__assign({}, tx))];
case 1:
gas = _a.sent();
debugGasEstimation('estimatedGas: %s', gas.toString());
return [2 /*return*/, gas];
case 2:
e_1 = _a.sent();
return [4 /*yield*/, caller({ data: tx.data, to: tx.to, from: tx.from })];
case 3:
called = _a.sent();
revertReason = 'Could not decode transaction failure reason';
if (called.startsWith('0x08c379a')) {
revertReason = (0, abi_utils_1.decodeStringParameter)(this.getAbiCoder(), called.substring(10));
}
debugGasEstimation('Recover transaction failure reason', {
called: called,
data: tx.data,
to: tx.to,
from: tx.from,
error: e_1,
revertReason: revertReason,
});
return [2 /*return*/, Promise.reject("Gas estimation failed: ".concat(revertReason, " or ").concat(e_1))];
case 4: return [2 /*return*/];
}
});
});
};
this.estimateGasWithInflationFactor = function (tx, gasEstimator, caller) { return __awaiter(_this, void 0, void 0, function () {
var gas, _a, _b, e_2;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_c.trys.push([0, 2, , 3]);
_b = (_a = Math).round;
return [4 /*yield*/, this.estimateGas(tx, gasEstimator, caller)];
case 1:
gas = _b.apply(_a, [(_c.sent()) * this.config.gasInflationFactor]);
debugGasEstimation('estimatedGasWithInflationFactor: %s', gas);
return [2 /*return*/, gas];
case 2:
e_2 = _c.sent();
throw new Error(e_2);
case 3: return [2 /*return*/];
}
});
}); };
this.chainId = function () { return __awaiter(_this, void 0, void 0, function () {
var response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.rpcCaller.call('net_version', [])];
case 1:
response = _a.sent();
return [2 /*return*/, parseInt(response.result.toString(), 10)];
}
});
}); };
this.getTransactionCount = function (address) { return __awaiter(_this, void 0, void 0, function () {
var response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.rpcCaller.call('eth_getTransactionCount', [address, 'pending'])];
case 1:
response = _a.sent();
return [2 /*return*/, (0, formatter_1.hexToNumber)(response.result)];
}
});
}); };
this.nonce = function (address) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.getTransactionCount(address)];
});
}); };
this.coinbase = function () { return __awaiter(_this, void 0, void 0, function () {
var response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.rpcCaller.call('eth_coinbase', [])];
case 1:
response = _a.sent();
return [2 /*return*/, response.result.toString()];
}
});
}); };
this.gasPrice = function (feeCurrency) { return __awaiter(_this, void 0, void 0, function () {
var parameter, response, gasPriceInHex;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
parameter = feeCurrency ? [feeCurrency] : [];
return [4 /*yield*/, this.rpcCaller.call('eth_gasPrice', parameter)];
case 1:
response = _a.sent();
gasPriceInHex = response.result.toString();
return [2 /*return*/, gasPriceInHex];
}
});
}); };
this.getBlockNumber = function () { return __awaiter(_this, void 0, void 0, function () {
var response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.rpcCaller.call('eth_blockNumber', [])];
case 1:
response = _a.sent();
return [2 /*return*/, (0, formatter_1.hexToNumber)(response.result)];
}
});
}); };
this.isBlockNumberHash = function (blockNumber) {
return blockNumber instanceof String && blockNumber.indexOf('0x') === 0;
};
this.getBlock = function (blockHashOrBlockNumber, fullTxObjects) {
if (fullTxObjects === void 0) { fullTxObjects = true; }
return __awaiter(_this, void 0, void 0, function () {
var endpoint, response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
endpoint = this.isBlockNumberHash(blockHashOrBlockNumber)
? 'eth_getBlockByHash' // Reference: https://eth.wiki/json-rpc/API#eth_getBlockByHash
: 'eth_getBlockByNumber' // Reference: https://eth.wiki/json-rpc/API#eth_getBlockByNumber
;
return [4 /*yield*/, this.rpcCaller.call(endpoint, [
(0, formatter_1.inputBlockNumberFormatter)(blockHashOrBlockNumber),
fullTxObjects,
])];
case 1:
response = _a.sent();
return [2 /*return*/, (0, formatter_1.outputBlockFormatter)(response.result)];
}
});
});
};
this.getBlockHeader = function (blockHashOrBlockNumber) { return __awaiter(_this, void 0, void 0, function () {
var endpoint, response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
endpoint = this.isBlockNumberHash(blockHashOrBlockNumber)
? 'eth_getHeaderByHash'
: 'eth_getHeaderByNumber';
return [4 /*yield*/, this.rpcCaller.call(endpoint, [
(0, formatter_1.inputBlockNumberFormatter)(blockHashOrBlockNumber),
])];
case 1:
response = _a.sent();
return [2 /*return*/, (0, formatter_1.outputBlockHeaderFormatter)(response.result)];
}
});
}); };
this.getBalance = function (address, defaultBlock) { return __awaiter(_this, void 0, void 0, function () {
var response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.rpcCaller.call('eth_getBalance', [
(0, formatter_1.inputAddressFormatter)(address),
(0, formatter_1.inputDefaultBlockNumberFormatter)(defaultBlock),
])];
case 1:
response = _a.sent();
return [2 /*return*/, (0, formatter_1.outputBigNumberFormatter)(response.result)];
}
});
}); };
this.getTransaction = function (transactionHash) { return __awaiter(_this, void 0, void 0, function () {
var response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.rpcCaller.call('eth_getTransactionByHash', [
(0, address_1.ensureLeading0x)(transactionHash),
])];
case 1:
response = _a.sent();
return [2 /*return*/, (0, formatter_1.outputCeloTxFormatter)(response.result)];
}
});
}); };
this.getTransactionReceipt = function (txhash) { return __awaiter(_this, void 0, void 0, function () {
var response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.rpcCaller.call('eth_getTransactionReceipt', [
(0, address_1.ensureLeading0x)(txhash),
])];
case 1:
response = _a.sent();
if (response.result === null) {
return [2 /*return*/, null];
}
return [2 /*return*/, (0, formatter_1.outputCeloTxReceiptFormatter)(response.result)];
}
});
}); };
web3.eth.handleRevert = handleRevert;
this.config = {
gasInflationFactor: 1.3,
// gasPrice:0 means the node will compute gasPrice on its own
gasPrice: '0',
};
var existingProvider = web3.currentProvider;
this.setProvider(existingProvider);
// TODO: Add this line with the wallets separation completed
// this.wallet = _wallet ?? new LocalWallet()
this.config.from = (_a = web3.eth.defaultAccount) !== null && _a !== void 0 ? _a : undefined;
this.paramsPopulator = new tx_params_normalizer_1.TxParamsNormalizer(this);
}
Connection.prototype.setProvider = function (provider) {
if (!provider) {
throw new Error('Must have a valid Provider');
}
try {
if (!(provider instanceof celo_provider_1.CeloProvider)) {
this.rpcCaller = new rpc_caller_1.DefaultRpcCaller(provider);
provider = new celo_provider_1.CeloProvider(provider, this);
}
this.web3.setProvider(provider);
return true;
}
catch (_a) {
return false;
}
};
Object.defineProperty(Connection.prototype, "defaultAccount", {
/**
* Default account for generated transactions (eg. tx.from)
*/
get: function () {
return this.config.from;
},
/**
* Set default account for generated transactions (eg. tx.from )
*/
set: function (address) {
this.config.from = address;
this.web3.eth.defaultAccount = address ? address : null;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Connection.prototype, "defaultGasInflationFactor", {
get: function () {
return this.config.gasInflationFactor;
},
set: function (factor) {
this.config.gasInflationFactor = factor;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Connection.prototype, "defaultGasPrice", {
get: function () {
return parseInt(this.config.gasPrice, 10);
},
set: function (price) {
this.config.gasPrice = price.toString(10);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Connection.prototype, "defaultFeeCurrency", {
get: function () {
return this.config.feeCurrency;
},
/**
* Set the ERC20 address for the token to use to pay for transaction fees.
* The ERC20 must be whitelisted for gas.
*
* Set to `null` to use CELO
*
* @param address ERC20 address
*/
set: function (address) {
this.config.feeCurrency = address;
},
enumerable: false,
configurable: true
});
Connection.prototype.isLocalAccount = function (address) {
return this.wallet != null && this.wallet.hasAccount(address);
};
Connection.prototype.addAccount = function (privateKey) {
if (this.wallet) {
if ((0, provider_utils_1.hasProperty)(this.wallet, 'addAccount')) {
this.wallet.addAccount(privateKey);
}
else {
throw new Error("The wallet used, can't add accounts");
}
}
else {
throw new Error('No wallet set');
}
};
Connection.prototype.removeAccount = function (address) {
if (this.wallet) {
if ((0, provider_utils_1.hasProperty)(this.wallet, 'removeAccount')) {
this.wallet.removeAccount(address);
}
else {
throw new Error("The wallet used, can't remove accounts");
}
}
else {
throw new Error('No wallet set');
}
};
Connection.prototype.getNodeAccounts = function () {
var _a;
return __awaiter(this, void 0, void 0, function () {
var nodeAccountsResp;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.rpcCaller.call('eth_accounts', [])];
case 1:
nodeAccountsResp = _b.sent();
return [2 /*return*/, this.toChecksumAddresses((_a = nodeAccountsResp.result) !== null && _a !== void 0 ? _a : [])];
}
});
});
};
Connection.prototype.getLocalAccounts = function () {
return this.wallet ? this.toChecksumAddresses(this.wallet.getAccounts()) : [];
};
Connection.prototype.getAccounts = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getNodeAccounts()];
case 1: return [2 /*return*/, (_a.sent()).concat(this.getLocalAccounts())];
}
});
});
};
Connection.prototype.toChecksumAddresses = function (addresses) {
return addresses.map(function (value) { return (0, address_1.toChecksumAddress)(value); });
};
Connection.prototype.isListening = function () {
return this.web3.eth.net.isListening();
};
Connection.prototype.isSyncing = function () {
var _this = this;
return new Promise(function (resolve, reject) {
_this.web3.eth
.isSyncing()
.then(function (response) {
// isSyncing returns a syncProgress object when it's still syncing
if (typeof response === 'boolean') {
resolve(response);
}
else {
resolve(true);
}
})
.catch(reject);
});
};
/** @deprecated no longer needed since gasPrice is available on minimumClientVersion node rpc */
Connection.prototype.fillGasPrice = function (tx) {
if (tx.feeCurrency && tx.gasPrice === '0' && this.currencyGasPrice.has(tx.feeCurrency)) {
return __assign(__assign({}, tx), { gasPrice: this.currencyGasPrice.get(tx.feeCurrency) });
}
return tx;
};
/** @deprecated no longer needed since gasPrice is available on minimumClientVersion node rpc */
Connection.prototype.setGasPriceForCurrency = function (address, gasPrice) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
this.currencyGasPrice.set(address, gasPrice);
return [2 /*return*/];
});
});
};
Connection.prototype.getAbiCoder = function () {
return this.web3.eth.abi;
};
Connection.prototype.fillTxDefaults = function (tx) {
var defaultTx = {
from: this.config.from,
feeCurrency: this.config.feeCurrency,
gasPrice: this.config.gasPrice,
};
return __assign(__assign({}, defaultTx), tx);
};
Connection.prototype.stop = function () {
(0, celo_provider_1.assertIsCeloProvider)(this.web3.currentProvider);
this.web3.currentProvider.stop();
};
return Connection;
}());
exports.Connection = Connection;
//# sourceMappingURL=connection.js.mapВыполнить команду
Для локальной разработки. Не используйте в интернете!