Ejemplo n.º 1
0
        public async Task CreateNewAccount()
        {
            JObject jsonContent = null;

            try
            {
                var body = await this.Request.Content.ReadAsStringAsync();

                jsonContent = JObject.Parse(body);
            }
            catch (JsonReaderException)
            {
                CreateErrorResponse(HttpStatusCode.BadRequest, "Reading JSON Exception");
            }


            if (!(jsonContent["accountKey"] is JValue) && !(jsonContent["login"] is JValue) && !(jsonContent["password"] is JValue))
            {
                CreateErrorResponse(HttpStatusCode.BadRequest, "Bad json");
            }


            //var key = KeyParser((string)jsonContent["accountKey"]);

            //Account account = work.Accounts.GetAccount(key);
            //if (account == null)
            //{
            //    BadRequest();
            //}

            //if ( account.Role != UserRole.Admin)
            //{
            //    CreateErrorResponse(HttpStatusCode.Unauthorized, "Need admin permissions ");
            //}
            AccountKeyBuilder keyBuilder = new AccountKeyBuilder();
            var     password             = (string)jsonContent["password"];
            Account newAcc;

            using (var hasher = SHA256.Create())
            {
                newAcc = new Account()
                {
                    Key      = keyBuilder.CreateAccKey(),
                    Login    = (string)jsonContent["login"],
                    Password = new HexString(hasher.ComputeHash
                                                 (DataProvider.Serializer.ToBinaryArray(password))),
                    Role = UserRole.Admin
                };
            }
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream    stream    = new MemoryStream();

            formatter.Serialize(stream, newAcc);
            Record record = new Record(1, "New acc " + newAcc.Login,
                                       new HexString(stream.ToArray()), TypeData.Account);
            var pub_key = new ECKeyValidator().RSA.ToXmlString(false);

            new ECKeyValidator().RSA.PersistKeyInCsp = true;
            await work.TransactionValidator.ValidateTransaction(record, pub_key);
        }
Ejemplo n.º 2
0
        public HttpResponseMessage GetChain()
        {
            var currentContext = HttpContext.Current;
            var ip             = currentContext.Request.Url.Authority;

            if (currentContext.IsWebSocketRequest ||
                currentContext.IsWebSocketRequestUpgrading)
            {
                currentContext.AcceptWebSocketRequest(SendChainFromWebSockets);
                return(Request.CreateResponse(HttpStatusCode.SwitchingProtocols));
            }

            var accountKey = currentContext.Request.Form["key"];

            if (accountKey == null)
            {
                CreateErrorResponse(HttpStatusCode.Unauthorized,
                                    "Key can't be null  ");
            }
            HexString hexKey = KeyParser(accountKey);

            var     rawKey  = AccountKeyBuilder.Decode(hexKey.ToByteArray());
            Account account = work.Accounts.GetAccount(rawKey);

            if (account == null)
            {
                BadRequest();
            }

            if (account.Role == UserRole.Unset || account.Role == UserRole.Writer)
            {
                CreateErrorResponse(HttpStatusCode.Unauthorized,
                                    "Permission denied. User not have permission for reading  ");
            }


            var chain    = connector.GetLocalChain();
            var rawChain = JsonConvert.SerializeObject(chain);

            return(Request.CreateResponse(HttpStatusCode.OK, rawChain));
        }
Ejemplo n.º 3
0
        public void TestValidateTransactions()
        {
            TransactionValidator validator = new TransactionValidator();
            string dataString = "String";

            byte[]            data       = Serializer.ToHexString(dataString);
            var               message    = "Peace!!!!";
            AccountKeyBuilder keyBuilder = new AccountKeyBuilder();
            Account           newAcc;

            using (var hasher = SHA256.Create())
            {
                newAcc = new Account()
                {
                    Key      = keyBuilder.CreateAccKey(),
                    Login    = message,
                    Password = new HexString(hasher.ComputeHash
                                                 (Serializer.ToBinaryArray("212121"))),
                    Role = UserRole.Admin
                };
            }
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream    stream    = new MemoryStream();

            formatter.Serialize(stream, newAcc);
            Record record = new Record(1, "New acc " + newAcc.Login,
                                       new HexString(stream.ToArray()), TypeData.Account);

            Record records = new Record(1, message, new HexString(data), TypeData.Host);

            var jArray = JArray.FromObject(new List <Record>()
            {
                records
            });
            // string privkey = new ECKeyValidator().CreateKeys().ToXmlString(true);
            string pubkey = new ECKeyValidator().RSA.ToXmlString(false);

            validator.ValidateTransaction(record, pubkey).Wait();
        }