public Server(Blockchain blockchain, string host, string port) { var server = new TinyWebServer.WebServer(request => { string path = request.Url.PathAndQuery.ToLower(); string query; if (path.Contains("?")) { string[] parts = path.Split('?'); path = parts[0]; query = parts[1]; } switch (path) { case "/mine": return(blockchain.Mine()); case "/chain": return(blockchain.GetChain()); case "/node/register": string json = new StreamReader(request.InputStream).ReadToEnd(); var urlList = new { Urls = "" }; var obj = JsonConvert.DeserializeAnonymousType(json, urlList); return(blockchain.RegNode(obj.Urls)); case "/transaction/new": json = new StreamReader(request.InputStream).ReadToEnd(); Transaction trx = JsonConvert.DeserializeObject <Transaction>(json); int blockId = blockchain.NewTransaction(trx.sender, trx.recipient, trx.amount); return("Ваша транзакция была добавлена"); case "/node/resolve": return(blockchain.Consensus()); } return(""); }, "http://" + host + ":" + port + "/mine/", "http://" + host + ":" + port + "/chain/", "http://" + host + ":" + port + "/transaction/new/", "http://" + host + ":" + port + "/node/register/", "http://" + host + ":" + port + "/node/resolve/" ); server.Run(); }
public WebServer(BlockChain chain) { var settings = ConfigurationManager.AppSettings; string host = settings["host"]?.Length > 1 ? settings["host"] : "localhost"; string port = settings["port"]?.Length > 1 ? settings["port"] : "12345"; WriteDebug($"Endpoint : http://{host}:{port}/"); var server = new TinyWebServer.WebServer(request => { string path = request.Url.PathAndQuery.ToLower(); string query = ""; string json = ""; if (path.Contains("?")) { string[] parts = path.Split('?'); path = parts[0]; query = parts[1]; } WriteDebug($"{request.HttpMethod} {path}"); switch (path) { case "/mine": return(chain.Mine()); case "/transactions/new": if (request.HttpMethod != HttpMethod.Post.Method) { return($"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}"); } else { json = new StreamReader(request.InputStream).ReadToEnd(); Transaction trx = JsonConvert.DeserializeObject <Transaction>(json); int blockId = chain.CreateTransaction(trx.Sender, trx.Recipient, trx.Amount); return($"Your Transaction will be included in Block {blockId}"); } case "/chain": return(chain.GetFullChain()); case "/nodes/register": if (request.HttpMethod != HttpMethod.Post.Method) { return($"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}"); } else { json = new StreamReader(request.InputStream).ReadToEnd(); var urlList = new { Urls = new string[0] }; var obj = JsonConvert.DeserializeAnonymousType(json, urlList); return(chain.RegisterNodes(obj.Urls)); } case "/nodes/resolve": return(chain.Consensus()); } return(""); }, $"http://{host}:{port}/mine/", $"http://{host}:{port}/transactions/new/", $"http://{host}:{port}/chain/", $"http://{host}:{port}/nodes/register/", $"http://{host}:{port}/nodes/resolve/" ); server.Run(); }
public WebServer(BlockChain chain, string port) { var settings = ConfigurationManager.AppSettings; string host = settings["host"]?.Length > 1 ? settings["host"] : "localhost"; //string port = settings["port"]?.Length > 1 ? settings["port"] : "12345"; var server = new TinyWebServer.WebServer(request => { string path = request.Url.PathAndQuery.ToLower(); string query = ""; string json = ""; if (path.Contains("?")) { string[] parts = path.Split('?'); path = parts[0]; query = parts[1]; } switch (path) { //GET: http://localhost:12345/mine case "/mine": return(chain.Mine()); //POST: http://localhost:12345/transactions/new //{ "Amount":123, "Recipient":"ebeabf5cc1d54abdbca5a8fe9493b479", "Sender":"31de2e0ef1cb4937830fcfd5d2b3b24f" } case "/transactions/new": if (request.HttpMethod != HttpMethod.Post.Method) { return($"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}"); } json = new StreamReader(request.InputStream).ReadToEnd(); Transaction trx = JsonConvert.DeserializeObject <Transaction>(json); int blockId = chain.CreateTransaction(trx.Sender, trx.Recipient, trx.Amount); return($"Your transaction will be included in block {blockId}"); //GET: http://localhost:12345/chain case "/chain": return(chain.GetFullChain()); //POST: http://localhost:12345/nodes/register //{ "Urls": ["localhost:54321", "localhost:54345", "localhost:12321"] } //curl -H "Content-Type: application/json" -X POST -d '{ "Urls": ["localhost:54321"] }' http://localhost:12345/nodes/register case "/nodes/register": if (request.HttpMethod != HttpMethod.Post.Method) { return($"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}"); } json = new StreamReader(request.InputStream).ReadToEnd(); var urlList = new { Urls = new string[0] }; var obj = JsonConvert.DeserializeAnonymousType(json, urlList); System.Console.WriteLine("New player joined: " + json); return(chain.RegisterNodes(obj.Urls)); //GET: http://localhost:12345/nodes/resolve case "/nodes/resolve": return(chain.Consensus()); } return(""); }, $"http://{host}:{port}/mine/", $"http://{host}:{port}/transactions/new/", $"http://{host}:{port}/chain/", $"http://{host}:{port}/nodes/register/", $"http://{host}:{port}/nodes/resolve/" ); server.Run(); }
public WebServer(BlockChain chain) { ReadAllSettings(); AddUpdateAppSettings("host", GetLocalIPv4(NetworkInterfaceType.Ethernet)); var settings = ConfigurationManager.AppSettings; //ApplySettings("host", hosti); string host = settings["host"]?.Length > 1 ? settings["host"] : "localhost"; string port = settings["port"]?.Length > 1 ? settings["port"] : "12345"; ReadAllSettings(); //Console.ReadKey(); var server = new TinyWebServer.WebServer(request => { // HttpContent testi; //request.Headers.Set("Access-Control-Allow-Origin", "*"); //request.Headers.Add("Access-Control-Allow-Origin", "*"); string path = request.Url.PathAndQuery.ToLower(); string query = ""; string json = ""; if (path.Contains("?")) { string[] parts = path.Split('?'); path = parts[0]; query = parts[1]; } switch (path) { //GET: http://localhost:12345/mine case "/mine": //FindAndSave(chain); return(chain.Mine()); //POST: http://localhost:12345/transactions/new //{ "Amount":123, "Recipient":"ebeabf5cc1d54abdbca5a8fe9493b479", "Sender":"31de2e0ef1cb4937830fcfd5d2b3b24f" } case "/transactions/new": if (request.HttpMethod != HttpMethod.Post.Method) { return($"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}"); } json = new StreamReader(request.InputStream).ReadToEnd(); Transaction trx = JsonConvert.DeserializeObject <Transaction>(json); int blockId = chain.CreateTransaction(trx.Sender, trx.Recipient, trx.Amount); //var s = chain.GetFullChain(); // System.Console.WriteLine(s); //System.IO.File.WriteAllText("file.json", s); //FindAndSave(chain); return($"Your transaction will be included in block {blockId}"); //GET: http://localhost:12345/chain case "/chain": return(chain.GetFullChain()); //POST: http://localhost:12345/nodes/register //{ "Urls": ["localhost:54321", "localhost:54345", "localhost:12321"] } case "/nodes/register": if (request.HttpMethod != HttpMethod.Post.Method) { return($"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}"); } //request.Headers.Add("Access-Control-Allow-Origin", "*"); json = new StreamReader(request.InputStream).ReadToEnd(); var urlList = new { Urls = new string[0] }; var obj = JsonConvert.DeserializeAnonymousType(json, urlList); return(chain.RegisterNodes(obj.Urls)); //GET: http://localhost:12345/nodes/resolve case "/nodes/resolve": return(chain.Consensus()); case "/tallenna": //InsertToDatabase(chain).Wait(); FindAndSave(chain); return("Tallennettiin tietokantaan"); case "/testi": //MainAsync(chain).Wait(); //FindAllFromChain().Wait(); // CallMain(chain).Wait(); //InsertToDatabase(chain).Wait(); FindAndSave(chain); return($"Tallennettiin tietokantaan,haku tietokannasta{chain.GetFullChain()}"); } return(""); }, $"http://{host}:{port}/mine/", $"http://{host}:{port}/transactions/new/", $"http://{host}:{port}/chain/", $"http://{host}:{port}/nodes/register/", $"http://{host}:{port}/nodes/resolve/", $"http://{host}:{port}/testi/", //Added by Severi $"http://{host}:{port}/tallenna/" ); server.Run(); }
public WebServer(BlockChain chain, string host = null, string port = null) { var settings = ConfigurationManager.AppSettings; _host = string.IsNullOrEmpty(host) ? (settings["host"]?.Length > 1 ? settings["host"] : "localhost") : host; _port = string.IsNullOrEmpty(port) ?(settings["port"]?.Length > 1 ? settings["port"] : "12345"):port; Url = $"http://{_host}:{_port}/"; _server = new TinyWebServer.WebServer(request => { string path = request.Url.PathAndQuery.ToLower(); string query = ""; string json = ""; if (path.Contains("?")) { string[] parts = path.Split('?'); path = parts[0]; query = parts[1]; } switch (path) { //GET: http://localhost:12345/mine case "/mine": chain.Mine(); return($"Mining process is started"); //POST: http://localhost:12345/transactions/new //{ "Amount":123, "Recipient":"ebeabf5cc1d54abdbca5a8fe9493b479", "Sender":"31de2e0ef1cb4937830fcfd5d2b3b24f" } case "/transactions/new": if (request.HttpMethod != HttpMethod.Post.Method) { return($"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}"); } json = new StreamReader(request.InputStream).ReadToEnd(); Transaction trx = JsonConvert.DeserializeObject <Transaction>(json); int blockId = chain.CreateTransaction(trx.Id, trx.Sender, trx.Recipient, trx.Amount, trx.Signature); if (blockId > 0) { return($"Your transaction will be included in block {blockId}"); } else { return("Your transaction is invalid"); } //POST: http://localhost:12345/transactions/add //{ "Id": "123", "Amount":123, "Recipient":"ebeabf5cc1d54abdbca5a8fe9493b479", "Sender":"31de2e0ef1cb4937830fcfd5d2b3b24f" } case "/transactions/add": if (request.HttpMethod != HttpMethod.Post.Method) { return($"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}"); } json = new StreamReader(request.InputStream).ReadToEnd(); Transaction incomeTrx = JsonConvert.DeserializeObject <Transaction>(json); chain.AddTransaction(incomeTrx.Id, incomeTrx.Sender, incomeTrx.Recipient, incomeTrx.Amount, incomeTrx.Signature); return($"Your transaction has been added"); //POST: http://localhost:12345/transactions/edit //{ "Id": "123", "Amount":123, "Recipient":"ebeabf5cc1d54abdbca5a8fe9493b479", "Sender":"31de2e0ef1cb4937830fcfd5d2b3b24f" } case "/transactions/edit": if (request.HttpMethod != HttpMethod.Post.Method) { return($"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}"); } json = new StreamReader(request.InputStream).ReadToEnd(); Transaction newTrx = JsonConvert.DeserializeObject <Transaction>(json); chain.EditTransaction(newTrx.Id, newTrx.Sender, newTrx.Recipient, newTrx.Amount, newTrx.Signature); return($"Your transaction has been edited"); //GET: http://localhost:12345/chain case "/chain": return(chain.GetFullChain()); //POST: http://localhost:12345/nodes/register //{ "Urls": ["localhost:54321", "localhost:54345", "localhost:12321"] } case "/nodes/register": if (request.HttpMethod != HttpMethod.Post.Method) { return($"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}"); } json = new StreamReader(request.InputStream).ReadToEnd(); var urlList = new { Urls = new string[0] }; var obj = JsonConvert.DeserializeAnonymousType(json, urlList); return(chain.RegisterNodes(obj.Urls)); //GET http://localhost:12345/nodes case "/nodes": return(chain.GetAllRegisteredNodes()); //GET: http://localhost:12345/nodes/resolve case "/nodes/resolve": return(chain.Consensus()); //GET: http://localhost:12345/nodes/update case "/consensus/request": chain.RequestUpdate(); return("A consensus request has been sent"); //POST: http://localhost:12345/balance/query //{ "Owner": "Node 1"} case "/balance/query": if (request.HttpMethod != HttpMethod.Post.Method) { return($"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}"); } json = new StreamReader(request.InputStream).ReadToEnd(); var ownerQuery = new { Owner = "" }; var owner = JsonConvert.DeserializeAnonymousType(json, ownerQuery); var balance = chain.QueryBalance(owner.Owner); return(balance.ToString()); //GET: http://localhost:12345/transactions/pool case "/transactions/pool": return(chain.GetTransactionsMemTool()); } return(""); }, $"http://{_host}:{_port}/mine/", $"http://{_host}:{_port}/transactions/new/", $"http://{_host}:{_port}/transactions/add/", $"http://{_host}:{_port}/transactions/edit/", $"http://{_host}:{_port}/chain/", $"http://{_host}:{_port}/nodes/register/", $"http://{_host}:{_port}/nodes/", $"http://{_host}:{_port}/nodes/resolve/", $"http://{_host}:{_port}/consensus/request/", $"http://{_host}:{_port}/balance/query/", $"http://{_host}:{_port}/transactions/pool/" ); }
public WebServer(Blockchain blockchain, string host, string port) { var server = new TinyWebServer.WebServer(request => { string path = request.Url.PathAndQuery.ToLower(); string json; if (path.Contains("?")) { string[] parts = path.Split('?'); path = parts[0]; } switch (path) { //GET: http://localhost:12345/mine case "/mine": return(blockchain.Mine()); //POST: http://localhost:12345/transactions/new //{ "Amount":1, "From":"x", "To":"y" } case "/transactions/new": if (request.HttpMethod != HttpMethod.Post.Method) { return ($"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}"); } json = new StreamReader(request.InputStream) .ReadToEnd(); Transaction trx = JsonConvert.DeserializeObject <Transaction>(json); int blockId = blockchain.CreateTransaction( trx.From, trx.To, trx.Amount); return ($"Twoja transakcja zostanie dodana do bloku {blockId}"); //GET: http://localhost:12345/blockchain case "/blockchain": return(blockchain.GetFullChain()); //POST: http://localhost:12345/nodes/register //{ "Url": "localhost:54321" } case "/nodes/register": if (request.HttpMethod != HttpMethod.Post.Method) { return ($"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}"); } json = new StreamReader(request.InputStream) .ReadToEnd(); var urlObject = new { Url = string.Empty }; var obj = JsonConvert.DeserializeAnonymousType( json, urlObject); return(blockchain.RegisterNode(obj.Url)); //GET: http://localhost:12345/nodes/resolve case "/nodes/resolve": return(blockchain.Consensus()); } return(""); }, $"http://{host}:{port}/mine/", $"http://{host}:{port}/transactions/new/", $"http://{host}:{port}/blockchain/", $"http://{host}:{port}/nodes/register/", $"http://{host}:{port}/nodes/resolve/"); server.Run(); }
public WebServer(BlockChain chain) { //start the web-server var settings = ConfigurationManager.AppSettings; string host = settings["host"]?.Length > 1 ? settings["host"] : "localhost"; string port = settings["port"]?.Length > 1 ? settings["port"] : "12345"; Logger.Log($"Web Service Listening on port: {port}"); var server = new TinyWebServer.WebServer(request => { string path = request.Url.PathAndQuery.ToLower(); string query = ""; string json = ""; Logger.Log($"Request:{path}"); if (path.Contains("?")) { string[] parts = path.Split('?'); path = parts[0]; query = parts[1]; } switch (path) { //GET: http://localhost:12345/mine case "/mine": return(chain.Mine(query)); //POST: http://localhost:12345/transactions/new //{ "Amount":123, "Recipient":"ebeabf5cc1d54abdbca5a8fe9493b479", "Sender":"31de2e0ef1cb4937830fcfd5d2b3b24f" } case "/transfer": if (request.HttpMethod != HttpMethod.Post.Method) { return($"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}"); } json = new StreamReader(request.InputStream).ReadToEnd(); Transaction trx = JsonConvert.DeserializeObject <Transaction>(json); try { int blockId = chain.CreateTransaction(trx, false); return($"Your transaction will be included in block {blockId}"); } catch (System.Exception ex) { return(ex.Message); } //GET: http://localhost:12345/chain case "/chain": return(chain.GetFullChain()); //POST: http://localhost:12345/nodes/register //{ "Urls": ["localhost:54321", "localhost:54345", "localhost:12321"] } case "/nodes/register": if (request.HttpMethod != HttpMethod.Post.Method) { return($"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}"); } json = new StreamReader(request.InputStream).ReadToEnd(); var urlList = new { Urls = new string[0] }; var obj = JsonConvert.DeserializeAnonymousType(json, urlList); return(chain.RegisterNodes(obj.Urls)); //GET: http://localhost:12345/nodes/resolve case "/nodes/resolve": return(chain.Consensus(false)); case "/balance": if (request.HttpMethod != HttpMethod.Post.Method) { return($"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}"); } json = new StreamReader(request.InputStream).ReadToEnd(); return(chain.Balance(json)); case "/history": List <string> history = chain.TransactionHistory(query); return(JsonConvert.SerializeObject(history)); case "/pending": return(JsonConvert.SerializeObject(chain.PendingTransactions())); case "/test/start": Logger.Log($"Test {query} Start ----------------------------------------"); return($"Test {query} Start"); case "/test/end": Logger.Log($"Test {query} End ------------------------------------------"); return($"Test {query} end"); case "/test/init": chain.Init(); return($"BlockChain initialized"); case "/test/checkpoint": return(chain.CheckPoint()); case "/test/rollback": return(chain.Rollback()); case "/test/miner/start": string[] cmdArgs = query.Split('&'); chain.Miner_Start(cmdArgs[0]); return("Miner started"); case "/test/miner/stop": chain.Miner_Stop(); return("Miner Stopped"); } return(""); }, $"http://{host}:{port}/mine/", $"http://{host}:{port}/transfer/", $"http://{host}:{port}/chain/", $"http://{host}:{port}/nodes/register/", $"http://{host}:{port}/nodes/resolve/", $"http://{host}:{port}/balance/", $"http://{host}:{port}/history/", $"http://{host}:{port}/pending/", $"http://{host}:{port}/test/init/", $"http://{host}:{port}/test/start/", $"http://{host}:{port}/test/end/", $"http://{host}:{port}/test/checkpoint/", $"http://{host}:{port}/test/rollback/", $"http://{host}:{port}/test/miner/start/", $"http://{host}:{port}/test/miner/stop/" ); server.Run(); }