def toJSON(self): """ generated source for method toJSON """ theJSON = JSONObject() try: theJSON.put("matchId", self.matchId) theJSON.put("randomToken", self.randomToken) theJSON.put("startTime", self.startTime.getTime()) theJSON.put("gameMetaURL", getGameRepositoryURL()) theJSON.put("isCompleted", self.isCompleted) theJSON.put("isAborted", self.isAborted) theJSON.put("states", JSONArray(renderArrayAsJSON(renderStateHistory(self.stateHistory), True))) theJSON.put("moves", JSONArray(renderArrayAsJSON(renderMoveHistory(self.moveHistory), False))) theJSON.put("stateTimes", JSONArray(renderArrayAsJSON(self.stateTimeHistory, False))) if len(self.errorHistory) > 0: theJSON.put("errors", JSONArray(renderArrayAsJSON(renderErrorHistory(self.errorHistory), False))) if len(self.goalValues) > 0: theJSON.put("goalValues", self.goalValues) theJSON.put("previewClock", self.previewClock) theJSON.put("startClock", self.startClock) theJSON.put("playClock", self.playClock) if self.thePlayerNamesFromHost != None: theJSON.put("playerNamesFromHost", self.thePlayerNamesFromHost) if self.isPlayerHuman != None: theJSON.put("isPlayerHuman", self.isPlayerHuman) theJSON.put("scrambled", self.theGdlScrambler.scrambles() if self.theGdlScrambler != None else False) except JSONException as e: return None if self.theCryptographicKeys != None: try: SignableJSON.signJSON(theJSON, self.theCryptographicKeys.thePublicKey, self.theCryptographicKeys.thePrivateKey) if not SignableJSON.isSignedJSON(theJSON): raise Exception("Could not recognize signed match: " + theJSON) if not SignableJSON.verifySignedJSON(theJSON): raise Exception("Could not verify signed match: " + theJSON) except Exception as e: System.err.println(e) theJSON.remove("matchHostPK") theJSON.remove("matchHostSignature") return theJSON.__str__()
def verifySignedJSON(cls, theJSON): """ generated source for method verifySignedJSON """ if not theJSON.has("matchHostPK") or not theJSON.has("matchHostSignature"): raise RuntimeException("JSON not signed! Cannot verify.") thePK = theJSON.getString("matchHostPK") theSignature = theJSON.getString("matchHostSignature") if not theSignature.startsWith(cls.theCanonicalizationPrefix): return False theSignature = theSignature.replaceFirst(cls.theCanonicalizationPrefix, "") tempObject = JSONObject(theJSON.__str__()) tempObject.remove("matchHostSignature") try: return BaseCryptography.verifySignature(thePK, theSignature, CanonicalJSON.getCanonicalForm(tempObject, CanonicalizationStrategy.SIMPLE)) except InvalidKeyException as e: pass except SignatureException as e: pass except NoSuchAlgorithmException as e: pass except UnsupportedEncodingException as e: pass return False