#!/bin/bash # Convert MeshCore firmware 64-byte private key to pyMC_Repeater format # # Usage: sudo ./convert_firmware_key.sh <64-byte-hex-key> [--output-format=] [config-path] # Example: sudo ./convert_firmware_key.sh 987BDA619630197351F2B3040FD19B2EE0DEE357DD69BBEEE295786FA78A4D5F298B0BF1B7DE73CBC23257CDB2C562F5033DF58C232916432948B0F6BA4448F2 set -e if [ $# -eq 0 ]; then echo "Error: No key provided" echo "" echo "Usage: sudo $0 <64-byte-hex-key> [--output-format=] [config-path]" echo "" echo "This script imports a 64-byte MeshCore firmware private key into" echo "pyMC_Repeater for full identity compatibility." echo "" echo "The 64-byte key format: [32-byte scalar][32-byte nonce]" echo " - Enables same node address as firmware device" echo " - Supports signing using MeshCore/orlp ed25519 algorithm" echo " - Fully compatible with pyMC_core LocalIdentity" echo "" echo "Arguments:" echo " --output-format: Optional output format (yaml|identity, default: yaml)" echo " yaml - Store in config.yaml (embedded binary)" echo " identity - Save to identity.key file (base64 encoded)" echo " config-path: Optional path to config.yaml (default: /etc/pymc_repeater/config.yaml)" echo "" echo "Examples:" echo " # Save to config.yaml (default)" echo " sudo $0 987BDA619630197351F2B3040FD19B2EE0DEE357DD69BBEEE295786FA78A4D5F298B0BF1B7DE73CBC23257CDB2C562F5033DF58C232916432948B0F6BA4448F2" echo "" echo " # Save to identity.key file" echo " sudo $0 987BDA619630197351F2B3040FD19B2EE0DEE357DD69BBEEE295786FA78A4D5F298B0BF1B7DE73CBC23257CDB2C562F5033DF58C232916432948B0F6BA4448F2 --output-format=identity" exit 1 fi # Check if running with sudo/root if [ "$EUID" -ne 0 ]; then echo "Error: This script must be run with sudo to update config.yaml" echo "Usage: sudo $0 <64-byte-hex-key>" exit 1 fi FULL_KEY="$1" OUTPUT_FORMAT="yaml" # Default format CONFIG_PATH="" # Parse arguments shift # Remove the key argument while [ $# -gt 0 ]; do case "$1" in --output-format=*) OUTPUT_FORMAT="${1#*=}" ;; *) CONFIG_PATH="$1" ;; esac shift done # Validate output format if [ "$OUTPUT_FORMAT" != "yaml" ] && [ "$OUTPUT_FORMAT" != "identity" ]; then echo "Error: Invalid output format '$OUTPUT_FORMAT'. Must be 'yaml' or 'identity'" exit 1 fi # Set default config path if not provided if [ -z "$CONFIG_PATH" ]; then CONFIG_PATH="/etc/pymc_repeater/config.yaml" fi # Validate hex string if ! [[ "$FULL_KEY" =~ ^[0-9a-fA-F]+$ ]]; then echo "Error: Key must be a hexadecimal string" exit 1 fi KEY_LEN=${#FULL_KEY} if [ "$KEY_LEN" -ne 128 ]; then echo "Error: Key must be 64 bytes (128 hex characters), got $KEY_LEN characters" exit 1 fi # Check if config/identity file location exists (only for yaml format or if saving identity.key) if [ "$OUTPUT_FORMAT" = "yaml" ]; then # Check if config exists if [ ! -f "$CONFIG_PATH" ]; then echo "Error: Config file not found: $CONFIG_PATH" exit 1 fi else # For identity format, use system-wide location matching config.yaml IDENTITY_DIR="/etc/pymc_repeater" IDENTITY_PATH="$IDENTITY_DIR/identity.key" fi echo "=== MeshCore Firmware Key Import ===" echo "" echo "Output format: $OUTPUT_FORMAT" if [ "$OUTPUT_FORMAT" = "yaml" ]; then echo "Target file: $CONFIG_PATH" else echo "Target file: $IDENTITY_PATH" fi echo "" echo "Input (64-byte firmware key):" echo " $FULL_KEY" echo "" # Verify public key derivation and import key using Python with safe YAML handling python3 </dev/null; then read -p "Restart pymc-repeater service now? (yes/no): " RESTART if [ "$RESTART" = "yes" ]; then systemctl restart pymc-repeater echo "✓ Service restarted" echo "" echo "Check logs for new identity:" echo " sudo journalctl -u pymc-repeater -f | grep -i 'identity\|hash'" else echo "Remember to restart the service:" echo " sudo systemctl restart pymc-repeater" fi else echo "Note: pymc-repeater service is not running" echo "Start it with: sudo systemctl start pymc-repeater" fi else echo "Identity key saved to file." echo "" if systemctl is-active --quiet pymc-repeater 2>/dev/null; then read -p "Restart pymc-repeater service now? (yes/no): " RESTART if [ "$RESTART" = "yes" ]; then systemctl restart pymc-repeater echo "✓ Service restarted" echo "" echo "Check logs for new identity:" echo " sudo journalctl -u pymc-repeater -f | grep -i 'identity\|hash'" else echo "Remember to restart the service:" echo " sudo systemctl restart pymc-repeater" fi else echo "Note: pymc-repeater service is not running" echo "Start it with: sudo systemctl start pymc-repeater" fi fi