fromRawBytes static method

Future<PrivateKey> fromRawBytes(
  1. Uint8List bytes
)

Creates an EcdsaPrivateKey from raw bytes (DER encoded)

Implementation

static Future<p2pkeys.PrivateKey> fromRawBytes(Uint8List bytes) async {
  try {
    final parser = pc.ASN1Parser(bytes);
    final asn1Sequence = parser.nextObject() as pc.ASN1Sequence;

    // Extract the private value (d)
    final d = (asn1Sequence.elements![0] as pc.ASN1Integer).integer!;

    // Extract the public key coordinates (if present)
    final x = (asn1Sequence.elements![1] as pc.ASN1Integer).integer!;
    final y = (asn1Sequence.elements![2] 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');
    }

    final privateKey = ECPrivateKey(d, ECDSACurve);
    final publicKey = ECPublicKey(point, ECDSACurve);

    return EcdsaPrivateKey(privateKey, EcdsaPublicKey(publicKey));
  } catch (e) {
    throw ECDSAKeyException('Failed to parse ECDSA private key: ${e.toString()}');
  }
}