publicKey property
Get the public key of this node (base64 encoded)
Implementation
Future<String> get publicKey async {
try {
if (!_container.isRegistered(SecurityManager)) return '';
final key = await _container.get<SecurityManager>().getPrivateKey('self');
if (key != null) {
final keyBytes = key.publicKeyBytes;
if (keyBytes.isEmpty) return '';
// Manually construct Protobuf: PublicKey { required KeyType Type = 1; required bytes Data = 2; }
// Type 2 = Secp256k1
// Tag 1 (Type) = (1 << 3) | 0 = 8. Value = 2. -> [0x08, 0x02]
// Tag 2 (Data) = (2 << 3) | 2 = 18 (0x12). Value = length + bytes.
// Compressed Secp256k1 is 33 bytes, fits in 1 byte varint.
final protoBytes = <int>[
0x08, 0x02, // Type: Secp256k1
0x12, keyBytes.length, // Data tag + length
...keyBytes,
];
return base64.encode(protoBytes);
}
return '';
} catch (e) {
_logger.warning('Failed to get public key: $e');
return '';
}
}