getValidationSessionFlipDetail method

Future<ValidationSessionInfoFlips> getValidationSessionFlipDetail(
  1. ValidationSessionInfoFlips validationSessionInfoFlips,
  2. String address,
  3. bool simulationMode,
  4. String privateKey,
)

Implementation

Future<ValidationSessionInfoFlips> getValidationSessionFlipDetail(
    ValidationSessionInfoFlips validationSessionInfoFlips,
    String address,
    bool simulationMode,
    String privateKey) async {
  Completer<ValidationSessionInfoFlips> _completer =
      new Completer<ValidationSessionInfoFlips>();

  try {
    // get Flip
    FlipGetResponse flipGetResponse;
    FlipGetKeyResponse flipGetKeyResponse;
    FlipGetRequest flipGetRequest;

    Uint8List imageUint8_1;
    Uint8List imageUint8_2;
    Uint8List imageUint8_3;
    Uint8List imageUint8_4;

    Decoded images;
    Decoded privateImages;
    List listImages = new List(4);
    List orders = new List(2);

    if (simulationMode) {
      String data =
          await loadAssets(validationSessionInfoFlips.hash + "_images");
      flipGetResponse = flipGetResponseFromJson(data);

      if (flipGetResponse.result.privateHex != null &&
          flipGetResponse.result.privateHex != '0x') {
        // ;[images] = decode(publicHex || hex)
        if (flipGetResponse.result.publicHex != null) {
          images = decodeRlp(
              Uint8List.fromList(toBuffer(flipGetResponse.result.publicHex)),
              true);
        } else {
          if (flipGetResponse.result.hex != null) {
            images = decodeRlp(
                Uint8List.fromList(toBuffer(flipGetResponse.result.hex)),
                true);
          }
        }

        // let privateImages
        // ;[privateImages, orders] = decode(privateHex)
        privateImages = decodeRlp(
            Uint8List.fromList(toBuffer(flipGetResponse.result.privateHex)),
            true);
      } else {
        // TODO: implement this case
        // ;[images, orders] = decode(hex)
        var images3 = decodeRlp(
            Uint8List.fromList(toBuffer(flipGetResponse.result.hex)), true);
      }
    } else {
      if (this.nodeType == SHARED_NODE) {
        mapParams = {
          'method': FlipGetRequest.METHOD_NAME_RAW,
          'params': [validationSessionInfoFlips.hash],
          'id': 101,
          'key': keyApp
        };

        flipGetRequest = FlipGetRequest.fromJson(mapParams);
        body = json.encode(flipGetRequest.toJson());
        responseHttp = await http.post(Uri.parse(this.apiUrl),
            body: body, headers: requestHeaders);
        if (responseHttp.statusCode == 200) {
          flipGetResponse = flipGetResponseFromJson(responseHttp.body);
        }

        mapParams = {
          'method': FlipGetKeyRequest.METHOD_NAME,
          'params': [address, validationSessionInfoFlips.hash],
          'id': 101,
          'key': keyApp
        };

        FlipGetKeyRequest flipGetKeyRequest =
            FlipGetKeyRequest.fromJson(mapParams);
        body = json.encode(flipGetKeyRequest.toJson());
        responseHttp = await http.post(Uri.parse(this.apiUrl),
            body: body, headers: requestHeaders);
        if (responseHttp.statusCode == 200) {
          flipGetKeyResponse = flipGetKeyResponseFromJson(responseHttp.body);
        }
        var decryptedPublicPart = decryptMessage(
            flipGetKeyResponse.result.publicKey,
            flipGetResponse.result.publicHex);
        var decryptedPrivateKey =
            decryptMessage(privateKey, flipGetKeyResponse.result.privateKey);
        var decryptedPrivatePart = decryptMessage(
            decryptedPrivateKey, flipGetResponse.result.privateHex);

        // const [images] = decode(decryptedPublicPart)
        images = decodeRlp(
            Uint8List.fromList(toBuffer('0x' + decryptedPublicPart)), true);

        // const [privateImages, orders] = decode(decryptedPrivatePart)
        privateImages = decodeRlp(
            Uint8List.fromList(toBuffer('0x' + decryptedPrivatePart)), true);
      } else {
        mapParams = {
          'method': FlipGetRequest.METHOD_NAME,
          'params': [validationSessionInfoFlips.hash],
          'id': 101,
          'key': keyApp
        };
        if (this.nodeType == NORMAL_VPS_NODE) {
          SSHClientStatus sshClientStatus;
          sshClientStatus = await VpsUtil().connectVps(apiUrl, keyApp);
          if (sshClientStatus.sshClientStatus) {
            sshClient = sshClientStatus.sshClient;
          }
          var response = await ssh.HttpClientImpl(
                  clientFactory: () =>
                      ssh.SSHTunneledBaseClient(sshClientStatus.sshClient))
              .request(apiUrl,
                  method: 'POST',
                  data: jsonEncode(mapParams),
                  headers: requestHeaders);
          if (response.status == 200) {
            flipGetResponse = flipGetResponseFromJson(response.text);
          }
        } else {
          flipGetRequest = FlipGetRequest.fromJson(mapParams);
          body = json.encode(flipGetRequest.toJson());
          responseHttp = await http.post(Uri.parse(this.apiUrl),
              body: body, headers: requestHeaders);
          if (responseHttp.statusCode == 200) {
            flipGetResponse = flipGetResponseFromJson(responseHttp.body);
          }
        }

        if (flipGetResponse.result.privateHex != null &&
            flipGetResponse.result.privateHex != '0x') {
          // ;[images] = decode(publicHex || hex)
          if (flipGetResponse.result.publicHex != null) {
            images = decodeRlp(
                Uint8List.fromList(
                    toBuffer(flipGetResponse.result.publicHex)),
                true);
          } else {
            if (flipGetResponse.result.hex != null) {
              images = decodeRlp(
                  Uint8List.fromList(toBuffer(flipGetResponse.result.hex)),
                  true);
            }
          }

          // let privateImages
          // ;[privateImages, orders] = decode(privateHex)
          privateImages = decodeRlp(
              Uint8List.fromList(toBuffer(flipGetResponse.result.privateHex)),
              true);
        } else {
          // TODO: implement this case
          // ;[images, orders] = decode(hex)
          var images3 = decodeRlp(
              Uint8List.fromList(toBuffer(flipGetResponse.result.hex)), true);
        }
      }
    }

    // images = images.concat(privateImages)
    imageUint8_1 = images.data[0][0];
    imageUint8_2 = images.data[0][1];
    imageUint8_3 = privateImages.data[0][0];
    imageUint8_4 = privateImages.data[0][1];
    listImages[0] = imageUint8_1;
    listImages[1] = imageUint8_2;
    listImages[2] = imageUint8_3;
    listImages[3] = imageUint8_4;
    orders = privateImages.data[1];

    String order1 =
        orders[0][0].toString().replaceAll('[', '').replaceAll(']', '');
    String order2 =
        orders[0][1].toString().replaceAll('[', '').replaceAll(']', '');
    String order3 =
        orders[0][2].toString().replaceAll('[', '').replaceAll(']', '');
    String order4 =
        orders[0][3].toString().replaceAll('[', '').replaceAll(']', '');
    validationSessionInfoFlips.listImagesLeft = new List<Uint8List>(4);
    validationSessionInfoFlips.listImagesLeft[0] =
        listImages[int.tryParse(order1) ?? 0];
    validationSessionInfoFlips.listImagesLeft[1] =
        listImages[int.tryParse(order2) ?? 0];
    validationSessionInfoFlips.listImagesLeft[2] =
        listImages[int.tryParse(order3) ?? 0];
    validationSessionInfoFlips.listImagesLeft[3] =
        listImages[int.tryParse(order4) ?? 0];

    // TODO .. dirty
    order1 = orders[1][0].toString().replaceAll('[', '').replaceAll(']', '');
    order2 = orders[1][1].toString().replaceAll('[', '').replaceAll(']', '');
    order3 = orders[1][2].toString().replaceAll('[', '').replaceAll(']', '');
    order4 = orders[1][3].toString().replaceAll('[', '').replaceAll(']', '');
    validationSessionInfoFlips.listImagesRight = new List<Uint8List>(4);
    validationSessionInfoFlips.listImagesRight[0] =
        listImages[int.tryParse(order1) ?? 0];
    validationSessionInfoFlips.listImagesRight[1] =
        listImages[int.tryParse(order2) ?? 0];
    validationSessionInfoFlips.listImagesRight[2] =
        listImages[int.tryParse(order3) ?? 0];
    validationSessionInfoFlips.listImagesRight[3] =
        listImages[int.tryParse(order4) ?? 0];
  } catch (e) {
    logger.e(e.toString());
  }

  //print("flip loaded : " + validationSessionInfoFlips.hash);

  _completer.complete(validationSessionInfoFlips);
  return _completer.future;
}