UNPKG

wampy

Version:

Amazingly fast, feature-rich, lightweight WAMP Javascript client (for browser and node.js)

81 lines (80 loc) 3.2 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/auth/cryptosign/wampy-cryptosign.ts var wampy_cryptosign_exports = {}; __export(wampy_cryptosign_exports, { bytes2hex: () => bytes2hex, default: () => wampy_cryptosign_default, hex2bytes: () => hex2bytes, sign: () => sign }); module.exports = __toCommonJS(wampy_cryptosign_exports); var import_tweetnacl = __toESM(require("tweetnacl"), 1); var { sign: NaclSign } = import_tweetnacl.default; function hex2bytes(str) { return new Uint8Array(str.match(/../g).map((h) => Number.parseInt(h, 16))); } function bytes2hex(bytes) { return bytes ? Array.from(bytes, function(byte) { return ("0" + (byte & 255).toString(16)).slice(-2); }).join("") : null; } function sign(privateKey) { const keyPair = privateKey.length === 64 ? NaclSign.keyPair.fromSeed(hex2bytes(privateKey)) : NaclSign.keyPair.fromSecretKey(hex2bytes(privateKey)); return function(method, info) { if (method === "cryptosign") { if (!info.challenge) { throw new Error("No challenge provided!"); } const l = info.challenge.length; if (l % 2 !== 0) { throw new Error("Expected challenge to be an even number of characters!"); } const signature = NaclSign.detached(hex2bytes(info.challenge), keyPair.secretKey); return bytes2hex(signature) + info.challenge; } else { throw new Error("Unknown authentication method requested!"); } }; } var wampy_cryptosign_default = sign; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { bytes2hex, hex2bytes, sign }); /** * Wampy.js Cryptosign-based Authentication plugin * * Copyright 2022 KSDaemon. Licensed under the MIT License. * See @license text at http://www.opensource.org/licenses/mit-license.php * */ //# sourceMappingURL=wampy-cryptosign.cjs.map