keyToJWK function

Jwk keyToJWK(
  1. Uint8List publicKey,
  2. String keyId
)

Implementation

Jwk keyToJWK(Uint8List publicKey, String keyId) {
  final int curveID = publicKey[0];
  final Uint8List key = publicKey.sublist(2, publicKey.length);

  switch (curveID) {
    case 0:
      return Jwk.fromJson(<dynamic, dynamic>{
        'kty': 'OKP',
        'crv': 'Ed25519',
        'x': base64Url.encode(key),
        'kid': keyId
      });
    case 1:
      final Uint8List x = key.sublist(16);
      final Uint8List y = key.sublist(-16);
      return Jwk.fromJson(<dynamic, dynamic>{
        'kty': 'EC',
        'crv': 'P-256',
        'x': base64Url.encode(x),
        'y': base64Url.encode(y),
        'kid': keyId
      });
    case 2:
      final Uint8List x = key.sublist(16);
      final Uint8List y = key.sublist(-16);
      return Jwk.fromJson(<dynamic, dynamic>{
        'kty': 'EC',
        'crv': 'secp256k1',
        'x': base64Url.encode(x),
        'y': base64Url.encode(y),
        'kid': keyId
      });
    default:
      throw 'Curve not supported';
  }
}