UNPKG

@synet/keys

Version:

Zero-dependency, secure key generation library. Supports ed25519, x25519, secp256k1, RSA, and WireGuard keys.

148 lines (147 loc) 4.45 kB
/** * Signer Unit - Primary cryptographic signing unit * [🔐] Self-contained cryptographic engine that knows how to sign * * Design principles: * - Signer is the primary unit (holds private key securely) * - Full Unit architecture with execute, teach, capabilities * - Self-contained cryptographic operations (no external dependencies) * - Can teach capabilities to Key units * * @author Synet Team */ import { Unit, type TeachingContract, type UnitProps } from '@synet/unit'; import { type KeyType } from './keys'; import { Key } from './key'; /** * ISigner interface for external signing implementations * Simplified to core signing functionality only */ export interface ISigner { sign(data: string): Promise<string>; } /** * Configuration for creating a Signer unit */ export interface SignerConfig { privateKeyPEM: string; publicKeyPEM: string; keyType: KeyType; secure?: boolean; metadata?: Record<string, unknown>; isigner?: ISigner; } /** * Props interface for Signer unit - follows Unit Architecture Doctrine */ export interface SignerProps extends UnitProps { privateKeyPEM: string; publicKeyPEM: string; keyType: KeyType; keyId: string; metadata: Record<string, unknown>; secure: boolean; isigner?: ISigner; } interface SignerGenerateParams { secure?: boolean; metadata?: Record<string, unknown>; } /** * Signer Unit - Primary unit for key generation and signing * The source of truth for cryptographic operations */ export declare class Signer extends Unit<SignerProps> implements ISigner { protected constructor(props: SignerProps); /** * Generate new signer with fresh key pair */ static generate(keyType: KeyType, params?: SignerGenerateParams): Signer; /** * Create signer from existing key pair */ static create(config: SignerConfig): Signer; /** * Create signer from existing key pair (compatibility method) */ static createFromKeyPair(privateKeyPEM: string, publicKeyPEM: string, keyType: KeyType, metadata?: Record<string, unknown>): Signer | null; /** * Create signer with external ISigner (edge case) * For cases where signing logic is handled externally */ static createWithSigner(params: { signer: ISigner; keyType?: KeyType; publicKeyPEM?: string; metadata?: Record<string, unknown>; }): Signer | null; whoami(): string; capabilities(): string[]; help(): void; teach(): TeachingContract; sign(data: string): Promise<string>; getPublicKey(): string; getAlgorithm(): string; /** * Create a Key unit from this Signer's key material * The Key will learn signing capabilities from this Signer */ createKey(): Key | null; verify(data: string, signature: string): Promise<boolean>; /** * Get data needed to create associated Key unit * Returns the data needed for Key.createFromSigner() to avoid circular dependency */ getKey(meta?: Record<string, unknown>): { publicKeyPEM: string; keyType: KeyType; meta: Record<string, unknown>; signer: ISigner; }; /** * Export signer metadata (excludes private key for security) */ toJSON(): Record<string, unknown>; get id(): string; get type(): KeyType; get metadata(): Record<string, unknown>; get privateKeyPEM(): string; get publicKeyPEM(): string; get keyType(): string; /** * Convert key to PEM format for cryptographic operations */ private convertToPEMFormat; /** * Perform cryptographic signing based on key type */ private performSigning; /** * Perform cryptographic verification based on key type * Uses tested verification functions from verify.ts */ private performVerification; /** * Sign data with Ed25519 key */ private signEd25519; /** * Sign data with RSA key */ private signRSA; /** * Sign data with secp256k1 key */ private signSecp256k1; /** * Get public key in hex format for DID generation * This allows DID generation to work immediately with hex format */ getPublicKeyHex(): string | null; /** * Get private key in hex format (respects security flag) * Only returns private key if secure flag is false */ getPrivateKeyHex(): string | null; } export {};