"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.generatePreshareKey = exports.generateKeyPair = exports.default = exports.WireguardKey = void 0;
var Function = _interopRequireWildcard(require("effect/Function"));
var Schema = _interopRequireWildcard(require("effect/Schema"));
var crypto = _interopRequireWildcard(require("node:crypto"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/**
 * Wireguard key schemas and helpers
 *
 * @since 1.0.0
 */

/**
 * A wireguard key, which is a 44 character base64 string.
 *
 * @since 1.0.0
 * @category Schemas
 * @see {@link generateKeyPair}
 *
 * @see https://lists.zx2c4.com/pipermail/wireguard/2020-December/006222.html
 */
const WireguardKey = exports.WireguardKey = /*#__PURE__*/Function.pipe(Schema.String, /*#__PURE__*/Schema.pattern(/^[\d+/A-Za-z]{42}[048AEIMQUYcgkosw]=$/), /*#__PURE__*/Schema.annotations({
  identifier: "WireguardKey",
  description: "A wireguard key"
}), /*#__PURE__*/Schema.brand("WireguardKey"));
/**
 * Generates a wireguard public private key pair.
 *
 * @since 1.0.0
 * @category Crypto
 * @example
 *     import { generateKeyPair } from "the-wireguard-effect/WireguardKey";
 *     const { privateKey, publicKey } = generateKeyPair();
 */
const generateKeyPair = () => {
  const keys = crypto.generateKeyPairSync("x25519", {
    publicKeyEncoding: {
      format: "der",
      type: "spki"
    },
    privateKeyEncoding: {
      format: "der",
      type: "pkcs8"
    }
  });
  const publicKey = Schema.decodeSync(WireguardKey)(keys.publicKey.subarray(12).toString("base64"));
  const privateKey = Schema.decodeSync(WireguardKey)(keys.privateKey.subarray(16).toString("base64"));
  return {
    publicKey,
    privateKey
  };
};
/**
 * Generates a wireguard preshare key.
 *
 * @since 1.0.0
 * @category Crypto
 * @example
 *     import { generatePreshareKey } from "the-wireguard-effect/WireguardKey";
 *     const preshareKey = generatePreshareKey();
 */
exports.generateKeyPair = generateKeyPair;
const generatePreshareKey = () => {
  const key = crypto.generateKeySync("hmac", {
    length: 256
  });
  return Schema.decodeSync(WireguardKey)(key.export().toString("base64"));
};
exports.generatePreshareKey = generatePreshareKey;
var _default = exports.default = WireguardKey;
//# sourceMappingURL=WireguardKey.js.map