fromPublicKey static method

Jwk fromPublicKey(
  1. PublicKey publicKey
)

Converts PublicKey into Jwk.

Implementation

static Jwk fromPublicKey(PublicKey publicKey) {
  if (publicKey is EcPublicKey) {
    final crv = const <KeyPairType, String>{
      KeyPairType.p256: 'P-256',
      KeyPairType.p256k: 'secp256k1',
      KeyPairType.p384: 'P-384',
      KeyPairType.p521: 'P-521',
    }[publicKey.type];
    if (crv != null) {
      return Jwk(
        kty: 'EC',
        crv: crv,
        x: publicKey.x,
        y: publicKey.y,
      );
    }
  } else if (publicKey is SimplePublicKey) {
    final crv = const <KeyPairType, String>{
      KeyPairType.ed25519: 'Ed25519',
      KeyPairType.x25519: 'X25519',
    }[publicKey.type];
    if (crv != null) {
      return Jwk(
        kty: 'OKP',
        crv: crv,
        x: publicKey.bytes,
      );
    }
  } else if (publicKey is RsaPublicKey) {
    return Jwk(
      kty: 'RSA',
      e: publicKey.e,
      n: publicKey.n,
    );
  }
  throw ArgumentError.value(publicKey);
}