node-red-contrib-meshtastic-advanced 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -163,4 +163,4 @@ exports.decodeMessageByPortNum = decodeMessageByPortNum;
163
163
  exports.decodeServiceEnvelope = decodeServiceEnvelope;
164
164
  exports.ensureBuffer = ensureBuffer;
165
165
  exports.getPortNumName = getPortNumName;
166
- //# sourceMappingURL=decoder-BvBAtm2U.js.map
166
+ //# sourceMappingURL=decoder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decoder.js","sources":["../src/lib/utils/error-types.ts","../src/lib/utils/buffer-utils.ts","../src/lib/protobuf/decoder.ts"],"sourcesContent":["/**\n * Custom error types for Meshtastic operations\n */\n\nexport class MeshtasticError extends Error {\n constructor(message: string, public cause?: Error) {\n super(message);\n this.name = 'MeshtasticError';\n if (cause) {\n this.stack = `${this.stack}\\nCaused by: ${cause.stack}`;\n }\n }\n}\n\nexport class MeshtasticDecryptError extends MeshtasticError {\n constructor(message: string, cause?: Error) {\n super(message, cause);\n this.name = 'MeshtasticDecryptError';\n }\n}\n\nexport class MeshtasticDecodeError extends MeshtasticError {\n constructor(message: string, cause?: Error) {\n super(message, cause);\n this.name = 'MeshtasticDecodeError';\n }\n}\n\nexport class MeshtasticEncodeError extends MeshtasticError {\n constructor(message: string, cause?: Error) {\n super(message, cause);\n this.name = 'MeshtasticEncodeError';\n }\n}\n\nexport class MeshtasticEncryptError extends MeshtasticError {\n constructor(message: string, cause?: Error) {\n super(message, cause);\n this.name = 'MeshtasticEncryptError';\n }\n}\n\nexport class InvalidKeyError extends MeshtasticError {\n constructor(message: string) {\n super(message);\n this.name = 'InvalidKeyError';\n }\n}\n\nexport class InvalidNonceError extends MeshtasticError {\n constructor(message: string) {\n super(message);\n this.name = 'InvalidNonceError';\n }\n}\n\nexport class ProtobufError extends MeshtasticError {\n constructor(message: string, cause?: Error) {\n super(message, cause);\n this.name = 'ProtobufError';\n }\n}\n","/**\n * Buffer manipulation utilities\n */\n\n/**\n * Convert a Uint8Array to Buffer\n * @param uint8Array - Uint8Array to convert\n * @returns Buffer\n */\nexport function uint8ArrayToBuffer(uint8Array: Uint8Array): Buffer {\n return Buffer.from(uint8Array);\n}\n\n/**\n * Convert a Buffer to Uint8Array\n * @param buffer - Buffer to convert\n * @returns Uint8Array\n */\nexport function bufferToUint8Array(buffer: Buffer): Uint8Array {\n return new Uint8Array(buffer);\n}\n\n/**\n * Check if input is a Buffer or Uint8Array\n * @param input - Input to check\n * @returns true if input is Buffer or Uint8Array\n */\nexport function isBufferLike(input: any): input is Buffer | Uint8Array {\n return Buffer.isBuffer(input) || input instanceof Uint8Array;\n}\n\n/**\n * Ensure input is a Buffer (convert if needed)\n * @param input - Buffer or Uint8Array\n * @returns Buffer\n */\nexport function ensureBuffer(input: Buffer | Uint8Array): Buffer {\n if (Buffer.isBuffer(input)) {\n return input;\n }\n return Buffer.from(input);\n}\n\n/**\n * Ensure input is a Uint8Array (convert if needed)\n * @param input - Buffer or Uint8Array\n * @returns Uint8Array\n */\nexport function ensureUint8Array(input: Buffer | Uint8Array): Uint8Array {\n if (input instanceof Uint8Array && !(input instanceof Buffer)) {\n return input;\n }\n return new Uint8Array(input);\n}\n\n/**\n * Convert a hex string to Buffer\n * @param hex - Hex string\n * @returns Buffer\n */\nexport function hexToBuffer(hex: string): Buffer {\n return Buffer.from(hex, 'hex');\n}\n\n/**\n * Convert a base64 string to Buffer\n * @param base64 - Base64 string\n * @returns Buffer\n */\nexport function base64ToBuffer(base64: string): Buffer {\n return Buffer.from(base64, 'base64');\n}\n\n/**\n * Convert a Buffer to hex string\n * @param buffer - Buffer\n * @returns Hex string\n */\nexport function bufferToHex(buffer: Buffer): string {\n return buffer.toString('hex');\n}\n\n/**\n * Convert a Buffer to base64 string\n * @param buffer - Buffer\n * @returns Base64 string\n */\nexport function bufferToBase64(buffer: Buffer): string {\n return buffer.toString('base64');\n}\n\n/**\n * Check if two buffers are equal\n * @param a - First buffer\n * @param b - Second buffer\n * @returns true if buffers are equal\n */\nexport function buffersEqual(a: Buffer, b: Buffer): boolean {\n return a.equals(b);\n}\n","/**\n * Protobuf decoding utilities for Meshtastic messages\n */\n\nimport { Protobuf } from '@meshtastic/protobufs';\nimport { ProtobufError, MeshtasticDecodeError } from '../utils/error-types';\nimport { ensureUint8Array } from '../utils/buffer-utils';\n\n/**\n * Decode a ServiceEnvelope from binary data\n * @param buffer - Binary data containing ServiceEnvelope\n * @returns Decoded ServiceEnvelope\n * @throws {ProtobufError} If decoding fails\n */\nexport function decodeServiceEnvelope(buffer: Buffer | Uint8Array): Protobuf.Mqtt.ServiceEnvelope {\n try {\n const uint8 = ensureUint8Array(buffer);\n return Protobuf.Mqtt.ServiceEnvelope.fromBinary(uint8);\n } catch (error) {\n throw new ProtobufError(\n `Failed to decode ServiceEnvelope: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n\n/**\n * Decode a Data protobuf from binary data\n * @param buffer - Binary data containing Data message\n * @returns Decoded Data\n * @throws {ProtobufError} If decoding fails\n */\nexport function decodeData(buffer: Buffer | Uint8Array): Protobuf.Mesh.Data {\n try {\n const uint8 = ensureUint8Array(buffer);\n return Protobuf.Mesh.Data.fromBinary(uint8);\n } catch (error) {\n throw new ProtobufError(\n `Failed to decode Data: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n\n/**\n * Decode a message payload based on its port number\n * @param portNum - Port number (message type)\n * @param payload - Binary payload data\n * @returns Decoded message (type depends on portNum)\n * @throws {MeshtasticDecodeError} If decoding fails\n */\nexport function decodeMessageByPortNum(\n portNum: Protobuf.Portnums.PortNum | number,\n payload: Uint8Array | Buffer\n): any {\n try {\n const uint8 = ensureUint8Array(payload);\n\n switch (portNum) {\n case Protobuf.Portnums.PortNum.TEXT_MESSAGE_APP:\n return new TextDecoder().decode(uint8);\n\n case Protobuf.Portnums.PortNum.POSITION_APP:\n return Protobuf.Mesh.Position.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.NODEINFO_APP:\n return Protobuf.Mesh.User.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.TELEMETRY_APP:\n return Protobuf.Telemetry.Telemetry.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.ROUTING_APP:\n return Protobuf.Mesh.Routing.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.ADMIN_APP:\n return Protobuf.Admin.AdminMessage.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.WAYPOINT_APP:\n return Protobuf.Mesh.Waypoint.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.NEIGHBORINFO_APP:\n return Protobuf.Mesh.NeighborInfo.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.STORE_FORWARD_APP:\n return Protobuf.StoreForward.StoreAndForward.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.RANGE_TEST_APP:\n return new TextDecoder('ascii').decode(uint8);\n\n case Protobuf.Portnums.PortNum.REMOTE_HARDWARE_APP:\n return Protobuf.RemoteHardware.HardwareMessage.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.DETECTION_SENSOR_APP:\n return new TextDecoder().decode(uint8);\n\n case Protobuf.Portnums.PortNum.REPLY_APP:\n return new TextDecoder('ascii').decode(uint8);\n\n case Protobuf.Portnums.PortNum.TRACEROUTE_APP:\n return Protobuf.Mesh.RouteDiscovery.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.PAXCOUNTER_APP:\n return Protobuf.Paxcount.Paxcount.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.MAP_REPORT_APP:\n return Protobuf.Mqtt.MapReport.fromBinary(uint8);\n\n // For unknown or unsupported message types, return raw bytes\n case Protobuf.Portnums.PortNum.UNKNOWN_APP:\n case Protobuf.Portnums.PortNum.IP_TUNNEL_APP:\n case Protobuf.Portnums.PortNum.SERIAL_APP:\n case Protobuf.Portnums.PortNum.AUDIO_APP:\n case Protobuf.Portnums.PortNum.ZPS_APP:\n case Protobuf.Portnums.PortNum.SIMULATOR_APP:\n case Protobuf.Portnums.PortNum.PRIVATE_APP:\n case Protobuf.Portnums.PortNum.ATAK_FORWARDER:\n case Protobuf.Portnums.PortNum.TEXT_MESSAGE_COMPRESSED_APP:\n default:\n return {\n raw: Buffer.from(uint8),\n portNum,\n note: `Unsupported or binary message type: ${getPortNumName(portNum)}`\n };\n }\n } catch (error) {\n throw new MeshtasticDecodeError(\n `Failed to decode message for portNum ${portNum}: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n\n/**\n * Get the name of a port number\n * @param portNum - Port number\n * @returns Port number name (e.g., \"TEXT_MESSAGE_APP\")\n */\nexport function getPortNumName(portNum: Protobuf.Portnums.PortNum | number): string {\n const name = Protobuf.Portnums.PortNum[portNum];\n return name || `UNKNOWN(${portNum})`;\n}\n\n/**\n * Format a ServiceEnvelope as a JSON object for output\n * @param envelope - ServiceEnvelope to format\n * @param decodePayload - Whether to decode the payload (default: true)\n * @returns Formatted JSON object\n */\nexport function formatServiceEnvelopeAsJson(\n envelope: Protobuf.Mqtt.ServiceEnvelope,\n decodePayload: boolean = true\n): any {\n const packet = envelope.packet;\n\n if (!packet) {\n return {\n channelId: envelope.channelId,\n gatewayId: envelope.gatewayId,\n error: 'No packet in ServiceEnvelope'\n };\n }\n\n const result: any = {\n channelId: envelope.channelId,\n gatewayId: envelope.gatewayId,\n from: packet.from,\n to: packet.to,\n channel: packet.channel,\n packetId: packet.id,\n rxTime: packet.rxTime,\n rxSnr: packet.rxSnr,\n rxRssi: packet.rxRssi,\n hopLimit: packet.hopLimit,\n wantAck: packet.wantAck,\n priority: packet.priority,\n hopStart: packet.hopStart,\n };\n\n // Handle payload based on variant type\n if (packet.payloadVariant.case === 'encrypted') {\n result.encrypted = true;\n result.encryptedData = Buffer.from(packet.payloadVariant.value).toString('base64');\n } else if (packet.payloadVariant.case === 'decoded') {\n const decoded = packet.payloadVariant.value;\n result.encrypted = false;\n result.portNum = getPortNumName(decoded.portnum);\n result.portNumValue = decoded.portnum;\n result.wantResponse = decoded.wantResponse;\n result.requestId = decoded.requestId;\n result.emoji = decoded.emoji;\n\n if (decodePayload && decoded.payload && decoded.payload.length > 0) {\n try {\n result.payload = decodeMessageByPortNum(decoded.portnum, decoded.payload);\n } catch (error) {\n result.payload = {\n raw: Buffer.from(decoded.payload).toString('base64'),\n error: error instanceof Error ? error.message : String(error)\n };\n }\n } else {\n result.payload = decoded.payload ? Buffer.from(decoded.payload).toString('base64') : null;\n }\n }\n\n return result;\n}\n"],"names":["Protobuf"],"mappings":";;AAIO,MAAM,wBAAwB,MAAM;AAAA,EACzC,YAAY,SAAwB,OAAe;AACjD,UAAM,OAAO;AADqB,SAAA,QAAA;AAElC,SAAK,OAAO;AACZ,QAAI,OAAO;AACT,WAAK,QAAQ,GAAG,KAAK,KAAK;AAAA,aAAgB,MAAM,KAAK;AAAA,IACvD;AAAA,EACF;AACF;AAEO,MAAM,+BAA+B,gBAAgB;AAAA,EAC1D,YAAY,SAAiB,OAAe;AAC1C,UAAM,SAAS,KAAK;AACpB,SAAK,OAAO;AAAA,EACd;AACF;AAEO,MAAM,8BAA8B,gBAAgB;AAAA,EACzD,YAAY,SAAiB,OAAe;AAC1C,UAAM,SAAS,KAAK;AACpB,SAAK,OAAO;AAAA,EACd;AACF;AAEO,MAAM,8BAA8B,gBAAgB;AAAA,EACzD,YAAY,SAAiB,OAAe;AAC1C,UAAM,SAAS,KAAK;AACpB,SAAK,OAAO;AAAA,EACd;AACF;AAEO,MAAM,+BAA+B,gBAAgB;AAAA,EAC1D,YAAY,SAAiB,OAAe;AAC1C,UAAM,SAAS,KAAK;AACpB,SAAK,OAAO;AAAA,EACd;AACF;AAEO,MAAM,wBAAwB,gBAAgB;AAAA,EACnD,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;AAEO,MA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oder = require("./decoder.js");
4
4
  function encodeMessageByPortNum(portNum, message) {
5
5
  try {
6
6
  switch (portNum) {
@@ -123,4 +123,4 @@ exports.encodeData = encodeData;
123
123
  exports.encodeMessageByPortNum = encodeMessageByPortNum;
124
124
  exports.encodeServiceEnvelope = encodeServiceEnvelope;
125
125
  exports.parsePortNum = parsePortNum;
126
- //# sourceMappingURL=encoder-DU3wTIEA.js.map
126
+ //# sourceMappingURL=encoder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encoder.js","sources":["../src/lib/protobuf/encoder.ts"],"sourcesContent":["/**\n * Protobuf encoding utilities for Meshtastic messages\n */\n\nimport { Protobuf } from '@meshtastic/protobufs';\nimport { MeshtasticEncodeError, ProtobufError } from '../utils/error-types';\nimport { ensureBuffer } from '../utils/buffer-utils';\n\n/**\n * Encode a message payload based on its port number\n * @param portNum - Port number (message type)\n * @param message - Message data to encode\n * @returns Encoded binary payload\n * @throws {MeshtasticEncodeError} If encoding fails\n */\nexport function encodeMessageByPortNum(\n portNum: Protobuf.Portnums.PortNum | number,\n message: any\n): Uint8Array {\n try {\n switch (portNum) {\n case Protobuf.Portnums.PortNum.TEXT_MESSAGE_APP:\n // Encode string as UTF-8 bytes\n if (typeof message !== 'string') {\n throw new Error('TEXT_MESSAGE_APP requires a string message');\n }\n return new TextEncoder().encode(message);\n\n case Protobuf.Portnums.PortNum.POSITION_APP:\n return Protobuf.Mesh.Position.toBinary(message as Protobuf.Mesh.Position);\n\n case Protobuf.Portnums.PortNum.NODEINFO_APP:\n return Protobuf.Mesh.User.toBinary(message as Protobuf.Mesh.User);\n\n case Protobuf.Portnums.PortNum.TELEMETRY_APP:\n return Protobuf.Telemetry.Telemetry.toBinary(message as Protobuf.Telemetry.Telemetry);\n\n case Protobuf.Portnums.PortNum.ROUTING_APP:\n return Protobuf.Mesh.Routing.toBinary(message as Protobuf.Mesh.Routing);\n\n case Protobuf.Portnums.PortNum.ADMIN_APP:\n return Protobuf.Admin.AdminMessage.toBinary(message as Protobuf.Admin.AdminMessage);\n\n case Protobuf.Portnums.PortNum.WAYPOINT_APP:\n return Protobuf.Mesh.Waypoint.toBinary(message as Protobuf.Mesh.Waypoint);\n\n case Protobuf.Portnums.PortNum.NEIGHBORINFO_APP:\n return Protobuf.Mesh.NeighborInfo.toBinary(message as Protobuf.Mesh.NeighborInfo);\n\n case Protobuf.Portnums.PortNum.STORE_FORWARD_APP:\n return Protobuf.StoreForward.StoreAndForward.toBinary(message as Protobuf.StoreForward.StoreAndForward);\n\n case Protobuf.Portnums.PortNum.RANGE_TEST_APP:\n case Protobuf.Portnums.PortNum.DETECTION_SENSOR_APP:\n case Protobuf.Portnums.PortNum.REPLY_APP:\n // Encode string as ASCII/UTF-8 bytes\n if (typeof message !== 'string') {\n throw new Error(`${portNum} requires a string message`);\n }\n return new TextEncoder().encode(message);\n\n case Protobuf.Portnums.PortNum.REMOTE_HARDWARE_APP:\n return Protobuf.RemoteHardware.HardwareMessage.toBinary(message as Protobuf.RemoteHardware.HardwareMessage);\n\n case Protobuf.Portnums.PortNum.TRACEROUTE_APP:\n return Protobuf.Mesh.RouteDiscovery.toBinary(message as Protobuf.Mesh.RouteDiscovery);\n\n case Protobuf.Portnums.PortNum.PAXCOUNTER_APP:\n return Protobuf.Paxcount.Paxcount.toBinary(message as Protobuf.Paxcount.Paxcount);\n\n case Protobuf.Portnums.PortNum.MAP_REPORT_APP:\n return Protobuf.Mqtt.MapReport.toBinary(message as Protobuf.Mqtt.MapReport);\n\n default:\n throw new Error(`Encoding not supported for portNum: ${portNum}`);\n }\n } catch (error) {\n throw new MeshtasticEncodeError(\n `Failed to encode message for portNum ${portNum}: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n\n/**\n * Create a Data protobuf from encoded payload\n * @param portNum - Port number (message type)\n * @param payload - Encoded message payload\n * @param options - Additional options (wantResponse, requestId, emoji)\n * @returns Data protobuf\n */\nexport function createDataPayload(\n portNum: Protobuf.Portnums.PortNum | number,\n payload: Uint8Array,\n options?: {\n wantResponse?: boolean;\n requestId?: number;\n emoji?: number;\n }\n): Protobuf.Mesh.Data {\n return {\n portnum: portNum as Protobuf.Portnums.PortNum,\n payload,\n wantResponse: options?.wantResponse ?? false,\n requestId: options?.requestId ?? 0,\n emoji: options?.emoji ?? 0,\n };\n}\n\n/**\n * Create a ServiceEnvelope with a Data payload\n * @param data - Data protobuf\n * @param from - Source node number\n * @param to - Destination node number (0xFFFFFFFF for broadcast)\n * @param channel - Channel index\n * @param packetId - Packet ID (if undefined, needs to be set later)\n * @param options - Additional options\n * @returns ServiceEnvelope\n */\nexport function createServiceEnvelope(\n data: Protobuf.Mesh.Data,\n from: number,\n to: number = 0xFFFFFFFF,\n channel: number = 0,\n packetId?: number,\n options?: {\n wantAck?: boolean;\n hopLimit?: number;\n priority?: Protobuf.Mesh.MeshPacket_Priority;\n channelId?: string;\n gatewayId?: string;\n }\n): Protobuf.Mqtt.ServiceEnvelope {\n return {\n packet: {\n from,\n to,\n channel,\n id: packetId ?? 0,\n rxTime: 0,\n rxSnr: 0,\n rxRssi: 0,\n hopLimit: options?.hopLimit ?? 3,\n wantAck: options?.wantAck ?? false,\n priority: options?.priority ?? Protobuf.Mesh.MeshPacket_Priority.UNSET,\n hopStart: options?.hopLimit ?? 3,\n payloadVariant: {\n case: 'decoded',\n value: data,\n },\n },\n channelId: options?.channelId ?? '',\n gatewayId: options?.gatewayId ?? '',\n };\n}\n\n/**\n * Encode a complete ServiceEnvelope to binary\n * @param envelope - ServiceEnvelope to encode\n * @returns Binary encoded ServiceEnvelope\n * @throws {ProtobufError} If encoding fails\n */\nexport function encodeServiceEnvelope(envelope: Protobuf.Mqtt.ServiceEnvelope): Buffer {\n try {\n const binary = Protobuf.Mqtt.ServiceEnvelope.toBinary(envelope);\n return ensureBuffer(binary);\n } catch (error) {\n throw new ProtobufError(\n `Failed to encode ServiceEnvelope: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n\n/**\n * Encode a Data protobuf to binary\n * @param data - Data protobuf to encode\n * @returns Binary encoded Data\n * @throws {ProtobufError} If encoding fails\n */\nexport function encodeData(data: Protobuf.Mesh.Data): Buffer {\n try {\n const binary = Protobuf.Mesh.Data.toBinary(data);\n return ensureBuffer(binary);\n } catch (error) {\n throw new ProtobufError(\n `Failed to encode Data: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n\n/**\n * Parse port number from string name or number\n * @param portNum - Port number as string name or number\n * @returns Port number as enum value\n * @throws {MeshtasticEncodeError} If port number is invalid\n */\nexport function parsePortNum(portNum: string | number): Protobuf.Portnums.PortNum {\n if (typeof portNum === 'number') {\n // Validate that the number is a valid PortNum\n if (portNum in Protobuf.Portnums.PortNum) {\n return portNum as Protobuf.Portnums.PortNum;\n }\n throw new MeshtasticEncodeError(`Invalid port number: ${portNum}`);\n }\n\n // Parse string name to enum value\n const enumValue = (Protobuf.Portnums.PortNum as any)[portNum];\n if (enumValue === undefined) {\n throw new MeshtasticEncodeError(`Unknown port number name: ${portNum}`);\n }\n\n return enumValue;\n}\n"],"names":["Protobuf","MeshtasticEncodeError","ensureBuffer","ProtobufError"],"mappings":";;;AAeO,SAAS,uBACd,SACA,SACY;AACZ,MAAI;AACF,YAAQ,SAAA;AAAA,MACN,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAE7B,YAAI,OAAO,YAAY,UAAU;AAC/B,gBAAM,IAAI,MAAM,4CAA4C;AAAA,QAC9D;AACA,eAAO,IAAI,YAAA,EAAc,OAAO,OAAO;AAAA,MAEzC,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,eAAOA,UAAAA,SAAS,KAAK,SAAS,SAAS,OAAiC;AAAA,MAE1E,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,eAAOA,UAAAA,SAAS,KAAK,KAAK,SAAS,OAA6B;AAAA,MAElE,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC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,QAAQ;AAAA,IAAA;AAAA,EAErC;AACF;AAQO,SAAS,WAAW,MAAkC;AAC3D,MAAI;AACF,UAAM,SAASH,UAAAA,SAAS,KAAK,KAAK,SAAS,IAAI;AAC/C,WAAOE,QAAAA,aAAa,MAAM;AAAA,EAC5B,SAAS,OAAO;AACd,UAAM,IAAIC,QAAAA;AAAAA,MACR,0BAA0B,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CAAC;AAAA,MAChF,iBAAiB,QAAQ,QAAQ;AAAA,IAAA;AAAA,EAErC;AACF;AAQO,SAAS,aAAa,SAAqD;AAChF,MAAI,OAAO,YAAY,UAAU;AAE/B,QAAI,WAAWH,UAAAA,SAAS,SAAS,SAAS;AACxC,aAAO;AAAA,IACT;AACA,UAAM,IAAIC,QAAAA,sBAAsB,wBAAwB,OAAO,EAAE;AAAA,EACnE;AAGA,QAAM,YAAaD,UAAAA,SAAS,SAAS,QAAgB,OAAO;AAC5D,MAAI,cAAc,QAAW;AAC3B,UAAM,IAAIC,QAAAA,sBAAsB,6BAA6B,OAAO,EAAE;AAAA,EACxE;AAEA,SAAO;AACT;;;;;;;"}
@@ -88,4 +88,4 @@ const BROADCAST_ADDR = 4294967295;
88
88
  exports.BROADCAST_ADDR = BROADCAST_ADDR;
89
89
  exports.getMessageCategory = getMessageCategory;
90
90
  exports.getPortNumDescription = getPortNumDescription;
91
- //# sourceMappingURL=message-types-FA26fPjF.js.map
91
+ //# sourceMappingURL=message-types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"message-types.js","sources":["../src/lib/protobuf/message-types.ts"],"sourcesContent":["/**\n * Meshtastic message type definitions and utilities\n */\n\nimport { Protobuf } from '@meshtastic/protobufs';\n\n/**\n * Common message type categories\n */\nexport enum MessageCategory {\n TEXT = 'text',\n POSITION = 'position',\n NODEINFO = 'nodeinfo',\n TELEMETRY = 'telemetry',\n ROUTING = 'routing',\n ADMIN = 'admin',\n HARDWARE = 'hardware',\n OTHER = 'other',\n}\n\n/**\n * Get the category for a port number\n * @param portNum - Port number\n * @returns Message category\n */\nexport function getMessageCategory(portNum: Protobuf.Portnums.PortNum): MessageCategory {\n switch (portNum) {\n case Protobuf.Portnums.PortNum.TEXT_MESSAGE_APP:\n case Protobuf.Portnums.PortNum.TEXT_MESSAGE_COMPRESSED_APP:\n case Protobuf.Portnums.PortNum.REPLY_APP:\n return MessageCategory.TEXT;\n\n case Protobuf.Portnums.PortNum.POSITION_APP:\n case Protobuf.Portnums.PortNum.WAYPOINT_APP:\n case Protobuf.Portnums.PortNum.MAP_REPORT_APP:\n return MessageCategory.POSITION;\n\n case Protobuf.Portnums.PortNum.NODEINFO_APP:\n case Protobuf.Portnums.PortNum.NEIGHBORINFO_APP:\n return MessageCategory.NODEINFO;\n\n case Protobuf.Portnums.PortNum.TELEMETRY_APP:\n case Protobuf.Portnums.PortNum.PAXCOUNTER_APP:\n case Protobuf.Portnums.PortNum.DETECTION_SENSOR_APP:\n return MessageCategory.TELEMETRY;\n\n case Protobuf.Portnums.PortNum.ROUTING_APP:\n case Protobuf.Portnums.PortNum.TRACEROUTE_APP:\n return MessageCategory.ROUTING;\n\n case Protobuf.Portnums.PortNum.ADMIN_APP:\n return MessageCategory.ADMIN;\n\n case Protobuf.Portnums.PortNum.REMOTE_HARDWARE_APP:\n return MessageCategory.HARDWARE;\n\n default:\n return MessageCategory.OTHER;\n }\n}\n\n/**\n * Check if a port number represents a text message\n * @param portNum - Port number\n * @returns true if text message\n */\nexport function isTextMessage(portNum: Protobuf.Portnums.PortNum): boolean {\n return getMessageCategory(portNum) === MessageCategory.TEXT;\n}\n\n/**\n * Check if a port number represents a position message\n * @param portNum - Port number\n * @returns true if position message\n */\nexport function isPositionMessage(portNum: Protobuf.Portnums.PortNum): boolean {\n return getMessageCategory(portNum) === MessageCategory.POSITION;\n}\n\n/**\n * Get a human-readable description for a port number\n * @param portNum - Port number\n * @returns Description\n */\nexport function getPortNumDescription(portNum: Protobuf.Portnums.PortNum): string {\n switch (portNum) {\n case Protobuf.Portnums.PortNum.UNKNOWN_APP:\n return 'Unknown message type';\n case Protobuf.Portnums.PortNum.TEXT_MESSAGE_APP:\n return 'Text message';\n case Protobuf.Portnums.PortNum.REMOTE_HARDWARE_APP:\n return 'Remote hardware control';\n case Protobuf.Portnums.PortNum.POSITION_APP:\n return 'GPS position';\n case Protobuf.Portnums.PortNum.NODEINFO_APP:\n return 'Node information';\n case Protobuf.Portnums.PortNum.ROUTING_APP:\n return 'Routing information';\n case Protobuf.Portnums.PortNum.ADMIN_APP:\n return 'Admin command';\n case Protobuf.Portnums.PortNum.TEXT_MESSAGE_COMPRESSED_APP:\n return 'Compressed text message';\n case Protobuf.Portnums.PortNum.WAYPOINT_APP:\n return 'Waypoint';\n case Protobuf.Portnums.PortNum.AUDIO_APP:\n return 'Audio message';\n case Protobuf.Portnums.PortNum.DETECTION_SENSOR_APP:\n return 'Detection sensor data';\n case Protobuf.Portnums.PortNum.REPLY_APP:\n return 'Reply message';\n case Protobuf.Portnums.PortNum.IP_TUNNEL_APP:\n return 'IP tunnel';\n case Protobuf.Portnums.PortNum.SERIAL_APP:\n return 'Serial data';\n case Protobuf.Portnums.PortNum.STORE_FORWARD_APP:\n return 'Store and forward';\n case Protobuf.Portnums.PortNum.RANGE_TEST_APP:\n return 'Range test';\n case Protobuf.Portnums.PortNum.TELEMETRY_APP:\n return 'Telemetry data';\n case Protobuf.Portnums.PortNum.ZPS_APP:\n return 'ZPS application';\n case Protobuf.Portnums.PortNum.SIMULATOR_APP:\n return 'Simulator';\n case Protobuf.Portnums.PortNum.TRACEROUTE_APP:\n return 'Traceroute';\n case Protobuf.Portnums.PortNum.NEIGHBORINFO_APP:\n return 'Neighbor information';\n case Protobuf.Portnums.PortNum.ATAK_FORWARDER:\n return 'ATAK forwarder';\n case Protobuf.Portnums.PortNum.PAXCOUNTER_APP:\n return 'People counter';\n case Protobuf.Portnums.PortNum.MAP_REPORT_APP:\n return 'Map report';\n case Protobuf.Portnums.PortNum.PRIVATE_APP:\n return 'Private application';\n default:\n return `Unknown (${portNum})`;\n }\n}\n\n/**\n * Broadcast address constant\n */\nexport const BROADCAST_ADDR = 0xFFFFFFFF;\n\n/**\n * Check if an address is broadcast\n * @param addr - Address to check\n * @returns true if broadcast address\n */\nexport function isBroadcastAddr(addr: number): boolean {\n return addr === BROADCAST_ADDR;\n}\n"],"names":["Protobuf"],"mappings":";;AAyBO,SAAS,mBAAmB,SAAqD;AACtF,UAAQ,SAAA;AAAA,IACN,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAAA,IAC/B,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAAA,IAC/B,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IAET,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAAA,IAC/B,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAAA,IAC/B,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IAET,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAAA,IAC/B,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IAET,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAAA,IAC/B,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAAA,IAC/B,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IAET,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAAA,IAC/B,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IAET,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IAET,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IAET;AACE,aAAO;AAAA,EAAA;AAEb;AAyBO,SAAS,sBAAsB,SAA4C;AAChF,UAAQ,SAAA;AAAA,IACN,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IACT,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,SAAS,QAAQ;AAC7B,aAAO;AAAA,IACT,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IACT,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IACT;AACE,aAAO,YAAY,OAAO;AAAA,EAAA;AAEhC;AAKO,MAAM,iBAAiB;;;;"}
@@ -1,7 +1,7 @@
1
1
  <script type="text/javascript">
2
2
  RED.nodes.registerType('meshtastic-decode', {
3
3
  category: 'meshtastic',
4
- color: '#3FADD7',
4
+ color: '#67EA94',
5
5
  defaults: {
6
6
  name: { value: '' },
7
7
  includeRaw: { value: false },
@@ -9,7 +9,7 @@
9
9
  },
10
10
  inputs: 1,
11
11
  outputs: 1,
12
- icon: 'parser-json.svg',
12
+ icon: 'meshtastic.svg',
13
13
  label: function() {
14
14
  return this.name || 'meshtastic-decode';
15
15
  },
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  require("crypto");
3
- const decoder = require("../../decoder-BvBAtm2U.js");
3
+ const decoder = require("../../decoder.js");
4
4
  require("@meshtastic/protobufs");
5
- const messageTypes = require("../../message-types-FA26fPjF.js");
5
+ const messageTypes = require("../../message-types.js");
6
6
  const nodeInit = (RED) => {
7
7
  function DecodeNodeConstructor(config) {
8
8
  RED.nodes.createNode(this, config);
@@ -1,7 +1,7 @@
1
1
  <script type="text/javascript">
2
2
  RED.nodes.registerType('meshtastic-decrypt', {
3
3
  category: 'meshtastic',
4
- color: '#3FADD7',
4
+ color: '#67EA94',
5
5
  defaults: {
6
6
  name: { value: '' },
7
7
  mode: { value: 'auto' },
@@ -15,7 +15,7 @@
15
15
  },
16
16
  inputs: 1,
17
17
  outputs: 1,
18
- icon: 'lock.svg',
18
+ icon: 'meshtastic.svg',
19
19
  label: function() {
20
20
  return this.name || 'meshtastic-decrypt';
21
21
  },
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  const protobufs = require("@meshtastic/protobufs");
3
- const x25519 = require("../../x25519-D0dlDGB0.js");
4
- const decoder = require("../../decoder-BvBAtm2U.js");
3
+ const x25519 = require("../../x25519.js");
4
+ const decoder = require("../../decoder.js");
5
5
  require("crypto");
6
- const validation = require("../../validation-BegQyBSh.js");
6
+ const validation = require("../../validation.js");
7
7
  const nodeInit = (RED) => {
8
8
  function DecryptNodeConstructor(config) {
9
9
  RED.nodes.createNode(this, config);
@@ -1,7 +1,7 @@
1
1
  <script type="text/javascript">
2
2
  RED.nodes.registerType('meshtastic-encode', {
3
3
  category: 'meshtastic',
4
- color: '#3FADD7',
4
+ color: '#67EA94',
5
5
  defaults: {
6
6
  name: { value: '' },
7
7
  defaultTo: { value: 4294967295 }, // BROADCAST_ADDR
@@ -12,7 +12,7 @@
12
12
  },
13
13
  inputs: 1,
14
14
  outputs: 1,
15
- icon: 'parser-json.svg',
15
+ icon: 'meshtastic.svg',
16
16
  label: function() {
17
17
  return this.name || 'meshtastic-encode';
18
18
  },
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  require("crypto");
3
- const decoder = require("../../decoder-BvBAtm2U.js");
4
- const encoder = require("../../encoder-DU3wTIEA.js");
5
- const messageTypes = require("../../message-types-FA26fPjF.js");
6
- const validation = require("../../validation-BegQyBSh.js");
3
+ const decoder = require("../../decoder.js");
4
+ const encoder = require("../../encoder.js");
5
+ const messageTypes = require("../../message-types.js");
6
+ const validation = require("../../validation.js");
7
7
  const nodeInit = (RED) => {
8
8
  function EncodeNodeConstructor(config) {
9
9
  RED.nodes.createNode(this, config);
@@ -1,7 +1,7 @@
1
1
  <script type="text/javascript">
2
2
  RED.nodes.registerType('meshtastic-encrypt', {
3
3
  category: 'meshtastic',
4
- color: '#3FADD7',
4
+ color: '#67EA94',
5
5
  defaults: {
6
6
  name: { value: '' },
7
7
  mode: { value: 'channel' },
@@ -17,7 +17,7 @@
17
17
  },
18
18
  inputs: 1,
19
19
  outputs: 1,
20
- icon: 'lock.svg',
20
+ icon: 'meshtastic.svg',
21
21
  label: function() {
22
22
  return this.name || 'meshtastic-encrypt';
23
23
  },
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  const protobufs = require("@meshtastic/protobufs");
3
3
  const crypto = require("crypto");
4
- const x25519 = require("../../x25519-D0dlDGB0.js");
5
- const decoder = require("../../decoder-BvBAtm2U.js");
6
- const encoder = require("../../encoder-DU3wTIEA.js");
7
- const validation = require("../../validation-BegQyBSh.js");
4
+ const x25519 = require("../../x25519.js");
5
+ const decoder = require("../../decoder.js");
6
+ const encoder = require("../../encoder.js");
7
+ const validation = require("../../validation.js");
8
8
  const nodeInit = (RED) => {
9
9
  function EncryptNodeConstructor(config) {
10
10
  RED.nodes.createNode(this, config);
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- const decoder = require("./decoder-BvBAtm2U.js");
2
+ const decoder = require("./decoder.js");
3
3
  function validateNodeNumber(value, fieldName = "nodeNum") {
4
4
  if (typeof value !== "number" || !Number.isInteger(value)) {
5
5
  throw new decoder.MeshtasticError(`${fieldName} must be an integer, got ${typeof value}`);
@@ -31,4 +31,4 @@ exports.validateChannelIndex = validateChannelIndex;
31
31
  exports.validateHopLimit = validateHopLimit;
32
32
  exports.validateNodeNumber = validateNodeNumber;
33
33
  exports.validatePacketId = validatePacketId;
34
- //# sourceMappingURL=validation-BegQyBSh.js.map
34
+ //# sourceMappingURL=validation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.js","sources":["../src/lib/utils/validation.ts"],"sourcesContent":["/**\n * Input validation utilities\n */\n\nimport { MeshtasticError } from './error-types';\n\n/**\n * Validate that a value is a valid node number (uint32)\n * @param value - Value to validate\n * @param fieldName - Name of the field (for error messages)\n * @throws {MeshtasticError} If validation fails\n */\nexport function validateNodeNumber(value: any, fieldName: string = 'nodeNum'): void {\n if (typeof value !== 'number' || !Number.isInteger(value)) {\n throw new MeshtasticError(`${fieldName} must be an integer, got ${typeof value}`);\n }\n\n if (value < 0 || value > 0xFFFFFFFF) {\n throw new MeshtasticError(`${fieldName} must be a valid uint32 (0-4294967295), got ${value}`);\n }\n}\n\n/**\n * Validate that a value is a valid packet ID (uint32)\n * @param value - Value to validate\n * @param fieldName - Name of the field (for error messages)\n * @throws {MeshtasticError} If validation fails\n */\nexport function validatePacketId(value: any, fieldName: string = 'packetId'): void {\n validateNodeNumber(value, fieldName);\n}\n\n/**\n * Validate that a value is a valid channel index\n * @param value - Value to validate\n * @param fieldName - Name of the field (for error messages)\n * @throws {MeshtasticError} If validation fails\n */\nexport function validateChannelIndex(value: any, fieldName: string = 'channel'): void {\n if (typeof value !== 'number' || !Number.isInteger(value)) {\n throw new MeshtasticError(`${fieldName} must be an integer, got ${typeof value}`);\n }\n\n if (value < 0 || value > 7) {\n throw new MeshtasticError(`${fieldName} must be 0-7, got ${value}`);\n }\n}\n\n/**\n * Validate that a value is a valid hop limit\n * @param value - Value to validate\n * @param fieldName - Name of the field (for error messages)\n * @throws {MeshtasticError} If validation fails\n */\nexport function validateHopLimit(value: any, fieldName: string = 'hopLimit'): void {\n if (typeof value !== 'number' || !Number.isInteger(value)) {\n throw new MeshtasticError(`${fieldName} must be an integer, got ${typeof value}`);\n }\n\n if (value < 0 || value > 7) {\n throw new MeshtasticError(`${fieldName} must be 0-7, got ${value}`);\n }\n}\n\n/**\n * Validate that a value is a buffer\n * @param value - Value to validate\n * @param fieldName - Name of the field (for error messages)\n * @throws {MeshtasticError} If validation fails\n */\nexport function validateBuffer(value: any, fieldName: string = 'buffer'): void {\n if (!Buffer.isBuffer(value) && !(value instanceof Uint8Array)) {\n throw new MeshtasticError(`${fieldName} must be a Buffer or Uint8Array, got ${typeof value}`);\n }\n}\n\n/**\n * Validate that a value is a non-empty buffer\n * @param value - Value to validate\n * @param fieldName - Name of the field (for error messages)\n * @throws {MeshtasticError} If validation fails\n */\nexport function validateNonEmptyBuffer(value: any, fieldName: string = 'buffer'): void {\n validateBuffer(value, fieldName);\n if (value.length === 0) {\n throw new MeshtasticError(`${fieldName} cannot be empty`);\n }\n}\n\n/**\n * Validate that a value is a non-empty string\n * @param value - Value to validate\n * @param fieldName - Name of the field (for error messages)\n * @throws {MeshtasticError} If validation fails\n */\nexport function validateNonEmptyString(value: any, fieldName: string = 'string'): void {\n if (typeof value !== 'string' || value.trim().length === 0) {\n throw new MeshtasticError(`${fieldName} must be a non-empty string`);\n }\n}\n\n/**\n * Validate that a value is defined (not null or undefined)\n * @param value - Value to validate\n * @param fieldName - Name of the field (for error messages)\n * @throws {MeshtasticError} If validation fails\n */\nexport function validateDefined<T>(value: T | null | undefined, fieldName: string): asserts value is T {\n if (value === null || value === undefined) {\n throw new MeshtasticError(`${fieldName} is required but was ${value}`);\n }\n}\n"],"names":["MeshtasticError"],"mappings":";;AAYO,SAAS,mBAAmB,OAAY,YAAoB,WAAiB;AAClF,MAAI,OAAO,UAAU,YAAY,CAAC,OAAO,UAAU,KAAK,GAAG;AACzD,UAAM,IAAIA,QAAAA,gBAAgB,GAAG,SAAS,4BAA4B,OAAO,KAAK,EAAE;AAAA,EAClF;AAEA,MAAI,QAAQ,KAAK,QAAQ,YAAY;AACnC,UAAM,IAAIA,QAAAA,gBAAgB,GAAG,SAAS,+CAA+C,KAAK,EAAE;AAAA,EAC9F;AACF;AAQO,SAAS,iBAAiB,OAAY,YAAoB,YAAkB;AACjF,qBAAmB,OAAO,SAAS;AACrC;AAQO,SAAS,qBAAqB,OAAY,YAAoB,WAAiB;AACpF,MAAI,OAAO,UAAU,YAAY,CAAC,OAAO,UAAU,KAAK,GAAG;AACzD,UAAM,IAAIA,QAAAA,gBAAgB,GAAG,SAAS,4BAA4B,OAAO,KAAK,EAAE;AAAA,EAClF;AAEA,MAAI,QAAQ,KAAK,QAAQ,GAAG;AAC1B,UAAM,IAAIA,QAAAA,gBAAgB,GAAG,SAAS,qBAAqB,KAAK,EAAE;AAAA,EACpE;AACF;AAQO,SAAS,iBAAiB,OAAY,YAAoB,YAAkB;AACjF,MAAI,OAAO,UAAU,YAAY,CAAC,OAAO,UAAU,KAAK,GAAG;AACzD,UAAM,IAAIA,QAAAA,gBAAgB,GAAG,SAAS,4BAA4B,OAAO,KAAK,EAAE;AAAA,EAClF;AAEA,MAAI,QAAQ,KAAK,QAAQ,GAAG;AAC1B,UAAM,IAAIA,QAAAA,gBAAgB,GAAG,SAAS,qBAAqB,KAAK,EAAE;AAAA,EACpE;AACF;;;;;"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  const crypto$1 = require("crypto");
3
- const decoder = require("./decoder-BvBAtm2U.js");
3
+ const decoder = require("./decoder.js");
4
4
  function constructChannelNonce(nodeNum, packetId) {
5
5
  if (!Number.isInteger(nodeNum) || nodeNum < 0 || nodeNum > 4294967295) {
6
6
  throw new decoder.InvalidNonceError(`Invalid node number: ${nodeNum}. Must be uint32.`);
@@ -768,4 +768,4 @@ exports.isDefaultKey = isDefaultKey;
768
768
  exports.parseKey = parseKey;
769
769
  exports.parsePrivateKey = parsePrivateKey;
770
770
  exports.parsePublicKey = parsePublicKey;
771
- //# sourceMappingURL=x25519-D0dlDGB0.js.map
771
+ //# sourceMappingURL=x25519.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"x25519.js","sources":["../src/lib/crypto/nonce.ts","../src/lib/crypto/key-parser.ts","../src/lib/crypto/aes-ctr.ts","../src/lib/crypto/aes-ccm.ts","../node_modules/@noble/hashes/esm/crypto.js","../node_modules/@noble/hashes/esm/utils.js","../node_modules/@noble/curves/esm/utils.js","../node_modules/@noble/curves/esm/abstract/modular.js","../node_modules/@noble/curves/esm/abstract/montgomery.js","../node_modules/@noble/curves/esm/ed25519.js","../src/lib/crypto/x25519.ts"],"sourcesContent":["/**\n * Nonce and IV construction for Meshtastic encryption\n */\n\nimport { InvalidNonceError } from '../utils/error-types';\nimport { randomBytes } from 'crypto';\n\n/**\n * Construct nonce/IV for AES-CTR channel encryption\n *\n * Meshtastic uses a 128-bit IV for AES-CTR constructed as follows:\n * - Bytes 0-3: Node number (from field, little-endian uint32)\n * - Bytes 4-7: Packet ID (id field, little-endian uint32)\n * - Bytes 8-15: Zero (counter starts at 0, increments per 16-byte block)\n *\n * @param nodeNum - Source node number (from field)\n * @param packetId - Packet ID\n * @returns 16-byte Buffer containing the IV\n * @throws {InvalidNonceError} If parameters are invalid\n */\nexport function constructChannelNonce(nodeNum: number, packetId: number): Buffer {\n if (!Number.isInteger(nodeNum) || nodeNum < 0 || nodeNum > 0xFFFFFFFF) {\n throw new InvalidNonceError(`Invalid node number: ${nodeNum}. Must be uint32.`);\n }\n\n if (!Number.isInteger(packetId) || packetId < 0 || packetId > 0xFFFFFFFF) {\n throw new InvalidNonceError(`Invalid packet ID: ${packetId}. Must be uint32.`);\n }\n\n const iv = Buffer.alloc(16);\n\n // Write node number at offset 0 (4 bytes, little-endian)\n iv.writeUInt32LE(nodeNum, 0);\n\n // Write packet ID at offset 4 (4 bytes, little-endian)\n iv.writeUInt32LE(packetId, 4);\n\n // Bytes 8-15 remain zero (counter starts at 0)\n\n return iv;\n}\n\n/**\n * Generate a random nonce for AES-CCM direct message encryption\n *\n * For PKC direct messages, Meshtastic uses AES-CCM which requires a nonce.\n * The nonce should be random and unique for each message.\n *\n * @param length - Nonce length in bytes (default: 12, suitable for AES-CCM)\n * @returns Buffer containing random nonce\n */\nexport function generateRandomNonce(length: number = 12): Buffer {\n if (length < 7 || length > 13) {\n throw new InvalidNonceError(`Nonce length must be between 7 and 13 bytes, got ${length}`);\n }\n\n return randomBytes(length);\n}\n\n/**\n * Validate a nonce buffer\n * @param nonce - Nonce buffer to validate\n * @param expectedLength - Expected length (optional)\n * @throws {InvalidNonceError} If nonce is invalid\n */\nexport function validateNonce(nonce: Buffer, expectedLength?: number): void {\n if (!Buffer.isBuffer(nonce)) {\n throw new InvalidNonceError('Nonce must be a Buffer');\n }\n\n if (expectedLength !== undefined && nonce.length !== expectedLength) {\n throw new InvalidNonceError(\n `Invalid nonce length: ${nonce.length} bytes. Expected ${expectedLength} bytes.`\n );\n }\n}\n","/**\n * Key parsing and validation utilities for Meshtastic encryption\n */\n\nimport { InvalidKeyError } from '../utils/error-types';\n\nexport type KeyFormat = 'base64' | 'hex';\n\n/**\n * Default Meshtastic channel key (insecure)\n */\nexport const DEFAULT_KEY_BASE64 = 'AQ==';\n\n/**\n * Parse a key from string format to Buffer\n * @param key - Key string in base64 or hex format\n * @param format - Format of the key string\n * @returns Buffer containing the key bytes\n * @throws {InvalidKeyError} If key format is invalid\n */\nexport function parseKey(key: string, format: KeyFormat = 'base64'): Buffer {\n if (!key || key.trim().length === 0) {\n throw new InvalidKeyError('Key cannot be empty');\n }\n\n let keyBuffer: Buffer;\n\n try {\n if (format === 'base64') {\n keyBuffer = Buffer.from(key, 'base64');\n } else if (format === 'hex') {\n keyBuffer = Buffer.from(key, 'hex');\n } else {\n throw new InvalidKeyError(`Unsupported key format: ${format}`);\n }\n } catch (error) {\n throw new InvalidKeyError(\n `Failed to parse key as ${format}: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n\n validateKeyLength(keyBuffer);\n return keyBuffer;\n}\n\n/**\n * Validate that a key has a valid length for AES encryption\n * @param key - Key buffer to validate\n * @throws {InvalidKeyError} If key length is invalid\n */\nexport function validateKeyLength(key: Buffer): void {\n const validLengths = [16, 32]; // AES-128 or AES-256\n if (!validLengths.includes(key.length)) {\n throw new InvalidKeyError(\n `Invalid key length: ${key.length} bytes. Expected 16 (AES-128) or 32 (AES-256) bytes.`\n );\n }\n}\n\n/**\n * Check if a key is the insecure default Meshtastic key\n * @param key - Key buffer to check\n * @returns true if the key is the default key\n */\nexport function isDefaultKey(key: Buffer): boolean {\n const defaultKey = Buffer.from(DEFAULT_KEY_BASE64, 'base64');\n return key.equals(defaultKey);\n}\n\n/**\n * Get the AES algorithm name for a given key length\n * @param key - Key buffer\n * @returns Algorithm name (e.g., 'aes-256-ctr')\n */\nexport function getAlgorithmForKey(key: Buffer, mode: 'ctr' | 'ccm' = 'ctr'): string {\n validateKeyLength(key);\n const keySize = key.length === 32 ? '256' : '128';\n return `aes-${keySize}-${mode}`;\n}\n\n/**\n * Parse a public key for x25519\n * @param key - Key string in base64 or hex format\n * @param format - Format of the key string\n * @returns Buffer containing the 32-byte public key\n * @throws {InvalidKeyError} If key is not 32 bytes\n */\nexport function parsePublicKey(key: string, format: KeyFormat = 'base64'): Buffer {\n const keyBuffer = parseKey(key, format);\n if (keyBuffer.length !== 32) {\n throw new InvalidKeyError(`Public key must be 32 bytes, got ${keyBuffer.length} bytes`);\n }\n return keyBuffer;\n}\n\n/**\n * Parse a private key for x25519\n * @param key - Key string in base64 or hex format\n * @param format - Format of the key string\n * @returns Buffer containing the 32-byte private key\n * @throws {InvalidKeyError} If key is not 32 bytes\n */\nexport function parsePrivateKey(key: string, format: KeyFormat = 'base64'): Buffer {\n const keyBuffer = parseKey(key, format);\n if (keyBuffer.length !== 32) {\n throw new InvalidKeyError(`Private key must be 32 bytes, got ${keyBuffer.length} bytes`);\n }\n return keyBuffer;\n}\n","/**\n * AES-CTR encryption/decryption for Meshtastic channel messages\n */\n\nimport { createCipheriv, createDecipheriv } from 'crypto';\nimport { MeshtasticEncryptError, MeshtasticDecryptError } from '../utils/error-types';\nimport { constructChannelNonce } from './nonce';\nimport { getAlgorithmForKey } from './key-parser';\n\n/**\n * Encrypt a message using AES-CTR with Meshtastic channel encryption\n *\n * @param plaintext - Data to encrypt\n * @param psk - Pre-shared key (16 or 32 bytes)\n * @param nodeNum - Source node number (for nonce construction)\n * @param packetId - Packet ID (for nonce construction)\n * @returns Encrypted ciphertext\n * @throws {MeshtasticEncryptError} If encryption fails\n */\nexport function encryptChannelMessage(\n plaintext: Buffer,\n psk: Buffer,\n nodeNum: number,\n packetId: number\n): Buffer {\n try {\n // Construct IV from node number and packet ID\n const iv = constructChannelNonce(nodeNum, packetId);\n\n // Determine algorithm based on key length\n const algorithm = getAlgorithmForKey(psk, 'ctr');\n\n // Create cipher\n const cipher = createCipheriv(algorithm, psk, iv);\n\n // Encrypt\n const ciphertext = Buffer.concat([\n cipher.update(plaintext),\n cipher.final()\n ]);\n\n return ciphertext;\n } catch (error) {\n throw new MeshtasticEncryptError(\n `Failed to encrypt channel message: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n\n/**\n * Decrypt a message using AES-CTR with Meshtastic channel encryption\n *\n * @param ciphertext - Data to decrypt\n * @param psk - Pre-shared key (16 or 32 bytes)\n * @param nodeNum - Source node number (from packet from field)\n * @param packetId - Packet ID (from packet id field)\n * @returns Decrypted plaintext\n * @throws {MeshtasticDecryptError} If decryption fails\n */\nexport function decryptChannelMessage(\n ciphertext: Buffer,\n psk: Buffer,\n nodeNum: number,\n packetId: number\n): Buffer {\n try {\n // Construct IV from node number and packet ID\n const iv = constructChannelNonce(nodeNum, packetId);\n\n // Determine algorithm based on key length\n const algorithm = getAlgorithmForKey(psk, 'ctr');\n\n // Create decipher (in CTR mode, decryption is same as encryption)\n const decipher = createDecipheriv(algorithm, psk, iv);\n\n // Decrypt\n const plaintext = Buffer.concat([\n decipher.update(ciphertext),\n decipher.final()\n ]);\n\n return plaintext;\n } catch (error) {\n throw new MeshtasticDecryptError(\n `Failed to decrypt channel message: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n","/**\n * AES-CCM encryption/decryption for Meshtastic direct messages (PKC)\n */\n\nimport { createCipheriv, createDecipheriv } from 'crypto';\nimport { MeshtasticEncryptError, MeshtasticDecryptError } from '../utils/error-types';\nimport { validateNonce } from './nonce';\n\n/**\n * Result of AES-CCM encryption\n */\nexport interface EncryptedMessage {\n ciphertext: Buffer;\n authTag: Buffer;\n}\n\n/**\n * Encrypt a direct message using AES-CCM\n *\n * AES-CCM provides both confidentiality and authenticity through\n * an authentication tag.\n *\n * @param plaintext - Data to encrypt\n * @param sharedSecret - Derived shared secret (32 bytes for AES-256)\n * @param nonce - Random nonce (12 bytes recommended for AES-CCM)\n * @param authTagLength - Authentication tag length in bytes (default: 16)\n * @returns Object containing ciphertext and authentication tag\n * @throws {MeshtasticEncryptError} If encryption fails\n */\nexport function encryptDirectMessage(\n plaintext: Buffer,\n sharedSecret: Buffer,\n nonce: Buffer,\n authTagLength: number = 16\n): EncryptedMessage {\n try {\n // Validate nonce\n validateNonce(nonce);\n\n // Validate shared secret (should be 32 bytes for AES-256)\n if (sharedSecret.length !== 32) {\n throw new Error(`Shared secret must be 32 bytes, got ${sharedSecret.length}`);\n }\n\n // Validate auth tag length (must be 4, 6, 8, 10, 12, 14, or 16)\n const validTagLengths = [4, 6, 8, 10, 12, 14, 16];\n if (!validTagLengths.includes(authTagLength)) {\n throw new Error(`Invalid auth tag length: ${authTagLength}. Must be one of ${validTagLengths.join(', ')}`);\n }\n\n // Create cipher with CCM mode\n const cipher = createCipheriv('aes-256-ccm', sharedSecret, nonce, {\n authTagLength,\n });\n\n // Encrypt\n const ciphertext = Buffer.concat([\n cipher.update(plaintext),\n cipher.final()\n ]);\n\n // Get authentication tag\n const authTag = cipher.getAuthTag();\n\n return { ciphertext, authTag };\n } catch (error) {\n throw new MeshtasticEncryptError(\n `Failed to encrypt direct message: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n\n/**\n * Decrypt a direct message using AES-CCM\n *\n * @param ciphertext - Data to decrypt\n * @param sharedSecret - Derived shared secret (32 bytes for AES-256)\n * @param nonce - Nonce used during encryption\n * @param authTag - Authentication tag from encryption\n * @returns Decrypted plaintext\n * @throws {MeshtasticDecryptError} If decryption or authentication fails\n */\nexport function decryptDirectMessage(\n ciphertext: Buffer,\n sharedSecret: Buffer,\n nonce: Buffer,\n authTag: Buffer\n): Buffer {\n try {\n // Validate nonce\n validateNonce(nonce);\n\n // Validate shared secret\n if (sharedSecret.length !== 32) {\n throw new Error(`Shared secret must be 32 bytes, got ${sharedSecret.length}`);\n }\n\n // Validate auth tag\n if (!Buffer.isBuffer(authTag) || authTag.length === 0) {\n throw new Error('Invalid authentication tag');\n }\n\n // Create decipher with CCM mode\n const decipher = createDecipheriv('aes-256-ccm', sharedSecret, nonce, {\n authTagLength: authTag.length,\n });\n\n // Set authentication tag\n decipher.setAuthTag(authTag);\n\n // Decrypt and verify\n const plaintext = Buffer.concat([\n decipher.update(ciphertext),\n decipher.final() // This will throw if authentication fails\n ]);\n\n return plaintext;\n } catch (error) {\n // Authentication failure or decryption error\n throw new MeshtasticDecryptError(\n `Failed to decrypt direct message: ${error instanceof Error ? error.message : String(error)}. This may indicate message tampering or wrong key.`,\n error instanceof Error ? error : undefined\n );\n }\n}\n","export const crypto = typeof globalThis === 'object' && 'crypto' in globalThis ? globalThis.crypto : undefined;\n//# sourceMappingURL=crypto.js.map","/**\n * Utilities for hex, bytes, CSPRNG.\n * @module\n */\n/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */\n// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.\n// node.js versions earlier than v19 don't declare it in global scope.\n// For node.js, package.json#exports field mapping rewrites import\n// from `crypto` to `cryptoNode`, which imports native module.\n// Makes the utils un-importable in browsers without a bundler.\n// Once node.js 18 is deprecated (2025-04-30), we can just drop the import.\nimport { crypto } from '@noble/hashes/crypto';\n/** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */\nexport function isBytes(a) {\n return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');\n}\n/** Asserts something is positive integer. */\nexport function anumber(n) {\n if (!Number.isSafeInteger(n) || n < 0)\n throw new Error('positive integer expected, got ' + n);\n}\n/** Asserts something is Uint8Array. */\nexport function abytes(b, ...lengths) {\n if (!isBytes(b))\n throw new Error('Uint8Array expected');\n if (lengths.length > 0 && !lengths.includes(b.length))\n throw new Error('Uint8Array expected of length ' + lengths + ', got length=' + b.length);\n}\n/** Asserts something is hash */\nexport function ahash(h) {\n if (typeof h !== 'function' || typeof h.create !== 'function')\n throw new Error('Hash should be wrapped by utils.createHasher');\n anumber(h.outputLen);\n anumber(h.blockLen);\n}\n/** Asserts a hash instance has not been destroyed / finished */\nexport function aexists(instance, checkFinished = true) {\n if (instance.destroyed)\n throw new Error('Hash instance has been destroyed');\n if (checkFinished && instance.finished)\n throw new Error('Hash#digest() has already been called');\n}\n/** Asserts output is properly-sized byte array */\nexport function aoutput(out, instance) {\n abytes(out);\n const min = instance.outputLen;\n if (out.length < min) {\n throw new Error('digestInto() expects output buffer of length at least ' + min);\n }\n}\n/** Cast u8 / u16 / u32 to u8. */\nexport function u8(arr) {\n return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);\n}\n/** Cast u8 / u16 / u32 to u32. */\nexport function u32(arr) {\n return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));\n}\n/** Zeroize a byte array. Warning: JS provides no guarantees. */\nexport function clean(...arrays) {\n for (let i = 0; i < arrays.length; i++) {\n arrays[i].fill(0);\n }\n}\n/** Create DataView of an array for easy byte-level manipulation. */\nexport function createView(arr) {\n return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);\n}\n/** The rotate right (circular right shift) operation for uint32 */\nexport function rotr(word, shift) {\n return (word << (32 - shift)) | (word >>> shift);\n}\n/** The rotate left (circular left shift) operation for uint32 */\nexport function rotl(word, shift) {\n return (word << shift) | ((word >>> (32 - shift)) >>> 0);\n}\n/** Is current platform little-endian? Most are. Big-Endian platform: IBM */\nexport const isLE = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();\n/** The byte swap operation for uint32 */\nexport function byteSwap(word) {\n return (((word << 24) & 0xff000000) |\n ((word << 8) & 0xff0000) |\n ((word >>> 8) & 0xff00) |\n ((word >>> 24) & 0xff));\n}\n/** Conditionally byte swap if on a big-endian platform */\nexport const swap8IfBE = isLE\n ? (n) => n\n : (n) => byteSwap(n);\n/** @deprecated */\nexport const byteSwapIfBE = swap8IfBE;\n/** In place byte swap for Uint32Array */\nexport function byteSwap32(arr) {\n for (let i = 0; i < arr.length; i++) {\n arr[i] = byteSwap(arr[i]);\n }\n return arr;\n}\nexport const swap32IfBE = isLE\n ? (u) => u\n : byteSwap32;\n// Built-in hex conversion https://caniuse.com/mdn-javascript_builtins_uint8array_fromhex\nconst hasHexBuiltin = /* @__PURE__ */ (() => \n// @ts-ignore\ntypeof Uint8Array.from([]).toHex === 'function' && typeof Uint8Array.fromHex === 'function')();\n// Array where index 0xf0 (240) is mapped to string 'f0'\nconst hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'));\n/**\n * Convert byte array to hex string. Uses built-in function, when available.\n * @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'\n */\nexport function bytesToHex(bytes) {\n abytes(bytes);\n // @ts-ignore\n if (hasHexBuiltin)\n return bytes.toHex();\n // pre-caching improves the speed 6x\n let hex = '';\n for (let i = 0; i < bytes.length; i++) {\n hex += hexes[bytes[i]];\n }\n return hex;\n}\n// We use optimized technique to convert hex string to byte array\nconst asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };\nfunction asciiToBase16(ch) {\n if (ch >= asciis._0 && ch <= asciis._9)\n return ch - asciis._0; // '2' => 50-48\n if (ch >= asciis.A && ch <= asciis.F)\n return ch - (asciis.A - 10); // 'B' => 66-(65-10)\n if (ch >= asciis.a && ch <= asciis.f)\n return ch - (asciis.a - 10); // 'b' => 98-(97-10)\n return;\n}\n/**\n * Convert hex string to byte array. Uses built-in function, when available.\n * @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])\n */\nexport function hexToBytes(hex) {\n if (typeof hex !== 'string')\n throw new Error('hex string expected, got ' + typeof hex);\n // @ts-ignore\n if (hasHexBuiltin)\n return Uint8Array.fromHex(hex);\n const hl = hex.length;\n const al = hl / 2;\n if (hl % 2)\n throw new Error('hex string expected, got unpadded hex of length ' + hl);\n const array = new Uint8Array(al);\n for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {\n const n1 = asciiToBase16(hex.charCodeAt(hi));\n const n2 = asciiToBase16(hex.charCodeAt(hi + 1));\n if (n1 === undefined || n2 === undefined) {\n const char = hex[hi] + hex[hi + 1];\n throw new Error('hex string expected, got non-hex character \"' + char + '\" at index ' + hi);\n }\n array[ai] = n1 * 16 + n2; // multiply first octet, e.g. 'a3' => 10*16+3 => 160 + 3 => 163\n }\n return array;\n}\n/**\n * There is no setImmediate in browser and setTimeout is slow.\n * Call of async fn will return Promise, which will be fullfiled only on\n * next scheduler queue processing step and this is exactly what we need.\n */\nexport const nextTick = async () => { };\n/** Returns control to thread each 'tick' ms to avoid blocking. */\nexport async function asyncLoop(iters, tick, cb) {\n let ts = Date.now();\n for (let i = 0; i < iters; i++) {\n cb(i);\n // Date.now() is not monotonic, so in case if clock goes backwards we return return control too\n const diff = Date.now() - ts;\n if (diff >= 0 && diff < tick)\n continue;\n await nextTick();\n ts += diff;\n }\n}\n/**\n * Converts string to bytes using UTF8 encoding.\n * @example utf8ToBytes('abc') // Uint8Array.from([97, 98, 99])\n */\nexport function utf8ToBytes(str) {\n if (typeof str !== 'string')\n throw new Error('string expected');\n return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809\n}\n/**\n * Converts bytes to string using UTF8 encoding.\n * @example bytesToUtf8(Uint8Array.from([97, 98, 99])) // 'abc'\n */\nexport function bytesToUtf8(bytes) {\n return new TextDecoder().decode(bytes);\n}\n/**\n * Normalizes (non-hex) string or Uint8Array to Uint8Array.\n * Warning: when Uint8Array is passed, it would NOT get copied.\n * Keep in mind for future mutable operations.\n */\nexport function toBytes(data) {\n if (typeof data === 'string')\n data = utf8ToBytes(data);\n abytes(data);\n return data;\n}\n/**\n * Helper for KDFs: consumes uint8array or string.\n * When string is passed, does utf8 decoding, using TextDecoder.\n */\nexport function kdfInputToBytes(data) {\n if (typeof data === 'string')\n data = utf8ToBytes(data);\n abytes(data);\n return data;\n}\n/** Copies several Uint8Arrays into one. */\nexport function concatBytes(...arrays) {\n let sum = 0;\n for (let i = 0; i < arrays.length; i++) {\n const a = arrays[i];\n abytes(a);\n sum += a.length;\n }\n const res = new Uint8Array(sum);\n for (let i = 0, pad = 0; i < arrays.length; i++) {\n const a = arrays[i];\n res.set(a, pad);\n pad += a.length;\n }\n return res;\n}\nexport function checkOpts(defaults, opts) {\n if (opts !== undefined && {}.toString.call(opts) !== '[object Object]')\n throw new Error('options should be object or undefined');\n const merged = Object.assign(defaults, opts);\n return merged;\n}\n/** For runtime check if class implements interface */\nexport class Hash {\n}\n/** Wraps hash function, creating an interface on top of it */\nexport function createHasher(hashCons) {\n const hashC = (msg) => hashCons().update(toBytes(msg)).digest();\n const tmp = hashCons();\n hashC.outputLen = tmp.outputLen;\n hashC.blockLen = tmp.blockLen;\n hashC.create = () => hashCons();\n return hashC;\n}\nexport function createOptHasher(hashCons) {\n const hashC = (msg, opts) => hashCons(opts).update(toBytes(msg)).digest();\n const tmp = hashCons({});\n hashC.outputLen = tmp.outputLen;\n hashC.blockLen = tmp.blockLen;\n hashC.create = (opts) => hashCons(opts);\n return hashC;\n}\nexport function createXOFer(hashCons) {\n const hashC = (msg, opts) => hashCons(opts).update(toBytes(msg)).digest();\n const tmp = hashCons({});\n hashC.outputLen = tmp.outputLen;\n hashC.blockLen = tmp.blockLen;\n hashC.create = (opts) => hashCons(opts);\n return hashC;\n}\nexport const wrapConstructor = createHasher;\nexport const wrapConstructorWithOpts = createOptHasher;\nexport const wrapXOFConstructorWithOpts = createXOFer;\n/** Cryptographically secure PRNG. Uses internal OS-level `crypto.getRandomValues`. */\nexport function randomBytes(bytesLength = 32) {\n if (crypto && typeof crypto.getRandomValues === 'function') {\n return crypto.getRandomValues(new Uint8Array(bytesLength));\n }\n // Legacy Node.js compatibility\n if (crypto && typeof crypto.randomBytes === 'function') {\n return Uint8Array.from(crypto.randomBytes(bytesLength));\n }\n throw new Error('crypto.getRandomValues must be defined');\n}\n//# sourceMappingURL=utils.js.map","/**\n * Hex, bytes and number utilities.\n * @module\n */\n/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */\nimport { abytes as abytes_, bytesToHex as bytesToHex_, concatBytes as concatBytes_, hexToBytes as hexToBytes_, isBytes as isBytes_, } from '@noble/hashes/utils.js';\nexport { abytes, anumber, bytesToHex, bytesToUtf8, concatBytes, hexToBytes, isBytes, randomBytes, utf8ToBytes, } from '@noble/hashes/utils.js';\nconst _0n = /* @__PURE__ */ BigInt(0);\nconst _1n = /* @__PURE__ */ BigInt(1);\nexport function abool(title, value) {\n if (typeof value !== 'boolean')\n throw new Error(title + ' boolean expected, got ' + value);\n}\n// tmp name until v2\nexport function _abool2(value, title = '') {\n if (typeof value !== 'boolean') {\n const prefix = title && `\"${title}\"`;\n throw new Error(prefix + 'expected boolean, got type=' + typeof value);\n }\n return value;\n}\n// tmp name until v2\n/** Asserts something is Uint8Array. */\nexport function _abytes2(value, length, title = '') {\n const bytes = isBytes_(value);\n const len = value?.length;\n const needsLen = length !== undefined;\n if (!bytes || (needsLen && len !== length)) {\n const prefix = title && `\"${title}\" `;\n const ofLen = needsLen ? ` of length ${length}` : '';\n const got = bytes ? `length=${len}` : `type=${typeof value}`;\n throw new Error(prefix + 'expected Uint8Array' + ofLen + ', got ' + got);\n }\n return value;\n}\n// Used in weierstrass, der\nexport function numberToHexUnpadded(num) {\n const hex = num.toString(16);\n return hex.length & 1 ? '0' + hex : hex;\n}\nexport function hexToNumber(hex) {\n if (typeof hex !== 'string')\n throw new Error('hex string expected, got ' + typeof hex);\n return hex === '' ? _0n : BigInt('0x' + hex); // Big Endian\n}\n// BE: Big Endian, LE: Little Endian\nexport function bytesToNumberBE(bytes) {\n return hexToNumber(bytesToHex_(bytes));\n}\nexport function bytesToNumberLE(bytes) {\n abytes_(bytes);\n return hexToNumber(bytesToHex_(Uint8Array.from(bytes).reverse()));\n}\nexport function numberToBytesBE(n, len) {\n return hexToBytes_(n.toString(16).padStart(len * 2, '0'));\n}\nexport function numberToBytesLE(n, len) {\n return numberToBytesBE(n, len).reverse();\n}\n// Unpadded, rarely used\nexport function numberToVarBytesBE(n) {\n return hexToBytes_(numberToHexUnpadded(n));\n}\n/**\n * Takes hex string or Uint8Array, converts to Uint8Array.\n * Validates output length.\n * Will throw error for other types.\n * @param title descriptive title for an error e.g. 'secret key'\n * @param hex hex string or Uint8Array\n * @param expectedLength optional, will compare to result array's length\n * @returns\n */\nexport function ensureBytes(title, hex, expectedLength) {\n let res;\n if (typeof hex === 'string') {\n try {\n res = hexToBytes_(hex);\n }\n catch (e) {\n throw new Error(title + ' must be hex string or Uint8Array, cause: ' + e);\n }\n }\n else if (isBytes_(hex)) {\n // Uint8Array.from() instead of hash.slice() because node.js Buffer\n // is instance of Uint8Array, and its slice() creates **mutable** copy\n res = Uint8Array.from(hex);\n }\n else {\n throw new Error(title + ' must be hex string or Uint8Array');\n }\n const len = res.length;\n if (typeof expectedLength === 'number' && len !== expectedLength)\n throw new Error(title + ' of length ' + expectedLength + ' expected, got ' + len);\n return res;\n}\n// Compares 2 u8a-s in kinda constant time\nexport function equalBytes(a, b) {\n if (a.length !== b.length)\n return false;\n let diff = 0;\n for (let i = 0; i < a.length; i++)\n diff |= a[i] ^ b[i];\n return diff === 0;\n}\n/**\n * Copies Uint8Array. We can't use u8a.slice(), because u8a can be Buffer,\n * and Buffer#slice creates mutable copy. Never use Buffers!\n */\nexport function copyBytes(bytes) {\n return Uint8Array.from(bytes);\n}\n/**\n * Decodes 7-bit ASCII string to Uint8Array, throws on non-ascii symbols\n * Should be safe to use for things expected to be ASCII.\n * Returns exact same result as utf8ToBytes for ASCII or throws.\n */\nexport function asciiToBytes(ascii) {\n return Uint8Array.from(ascii, (c, i) => {\n const charCode = c.charCodeAt(0);\n if (c.length !== 1 || charCode > 127) {\n throw new Error(`string contains non-ASCII character \"${ascii[i]}\" with code ${charCode} at position ${i}`);\n }\n return charCode;\n });\n}\n/**\n * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])\n */\n// export const utf8ToBytes: typeof utf8ToBytes_ = utf8ToBytes_;\n/**\n * Converts bytes to string using UTF8 encoding.\n * @example bytesToUtf8(Uint8Array.from([97, 98, 99])) // 'abc'\n */\n// export const bytesToUtf8: typeof bytesToUtf8_ = bytesToUtf8_;\n// Is positive bigint\nconst isPosBig = (n) => typeof n === 'bigint' && _0n <= n;\nexport function inRange(n, min, max) {\n return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;\n}\n/**\n * Asserts min <= n < max. NOTE: It's < max and not <= max.\n * @example\n * aInRange('x', x, 1n, 256n); // would assume x is in (1n..255n)\n */\nexport function aInRange(title, n, min, max) {\n // Why min <= n < max and not a (min < n < max) OR b (min <= n <= max)?\n // consider P=256n, min=0n, max=P\n // - a for min=0 would require -1: `inRange('x', x, -1n, P)`\n // - b would commonly require subtraction: `inRange('x', x, 0n, P - 1n)`\n // - our way is the cleanest: `inRange('x', x, 0n, P)\n if (!inRange(n, min, max))\n throw new Error('expected valid ' + title + ': ' + min + ' <= n < ' + max + ', got ' + n);\n}\n// Bit operations\n/**\n * Calculates amount of bits in a bigint.\n * Same as `n.toString(2).length`\n * TODO: merge with nLength in modular\n */\nexport function bitLen(n) {\n let len;\n for (len = 0; n > _0n; n >>= _1n, len += 1)\n ;\n return len;\n}\n/**\n * Gets single bit at position.\n * NOTE: first bit position is 0 (same as arrays)\n * Same as `!!+Array.from(n.toString(2)).reverse()[pos]`\n */\nexport function bitGet(n, pos) {\n return (n >> BigInt(pos)) & _1n;\n}\n/**\n * Sets single bit at position.\n */\nexport function bitSet(n, pos, value) {\n return n | ((value ? _1n : _0n) << BigInt(pos));\n}\n/**\n * Calculate mask for N bits. Not using ** operator with bigints because of old engines.\n * Same as BigInt(`0b${Array(i).fill('1').join('')}`)\n */\nexport const bitMask = (n) => (_1n << BigInt(n)) - _1n;\n/**\n * Minimal HMAC-DRBG from NIST 800-90 for RFC6979 sigs.\n * @returns function that will call DRBG until 2nd arg returns something meaningful\n * @example\n * const drbg = createHmacDRBG<Key>(32, 32, hmac);\n * drbg(seed, bytesToKey); // bytesToKey must return Key or undefined\n */\nexport function createHmacDrbg(hashLen, qByteLen, hmacFn) {\n if (typeof hashLen !== 'number' || hashLen < 2)\n throw new Error('hashLen must be a number');\n if (typeof qByteLen !== 'number' || qByteLen < 2)\n throw new Error('qByteLen must be a number');\n if (typeof hmacFn !== 'function')\n throw new Error('hmacFn must be a function');\n // Step B, Step C: set hashLen to 8*ceil(hlen/8)\n const u8n = (len) => new Uint8Array(len); // creates Uint8Array\n const u8of = (byte) => Uint8Array.of(byte); // another shortcut\n let v = u8n(hashLen); // Minimal non-full-spec HMAC-DRBG from NIST 800-90 for RFC6979 sigs.\n let k = u8n(hashLen); // Steps B and C of RFC6979 3.2: set hashLen, in our case always same\n let i = 0; // Iterations counter, will throw when over 1000\n const reset = () => {\n v.fill(1);\n k.fill(0);\n i = 0;\n };\n const h = (...b) => hmacFn(k, v, ...b); // hmac(k)(v, ...values)\n const reseed = (seed = u8n(0)) => {\n // HMAC-DRBG reseed() function. Steps D-G\n k = h(u8of(0x00), seed); // k = hmac(k || v || 0x00 || seed)\n v = h(); // v = hmac(k || v)\n if (seed.length === 0)\n return;\n k = h(u8of(0x01), seed); // k = hmac(k || v || 0x01 || seed)\n v = h(); // v = hmac(k || v)\n };\n const gen = () => {\n // HMAC-DRBG generate() function\n if (i++ >= 1000)\n throw new Error('drbg: tried 1000 values');\n let len = 0;\n const out = [];\n while (len < qByteLen) {\n v = h();\n const sl = v.slice();\n out.push(sl);\n len += v.length;\n }\n return concatBytes_(...out);\n };\n const genUntil = (seed, pred) => {\n reset();\n reseed(seed); // Steps D-G\n let res = undefined; // Step H: grind until k is in [1..n-1]\n while (!(res = pred(gen())))\n reseed();\n reset();\n return res;\n };\n return genUntil;\n}\n// Validating curves and fields\nconst validatorFns = {\n bigint: (val) => typeof val === 'bigint',\n function: (val) => typeof val === 'function',\n boolean: (val) => typeof val === 'boolean',\n string: (val) => typeof val === 'string',\n stringOrUint8Array: (val) => typeof val === 'string' || isBytes_(val),\n isSafeInteger: (val) => Number.isSafeInteger(val),\n array: (val) => Array.isArray(val),\n field: (val, object) => object.Fp.isValid(val),\n hash: (val) => typeof val === 'function' && Number.isSafeInteger(val.outputLen),\n};\n// type Record<K extends string | number | symbol, T> = { [P in K]: T; }\nexport function validateObject(object, validators, optValidators = {}) {\n const checkField = (fieldName, type, isOptional) => {\n const checkVal = validatorFns[type];\n if (typeof checkVal !== 'function')\n throw new Error('invalid validator function');\n const val = object[fieldName];\n if (isOptional && val === undefined)\n return;\n if (!checkVal(val, object)) {\n throw new Error('param ' + String(fieldName) + ' is invalid. Expected ' + type + ', got ' + val);\n }\n };\n for (const [fieldName, type] of Object.entries(validators))\n checkField(fieldName, type, false);\n for (const [fieldName, type] of Object.entries(optValidators))\n checkField(fieldName, type, true);\n return object;\n}\n// validate type tests\n// const o: { a: number; b: number; c: number } = { a: 1, b: 5, c: 6 };\n// const z0 = validateObject(o, { a: 'isSafeInteger' }, { c: 'bigint' }); // Ok!\n// // Should fail type-check\n// const z1 = validateObject(o, { a: 'tmp' }, { c: 'zz' });\n// const z2 = validateObject(o, { a: 'isSafeInteger' }, { c: 'zz' });\n// const z3 = validateObject(o, { test: 'boolean', z: 'bug' });\n// const z4 = validateObject(o, { a: 'boolean', z: 'bug' });\nexport function isHash(val) {\n return typeof val === 'function' && Number.isSafeInteger(val.outputLen);\n}\nexport function _validateObject(object, fields, optFields = {}) {\n if (!object || typeof object !== 'object')\n throw new Error('expected valid options object');\n function checkField(fieldName, expectedType, isOpt) {\n const val = object[fieldName];\n if (isOpt && val === undefined)\n return;\n const current = typeof val;\n if (current !== expectedType || val === null)\n throw new Error(`param \"${fieldName}\" is invalid: expected ${expectedType}, got ${current}`);\n }\n Object.entries(fields).forEach(([k, v]) => checkField(k, v, false));\n Object.entries(optFields).forEach(([k, v]) => checkField(k, v, true));\n}\n/**\n * throws not implemented error\n */\nexport const notImplemented = () => {\n throw new Error('not implemented');\n};\n/**\n * Memoizes (caches) computation result.\n * Uses WeakMap: the value is going auto-cleaned by GC after last reference is removed.\n */\nexport function memoized(fn) {\n const map = new WeakMap();\n return (arg, ...args) => {\n const val = map.get(arg);\n if (val !== undefined)\n return val;\n const computed = fn(arg, ...args);\n map.set(arg, computed);\n return computed;\n };\n}\n//# sourceMappingURL=utils.js.map","/**\n * Utils for modular division and fields.\n * Field over 11 is a finite (Galois) field is integer number operations `mod 11`.\n * There is no division: it is replaced by modular multiplicative inverse.\n * @module\n */\n/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */\nimport { _validateObject, anumber, bitMask, bytesToNumberBE, bytesToNumberLE, ensureBytes, numberToBytesBE, numberToBytesLE, } from \"../utils.js\";\n// prettier-ignore\nconst _0n = BigInt(0), _1n = BigInt(1), _2n = /* @__PURE__ */ BigInt(2), _3n = /* @__PURE__ */ BigInt(3);\n// prettier-ignore\nconst _4n = /* @__PURE__ */ BigInt(4), _5n = /* @__PURE__ */ BigInt(5), _7n = /* @__PURE__ */ BigInt(7);\n// prettier-ignore\nconst _8n = /* @__PURE__ */ BigInt(8), _9n = /* @__PURE__ */ BigInt(9), _16n = /* @__PURE__ */ BigInt(16);\n// Calculates a modulo b\nexport function mod(a, b) {\n const result = a % b;\n return result >= _0n ? result : b + result;\n}\n/**\n * Efficiently raise num to power and do modular division.\n * Unsafe in some contexts: uses ladder, so can expose bigint bits.\n * @example\n * pow(2n, 6n, 11n) // 64n % 11n == 9n\n */\nexport function pow(num, power, modulo) {\n return FpPow(Field(modulo), num, power);\n}\n/** Does `x^(2^power)` mod p. `pow2(30, 4)` == `30^(2^4)` */\nexport function pow2(x, power, modulo) {\n let res = x;\n while (power-- > _0n) {\n res *= res;\n res %= modulo;\n }\n return res;\n}\n/**\n * Inverses number over modulo.\n * Implemented using [Euclidean GCD](https://brilliant.org/wiki/extended-euclidean-algorithm/).\n */\nexport function invert(number, modulo) {\n if (number === _0n)\n throw new Error('invert: expected non-zero number');\n if (modulo <= _0n)\n throw new Error('invert: expected positive modulus, got ' + modulo);\n // Fermat's little theorem \"CT-like\" version inv(n) = n^(m-2) mod m is 30x slower.\n let a = mod(number, modulo);\n let b = modulo;\n // prettier-ignore\n let x = _0n, y = _1n, u = _1n, v = _0n;\n while (a !== _0n) {\n // JIT applies optimization if those two lines follow each other\n const q = b / a;\n const r = b % a;\n const m = x - u * q;\n const n = y - v * q;\n // prettier-ignore\n b = a, a = r, x = u, y = v, u = m, v = n;\n }\n const gcd = b;\n if (gcd !== _1n)\n throw new Error('invert: does not exist');\n return mod(x, modulo);\n}\nfunction assertIsSquare(Fp, root, n) {\n if (!Fp.eql(Fp.sqr(root), n))\n throw new Error('Cannot find square root');\n}\n// Not all roots are possible! Example which will throw:\n// const NUM =\n// n = 72057594037927816n;\n// Fp = Field(BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab'));\nfunction sqrt3mod4(Fp, n) {\n const p1div4 = (Fp.ORDER + _1n) / _4n;\n const root = Fp.pow(n, p1div4);\n assertIsSquare(Fp, root, n);\n return root;\n}\nfunction sqrt5mod8(Fp, n) {\n const p5div8 = (Fp.ORDER - _5n) / _8n;\n const n2 = Fp.mul(n, _2n);\n const v = Fp.pow(n2, p5div8);\n const nv = Fp.mul(n, v);\n const i = Fp.mul(Fp.mul(nv, _2n), v);\n const root = Fp.mul(nv, Fp.sub(i, Fp.ONE));\n assertIsSquare(Fp, root, n);\n return root;\n}\n// Based on RFC9380, Kong algorithm\n// prettier-ignore\nfunction sqrt9mod16(P) {\n const Fp_ = Field(P);\n const tn = tonelliShanks(P);\n const c1 = tn(Fp_, Fp_.neg(Fp_.ONE)); // 1. c1 = sqrt(-1) in F, i.e., (c1^2) == -1 in F\n const c2 = tn(Fp_, c1); // 2. c2 = sqrt(c1) in F, i.e., (c2^2) == c1 in F\n const c3 = tn(Fp_, Fp_.neg(c1)); // 3. c3 = sqrt(-c1) in F, i.e., (c3^2) == -c1 in F\n const c4 = (P + _7n) / _16n; // 4. c4 = (q + 7) / 16 # Integer arithmetic\n return (Fp, n) => {\n let tv1 = Fp.pow(n, c4); // 1. tv1 = x^c4\n let tv2 = Fp.mul(tv1, c1); // 2. tv2 = c1 * tv1\n const tv3 = Fp.mul(tv1, c2); // 3. tv3 = c2 * tv1\n const tv4 = Fp.mul(tv1, c3); // 4. tv4 = c3 * tv1\n const e1 = Fp.eql(Fp.sqr(tv2), n); // 5. e1 = (tv2^2) == x\n const e2 = Fp.eql(Fp.sqr(tv3), n); // 6. e2 = (tv3^2) == x\n tv1 = Fp.cmov(tv1, tv2, e1); // 7. tv1 = CMOV(tv1, tv2, e1) # Select tv2 if (tv2^2) == x\n tv2 = Fp.cmov(tv4, tv3, e2); // 8. tv2 = CMOV(tv4, tv3, e2) # Select tv3 if (tv3^2) == x\n const e3 = Fp.eql(Fp.sqr(tv2), n); // 9. e3 = (tv2^2) == x\n const root = Fp.cmov(tv1, tv2, e3); // 10. z = CMOV(tv1, tv2, e3) # Select sqrt from tv1 & tv2\n assertIsSquare(Fp, root, n);\n return root;\n };\n}\n/**\n * Tonelli-Shanks square root search algorithm.\n * 1. https://eprint.iacr.org/2012/685.pdf (page 12)\n * 2. Square Roots from 1; 24, 51, 10 to Dan Shanks\n * @param P field order\n * @returns function that takes field Fp (created from P) and number n\n */\nexport function tonelliShanks(P) {\n // Initialization (precomputation).\n // Caching initialization could boost perf by 7%.\n if (P < _3n)\n throw new Error('sqrt is not defined for small field');\n // Factor P - 1 = Q * 2^S, where Q is odd\n let Q = P - _1n;\n let S = 0;\n while (Q % _2n === _0n) {\n Q /= _2n;\n S++;\n }\n // Find the first quadratic non-residue Z >= 2\n let Z = _2n;\n const _Fp = Field(P);\n while (FpLegendre(_Fp, Z) === 1) {\n // Basic primality test for P. After x iterations, chance of\n // not finding quadratic non-residue is 2^x, so 2^1000.\n if (Z++ > 1000)\n throw new Error('Cannot find square root: probably non-prime P');\n }\n // Fast-path; usually done before Z, but we do \"primality test\".\n if (S === 1)\n return sqrt3mod4;\n // Slow-path\n // TODO: test on Fp2 and others\n let cc = _Fp.pow(Z, Q); // c = z^Q\n const Q1div2 = (Q + _1n) / _2n;\n return function tonelliSlow(Fp, n) {\n if (Fp.is0(n))\n return n;\n // Check if n is a quadratic residue using Legendre symbol\n if (FpLegendre(Fp, n) !== 1)\n throw new Error('Cannot find square root');\n // Initialize variables for the main loop\n let M = S;\n let c = Fp.mul(Fp.ONE, cc); // c = z^Q, move cc from field _Fp into field Fp\n let t = Fp.pow(n, Q); // t = n^Q, first guess at the fudge factor\n let R = Fp.pow(n, Q1div2); // R = n^((Q+1)/2), first guess at the square root\n // Main loop\n // while t != 1\n while (!Fp.eql(t, Fp.ONE)) {\n if (Fp.is0(t))\n return Fp.ZERO; // if t=0 return R=0\n let i = 1;\n // Find the smallest i >= 1 such that t^(2^i) ≡ 1 (mod P)\n let t_tmp = Fp.sqr(t); // t^(2^1)\n while (!Fp.eql(t_tmp, Fp.ONE)) {\n i++;\n t_tmp = Fp.sqr(t_tmp); // t^(2^2)...\n if (i === M)\n throw new Error('Cannot find square root');\n }\n // Calculate the exponent for b: 2^(M - i - 1)\n const exponent = _1n << BigInt(M - i - 1); // bigint is important\n const b = Fp.pow(c, exponent); // b = 2^(M - i - 1)\n // Update variables\n M = i;\n c = Fp.sqr(b); // c = b^2\n t = Fp.mul(t, c); // t = (t * b^2)\n R = Fp.mul(R, b); // R = R*b\n }\n return R;\n };\n}\n/**\n * Square root for a finite field. Will try optimized versions first:\n *\n * 1. P ≡ 3 (mod 4)\n * 2. P ≡ 5 (mod 8)\n * 3. P ≡ 9 (mod 16)\n * 4. Tonelli-Shanks algorithm\n *\n * Different algorithms can give different roots, it is up to user to decide which one they want.\n * For example there is FpSqrtOdd/FpSqrtEven to choice root based on oddness (used for hash-to-curve).\n */\nexport function FpSqrt(P) {\n // P ≡ 3 (mod 4) => √n = n^((P+1)/4)\n if (P % _4n === _3n)\n return sqrt3mod4;\n // P ≡ 5 (mod 8) => Atkin algorithm, page 10 of https://eprint.iacr.org/2012/685.pdf\n if (P % _8n === _5n)\n return sqrt5mod8;\n // P ≡ 9 (mod 16) => Kong algorithm, page 11 of https://eprint.iacr.org/2012/685.pdf (algorithm 4)\n if (P % _16n === _9n)\n return sqrt9mod16(P);\n // Tonelli-Shanks algorithm\n return tonelliShanks(P);\n}\n// Little-endian check for first LE bit (last BE bit);\nexport const isNegativeLE = (num, modulo) => (mod(num, modulo) & _1n) === _1n;\n// prettier-ignore\nconst FIELD_FIELDS = [\n 'create', 'isValid', 'is0', 'neg', 'inv', 'sqrt', 'sqr',\n 'eql', 'add', 'sub', 'mul', 'pow', 'div',\n 'addN', 'subN', 'mulN', 'sqrN'\n];\nexport function validateField(field) {\n const initial = {\n ORDER: 'bigint',\n MASK: 'bigint',\n BYTES: 'number',\n BITS: 'number',\n };\n const opts = FIELD_FIELDS.reduce((map, val) => {\n map[val] = 'function';\n return map;\n }, initial);\n _validateObject(field, opts);\n // const max = 16384;\n // if (field.BYTES < 1 || field.BYTES > max) throw new Error('invalid field');\n // if (field.BITS < 1 || field.BITS > 8 * max) throw new Error('invalid field');\n return field;\n}\n// Generic field functions\n/**\n * Same as `pow` but for Fp: non-constant-time.\n * Unsafe in some contexts: uses ladder, so can expose bigint bits.\n */\nexport function FpPow(Fp, num, power) {\n if (power < _0n)\n throw new Error('invalid exponent, negatives unsupported');\n if (power === _0n)\n return Fp.ONE;\n if (power === _1n)\n return num;\n let p = Fp.ONE;\n let d = num;\n while (power > _0n) {\n if (power & _1n)\n p = Fp.mul(p, d);\n d = Fp.sqr(d);\n power >>= _1n;\n }\n return p;\n}\n/**\n * Efficiently invert an array of Field elements.\n * Exception-free. Will return `undefined` for 0 elements.\n * @param passZero map 0 to 0 (instead of undefined)\n */\nexport function FpInvertBatch(Fp, nums, passZero = false) {\n const inverted = new Array(nums.length).fill(passZero ? Fp.ZERO : undefined);\n // Walk from first to last, multiply them by each other MOD p\n const multipliedAcc = nums.reduce((acc, num, i) => {\n if (Fp.is0(num))\n return acc;\n inverted[i] = acc;\n return Fp.mul(acc, num);\n }, Fp.ONE);\n // Invert last element\n const invertedAcc = Fp.inv(multipliedAcc);\n // Walk from last to first, multiply them by inverted each other MOD p\n nums.reduceRight((acc, num, i) => {\n if (Fp.is0(num))\n return acc;\n inverted[i] = Fp.mul(acc, inverted[i]);\n return Fp.mul(acc, num);\n }, invertedAcc);\n return inverted;\n}\n// TODO: remove\nexport function FpDiv(Fp, lhs, rhs) {\n return Fp.mul(lhs, typeof rhs === 'bigint' ? invert(rhs, Fp.ORDER) : Fp.inv(rhs));\n}\n/**\n * Legendre symbol.\n * Legendre constant is used to calculate Legendre symbol (a | p)\n * which denotes the value of a^((p-1)/2) (mod p).\n *\n * * (a | p) ≡ 1 if a is a square (mod p), quadratic residue\n * * (a | p) ≡ -1 if a is not a square (mod p), quadratic non residue\n * * (a | p) ≡ 0 if a ≡ 0 (mod p)\n */\nexport function FpLegendre(Fp, n) {\n // We can use 3rd argument as optional cache of this value\n // but seems unneeded for now. The operation is very fast.\n const p1mod2 = (Fp.ORDER - _1n) / _2n;\n const powered = Fp.pow(n, p1mod2);\n const yes = Fp.eql(powered, Fp.ONE);\n const zero = Fp.eql(powered, Fp.ZERO);\n const no = Fp.eql(powered, Fp.neg(Fp.ONE));\n if (!yes && !zero && !no)\n throw new Error('invalid Legendre symbol result');\n return yes ? 1 : zero ? 0 : -1;\n}\n// This function returns True whenever the value x is a square in the field F.\nexport function FpIsSquare(Fp, n) {\n const l = FpLegendre(Fp, n);\n return l === 1;\n}\n// CURVE.n lengths\nexport function nLength(n, nBitLength) {\n // Bit size, byte size of CURVE.n\n if (nBitLength !== undefined)\n anumber(nBitLength);\n const _nBitLength = nBitLength !== undefined ? nBitLength : n.toString(2).length;\n const nByteLength = Math.ceil(_nBitLength / 8);\n return { nBitLength: _nBitLength, nByteLength };\n}\n/**\n * Creates a finite field. Major performance optimizations:\n * * 1. Denormalized operations like mulN instead of mul.\n * * 2. Identical object shape: never add or remove keys.\n * * 3. `Object.freeze`.\n * Fragile: always run a benchmark on a change.\n * Security note: operations don't check 'isValid' for all elements for performance reasons,\n * it is caller responsibility to check this.\n * This is low-level code, please make sure you know what you're doing.\n *\n * Note about field properties:\n * * CHARACTERISTIC p = prime number, number of elements in main subgroup.\n * * ORDER q = similar to cofactor in curves, may be composite `q = p^m`.\n *\n * @param ORDER field order, probably prime, or could be composite\n * @param bitLen how many bits the field consumes\n * @param isLE (default: false) if encoding / decoding should be in little-endian\n * @param redef optional faster redefinitions of sqrt and other methods\n */\nexport function Field(ORDER, bitLenOrOpts, // TODO: use opts only in v2?\nisLE = false, opts = {}) {\n if (ORDER <= _0n)\n throw new Error('invalid field: expected ORDER > 0, got ' + ORDER);\n let _nbitLength = undefined;\n let _sqrt = undefined;\n let modFromBytes = false;\n let allowedLengths = undefined;\n if (typeof bitLenOrOpts === 'object' && bitLenOrOpts != null) {\n if (opts.sqrt || isLE)\n throw new Error('cannot specify opts in two arguments');\n const _opts = bitLenOrOpts;\n if (_opts.BITS)\n _nbitLength = _opts.BITS;\n if (_opts.sqrt)\n _sqrt = _opts.sqrt;\n if (typeof _opts.isLE === 'boolean')\n isLE = _opts.isLE;\n if (typeof _opts.modFromBytes === 'boolean')\n modFromBytes = _opts.modFromBytes;\n allowedLengths = _opts.allowedLengths;\n }\n else {\n if (typeof bitLenOrOpts === 'number')\n _nbitLength = bitLenOrOpts;\n if (opts.sqrt)\n _sqrt = opts.sqrt;\n }\n const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, _nbitLength);\n if (BYTES > 2048)\n throw new Error('invalid field: expected ORDER of <= 2048 bytes');\n let sqrtP; // cached sqrtP\n const f = Object.freeze({\n ORDER,\n isLE,\n BITS,\n BYTES,\n MASK: bitMask(BITS),\n ZERO: _0n,\n ONE: _1n,\n allowedLengths: allowedLengths,\n create: (num) => mod(num, ORDER),\n isValid: (num) => {\n if (typeof num !== 'bigint')\n throw new Error('invalid field element: expected bigint, got ' + typeof num);\n return _0n <= num && num < ORDER; // 0 is valid element, but it's not invertible\n },\n is0: (num) => num === _0n,\n // is valid and invertible\n isValidNot0: (num) => !f.is0(num) && f.isValid(num),\n isOdd: (num) => (num & _1n) === _1n,\n neg: (num) => mod(-num, ORDER),\n eql: (lhs, rhs) => lhs === rhs,\n sqr: (num) => mod(num * num, ORDER),\n add: (lhs, rhs) => mod(lhs + rhs, ORDER),\n sub: (lhs, rhs) => mod(lhs - rhs, ORDER),\n mul: (lhs, rhs) => mod(lhs * rhs, ORDER),\n pow: (num, power) => FpPow(f, num, power),\n div: (lhs, rhs) => mod(lhs * invert(rhs, ORDER), ORDER),\n // Same as above, but doesn't normalize\n sqrN: (num) => num * num,\n addN: (lhs, rhs) => lhs + rhs,\n subN: (lhs, rhs) => lhs - rhs,\n mulN: (lhs, rhs) => lhs * rhs,\n inv: (num) => invert(num, ORDER),\n sqrt: _sqrt ||\n ((n) => {\n if (!sqrtP)\n sqrtP = FpSqrt(ORDER);\n return sqrtP(f, n);\n }),\n toBytes: (num) => (isLE ? numberToBytesLE(num, BYTES) : numberToBytesBE(num, BYTES)),\n fromBytes: (bytes, skipValidation = true) => {\n if (allowedLengths) {\n if (!allowedLengths.includes(bytes.length) || bytes.length > BYTES) {\n throw new Error('Field.fromBytes: expected ' + allowedLengths + ' bytes, got ' + bytes.length);\n }\n const padded = new Uint8Array(BYTES);\n // isLE add 0 to right, !isLE to the left.\n padded.set(bytes, isLE ? 0 : padded.length - bytes.length);\n bytes = padded;\n }\n if (bytes.length !== BYTES)\n throw new Error('Field.fromBytes: expected ' + BYTES + ' bytes, got ' + bytes.length);\n let scalar = isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);\n if (modFromBytes)\n scalar = mod(scalar, ORDER);\n if (!skipValidation)\n if (!f.isValid(scalar))\n throw new Error('invalid field element: outside of range 0..ORDER');\n // NOTE: we don't validate scalar here, please use isValid. This done such way because some\n // protocol may allow non-reduced scalar that reduced later or changed some other way.\n return scalar;\n },\n // TODO: we don't need it here, move out to separate fn\n invertBatch: (lst) => FpInvertBatch(f, lst),\n // We can't move this out because Fp6, Fp12 implement it\n // and it's unclear what to return in there.\n cmov: (a, b, c) => (c ? b : a),\n });\n return Object.freeze(f);\n}\n// Generic random scalar, we can do same for other fields if via Fp2.mul(Fp2.ONE, Fp2.random)?\n// This allows unsafe methods like ignore bias or zero. These unsafe, but often used in different protocols (if deterministic RNG).\n// which mean we cannot force this via opts.\n// Not sure what to do with randomBytes, we can accept it inside opts if wanted.\n// Probably need to export getMinHashLength somewhere?\n// random(bytes?: Uint8Array, unsafeAllowZero = false, unsafeAllowBias = false) {\n// const LEN = !unsafeAllowBias ? getMinHashLength(ORDER) : BYTES;\n// if (bytes === undefined) bytes = randomBytes(LEN); // _opts.randomBytes?\n// const num = isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);\n// // `mod(x, 11)` can sometimes produce 0. `mod(x, 10) + 1` is the same, but no 0\n// const reduced = unsafeAllowZero ? mod(num, ORDER) : mod(num, ORDER - _1n) + _1n;\n// return reduced;\n// },\nexport function FpSqrtOdd(Fp, elm) {\n if (!Fp.isOdd)\n throw new Error(\"Field doesn't have isOdd\");\n const root = Fp.sqrt(elm);\n return Fp.isOdd(root) ? root : Fp.neg(root);\n}\nexport function FpSqrtEven(Fp, elm) {\n if (!Fp.isOdd)\n throw new Error(\"Field doesn't have isOdd\");\n const root = Fp.sqrt(elm);\n return Fp.isOdd(root) ? Fp.neg(root) : root;\n}\n/**\n * \"Constant-time\" private key generation utility.\n * Same as mapKeyToField, but accepts less bytes (40 instead of 48 for 32-byte field).\n * Which makes it slightly more biased, less secure.\n * @deprecated use `mapKeyToField` instead\n */\nexport function hashToPrivateScalar(hash, groupOrder, isLE = false) {\n hash = ensureBytes('privateHash', hash);\n const hashLen = hash.length;\n const minLen = nLength(groupOrder).nByteLength + 8;\n if (minLen < 24 || hashLen < minLen || hashLen > 1024)\n throw new Error('hashToPrivateScalar: expected ' + minLen + '-1024 bytes of input, got ' + hashLen);\n const num = isLE ? bytesToNumberLE(hash) : bytesToNumberBE(hash);\n return mod(num, groupOrder - _1n) + _1n;\n}\n/**\n * Returns total number of bytes consumed by the field element.\n * For example, 32 bytes for usual 256-bit weierstrass curve.\n * @param fieldOrder number of field elements, usually CURVE.n\n * @returns byte length of field\n */\nexport function getFieldBytesLength(fieldOrder) {\n if (typeof fieldOrder !== 'bigint')\n throw new Error('field order must be bigint');\n const bitLength = fieldOrder.toString(2).length;\n return Math.ceil(bitLength / 8);\n}\n/**\n * Returns minimal amount of bytes that can be safely reduced\n * by field order.\n * Should be 2^-128 for 128-bit curve such as P256.\n * @param fieldOrder number of field elements, usually CURVE.n\n * @returns byte length of target hash\n */\nexport function getMinHashLength(fieldOrder) {\n const length = getFieldBytesLength(fieldOrder);\n return length + Math.ceil(length / 2);\n}\n/**\n * \"Constant-time\" private key generation utility.\n * Can take (n + n/2) or more bytes of uniform input e.g. from CSPRNG or KDF\n * and convert them into private scalar, with the modulo bias being negligible.\n * Needs at least 48 bytes of input for 32-byte private key.\n * https://research.kudelskisecurity.com/2020/07/28/the-definitive-guide-to-modulo-bias-and-how-to-avoid-it/\n * FIPS 186-5, A.2 https://csrc.nist.gov/publications/detail/fips/186/5/final\n * RFC 9380, https://www.rfc-editor.org/rfc/rfc9380#section-5\n * @param hash hash output from SHA3 or a similar function\n * @param groupOrder size of subgroup - (e.g. secp256k1.CURVE.n)\n * @param isLE interpret hash bytes as LE num\n * @returns valid private scalar\n */\nexport function mapHashToField(key, fieldOrder, isLE = false) {\n const len = key.length;\n const fieldLen = getFieldBytesLength(fieldOrder);\n const minLen = getMinHashLength(fieldOrder);\n // No small numbers: need to understand bias story. No huge numbers: easier to detect JS timings.\n if (len < 16 || len < minLen || len > 1024)\n throw new Error('expected ' + minLen + '-1024 bytes of input, got ' + len);\n const num = isLE ? bytesToNumberLE(key) : bytesToNumberBE(key);\n // `mod(x, 11)` can sometimes produce 0. `mod(x, 10) + 1` is the same, but no 0\n const reduced = mod(num, fieldOrder - _1n) + _1n;\n return isLE ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);\n}\n//# sourceMappingURL=modular.js.map","/**\n * Montgomery curve methods. It's not really whole montgomery curve,\n * just bunch of very specific methods for X25519 / X448 from\n * [RFC 7748](https://www.rfc-editor.org/rfc/rfc7748)\n * @module\n */\n/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */\nimport { _validateObject, abytes, aInRange, bytesToNumberLE, ensureBytes, numberToBytesLE, randomBytes, } from \"../utils.js\";\nimport { mod } from \"./modular.js\";\nconst _0n = BigInt(0);\nconst _1n = BigInt(1);\nconst _2n = BigInt(2);\nfunction validateOpts(curve) {\n _validateObject(curve, {\n adjustScalarBytes: 'function',\n powPminus2: 'function',\n });\n return Object.freeze({ ...curve });\n}\nexport function montgomery(curveDef) {\n const CURVE = validateOpts(curveDef);\n const { P, type, adjustScalarBytes, powPminus2, randomBytes: rand } = CURVE;\n const is25519 = type === 'x25519';\n if (!is25519 && type !== 'x448')\n throw new Error('invalid type');\n const randomBytes_ = rand || randomBytes;\n const montgomeryBits = is25519 ? 255 : 448;\n const fieldLen = is25519 ? 32 : 56;\n const Gu = is25519 ? BigInt(9) : BigInt(5);\n // RFC 7748 #5:\n // The constant a24 is (486662 - 2) / 4 = 121665 for curve25519/X25519 and\n // (156326 - 2) / 4 = 39081 for curve448/X448\n // const a = is25519 ? 156326n : 486662n;\n const a24 = is25519 ? BigInt(121665) : BigInt(39081);\n // RFC: x25519 \"the resulting integer is of the form 2^254 plus\n // eight times a value between 0 and 2^251 - 1 (inclusive)\"\n // x448: \"2^447 plus four times a value between 0 and 2^445 - 1 (inclusive)\"\n const minScalar = is25519 ? _2n ** BigInt(254) : _2n ** BigInt(447);\n const maxAdded = is25519\n ? BigInt(8) * _2n ** BigInt(251) - _1n\n : BigInt(4) * _2n ** BigInt(445) - _1n;\n const maxScalar = minScalar + maxAdded + _1n; // (inclusive)\n const modP = (n) => mod(n, P);\n const GuBytes = encodeU(Gu);\n function encodeU(u) {\n return numberToBytesLE(modP(u), fieldLen);\n }\n function decodeU(u) {\n const _u = ensureBytes('u coordinate', u, fieldLen);\n // RFC: When receiving such an array, implementations of X25519\n // (but not X448) MUST mask the most significant bit in the final byte.\n if (is25519)\n _u[31] &= 127; // 0b0111_1111\n // RFC: Implementations MUST accept non-canonical values and process them as\n // if they had been reduced modulo the field prime. The non-canonical\n // values are 2^255 - 19 through 2^255 - 1 for X25519 and 2^448 - 2^224\n // - 1 through 2^448 - 1 for X448.\n return modP(bytesToNumberLE(_u));\n }\n function decodeScalar(scalar) {\n return bytesToNumberLE(adjustScalarBytes(ensureBytes('scalar', scalar, fieldLen)));\n }\n function scalarMult(scalar, u) {\n const pu = montgomeryLadder(decodeU(u), decodeScalar(scalar));\n // Some public keys are useless, of low-order. Curve author doesn't think\n // it needs to be validated, but we do it nonetheless.\n // https://cr.yp.to/ecdh.html#validate\n if (pu === _0n)\n throw new Error('invalid private or public key received');\n return encodeU(pu);\n }\n // Computes public key from private. By doing scalar multiplication of base point.\n function scalarMultBase(scalar) {\n return scalarMult(scalar, GuBytes);\n }\n // cswap from RFC7748 \"example code\"\n function cswap(swap, x_2, x_3) {\n // dummy = mask(swap) AND (x_2 XOR x_3)\n // Where mask(swap) is the all-1 or all-0 word of the same length as x_2\n // and x_3, computed, e.g., as mask(swap) = 0 - swap.\n const dummy = modP(swap * (x_2 - x_3));\n x_2 = modP(x_2 - dummy); // x_2 = x_2 XOR dummy\n x_3 = modP(x_3 + dummy); // x_3 = x_3 XOR dummy\n return { x_2, x_3 };\n }\n /**\n * Montgomery x-only multiplication ladder.\n * @param pointU u coordinate (x) on Montgomery Curve 25519\n * @param scalar by which the point would be multiplied\n * @returns new Point on Montgomery curve\n */\n function montgomeryLadder(u, scalar) {\n aInRange('u', u, _0n, P);\n aInRange('scalar', scalar, minScalar, maxScalar);\n const k = scalar;\n const x_1 = u;\n let x_2 = _1n;\n let z_2 = _0n;\n let x_3 = u;\n let z_3 = _1n;\n let swap = _0n;\n for (let t = BigInt(montgomeryBits - 1); t >= _0n; t--) {\n const k_t = (k >> t) & _1n;\n swap ^= k_t;\n ({ x_2, x_3 } = cswap(swap, x_2, x_3));\n ({ x_2: z_2, x_3: z_3 } = cswap(swap, z_2, z_3));\n swap = k_t;\n const A = x_2 + z_2;\n const AA = modP(A * A);\n const B = x_2 - z_2;\n const BB = modP(B * B);\n const E = AA - BB;\n const C = x_3 + z_3;\n const D = x_3 - z_3;\n const DA = modP(D * A);\n const CB = modP(C * B);\n const dacb = DA + CB;\n const da_cb = DA - CB;\n x_3 = modP(dacb * dacb);\n z_3 = modP(x_1 * modP(da_cb * da_cb));\n x_2 = modP(AA * BB);\n z_2 = modP(E * (AA + modP(a24 * E)));\n }\n ({ x_2, x_3 } = cswap(swap, x_2, x_3));\n ({ x_2: z_2, x_3: z_3 } = cswap(swap, z_2, z_3));\n const z2 = powPminus2(z_2); // `Fp.pow(x, P - _2n)` is much slower equivalent\n return modP(x_2 * z2); // Return x_2 * (z_2^(p - 2))\n }\n const lengths = {\n secretKey: fieldLen,\n publicKey: fieldLen,\n seed: fieldLen,\n };\n const randomSecretKey = (seed = randomBytes_(fieldLen)) => {\n abytes(seed, lengths.seed);\n return seed;\n };\n function keygen(seed) {\n const secretKey = randomSecretKey(seed);\n return { secretKey, publicKey: scalarMultBase(secretKey) };\n }\n const utils = {\n randomSecretKey,\n randomPrivateKey: randomSecretKey,\n };\n return {\n keygen,\n getSharedSecret: (secretKey, publicKey) => scalarMult(secretKey, publicKey),\n getPublicKey: (secretKey) => scalarMultBase(secretKey),\n scalarMult,\n scalarMultBase,\n utils,\n GuBytes: GuBytes.slice(),\n lengths,\n };\n}\n//# sourceMappingURL=montgomery.js.map","/**\n * ed25519 Twisted Edwards curve with following addons:\n * - X25519 ECDH\n * - Ristretto cofactor elimination\n * - Elligator hash-to-group / point indistinguishability\n * @module\n */\n/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */\nimport { sha512 } from '@noble/hashes/sha2.js';\nimport { abytes, concatBytes, utf8ToBytes } from '@noble/hashes/utils.js';\nimport { pippenger } from \"./abstract/curve.js\";\nimport { PrimeEdwardsPoint, twistedEdwards, } from \"./abstract/edwards.js\";\nimport { _DST_scalar, createHasher, expand_message_xmd, } from \"./abstract/hash-to-curve.js\";\nimport { Field, FpInvertBatch, FpSqrtEven, isNegativeLE, mod, pow2, } from \"./abstract/modular.js\";\nimport { montgomery } from \"./abstract/montgomery.js\";\nimport { bytesToNumberLE, ensureBytes, equalBytes } from \"./utils.js\";\n// prettier-ignore\nconst _0n = /* @__PURE__ */ BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3);\n// prettier-ignore\nconst _5n = BigInt(5), _8n = BigInt(8);\n// P = 2n**255n-19n\nconst ed25519_CURVE_p = BigInt('0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed');\n// N = 2n**252n + 27742317777372353535851937790883648493n\n// a = Fp.create(BigInt(-1))\n// d = -121665/121666 a.k.a. Fp.neg(121665 * Fp.inv(121666))\nconst ed25519_CURVE = /* @__PURE__ */ (() => ({\n p: ed25519_CURVE_p,\n n: BigInt('0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed'),\n h: _8n,\n a: BigInt('0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec'),\n d: BigInt('0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3'),\n Gx: BigInt('0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a'),\n Gy: BigInt('0x6666666666666666666666666666666666666666666666666666666666666658'),\n}))();\nfunction ed25519_pow_2_252_3(x) {\n // prettier-ignore\n const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);\n const P = ed25519_CURVE_p;\n const x2 = (x * x) % P;\n const b2 = (x2 * x) % P; // x^3, 11\n const b4 = (pow2(b2, _2n, P) * b2) % P; // x^15, 1111\n const b5 = (pow2(b4, _1n, P) * x) % P; // x^31\n const b10 = (pow2(b5, _5n, P) * b5) % P;\n const b20 = (pow2(b10, _10n, P) * b10) % P;\n const b40 = (pow2(b20, _20n, P) * b20) % P;\n const b80 = (pow2(b40, _40n, P) * b40) % P;\n const b160 = (pow2(b80, _80n, P) * b80) % P;\n const b240 = (pow2(b160, _80n, P) * b80) % P;\n const b250 = (pow2(b240, _10n, P) * b10) % P;\n const pow_p_5_8 = (pow2(b250, _2n, P) * x) % P;\n // ^ To pow to (p+3)/8, multiply it by x.\n return { pow_p_5_8, b2 };\n}\nfunction adjustScalarBytes(bytes) {\n // Section 5: For X25519, in order to decode 32 random bytes as an integer scalar,\n // set the three least significant bits of the first byte\n bytes[0] &= 248; // 0b1111_1000\n // and the most significant bit of the last to zero,\n bytes[31] &= 127; // 0b0111_1111\n // set the second most significant bit of the last byte to 1\n bytes[31] |= 64; // 0b0100_0000\n return bytes;\n}\n// √(-1) aka √(a) aka 2^((p-1)/4)\n// Fp.sqrt(Fp.neg(1))\nconst ED25519_SQRT_M1 = /* @__PURE__ */ BigInt('19681161376707505956807079304988542015446066515923890162744021073123829784752');\n// sqrt(u/v)\nfunction uvRatio(u, v) {\n const P = ed25519_CURVE_p;\n const v3 = mod(v * v * v, P); // v³\n const v7 = mod(v3 * v3 * v, P); // v⁷\n // (p+3)/8 and (p-5)/8\n const pow = ed25519_pow_2_252_3(u * v7).pow_p_5_8;\n let x = mod(u * v3 * pow, P); // (uv³)(uv⁷)^(p-5)/8\n const vx2 = mod(v * x * x, P); // vx²\n const root1 = x; // First root candidate\n const root2 = mod(x * ED25519_SQRT_M1, P); // Second root candidate\n const useRoot1 = vx2 === u; // If vx² = u (mod p), x is a square root\n const useRoot2 = vx2 === mod(-u, P); // If vx² = -u, set x <-- x * 2^((p-1)/4)\n const noRoot = vx2 === mod(-u * ED25519_SQRT_M1, P); // There is no valid root, vx² = -u√(-1)\n if (useRoot1)\n x = root1;\n if (useRoot2 || noRoot)\n x = root2; // We return root2 anyway, for const-time\n if (isNegativeLE(x, P))\n x = mod(-x, P);\n return { isValid: useRoot1 || useRoot2, value: x };\n}\nconst Fp = /* @__PURE__ */ (() => Field(ed25519_CURVE.p, { isLE: true }))();\nconst Fn = /* @__PURE__ */ (() => Field(ed25519_CURVE.n, { isLE: true }))();\nconst ed25519Defaults = /* @__PURE__ */ (() => ({\n ...ed25519_CURVE,\n Fp,\n hash: sha512,\n adjustScalarBytes,\n // dom2\n // Ratio of u to v. Allows us to combine inversion and square root. Uses algo from RFC8032 5.1.3.\n // Constant-time, u/√v\n uvRatio,\n}))();\n/**\n * ed25519 curve with EdDSA signatures.\n * @example\n * import { ed25519 } from '@noble/curves/ed25519';\n * const { secretKey, publicKey } = ed25519.keygen();\n * const msg = new TextEncoder().encode('hello');\n * const sig = ed25519.sign(msg, priv);\n * ed25519.verify(sig, msg, pub); // Default mode: follows ZIP215\n * ed25519.verify(sig, msg, pub, { zip215: false }); // RFC8032 / FIPS 186-5\n */\nexport const ed25519 = /* @__PURE__ */ (() => twistedEdwards(ed25519Defaults))();\nfunction ed25519_domain(data, ctx, phflag) {\n if (ctx.length > 255)\n throw new Error('Context is too big');\n return concatBytes(utf8ToBytes('SigEd25519 no Ed25519 collisions'), new Uint8Array([phflag ? 1 : 0, ctx.length]), ctx, data);\n}\n/** Context of ed25519. Uses context for domain separation. */\nexport const ed25519ctx = /* @__PURE__ */ (() => twistedEdwards({\n ...ed25519Defaults,\n domain: ed25519_domain,\n}))();\n/** Prehashed version of ed25519. Accepts already-hashed messages in sign() and verify(). */\nexport const ed25519ph = /* @__PURE__ */ (() => twistedEdwards(Object.assign({}, ed25519Defaults, {\n domain: ed25519_domain,\n prehash: sha512,\n})))();\n/**\n * ECDH using curve25519 aka x25519.\n * @example\n * import { x25519 } from '@noble/curves/ed25519';\n * const priv = 'a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4';\n * const pub = 'e6db6867583030db3594c1a424b15f7c726624ec26b3353b10a903a6d0ab1c4c';\n * x25519.getSharedSecret(priv, pub) === x25519.scalarMult(priv, pub); // aliases\n * x25519.getPublicKey(priv) === x25519.scalarMultBase(priv);\n * x25519.getPublicKey(x25519.utils.randomSecretKey());\n */\nexport const x25519 = /* @__PURE__ */ (() => {\n const P = Fp.ORDER;\n return montgomery({\n P,\n type: 'x25519',\n powPminus2: (x) => {\n // x^(p-2) aka x^(2^255-21)\n const { pow_p_5_8, b2 } = ed25519_pow_2_252_3(x);\n return mod(pow2(pow_p_5_8, _3n, P) * b2, P);\n },\n adjustScalarBytes,\n });\n})();\n// Hash To Curve Elligator2 Map (NOTE: different from ristretto255 elligator)\n// NOTE: very important part is usage of FpSqrtEven for ELL2_C1_EDWARDS, since\n// SageMath returns different root first and everything falls apart\nconst ELL2_C1 = /* @__PURE__ */ (() => (ed25519_CURVE_p + _3n) / _8n)(); // 1. c1 = (q + 3) / 8 # Integer arithmetic\nconst ELL2_C2 = /* @__PURE__ */ (() => Fp.pow(_2n, ELL2_C1))(); // 2. c2 = 2^c1\nconst ELL2_C3 = /* @__PURE__ */ (() => Fp.sqrt(Fp.neg(Fp.ONE)))(); // 3. c3 = sqrt(-1)\n// prettier-ignore\nfunction map_to_curve_elligator2_curve25519(u) {\n const ELL2_C4 = (ed25519_CURVE_p - _5n) / _8n; // 4. c4 = (q - 5) / 8 # Integer arithmetic\n const ELL2_J = BigInt(486662);\n let tv1 = Fp.sqr(u); // 1. tv1 = u^2\n tv1 = Fp.mul(tv1, _2n); // 2. tv1 = 2 * tv1\n let xd = Fp.add(tv1, Fp.ONE); // 3. xd = tv1 + 1 # Nonzero: -1 is square (mod p), tv1 is not\n let x1n = Fp.neg(ELL2_J); // 4. x1n = -J # x1 = x1n / xd = -J / (1 + 2 * u^2)\n let tv2 = Fp.sqr(xd); // 5. tv2 = xd^2\n let gxd = Fp.mul(tv2, xd); // 6. gxd = tv2 * xd # gxd = xd^3\n let gx1 = Fp.mul(tv1, ELL2_J); // 7. gx1 = J * tv1 # x1n + J * xd\n gx1 = Fp.mul(gx1, x1n); // 8. gx1 = gx1 * x1n # x1n^2 + J * x1n * xd\n gx1 = Fp.add(gx1, tv2); // 9. gx1 = gx1 + tv2 # x1n^2 + J * x1n * xd + xd^2\n gx1 = Fp.mul(gx1, x1n); // 10. gx1 = gx1 * x1n # x1n^3 + J * x1n^2 * xd + x1n * xd^2\n let tv3 = Fp.sqr(gxd); // 11. tv3 = gxd^2\n tv2 = Fp.sqr(tv3); // 12. tv2 = tv3^2 # gxd^4\n tv3 = Fp.mul(tv3, gxd); // 13. tv3 = tv3 * gxd # gxd^3\n tv3 = Fp.mul(tv3, gx1); // 14. tv3 = tv3 * gx1 # gx1 * gxd^3\n tv2 = Fp.mul(tv2, tv3); // 15. tv2 = tv2 * tv3 # gx1 * gxd^7\n let y11 = Fp.pow(tv2, ELL2_C4); // 16. y11 = tv2^c4 # (gx1 * gxd^7)^((p - 5) / 8)\n y11 = Fp.mul(y11, tv3); // 17. y11 = y11 * tv3 # gx1*gxd^3*(gx1*gxd^7)^((p-5)/8)\n let y12 = Fp.mul(y11, ELL2_C3); // 18. y12 = y11 * c3\n tv2 = Fp.sqr(y11); // 19. tv2 = y11^2\n tv2 = Fp.mul(tv2, gxd); // 20. tv2 = tv2 * gxd\n let e1 = Fp.eql(tv2, gx1); // 21. e1 = tv2 == gx1\n let y1 = Fp.cmov(y12, y11, e1); // 22. y1 = CMOV(y12, y11, e1) # If g(x1) is square, this is its sqrt\n let x2n = Fp.mul(x1n, tv1); // 23. x2n = x1n * tv1 # x2 = x2n / xd = 2 * u^2 * x1n / xd\n let y21 = Fp.mul(y11, u); // 24. y21 = y11 * u\n y21 = Fp.mul(y21, ELL2_C2); // 25. y21 = y21 * c2\n let y22 = Fp.mul(y21, ELL2_C3); // 26. y22 = y21 * c3\n let gx2 = Fp.mul(gx1, tv1); // 27. gx2 = gx1 * tv1 # g(x2) = gx2 / gxd = 2 * u^2 * g(x1)\n tv2 = Fp.sqr(y21); // 28. tv2 = y21^2\n tv2 = Fp.mul(tv2, gxd); // 29. tv2 = tv2 * gxd\n let e2 = Fp.eql(tv2, gx2); // 30. e2 = tv2 == gx2\n let y2 = Fp.cmov(y22, y21, e2); // 31. y2 = CMOV(y22, y21, e2) # If g(x2) is square, this is its sqrt\n tv2 = Fp.sqr(y1); // 32. tv2 = y1^2\n tv2 = Fp.mul(tv2, gxd); // 33. tv2 = tv2 * gxd\n let e3 = Fp.eql(tv2, gx1); // 34. e3 = tv2 == gx1\n let xn = Fp.cmov(x2n, x1n, e3); // 35. xn = CMOV(x2n, x1n, e3) # If e3, x = x1, else x = x2\n let y = Fp.cmov(y2, y1, e3); // 36. y = CMOV(y2, y1, e3) # If e3, y = y1, else y = y2\n let e4 = Fp.isOdd(y); // 37. e4 = sgn0(y) == 1 # Fix sign of y\n y = Fp.cmov(y, Fp.neg(y), e3 !== e4); // 38. y = CMOV(y, -y, e3 XOR e4)\n return { xMn: xn, xMd: xd, yMn: y, yMd: _1n }; // 39. return (xn, xd, y, 1)\n}\nconst ELL2_C1_EDWARDS = /* @__PURE__ */ (() => FpSqrtEven(Fp, Fp.neg(BigInt(486664))))(); // sgn0(c1) MUST equal 0\nfunction map_to_curve_elligator2_edwards25519(u) {\n const { xMn, xMd, yMn, yMd } = map_to_curve_elligator2_curve25519(u); // 1. (xMn, xMd, yMn, yMd) =\n // map_to_curve_elligator2_curve25519(u)\n let xn = Fp.mul(xMn, yMd); // 2. xn = xMn * yMd\n xn = Fp.mul(xn, ELL2_C1_EDWARDS); // 3. xn = xn * c1\n let xd = Fp.mul(xMd, yMn); // 4. xd = xMd * yMn # xn / xd = c1 * xM / yM\n let yn = Fp.sub(xMn, xMd); // 5. yn = xMn - xMd\n let yd = Fp.add(xMn, xMd); // 6. yd = xMn + xMd # (n / d - 1) / (n / d + 1) = (n - d) / (n + d)\n let tv1 = Fp.mul(xd, yd); // 7. tv1 = xd * yd\n let e = Fp.eql(tv1, Fp.ZERO); // 8. e = tv1 == 0\n xn = Fp.cmov(xn, Fp.ZERO, e); // 9. xn = CMOV(xn, 0, e)\n xd = Fp.cmov(xd, Fp.ONE, e); // 10. xd = CMOV(xd, 1, e)\n yn = Fp.cmov(yn, Fp.ONE, e); // 11. yn = CMOV(yn, 1, e)\n yd = Fp.cmov(yd, Fp.ONE, e); // 12. yd = CMOV(yd, 1, e)\n const [xd_inv, yd_inv] = FpInvertBatch(Fp, [xd, yd], true); // batch division\n return { x: Fp.mul(xn, xd_inv), y: Fp.mul(yn, yd_inv) }; // 13. return (xn, xd, yn, yd)\n}\n/** Hashing to ed25519 points / field. RFC 9380 methods. */\nexport const ed25519_hasher = /* @__PURE__ */ (() => createHasher(ed25519.Point, (scalars) => map_to_curve_elligator2_edwards25519(scalars[0]), {\n DST: 'edwards25519_XMD:SHA-512_ELL2_RO_',\n encodeDST: 'edwards25519_XMD:SHA-512_ELL2_NU_',\n p: ed25519_CURVE_p,\n m: 1,\n k: 128,\n expand: 'xmd',\n hash: sha512,\n}))();\n// √(-1) aka √(a) aka 2^((p-1)/4)\nconst SQRT_M1 = ED25519_SQRT_M1;\n// √(ad - 1)\nconst SQRT_AD_MINUS_ONE = /* @__PURE__ */ BigInt('25063068953384623474111414158702152701244531502492656460079210482610430750235');\n// 1 / √(a-d)\nconst INVSQRT_A_MINUS_D = /* @__PURE__ */ BigInt('54469307008909316920995813868745141605393597292927456921205312896311721017578');\n// 1-d²\nconst ONE_MINUS_D_SQ = /* @__PURE__ */ BigInt('1159843021668779879193775521855586647937357759715417654439879720876111806838');\n// (d-1)²\nconst D_MINUS_ONE_SQ = /* @__PURE__ */ BigInt('40440834346308536858101042469323190826248399146238708352240133220865137265952');\n// Calculates 1/√(number)\nconst invertSqrt = (number) => uvRatio(_1n, number);\nconst MAX_255B = /* @__PURE__ */ BigInt('0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff');\nconst bytes255ToNumberLE = (bytes) => ed25519.Point.Fp.create(bytesToNumberLE(bytes) & MAX_255B);\n/**\n * Computes Elligator map for Ristretto255.\n * Described in [RFC9380](https://www.rfc-editor.org/rfc/rfc9380#appendix-B) and on\n * the [website](https://ristretto.group/formulas/elligator.html).\n */\nfunction calcElligatorRistrettoMap(r0) {\n const { d } = ed25519_CURVE;\n const P = ed25519_CURVE_p;\n const mod = (n) => Fp.create(n);\n const r = mod(SQRT_M1 * r0 * r0); // 1\n const Ns = mod((r + _1n) * ONE_MINUS_D_SQ); // 2\n let c = BigInt(-1); // 3\n const D = mod((c - d * r) * mod(r + d)); // 4\n let { isValid: Ns_D_is_sq, value: s } = uvRatio(Ns, D); // 5\n let s_ = mod(s * r0); // 6\n if (!isNegativeLE(s_, P))\n s_ = mod(-s_);\n if (!Ns_D_is_sq)\n s = s_; // 7\n if (!Ns_D_is_sq)\n c = r; // 8\n const Nt = mod(c * (r - _1n) * D_MINUS_ONE_SQ - D); // 9\n const s2 = s * s;\n const W0 = mod((s + s) * D); // 10\n const W1 = mod(Nt * SQRT_AD_MINUS_ONE); // 11\n const W2 = mod(_1n - s2); // 12\n const W3 = mod(_1n + s2); // 13\n return new ed25519.Point(mod(W0 * W3), mod(W2 * W1), mod(W1 * W3), mod(W0 * W2));\n}\nfunction ristretto255_map(bytes) {\n abytes(bytes, 64);\n const r1 = bytes255ToNumberLE(bytes.subarray(0, 32));\n const R1 = calcElligatorRistrettoMap(r1);\n const r2 = bytes255ToNumberLE(bytes.subarray(32, 64));\n const R2 = calcElligatorRistrettoMap(r2);\n return new _RistrettoPoint(R1.add(R2));\n}\n/**\n * Wrapper over Edwards Point for ristretto255.\n *\n * Each ed25519/ExtendedPoint has 8 different equivalent points. This can be\n * a source of bugs for protocols like ring signatures. Ristretto was created to solve this.\n * Ristretto point operates in X:Y:Z:T extended coordinates like ExtendedPoint,\n * but it should work in its own namespace: do not combine those two.\n * See [RFC9496](https://www.rfc-editor.org/rfc/rfc9496).\n */\nclass _RistrettoPoint extends PrimeEdwardsPoint {\n constructor(ep) {\n super(ep);\n }\n static fromAffine(ap) {\n return new _RistrettoPoint(ed25519.Point.fromAffine(ap));\n }\n assertSame(other) {\n if (!(other instanceof _RistrettoPoint))\n throw new Error('RistrettoPoint expected');\n }\n init(ep) {\n return new _RistrettoPoint(ep);\n }\n /** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */\n static hashToCurve(hex) {\n return ristretto255_map(ensureBytes('ristrettoHash', hex, 64));\n }\n static fromBytes(bytes) {\n abytes(bytes, 32);\n const { a, d } = ed25519_CURVE;\n const P = ed25519_CURVE_p;\n const mod = (n) => Fp.create(n);\n const s = bytes255ToNumberLE(bytes);\n // 1. Check that s_bytes is the canonical encoding of a field element, or else abort.\n // 3. Check that s is non-negative, or else abort\n if (!equalBytes(Fp.toBytes(s), bytes) || isNegativeLE(s, P))\n throw new Error('invalid ristretto255 encoding 1');\n const s2 = mod(s * s);\n const u1 = mod(_1n + a * s2); // 4 (a is -1)\n const u2 = mod(_1n - a * s2); // 5\n const u1_2 = mod(u1 * u1);\n const u2_2 = mod(u2 * u2);\n const v = mod(a * d * u1_2 - u2_2); // 6\n const { isValid, value: I } = invertSqrt(mod(v * u2_2)); // 7\n const Dx = mod(I * u2); // 8\n const Dy = mod(I * Dx * v); // 9\n let x = mod((s + s) * Dx); // 10\n if (isNegativeLE(x, P))\n x = mod(-x); // 10\n const y = mod(u1 * Dy); // 11\n const t = mod(x * y); // 12\n if (!isValid || isNegativeLE(t, P) || y === _0n)\n throw new Error('invalid ristretto255 encoding 2');\n return new _RistrettoPoint(new ed25519.Point(x, y, _1n, t));\n }\n /**\n * Converts ristretto-encoded string to ristretto point.\n * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).\n * @param hex Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding\n */\n static fromHex(hex) {\n return _RistrettoPoint.fromBytes(ensureBytes('ristrettoHex', hex, 32));\n }\n static msm(points, scalars) {\n return pippenger(_RistrettoPoint, ed25519.Point.Fn, points, scalars);\n }\n /**\n * Encodes ristretto point to Uint8Array.\n * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).\n */\n toBytes() {\n let { X, Y, Z, T } = this.ep;\n const P = ed25519_CURVE_p;\n const mod = (n) => Fp.create(n);\n const u1 = mod(mod(Z + Y) * mod(Z - Y)); // 1\n const u2 = mod(X * Y); // 2\n // Square root always exists\n const u2sq = mod(u2 * u2);\n const { value: invsqrt } = invertSqrt(mod(u1 * u2sq)); // 3\n const D1 = mod(invsqrt * u1); // 4\n const D2 = mod(invsqrt * u2); // 5\n const zInv = mod(D1 * D2 * T); // 6\n let D; // 7\n if (isNegativeLE(T * zInv, P)) {\n let _x = mod(Y * SQRT_M1);\n let _y = mod(X * SQRT_M1);\n X = _x;\n Y = _y;\n D = mod(D1 * INVSQRT_A_MINUS_D);\n }\n else {\n D = D2; // 8\n }\n if (isNegativeLE(X * zInv, P))\n Y = mod(-Y); // 9\n let s = mod((Z - Y) * D); // 10 (check footer's note, no sqrt(-a))\n if (isNegativeLE(s, P))\n s = mod(-s);\n return Fp.toBytes(s); // 11\n }\n /**\n * Compares two Ristretto points.\n * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).\n */\n equals(other) {\n this.assertSame(other);\n const { X: X1, Y: Y1 } = this.ep;\n const { X: X2, Y: Y2 } = other.ep;\n const mod = (n) => Fp.create(n);\n // (x1 * y2 == y1 * x2) | (y1 * y2 == x1 * x2)\n const one = mod(X1 * Y2) === mod(Y1 * X2);\n const two = mod(Y1 * Y2) === mod(X1 * X2);\n return one || two;\n }\n is0() {\n return this.equals(_RistrettoPoint.ZERO);\n }\n}\n// Do NOT change syntax: the following gymnastics is done,\n// because typescript strips comments, which makes bundlers disable tree-shaking.\n// prettier-ignore\n_RistrettoPoint.BASE = \n/* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.BASE))();\n// prettier-ignore\n_RistrettoPoint.ZERO = \n/* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.ZERO))();\n// prettier-ignore\n_RistrettoPoint.Fp = \n/* @__PURE__ */ (() => Fp)();\n// prettier-ignore\n_RistrettoPoint.Fn = \n/* @__PURE__ */ (() => Fn)();\nexport const ristretto255 = { Point: _RistrettoPoint };\n/** Hashing to ristretto255 points / field. RFC 9380 methods. */\nexport const ristretto255_hasher = {\n hashToCurve(msg, options) {\n const DST = options?.DST || 'ristretto255_XMD:SHA-512_R255MAP_RO_';\n const xmd = expand_message_xmd(msg, DST, 64, sha512);\n return ristretto255_map(xmd);\n },\n hashToScalar(msg, options = { DST: _DST_scalar }) {\n const xmd = expand_message_xmd(msg, options.DST, 64, sha512);\n return Fn.create(bytesToNumberLE(xmd));\n },\n};\n// export const ristretto255_oprf: OPRF = createORPF({\n// name: 'ristretto255-SHA512',\n// Point: RistrettoPoint,\n// hash: sha512,\n// hashToGroup: ristretto255_hasher.hashToCurve,\n// hashToScalar: ristretto255_hasher.hashToScalar,\n// });\n/**\n * Weird / bogus points, useful for debugging.\n * All 8 ed25519 points of 8-torsion subgroup can be generated from the point\n * T = `26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05`.\n * ⟨T⟩ = { O, T, 2T, 3T, 4T, 5T, 6T, 7T }\n */\nexport const ED25519_TORSION_SUBGROUP = [\n '0100000000000000000000000000000000000000000000000000000000000000',\n 'c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a',\n '0000000000000000000000000000000000000000000000000000000000000080',\n '26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05',\n 'ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f',\n '26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc85',\n '0000000000000000000000000000000000000000000000000000000000000000',\n 'c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa',\n];\n/** @deprecated use `ed25519.utils.toMontgomery` */\nexport function edwardsToMontgomeryPub(edwardsPub) {\n return ed25519.utils.toMontgomery(ensureBytes('pub', edwardsPub));\n}\n/** @deprecated use `ed25519.utils.toMontgomery` */\nexport const edwardsToMontgomery = edwardsToMontgomeryPub;\n/** @deprecated use `ed25519.utils.toMontgomerySecret` */\nexport function edwardsToMontgomeryPriv(edwardsPriv) {\n return ed25519.utils.toMontgomerySecret(ensureBytes('pub', edwardsPriv));\n}\n/** @deprecated use `ristretto255.Point` */\nexport const RistrettoPoint = _RistrettoPoint;\n/** @deprecated use `import { ed25519_hasher } from '@noble/curves/ed25519.js';` */\nexport const hashToCurve = /* @__PURE__ */ (() => ed25519_hasher.hashToCurve)();\n/** @deprecated use `import { ed25519_hasher } from '@noble/curves/ed25519.js';` */\nexport const encodeToCurve = /* @__PURE__ */ (() => ed25519_hasher.encodeToCurve)();\n/** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */\nexport const hashToRistretto255 = /* @__PURE__ */ (() => ristretto255_hasher.hashToCurve)();\n/** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */\nexport const hash_to_ristretto255 = /* @__PURE__ */ (() => ristretto255_hasher.hashToCurve)();\n//# sourceMappingURL=ed25519.js.map","/**\n * x25519 key exchange for Meshtastic direct messages\n */\n\nimport { x25519 } from '@noble/curves/ed25519';\nimport { InvalidKeyError } from '../utils/error-types';\n\n/**\n * Key pair for x25519\n */\nexport interface KeyPair {\n publicKey: Buffer;\n privateKey: Buffer;\n}\n\n/**\n * Generate a new x25519 key pair\n * @returns Key pair with public and private keys\n */\nexport function generateKeyPair(): KeyPair {\n const privateKey = x25519.utils.randomPrivateKey();\n const publicKey = x25519.getPublicKey(privateKey);\n\n return {\n publicKey: Buffer.from(publicKey),\n privateKey: Buffer.from(privateKey),\n };\n}\n\n/**\n * Derive a shared secret using x25519\n *\n * @param myPrivateKey - My private key (32 bytes)\n * @param theirPublicKey - Their public key (32 bytes)\n * @returns Shared secret (32 bytes)\n * @throws {InvalidKeyError} If keys are invalid\n */\nexport function deriveSharedSecret(\n myPrivateKey: Buffer | Uint8Array,\n theirPublicKey: Buffer | Uint8Array\n): Buffer {\n try {\n // Validate key lengths\n if (myPrivateKey.length !== 32) {\n throw new InvalidKeyError(`Private key must be 32 bytes, got ${myPrivateKey.length}`);\n }\n\n if (theirPublicKey.length !== 32) {\n throw new InvalidKeyError(`Public key must be 32 bytes, got ${theirPublicKey.length}`);\n }\n\n // Derive shared secret\n const sharedSecret = x25519.getSharedSecret(\n myPrivateKey instanceof Buffer ? new Uint8Array(myPrivateKey) : myPrivateKey,\n theirPublicKey instanceof Buffer ? new Uint8Array(theirPublicKey) : theirPublicKey\n );\n\n return Buffer.from(sharedSecret);\n } catch (error) {\n throw new InvalidKeyError(\n `Failed to derive shared secret: ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n}\n\n/**\n * Verify that a public key is valid\n * @param publicKey - Public key to verify\n * @returns true if valid\n * @throws {InvalidKeyError} If key is invalid\n */\nexport function verifyPublicKey(publicKey: Buffer | Uint8Array): boolean {\n if (publicKey.length !== 32) {\n throw new InvalidKeyError(`Public key must be 32 bytes, got ${publicKey.length}`);\n }\n\n // All 32-byte values are technically valid x25519 public keys\n // Additional validation could check for low-order points, but\n // the library handles this internally\n return true;\n}\n\n/**\n * Convert a public key to base64 string\n * @param publicKey - Public key buffer\n * @returns Base64-encoded public key\n */\nexport function publicKeyToBase64(publicKey: Buffer): string {\n return publicKey.toString('base64');\n}\n\n/**\n * Convert a private key to base64 string\n * @param privateKey - Private key buffer\n * @returns Base64-encoded private key\n */\nexport function privateKeyToBase64(privateKey: Buffer): string {\n return privateKey.toString('base64');\n}\n\n/**\n * Convert a public key to hex string\n * @param publicKey - Public key buffer\n * @returns Hex-encoded public key\n */\nexport function publicKeyToHex(publicKey: Buffer): string {\n return publicKey.toString('hex');\n}\n\n/**\n * Convert a private key to hex string\n * @param privateKey - Private key buffer\n * @returns Hex-encoded private key\n */\nexport function privateKeyToHex(privateKey: Buffer): string {\n return privateKey.toString('hex');\n}\n"],"names":["InvalidNonceError","InvalidKeyError","createCipheriv","MeshtasticEncryptError","createDecipheriv","MeshtasticDecryptError","_0n","_1n","bytesToHex_","abytes_","hexToBytes_","isBytes_","_2n","_3n","_5n","_8n","Fp","adjustScalarBytes"],"mappings":";;;AAoBO,SAAS,sBAAsB,SAAiB,UAA0B;AAC/E,MAAI,CAAC,OAAO,UAAU,OAAO,KAAK,UAAU,KAAK,UAAU,YAAY;AACrE,UAAM,IAAIA,QAAAA,kBAAkB,wBAAwB,OAAO,mBAAmB;AAAA,EAChF;AAEA,MAAI,CAAC,OAAO,UAAU,QAAQ,KAAK,WAAW,KAAK,WAAW,YAAY;AACxE,UAAM,IAAIA,QAAAA,kBAAkB,sBAAsB,QAAQ,mBAAmB;AAAA,EAC/E;AAEA,QAAM,KAAK,OAAO,MAAM,EAAE;AAG1B,KAAG,cAAc,SAAS,CAAC;AAG3B,KAAG,cAAc,UAAU,CAAC;AAI5B,SAAO;AACT;AAyBO,SAAS,cAAc,OAAe,gBAA+B;AAC1E,MAAI,CAAC,OAAO,SAAS,KAAK,GAAG;AAC3B,UAAM,IAAIA,QAAAA,kBAAkB,wBAAwB;AAAA,EACtD;AAOF;AChEO,MAAM,qBAAqB;AAS3B,SAAS,SAAS,KAAa,SAAoB,UAAkB;AAC1E,MAAI,CAAC,OAAO,IAAI,KAAA,EAAO,WAAW,GAAG;AACnC,UAAM,IAAIC,QAAAA,gBAAgB,qBAAqB;AAAA,EACjD;AAEA,MAAI;AAEJ,MAAI;AACF,QAAI,WAAW,UAAU;AACvB,kBAAY,OAAO,KAAK,KAAK,QAAQ;AAAA,IACvC,WAAW,WAAW,OAAO;AAC3B,kBAAY,OAAO,KAAK,KAAK,KAAK;AAAA,IACpC,OAAO;AACL,YAAM,IAAIA,QAAAA,gBAAgB,2BAA2B,MAAM,EAAE;AAAA,IAC/D;AAAA,EACF,SAAS,OAAO;AACd,UAAM,IAAIA,QAAAA;AAAAA,MACR,0BAA0B,MAAM,KAAK,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK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 <!-- Meshtastic-style mesh network icon -->
3
+ <defs>
4
+ <style>
5
+ .mesh-line { stroke: #67EA94; stroke-width: 2; fill: none; }
6
+ .mesh-node { fill: #67EA94; }
7
+ </style>
8
+ </defs>
9
+
10
+ <!-- Mesh network lines -->
11
+ <line x1="8" y1="8" x2="20" y2="20" class="mesh-line"/>
12
+ <line x1="32" y1="8" x2="20" y2="20" class="mesh-line"/>
13
+ <line x1="8" y1="32" x2="20" y2="20" class="mesh-line"/>
14
+ <line x1="32" y1="32" x2="20" y2="20" class="mesh-line"/>
15
+ <line x1="8" y1="8" x2="32" y2="8" class="mesh-line"/>
16
+ <line x1="8" y1="32" x2="32" y2="32" class="mesh-line"/>
17
+ <line x1="8" y1="8" x2="8" y2="32" class="mesh-line"/>
18
+ <line x1="32" y1="8" x2="32" y2="32" class="mesh-line"/>
19
+
20
+ <!-- Network nodes -->
21
+ <circle cx="8" cy="8" r="3" class="mesh-node"/>
22
+ <circle cx="32" cy="8" r="3" class="mesh-node"/>
23
+ <circle cx="8" cy="32" r="3" class="mesh-node"/>
24
+ <circle cx="32" cy="32" r="3" class="mesh-node"/>
25
+ <circle cx="20" cy="20" r="4" class="mesh-node"/>
26
+ </svg>
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "node-red-contrib-meshtastic-advanced",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Advanced Node-RED nodes for Meshtastic message encoding, decoding, encryption (PSK/PKC), and decryption",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
7
7
  "build": "vite build && npm run copy-html",
8
- "copy-html": "mkdir -p dist/nodes/decrypt dist/nodes/decode dist/nodes/encode dist/nodes/encrypt && cp src/nodes/decrypt/*.html dist/nodes/decrypt/ && cp src/nodes/decode/*.html dist/nodes/decode/ && cp src/nodes/encode/*.html dist/nodes/encode/ && cp src/nodes/encrypt/*.html dist/nodes/encrypt/",
8
+ "copy-html": "mkdir -p dist/nodes/decrypt dist/nodes/decode dist/nodes/encode dist/nodes/encrypt icons && cp src/nodes/decrypt/*.html dist/nodes/decrypt/ && cp src/nodes/decode/*.html dist/nodes/decode/ && cp src/nodes/encode/*.html dist/nodes/encode/ && cp src/nodes/encrypt/*.html dist/nodes/encrypt/ && cp src/icons/*.svg icons/",
9
9
  "dev": "vite build --watch",
10
10
  "test": "vitest",
11
11
  "test:coverage": "vitest --coverage",
@@ -37,6 +37,7 @@
37
37
  },
38
38
  "files": [
39
39
  "dist",
40
+ "icons",
40
41
  "README.md",
41
42
  "LICENSE"
42
43
  ],
@@ -1 +0,0 @@
1
- {"version":3,"file":"decoder-BvBAtm2U.js","sources":["../src/lib/utils/error-types.ts","../src/lib/utils/buffer-utils.ts","../src/lib/protobuf/decoder.ts"],"sourcesContent":["/**\n * Custom error types for Meshtastic operations\n */\n\nexport class MeshtasticError extends Error {\n constructor(message: string, public cause?: Error) {\n super(message);\n this.name = 'MeshtasticError';\n if (cause) {\n this.stack = `${this.stack}\\nCaused by: ${cause.stack}`;\n }\n }\n}\n\nexport class MeshtasticDecryptError extends MeshtasticError {\n constructor(message: string, cause?: Error) {\n super(message, cause);\n this.name = 'MeshtasticDecryptError';\n }\n}\n\nexport class MeshtasticDecodeError extends MeshtasticError {\n constructor(message: string, cause?: Error) {\n super(message, cause);\n this.name = 'MeshtasticDecodeError';\n }\n}\n\nexport class MeshtasticEncodeError extends MeshtasticError {\n constructor(message: string, cause?: Error) {\n super(message, cause);\n this.name = 'MeshtasticEncodeError';\n }\n}\n\nexport class MeshtasticEncryptError extends MeshtasticError {\n constructor(message: string, cause?: Error) {\n super(message, cause);\n this.name = 'MeshtasticEncryptError';\n }\n}\n\nexport class InvalidKeyError extends MeshtasticError {\n constructor(message: string) {\n super(message);\n this.name = 'InvalidKeyError';\n }\n}\n\nexport class InvalidNonceError extends MeshtasticError {\n constructor(message: string) {\n super(message);\n this.name = 'InvalidNonceError';\n }\n}\n\nexport class ProtobufError extends MeshtasticError {\n constructor(message: string, cause?: Error) {\n super(message, cause);\n this.name = 'ProtobufError';\n }\n}\n","/**\n * Buffer manipulation utilities\n */\n\n/**\n * Convert a Uint8Array to Buffer\n * @param uint8Array - Uint8Array to convert\n * @returns Buffer\n */\nexport function uint8ArrayToBuffer(uint8Array: Uint8Array): Buffer {\n return Buffer.from(uint8Array);\n}\n\n/**\n * Convert a Buffer to Uint8Array\n * @param buffer - Buffer to convert\n * @returns Uint8Array\n */\nexport function bufferToUint8Array(buffer: Buffer): Uint8Array {\n return new Uint8Array(buffer);\n}\n\n/**\n * Check if input is a Buffer or Uint8Array\n * @param input - Input to check\n * @returns true if input is Buffer or Uint8Array\n */\nexport function isBufferLike(input: any): input is Buffer | Uint8Array {\n return Buffer.isBuffer(input) || input instanceof Uint8Array;\n}\n\n/**\n * Ensure input is a Buffer (convert if needed)\n * @param input - Buffer or Uint8Array\n * @returns Buffer\n */\nexport function ensureBuffer(input: Buffer | Uint8Array): Buffer {\n if (Buffer.isBuffer(input)) {\n return input;\n }\n return Buffer.from(input);\n}\n\n/**\n * Ensure input is a Uint8Array (convert if needed)\n * @param input - Buffer or Uint8Array\n * @returns Uint8Array\n */\nexport function ensureUint8Array(input: Buffer | Uint8Array): Uint8Array {\n if (input instanceof Uint8Array && !(input instanceof Buffer)) {\n return input;\n }\n return new Uint8Array(input);\n}\n\n/**\n * Convert a hex string to Buffer\n * @param hex - Hex string\n * @returns Buffer\n */\nexport function hexToBuffer(hex: string): Buffer {\n return Buffer.from(hex, 'hex');\n}\n\n/**\n * Convert a base64 string to Buffer\n * @param base64 - Base64 string\n * @returns Buffer\n */\nexport function base64ToBuffer(base64: string): Buffer {\n return Buffer.from(base64, 'base64');\n}\n\n/**\n * Convert a Buffer to hex string\n * @param buffer - Buffer\n * @returns Hex string\n */\nexport function bufferToHex(buffer: Buffer): string {\n return buffer.toString('hex');\n}\n\n/**\n * Convert a Buffer to base64 string\n * @param buffer - Buffer\n * @returns Base64 string\n */\nexport function bufferToBase64(buffer: Buffer): string {\n return buffer.toString('base64');\n}\n\n/**\n * Check if two buffers are equal\n * @param a - First buffer\n * @param b - Second buffer\n * @returns true if buffers are equal\n */\nexport function buffersEqual(a: Buffer, b: Buffer): boolean {\n return a.equals(b);\n}\n","/**\n * Protobuf decoding utilities for Meshtastic messages\n */\n\nimport { Protobuf } from '@meshtastic/protobufs';\nimport { ProtobufError, MeshtasticDecodeError } from '../utils/error-types';\nimport { ensureUint8Array } from '../utils/buffer-utils';\n\n/**\n * Decode a ServiceEnvelope from binary data\n * @param buffer - Binary data containing ServiceEnvelope\n * @returns Decoded ServiceEnvelope\n * @throws {ProtobufError} If decoding fails\n */\nexport function decodeServiceEnvelope(buffer: Buffer | Uint8Array): Protobuf.Mqtt.ServiceEnvelope {\n try {\n const uint8 = ensureUint8Array(buffer);\n return Protobuf.Mqtt.ServiceEnvelope.fromBinary(uint8);\n } catch (error) {\n throw new ProtobufError(\n `Failed to decode ServiceEnvelope: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n\n/**\n * Decode a Data protobuf from binary data\n * @param buffer - Binary data containing Data message\n * @returns Decoded Data\n * @throws {ProtobufError} If decoding fails\n */\nexport function decodeData(buffer: Buffer | Uint8Array): Protobuf.Mesh.Data {\n try {\n const uint8 = ensureUint8Array(buffer);\n return Protobuf.Mesh.Data.fromBinary(uint8);\n } catch (error) {\n throw new ProtobufError(\n `Failed to decode Data: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n\n/**\n * Decode a message payload based on its port number\n * @param portNum - Port number (message type)\n * @param payload - Binary payload data\n * @returns Decoded message (type depends on portNum)\n * @throws {MeshtasticDecodeError} If decoding fails\n */\nexport function decodeMessageByPortNum(\n portNum: Protobuf.Portnums.PortNum | number,\n payload: Uint8Array | Buffer\n): any {\n try {\n const uint8 = ensureUint8Array(payload);\n\n switch (portNum) {\n case Protobuf.Portnums.PortNum.TEXT_MESSAGE_APP:\n return new TextDecoder().decode(uint8);\n\n case Protobuf.Portnums.PortNum.POSITION_APP:\n return Protobuf.Mesh.Position.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.NODEINFO_APP:\n return Protobuf.Mesh.User.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.TELEMETRY_APP:\n return Protobuf.Telemetry.Telemetry.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.ROUTING_APP:\n return Protobuf.Mesh.Routing.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.ADMIN_APP:\n return Protobuf.Admin.AdminMessage.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.WAYPOINT_APP:\n return Protobuf.Mesh.Waypoint.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.NEIGHBORINFO_APP:\n return Protobuf.Mesh.NeighborInfo.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.STORE_FORWARD_APP:\n return Protobuf.StoreForward.StoreAndForward.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.RANGE_TEST_APP:\n return new TextDecoder('ascii').decode(uint8);\n\n case Protobuf.Portnums.PortNum.REMOTE_HARDWARE_APP:\n return Protobuf.RemoteHardware.HardwareMessage.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.DETECTION_SENSOR_APP:\n return new TextDecoder().decode(uint8);\n\n case Protobuf.Portnums.PortNum.REPLY_APP:\n return new TextDecoder('ascii').decode(uint8);\n\n case Protobuf.Portnums.PortNum.TRACEROUTE_APP:\n return Protobuf.Mesh.RouteDiscovery.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.PAXCOUNTER_APP:\n return Protobuf.Paxcount.Paxcount.fromBinary(uint8);\n\n case Protobuf.Portnums.PortNum.MAP_REPORT_APP:\n return Protobuf.Mqtt.MapReport.fromBinary(uint8);\n\n // For unknown or unsupported message types, return raw bytes\n case Protobuf.Portnums.PortNum.UNKNOWN_APP:\n case Protobuf.Portnums.PortNum.IP_TUNNEL_APP:\n case Protobuf.Portnums.PortNum.SERIAL_APP:\n case Protobuf.Portnums.PortNum.AUDIO_APP:\n case Protobuf.Portnums.PortNum.ZPS_APP:\n case Protobuf.Portnums.PortNum.SIMULATOR_APP:\n case Protobuf.Portnums.PortNum.PRIVATE_APP:\n case Protobuf.Portnums.PortNum.ATAK_FORWARDER:\n case Protobuf.Portnums.PortNum.TEXT_MESSAGE_COMPRESSED_APP:\n default:\n return {\n raw: Buffer.from(uint8),\n portNum,\n note: `Unsupported or binary message type: ${getPortNumName(portNum)}`\n };\n }\n } catch (error) {\n throw new MeshtasticDecodeError(\n `Failed to decode message for portNum ${portNum}: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n\n/**\n * Get the name of a port number\n * @param portNum - Port number\n * @returns Port number name (e.g., \"TEXT_MESSAGE_APP\")\n */\nexport function getPortNumName(portNum: Protobuf.Portnums.PortNum | number): string {\n const name = Protobuf.Portnums.PortNum[portNum];\n return name || `UNKNOWN(${portNum})`;\n}\n\n/**\n * Format a ServiceEnvelope as a JSON object for output\n * @param envelope - ServiceEnvelope to format\n * @param decodePayload - Whether to decode the payload (default: true)\n * @returns Formatted JSON object\n */\nexport function formatServiceEnvelopeAsJson(\n envelope: Protobuf.Mqtt.ServiceEnvelope,\n decodePayload: boolean = true\n): any {\n const packet = envelope.packet;\n\n if (!packet) {\n return {\n channelId: envelope.channelId,\n gatewayId: envelope.gatewayId,\n error: 'No packet in ServiceEnvelope'\n };\n }\n\n const result: any = {\n channelId: envelope.channelId,\n gatewayId: envelope.gatewayId,\n from: packet.from,\n to: packet.to,\n channel: packet.channel,\n packetId: packet.id,\n rxTime: packet.rxTime,\n rxSnr: packet.rxSnr,\n rxRssi: packet.rxRssi,\n hopLimit: packet.hopLimit,\n wantAck: packet.wantAck,\n priority: packet.priority,\n hopStart: packet.hopStart,\n };\n\n // Handle payload based on variant type\n if (packet.payloadVariant.case === 'encrypted') {\n result.encrypted = true;\n result.encryptedData = Buffer.from(packet.payloadVariant.value).toString('base64');\n } else if (packet.payloadVariant.case === 'decoded') {\n const decoded = packet.payloadVariant.value;\n result.encrypted = false;\n result.portNum = getPortNumName(decoded.portnum);\n result.portNumValue = decoded.portnum;\n result.wantResponse = decoded.wantResponse;\n result.requestId = decoded.requestId;\n result.emoji = decoded.emoji;\n\n if (decodePayload && decoded.payload && decoded.payload.length > 0) {\n try {\n result.payload = decodeMessageByPortNum(decoded.portnum, decoded.payload);\n } catch (error) {\n result.payload = {\n raw: Buffer.from(decoded.payload).toString('base64'),\n error: error instanceof Error ? error.message : String(error)\n };\n }\n } else {\n result.payload = decoded.payload ? Buffer.from(decoded.payload).toString('base64') : null;\n }\n }\n\n return result;\n}\n"],"names":["Protobuf"],"mappings":";;AAIO,MAAM,wBAAwB,MAAM;AAAA,EACzC,YAAY,SAAwB,OAAe;AACjD,UAAM,OAAO;AADqB,SAAA,QAAA;AAElC,SAAK,OAAO;AACZ,QAAI,OAAO;AACT,WAAK,QAAQ,GAAG,KAAK,KAAK;AAAA,aAAgB,MAAM,KAAK;AAAA,IACvD;AAAA,EACF;AACF;AAEO,MAAM,+BAA+B,gBAAgB;AAAA,EAC1D,YAAY,SAAiB,OAAe;AAC1C,UAAM,SAAS,KAAK;AACpB,SAAK,OAAO;AAAA,EACd;AACF;AAEO,MAAM,8BAA8B,gBAAgB;AAAA,EACzD,YAAY,SAAiB,OAAe;AAC1C,UAAM,SAAS,KAAK;AACpB,SAAK,OAAO;AAAA,EACd;AACF;AAEO,MAAM,8BAA8B,gBAAgB;AAAA,EACzD,YAAY,SAAiB,OAAe;AAC1C,UAAM,SAAS,KAAK;AACpB,SAAK,OAAO;AAAA,EACd;AACF;AAEO,MAAM,+BAA+B,gBAAgB;AAAA,EAC1D,YAAY,SAAiB,OAAe;AAC1C,UAAM,SAAS,KAAK;AACpB,SAAK,OAAO;AAAA,EACd;AACF;AAEO,MAAM,wBAAwB,gBAAgB;AAAA,EACnD,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AA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cesContent":["/**\n * Protobuf encoding utilities for Meshtastic messages\n */\n\nimport { Protobuf } from '@meshtastic/protobufs';\nimport { MeshtasticEncodeError, ProtobufError } from '../utils/error-types';\nimport { ensureBuffer } from '../utils/buffer-utils';\n\n/**\n * Encode a message payload based on its port number\n * @param portNum - Port number (message type)\n * @param message - Message data to encode\n * @returns Encoded binary payload\n * @throws {MeshtasticEncodeError} If encoding fails\n */\nexport function encodeMessageByPortNum(\n portNum: Protobuf.Portnums.PortNum | number,\n message: any\n): Uint8Array {\n try {\n switch (portNum) {\n case Protobuf.Portnums.PortNum.TEXT_MESSAGE_APP:\n // Encode string as UTF-8 bytes\n if (typeof message !== 'string') {\n throw new Error('TEXT_MESSAGE_APP requires a string message');\n }\n return new TextEncoder().encode(message);\n\n case Protobuf.Portnums.PortNum.POSITION_APP:\n return Protobuf.Mesh.Position.toBinary(message as Protobuf.Mesh.Position);\n\n case Protobuf.Portnums.PortNum.NODEINFO_APP:\n return Protobuf.Mesh.User.toBinary(message as Protobuf.Mesh.User);\n\n case Protobuf.Portnums.PortNum.TELEMETRY_APP:\n return Protobuf.Telemetry.Telemetry.toBinary(message as Protobuf.Telemetry.Telemetry);\n\n case Protobuf.Portnums.PortNum.ROUTING_APP:\n return Protobuf.Mesh.Routing.toBinary(message as Protobuf.Mesh.Routing);\n\n case Protobuf.Portnums.PortNum.ADMIN_APP:\n return Protobuf.Admin.AdminMessage.toBinary(message as Protobuf.Admin.AdminMessage);\n\n case Protobuf.Portnums.PortNum.WAYPOINT_APP:\n return Protobuf.Mesh.Waypoint.toBinary(message as Protobuf.Mesh.Waypoint);\n\n case Protobuf.Portnums.PortNum.NEIGHBORINFO_APP:\n return Protobuf.Mesh.NeighborInfo.toBinary(message as Protobuf.Mesh.NeighborInfo);\n\n case Protobuf.Portnums.PortNum.STORE_FORWARD_APP:\n return Protobuf.StoreForward.StoreAndForward.toBinary(message as Protobuf.StoreForward.StoreAndForward);\n\n case Protobuf.Portnums.PortNum.RANGE_TEST_APP:\n case Protobuf.Portnums.PortNum.DETECTION_SENSOR_APP:\n case Protobuf.Portnums.PortNum.REPLY_APP:\n // Encode string as ASCII/UTF-8 bytes\n if (typeof message !== 'string') {\n throw new Error(`${portNum} requires a string message`);\n }\n return new TextEncoder().encode(message);\n\n case Protobuf.Portnums.PortNum.REMOTE_HARDWARE_APP:\n return Protobuf.RemoteHardware.HardwareMessage.toBinary(message as Protobuf.RemoteHardware.HardwareMessage);\n\n case Protobuf.Portnums.PortNum.TRACEROUTE_APP:\n return Protobuf.Mesh.RouteDiscovery.toBinary(message as Protobuf.Mesh.RouteDiscovery);\n\n case Protobuf.Portnums.PortNum.PAXCOUNTER_APP:\n return Protobuf.Paxcount.Paxcount.toBinary(message as Protobuf.Paxcount.Paxcount);\n\n case Protobuf.Portnums.PortNum.MAP_REPORT_APP:\n return Protobuf.Mqtt.MapReport.toBinary(message as Protobuf.Mqtt.MapReport);\n\n default:\n throw new Error(`Encoding not supported for portNum: ${portNum}`);\n }\n } catch (error) {\n throw new MeshtasticEncodeError(\n `Failed to encode message for portNum ${portNum}: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n\n/**\n * Create a Data protobuf from encoded payload\n * @param portNum - Port number (message type)\n * @param payload - Encoded message payload\n * @param options - Additional options (wantResponse, requestId, emoji)\n * @returns Data protobuf\n */\nexport function createDataPayload(\n portNum: Protobuf.Portnums.PortNum | number,\n payload: Uint8Array,\n options?: {\n wantResponse?: boolean;\n requestId?: number;\n emoji?: number;\n }\n): Protobuf.Mesh.Data {\n return {\n portnum: portNum as Protobuf.Portnums.PortNum,\n payload,\n wantResponse: options?.wantResponse ?? false,\n requestId: options?.requestId ?? 0,\n emoji: options?.emoji ?? 0,\n };\n}\n\n/**\n * Create a ServiceEnvelope with a Data payload\n * @param data - Data protobuf\n * @param from - Source node number\n * @param to - Destination node number (0xFFFFFFFF for broadcast)\n * @param channel - Channel index\n * @param packetId - Packet ID (if undefined, needs to be set later)\n * @param options - Additional options\n * @returns ServiceEnvelope\n */\nexport function createServiceEnvelope(\n data: Protobuf.Mesh.Data,\n from: number,\n to: number = 0xFFFFFFFF,\n channel: number = 0,\n packetId?: number,\n options?: {\n wantAck?: boolean;\n hopLimit?: number;\n priority?: Protobuf.Mesh.MeshPacket_Priority;\n channelId?: string;\n gatewayId?: string;\n }\n): Protobuf.Mqtt.ServiceEnvelope {\n return {\n packet: {\n from,\n to,\n channel,\n id: packetId ?? 0,\n rxTime: 0,\n rxSnr: 0,\n rxRssi: 0,\n hopLimit: options?.hopLimit ?? 3,\n wantAck: options?.wantAck ?? false,\n priority: options?.priority ?? Protobuf.Mesh.MeshPacket_Priority.UNSET,\n hopStart: options?.hopLimit ?? 3,\n payloadVariant: {\n case: 'decoded',\n value: data,\n },\n },\n channelId: options?.channelId ?? '',\n gatewayId: options?.gatewayId ?? '',\n };\n}\n\n/**\n * Encode a complete ServiceEnvelope to binary\n * @param envelope - ServiceEnvelope to encode\n * @returns Binary encoded ServiceEnvelope\n * @throws {ProtobufError} If encoding fails\n */\nexport function encodeServiceEnvelope(envelope: Protobuf.Mqtt.ServiceEnvelope): Buffer {\n try {\n const binary = Protobuf.Mqtt.ServiceEnvelope.toBinary(envelope);\n return ensureBuffer(binary);\n } catch (error) {\n throw new ProtobufError(\n `Failed to encode ServiceEnvelope: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n\n/**\n * Encode a Data protobuf to binary\n * @param data - Data protobuf to encode\n * @returns Binary encoded Data\n * @throws {ProtobufError} If encoding fails\n */\nexport function encodeData(data: Protobuf.Mesh.Data): Buffer {\n try {\n const binary = Protobuf.Mesh.Data.toBinary(data);\n return ensureBuffer(binary);\n } catch (error) {\n throw new ProtobufError(\n `Failed to encode Data: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n\n/**\n * Parse port number from string name or number\n * @param portNum - Port number as string name or number\n * @returns Port number as enum value\n * @throws {MeshtasticEncodeError} If port number is invalid\n */\nexport function parsePortNum(portNum: string | number): Protobuf.Portnums.PortNum {\n if (typeof portNum === 'number') {\n // Validate that the number is a valid PortNum\n if (portNum in Protobuf.Portnums.PortNum) {\n return portNum as Protobuf.Portnums.PortNum;\n }\n throw new MeshtasticEncodeError(`Invalid port number: ${portNum}`);\n }\n\n // Parse string name to enum value\n const enumValue = (Protobuf.Portnums.PortNum as any)[portNum];\n if (enumValue === undefined) {\n throw new MeshtasticEncodeError(`Unknown port number name: ${portNum}`);\n }\n\n return enumValue;\n}\n"],"names":["Protobuf","MeshtasticEncodeError","ensureBuffer","ProtobufError"],"mappings":";;;AAeO,SAAS,uBACd,SACA,SACY;AACZ,MAAI;AACF,YAAQ,SAAA;AAAA,MACN,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAE7B,YAAI,OAAO,YAAY,UAAU;AAC/B,gBAAM,IAAI,MAAM,4CAA4C;AAAA,QAC9D;AACA,eAAO,IAAI,YAAA,EAAc,OAAO,OAAO;AAAA,MAEzC,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,eAAOA,UAAAA,SAAS,KAAK,SAAS,SAAS,OAAiC;AAAA,MAE1E,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,eAAOA,UAAAA,SAAS,KAAK,KAAK,SAAS,OAA6B;AAAA,MAElE,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,eAAOA,UAAAA,SAAS,UAAU,UAAU,SAAS,OAAuC;AAAA,MAEtF,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,eAAOA,UAAAA,SAAS,KAAK,QAAQ,SAAS,OAAgC;AAAA,MAExE,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,eAAOA,UAAAA,SAAS,MAAM,aAAa,SAAS,OAAsC;AAAA,MAEpF,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,eAAOA,UAAAA,SAAS,KAAK,SAAS,SAAS,OAAiC;AAAA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AS,aAAa,SAAqD;AAChF,MAAI,OAAO,YAAY,UAAU;AAE/B,QAAI,WAAWH,UAAAA,SAAS,SAAS,SAAS;AACxC,aAAO;AAAA,IACT;AACA,UAAM,IAAIC,QAAAA,sBAAsB,wBAAwB,OAAO,EAAE;AAAA,EACnE;AAGA,QAAM,YAAaD,UAAAA,SAAS,SAAS,QAAgB,OAAO;AAC5D,MAAI,cAAc,QAAW;AAC3B,UAAM,IAAIC,QAAAA,sBAAsB,6BAA6B,OAAO,EAAE;AAAA,EACxE;AAEA,SAAO;AACT;;;;;;;"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"message-types-FA26fPjF.js","sources":["../src/lib/protobuf/message-types.ts"],"sourcesContent":["/**\n * Meshtastic message type definitions and utilities\n */\n\nimport { Protobuf } from '@meshtastic/protobufs';\n\n/**\n * Common message type categories\n */\nexport enum MessageCategory {\n TEXT = 'text',\n POSITION = 'position',\n NODEINFO = 'nodeinfo',\n TELEMETRY = 'telemetry',\n ROUTING = 'routing',\n ADMIN = 'admin',\n HARDWARE = 'hardware',\n OTHER = 'other',\n}\n\n/**\n * Get the category for a port number\n * @param portNum - Port number\n * @returns Message category\n */\nexport function getMessageCategory(portNum: Protobuf.Portnums.PortNum): MessageCategory {\n switch (portNum) {\n case Protobuf.Portnums.PortNum.TEXT_MESSAGE_APP:\n case Protobuf.Portnums.PortNum.TEXT_MESSAGE_COMPRESSED_APP:\n case Protobuf.Portnums.PortNum.REPLY_APP:\n return MessageCategory.TEXT;\n\n case Protobuf.Portnums.PortNum.POSITION_APP:\n case Protobuf.Portnums.PortNum.WAYPOINT_APP:\n case Protobuf.Portnums.PortNum.MAP_REPORT_APP:\n return MessageCategory.POSITION;\n\n case Protobuf.Portnums.PortNum.NODEINFO_APP:\n case Protobuf.Portnums.PortNum.NEIGHBORINFO_APP:\n return MessageCategory.NODEINFO;\n\n case Protobuf.Portnums.PortNum.TELEMETRY_APP:\n case Protobuf.Portnums.PortNum.PAXCOUNTER_APP:\n case Protobuf.Portnums.PortNum.DETECTION_SENSOR_APP:\n return MessageCategory.TELEMETRY;\n\n case Protobuf.Portnums.PortNum.ROUTING_APP:\n case Protobuf.Portnums.PortNum.TRACEROUTE_APP:\n return MessageCategory.ROUTING;\n\n case Protobuf.Portnums.PortNum.ADMIN_APP:\n return MessageCategory.ADMIN;\n\n case Protobuf.Portnums.PortNum.REMOTE_HARDWARE_APP:\n return MessageCategory.HARDWARE;\n\n default:\n return MessageCategory.OTHER;\n }\n}\n\n/**\n * Check if a port number represents a text message\n * @param portNum - Port number\n * @returns true if text message\n */\nexport function isTextMessage(portNum: Protobuf.Portnums.PortNum): boolean {\n return getMessageCategory(portNum) === MessageCategory.TEXT;\n}\n\n/**\n * Check if a port number represents a position message\n * @param portNum - Port number\n * @returns true if position message\n */\nexport function isPositionMessage(portNum: Protobuf.Portnums.PortNum): boolean {\n return getMessageCategory(portNum) === MessageCategory.POSITION;\n}\n\n/**\n * Get a human-readable description for a port number\n * @param portNum - Port number\n * @returns Description\n */\nexport function getPortNumDescription(portNum: Protobuf.Portnums.PortNum): string {\n switch (portNum) {\n case Protobuf.Portnums.PortNum.UNKNOWN_APP:\n return 'Unknown message type';\n case Protobuf.Portnums.PortNum.TEXT_MESSAGE_APP:\n return 'Text message';\n case Protobuf.Portnums.PortNum.REMOTE_HARDWARE_APP:\n return 'Remote hardware control';\n case Protobuf.Portnums.PortNum.POSITION_APP:\n return 'GPS position';\n case Protobuf.Portnums.PortNum.NODEINFO_APP:\n return 'Node information';\n case Protobuf.Portnums.PortNum.ROUTING_APP:\n return 'Routing information';\n case Protobuf.Portnums.PortNum.ADMIN_APP:\n return 'Admin command';\n case Protobuf.Portnums.PortNum.TEXT_MESSAGE_COMPRESSED_APP:\n return 'Compressed text message';\n case Protobuf.Portnums.PortNum.WAYPOINT_APP:\n return 'Waypoint';\n case Protobuf.Portnums.PortNum.AUDIO_APP:\n return 'Audio message';\n case Protobuf.Portnums.PortNum.DETECTION_SENSOR_APP:\n return 'Detection sensor data';\n case Protobuf.Portnums.PortNum.REPLY_APP:\n return 'Reply message';\n case Protobuf.Portnums.PortNum.IP_TUNNEL_APP:\n return 'IP tunnel';\n case Protobuf.Portnums.PortNum.SERIAL_APP:\n return 'Serial data';\n case Protobuf.Portnums.PortNum.STORE_FORWARD_APP:\n return 'Store and forward';\n case Protobuf.Portnums.PortNum.RANGE_TEST_APP:\n return 'Range test';\n case Protobuf.Portnums.PortNum.TELEMETRY_APP:\n return 'Telemetry data';\n case Protobuf.Portnums.PortNum.ZPS_APP:\n return 'ZPS application';\n case Protobuf.Portnums.PortNum.SIMULATOR_APP:\n return 'Simulator';\n case Protobuf.Portnums.PortNum.TRACEROUTE_APP:\n return 'Traceroute';\n case Protobuf.Portnums.PortNum.NEIGHBORINFO_APP:\n return 'Neighbor information';\n case Protobuf.Portnums.PortNum.ATAK_FORWARDER:\n return 'ATAK forwarder';\n case Protobuf.Portnums.PortNum.PAXCOUNTER_APP:\n return 'People counter';\n case Protobuf.Portnums.PortNum.MAP_REPORT_APP:\n return 'Map report';\n case Protobuf.Portnums.PortNum.PRIVATE_APP:\n return 'Private application';\n default:\n return `Unknown (${portNum})`;\n }\n}\n\n/**\n * Broadcast address constant\n */\nexport const BROADCAST_ADDR = 0xFFFFFFFF;\n\n/**\n * Check if an address is broadcast\n * @param addr - Address to check\n * @returns true if broadcast address\n */\nexport function isBroadcastAddr(addr: number): boolean {\n return addr === BROADCAST_ADDR;\n}\n"],"names":["Protobuf"],"mappings":";;AAyBO,SAAS,mBAAmB,SAAqD;AACtF,UAAQ,SAAA;AAAA,IACN,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAAA,IAC/B,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAAA,IAC/B,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IAET,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAAA,IAC/B,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAAA,IAC/B,KA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AC7B,aAAO;AAAA,IACT,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IACT,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IACT,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IACT,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IACT,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IACT,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IACT,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IACT,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IACT,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IACT,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IACT,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IACT,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IACT,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IACT,KAAKA,UAAAA,SAAS,SAAS,QAAQ;AAC7B,aAAO;AAAA,IACT;AACE,aAAO,YAAY,OAAO;AAAA,EAAA;AAEhC;AAKO,MAAM,iBAAiB;;;;"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"validation-BegQyBSh.js","sources":["../src/lib/utils/validation.ts"],"sourcesContent":["/**\n * Input validation utilities\n */\n\nimport { MeshtasticError } from './error-types';\n\n/**\n * Validate that a value is a valid node number (uint32)\n * @param value - Value to validate\n * @param fieldName - Name of the field (for error messages)\n * @throws {MeshtasticError} If validation fails\n */\nexport function validateNodeNumber(value: any, fieldName: string = 'nodeNum'): void {\n if (typeof value !== 'number' || !Number.isInteger(value)) {\n throw new MeshtasticError(`${fieldName} must be an integer, got ${typeof value}`);\n }\n\n if (value < 0 || value > 0xFFFFFFFF) {\n throw new MeshtasticError(`${fieldName} must be a valid uint32 (0-4294967295), got ${value}`);\n }\n}\n\n/**\n * Validate that a value is a valid packet ID (uint32)\n * @param value - Value to validate\n * @param fieldName - Name of the field (for error messages)\n * @throws {MeshtasticError} If validation fails\n */\nexport function validatePacketId(value: any, fieldName: string = 'packetId'): void {\n validateNodeNumber(value, fieldName);\n}\n\n/**\n * Validate that a value is a valid channel index\n * @param value - Value to validate\n * @param fieldName - Name of the field (for error messages)\n * @throws {MeshtasticError} If validation fails\n */\nexport function validateChannelIndex(value: any, fieldName: string = 'channel'): void {\n if (typeof value !== 'number' || !Number.isInteger(value)) {\n throw new MeshtasticError(`${fieldName} must be an integer, got ${typeof value}`);\n }\n\n if (value < 0 || value > 7) {\n throw new MeshtasticError(`${fieldName} must be 0-7, got ${value}`);\n }\n}\n\n/**\n * Validate that a value is a valid hop limit\n * @param value - Value to validate\n * @param fieldName - Name of the field (for error messages)\n * @throws {MeshtasticError} If validation fails\n */\nexport function validateHopLimit(value: any, fieldName: string = 'hopLimit'): void {\n if (typeof value !== 'number' || !Number.isInteger(value)) {\n throw new MeshtasticError(`${fieldName} must be an integer, got ${typeof value}`);\n }\n\n if (value < 0 || value > 7) {\n throw new MeshtasticError(`${fieldName} must be 0-7, got ${value}`);\n }\n}\n\n/**\n * Validate that a value is a buffer\n * @param value - Value to validate\n * @param fieldName - Name of the field (for error messages)\n * @throws {MeshtasticError} If validation fails\n */\nexport function validateBuffer(value: any, fieldName: string = 'buffer'): void {\n if (!Buffer.isBuffer(value) && !(value instanceof Uint8Array)) {\n throw new MeshtasticError(`${fieldName} must be a Buffer or Uint8Array, got ${typeof value}`);\n }\n}\n\n/**\n * Validate that a value is a non-empty buffer\n * @param value - Value to validate\n * @param fieldName - Name of the field (for error messages)\n * @throws {MeshtasticError} If validation fails\n */\nexport function validateNonEmptyBuffer(value: any, fieldName: string = 'buffer'): void {\n validateBuffer(value, fieldName);\n if (value.length === 0) {\n throw new MeshtasticError(`${fieldName} cannot be empty`);\n }\n}\n\n/**\n * Validate that a value is a non-empty string\n * @param value - Value to validate\n * @param fieldName - Name of the field (for error messages)\n * @throws {MeshtasticError} If validation fails\n */\nexport function validateNonEmptyString(value: any, fieldName: string = 'string'): void {\n if (typeof value !== 'string' || value.trim().length === 0) {\n throw new MeshtasticError(`${fieldName} must be a non-empty string`);\n }\n}\n\n/**\n * Validate that a value is defined (not null or undefined)\n * @param value - Value to validate\n * @param fieldName - Name of the field (for error messages)\n * @throws {MeshtasticError} If validation fails\n */\nexport function validateDefined<T>(value: T | null | undefined, fieldName: string): asserts value is T {\n if (value === null || value === undefined) {\n throw new MeshtasticError(`${fieldName} is required but was ${value}`);\n }\n}\n"],"names":["MeshtasticError"],"mappings":";;AAYO,SAAS,mBAAmB,OAAY,YAAoB,WAAiB;AAClF,MAAI,OAAO,UAAU,YAAY,CAAC,OAAO,UAAU,KAAK,GAAG;AACzD,UAAM,IAAIA,QAAAA,gBAAgB,GAAG,SAAS,4BAA4B,OAAO,KAAK,EAAE;AAAA,EAClF;AAEA,MAAI,QAAQ,KAAK,QAAQ,YAAY;AACnC,UAAM,IAAIA,QAAAA,gBAAgB,GAAG,SAAS,+CAA+C,KAAK,EAAE;AAAA,EAC9F;AACF;AAQO,SAAS,iBAAiB,OAAY,YAAoB,YAAkB;AACjF,qBAAmB,OAAO,SAAS;AACrC;AAQO,SAAS,qBAAqB,OAAY,YAAoB,WAAiB;AACpF,MAAI,OAAO,UAAU,YAAY,CAAC,OAAO,UAAU,KAAK,GAAG;AACzD,UAAM,IAAIA,QAAAA,gBAAgB,GAAG,SAAS,4BAA4B,OAAO,KAAK,EAAE;AAAA,EAClF;AAEA,MAAI,QAAQ,KAAK,QAAQ,GAAG;AAC1B,UAAM,IAAIA,QAAAA,gBAAgB,GAAG,SAAS,qBAAqB,KAAK,EAAE;AAAA,EACpE;AACF;AAQO,SAAS,iBAAiB,OAAY,YAAoB,YAAkB;AACjF,MAAI,OAAO,UAAU,YAAY,CAAC,OAAO,UAAU,KAAK,GAAG;AACzD,UAAM,IAAIA,QAAAA,gBAAgB,GAAG,SAAS,4BAA4B,OAAO,KAAK,EAAE;AAAA,EAClF;AAEA,MAAI,QAAQ,KAAK,QAAQ,GAAG;AAC1B,UAAM,IAAIA,QAAAA,gBAAgB,GAAG,SAAS,qBAAqB,KAAK,EAAE;AAAA,EACpE;AACF;;;;;"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"x25519-D0dlDGB0.js","sources":["../src/lib/crypto/nonce.ts","../src/lib/crypto/key-parser.ts","../src/lib/crypto/aes-ctr.ts","../src/lib/crypto/aes-ccm.ts","../node_modules/@noble/hashes/esm/crypto.js","../node_modules/@noble/hashes/esm/utils.js","../node_modules/@noble/curves/esm/utils.js","../node_modules/@noble/curves/esm/abstract/modular.js","../node_modules/@noble/curves/esm/abstract/montgomery.js","../node_modules/@noble/curves/esm/ed25519.js","../src/lib/crypto/x25519.ts"],"sourcesContent":["/**\n * Nonce and IV construction for Meshtastic encryption\n */\n\nimport { InvalidNonceError } from '../utils/error-types';\nimport { randomBytes } from 'crypto';\n\n/**\n * Construct nonce/IV for AES-CTR channel encryption\n *\n * Meshtastic uses a 128-bit IV for AES-CTR constructed as follows:\n * - Bytes 0-3: Node number (from field, little-endian uint32)\n * - Bytes 4-7: Packet ID (id field, little-endian uint32)\n * - Bytes 8-15: Zero (counter starts at 0, increments per 16-byte block)\n *\n * @param nodeNum - Source node number (from field)\n * @param packetId - Packet ID\n * @returns 16-byte Buffer containing the IV\n * @throws {InvalidNonceError} If parameters are invalid\n */\nexport function constructChannelNonce(nodeNum: number, packetId: number): Buffer {\n if (!Number.isInteger(nodeNum) || nodeNum < 0 || nodeNum > 0xFFFFFFFF) {\n throw new InvalidNonceError(`Invalid node number: ${nodeNum}. Must be uint32.`);\n }\n\n if (!Number.isInteger(packetId) || packetId < 0 || packetId > 0xFFFFFFFF) {\n throw new InvalidNonceError(`Invalid packet ID: ${packetId}. Must be uint32.`);\n }\n\n const iv = Buffer.alloc(16);\n\n // Write node number at offset 0 (4 bytes, little-endian)\n iv.writeUInt32LE(nodeNum, 0);\n\n // Write packet ID at offset 4 (4 bytes, little-endian)\n iv.writeUInt32LE(packetId, 4);\n\n // Bytes 8-15 remain zero (counter starts at 0)\n\n return iv;\n}\n\n/**\n * Generate a random nonce for AES-CCM direct message encryption\n *\n * For PKC direct messages, Meshtastic uses AES-CCM which requires a nonce.\n * The nonce should be random and unique for each message.\n *\n * @param length - Nonce length in bytes (default: 12, suitable for AES-CCM)\n * @returns Buffer containing random nonce\n */\nexport function generateRandomNonce(length: number = 12): Buffer {\n if (length < 7 || length > 13) {\n throw new InvalidNonceError(`Nonce length must be between 7 and 13 bytes, got ${length}`);\n }\n\n return randomBytes(length);\n}\n\n/**\n * Validate a nonce buffer\n * @param nonce - Nonce buffer to validate\n * @param expectedLength - Expected length (optional)\n * @throws {InvalidNonceError} If nonce is invalid\n */\nexport function validateNonce(nonce: Buffer, expectedLength?: number): void {\n if (!Buffer.isBuffer(nonce)) {\n throw new InvalidNonceError('Nonce must be a Buffer');\n }\n\n if (expectedLength !== undefined && nonce.length !== expectedLength) {\n throw new InvalidNonceError(\n `Invalid nonce length: ${nonce.length} bytes. Expected ${expectedLength} bytes.`\n );\n }\n}\n","/**\n * Key parsing and validation utilities for Meshtastic encryption\n */\n\nimport { InvalidKeyError } from '../utils/error-types';\n\nexport type KeyFormat = 'base64' | 'hex';\n\n/**\n * Default Meshtastic channel key (insecure)\n */\nexport const DEFAULT_KEY_BASE64 = 'AQ==';\n\n/**\n * Parse a key from string format to Buffer\n * @param key - Key string in base64 or hex format\n * @param format - Format of the key string\n * @returns Buffer containing the key bytes\n * @throws {InvalidKeyError} If key format is invalid\n */\nexport function parseKey(key: string, format: KeyFormat = 'base64'): Buffer {\n if (!key || key.trim().length === 0) {\n throw new InvalidKeyError('Key cannot be empty');\n }\n\n let keyBuffer: Buffer;\n\n try {\n if (format === 'base64') {\n keyBuffer = Buffer.from(key, 'base64');\n } else if (format === 'hex') {\n keyBuffer = Buffer.from(key, 'hex');\n } else {\n throw new InvalidKeyError(`Unsupported key format: ${format}`);\n }\n } catch (error) {\n throw new InvalidKeyError(\n `Failed to parse key as ${format}: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n\n validateKeyLength(keyBuffer);\n return keyBuffer;\n}\n\n/**\n * Validate that a key has a valid length for AES encryption\n * @param key - Key buffer to validate\n * @throws {InvalidKeyError} If key length is invalid\n */\nexport function validateKeyLength(key: Buffer): void {\n const validLengths = [16, 32]; // AES-128 or AES-256\n if (!validLengths.includes(key.length)) {\n throw new InvalidKeyError(\n `Invalid key length: ${key.length} bytes. Expected 16 (AES-128) or 32 (AES-256) bytes.`\n );\n }\n}\n\n/**\n * Check if a key is the insecure default Meshtastic key\n * @param key - Key buffer to check\n * @returns true if the key is the default key\n */\nexport function isDefaultKey(key: Buffer): boolean {\n const defaultKey = Buffer.from(DEFAULT_KEY_BASE64, 'base64');\n return key.equals(defaultKey);\n}\n\n/**\n * Get the AES algorithm name for a given key length\n * @param key - Key buffer\n * @returns Algorithm name (e.g., 'aes-256-ctr')\n */\nexport function getAlgorithmForKey(key: Buffer, mode: 'ctr' | 'ccm' = 'ctr'): string {\n validateKeyLength(key);\n const keySize = key.length === 32 ? '256' : '128';\n return `aes-${keySize}-${mode}`;\n}\n\n/**\n * Parse a public key for x25519\n * @param key - Key string in base64 or hex format\n * @param format - Format of the key string\n * @returns Buffer containing the 32-byte public key\n * @throws {InvalidKeyError} If key is not 32 bytes\n */\nexport function parsePublicKey(key: string, format: KeyFormat = 'base64'): Buffer {\n const keyBuffer = parseKey(key, format);\n if (keyBuffer.length !== 32) {\n throw new InvalidKeyError(`Public key must be 32 bytes, got ${keyBuffer.length} bytes`);\n }\n return keyBuffer;\n}\n\n/**\n * Parse a private key for x25519\n * @param key - Key string in base64 or hex format\n * @param format - Format of the key string\n * @returns Buffer containing the 32-byte private key\n * @throws {InvalidKeyError} If key is not 32 bytes\n */\nexport function parsePrivateKey(key: string, format: KeyFormat = 'base64'): Buffer {\n const keyBuffer = parseKey(key, format);\n if (keyBuffer.length !== 32) {\n throw new InvalidKeyError(`Private key must be 32 bytes, got ${keyBuffer.length} bytes`);\n }\n return keyBuffer;\n}\n","/**\n * AES-CTR encryption/decryption for Meshtastic channel messages\n */\n\nimport { createCipheriv, createDecipheriv } from 'crypto';\nimport { MeshtasticEncryptError, MeshtasticDecryptError } from '../utils/error-types';\nimport { constructChannelNonce } from './nonce';\nimport { getAlgorithmForKey } from './key-parser';\n\n/**\n * Encrypt a message using AES-CTR with Meshtastic channel encryption\n *\n * @param plaintext - Data to encrypt\n * @param psk - Pre-shared key (16 or 32 bytes)\n * @param nodeNum - Source node number (for nonce construction)\n * @param packetId - Packet ID (for nonce construction)\n * @returns Encrypted ciphertext\n * @throws {MeshtasticEncryptError} If encryption fails\n */\nexport function encryptChannelMessage(\n plaintext: Buffer,\n psk: Buffer,\n nodeNum: number,\n packetId: number\n): Buffer {\n try {\n // Construct IV from node number and packet ID\n const iv = constructChannelNonce(nodeNum, packetId);\n\n // Determine algorithm based on key length\n const algorithm = getAlgorithmForKey(psk, 'ctr');\n\n // Create cipher\n const cipher = createCipheriv(algorithm, psk, iv);\n\n // Encrypt\n const ciphertext = Buffer.concat([\n cipher.update(plaintext),\n cipher.final()\n ]);\n\n return ciphertext;\n } catch (error) {\n throw new MeshtasticEncryptError(\n `Failed to encrypt channel message: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n\n/**\n * Decrypt a message using AES-CTR with Meshtastic channel encryption\n *\n * @param ciphertext - Data to decrypt\n * @param psk - Pre-shared key (16 or 32 bytes)\n * @param nodeNum - Source node number (from packet from field)\n * @param packetId - Packet ID (from packet id field)\n * @returns Decrypted plaintext\n * @throws {MeshtasticDecryptError} If decryption fails\n */\nexport function decryptChannelMessage(\n ciphertext: Buffer,\n psk: Buffer,\n nodeNum: number,\n packetId: number\n): Buffer {\n try {\n // Construct IV from node number and packet ID\n const iv = constructChannelNonce(nodeNum, packetId);\n\n // Determine algorithm based on key length\n const algorithm = getAlgorithmForKey(psk, 'ctr');\n\n // Create decipher (in CTR mode, decryption is same as encryption)\n const decipher = createDecipheriv(algorithm, psk, iv);\n\n // Decrypt\n const plaintext = Buffer.concat([\n decipher.update(ciphertext),\n decipher.final()\n ]);\n\n return plaintext;\n } catch (error) {\n throw new MeshtasticDecryptError(\n `Failed to decrypt channel message: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n","/**\n * AES-CCM encryption/decryption for Meshtastic direct messages (PKC)\n */\n\nimport { createCipheriv, createDecipheriv } from 'crypto';\nimport { MeshtasticEncryptError, MeshtasticDecryptError } from '../utils/error-types';\nimport { validateNonce } from './nonce';\n\n/**\n * Result of AES-CCM encryption\n */\nexport interface EncryptedMessage {\n ciphertext: Buffer;\n authTag: Buffer;\n}\n\n/**\n * Encrypt a direct message using AES-CCM\n *\n * AES-CCM provides both confidentiality and authenticity through\n * an authentication tag.\n *\n * @param plaintext - Data to encrypt\n * @param sharedSecret - Derived shared secret (32 bytes for AES-256)\n * @param nonce - Random nonce (12 bytes recommended for AES-CCM)\n * @param authTagLength - Authentication tag length in bytes (default: 16)\n * @returns Object containing ciphertext and authentication tag\n * @throws {MeshtasticEncryptError} If encryption fails\n */\nexport function encryptDirectMessage(\n plaintext: Buffer,\n sharedSecret: Buffer,\n nonce: Buffer,\n authTagLength: number = 16\n): EncryptedMessage {\n try {\n // Validate nonce\n validateNonce(nonce);\n\n // Validate shared secret (should be 32 bytes for AES-256)\n if (sharedSecret.length !== 32) {\n throw new Error(`Shared secret must be 32 bytes, got ${sharedSecret.length}`);\n }\n\n // Validate auth tag length (must be 4, 6, 8, 10, 12, 14, or 16)\n const validTagLengths = [4, 6, 8, 10, 12, 14, 16];\n if (!validTagLengths.includes(authTagLength)) {\n throw new Error(`Invalid auth tag length: ${authTagLength}. Must be one of ${validTagLengths.join(', ')}`);\n }\n\n // Create cipher with CCM mode\n const cipher = createCipheriv('aes-256-ccm', sharedSecret, nonce, {\n authTagLength,\n });\n\n // Encrypt\n const ciphertext = Buffer.concat([\n cipher.update(plaintext),\n cipher.final()\n ]);\n\n // Get authentication tag\n const authTag = cipher.getAuthTag();\n\n return { ciphertext, authTag };\n } catch (error) {\n throw new MeshtasticEncryptError(\n `Failed to encrypt direct message: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n\n/**\n * Decrypt a direct message using AES-CCM\n *\n * @param ciphertext - Data to decrypt\n * @param sharedSecret - Derived shared secret (32 bytes for AES-256)\n * @param nonce - Nonce used during encryption\n * @param authTag - Authentication tag from encryption\n * @returns Decrypted plaintext\n * @throws {MeshtasticDecryptError} If decryption or authentication fails\n */\nexport function decryptDirectMessage(\n ciphertext: Buffer,\n sharedSecret: Buffer,\n nonce: Buffer,\n authTag: Buffer\n): Buffer {\n try {\n // Validate nonce\n validateNonce(nonce);\n\n // Validate shared secret\n if (sharedSecret.length !== 32) {\n throw new Error(`Shared secret must be 32 bytes, got ${sharedSecret.length}`);\n }\n\n // Validate auth tag\n if (!Buffer.isBuffer(authTag) || authTag.length === 0) {\n throw new Error('Invalid authentication tag');\n }\n\n // Create decipher with CCM mode\n const decipher = createDecipheriv('aes-256-ccm', sharedSecret, nonce, {\n authTagLength: authTag.length,\n });\n\n // Set authentication tag\n decipher.setAuthTag(authTag);\n\n // Decrypt and verify\n const plaintext = Buffer.concat([\n decipher.update(ciphertext),\n decipher.final() // This will throw if authentication fails\n ]);\n\n return plaintext;\n } catch (error) {\n // Authentication failure or decryption error\n throw new MeshtasticDecryptError(\n `Failed to decrypt direct message: ${error instanceof Error ? error.message : String(error)}. This may indicate message tampering or wrong key.`,\n error instanceof Error ? error : undefined\n );\n }\n}\n","export const crypto = typeof globalThis === 'object' && 'crypto' in globalThis ? globalThis.crypto : undefined;\n//# sourceMappingURL=crypto.js.map","/**\n * Utilities for hex, bytes, CSPRNG.\n * @module\n */\n/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */\n// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.\n// node.js versions earlier than v19 don't declare it in global scope.\n// For node.js, package.json#exports field mapping rewrites import\n// from `crypto` to `cryptoNode`, which imports native module.\n// Makes the utils un-importable in browsers without a bundler.\n// Once node.js 18 is deprecated (2025-04-30), we can just drop the import.\nimport { crypto } from '@noble/hashes/crypto';\n/** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */\nexport function isBytes(a) {\n return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');\n}\n/** Asserts something is positive integer. */\nexport function anumber(n) {\n if (!Number.isSafeInteger(n) || n < 0)\n throw new Error('positive integer expected, got ' + n);\n}\n/** Asserts something is Uint8Array. */\nexport function abytes(b, ...lengths) {\n if (!isBytes(b))\n throw new Error('Uint8Array expected');\n if (lengths.length > 0 && !lengths.includes(b.length))\n throw new Error('Uint8Array expected of length ' + lengths + ', got length=' + b.length);\n}\n/** Asserts something is hash */\nexport function ahash(h) {\n if (typeof h !== 'function' || typeof h.create !== 'function')\n throw new Error('Hash should be wrapped by utils.createHasher');\n anumber(h.outputLen);\n anumber(h.blockLen);\n}\n/** Asserts a hash instance has not been destroyed / finished */\nexport function aexists(instance, checkFinished = true) {\n if (instance.destroyed)\n throw new Error('Hash instance has been destroyed');\n if (checkFinished && instance.finished)\n throw new Error('Hash#digest() has already been called');\n}\n/** Asserts output is properly-sized byte array */\nexport function aoutput(out, instance) {\n abytes(out);\n const min = instance.outputLen;\n if (out.length < min) {\n throw new Error('digestInto() expects output buffer of length at least ' + min);\n }\n}\n/** Cast u8 / u16 / u32 to u8. */\nexport function u8(arr) {\n return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);\n}\n/** Cast u8 / u16 / u32 to u32. */\nexport function u32(arr) {\n return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));\n}\n/** Zeroize a byte array. Warning: JS provides no guarantees. */\nexport function clean(...arrays) {\n for (let i = 0; i < arrays.length; i++) {\n arrays[i].fill(0);\n }\n}\n/** Create DataView of an array for easy byte-level manipulation. */\nexport function createView(arr) {\n return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);\n}\n/** The rotate right (circular right shift) operation for uint32 */\nexport function rotr(word, shift) {\n return (word << (32 - shift)) | (word >>> shift);\n}\n/** The rotate left (circular left shift) operation for uint32 */\nexport function rotl(word, shift) {\n return (word << shift) | ((word >>> (32 - shift)) >>> 0);\n}\n/** Is current platform little-endian? Most are. Big-Endian platform: IBM */\nexport const isLE = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();\n/** The byte swap operation for uint32 */\nexport function byteSwap(word) {\n return (((word << 24) & 0xff000000) |\n ((word << 8) & 0xff0000) |\n ((word >>> 8) & 0xff00) |\n ((word >>> 24) & 0xff));\n}\n/** Conditionally byte swap if on a big-endian platform */\nexport const swap8IfBE = isLE\n ? (n) => n\n : (n) => byteSwap(n);\n/** @deprecated */\nexport const byteSwapIfBE = swap8IfBE;\n/** In place byte swap for Uint32Array */\nexport function byteSwap32(arr) {\n for (let i = 0; i < arr.length; i++) {\n arr[i] = byteSwap(arr[i]);\n }\n return arr;\n}\nexport const swap32IfBE = isLE\n ? (u) => u\n : byteSwap32;\n// Built-in hex conversion https://caniuse.com/mdn-javascript_builtins_uint8array_fromhex\nconst hasHexBuiltin = /* @__PURE__ */ (() => \n// @ts-ignore\ntypeof Uint8Array.from([]).toHex === 'function' && typeof Uint8Array.fromHex === 'function')();\n// Array where index 0xf0 (240) is mapped to string 'f0'\nconst hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'));\n/**\n * Convert byte array to hex string. Uses built-in function, when available.\n * @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'\n */\nexport function bytesToHex(bytes) {\n abytes(bytes);\n // @ts-ignore\n if (hasHexBuiltin)\n return bytes.toHex();\n // pre-caching improves the speed 6x\n let hex = '';\n for (let i = 0; i < bytes.length; i++) {\n hex += hexes[bytes[i]];\n }\n return hex;\n}\n// We use optimized technique to convert hex string to byte array\nconst asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };\nfunction asciiToBase16(ch) {\n if (ch >= asciis._0 && ch <= asciis._9)\n return ch - asciis._0; // '2' => 50-48\n if (ch >= asciis.A && ch <= asciis.F)\n return ch - (asciis.A - 10); // 'B' => 66-(65-10)\n if (ch >= asciis.a && ch <= asciis.f)\n return ch - (asciis.a - 10); // 'b' => 98-(97-10)\n return;\n}\n/**\n * Convert hex string to byte array. Uses built-in function, when available.\n * @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])\n */\nexport function hexToBytes(hex) {\n if (typeof hex !== 'string')\n throw new Error('hex string expected, got ' + typeof hex);\n // @ts-ignore\n if (hasHexBuiltin)\n return Uint8Array.fromHex(hex);\n const hl = hex.length;\n const al = hl / 2;\n if (hl % 2)\n throw new Error('hex string expected, got unpadded hex of length ' + hl);\n const array = new Uint8Array(al);\n for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {\n const n1 = asciiToBase16(hex.charCodeAt(hi));\n const n2 = asciiToBase16(hex.charCodeAt(hi + 1));\n if (n1 === undefined || n2 === undefined) {\n const char = hex[hi] + hex[hi + 1];\n throw new Error('hex string expected, got non-hex character \"' + char + '\" at index ' + hi);\n }\n array[ai] = n1 * 16 + n2; // multiply first octet, e.g. 'a3' => 10*16+3 => 160 + 3 => 163\n }\n return array;\n}\n/**\n * There is no setImmediate in browser and setTimeout is slow.\n * Call of async fn will return Promise, which will be fullfiled only on\n * next scheduler queue processing step and this is exactly what we need.\n */\nexport const nextTick = async () => { };\n/** Returns control to thread each 'tick' ms to avoid blocking. */\nexport async function asyncLoop(iters, tick, cb) {\n let ts = Date.now();\n for (let i = 0; i < iters; i++) {\n cb(i);\n // Date.now() is not monotonic, so in case if clock goes backwards we return return control too\n const diff = Date.now() - ts;\n if (diff >= 0 && diff < tick)\n continue;\n await nextTick();\n ts += diff;\n }\n}\n/**\n * Converts string to bytes using UTF8 encoding.\n * @example utf8ToBytes('abc') // Uint8Array.from([97, 98, 99])\n */\nexport function utf8ToBytes(str) {\n if (typeof str !== 'string')\n throw new Error('string expected');\n return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809\n}\n/**\n * Converts bytes to string using UTF8 encoding.\n * @example bytesToUtf8(Uint8Array.from([97, 98, 99])) // 'abc'\n */\nexport function bytesToUtf8(bytes) {\n return new TextDecoder().decode(bytes);\n}\n/**\n * Normalizes (non-hex) string or Uint8Array to Uint8Array.\n * Warning: when Uint8Array is passed, it would NOT get copied.\n * Keep in mind for future mutable operations.\n */\nexport function toBytes(data) {\n if (typeof data === 'string')\n data = utf8ToBytes(data);\n abytes(data);\n return data;\n}\n/**\n * Helper for KDFs: consumes uint8array or string.\n * When string is passed, does utf8 decoding, using TextDecoder.\n */\nexport function kdfInputToBytes(data) {\n if (typeof data === 'string')\n data = utf8ToBytes(data);\n abytes(data);\n return data;\n}\n/** Copies several Uint8Arrays into one. */\nexport function concatBytes(...arrays) {\n let sum = 0;\n for (let i = 0; i < arrays.length; i++) {\n const a = arrays[i];\n abytes(a);\n sum += a.length;\n }\n const res = new Uint8Array(sum);\n for (let i = 0, pad = 0; i < arrays.length; i++) {\n const a = arrays[i];\n res.set(a, pad);\n pad += a.length;\n }\n return res;\n}\nexport function checkOpts(defaults, opts) {\n if (opts !== undefined && {}.toString.call(opts) !== '[object Object]')\n throw new Error('options should be object or undefined');\n const merged = Object.assign(defaults, opts);\n return merged;\n}\n/** For runtime check if class implements interface */\nexport class Hash {\n}\n/** Wraps hash function, creating an interface on top of it */\nexport function createHasher(hashCons) {\n const hashC = (msg) => hashCons().update(toBytes(msg)).digest();\n const tmp = hashCons();\n hashC.outputLen = tmp.outputLen;\n hashC.blockLen = tmp.blockLen;\n hashC.create = () => hashCons();\n return hashC;\n}\nexport function createOptHasher(hashCons) {\n const hashC = (msg, opts) => hashCons(opts).update(toBytes(msg)).digest();\n const tmp = hashCons({});\n hashC.outputLen = tmp.outputLen;\n hashC.blockLen = tmp.blockLen;\n hashC.create = (opts) => hashCons(opts);\n return hashC;\n}\nexport function createXOFer(hashCons) {\n const hashC = (msg, opts) => hashCons(opts).update(toBytes(msg)).digest();\n const tmp = hashCons({});\n hashC.outputLen = tmp.outputLen;\n hashC.blockLen = tmp.blockLen;\n hashC.create = (opts) => hashCons(opts);\n return hashC;\n}\nexport const wrapConstructor = createHasher;\nexport const wrapConstructorWithOpts = createOptHasher;\nexport const wrapXOFConstructorWithOpts = createXOFer;\n/** Cryptographically secure PRNG. Uses internal OS-level `crypto.getRandomValues`. */\nexport function randomBytes(bytesLength = 32) {\n if (crypto && typeof crypto.getRandomValues === 'function') {\n return crypto.getRandomValues(new Uint8Array(bytesLength));\n }\n // Legacy Node.js compatibility\n if (crypto && typeof crypto.randomBytes === 'function') {\n return Uint8Array.from(crypto.randomBytes(bytesLength));\n }\n throw new Error('crypto.getRandomValues must be defined');\n}\n//# sourceMappingURL=utils.js.map","/**\n * Hex, bytes and number utilities.\n * @module\n */\n/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */\nimport { abytes as abytes_, bytesToHex as bytesToHex_, concatBytes as concatBytes_, hexToBytes as hexToBytes_, isBytes as isBytes_, } from '@noble/hashes/utils.js';\nexport { abytes, anumber, bytesToHex, bytesToUtf8, concatBytes, hexToBytes, isBytes, randomBytes, utf8ToBytes, } from '@noble/hashes/utils.js';\nconst _0n = /* @__PURE__ */ BigInt(0);\nconst _1n = /* @__PURE__ */ BigInt(1);\nexport function abool(title, value) {\n if (typeof value !== 'boolean')\n throw new Error(title + ' boolean expected, got ' + value);\n}\n// tmp name until v2\nexport function _abool2(value, title = '') {\n if (typeof value !== 'boolean') {\n const prefix = title && `\"${title}\"`;\n throw new Error(prefix + 'expected boolean, got type=' + typeof value);\n }\n return value;\n}\n// tmp name until v2\n/** Asserts something is Uint8Array. */\nexport function _abytes2(value, length, title = '') {\n const bytes = isBytes_(value);\n const len = value?.length;\n const needsLen = length !== undefined;\n if (!bytes || (needsLen && len !== length)) {\n const prefix = title && `\"${title}\" `;\n const ofLen = needsLen ? ` of length ${length}` : '';\n const got = bytes ? `length=${len}` : `type=${typeof value}`;\n throw new Error(prefix + 'expected Uint8Array' + ofLen + ', got ' + got);\n }\n return value;\n}\n// Used in weierstrass, der\nexport function numberToHexUnpadded(num) {\n const hex = num.toString(16);\n return hex.length & 1 ? '0' + hex : hex;\n}\nexport function hexToNumber(hex) {\n if (typeof hex !== 'string')\n throw new Error('hex string expected, got ' + typeof hex);\n return hex === '' ? _0n : BigInt('0x' + hex); // Big Endian\n}\n// BE: Big Endian, LE: Little Endian\nexport function bytesToNumberBE(bytes) {\n return hexToNumber(bytesToHex_(bytes));\n}\nexport function bytesToNumberLE(bytes) {\n abytes_(bytes);\n return hexToNumber(bytesToHex_(Uint8Array.from(bytes).reverse()));\n}\nexport function numberToBytesBE(n, len) {\n return hexToBytes_(n.toString(16).padStart(len * 2, '0'));\n}\nexport function numberToBytesLE(n, len) {\n return numberToBytesBE(n, len).reverse();\n}\n// Unpadded, rarely used\nexport function numberToVarBytesBE(n) {\n return hexToBytes_(numberToHexUnpadded(n));\n}\n/**\n * Takes hex string or Uint8Array, converts to Uint8Array.\n * Validates output length.\n * Will throw error for other types.\n * @param title descriptive title for an error e.g. 'secret key'\n * @param hex hex string or Uint8Array\n * @param expectedLength optional, will compare to result array's length\n * @returns\n */\nexport function ensureBytes(title, hex, expectedLength) {\n let res;\n if (typeof hex === 'string') {\n try {\n res = hexToBytes_(hex);\n }\n catch (e) {\n throw new Error(title + ' must be hex string or Uint8Array, cause: ' + e);\n }\n }\n else if (isBytes_(hex)) {\n // Uint8Array.from() instead of hash.slice() because node.js Buffer\n // is instance of Uint8Array, and its slice() creates **mutable** copy\n res = Uint8Array.from(hex);\n }\n else {\n throw new Error(title + ' must be hex string or Uint8Array');\n }\n const len = res.length;\n if (typeof expectedLength === 'number' && len !== expectedLength)\n throw new Error(title + ' of length ' + expectedLength + ' expected, got ' + len);\n return res;\n}\n// Compares 2 u8a-s in kinda constant time\nexport function equalBytes(a, b) {\n if (a.length !== b.length)\n return false;\n let diff = 0;\n for (let i = 0; i < a.length; i++)\n diff |= a[i] ^ b[i];\n return diff === 0;\n}\n/**\n * Copies Uint8Array. We can't use u8a.slice(), because u8a can be Buffer,\n * and Buffer#slice creates mutable copy. Never use Buffers!\n */\nexport function copyBytes(bytes) {\n return Uint8Array.from(bytes);\n}\n/**\n * Decodes 7-bit ASCII string to Uint8Array, throws on non-ascii symbols\n * Should be safe to use for things expected to be ASCII.\n * Returns exact same result as utf8ToBytes for ASCII or throws.\n */\nexport function asciiToBytes(ascii) {\n return Uint8Array.from(ascii, (c, i) => {\n const charCode = c.charCodeAt(0);\n if (c.length !== 1 || charCode > 127) {\n throw new Error(`string contains non-ASCII character \"${ascii[i]}\" with code ${charCode} at position ${i}`);\n }\n return charCode;\n });\n}\n/**\n * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])\n */\n// export const utf8ToBytes: typeof utf8ToBytes_ = utf8ToBytes_;\n/**\n * Converts bytes to string using UTF8 encoding.\n * @example bytesToUtf8(Uint8Array.from([97, 98, 99])) // 'abc'\n */\n// export const bytesToUtf8: typeof bytesToUtf8_ = bytesToUtf8_;\n// Is positive bigint\nconst isPosBig = (n) => typeof n === 'bigint' && _0n <= n;\nexport function inRange(n, min, max) {\n return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;\n}\n/**\n * Asserts min <= n < max. NOTE: It's < max and not <= max.\n * @example\n * aInRange('x', x, 1n, 256n); // would assume x is in (1n..255n)\n */\nexport function aInRange(title, n, min, max) {\n // Why min <= n < max and not a (min < n < max) OR b (min <= n <= max)?\n // consider P=256n, min=0n, max=P\n // - a for min=0 would require -1: `inRange('x', x, -1n, P)`\n // - b would commonly require subtraction: `inRange('x', x, 0n, P - 1n)`\n // - our way is the cleanest: `inRange('x', x, 0n, P)\n if (!inRange(n, min, max))\n throw new Error('expected valid ' + title + ': ' + min + ' <= n < ' + max + ', got ' + n);\n}\n// Bit operations\n/**\n * Calculates amount of bits in a bigint.\n * Same as `n.toString(2).length`\n * TODO: merge with nLength in modular\n */\nexport function bitLen(n) {\n let len;\n for (len = 0; n > _0n; n >>= _1n, len += 1)\n ;\n return len;\n}\n/**\n * Gets single bit at position.\n * NOTE: first bit position is 0 (same as arrays)\n * Same as `!!+Array.from(n.toString(2)).reverse()[pos]`\n */\nexport function bitGet(n, pos) {\n return (n >> BigInt(pos)) & _1n;\n}\n/**\n * Sets single bit at position.\n */\nexport function bitSet(n, pos, value) {\n return n | ((value ? _1n : _0n) << BigInt(pos));\n}\n/**\n * Calculate mask for N bits. Not using ** operator with bigints because of old engines.\n * Same as BigInt(`0b${Array(i).fill('1').join('')}`)\n */\nexport const bitMask = (n) => (_1n << BigInt(n)) - _1n;\n/**\n * Minimal HMAC-DRBG from NIST 800-90 for RFC6979 sigs.\n * @returns function that will call DRBG until 2nd arg returns something meaningful\n * @example\n * const drbg = createHmacDRBG<Key>(32, 32, hmac);\n * drbg(seed, bytesToKey); // bytesToKey must return Key or undefined\n */\nexport function createHmacDrbg(hashLen, qByteLen, hmacFn) {\n if (typeof hashLen !== 'number' || hashLen < 2)\n throw new Error('hashLen must be a number');\n if (typeof qByteLen !== 'number' || qByteLen < 2)\n throw new Error('qByteLen must be a number');\n if (typeof hmacFn !== 'function')\n throw new Error('hmacFn must be a function');\n // Step B, Step C: set hashLen to 8*ceil(hlen/8)\n const u8n = (len) => new Uint8Array(len); // creates Uint8Array\n const u8of = (byte) => Uint8Array.of(byte); // another shortcut\n let v = u8n(hashLen); // Minimal non-full-spec HMAC-DRBG from NIST 800-90 for RFC6979 sigs.\n let k = u8n(hashLen); // Steps B and C of RFC6979 3.2: set hashLen, in our case always same\n let i = 0; // Iterations counter, will throw when over 1000\n const reset = () => {\n v.fill(1);\n k.fill(0);\n i = 0;\n };\n const h = (...b) => hmacFn(k, v, ...b); // hmac(k)(v, ...values)\n const reseed = (seed = u8n(0)) => {\n // HMAC-DRBG reseed() function. Steps D-G\n k = h(u8of(0x00), seed); // k = hmac(k || v || 0x00 || seed)\n v = h(); // v = hmac(k || v)\n if (seed.length === 0)\n return;\n k = h(u8of(0x01), seed); // k = hmac(k || v || 0x01 || seed)\n v = h(); // v = hmac(k || v)\n };\n const gen = () => {\n // HMAC-DRBG generate() function\n if (i++ >= 1000)\n throw new Error('drbg: tried 1000 values');\n let len = 0;\n const out = [];\n while (len < qByteLen) {\n v = h();\n const sl = v.slice();\n out.push(sl);\n len += v.length;\n }\n return concatBytes_(...out);\n };\n const genUntil = (seed, pred) => {\n reset();\n reseed(seed); // Steps D-G\n let res = undefined; // Step H: grind until k is in [1..n-1]\n while (!(res = pred(gen())))\n reseed();\n reset();\n return res;\n };\n return genUntil;\n}\n// Validating curves and fields\nconst validatorFns = {\n bigint: (val) => typeof val === 'bigint',\n function: (val) => typeof val === 'function',\n boolean: (val) => typeof val === 'boolean',\n string: (val) => typeof val === 'string',\n stringOrUint8Array: (val) => typeof val === 'string' || isBytes_(val),\n isSafeInteger: (val) => Number.isSafeInteger(val),\n array: (val) => Array.isArray(val),\n field: (val, object) => object.Fp.isValid(val),\n hash: (val) => typeof val === 'function' && Number.isSafeInteger(val.outputLen),\n};\n// type Record<K extends string | number | symbol, T> = { [P in K]: T; }\nexport function validateObject(object, validators, optValidators = {}) {\n const checkField = (fieldName, type, isOptional) => {\n const checkVal = validatorFns[type];\n if (typeof checkVal !== 'function')\n throw new Error('invalid validator function');\n const val = object[fieldName];\n if (isOptional && val === undefined)\n return;\n if (!checkVal(val, object)) {\n throw new Error('param ' + String(fieldName) + ' is invalid. Expected ' + type + ', got ' + val);\n }\n };\n for (const [fieldName, type] of Object.entries(validators))\n checkField(fieldName, type, false);\n for (const [fieldName, type] of Object.entries(optValidators))\n checkField(fieldName, type, true);\n return object;\n}\n// validate type tests\n// const o: { a: number; b: number; c: number } = { a: 1, b: 5, c: 6 };\n// const z0 = validateObject(o, { a: 'isSafeInteger' }, { c: 'bigint' }); // Ok!\n// // Should fail type-check\n// const z1 = validateObject(o, { a: 'tmp' }, { c: 'zz' });\n// const z2 = validateObject(o, { a: 'isSafeInteger' }, { c: 'zz' });\n// const z3 = validateObject(o, { test: 'boolean', z: 'bug' });\n// const z4 = validateObject(o, { a: 'boolean', z: 'bug' });\nexport function isHash(val) {\n return typeof val === 'function' && Number.isSafeInteger(val.outputLen);\n}\nexport function _validateObject(object, fields, optFields = {}) {\n if (!object || typeof object !== 'object')\n throw new Error('expected valid options object');\n function checkField(fieldName, expectedType, isOpt) {\n const val = object[fieldName];\n if (isOpt && val === undefined)\n return;\n const current = typeof val;\n if (current !== expectedType || val === null)\n throw new Error(`param \"${fieldName}\" is invalid: expected ${expectedType}, got ${current}`);\n }\n Object.entries(fields).forEach(([k, v]) => checkField(k, v, false));\n Object.entries(optFields).forEach(([k, v]) => checkField(k, v, true));\n}\n/**\n * throws not implemented error\n */\nexport const notImplemented = () => {\n throw new Error('not implemented');\n};\n/**\n * Memoizes (caches) computation result.\n * Uses WeakMap: the value is going auto-cleaned by GC after last reference is removed.\n */\nexport function memoized(fn) {\n const map = new WeakMap();\n return (arg, ...args) => {\n const val = map.get(arg);\n if (val !== undefined)\n return val;\n const computed = fn(arg, ...args);\n map.set(arg, computed);\n return computed;\n };\n}\n//# sourceMappingURL=utils.js.map","/**\n * Utils for modular division and fields.\n * Field over 11 is a finite (Galois) field is integer number operations `mod 11`.\n * There is no division: it is replaced by modular multiplicative inverse.\n * @module\n */\n/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */\nimport { _validateObject, anumber, bitMask, bytesToNumberBE, bytesToNumberLE, ensureBytes, numberToBytesBE, numberToBytesLE, } from \"../utils.js\";\n// prettier-ignore\nconst _0n = BigInt(0), _1n = BigInt(1), _2n = /* @__PURE__ */ BigInt(2), _3n = /* @__PURE__ */ BigInt(3);\n// prettier-ignore\nconst _4n = /* @__PURE__ */ BigInt(4), _5n = /* @__PURE__ */ BigInt(5), _7n = /* @__PURE__ */ BigInt(7);\n// prettier-ignore\nconst _8n = /* @__PURE__ */ BigInt(8), _9n = /* @__PURE__ */ BigInt(9), _16n = /* @__PURE__ */ BigInt(16);\n// Calculates a modulo b\nexport function mod(a, b) {\n const result = a % b;\n return result >= _0n ? result : b + result;\n}\n/**\n * Efficiently raise num to power and do modular division.\n * Unsafe in some contexts: uses ladder, so can expose bigint bits.\n * @example\n * pow(2n, 6n, 11n) // 64n % 11n == 9n\n */\nexport function pow(num, power, modulo) {\n return FpPow(Field(modulo), num, power);\n}\n/** Does `x^(2^power)` mod p. `pow2(30, 4)` == `30^(2^4)` */\nexport function pow2(x, power, modulo) {\n let res = x;\n while (power-- > _0n) {\n res *= res;\n res %= modulo;\n }\n return res;\n}\n/**\n * Inverses number over modulo.\n * Implemented using [Euclidean GCD](https://brilliant.org/wiki/extended-euclidean-algorithm/).\n */\nexport function invert(number, modulo) {\n if (number === _0n)\n throw new Error('invert: expected non-zero number');\n if (modulo <= _0n)\n throw new Error('invert: expected positive modulus, got ' + modulo);\n // Fermat's little theorem \"CT-like\" version inv(n) = n^(m-2) mod m is 30x slower.\n let a = mod(number, modulo);\n let b = modulo;\n // prettier-ignore\n let x = _0n, y = _1n, u = _1n, v = _0n;\n while (a !== _0n) {\n // JIT applies optimization if those two lines follow each other\n const q = b / a;\n const r = b % a;\n const m = x - u * q;\n const n = y - v * q;\n // prettier-ignore\n b = a, a = r, x = u, y = v, u = m, v = n;\n }\n const gcd = b;\n if (gcd !== _1n)\n throw new Error('invert: does not exist');\n return mod(x, modulo);\n}\nfunction assertIsSquare(Fp, root, n) {\n if (!Fp.eql(Fp.sqr(root), n))\n throw new Error('Cannot find square root');\n}\n// Not all roots are possible! Example which will throw:\n// const NUM =\n// n = 72057594037927816n;\n// Fp = Field(BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab'));\nfunction sqrt3mod4(Fp, n) {\n const p1div4 = (Fp.ORDER + _1n) / _4n;\n const root = Fp.pow(n, p1div4);\n assertIsSquare(Fp, root, n);\n return root;\n}\nfunction sqrt5mod8(Fp, n) {\n const p5div8 = (Fp.ORDER - _5n) / _8n;\n const n2 = Fp.mul(n, _2n);\n const v = Fp.pow(n2, p5div8);\n const nv = Fp.mul(n, v);\n const i = Fp.mul(Fp.mul(nv, _2n), v);\n const root = Fp.mul(nv, Fp.sub(i, Fp.ONE));\n assertIsSquare(Fp, root, n);\n return root;\n}\n// Based on RFC9380, Kong algorithm\n// prettier-ignore\nfunction sqrt9mod16(P) {\n const Fp_ = Field(P);\n const tn = tonelliShanks(P);\n const c1 = tn(Fp_, Fp_.neg(Fp_.ONE)); // 1. c1 = sqrt(-1) in F, i.e., (c1^2) == -1 in F\n const c2 = tn(Fp_, c1); // 2. c2 = sqrt(c1) in F, i.e., (c2^2) == c1 in F\n const c3 = tn(Fp_, Fp_.neg(c1)); // 3. c3 = sqrt(-c1) in F, i.e., (c3^2) == -c1 in F\n const c4 = (P + _7n) / _16n; // 4. c4 = (q + 7) / 16 # Integer arithmetic\n return (Fp, n) => {\n let tv1 = Fp.pow(n, c4); // 1. tv1 = x^c4\n let tv2 = Fp.mul(tv1, c1); // 2. tv2 = c1 * tv1\n const tv3 = Fp.mul(tv1, c2); // 3. tv3 = c2 * tv1\n const tv4 = Fp.mul(tv1, c3); // 4. tv4 = c3 * tv1\n const e1 = Fp.eql(Fp.sqr(tv2), n); // 5. e1 = (tv2^2) == x\n const e2 = Fp.eql(Fp.sqr(tv3), n); // 6. e2 = (tv3^2) == x\n tv1 = Fp.cmov(tv1, tv2, e1); // 7. tv1 = CMOV(tv1, tv2, e1) # Select tv2 if (tv2^2) == x\n tv2 = Fp.cmov(tv4, tv3, e2); // 8. tv2 = CMOV(tv4, tv3, e2) # Select tv3 if (tv3^2) == x\n const e3 = Fp.eql(Fp.sqr(tv2), n); // 9. e3 = (tv2^2) == x\n const root = Fp.cmov(tv1, tv2, e3); // 10. z = CMOV(tv1, tv2, e3) # Select sqrt from tv1 & tv2\n assertIsSquare(Fp, root, n);\n return root;\n };\n}\n/**\n * Tonelli-Shanks square root search algorithm.\n * 1. https://eprint.iacr.org/2012/685.pdf (page 12)\n * 2. Square Roots from 1; 24, 51, 10 to Dan Shanks\n * @param P field order\n * @returns function that takes field Fp (created from P) and number n\n */\nexport function tonelliShanks(P) {\n // Initialization (precomputation).\n // Caching initialization could boost perf by 7%.\n if (P < _3n)\n throw new Error('sqrt is not defined for small field');\n // Factor P - 1 = Q * 2^S, where Q is odd\n let Q = P - _1n;\n let S = 0;\n while (Q % _2n === _0n) {\n Q /= _2n;\n S++;\n }\n // Find the first quadratic non-residue Z >= 2\n let Z = _2n;\n const _Fp = Field(P);\n while (FpLegendre(_Fp, Z) === 1) {\n // Basic primality test for P. After x iterations, chance of\n // not finding quadratic non-residue is 2^x, so 2^1000.\n if (Z++ > 1000)\n throw new Error('Cannot find square root: probably non-prime P');\n }\n // Fast-path; usually done before Z, but we do \"primality test\".\n if (S === 1)\n return sqrt3mod4;\n // Slow-path\n // TODO: test on Fp2 and others\n let cc = _Fp.pow(Z, Q); // c = z^Q\n const Q1div2 = (Q + _1n) / _2n;\n return function tonelliSlow(Fp, n) {\n if (Fp.is0(n))\n return n;\n // Check if n is a quadratic residue using Legendre symbol\n if (FpLegendre(Fp, n) !== 1)\n throw new Error('Cannot find square root');\n // Initialize variables for the main loop\n let M = S;\n let c = Fp.mul(Fp.ONE, cc); // c = z^Q, move cc from field _Fp into field Fp\n let t = Fp.pow(n, Q); // t = n^Q, first guess at the fudge factor\n let R = Fp.pow(n, Q1div2); // R = n^((Q+1)/2), first guess at the square root\n // Main loop\n // while t != 1\n while (!Fp.eql(t, Fp.ONE)) {\n if (Fp.is0(t))\n return Fp.ZERO; // if t=0 return R=0\n let i = 1;\n // Find the smallest i >= 1 such that t^(2^i) ≡ 1 (mod P)\n let t_tmp = Fp.sqr(t); // t^(2^1)\n while (!Fp.eql(t_tmp, Fp.ONE)) {\n i++;\n t_tmp = Fp.sqr(t_tmp); // t^(2^2)...\n if (i === M)\n throw new Error('Cannot find square root');\n }\n // Calculate the exponent for b: 2^(M - i - 1)\n const exponent = _1n << BigInt(M - i - 1); // bigint is important\n const b = Fp.pow(c, exponent); // b = 2^(M - i - 1)\n // Update variables\n M = i;\n c = Fp.sqr(b); // c = b^2\n t = Fp.mul(t, c); // t = (t * b^2)\n R = Fp.mul(R, b); // R = R*b\n }\n return R;\n };\n}\n/**\n * Square root for a finite field. Will try optimized versions first:\n *\n * 1. P ≡ 3 (mod 4)\n * 2. P ≡ 5 (mod 8)\n * 3. P ≡ 9 (mod 16)\n * 4. Tonelli-Shanks algorithm\n *\n * Different algorithms can give different roots, it is up to user to decide which one they want.\n * For example there is FpSqrtOdd/FpSqrtEven to choice root based on oddness (used for hash-to-curve).\n */\nexport function FpSqrt(P) {\n // P ≡ 3 (mod 4) => √n = n^((P+1)/4)\n if (P % _4n === _3n)\n return sqrt3mod4;\n // P ≡ 5 (mod 8) => Atkin algorithm, page 10 of https://eprint.iacr.org/2012/685.pdf\n if (P % _8n === _5n)\n return sqrt5mod8;\n // P ≡ 9 (mod 16) => Kong algorithm, page 11 of https://eprint.iacr.org/2012/685.pdf (algorithm 4)\n if (P % _16n === _9n)\n return sqrt9mod16(P);\n // Tonelli-Shanks algorithm\n return tonelliShanks(P);\n}\n// Little-endian check for first LE bit (last BE bit);\nexport const isNegativeLE = (num, modulo) => (mod(num, modulo) & _1n) === _1n;\n// prettier-ignore\nconst FIELD_FIELDS = [\n 'create', 'isValid', 'is0', 'neg', 'inv', 'sqrt', 'sqr',\n 'eql', 'add', 'sub', 'mul', 'pow', 'div',\n 'addN', 'subN', 'mulN', 'sqrN'\n];\nexport function validateField(field) {\n const initial = {\n ORDER: 'bigint',\n MASK: 'bigint',\n BYTES: 'number',\n BITS: 'number',\n };\n const opts = FIELD_FIELDS.reduce((map, val) => {\n map[val] = 'function';\n return map;\n }, initial);\n _validateObject(field, opts);\n // const max = 16384;\n // if (field.BYTES < 1 || field.BYTES > max) throw new Error('invalid field');\n // if (field.BITS < 1 || field.BITS > 8 * max) throw new Error('invalid field');\n return field;\n}\n// Generic field functions\n/**\n * Same as `pow` but for Fp: non-constant-time.\n * Unsafe in some contexts: uses ladder, so can expose bigint bits.\n */\nexport function FpPow(Fp, num, power) {\n if (power < _0n)\n throw new Error('invalid exponent, negatives unsupported');\n if (power === _0n)\n return Fp.ONE;\n if (power === _1n)\n return num;\n let p = Fp.ONE;\n let d = num;\n while (power > _0n) {\n if (power & _1n)\n p = Fp.mul(p, d);\n d = Fp.sqr(d);\n power >>= _1n;\n }\n return p;\n}\n/**\n * Efficiently invert an array of Field elements.\n * Exception-free. Will return `undefined` for 0 elements.\n * @param passZero map 0 to 0 (instead of undefined)\n */\nexport function FpInvertBatch(Fp, nums, passZero = false) {\n const inverted = new Array(nums.length).fill(passZero ? Fp.ZERO : undefined);\n // Walk from first to last, multiply them by each other MOD p\n const multipliedAcc = nums.reduce((acc, num, i) => {\n if (Fp.is0(num))\n return acc;\n inverted[i] = acc;\n return Fp.mul(acc, num);\n }, Fp.ONE);\n // Invert last element\n const invertedAcc = Fp.inv(multipliedAcc);\n // Walk from last to first, multiply them by inverted each other MOD p\n nums.reduceRight((acc, num, i) => {\n if (Fp.is0(num))\n return acc;\n inverted[i] = Fp.mul(acc, inverted[i]);\n return Fp.mul(acc, num);\n }, invertedAcc);\n return inverted;\n}\n// TODO: remove\nexport function FpDiv(Fp, lhs, rhs) {\n return Fp.mul(lhs, typeof rhs === 'bigint' ? invert(rhs, Fp.ORDER) : Fp.inv(rhs));\n}\n/**\n * Legendre symbol.\n * Legendre constant is used to calculate Legendre symbol (a | p)\n * which denotes the value of a^((p-1)/2) (mod p).\n *\n * * (a | p) ≡ 1 if a is a square (mod p), quadratic residue\n * * (a | p) ≡ -1 if a is not a square (mod p), quadratic non residue\n * * (a | p) ≡ 0 if a ≡ 0 (mod p)\n */\nexport function FpLegendre(Fp, n) {\n // We can use 3rd argument as optional cache of this value\n // but seems unneeded for now. The operation is very fast.\n const p1mod2 = (Fp.ORDER - _1n) / _2n;\n const powered = Fp.pow(n, p1mod2);\n const yes = Fp.eql(powered, Fp.ONE);\n const zero = Fp.eql(powered, Fp.ZERO);\n const no = Fp.eql(powered, Fp.neg(Fp.ONE));\n if (!yes && !zero && !no)\n throw new Error('invalid Legendre symbol result');\n return yes ? 1 : zero ? 0 : -1;\n}\n// This function returns True whenever the value x is a square in the field F.\nexport function FpIsSquare(Fp, n) {\n const l = FpLegendre(Fp, n);\n return l === 1;\n}\n// CURVE.n lengths\nexport function nLength(n, nBitLength) {\n // Bit size, byte size of CURVE.n\n if (nBitLength !== undefined)\n anumber(nBitLength);\n const _nBitLength = nBitLength !== undefined ? nBitLength : n.toString(2).length;\n const nByteLength = Math.ceil(_nBitLength / 8);\n return { nBitLength: _nBitLength, nByteLength };\n}\n/**\n * Creates a finite field. Major performance optimizations:\n * * 1. Denormalized operations like mulN instead of mul.\n * * 2. Identical object shape: never add or remove keys.\n * * 3. `Object.freeze`.\n * Fragile: always run a benchmark on a change.\n * Security note: operations don't check 'isValid' for all elements for performance reasons,\n * it is caller responsibility to check this.\n * This is low-level code, please make sure you know what you're doing.\n *\n * Note about field properties:\n * * CHARACTERISTIC p = prime number, number of elements in main subgroup.\n * * ORDER q = similar to cofactor in curves, may be composite `q = p^m`.\n *\n * @param ORDER field order, probably prime, or could be composite\n * @param bitLen how many bits the field consumes\n * @param isLE (default: false) if encoding / decoding should be in little-endian\n * @param redef optional faster redefinitions of sqrt and other methods\n */\nexport function Field(ORDER, bitLenOrOpts, // TODO: use opts only in v2?\nisLE = false, opts = {}) {\n if (ORDER <= _0n)\n throw new Error('invalid field: expected ORDER > 0, got ' + ORDER);\n let _nbitLength = undefined;\n let _sqrt = undefined;\n let modFromBytes = false;\n let allowedLengths = undefined;\n if (typeof bitLenOrOpts === 'object' && bitLenOrOpts != null) {\n if (opts.sqrt || isLE)\n throw new Error('cannot specify opts in two arguments');\n const _opts = bitLenOrOpts;\n if (_opts.BITS)\n _nbitLength = _opts.BITS;\n if (_opts.sqrt)\n _sqrt = _opts.sqrt;\n if (typeof _opts.isLE === 'boolean')\n isLE = _opts.isLE;\n if (typeof _opts.modFromBytes === 'boolean')\n modFromBytes = _opts.modFromBytes;\n allowedLengths = _opts.allowedLengths;\n }\n else {\n if (typeof bitLenOrOpts === 'number')\n _nbitLength = bitLenOrOpts;\n if (opts.sqrt)\n _sqrt = opts.sqrt;\n }\n const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, _nbitLength);\n if (BYTES > 2048)\n throw new Error('invalid field: expected ORDER of <= 2048 bytes');\n let sqrtP; // cached sqrtP\n const f = Object.freeze({\n ORDER,\n isLE,\n BITS,\n BYTES,\n MASK: bitMask(BITS),\n ZERO: _0n,\n ONE: _1n,\n allowedLengths: allowedLengths,\n create: (num) => mod(num, ORDER),\n isValid: (num) => {\n if (typeof num !== 'bigint')\n throw new Error('invalid field element: expected bigint, got ' + typeof num);\n return _0n <= num && num < ORDER; // 0 is valid element, but it's not invertible\n },\n is0: (num) => num === _0n,\n // is valid and invertible\n isValidNot0: (num) => !f.is0(num) && f.isValid(num),\n isOdd: (num) => (num & _1n) === _1n,\n neg: (num) => mod(-num, ORDER),\n eql: (lhs, rhs) => lhs === rhs,\n sqr: (num) => mod(num * num, ORDER),\n add: (lhs, rhs) => mod(lhs + rhs, ORDER),\n sub: (lhs, rhs) => mod(lhs - rhs, ORDER),\n mul: (lhs, rhs) => mod(lhs * rhs, ORDER),\n pow: (num, power) => FpPow(f, num, power),\n div: (lhs, rhs) => mod(lhs * invert(rhs, ORDER), ORDER),\n // Same as above, but doesn't normalize\n sqrN: (num) => num * num,\n addN: (lhs, rhs) => lhs + rhs,\n subN: (lhs, rhs) => lhs - rhs,\n mulN: (lhs, rhs) => lhs * rhs,\n inv: (num) => invert(num, ORDER),\n sqrt: _sqrt ||\n ((n) => {\n if (!sqrtP)\n sqrtP = FpSqrt(ORDER);\n return sqrtP(f, n);\n }),\n toBytes: (num) => (isLE ? numberToBytesLE(num, BYTES) : numberToBytesBE(num, BYTES)),\n fromBytes: (bytes, skipValidation = true) => {\n if (allowedLengths) {\n if (!allowedLengths.includes(bytes.length) || bytes.length > BYTES) {\n throw new Error('Field.fromBytes: expected ' + allowedLengths + ' bytes, got ' + bytes.length);\n }\n const padded = new Uint8Array(BYTES);\n // isLE add 0 to right, !isLE to the left.\n padded.set(bytes, isLE ? 0 : padded.length - bytes.length);\n bytes = padded;\n }\n if (bytes.length !== BYTES)\n throw new Error('Field.fromBytes: expected ' + BYTES + ' bytes, got ' + bytes.length);\n let scalar = isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);\n if (modFromBytes)\n scalar = mod(scalar, ORDER);\n if (!skipValidation)\n if (!f.isValid(scalar))\n throw new Error('invalid field element: outside of range 0..ORDER');\n // NOTE: we don't validate scalar here, please use isValid. This done such way because some\n // protocol may allow non-reduced scalar that reduced later or changed some other way.\n return scalar;\n },\n // TODO: we don't need it here, move out to separate fn\n invertBatch: (lst) => FpInvertBatch(f, lst),\n // We can't move this out because Fp6, Fp12 implement it\n // and it's unclear what to return in there.\n cmov: (a, b, c) => (c ? b : a),\n });\n return Object.freeze(f);\n}\n// Generic random scalar, we can do same for other fields if via Fp2.mul(Fp2.ONE, Fp2.random)?\n// This allows unsafe methods like ignore bias or zero. These unsafe, but often used in different protocols (if deterministic RNG).\n// which mean we cannot force this via opts.\n// Not sure what to do with randomBytes, we can accept it inside opts if wanted.\n// Probably need to export getMinHashLength somewhere?\n// random(bytes?: Uint8Array, unsafeAllowZero = false, unsafeAllowBias = false) {\n// const LEN = !unsafeAllowBias ? getMinHashLength(ORDER) : BYTES;\n// if (bytes === undefined) bytes = randomBytes(LEN); // _opts.randomBytes?\n// const num = isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);\n// // `mod(x, 11)` can sometimes produce 0. `mod(x, 10) + 1` is the same, but no 0\n// const reduced = unsafeAllowZero ? mod(num, ORDER) : mod(num, ORDER - _1n) + _1n;\n// return reduced;\n// },\nexport function FpSqrtOdd(Fp, elm) {\n if (!Fp.isOdd)\n throw new Error(\"Field doesn't have isOdd\");\n const root = Fp.sqrt(elm);\n return Fp.isOdd(root) ? root : Fp.neg(root);\n}\nexport function FpSqrtEven(Fp, elm) {\n if (!Fp.isOdd)\n throw new Error(\"Field doesn't have isOdd\");\n const root = Fp.sqrt(elm);\n return Fp.isOdd(root) ? Fp.neg(root) : root;\n}\n/**\n * \"Constant-time\" private key generation utility.\n * Same as mapKeyToField, but accepts less bytes (40 instead of 48 for 32-byte field).\n * Which makes it slightly more biased, less secure.\n * @deprecated use `mapKeyToField` instead\n */\nexport function hashToPrivateScalar(hash, groupOrder, isLE = false) {\n hash = ensureBytes('privateHash', hash);\n const hashLen = hash.length;\n const minLen = nLength(groupOrder).nByteLength + 8;\n if (minLen < 24 || hashLen < minLen || hashLen > 1024)\n throw new Error('hashToPrivateScalar: expected ' + minLen + '-1024 bytes of input, got ' + hashLen);\n const num = isLE ? bytesToNumberLE(hash) : bytesToNumberBE(hash);\n return mod(num, groupOrder - _1n) + _1n;\n}\n/**\n * Returns total number of bytes consumed by the field element.\n * For example, 32 bytes for usual 256-bit weierstrass curve.\n * @param fieldOrder number of field elements, usually CURVE.n\n * @returns byte length of field\n */\nexport function getFieldBytesLength(fieldOrder) {\n if (typeof fieldOrder !== 'bigint')\n throw new Error('field order must be bigint');\n const bitLength = fieldOrder.toString(2).length;\n return Math.ceil(bitLength / 8);\n}\n/**\n * Returns minimal amount of bytes that can be safely reduced\n * by field order.\n * Should be 2^-128 for 128-bit curve such as P256.\n * @param fieldOrder number of field elements, usually CURVE.n\n * @returns byte length of target hash\n */\nexport function getMinHashLength(fieldOrder) {\n const length = getFieldBytesLength(fieldOrder);\n return length + Math.ceil(length / 2);\n}\n/**\n * \"Constant-time\" private key generation utility.\n * Can take (n + n/2) or more bytes of uniform input e.g. from CSPRNG or KDF\n * and convert them into private scalar, with the modulo bias being negligible.\n * Needs at least 48 bytes of input for 32-byte private key.\n * https://research.kudelskisecurity.com/2020/07/28/the-definitive-guide-to-modulo-bias-and-how-to-avoid-it/\n * FIPS 186-5, A.2 https://csrc.nist.gov/publications/detail/fips/186/5/final\n * RFC 9380, https://www.rfc-editor.org/rfc/rfc9380#section-5\n * @param hash hash output from SHA3 or a similar function\n * @param groupOrder size of subgroup - (e.g. secp256k1.CURVE.n)\n * @param isLE interpret hash bytes as LE num\n * @returns valid private scalar\n */\nexport function mapHashToField(key, fieldOrder, isLE = false) {\n const len = key.length;\n const fieldLen = getFieldBytesLength(fieldOrder);\n const minLen = getMinHashLength(fieldOrder);\n // No small numbers: need to understand bias story. No huge numbers: easier to detect JS timings.\n if (len < 16 || len < minLen || len > 1024)\n throw new Error('expected ' + minLen + '-1024 bytes of input, got ' + len);\n const num = isLE ? bytesToNumberLE(key) : bytesToNumberBE(key);\n // `mod(x, 11)` can sometimes produce 0. `mod(x, 10) + 1` is the same, but no 0\n const reduced = mod(num, fieldOrder - _1n) + _1n;\n return isLE ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);\n}\n//# sourceMappingURL=modular.js.map","/**\n * Montgomery curve methods. It's not really whole montgomery curve,\n * just bunch of very specific methods for X25519 / X448 from\n * [RFC 7748](https://www.rfc-editor.org/rfc/rfc7748)\n * @module\n */\n/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */\nimport { _validateObject, abytes, aInRange, bytesToNumberLE, ensureBytes, numberToBytesLE, randomBytes, } from \"../utils.js\";\nimport { mod } from \"./modular.js\";\nconst _0n = BigInt(0);\nconst _1n = BigInt(1);\nconst _2n = BigInt(2);\nfunction validateOpts(curve) {\n _validateObject(curve, {\n adjustScalarBytes: 'function',\n powPminus2: 'function',\n });\n return Object.freeze({ ...curve });\n}\nexport function montgomery(curveDef) {\n const CURVE = validateOpts(curveDef);\n const { P, type, adjustScalarBytes, powPminus2, randomBytes: rand } = CURVE;\n const is25519 = type === 'x25519';\n if (!is25519 && type !== 'x448')\n throw new Error('invalid type');\n const randomBytes_ = rand || randomBytes;\n const montgomeryBits = is25519 ? 255 : 448;\n const fieldLen = is25519 ? 32 : 56;\n const Gu = is25519 ? BigInt(9) : BigInt(5);\n // RFC 7748 #5:\n // The constant a24 is (486662 - 2) / 4 = 121665 for curve25519/X25519 and\n // (156326 - 2) / 4 = 39081 for curve448/X448\n // const a = is25519 ? 156326n : 486662n;\n const a24 = is25519 ? BigInt(121665) : BigInt(39081);\n // RFC: x25519 \"the resulting integer is of the form 2^254 plus\n // eight times a value between 0 and 2^251 - 1 (inclusive)\"\n // x448: \"2^447 plus four times a value between 0 and 2^445 - 1 (inclusive)\"\n const minScalar = is25519 ? _2n ** BigInt(254) : _2n ** BigInt(447);\n const maxAdded = is25519\n ? BigInt(8) * _2n ** BigInt(251) - _1n\n : BigInt(4) * _2n ** BigInt(445) - _1n;\n const maxScalar = minScalar + maxAdded + _1n; // (inclusive)\n const modP = (n) => mod(n, P);\n const GuBytes = encodeU(Gu);\n function encodeU(u) {\n return numberToBytesLE(modP(u), fieldLen);\n }\n function decodeU(u) {\n const _u = ensureBytes('u coordinate', u, fieldLen);\n // RFC: When receiving such an array, implementations of X25519\n // (but not X448) MUST mask the most significant bit in the final byte.\n if (is25519)\n _u[31] &= 127; // 0b0111_1111\n // RFC: Implementations MUST accept non-canonical values and process them as\n // if they had been reduced modulo the field prime. The non-canonical\n // values are 2^255 - 19 through 2^255 - 1 for X25519 and 2^448 - 2^224\n // - 1 through 2^448 - 1 for X448.\n return modP(bytesToNumberLE(_u));\n }\n function decodeScalar(scalar) {\n return bytesToNumberLE(adjustScalarBytes(ensureBytes('scalar', scalar, fieldLen)));\n }\n function scalarMult(scalar, u) {\n const pu = montgomeryLadder(decodeU(u), decodeScalar(scalar));\n // Some public keys are useless, of low-order. Curve author doesn't think\n // it needs to be validated, but we do it nonetheless.\n // https://cr.yp.to/ecdh.html#validate\n if (pu === _0n)\n throw new Error('invalid private or public key received');\n return encodeU(pu);\n }\n // Computes public key from private. By doing scalar multiplication of base point.\n function scalarMultBase(scalar) {\n return scalarMult(scalar, GuBytes);\n }\n // cswap from RFC7748 \"example code\"\n function cswap(swap, x_2, x_3) {\n // dummy = mask(swap) AND (x_2 XOR x_3)\n // Where mask(swap) is the all-1 or all-0 word of the same length as x_2\n // and x_3, computed, e.g., as mask(swap) = 0 - swap.\n const dummy = modP(swap * (x_2 - x_3));\n x_2 = modP(x_2 - dummy); // x_2 = x_2 XOR dummy\n x_3 = modP(x_3 + dummy); // x_3 = x_3 XOR dummy\n return { x_2, x_3 };\n }\n /**\n * Montgomery x-only multiplication ladder.\n * @param pointU u coordinate (x) on Montgomery Curve 25519\n * @param scalar by which the point would be multiplied\n * @returns new Point on Montgomery curve\n */\n function montgomeryLadder(u, scalar) {\n aInRange('u', u, _0n, P);\n aInRange('scalar', scalar, minScalar, maxScalar);\n const k = scalar;\n const x_1 = u;\n let x_2 = _1n;\n let z_2 = _0n;\n let x_3 = u;\n let z_3 = _1n;\n let swap = _0n;\n for (let t = BigInt(montgomeryBits - 1); t >= _0n; t--) {\n const k_t = (k >> t) & _1n;\n swap ^= k_t;\n ({ x_2, x_3 } = cswap(swap, x_2, x_3));\n ({ x_2: z_2, x_3: z_3 } = cswap(swap, z_2, z_3));\n swap = k_t;\n const A = x_2 + z_2;\n const AA = modP(A * A);\n const B = x_2 - z_2;\n const BB = modP(B * B);\n const E = AA - BB;\n const C = x_3 + z_3;\n const D = x_3 - z_3;\n const DA = modP(D * A);\n const CB = modP(C * B);\n const dacb = DA + CB;\n const da_cb = DA - CB;\n x_3 = modP(dacb * dacb);\n z_3 = modP(x_1 * modP(da_cb * da_cb));\n x_2 = modP(AA * BB);\n z_2 = modP(E * (AA + modP(a24 * E)));\n }\n ({ x_2, x_3 } = cswap(swap, x_2, x_3));\n ({ x_2: z_2, x_3: z_3 } = cswap(swap, z_2, z_3));\n const z2 = powPminus2(z_2); // `Fp.pow(x, P - _2n)` is much slower equivalent\n return modP(x_2 * z2); // Return x_2 * (z_2^(p - 2))\n }\n const lengths = {\n secretKey: fieldLen,\n publicKey: fieldLen,\n seed: fieldLen,\n };\n const randomSecretKey = (seed = randomBytes_(fieldLen)) => {\n abytes(seed, lengths.seed);\n return seed;\n };\n function keygen(seed) {\n const secretKey = randomSecretKey(seed);\n return { secretKey, publicKey: scalarMultBase(secretKey) };\n }\n const utils = {\n randomSecretKey,\n randomPrivateKey: randomSecretKey,\n };\n return {\n keygen,\n getSharedSecret: (secretKey, publicKey) => scalarMult(secretKey, publicKey),\n getPublicKey: (secretKey) => scalarMultBase(secretKey),\n scalarMult,\n scalarMultBase,\n utils,\n GuBytes: GuBytes.slice(),\n lengths,\n };\n}\n//# sourceMappingURL=montgomery.js.map","/**\n * ed25519 Twisted Edwards curve with following addons:\n * - X25519 ECDH\n * - Ristretto cofactor elimination\n * - Elligator hash-to-group / point indistinguishability\n * @module\n */\n/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */\nimport { sha512 } from '@noble/hashes/sha2.js';\nimport { abytes, concatBytes, utf8ToBytes } from '@noble/hashes/utils.js';\nimport { pippenger } from \"./abstract/curve.js\";\nimport { PrimeEdwardsPoint, twistedEdwards, } from \"./abstract/edwards.js\";\nimport { _DST_scalar, createHasher, expand_message_xmd, } from \"./abstract/hash-to-curve.js\";\nimport { Field, FpInvertBatch, FpSqrtEven, isNegativeLE, mod, pow2, } from \"./abstract/modular.js\";\nimport { montgomery } from \"./abstract/montgomery.js\";\nimport { bytesToNumberLE, ensureBytes, equalBytes } from \"./utils.js\";\n// prettier-ignore\nconst _0n = /* @__PURE__ */ BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3);\n// prettier-ignore\nconst _5n = BigInt(5), _8n = BigInt(8);\n// P = 2n**255n-19n\nconst ed25519_CURVE_p = BigInt('0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed');\n// N = 2n**252n + 27742317777372353535851937790883648493n\n// a = Fp.create(BigInt(-1))\n// d = -121665/121666 a.k.a. Fp.neg(121665 * Fp.inv(121666))\nconst ed25519_CURVE = /* @__PURE__ */ (() => ({\n p: ed25519_CURVE_p,\n n: BigInt('0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed'),\n h: _8n,\n a: BigInt('0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec'),\n d: BigInt('0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3'),\n Gx: BigInt('0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a'),\n Gy: BigInt('0x6666666666666666666666666666666666666666666666666666666666666658'),\n}))();\nfunction ed25519_pow_2_252_3(x) {\n // prettier-ignore\n const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);\n const P = ed25519_CURVE_p;\n const x2 = (x * x) % P;\n const b2 = (x2 * x) % P; // x^3, 11\n const b4 = (pow2(b2, _2n, P) * b2) % P; // x^15, 1111\n const b5 = (pow2(b4, _1n, P) * x) % P; // x^31\n const b10 = (pow2(b5, _5n, P) * b5) % P;\n const b20 = (pow2(b10, _10n, P) * b10) % P;\n const b40 = (pow2(b20, _20n, P) * b20) % P;\n const b80 = (pow2(b40, _40n, P) * b40) % P;\n const b160 = (pow2(b80, _80n, P) * b80) % P;\n const b240 = (pow2(b160, _80n, P) * b80) % P;\n const b250 = (pow2(b240, _10n, P) * b10) % P;\n const pow_p_5_8 = (pow2(b250, _2n, P) * x) % P;\n // ^ To pow to (p+3)/8, multiply it by x.\n return { pow_p_5_8, b2 };\n}\nfunction adjustScalarBytes(bytes) {\n // Section 5: For X25519, in order to decode 32 random bytes as an integer scalar,\n // set the three least significant bits of the first byte\n bytes[0] &= 248; // 0b1111_1000\n // and the most significant bit of the last to zero,\n bytes[31] &= 127; // 0b0111_1111\n // set the second most significant bit of the last byte to 1\n bytes[31] |= 64; // 0b0100_0000\n return bytes;\n}\n// √(-1) aka √(a) aka 2^((p-1)/4)\n// Fp.sqrt(Fp.neg(1))\nconst ED25519_SQRT_M1 = /* @__PURE__ */ BigInt('19681161376707505956807079304988542015446066515923890162744021073123829784752');\n// sqrt(u/v)\nfunction uvRatio(u, v) {\n const P = ed25519_CURVE_p;\n const v3 = mod(v * v * v, P); // v³\n const v7 = mod(v3 * v3 * v, P); // v⁷\n // (p+3)/8 and (p-5)/8\n const pow = ed25519_pow_2_252_3(u * v7).pow_p_5_8;\n let x = mod(u * v3 * pow, P); // (uv³)(uv⁷)^(p-5)/8\n const vx2 = mod(v * x * x, P); // vx²\n const root1 = x; // First root candidate\n const root2 = mod(x * ED25519_SQRT_M1, P); // Second root candidate\n const useRoot1 = vx2 === u; // If vx² = u (mod p), x is a square root\n const useRoot2 = vx2 === mod(-u, P); // If vx² = -u, set x <-- x * 2^((p-1)/4)\n const noRoot = vx2 === mod(-u * ED25519_SQRT_M1, P); // There is no valid root, vx² = -u√(-1)\n if (useRoot1)\n x = root1;\n if (useRoot2 || noRoot)\n x = root2; // We return root2 anyway, for const-time\n if (isNegativeLE(x, P))\n x = mod(-x, P);\n return { isValid: useRoot1 || useRoot2, value: x };\n}\nconst Fp = /* @__PURE__ */ (() => Field(ed25519_CURVE.p, { isLE: true }))();\nconst Fn = /* @__PURE__ */ (() => Field(ed25519_CURVE.n, { isLE: true }))();\nconst ed25519Defaults = /* @__PURE__ */ (() => ({\n ...ed25519_CURVE,\n Fp,\n hash: sha512,\n adjustScalarBytes,\n // dom2\n // Ratio of u to v. Allows us to combine inversion and square root. Uses algo from RFC8032 5.1.3.\n // Constant-time, u/√v\n uvRatio,\n}))();\n/**\n * ed25519 curve with EdDSA signatures.\n * @example\n * import { ed25519 } from '@noble/curves/ed25519';\n * const { secretKey, publicKey } = ed25519.keygen();\n * const msg = new TextEncoder().encode('hello');\n * const sig = ed25519.sign(msg, priv);\n * ed25519.verify(sig, msg, pub); // Default mode: follows ZIP215\n * ed25519.verify(sig, msg, pub, { zip215: false }); // RFC8032 / FIPS 186-5\n */\nexport const ed25519 = /* @__PURE__ */ (() => twistedEdwards(ed25519Defaults))();\nfunction ed25519_domain(data, ctx, phflag) {\n if (ctx.length > 255)\n throw new Error('Context is too big');\n return concatBytes(utf8ToBytes('SigEd25519 no Ed25519 collisions'), new Uint8Array([phflag ? 1 : 0, ctx.length]), ctx, data);\n}\n/** Context of ed25519. Uses context for domain separation. */\nexport const ed25519ctx = /* @__PURE__ */ (() => twistedEdwards({\n ...ed25519Defaults,\n domain: ed25519_domain,\n}))();\n/** Prehashed version of ed25519. Accepts already-hashed messages in sign() and verify(). */\nexport const ed25519ph = /* @__PURE__ */ (() => twistedEdwards(Object.assign({}, ed25519Defaults, {\n domain: ed25519_domain,\n prehash: sha512,\n})))();\n/**\n * ECDH using curve25519 aka x25519.\n * @example\n * import { x25519 } from '@noble/curves/ed25519';\n * const priv = 'a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4';\n * const pub = 'e6db6867583030db3594c1a424b15f7c726624ec26b3353b10a903a6d0ab1c4c';\n * x25519.getSharedSecret(priv, pub) === x25519.scalarMult(priv, pub); // aliases\n * x25519.getPublicKey(priv) === x25519.scalarMultBase(priv);\n * x25519.getPublicKey(x25519.utils.randomSecretKey());\n */\nexport const x25519 = /* @__PURE__ */ (() => {\n const P = Fp.ORDER;\n return montgomery({\n P,\n type: 'x25519',\n powPminus2: (x) => {\n // x^(p-2) aka x^(2^255-21)\n const { pow_p_5_8, b2 } = ed25519_pow_2_252_3(x);\n return mod(pow2(pow_p_5_8, _3n, P) * b2, P);\n },\n adjustScalarBytes,\n });\n})();\n// Hash To Curve Elligator2 Map (NOTE: different from ristretto255 elligator)\n// NOTE: very important part is usage of FpSqrtEven for ELL2_C1_EDWARDS, since\n// SageMath returns different root first and everything falls apart\nconst ELL2_C1 = /* @__PURE__ */ (() => (ed25519_CURVE_p + _3n) / _8n)(); // 1. c1 = (q + 3) / 8 # Integer arithmetic\nconst ELL2_C2 = /* @__PURE__ */ (() => Fp.pow(_2n, ELL2_C1))(); // 2. c2 = 2^c1\nconst ELL2_C3 = /* @__PURE__ */ (() => Fp.sqrt(Fp.neg(Fp.ONE)))(); // 3. c3 = sqrt(-1)\n// prettier-ignore\nfunction map_to_curve_elligator2_curve25519(u) {\n const ELL2_C4 = (ed25519_CURVE_p - _5n) / _8n; // 4. c4 = (q - 5) / 8 # Integer arithmetic\n const ELL2_J = BigInt(486662);\n let tv1 = Fp.sqr(u); // 1. tv1 = u^2\n tv1 = Fp.mul(tv1, _2n); // 2. tv1 = 2 * tv1\n let xd = Fp.add(tv1, Fp.ONE); // 3. xd = tv1 + 1 # Nonzero: -1 is square (mod p), tv1 is not\n let x1n = Fp.neg(ELL2_J); // 4. x1n = -J # x1 = x1n / xd = -J / (1 + 2 * u^2)\n let tv2 = Fp.sqr(xd); // 5. tv2 = xd^2\n let gxd = Fp.mul(tv2, xd); // 6. gxd = tv2 * xd # gxd = xd^3\n let gx1 = Fp.mul(tv1, ELL2_J); // 7. gx1 = J * tv1 # x1n + J * xd\n gx1 = Fp.mul(gx1, x1n); // 8. gx1 = gx1 * x1n # x1n^2 + J * x1n * xd\n gx1 = Fp.add(gx1, tv2); // 9. gx1 = gx1 + tv2 # x1n^2 + J * x1n * xd + xd^2\n gx1 = Fp.mul(gx1, x1n); // 10. gx1 = gx1 * x1n # x1n^3 + J * x1n^2 * xd + x1n * xd^2\n let tv3 = Fp.sqr(gxd); // 11. tv3 = gxd^2\n tv2 = Fp.sqr(tv3); // 12. tv2 = tv3^2 # gxd^4\n tv3 = Fp.mul(tv3, gxd); // 13. tv3 = tv3 * gxd # gxd^3\n tv3 = Fp.mul(tv3, gx1); // 14. tv3 = tv3 * gx1 # gx1 * gxd^3\n tv2 = Fp.mul(tv2, tv3); // 15. tv2 = tv2 * tv3 # gx1 * gxd^7\n let y11 = Fp.pow(tv2, ELL2_C4); // 16. y11 = tv2^c4 # (gx1 * gxd^7)^((p - 5) / 8)\n y11 = Fp.mul(y11, tv3); // 17. y11 = y11 * tv3 # gx1*gxd^3*(gx1*gxd^7)^((p-5)/8)\n let y12 = Fp.mul(y11, ELL2_C3); // 18. y12 = y11 * c3\n tv2 = Fp.sqr(y11); // 19. tv2 = y11^2\n tv2 = Fp.mul(tv2, gxd); // 20. tv2 = tv2 * gxd\n let e1 = Fp.eql(tv2, gx1); // 21. e1 = tv2 == gx1\n let y1 = Fp.cmov(y12, y11, e1); // 22. y1 = CMOV(y12, y11, e1) # If g(x1) is square, this is its sqrt\n let x2n = Fp.mul(x1n, tv1); // 23. x2n = x1n * tv1 # x2 = x2n / xd = 2 * u^2 * x1n / xd\n let y21 = Fp.mul(y11, u); // 24. y21 = y11 * u\n y21 = Fp.mul(y21, ELL2_C2); // 25. y21 = y21 * c2\n let y22 = Fp.mul(y21, ELL2_C3); // 26. y22 = y21 * c3\n let gx2 = Fp.mul(gx1, tv1); // 27. gx2 = gx1 * tv1 # g(x2) = gx2 / gxd = 2 * u^2 * g(x1)\n tv2 = Fp.sqr(y21); // 28. tv2 = y21^2\n tv2 = Fp.mul(tv2, gxd); // 29. tv2 = tv2 * gxd\n let e2 = Fp.eql(tv2, gx2); // 30. e2 = tv2 == gx2\n let y2 = Fp.cmov(y22, y21, e2); // 31. y2 = CMOV(y22, y21, e2) # If g(x2) is square, this is its sqrt\n tv2 = Fp.sqr(y1); // 32. tv2 = y1^2\n tv2 = Fp.mul(tv2, gxd); // 33. tv2 = tv2 * gxd\n let e3 = Fp.eql(tv2, gx1); // 34. e3 = tv2 == gx1\n let xn = Fp.cmov(x2n, x1n, e3); // 35. xn = CMOV(x2n, x1n, e3) # If e3, x = x1, else x = x2\n let y = Fp.cmov(y2, y1, e3); // 36. y = CMOV(y2, y1, e3) # If e3, y = y1, else y = y2\n let e4 = Fp.isOdd(y); // 37. e4 = sgn0(y) == 1 # Fix sign of y\n y = Fp.cmov(y, Fp.neg(y), e3 !== e4); // 38. y = CMOV(y, -y, e3 XOR e4)\n return { xMn: xn, xMd: xd, yMn: y, yMd: _1n }; // 39. return (xn, xd, y, 1)\n}\nconst ELL2_C1_EDWARDS = /* @__PURE__ */ (() => FpSqrtEven(Fp, Fp.neg(BigInt(486664))))(); // sgn0(c1) MUST equal 0\nfunction map_to_curve_elligator2_edwards25519(u) {\n const { xMn, xMd, yMn, yMd } = map_to_curve_elligator2_curve25519(u); // 1. (xMn, xMd, yMn, yMd) =\n // map_to_curve_elligator2_curve25519(u)\n let xn = Fp.mul(xMn, yMd); // 2. xn = xMn * yMd\n xn = Fp.mul(xn, ELL2_C1_EDWARDS); // 3. xn = xn * c1\n let xd = Fp.mul(xMd, yMn); // 4. xd = xMd * yMn # xn / xd = c1 * xM / yM\n let yn = Fp.sub(xMn, xMd); // 5. yn = xMn - xMd\n let yd = Fp.add(xMn, xMd); // 6. yd = xMn + xMd # (n / d - 1) / (n / d + 1) = (n - d) / (n + d)\n let tv1 = Fp.mul(xd, yd); // 7. tv1 = xd * yd\n let e = Fp.eql(tv1, Fp.ZERO); // 8. e = tv1 == 0\n xn = Fp.cmov(xn, Fp.ZERO, e); // 9. xn = CMOV(xn, 0, e)\n xd = Fp.cmov(xd, Fp.ONE, e); // 10. xd = CMOV(xd, 1, e)\n yn = Fp.cmov(yn, Fp.ONE, e); // 11. yn = CMOV(yn, 1, e)\n yd = Fp.cmov(yd, Fp.ONE, e); // 12. yd = CMOV(yd, 1, e)\n const [xd_inv, yd_inv] = FpInvertBatch(Fp, [xd, yd], true); // batch division\n return { x: Fp.mul(xn, xd_inv), y: Fp.mul(yn, yd_inv) }; // 13. return (xn, xd, yn, yd)\n}\n/** Hashing to ed25519 points / field. RFC 9380 methods. */\nexport const ed25519_hasher = /* @__PURE__ */ (() => createHasher(ed25519.Point, (scalars) => map_to_curve_elligator2_edwards25519(scalars[0]), {\n DST: 'edwards25519_XMD:SHA-512_ELL2_RO_',\n encodeDST: 'edwards25519_XMD:SHA-512_ELL2_NU_',\n p: ed25519_CURVE_p,\n m: 1,\n k: 128,\n expand: 'xmd',\n hash: sha512,\n}))();\n// √(-1) aka √(a) aka 2^((p-1)/4)\nconst SQRT_M1 = ED25519_SQRT_M1;\n// √(ad - 1)\nconst SQRT_AD_MINUS_ONE = /* @__PURE__ */ BigInt('25063068953384623474111414158702152701244531502492656460079210482610430750235');\n// 1 / √(a-d)\nconst INVSQRT_A_MINUS_D = /* @__PURE__ */ BigInt('54469307008909316920995813868745141605393597292927456921205312896311721017578');\n// 1-d²\nconst ONE_MINUS_D_SQ = /* @__PURE__ */ BigInt('1159843021668779879193775521855586647937357759715417654439879720876111806838');\n// (d-1)²\nconst D_MINUS_ONE_SQ = /* @__PURE__ */ BigInt('40440834346308536858101042469323190826248399146238708352240133220865137265952');\n// Calculates 1/√(number)\nconst invertSqrt = (number) => uvRatio(_1n, number);\nconst MAX_255B = /* @__PURE__ */ BigInt('0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff');\nconst bytes255ToNumberLE = (bytes) => ed25519.Point.Fp.create(bytesToNumberLE(bytes) & MAX_255B);\n/**\n * Computes Elligator map for Ristretto255.\n * Described in [RFC9380](https://www.rfc-editor.org/rfc/rfc9380#appendix-B) and on\n * the [website](https://ristretto.group/formulas/elligator.html).\n */\nfunction calcElligatorRistrettoMap(r0) {\n const { d } = ed25519_CURVE;\n const P = ed25519_CURVE_p;\n const mod = (n) => Fp.create(n);\n const r = mod(SQRT_M1 * r0 * r0); // 1\n const Ns = mod((r + _1n) * ONE_MINUS_D_SQ); // 2\n let c = BigInt(-1); // 3\n const D = mod((c - d * r) * mod(r + d)); // 4\n let { isValid: Ns_D_is_sq, value: s } = uvRatio(Ns, D); // 5\n let s_ = mod(s * r0); // 6\n if (!isNegativeLE(s_, P))\n s_ = mod(-s_);\n if (!Ns_D_is_sq)\n s = s_; // 7\n if (!Ns_D_is_sq)\n c = r; // 8\n const Nt = mod(c * (r - _1n) * D_MINUS_ONE_SQ - D); // 9\n const s2 = s * s;\n const W0 = mod((s + s) * D); // 10\n const W1 = mod(Nt * SQRT_AD_MINUS_ONE); // 11\n const W2 = mod(_1n - s2); // 12\n const W3 = mod(_1n + s2); // 13\n return new ed25519.Point(mod(W0 * W3), mod(W2 * W1), mod(W1 * W3), mod(W0 * W2));\n}\nfunction ristretto255_map(bytes) {\n abytes(bytes, 64);\n const r1 = bytes255ToNumberLE(bytes.subarray(0, 32));\n const R1 = calcElligatorRistrettoMap(r1);\n const r2 = bytes255ToNumberLE(bytes.subarray(32, 64));\n const R2 = calcElligatorRistrettoMap(r2);\n return new _RistrettoPoint(R1.add(R2));\n}\n/**\n * Wrapper over Edwards Point for ristretto255.\n *\n * Each ed25519/ExtendedPoint has 8 different equivalent points. This can be\n * a source of bugs for protocols like ring signatures. Ristretto was created to solve this.\n * Ristretto point operates in X:Y:Z:T extended coordinates like ExtendedPoint,\n * but it should work in its own namespace: do not combine those two.\n * See [RFC9496](https://www.rfc-editor.org/rfc/rfc9496).\n */\nclass _RistrettoPoint extends PrimeEdwardsPoint {\n constructor(ep) {\n super(ep);\n }\n static fromAffine(ap) {\n return new _RistrettoPoint(ed25519.Point.fromAffine(ap));\n }\n assertSame(other) {\n if (!(other instanceof _RistrettoPoint))\n throw new Error('RistrettoPoint expected');\n }\n init(ep) {\n return new _RistrettoPoint(ep);\n }\n /** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */\n static hashToCurve(hex) {\n return ristretto255_map(ensureBytes('ristrettoHash', hex, 64));\n }\n static fromBytes(bytes) {\n abytes(bytes, 32);\n const { a, d } = ed25519_CURVE;\n const P = ed25519_CURVE_p;\n const mod = (n) => Fp.create(n);\n const s = bytes255ToNumberLE(bytes);\n // 1. Check that s_bytes is the canonical encoding of a field element, or else abort.\n // 3. Check that s is non-negative, or else abort\n if (!equalBytes(Fp.toBytes(s), bytes) || isNegativeLE(s, P))\n throw new Error('invalid ristretto255 encoding 1');\n const s2 = mod(s * s);\n const u1 = mod(_1n + a * s2); // 4 (a is -1)\n const u2 = mod(_1n - a * s2); // 5\n const u1_2 = mod(u1 * u1);\n const u2_2 = mod(u2 * u2);\n const v = mod(a * d * u1_2 - u2_2); // 6\n const { isValid, value: I } = invertSqrt(mod(v * u2_2)); // 7\n const Dx = mod(I * u2); // 8\n const Dy = mod(I * Dx * v); // 9\n let x = mod((s + s) * Dx); // 10\n if (isNegativeLE(x, P))\n x = mod(-x); // 10\n const y = mod(u1 * Dy); // 11\n const t = mod(x * y); // 12\n if (!isValid || isNegativeLE(t, P) || y === _0n)\n throw new Error('invalid ristretto255 encoding 2');\n return new _RistrettoPoint(new ed25519.Point(x, y, _1n, t));\n }\n /**\n * Converts ristretto-encoded string to ristretto point.\n * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).\n * @param hex Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding\n */\n static fromHex(hex) {\n return _RistrettoPoint.fromBytes(ensureBytes('ristrettoHex', hex, 32));\n }\n static msm(points, scalars) {\n return pippenger(_RistrettoPoint, ed25519.Point.Fn, points, scalars);\n }\n /**\n * Encodes ristretto point to Uint8Array.\n * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).\n */\n toBytes() {\n let { X, Y, Z, T } = this.ep;\n const P = ed25519_CURVE_p;\n const mod = (n) => Fp.create(n);\n const u1 = mod(mod(Z + Y) * mod(Z - Y)); // 1\n const u2 = mod(X * Y); // 2\n // Square root always exists\n const u2sq = mod(u2 * u2);\n const { value: invsqrt } = invertSqrt(mod(u1 * u2sq)); // 3\n const D1 = mod(invsqrt * u1); // 4\n const D2 = mod(invsqrt * u2); // 5\n const zInv = mod(D1 * D2 * T); // 6\n let D; // 7\n if (isNegativeLE(T * zInv, P)) {\n let _x = mod(Y * SQRT_M1);\n let _y = mod(X * SQRT_M1);\n X = _x;\n Y = _y;\n D = mod(D1 * INVSQRT_A_MINUS_D);\n }\n else {\n D = D2; // 8\n }\n if (isNegativeLE(X * zInv, P))\n Y = mod(-Y); // 9\n let s = mod((Z - Y) * D); // 10 (check footer's note, no sqrt(-a))\n if (isNegativeLE(s, P))\n s = mod(-s);\n return Fp.toBytes(s); // 11\n }\n /**\n * Compares two Ristretto points.\n * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).\n */\n equals(other) {\n this.assertSame(other);\n const { X: X1, Y: Y1 } = this.ep;\n const { X: X2, Y: Y2 } = other.ep;\n const mod = (n) => Fp.create(n);\n // (x1 * y2 == y1 * x2) | (y1 * y2 == x1 * x2)\n const one = mod(X1 * Y2) === mod(Y1 * X2);\n const two = mod(Y1 * Y2) === mod(X1 * X2);\n return one || two;\n }\n is0() {\n return this.equals(_RistrettoPoint.ZERO);\n }\n}\n// Do NOT change syntax: the following gymnastics is done,\n// because typescript strips comments, which makes bundlers disable tree-shaking.\n// prettier-ignore\n_RistrettoPoint.BASE = \n/* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.BASE))();\n// prettier-ignore\n_RistrettoPoint.ZERO = \n/* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.ZERO))();\n// prettier-ignore\n_RistrettoPoint.Fp = \n/* @__PURE__ */ (() => Fp)();\n// prettier-ignore\n_RistrettoPoint.Fn = \n/* @__PURE__ */ (() => Fn)();\nexport const ristretto255 = { Point: _RistrettoPoint };\n/** Hashing to ristretto255 points / field. RFC 9380 methods. */\nexport const ristretto255_hasher = {\n hashToCurve(msg, options) {\n const DST = options?.DST || 'ristretto255_XMD:SHA-512_R255MAP_RO_';\n const xmd = expand_message_xmd(msg, DST, 64, sha512);\n return ristretto255_map(xmd);\n },\n hashToScalar(msg, options = { DST: _DST_scalar }) {\n const xmd = expand_message_xmd(msg, options.DST, 64, sha512);\n return Fn.create(bytesToNumberLE(xmd));\n },\n};\n// export const ristretto255_oprf: OPRF = createORPF({\n// name: 'ristretto255-SHA512',\n// Point: RistrettoPoint,\n// hash: sha512,\n// hashToGroup: ristretto255_hasher.hashToCurve,\n// hashToScalar: ristretto255_hasher.hashToScalar,\n// });\n/**\n * Weird / bogus points, useful for debugging.\n * All 8 ed25519 points of 8-torsion subgroup can be generated from the point\n * T = `26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05`.\n * ⟨T⟩ = { O, T, 2T, 3T, 4T, 5T, 6T, 7T }\n */\nexport const ED25519_TORSION_SUBGROUP = [\n '0100000000000000000000000000000000000000000000000000000000000000',\n 'c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a',\n '0000000000000000000000000000000000000000000000000000000000000080',\n '26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05',\n 'ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f',\n '26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc85',\n '0000000000000000000000000000000000000000000000000000000000000000',\n 'c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa',\n];\n/** @deprecated use `ed25519.utils.toMontgomery` */\nexport function edwardsToMontgomeryPub(edwardsPub) {\n return ed25519.utils.toMontgomery(ensureBytes('pub', edwardsPub));\n}\n/** @deprecated use `ed25519.utils.toMontgomery` */\nexport const edwardsToMontgomery = edwardsToMontgomeryPub;\n/** @deprecated use `ed25519.utils.toMontgomerySecret` */\nexport function edwardsToMontgomeryPriv(edwardsPriv) {\n return ed25519.utils.toMontgomerySecret(ensureBytes('pub', edwardsPriv));\n}\n/** @deprecated use `ristretto255.Point` */\nexport const RistrettoPoint = _RistrettoPoint;\n/** @deprecated use `import { ed25519_hasher } from '@noble/curves/ed25519.js';` */\nexport const hashToCurve = /* @__PURE__ */ (() => ed25519_hasher.hashToCurve)();\n/** @deprecated use `import { ed25519_hasher } from '@noble/curves/ed25519.js';` */\nexport const encodeToCurve = /* @__PURE__ */ (() => ed25519_hasher.encodeToCurve)();\n/** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */\nexport const hashToRistretto255 = /* @__PURE__ */ (() => ristretto255_hasher.hashToCurve)();\n/** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */\nexport const hash_to_ristretto255 = /* @__PURE__ */ (() => ristretto255_hasher.hashToCurve)();\n//# sourceMappingURL=ed25519.js.map","/**\n * x25519 key exchange for Meshtastic direct messages\n */\n\nimport { x25519 } from '@noble/curves/ed25519';\nimport { InvalidKeyError } from '../utils/error-types';\n\n/**\n * Key pair for x25519\n */\nexport interface KeyPair {\n publicKey: Buffer;\n privateKey: Buffer;\n}\n\n/**\n * Generate a new x25519 key pair\n * @returns Key pair with public and private keys\n */\nexport function generateKeyPair(): KeyPair {\n const privateKey = x25519.utils.randomPrivateKey();\n const publicKey = x25519.getPublicKey(privateKey);\n\n return {\n publicKey: Buffer.from(publicKey),\n privateKey: Buffer.from(privateKey),\n };\n}\n\n/**\n * Derive a shared secret using x25519\n *\n * @param myPrivateKey - My private key (32 bytes)\n * @param theirPublicKey - Their public key (32 bytes)\n * @returns Shared secret (32 bytes)\n * @throws {InvalidKeyError} If keys are invalid\n */\nexport function deriveSharedSecret(\n myPrivateKey: Buffer | Uint8Array,\n theirPublicKey: Buffer | Uint8Array\n): Buffer {\n try {\n // Validate key lengths\n if (myPrivateKey.length !== 32) {\n throw new InvalidKeyError(`Private key must be 32 bytes, got ${myPrivateKey.length}`);\n }\n\n if (theirPublicKey.length !== 32) {\n throw new InvalidKeyError(`Public key must be 32 bytes, got ${theirPublicKey.length}`);\n }\n\n // Derive shared secret\n const sharedSecret = x25519.getSharedSecret(\n myPrivateKey instanceof Buffer ? new Uint8Array(myPrivateKey) : myPrivateKey,\n theirPublicKey instanceof Buffer ? new Uint8Array(theirPublicKey) : theirPublicKey\n );\n\n return Buffer.from(sharedSecret);\n } catch (error) {\n throw new InvalidKeyError(\n `Failed to derive shared secret: ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n}\n\n/**\n * Verify that a public key is valid\n * @param publicKey - Public key to verify\n * @returns true if valid\n * @throws {InvalidKeyError} If key is invalid\n */\nexport function verifyPublicKey(publicKey: Buffer | Uint8Array): boolean {\n if (publicKey.length !== 32) {\n throw new InvalidKeyError(`Public key must be 32 bytes, got ${publicKey.length}`);\n }\n\n // All 32-byte values are technically valid x25519 public keys\n // Additional validation could check for low-order points, but\n // the library handles this internally\n return true;\n}\n\n/**\n * Convert a public key to base64 string\n * @param publicKey - Public key buffer\n * @returns Base64-encoded public key\n */\nexport function publicKeyToBase64(publicKey: Buffer): string {\n return publicKey.toString('base64');\n}\n\n/**\n * Convert a private key to base64 string\n * @param privateKey - Private key buffer\n * @returns Base64-encoded private key\n */\nexport function privateKeyToBase64(privateKey: Buffer): string {\n return privateKey.toString('base64');\n}\n\n/**\n * Convert a public key to hex string\n * @param publicKey - Public key buffer\n * @returns Hex-encoded public key\n */\nexport function publicKeyToHex(publicKey: Buffer): string {\n return publicKey.toString('hex');\n}\n\n/**\n * Convert a private key to hex string\n * @param privateKey - Private key buffer\n * @returns Hex-encoded private key\n */\nexport function privateKeyToHex(privateKey: Buffer): string {\n return privateKey.toString('hex');\n}\n"],"names":["InvalidNonceError","InvalidKeyError","createCipheriv","MeshtasticEncryptError","createDecipheriv","MeshtasticDecryptError","_0n","_1n","bytesToHex_","abytes_","hexToBytes_","isBytes_","_2n","_3n","_5n","_8n","Fp","adjustScalarBytes"],"mappings":";;;AAoBO,SAAS,sBAAsB,SAAiB,UAA0B;AAC/E,MAAI,CAAC,OAAO,UAAU,OAAO,KAAK,UAAU,KAAK,UAAU,YAAY;AACrE,UAAM,IAAIA,QAAAA,kBAAkB,wBAAwB,OAAO,mBAAmB;AAAA,EAChF;AAEA,MAAI,CAAC,OAAO,UAAU,QAAQ,KAAK,WAAW,KA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