UNPKG

besu-network-manager

Version:

TypeScript library for managing Hyperledger Besu networks with Clique consensus using Docker

38 lines 1.4 kB
import elliptic from 'elliptic'; import keccak from 'keccak'; export class KeyGenerator { ec; constructor() { this.ec = new elliptic.ec('secp256k1'); } generateKeyPair(ip, port) { const keyPair = this.ec.genKeyPair(); const privateKey = keyPair.getPrivate('hex'); const publicKey = keyPair.getPublic(false, 'hex').slice(2); const address = this.publicKeyToAddress(publicKey); const enode = `enode://${publicKey}@${ip}:${port}`; return { privateKey: this.addHexPrefix(privateKey), publicKey: this.addHexPrefix(publicKey), address: this.addHexPrefix(address), enode: enode }; } publicKeyToAddress(publicKey) { const cleanPublicKey = publicKey.startsWith('04') ? publicKey.slice(2) : publicKey; const publicKeyBytes = Buffer.from(cleanPublicKey, 'hex'); const hash = keccak('keccak256').update(publicKeyBytes).digest(); return hash.slice(-20).toString('hex'); } addHexPrefix(hex) { return hex.startsWith('0x') ? hex : `0x${hex}`; } removeHexPrefix(hex) { return hex.startsWith('0x') ? hex.slice(2) : hex; } isValidAddress(address) { const cleanAddress = this.removeHexPrefix(address); return /^[0-9a-fA-F]{40}$/.test(cleanAddress); } } //# sourceMappingURL=KeyGenerator.js.map