PHP WebShell
Текущая директория: /opt/BitGoJS/node_modules/@hashgraph/sdk/lib/client
Просмотр файла: NodeClient.cjs
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.Network = exports.MirrorNetwork = void 0;
var _fs = _interopRequireDefault(require("fs"));
var _util = _interopRequireDefault(require("util"));
var _Client = _interopRequireDefault(require("./Client.cjs"));
var _NodeChannel = _interopRequireDefault(require("../channel/NodeChannel.cjs"));
var _NodeMirrorChannel = _interopRequireDefault(require("../channel/NodeMirrorChannel.cjs"));
var _LedgerId = _interopRequireDefault(require("../LedgerId.cjs"));
var _AccountId = _interopRequireDefault(require("../account/AccountId.cjs"));
var _NodeAddressBook = _interopRequireDefault(require("../address_book/NodeAddressBook.cjs"));
var mainnet = _interopRequireWildcard(require("./addressbooks/mainnet.cjs"));
var testnet = _interopRequireWildcard(require("./addressbooks/testnet.cjs"));
var previewnet = _interopRequireWildcard(require("./addressbooks/previewnet.cjs"));
var hex = _interopRequireWildcard(require("../encoding/hex.cjs"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/*-
*
* Hedera JavaScript SDK
*
* Copyright (C) 2020 - 2022 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
const readFileAsync = _util.default.promisify(_fs.default.readFile);
/**
* @typedef {import("./Client.js").ClientConfiguration} ClientConfiguration
*/
const Network = {
LOCAL_NODE: {
"127.0.0.1:50211": new _AccountId.default(3)
}
};
exports.Network = Network;
const MirrorNetwork = {
/**
* @param {string} name
* @returns {string[]}
*/
fromName(name) {
switch (name) {
case "mainnet":
return MirrorNetwork.MAINNET;
case "testnet":
return MirrorNetwork.TESTNET;
case "previewnet":
return MirrorNetwork.PREVIEWNET;
case "local-node":
return MirrorNetwork.LOCAL_NODE;
default:
throw new Error(`unknown network name: ${name}`);
}
},
MAINNET: ["mainnet-public.mirrornode.hedera.com:443"],
TESTNET: ["testnet.mirrornode.hedera.com:443"],
PREVIEWNET: ["previewnet.mirrornode.hedera.com:443"],
LOCAL_NODE: ["127.0.0.1:5600"]
};
/**
* @augments {Client<NodeChannel, NodeMirrorChannel>}
*/
exports.MirrorNetwork = MirrorNetwork;
class NodeClient extends _Client.default {
/**
* @param {ClientConfiguration} [props]
*/
constructor(props) {
super(props);
/** @private */
this._maxExecutionTime = 10000;
if (props != null) {
if (typeof props.network === "string") {
this._setNetworkFromName(props.network);
} else if (props.network != null) {
this.setNetwork(props.network);
}
if (typeof props.mirrorNetwork === "string") {
switch (props.mirrorNetwork) {
case "mainnet":
this.setMirrorNetwork(MirrorNetwork.MAINNET);
break;
case "testnet":
this.setMirrorNetwork(MirrorNetwork.TESTNET);
break;
case "previewnet":
this.setMirrorNetwork(MirrorNetwork.PREVIEWNET);
break;
default:
this.setMirrorNetwork([props.mirrorNetwork]);
break;
}
} else if (props.mirrorNetwork != null) {
this.setMirrorNetwork(props.mirrorNetwork);
}
}
}
/**
* @param {string | ClientConfiguration} data
* @returns {NodeClient}
*/
static fromConfig(data) {
return new NodeClient(typeof data === "string" ? /** @type {ClientConfiguration | undefined} */
JSON.parse(data) : data);
}
/**
* @param {string} filename
* @returns {Promise<NodeClient>}
*/
static async fromConfigFile(filename) {
return NodeClient.fromConfig(await readFileAsync(filename, "utf8"));
}
/**
* Construct a client for a specific network.
*
* It is the responsibility of the caller to ensure that all nodes in the map are part of the
* same Hedera network. Failure to do so will result in undefined behavior.
*
* The client will load balance all requests to Hedera using a simple round-robin scheme to
* chose nodes to send transactions to. For one transaction, at most 1/3 of the nodes will be
* tried.
*
* @param {{[key: string]: (string | AccountId)}} network
* @param {ClientConfiguration} [props]
* @returns {NodeClient}
*/
static forNetwork(network, props) {
return new NodeClient({
network,
...props
});
}
/**
* @param {string} network
* @param {object} [props]
* @param {boolean} [props.scheduleNetworkUpdate]
* @returns {NodeClient}
*/
static forName(network, props = {}) {
return new NodeClient({
network,
...props
});
}
/**
* Construct a Hedera client pre-configured for Mainnet access.
*
* @param {object} [props]
* @param {boolean} [props.scheduleNetworkUpdate]
* @returns {NodeClient}
*/
static forMainnet(props = {}) {
return new NodeClient({
network: "mainnet",
...props
});
}
/**
* Construct a Hedera client pre-configured for Testnet access.
*
* @param {object} [props]
* @param {boolean} [props.scheduleNetworkUpdate]
* @returns {NodeClient}
*/
static forTestnet(props = {}) {
return new NodeClient({
network: "testnet",
...props
});
}
/**
* Construct a Hedera client pre-configured for Previewnet access.
*
* @param {object} [props]
* @param {boolean} [props.scheduleNetworkUpdate]
* @returns {NodeClient}
*/
static forPreviewnet(props = {}) {
return new NodeClient({
network: "previewnet",
...props
});
}
/**
* Construct a Hedera client pre-configured for local-node access.
*
* @param {object} [props]
* @param {boolean} [props.scheduleNetworkUpdate]
* @returns {NodeClient}
*/
static forLocalNode(props = {
scheduleNetworkUpdate: false
}) {
return new NodeClient({
network: "local-node",
...props
});
}
/**
* @param {{[key: string]: (string | AccountId)} | string} network
* @returns {void}
*/
setNetwork(network) {
if (typeof network === "string") {
this._setNetworkFromName(network);
} else {
this._network.setNetwork(network);
}
}
/**
* Available only for NodeClient
*
* @param {number} maxExecutionTime
* @returns {this}
*/
setMaxExecutionTime(maxExecutionTime) {
this._maxExecutionTime = maxExecutionTime;
return this;
}
/**
* @private
* @param {string} name
* @returns {this}
*/
_setNetworkFromName(name) {
switch (name) {
case "mainnet":
this.setNetworkFromAddressBook(_NodeAddressBook.default.fromBytes(hex.decode(mainnet.addressBook)));
this.setMirrorNetwork(MirrorNetwork.MAINNET);
this.setLedgerId(_LedgerId.default.MAINNET);
break;
case "testnet":
this.setNetworkFromAddressBook(_NodeAddressBook.default.fromBytes(hex.decode(testnet.addressBook)));
this.setMirrorNetwork(MirrorNetwork.TESTNET);
this.setLedgerId(_LedgerId.default.TESTNET);
break;
case "previewnet":
this.setNetworkFromAddressBook(_NodeAddressBook.default.fromBytes(hex.decode(previewnet.addressBook)));
this.setMirrorNetwork(MirrorNetwork.PREVIEWNET);
this.setLedgerId(_LedgerId.default.PREVIEWNET);
break;
case "local-node":
this.setNetwork(Network.LOCAL_NODE);
this.setMirrorNetwork(MirrorNetwork.LOCAL_NODE);
this.setLedgerId(_LedgerId.default.LOCAL_NODE);
break;
default:
throw new Error(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`unknown network: ${name}`);
}
return this;
}
/**
* @param {string[] | string} mirrorNetwork
* @returns {this}
*/
setMirrorNetwork(mirrorNetwork) {
if (typeof mirrorNetwork === "string") {
switch (mirrorNetwork) {
case "local-node":
this._mirrorNetwork.setNetwork(MirrorNetwork.LOCAL_NODE);
break;
case "previewnet":
this._mirrorNetwork.setNetwork(MirrorNetwork.PREVIEWNET);
break;
case "testnet":
this._mirrorNetwork.setNetwork(MirrorNetwork.TESTNET);
break;
case "mainnet":
this._mirrorNetwork.setNetwork(MirrorNetwork.MAINNET);
break;
default:
this._mirrorNetwork.setNetwork([mirrorNetwork]);
}
} else {
this._mirrorNetwork.setNetwork(mirrorNetwork);
}
return this;
}
/**
* @override
* @returns {(address: string, cert?: string) => NodeChannel}
*/
_createNetworkChannel() {
return (address, cert) => new _NodeChannel.default(address, cert, this._maxExecutionTime);
}
/**
* @override
* @returns {(address: string) => NodeMirrorChannel}
*/
_createMirrorNetworkChannel() {
return address => new _NodeMirrorChannel.default(address);
}
}
exports.default = NodeClient;Выполнить команду
Для локальной разработки. Не используйте в интернете!