EcdsaPublicKey.fromRawBytes constructor

EcdsaPublicKey.fromRawBytes(
  1. Uint8List bytes
)

Creates an EcdsaPublicKey from raw bytes (DER encoded)

Implementation

factory EcdsaPublicKey.fromRawBytes(Uint8List bytes) {
  try {
    final parser = pc.ASN1Parser(bytes);
    final asn1Sequence = parser.nextObject() as pc.ASN1Sequence;

    // Extract the x and y coordinates
    final x = (asn1Sequence.elements![0] as pc.ASN1Integer).integer!;
    final y = (asn1Sequence.elements![1] as pc.ASN1Integer).integer!;

    // Create the public key
    final curve = ECCurve_secp256r1();
    final point = curve.curve.createPoint(x, y);
    if (point == null) {
      throw ECDSAKeyException('Failed to create EC point from coordinates');
    }

    return EcdsaPublicKey(ECPublicKey(point, ECDSACurve));
  } catch (e) {
    throw ECDSAKeyException('Failed to parse ECDSA public key: ${e.toString()}');
  }
}