update certificate request data structure

Change-Id: Id73428d07e6a0315dcdf78da8aba380f429a8f46
diff --git a/src/challenge-module/challenge-pin.cpp b/src/challenge-module/challenge-pin.cpp
index 9302e42..caad97d 100644
--- a/src/challenge-module/challenge-pin.cpp
+++ b/src/challenge-module/challenge-pin.cpp
@@ -19,6 +19,7 @@
  */
 
 #include "challenge-pin.hpp"
+
 #include <ndn-cxx/util/random.hpp>
 
 namespace ndn {
@@ -51,31 +52,35 @@
     JsonSection secretJson;
     secretJson.add(PARAMETER_KEY_CODE, secretCode);
     _LOG_TRACE("Secret for request " << request.m_requestId << " : " << secretCode);
-    return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson), m_maxAttemptTimes, m_secretLifetime.count());
+    return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson), m_maxAttemptTimes, m_secretLifetime);
   }
-
-  if (request.m_challengeStatus == NEED_CODE || request.m_challengeStatus == WRONG_CODE) {
-    _LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.m_challengeStatus);
-    // the incoming interest should bring the pin code
-    std::string givenCode = readString(params.get(tlv_parameter_value));
-    auto secret = request.m_challengeSecrets;
-    if (currentTime - time::fromIsoString(request.m_challengeTp) >= m_secretLifetime) {
-      return returnWithError(request, ErrorCode::OUT_OF_TIME, "Secret expired.");
-    }
-    if (givenCode == secret.get<std::string>(PARAMETER_KEY_CODE)) {
-      _LOG_TRACE("Correct PIN code. Challenge succeeded.");
-      return returnWithSuccess(request);
-    }
-    // check rest attempt times
-    if (request.m_remainingTries > 1) {
-      auto remainTime = m_secretLifetime - (currentTime - time::fromIsoString(request.m_challengeTp));
-      _LOG_TRACE("Wrong PIN code provided. Remaining Tries - 1.");
-      return returnWithNewChallengeStatus(request, WRONG_CODE, std::move(secret), request.m_remainingTries - 1, remainTime.count());
-    }
-    else {
-      // run out times
-      _LOG_TRACE("Wrong PIN code provided. Ran out tires. Challenge failed.");
-      return returnWithError(request, ErrorCode::OUT_OF_TRIES, "Ran out tires.");
+  if (request.m_challengeState) {
+    if (request.m_challengeState->m_challengeStatus == NEED_CODE ||
+        request.m_challengeState->m_challengeStatus == WRONG_CODE) {
+      _LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.m_challengeState->m_challengeStatus);
+      // the incoming interest should bring the pin code
+      std::string givenCode = readString(params.get(tlv_parameter_value));
+      auto secret = request.m_challengeState->m_secrets;
+      if (currentTime - request.m_challengeState->m_timestamp >= m_secretLifetime) {
+        return returnWithError(request, ErrorCode::OUT_OF_TIME, "Secret expired.");
+      }
+      if (givenCode == secret.get<std::string>(PARAMETER_KEY_CODE)) {
+        _LOG_TRACE("Correct PIN code. Challenge succeeded.");
+        return returnWithSuccess(request);
+      }
+      // check rest attempt times
+      if (request.m_challengeState->m_remainingTries > 1) {
+        auto remainTime = m_secretLifetime - (currentTime - request.m_challengeState->m_timestamp);
+        _LOG_TRACE("Wrong PIN code provided. Remaining Tries - 1.");
+        return returnWithNewChallengeStatus(request, WRONG_CODE, std::move(secret),
+                                            request.m_challengeState->m_remainingTries - 1,
+                                            time::duration_cast<time::seconds>(remainTime));
+      }
+      else {
+        // run out times
+        _LOG_TRACE("Wrong PIN code provided. Ran out tires. Challenge failed.");
+        return returnWithError(request, ErrorCode::OUT_OF_TRIES, "Ran out tires.");
+      }
     }
   }
   return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected status or challenge status");