c# pow implementation work

master
Kevin Froman 2020-09-21 07:10:10 +00:00
parent 499981ed52
commit e9dbc9fa09
4 changed files with 42 additions and 36 deletions

View File

@ -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);
}
}
}

View File

@ -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<byte> data;
public void setData(List<byte> 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<byte>();
var metadataJson = new List<byte>();
int counter = 0;
var copy = new List<byte>();
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<byte>();
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<Block>(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'

View File

@ -5,6 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="SHA3.Net" Version="1.2.3" />
</ItemGroup>

1
pow-csharp/test.py Normal file
View File

@ -0,0 +1 @@
print('{"meta":"{\\"ch\\":\\"global\\",\\"type\\":\\"brd\\"}","sig":"pR4qmKGGCdnyNyZRlhGfF9GC7bONCsEnY04lTfiVuTHexPJypOqmxe9iyDQQqdR+PB2gwWuNqGMs5O8\\/S\\/hsCA==","signer":"UO74AP5LGQFI7EJTN6NAVINIPU2XO2KA7CAS6KSWGWAY5XIB5SUA====","time":1600542238,"pow":300182}\nxcvxcvvxcxcv')