From e9dbc9fa095fa48b199ffe0d02d7566b11490b48 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Mon, 21 Sep 2020 07:10:10 +0000 Subject: [PATCH] c# pow implementation work --- pow-csharp/onionrpow-cli/Program.cs | 2 +- pow-csharp/onionrpow/OnionrPow.cs | 74 ++++++++++++++------------- pow-csharp/onionrpow/onionrpow.csproj | 1 + pow-csharp/test.py | 1 + 4 files changed, 42 insertions(+), 36 deletions(-) create mode 100644 pow-csharp/test.py diff --git a/pow-csharp/onionrpow-cli/Program.cs b/pow-csharp/onionrpow-cli/Program.cs index 770b594a..9529fdda 100644 --- a/pow-csharp/onionrpow-cli/Program.cs +++ b/pow-csharp/onionrpow-cli/Program.cs @@ -21,7 +21,7 @@ namespace onionrpow_cli //stdout.Write(buffer, 0, bytes); data.AddRange(buffer); } - onionrpow.OnionrPow.compute(data.ToArray(), 3); + onionrpow.OnionrPow.compute(data.ToArray(), 1); } } } diff --git a/pow-csharp/onionrpow/OnionrPow.cs b/pow-csharp/onionrpow/OnionrPow.cs index 44ed14be..4dd6b8a2 100644 --- a/pow-csharp/onionrpow/OnionrPow.cs +++ b/pow-csharp/onionrpow/OnionrPow.cs @@ -1,55 +1,59 @@ using System; using System.Text; -using SHA3; using System.Linq; using System.Collections.Generic; +using Newtonsoft.Json; +using SHA3; namespace onionrpow { + + public class Meta{ + public string ch { get; set; } + public string type { get; set; } + } + + public class Block{ + public string meta { get; set; } + public string sig { get; set; } + public string signer { get; set; } + public int time; + + private List data; + + public void setData(List blockData){ + this.data = blockData; + } + + } public class OnionrPow { public static void compute(byte[] data, int difficulty){ using (var shaAlg = SHA3.Net.Sha3.Sha3256()) { - // Replace beginning json with counter + string stringData = Encoding.UTF8.GetString(data); + bool found = false; + var justData = new List(); + var metadataJson = new List(); int counter = 0; - var copy = new List(); - - foreach(byte co in data){ - if (counter < 1){ - counter += 1; - continue; - //copy.Add(Encoding.UTF8.GetBytes("{\"pow\": ")) + foreach(char c in stringData){ + if (found){ + justData.Add((byte) c); + } + else if (c == '\n'){ + for (int i = 0; i < counter + 1; i++){ + metadataJson.Add(data[i]); + } + found = true; + } + else{ + //Console.WriteLine(c.ToString()); } counter += 1; - copy.Add(co); } - int c = 0; - var copy2 = new List(); - while (true){ - toploop: - c += 1; - var num = Encoding.UTF8.GetBytes("{\"pow\": " + c.ToString() + ","); - copy2.Clear(); - copy2.AddRange(num); - copy2.AddRange(copy); - var hash = shaAlg.ComputeHash(copy2.ToArray()); + Block block = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(metadataJson.ToArray())); + block.setData(justData); - int counter2 = 0; - foreach(byte one in hash){ - if ((int) one != 0){ - goto toploop; - } - counter2 += 1; - if (counter2 == difficulty){ - break; - } - } - Console.WriteLine(Encoding.UTF8.GetString(copy2.ToArray())); - Console.WriteLine(BitConverter.ToString(hash)); - Console.WriteLine(c); - break; - } } } //b'{"meta":"{\\"ch\\":\\"global\\",\\"type\\":\\"brd\\"}","sig":"pR4qmKGGCdnyNyZRlhGfF9GC7bONCsEnY04lTfiVuTHexPJypOqmxe9iyDQQqdR+PB2gwWuNqGMs5O8\\/S\\/hsCA==","signer":"UO74AP5LGQFI7EJTN6NAVINIPU2XO2KA7CAS6KSWGWAY5XIB5SUA====","time":1600542238,"pow":300182}\nxcvxcvvxcxcv' diff --git a/pow-csharp/onionrpow/onionrpow.csproj b/pow-csharp/onionrpow/onionrpow.csproj index 22506a45..9b0aaa20 100644 --- a/pow-csharp/onionrpow/onionrpow.csproj +++ b/pow-csharp/onionrpow/onionrpow.csproj @@ -5,6 +5,7 @@ + diff --git a/pow-csharp/test.py b/pow-csharp/test.py new file mode 100644 index 00000000..73315bd5 --- /dev/null +++ b/pow-csharp/test.py @@ -0,0 +1 @@ +print('{"meta":"{\\"ch\\":\\"global\\",\\"type\\":\\"brd\\"}","sig":"pR4qmKGGCdnyNyZRlhGfF9GC7bONCsEnY04lTfiVuTHexPJypOqmxe9iyDQQqdR+PB2gwWuNqGMs5O8\\/S\\/hsCA==","signer":"UO74AP5LGQFI7EJTN6NAVINIPU2XO2KA7CAS6KSWGWAY5XIB5SUA====","time":1600542238,"pow":300182}\nxcvxcvvxcxcv')