UNPKG

@hashgraph/sdk

Version:
3 lines (2 loc) 7.72 kB
import t from"../account/AccountId.js";import e from"../account/AccountBalanceQuery.js";import r from"../Hbar.js";import o from"./Network.js";import s from"./MirrorNetwork.js";import n from"../PublicKey.js";import i from"../PrivateKey.js";import a from"../LedgerId.js";import h from"../file/FileId.js";import"pino";import"../logger/LogLevel.js";import{convertToNumber as u}from"../util.js";import d from"../network/AddressBookQuery.js";class l{constructor(t){this._mirrorNetwork=new s(this._createMirrorNetworkChannel()),this._network=new o(this._createNetworkChannel()),this._operator=null,this._defaultMaxTransactionFee=null,this._defaultMaxQueryPayment=new r(1),null!=t&&null!=t.operator&&this.setOperator(t.operator.accountId,t.operator.privateKey),this._maxAttempts=null,this._signOnDemand=!1,this._autoValidateChecksums=!1,this._minBackoff=250,this._maxBackoff=8e3,this._defaultRegenerateTransactionId=!0,this._requestTimeout=null,this._isUpdatingNetwork=!1,this._networkUpdatePeriod=864e5,this._isShutdown=!1,this._shard=0,this._realm=0,null!=t&&!1!==t.scheduleNetworkUpdate&&this._scheduleNetworkUpdate(),null!=t&&null!=t.shard&&(this._shard=t.shard),null!=t&&null!=t.realm&&(this._realm=t.realm),this._timer,this._logger=null}setNetworkName(t){return console.warn("Deprecated: Use `setLedgerId` instead"),this.setLedgerId(t)}get networkName(){return console.warn("Deprecated: Use `ledgerId` instead"),null!=this.ledgerId?this.ledgerId.toString():null}setLedgerId(t){return this._network.setLedgerId("string"==typeof t?a.fromString(t):t),this}get ledgerId(){return null!=this._network._ledgerId?this._network.ledgerId:null}setNetwork(t){throw new Error("not implemented")}setNetworkFromAddressBook(t){return this._network.setNetworkFromAddressBook(t),this}get network(){return this._network.network}get shard(){return this._shard}get realm(){return this._realm}setMirrorNetwork(t){throw new Error("not implemented")}get mirrorNetwork(){return this._mirrorNetwork.network}get mirrorRestApiBaseUrl(){try{const t=this._mirrorNetwork.getNextMirrorNode(),e=t.address.address,r=t.address.port;if(!e||!r)throw new Error("Mirror node has invalid address configuration");return`${this._getSchemeFromHostAndPort(e,r)}://${e}:${r}/api/v1`}catch(t){throw new Error("Client has no mirror network configured or no healthy mirror nodes are available")}}get signOnDemand(){return this._signOnDemand}setSignOnDemand(t){this._signOnDemand=t}isTransportSecurity(){return this._network.isTransportSecurity()}setTransportSecurity(t){return this._network.setTransportSecurity(t),this}setOperator(t,e){const r="string"==typeof e?i.fromStringDer(e):e;return this.setOperatorWith(t,r.publicKey,t=>Promise.resolve(r.sign(t)))}getOperator(){return this._operator}setOperatorWith(e,r,o){const s=e instanceof t?e:t.fromString(e);return null!=this._network._ledgerId&&s.validateChecksum(this),this._operator={transactionSigner:o,accountId:s,publicKey:r instanceof n?r:n.fromString(r)},this}setAutoValidateChecksums(t){return this._autoValidateChecksums=t,this}isAutoValidateChecksumsEnabled(){return this._autoValidateChecksums}get operatorAccountId(){return null!=this._operator?this._operator.accountId:null}get operatorPublicKey(){return null!=this._operator?this._operator.publicKey:null}get defaultMaxTransactionFee(){return this._defaultMaxTransactionFee}get maxTransactionFee(){return this.defaultMaxTransactionFee}setDefaultMaxTransactionFee(t){if(t.toTinybars().toInt()<0)throw new Error("defaultMaxTransactionFee must be non-negative");return this._defaultMaxTransactionFee=t,this}setMaxTransactionFee(t){return this.setDefaultMaxTransactionFee(t)}get defaultRegenerateTransactionId(){return this._defaultRegenerateTransactionId}setDefaultRegenerateTransactionId(t){return this._defaultRegenerateTransactionId=t,this}get defaultMaxQueryPayment(){return this._defaultMaxQueryPayment}get maxQueryPayment(){return this.defaultMaxQueryPayment}setDefaultMaxQueryPayment(t){if(u(t.toTinybars())<0)throw new Error("defaultMaxQueryPayment must be non-negative");return this._defaultMaxQueryPayment=t,this}setMaxQueryPayment(t){return this.setDefaultMaxQueryPayment(t)}get maxAttempts(){return null!=this._maxAttempts?this._maxAttempts:10}setMaxAttempts(t){return this._maxAttempts=t,this}get maxNodeAttempts(){return this._network.maxNodeAttempts}setMaxNodeAttempts(t){return this._network.setMaxNodeAttempts(t),this}get nodeWaitTime(){return this._network.minBackoff}setNodeWaitTime(t){return this._network.setMinBackoff(t),this}get maxNodesPerTransaction(){return this._network.maxNodesPerTransaction}setMaxNodesPerTransaction(t){return this._network.setMaxNodesPerTransaction(t),this}setMinBackoff(t){if(null==t)throw new Error("minBackoff cannot be null.");if(t>this._maxBackoff)throw new Error("minBackoff cannot be larger than maxBackoff.");return this._minBackoff=t,this}get minBackoff(){return this._minBackoff}setMaxBackoff(t){if(null==t)throw new Error("maxBackoff cannot be null.");if(t<this._minBackoff)throw new Error("maxBackoff cannot be smaller than minBackoff.");return this._maxBackoff=t,this}get maxBackoff(){return this._maxBackoff}setNodeMinBackoff(t){return this._network.setMinBackoff(t),this}get nodeMinBackoff(){return this._network.minBackoff}setNodeMaxBackoff(t){return this._network.setMaxBackoff(t),this}get nodeMaxBackoff(){return this._network.maxBackoff}setNodeMinReadmitPeriod(t){return this._network.setNodeMinReadmitPeriod(t),this}get nodeMinReadmitPeriod(){return this._network.nodeMinReadmitPeriod}setNodeMaxReadmitPeriod(t){return this._network.setNodeMaxReadmitPeriod(t),this}get nodeMaxReadmitPeriod(){return this._network.nodeMaxReadmitPeriod}setRequestTimeout(t){return this._requestTimeout=t,this}get requestTimeout(){return this._requestTimeout}get networkUpdatePeriod(){return this._networkUpdatePeriod}setNetworkUpdatePeriod(t){return clearTimeout(this._timer),this._networkUpdatePeriod=t,this._scheduleNetworkUpdate(),this}setLogger(t){return this._logger=t,this}get logger(){return this._logger}async ping(r){await new e({accountId:r}).setNodeAccountIds([r instanceof t?r:t.fromString(r)]).execute(this)}async pingAll(){for(const t of Object.values(this._network.network))await this.ping(t)}async updateNetwork(){if(this._isUpdatingNetwork)return this;this._isUpdatingNetwork=!0;try{const t=await(new d).setFileId(h.getAddressBookFileIdFor(this._shard,this._realm)).execute(this);this.setNetworkFromAddressBook(t)}catch(t){this._logger&&this._logger.trace(`failed to update client address book: ${t.toString()}`)}finally{this._isUpdatingNetwork=!1}return this}close(){this._network.close(),this._mirrorNetwork.close(),this._isShutdown=!0,clearTimeout(this._timer)}_createNetworkChannel(){throw new Error("not implemented")}_createMirrorNetworkChannel(){throw new Error("not implemented")}_scheduleNetworkUpdate(){this._timer=setTimeout(async()=>{await this.updateNetwork(),this._isShutdown||this._scheduleNetworkUpdate()},this._networkUpdatePeriod)}_getSchemeFromHostAndPort(t,e){return"localhost"===t||"127.0.0.1"===t?"http":443===e?"https":80===e?"http":"https"}get isClientShutDown(){return this._isShutdown}static _validateNetworkConsistency(t){if(0===Object.keys(t).length)return;const[,e]=Object.entries(t)[0],r=e.toString(),[o,s]=r.split(".").map(Number),n=Object.values(t).every(t=>{const e=t.toString(),[r,n]=e.split(".").map(Number);return r===o&&n===s});if(!n)throw new Error("Network is not valid, all nodes must be in the same shard and realm")}static _extractShardRealm(t){const e=Object.entries(t);if(0===e.length)return{shard:0,realm:0};const[,r]=e[0],o=r.toString(),[s,n]=o.split(".").map(Number);return{shard:s,realm:n}}}export{l as default}; //# sourceMappingURL=Client.js.map