PHP WebShell

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

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

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _BadKeyError = _interopRequireDefault(require("./BadKeyError.cjs"));

var _EcdsaPublicKey = _interopRequireDefault(require("./EcdsaPublicKey.cjs"));

var hex = _interopRequireWildcard(require("./encoding/hex.cjs"));

var ecdsa = _interopRequireWildcard(require("./primitive/ecdsa.cjs"));

var _array = require("./util/array.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 }; }

const derPrefix = "3030020100300706052b8104000a04220420";
const derPrefixBytes = hex.decode(derPrefix);
/**
 * @typedef {object} KeyPair
 * @property {Uint8Array} publicKey
 * @property {Uint8Array} privateKey
 */

class EcdsaPrivateKey {
  /**
   * @hideconstructor
   * @internal
   * @param {KeyPair} keyPair
   * @param {(Uint8Array)=} chainCode
   */
  constructor(keyPair, chainCode) {
    /**
     * @type {KeyPair}
     * @readonly
     * @private
     */
    this._keyPair = keyPair;
    /**
     * @type {?Uint8Array}
     * @readonly
     */

    this._chainCode = chainCode != null ? chainCode : null;
  }
  /**
   * @returns {string}
   */


  get _type() {
    return "secp256k1";
  }
  /**
   * Generate a random ECDSA private key.
   *
   * @returns {EcdsaPrivateKey}
   */


  static generate() {
    return new EcdsaPrivateKey(ecdsa.generate());
  }
  /**
   * Generate a random Ed25519 private key.
   *
   * @returns {Promise<EcdsaPrivateKey>}
   */


  static async generateAsync() {
    return new EcdsaPrivateKey(await ecdsa.generateAsync());
  }
  /**
   * Construct a private key from bytes.
   *
   * @param {Uint8Array} data
   * @returns {EcdsaPrivateKey}
   */


  static fromBytes(data) {
    switch (data.length) {
      case 32:
        return EcdsaPrivateKey.fromBytesRaw(data);

      case 50:
        return EcdsaPrivateKey.fromBytesDer(data);

      default:
        throw new _BadKeyError.default(`invalid private key length: ${data.length} bytes`);
    }
  }
  /**
   * Construct a private key from bytes.
   *
   * @param {Uint8Array} data
   * @returns {EcdsaPrivateKey}
   */


  static fromBytesDer(data) {
    if (data.length != 32 && !(0, _array.arrayStartsWith)(data, derPrefixBytes)) {
      throw new _BadKeyError.default("invalid der header");
    }

    return new EcdsaPrivateKey(ecdsa.fromBytes(data.subarray(derPrefixBytes.length)));
  }
  /**
   * Construct a private key from bytes.
   *
   * @param {Uint8Array} data
   * @returns {EcdsaPrivateKey}
   */


  static fromBytesRaw(data) {
    return new EcdsaPrivateKey(ecdsa.fromBytes(data));
  }
  /**
   * Construct a private key from a hex-encoded string.
   *
   * @param {string} text
   * @returns {EcdsaPrivateKey}
   */


  static fromString(text) {
    return EcdsaPrivateKey.fromBytes(hex.decode(text));
  }
  /**
   * Construct a private key from a hex-encoded string.
   *
   * @param {string} text
   * @returns {EcdsaPrivateKey}
   */


  static fromStringDer(text) {
    return EcdsaPrivateKey.fromBytesDer(hex.decode(text));
  }
  /**
   * Construct a private key from a hex-encoded string.
   *
   * @param {string} text
   * @returns {EcdsaPrivateKey}
   */


  static fromStringRaw(text) {
    return EcdsaPrivateKey.fromBytesRaw(hex.decode(text));
  }
  /**
   * Get the public key associated with this private key.
   *
   * The public key can be freely given and used by other parties to verify
   * the signatures generated by this private key.
   *
   * @returns {EcdsaPublicKey}
   */


  get publicKey() {
    return new _EcdsaPublicKey.default(this._keyPair.publicKey);
  }
  /**
   * Sign a message with this private key.
   *
   * @param {Uint8Array} bytes
   * @returns {Uint8Array} - The signature bytes without the message
   */


  sign(bytes) {
    return ecdsa.sign(this._keyPair.privateKey, bytes);
  }
  /**
   * @returns {Uint8Array}
   */


  toBytesDer() {
    const bytes = new Uint8Array(derPrefixBytes.length + 32);
    bytes.set(derPrefixBytes, 0);
    bytes.set(this._keyPair.privateKey.subarray(0, 32), derPrefixBytes.length);
    return bytes;
  }
  /**
   * @returns {Uint8Array}
   */


  toBytesRaw() {
    return this._keyPair.privateKey.slice(0, 32);
  }

}

exports.default = EcdsaPrivateKey;

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


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