fromKeyPair static method

Future<Jwk> fromKeyPair(
  1. KeyPair keyPair
)

Converts KeyPair to Jwk.

Implementation

static Future<Jwk> fromKeyPair(KeyPair keyPair) async {
  keyPair = await keyPair.extract();
  if (keyPair is EcKeyPairData) {
    final crv = const <KeyPairType, String>{
      KeyPairType.p256: 'P-256',
      KeyPairType.p256k: 'secp256k1',
      KeyPairType.p384: 'P-384',
      KeyPairType.p521: 'P-521',
    }[keyPair.type];
    if (crv != null) {
      return Jwk(
        kty: 'EC',
        crv: crv,
        d: keyPair.d,
        x: keyPair.x,
        y: keyPair.y,
      );
    }
  } else if (keyPair is SimpleKeyPairData) {
    final crv = const <KeyPairType, String>{
      KeyPairType.ed25519: 'Ed25519',
      KeyPairType.x25519: 'X25519',
    }[keyPair.type];
    if (crv != null) {
      return Jwk(
        kty: 'OKP',
        crv: crv,
        d: keyPair.bytes,
        x: keyPair.publicKey.bytes,
      );
    }
  } else if (keyPair is RsaKeyPairData) {
    return Jwk(
      kty: 'RSA',
      e: keyPair.e,
      d: keyPair.d,
      dp: keyPair.dp,
      dq: keyPair.dq,
      n: keyPair.n,
      p: keyPair.p,
      q: keyPair.q,
      qi: keyPair.qi,
    );
  }
  throw ArgumentError.value(keyPair);
}