presentCredential method

Future<List<VaultCredential>> presentCredential(
  1. String url,
  2. List<Wallet> wallets,
  3. String cookie
)

Implementation

Future<List<VaultCredential>> presentCredential(
    String url, List<Wallet> wallets, String cookie) async {
  try {
    String id = Uri.parse(url).queryParameters['state'] ?? "";

    var header = {
      "Cookie": cookie,
      "Content-Type": "application/json",
    };

    var presentationDefinition = await http
        .get(Uri.parse("${ApiConfig.baseUrl}verifier/openid4vc/pd/$id"));
    log("URL::${Uri.parse("${ApiConfig.baseUrl}verifier/openid4vc/pd/$id")} :: RESPONSE :: ${presentationDefinition.body}");

    header["Content-Type"] = "application/json";
    header["Accept"] = "application/json";

    List<VaultCredential> credentials = [];

    for (var wallet in wallets) {
      var response = await http.post(
        Uri.parse(
            "${ApiConfig.baseUrl}wallet/${wallet.id}/exchange/resolvePresentationRequest"),
        headers: header,
        body: jsonEncode({"offer": url}),
      );

      log("URL::${Uri.parse(
            "${ApiConfig.baseUrl}wallet/${wallet.id}/exchange/resolvePresentationRequest")} :: RESPONSE :: ${response.body}");

      var presentationResponse = await http.post(
        Uri.parse(
            "${ApiConfig.baseUrl}wallet/${wallet.id}/exchange/matchCredentialsForPresentationDefinition"),
        headers: header,
        body: jsonEncode(jsonDecode(presentationDefinition.body)["data"]),
      );

      final parsed = jsonDecode(presentationResponse.body)["data"];
      for (var i = 0; i < parsed.length; i++) {
        String doc = parsed[i]['document'].split(".")[1];
        //base64-encoded string's length is a multiple of 4, which is required for proper decoding
        if (doc.length % 4 > 0) {
          doc += '=' * (4 - doc.length % 4);
        }
        List<int> bytes = base64Decode(doc);
        parsed[i]['pending'] = false;
        parsed[i]['parsedDocument'] = jsonDecode(String.fromCharCodes(bytes));
      }
      List<VaultCredential> tempCreds = parsed
          .map<VaultCredential>((json) => VaultCredential.fromJson(json))
          .toList();

      credentials.addAll(tempCreds);
    }
    return credentials;
  } catch (err) {
    log("$err");
    return [];
    // Fluttertoast.showToast(
    //   msg: "Failed to get Credentials!",
    //   toastLength: Toast.LENGTH_SHORT,
    //   gravity: ToastGravity.BOTTOM,
    //   backgroundColor: Colors.grey,
    //   textColor: Colors.white,
    //   fontSize: 40 * VaultTheme.rem,
    // );
  }
}