# SPDX-License-Identifier: LSL-1.0 # Copyright (c) 2026 Markus Maiwald # Stewardship: Self Sovereign Society Foundation # # This file is part of the Nexus Sovereign Core. # See legal/LICENSE_SOVEREIGN.md for license terms. ## Signature Management for Nexus Formats ## ## This module implements Ed25519 signing and verification for NPK, NIP, and NEXTER formats. ## It handles key generation, storage, and cryptographic operations. ## ## Key Storage Structure: ## ~/.local/share/nexus/keys/ ## ├── private/ # Private keys (0600 permissions) ## │ └── .key ## ├── public/ # Public keys (0644 permissions) ## │ └── .pub ## └── trusted/ # Trusted public keys for verification ## └── .pub ## import std/[os, strutils, json, base64, tables, times, sets] import ed25519 import ./types type SignatureManager* = object keysPath*: string privateKeysPath*: string publicKeysPath*: string trustedKeysPath*: string trustedKeys*: Table[string, PublicKey] KeyId* = string SignatureError* = object of NimPakError KeyPairInfo* = object id*: KeyId publicKey*: string # Base64 encoded privateKey*: string # Base64 encoded (only when generating/exporting private) created*: DateTime const KeyExtension = ".key" PubExtension = ".pub" # Helper functions for conversion proc toArray32(data: seq[byte]): array[32, byte] = if data.len != 32: raise newException(ValueError, "Invalid length for 32-byte array: " & $data.len) for i in 0..<32: result[i] = data[i] proc toArray64(data: seq[byte]): array[64, byte] = if data.len != 64: raise newException(ValueError, "Invalid length for 64-byte array: " & $data.len) for i in 0..<64: result[i] = data[i] proc toSeq(arr: array[32, byte]): seq[byte] = result = newSeq[byte](32) for i in 0..<32: result[i] = arr[i] proc toSeq(arr: array[64, byte]): seq[byte] = result = newSeq[byte](64) for i in 0..<64: result[i] = arr[i] proc encodeKey(key: array[32, byte]): string = base64.encode(key.toSeq) proc encodeSig(sig: array[64, byte]): string = base64.encode(sig.toSeq) proc decodeKey(data: string): array[32, byte] = let decodedStr = base64.decode(data) var bytes = newSeq[byte](decodedStr.len) for i in 0..