getPrivateKeyJwk method

Future<Map<String, dynamic>> getPrivateKeyJwk(
  1. String hdPath, [
  2. KeyType keyType = KeyType.secp256k1
])

Implementation

Future<Map<String, dynamic>> getPrivateKeyJwk(String hdPath,
    [KeyType keyType = KeyType.secp256k1]) async {
  var privateKeyHex = await getPrivateKey(hdPath, keyType);
  var did = await getDid(hdPath, keyType);
  Map<String, dynamic> key = {};
  key['kid'] = '$did#${did.split(':').last}';
  if (keyType == KeyType.secp256k1) {
    key['kty'] = 'EC';
    key['crv'] = 'secp256k1';
    key['d'] =
        removePaddingFromBase64(base64UrlEncode(hexToBytes(privateKeyHex)));
  } else if (keyType == KeyType.x25519) {
    key['kty'] = 'OKP';
    key['crv'] = 'X25519';
    key['d'] = removePaddingFromBase64(
        base64UrlEncode(hexToBytes(privateKeyHex).sublist(0, 32)));
  } else if (keyType == KeyType.ed25519) {
    key['kty'] = 'OKP';
    key['crv'] = 'Ed25519';
    key['d'] = removePaddingFromBase64(
        base64UrlEncode(hexToBytes(privateKeyHex).sublist(0, 32)));
  } else if (keyType == KeyType.p384 ||
      keyType == KeyType.p256 ||
      keyType == KeyType.p521) {
    String crv;
    if (keyType == KeyType.p521) {
      crv = 'P-521';
    } else if (keyType == KeyType.p384) {
      crv = 'P-384';
    } else {
      crv = 'P-256';
    }
    key['kty'] = 'EC';
    key['crv'] = crv;
    key['d'] =
        removePaddingFromBase64(base64UrlEncode(hexToBytes(privateKeyHex)));
  } else {
    throw Exception('Unknown keyType');
  }

  return key;
}