예제 #1
0
        public JObject Register(JObject json)
        {
            var user = json["fields"].ToObject <User>();

            json = Validate(json);
            if (json.ContainsKey("invalidFields"))
            {
                return(json);
            }

            user.PublicKey            = CryptographyService.GetPublicKeyFromPrivate(user.PrivateKey);
            user.PrivateKey           = null; // since the service lies on blockchain, private keys must not be stored
            user.Enabled              = true;
            user.CorrespondingIndices = new Dictionary <string, string>();
            // For now, the newly created user is considered to belong to the demo company created during initialization.
            if (user.Position.Equals("companyVesselsRegistryManager"))
            {
                user.Company = "1234567890";
            }

            _usersDataAccess.Insert(user);
            json["saved"] = _usersDataAccess.FindByPublicKey(user.PublicKey) != null;
            if (!json["saved"].ToObject <bool>())
            {
                return(json);
            }

            json["token"] = GenerateToken(user);
            return(json);
        }
예제 #2
0
        public JObject Insert(JObject json, string privateKey)
        {
            json = Validate(json);
            if (json.ContainsKey("invalidFields"))
            {
                return(json);
            }
            var vessel = json.ToObject <Vessel>();

            vessel.Company = _usersDataAccess.FindByPublicKey(CryptographyService.GetPublicKeyFromPrivate(privateKey)).Company;

            try
            {
                _vesselsDataAccess.Insert(vessel);
                var company = _companiesDataAccess.FindByRegistryNumber(vessel.Company);
                var vessels = company.Vessels;
                vessels.AddLast(vessel.Id);
                company.Vessels = vessels;
                _companiesDataAccess.Update(company);
                json["saved"] = true;
                return(json);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                json["saved"]        = false;
                json["errorDetails"] = e.Message;
                return(json);
            }
        }
예제 #3
0
        // Creates, encrypts and saves a new contract in the database as block
        // Currently not used - implementation pending
        public void ConcludeContract(Contract contract)
        {
            // Parse contract into json string
            var jsonToString = JsonConvert.SerializeObject(contract);

            // Encrypt string
            var encryptedData     = CryptographyService.Encrypt(jsonToString);
            var encryptedContract = encryptedData[0];
            var secretKey         = encryptedData[1];

            // Save block in the database
            var lastBlock      = _blockchainDataAccess.FindLast();
            var lastBlockIndex = lastBlock.Index;
            var lastBlockHash  = lastBlock.Hash;
            var newBlockIndex  = (Convert.ToInt64(lastBlockIndex) + 1).ToString();

            _blockchainDataAccess.Insert(new Block(newBlockIndex, encryptedContract, lastBlockHash));

            // Encrypt and send the secret key to all users included in the contract
            var signatures = (List <dynamic>)contract.Essentials["signatures"];

            foreach (var signature in signatures.Select(hashMap => (Dictionary <string, dynamic>)hashMap))
            {
                foreach (var(key, value) in signature)
                {
                    if (!key.Equals("signer") && !key.Equals("onBehalfOf"))
                    {
                        continue;
                    }
                    var encryptedSecret          = CryptographyService.EncryptWithPublicKey(secretKey, (string)value);
                    var userToBeUpdated          = _usersDataAccess.FindByPublicKey((string)value);
                    var userCorrespondingIndices = userToBeUpdated.CorrespondingIndices;
                    userCorrespondingIndices[newBlockIndex] = encryptedSecret;
                    _usersDataAccess.Update(userToBeUpdated);
                }
            }
        }