Esempio n. 1
0
 /// <summary>
 /// Imports a public key to the wallet of the server node.
 /// This allows for checking the balance of the public key.
 /// </summary>
 /// <param name="person"></param>
 public static void ImportPublicKeyToWallet(Person person)
 {
     string chainName = WebConfigurationManager.AppSettings["ChainName"];
     string nodeIp = WebConfigurationManager.AppSettings["NodeIp"];
     MultiChainClient client = new MultiChainClient(chainName,nodeIp);
     client.ImportAddress(person.PublicKey);
 }
Esempio n. 2
0
 public Repository()
 {
     _client.DefaultRequestHeaders.Accept.Clear();
     _client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
     _loginToken = (LoginSession)HttpContext.Current.Session ["token"];
     _thisPerson = (Person)HttpContext.Current.Session ["person"];
     //_client.DefaultRequestHeaders.Add("Authorization", Repository.Token);
 }
Esempio n. 3
0
 /// <summary>
 /// Gets a given person's (user) assets.
 /// </summary>
 /// <param name="person">person (user)</param>
 /// <returns>AddressBalance array</returns>
 public static AddressBalance[] GetPersonBalance(Person person)
 {
     string chainName = WebConfigurationManager.AppSettings["ChainName"];
     string nodeIp = WebConfigurationManager.AppSettings["NodeIp"];
     MultiChainClient client = new MultiChainClient(chainName,nodeIp);
     var resp = client.GetAddressbalances (person.PublicKey);
     return resp.Balances;
 }
 /// <summary>
 /// Renews a LoginSession and replaces an already existing session.
 /// </summary>
 /// <param name="context">DataContext</param>
 /// <param name="session">LoginSession</param>
 /// <param name="person">Person (user)</param>
 /// <returns>LoginSession</returns>
 public static LoginSession RenewSession(DataContext context,LoginSession session,Person person)
 {
     session = new LoginSession (Guid.NewGuid ().ToString (), DateTime.Now);
     if (person.LoginSession != null)
         context.LoginSessions.Remove (person.LoginSession);
     person.LoginSession = session;
     context.SaveChanges();
     return session;
 }
Esempio n. 5
0
        public void LoginAttempt(string username, string password)
        {
            string publicKey = GetPublicKey ();
            if (publicKey != "") {
                Person person = new Person () { PublicKey = publicKey, Username = username, Password = password };

                StringContent content = new StringContent (JsonConvert.SerializeObject (person));
                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue ("application/json");
                HttpResponseMessage response = _client.PostAsync ("Person/Login", content).Result;

                if (!response.IsSuccessStatusCode) {
                    System.Web.HttpContext.Current.Response.Write ("<SCRIPT LANGUAGE=\"\"JavaScript\"\">alert(\"Login failed: " + response.StatusCode + ", make sure your login is correct and that your multichain node is the node associated with the account.\")</SCRIPT>");
                } else {
                    var stream = response.Content.ReadAsStringAsync ().Result;
                    LoginSession session = JsonConvert.DeserializeObject<LoginSession> (stream);
                    _loginToken = session;
                    person.LoginSession = _loginToken;
                    person.LoginSessionToken = _loginToken.Token;
                    _thisPerson = person;
                    HttpContext.Current.Session ["token"] = _loginToken;
                    HttpContext.Current.Session ["person"] = _thisPerson;
                }
            } else {
                System.Web.HttpContext.Current.Response.Write ("<SCRIPT LANGUAGE=\"\"JavaScript\"\">alert(\"Could not contact multichain node, make sure its running.\")</SCRIPT>");
            }
        }
        public ActionResult Login(Person person)
        {
            using (DataContext context = new DataContext())
            {
                var match =
                    context.Persons.FirstOrDefault(x => x.PublicKey == person.PublicKey &&x.Username == person.Username && x.Password == person.Password);

                if (match == null) {
                    if (!CreateNewPerson (person))
                        return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest);
                    match =
                        context.Persons.FirstOrDefault(x => x.PublicKey == person.PublicKey &&x.Username == person.Username && x.Password == person.Password);
                }

                if (match.LoginSession != null)
                    return Content ( JsonConvert.SerializeObject(RenewSession(context,match.LoginSession,match)));

                match.LoginSession = RenewSession(context,null,match);
                context.SaveChanges();

                return Content ( JsonConvert.SerializeObject(match.LoginSession));
            }
        }
        /// <summary>
        /// Creates a new user and returns a boolean (success).
        /// </summary>
        /// <param name="person">Person</param>
        /// <returns>Success boolean</returns>
        private bool CreateNewPerson(Person person)
        {
            using (DataContext context = new DataContext())
            {
                var match =
                    context.Persons.FirstOrDefault(x => x.PublicKey == person.PublicKey);

                if (match != null)
                    return false;

                if (String.IsNullOrEmpty(person.PublicKey) || String.IsNullOrEmpty(person.Username) || String.IsNullOrEmpty(person.Password))
                    return false;

                person.LoginSession = null;
                context.Persons.Add(person);
                context.SaveChanges();
                BlockChain.ImportPublicKeyToWallet (person);
                return true;
            }
        }
        /// <summary>
        /// Returns the S and F from the BlockChain for a given person (user).
        /// </summary>
        /// <param name="person">Person (user)</param>
        /// <returns>double array, first index is S amount, second index is F amount.</returns>
        private double[] GetSFFromBlockChain(Person person)
        {
            var balances = BlockChain.GetPersonBalance (person);

            var sBalance = balances.Where (x => x.name.StartsWith("S"));
            var fBalance = balances.Where (x => x.name.StartsWith("F"));
            var sAmount = sBalance != null ? sBalance.Sum(x=>x.qty) : 0;
            var fAmount = fBalance != null ? fBalance.Sum(x=>x.qty) : 0;

            return new double[] {sAmount, fAmount};
        }
        /// <summary>
        /// Calculates the Bayesian reputation value for a given Person (user).
        /// </summary>
        /// <param name="person">Person (user)</param>
        /// <returns>double reputation value</returns>
        private double CalculateBayesianModelTrust(Person person)
        {
            double initialTrust = Convert.ToDouble(WebConfigurationManager.AppSettings["InitialTrust"]);
            double trustProblabilityOfGoodPerson = Convert.ToDouble(WebConfigurationManager.AppSettings["TrustProblabilityOfGoodPerson"]);
            double nonTrustProblabilityOfBadPerson = Convert.ToDouble(WebConfigurationManager.AppSettings["ŃonTrustProblabilityOfBadPerson"]);

            double[] sfFloats = GetSFFromBlockChain(person);

            double reputation =  (initialTrust*Math.Pow(trustProblabilityOfGoodPerson, sfFloats[0])*
                    Math.Pow(1 - trustProblabilityOfGoodPerson, sfFloats[1]))/
                (initialTrust*Math.Pow(trustProblabilityOfGoodPerson, sfFloats[0])*
                 Math.Pow(1 - trustProblabilityOfGoodPerson, sfFloats[1]) +
                 (1 - initialTrust)*Math.Pow(nonTrustProblabilityOfBadPerson, sfFloats[0])*
                 Math.Pow(1 - nonTrustProblabilityOfBadPerson, sfFloats[1]));
            return reputation;
        }