// Returns a contract by its block index and validates its signatures public Dictionary <string, dynamic> GetContract(string index, string privateKey) { var user = _usersDataAccess.FindByPublicKey(CryptographyService.GetPublicKeyFromPrivate(privateKey)); var encryptedSecretKey = user.CorrespondingIndices[index]; var block = _blockchainDataAccess.FindByIndex(index); var composedContract = new Dictionary <string, dynamic> { ["index"] = block.Index, ["hash"] = block.Hash, ["timestamp"] = block.Timestamp }; var decryptedSecretKey = CryptographyService.DecryptWithPrivateKey(encryptedSecretKey, privateKey); var dataToString = JObject.Parse(CryptographyService.Decrypt(block.Data, decryptedSecretKey)); // If decrypted data is a contract indeed if (!dataToString.ContainsKey("type") || !dataToString["type"].Value <string>().Equals("Contract")) { return(null); } var contractNoSignatures = JObject.Parse(CryptographyService.Decrypt(block.Data, decryptedSecretKey)); // Delete all signatures, since the signature will be validated on a “clean” contract ((JObject)contractNoSignatures["essentials"]).Property("signatures").Remove(); if (dataToString["essentials"]["type"].Value <string>().Equals("timeCharter")) { dataToString["essentials"]["vessel"] = JToken.FromObject(_vesselsDataAccess.FindByImoNumber(dataToString["essentials"]["vessel"].Value <string>())); } foreach (var signature in dataToString["essentials"]["signatures"]) { foreach (var(key, value) in ((JObject)signature).ToObject <Dictionary <string, dynamic> >()) { if (key.Equals("signer") || key.Equals("onBehalfOf")) { signature[key] = JToken.FromObject(_usersDataAccess.FindByPublicKey(value)); } } var signatureToValidate = signature["signature"].Value <string>(); var publicKey = signature["signer"]["publicKey"].Value <string>(); signature["valid"] = CryptographyService.Verify(contractNoSignatures.ToString(Formatting.None), signatureToValidate, publicKey); } // Convert each paragraph to readable format and add it to the contract foreach (var clause in dataToString["clauses"]) { foreach (var paragraph in clause["paragraphs"]) { paragraph["readable"] = Clause.GetParagraphReadable(paragraph.ToObject <Dictionary <string, dynamic> >()["legen"]); } } composedContract["contract"] = dataToString; return(composedContract); }
public Vessel FindByImoNumber(string imoNumber) { return(_vesselsDataAccess.FindByImoNumber(imoNumber)); }