getEncryptionPublicKey method

String getEncryptionPublicKey(
  1. String privateKeyHex
)

Lấy encryption public key từ private key sử dụng web3dart

Implementation

String getEncryptionPublicKey(String privateKeyHex) {
  try {
    // 1. Parse private key
    final privateKey = EthPrivateKey.fromHex(privateKeyHex.startsWith('0x')
        ? privateKeyHex.substring(2)
        : privateKeyHex);

    // 2. Lấy public key dạng compressed
    final publicKeyPoints = privateKey.encodedPublicKey;

    // 3. Convert sang compressed format (33 bytes)
    // Prefix (0x02 nếu y chẵn, 0x03 nếu y lẻ) + x coordinates
    final compressedPubKey = Uint8List(33);
    compressedPubKey[0] = publicKeyPoints[64] & 1 == 0 ? 0x02 : 0x03;
    compressedPubKey.setRange(1, 33, publicKeyPoints.sublist(1, 33));

    // 4. Convert sang hex và thêm prefix 0x
    return '0x${HEX.encode(compressedPubKey)}';
  } catch (e) {
    throw Exception('Failed to get encryption public key: $e');
  }
}