getPublicKeyFromBytes function

Future<List<String>?> getPublicKeyFromBytes(
  1. Uint8List publicKeyBytes
)

Encrypts the provided public key bytes publicKeyBytes with EcdsaPublicKey.

  • publicKeyBytes: The bytes of the public key.

Returns a Future that completes with a list of Uint8List representing the JSON Web Key x and y. Throws an exception if the public key is invalid.

Implementation

Future<List<String>?> getPublicKeyFromBytes(Uint8List publicKeyBytes) async {
  final pKey =
      await EcdsaPublicKey.importSpkiKey(publicKeyBytes, EllipticCurve.p256);
  final jwk = await pKey.exportJsonWebKey();
  if (jwk.containsKey('x') && jwk.containsKey('y')) {
    final x = base64Url.normalize(jwk['x']);
    final y = base64Url.normalize(jwk['y']);

    final decodedX = hexlify(base64Url.decode(x));
    final decodedY = hexlify(base64Url.decode(y));

    return [decodedX, decodedY];
  } else {
    throw "Invalid public key";
  }
}