raw property

  1. @override
Uint8List get raw
override

The key as DER SEC 1 ECPrivateKey with the curve and public key, as go-libp2p marshals it (x509.MarshalECPrivateKey).

Implementation

@override
Uint8List get raw {
  try {
    final params = _key.parameters!;
    final curve = pc.ASN1Object(tag: 0xa0)
      ..valueBytes = pc.ASN1ObjectIdentifier.fromIdentifierString(_curveOid(params)).encode();
    final publicKey = pc.ASN1Object(tag: 0xa1)
      ..valueBytes = pc.ASN1BitString(stringValues: _encodePoint(params, _publicKey._key.Q!)).encode();
    final sec1 = pc.ASN1Sequence()
      ..add(pc.ASN1Integer(BigInt.one))
      ..add(pc.ASN1OctetString(octets: _unsignedBytes(_key.d!, (params.n.bitLength + 7) ~/ 8)))
      ..add(curve)
      ..add(publicKey);
    return Uint8List.fromList(sec1.encode());
  } catch (e) {
    throw ECDSAKeyException('Failed to encode ECDSA private key: ${e.toString()}');
  }
}