PHP WebShell

Текущая директория: /usr/lib/node_modules/bitgo/node_modules/@bitgo/sdk-coin-hbar/node_modules/@hashgraph/sdk/lib

Просмотр файла: Wallet.cjs

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _PrivateKey = _interopRequireDefault(require("./PrivateKey.cjs"));
var _AccountId = _interopRequireDefault(require("./account/AccountId.cjs"));
var _SignerSignature = _interopRequireDefault(require("./SignerSignature.cjs"));
var _AccountBalanceQuery = _interopRequireDefault(require("./account/AccountBalanceQuery.cjs"));
var _AccountInfoQuery = _interopRequireDefault(require("./account/AccountInfoQuery.cjs"));
var _AccountRecordsQuery = _interopRequireDefault(require("./account/AccountRecordsQuery.cjs"));
var _TransactionId = _interopRequireDefault(require("./transaction/TransactionId.cjs"));
var util = _interopRequireWildcard(require("./util.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.
 * ‍
 */

/**
 * @template RequestT
 * @template ResponseT
 * @template OutputT
 * @typedef {import("./Executable.js").default<RequestT, ResponseT, OutputT>} Executable<RequestT, ResponseT, OutputT>
 */

/**
 * @typedef {import("./Signer.js").Signer} Signer
 * @typedef {import("./Provider.js").Provider} Provider
 * @typedef {import("./LedgerId.js").default} LedgerId
 * @typedef {import("./Key.js").default} Key
 * @typedef {import("./transaction/Transaction.js").default} Transaction
 * @typedef {import("./transaction/TransactionResponse.js").default} TransactionResponse
 * @typedef {import("./transaction/TransactionReceipt.js").default} TransactionReceipt
 * @typedef {import("./transaction/TransactionRecord.js").default} TransactionRecord
 * @typedef {import("./account/AccountBalance.js").default} AccountBalance
 * @typedef {import("./account/AccountInfo.js").default} AccountInfo
 */

/**
 * @template {any} O
 * @typedef {import("./query/Query.js").default<O>} Query<O>
 */
/**
 * @implements {Signer}
 */
class Wallet {
  /**
   * @param {AccountId | string} accountId
   * @param {PrivateKey | string} privateKey
   * @param {Provider=} provider
   */
  constructor(accountId, privateKey, provider) {
    const key = typeof privateKey === "string" ? _PrivateKey.default.fromString(privateKey) : privateKey;
    this.publicKey = key.publicKey;
    /**
     * @type {(message: Uint8Array) => Promise<Uint8Array>}
     */
    this.signer = message => Promise.resolve(key.sign(message));
    this.provider = provider;
    this.accountId = typeof accountId === "string" ? _AccountId.default.fromString(accountId) : accountId;
  }

  /**
   * @returns {Promise<Wallet>}
   */
  static createRandomED25519() {
    const privateKey = _PrivateKey.default.generateED25519();
    const publicKey = privateKey.publicKey;
    const accountId = publicKey.toAccountId(0, 0);
    return Promise.resolve(new Wallet(accountId, privateKey));
  }

  /**
   * @returns {Promise<Wallet>}
   */
  static createRandomECDSA() {
    const privateKey = _PrivateKey.default.generateECDSA();
    const publicKey = privateKey.publicKey;
    const accountId = publicKey.toAccountId(0, 0);
    return Promise.resolve(new Wallet(accountId, privateKey));
  }

  /**
   * @returns {Provider=}
   */
  getProvider() {
    return this.provider;
  }

  /**
   * @abstract
   * @returns {AccountId}
   */
  getAccountId() {
    return this.accountId;
  }

  /**
   * @returns {Key}
   */
  getAccountKey() {
    return this.publicKey;
  }

  /**
   * @returns {LedgerId?}
   */
  getLedgerId() {
    return this.provider == null ? null : this.provider.getLedgerId();
  }

  /**
   * @abstract
   * @returns {{[key: string]: (string | AccountId)}}
   */
  getNetwork() {
    return this.provider == null ? {} : this.provider.getNetwork();
  }

  /**
   * @abstract
   * @returns {string[]}
   */
  getMirrorNetwork() {
    return this.provider == null ? [] : this.provider.getMirrorNetwork();
  }

  /**
   * @param {Uint8Array[]} messages
   * @returns {Promise<SignerSignature[]>}
   */
  async sign(messages) {
    const sigantures = [];
    for (const message of messages) {
      sigantures.push(new _SignerSignature.default({
        publicKey: this.publicKey,
        signature: await this.signer(message),
        accountId: this.accountId
      }));
    }
    return sigantures;
  }

  /**
   * @returns {Promise<AccountBalance>}
   */
  getAccountBalance() {
    return this.call(new _AccountBalanceQuery.default().setAccountId(this.accountId));
  }

  /**
   * @abstract
   * @returns {Promise<AccountInfo>}
   */
  getAccountInfo() {
    return this.call(new _AccountInfoQuery.default().setAccountId(this.accountId));
  }

  /**
   * @abstract
   * @returns {Promise<TransactionRecord[]>}
   */
  getAccountRecords() {
    return this.call(new _AccountRecordsQuery.default().setAccountId(this.accountId));
  }

  /**
   * @template {Transaction} T
   * @param {T} transaction
   * @returns {Promise<T>}
   */
  signTransaction(transaction) {
    return transaction.signWith(this.publicKey, this.signer);
  }

  /**
   * @template {Transaction} T
   * @param {T} transaction
   * @returns {Promise<T>}
   */
  checkTransaction(transaction) {
    const transactionId = transaction.transactionId;
    if (transactionId != null && transactionId.accountId != null && transactionId.accountId.compare(this.accountId) != 0) {
      throw new Error("transaction's ID constructed with a different account ID");
    }
    if (this.provider == null) {
      return Promise.resolve(transaction);
    }
    const nodeAccountIds = (transaction.nodeAccountIds != null ? transaction.nodeAccountIds : []).map(nodeAccountId => nodeAccountId.toString());
    const network = Object.values(this.provider.getNetwork()).map(nodeAccountId => nodeAccountId.toString());
    if (!nodeAccountIds.reduce((previous, current) => previous && network.includes(current), true)) {
      throw new Error("Transaction already set node account IDs to values not within the current network");
    }
    return Promise.resolve(transaction);
  }

  /**
   * @template {Transaction} T
   * @param {T} transaction
   * @returns {Promise<T>}
   */
  populateTransaction(transaction) {
    transaction._freezeWithAccountId(this.accountId);
    if (transaction.transactionId == null) {
      transaction.setTransactionId(_TransactionId.default.generate(this.accountId));
    }
    if (transaction.nodeAccountIds != null && transaction.nodeAccountIds.length != 0) {
      return Promise.resolve(transaction.freeze());
    }
    if (this.provider == null) {
      return Promise.resolve(transaction);
    }
    const nodeAccountIds = Object.values(this.provider.getNetwork()).map(id => typeof id === "string" ? _AccountId.default.fromString(id) : id);
    util.shuffle(nodeAccountIds);
    transaction.setNodeAccountIds(nodeAccountIds.slice(0, (nodeAccountIds.length + 3 - 1) / 3));
    return Promise.resolve(transaction.freeze());
  }

  /**
   * @template RequestT
   * @template ResponseT
   * @template OutputT
   * @param {Executable<RequestT, ResponseT, OutputT>} request
   * @returns {Promise<OutputT>}
   */
  call(request) {
    if (this.provider == null) {
      throw new Error("cannot send request with an wallet that doesn't contain a provider");
    }
    return this.provider.call(request._setOperatorWith(this.accountId, this.publicKey, this.signer));
  }
}
exports.default = Wallet;

Выполнить команду


Для локальной разработки. Не используйте в интернете!