toPublicKey method

PublicKey? toPublicKey()

Constructs a PublicKey from this Jwk.

Implementation

PublicKey? toPublicKey() {
  switch (kty) {
    case 'EC':
      final type = const <String, KeyPairType>{
        'P-256': KeyPairType.p256,
        'secp256k1': KeyPairType.p256k,
        'P-384': KeyPairType.p384,
        'P-521': KeyPairType.p521,
      }[crv];
      if (type == null) {
        throw StateError('Unsupported "crv": "$crv"');
      }
      return EcPublicKey(
        x: Uint8List.fromList(x ?? const <int>[]),
        y: Uint8List.fromList(y ?? const <int>[]),
        type: type,
      );

    case 'OKP':
      final type = const <String, KeyPairType>{
        'Ed25519': KeyPairType.ed25519,
        'X25519': KeyPairType.x25519,
      }[crv];
      if (type == null) {
        throw StateError('Unsupported "crv": "$crv"');
      }
      return SimplePublicKey(
        Uint8List.fromList(x ?? const <int>[]),
        type: type,
      );

    case 'RSA':
      return RsaPublicKey(
        e: Uint8List.fromList(e ?? const <int>[]),
        n: Uint8List.fromList(n ?? const <int>[]),
      );
  }
  return null;
}