getPrivateKeyForConnectionDidAsJwk method

Future<Map<String, dynamic>?> getPrivateKeyForConnectionDidAsJwk(
  1. String did
)

Implementation

Future<Map<String, dynamic>?> getPrivateKeyForConnectionDidAsJwk(
    String did) async {
  var private = await getPrivateKeyForConnectionDid(did);
  if (private == null) return null;
  Map<String, dynamic> key = {};
  key['kid'] = '$did#${did.split(':').last}';
  if (did.startsWith('did:key:z6Mk')) {
    key['kid'] = '$did#${did.split(':').last}';
    key['kty'] = 'OKP';
    key['crv'] = 'Ed25519';
    key['d'] = removePaddingFromBase64(
        base64UrlEncode(hexToBytes(private).sublist(0, 32)));
    return key;
  } else if (did.startsWith('did:key:z6LS')) {
    key['kty'] = 'OKP';
    key['crv'] = 'X25519';
    key['d'] = removePaddingFromBase64(
        base64UrlEncode(hexToBytes(private).sublist(0, 32)));
    return key;
  } else {
    key['kty'] = 'EC';
    if (did.startsWith('did:ethr') || did.startsWith('did:key:zQ3s')) {
      key['crv'] = 'secp256k1';
    } else if (did.startsWith('did:key:zDn')) {
      key['crv'] = 'P-256';
    } else if (did.startsWith('did:key:z82')) {
      key['crv'] = 'P-384';
    } else if (did.startsWith('did:key:z2J9')) {
      key['crv'] = 'P-521';
    } else {
      throw Exception('Unknown keytyp');
    }
    key['d'] = removePaddingFromBase64(base64UrlEncode(hexToBytes(private)));
    return key;
  }
}