@machinomy/hdwallet-provider
Version:
HD Wallet-enabled Web3 provider
89 lines • 3.03 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.timeout = exports.stripHexPrefix = exports.blockTagParamIndex = exports.blockTagForPayload = exports.baseProvider = exports.createPayload = exports.randomId = void 0;
const fetch_1 = __importDefault(require("web3-provider-engine/subproviders/fetch"));
const websocket_1 = __importDefault(require("web3-provider-engine/subproviders/websocket"));
const EXTRA_DIGITS = 3;
function randomId() {
// 13 time digits
const datePart = new Date().getTime() * Math.pow(10, EXTRA_DIGITS);
// 3 random digits
const extraPart = Math.floor(Math.random() * Math.pow(10, EXTRA_DIGITS));
// 16 digits
return datePart + extraPart;
}
exports.randomId = randomId;
function createPayload(data) {
const empty = {
id: randomId(),
jsonrpc: "2.0",
params: []
};
return Object.assign(empty, data);
}
exports.createPayload = createPayload;
function baseProvider(rpcUrl) {
const protocol = rpcUrl.split(":")[0].toLowerCase();
switch (protocol) {
case "http":
return new fetch_1.default({ rpcUrl });
case "https":
return new fetch_1.default({ rpcUrl });
case "ws":
return new websocket_1.default({ rpcUrl });
case "wss":
return new websocket_1.default({ rpcUrl });
default:
throw new Error(`ProviderEngine - unrecognized protocol in "${rpcUrl}"`);
}
}
exports.baseProvider = baseProvider;
function blockTagForPayload(payload) {
const index = blockTagParamIndex(payload);
// Block tag param not passed.
if (!index || index >= payload.params.length) {
return null;
}
return payload.params[index];
}
exports.blockTagForPayload = blockTagForPayload;
function blockTagParamIndex(payload) {
switch (payload.method) {
// blockTag is third param
case "eth_getStorageAt":
return 2;
// blockTag is second param
case "eth_getBalance":
case "eth_getCode":
case "eth_getTransactionCount":
case "eth_call":
case "eth_estimateGas":
return 1;
// blockTag is first param
case "eth_getBlockByNumber":
return 0;
// there is no blockTag
default:
return undefined;
}
}
exports.blockTagParamIndex = blockTagParamIndex;
function stripHexPrefix(str) {
const isHexPrefixed = str.slice(0, 2) === "0x";
return isHexPrefixed ? str.slice(2) : str;
}
exports.stripHexPrefix = stripHexPrefix;
function timeout(duration, unref) {
return new Promise(resolve => {
const timoutRef = setTimeout(resolve, duration);
// don't keep process open
if (timoutRef.unref && unref) {
timoutRef.unref();
}
});
}
exports.timeout = timeout;
//# sourceMappingURL=util.js.map