public void AddFiftyBlocksAndCheckRangeRetrieval() { foreach (IChainStorageProvider store in stores) { ResetStore(store); var Node = new BlockChainNode(new System.Security.Cryptography.SHA512Managed()); Node.Initiate(null, store); for (int i = 0; i < 50; i++) { Node.SubmitData($"Block number {i+1}"); } List <Block> blockList = Node.RetrieveMany(22, 5); Assert.Equal(5, blockList.Count); // the reason we expect this to be block with data "21" // is because of the additional geness block for (int i = 0; i <= 4; i++) { Assert.Equal($"Block number 2{i+1}", blockList[i].Data); } } }
private string GenerateDectriptKey(BlockChainNode nodetoadd, byte[] publicKey) { var keyBytes = this.cryptoService.EncodeKey(ASCIIEncoding.ASCII.GetBytes(nodetoadd.Hash), publicKey); // id of the record var keyStr = Base32.ToBase32String(keyBytes); return(keyStr); }
public string HttpAction(BlockChainNode Node, String data) { Console.WriteLine("Http host has received a request to count chain size."); int i = Node.GetBlockChain().Count; return(i.ToString()); }
public BlockChainNode FromJSonString(string text) { var obj = JObject.Parse(text); BlockChainNode bc = new BlockChainNode(); bc.Data = (JObject)obj.DeepClone();//othewise reactive field history override history obj["_history"]?.ToObject <List <string> >().ForEach((x) => { bc.History.Add(x.ToLower()); }); return(bc); }
public void CRUD() { var domain = "dns-blockchain.io"; LookupClient lookupClient = new LookupClient(IPAddress.Parse("127.0.0.1"), 53); ICryptoService cryptoService = new CryptoService(); IBlockChainService service = new BlockChainService(cli, lookupClient, cryptoService); // private for reader, public for writer var publicKey = File.ReadAllBytes("./public.txt"); var privateKey = File.ReadAllBytes("./private.txt"); this.OutputHelper.WriteLine($"public key: {publicKey}"); this.OutputHelper.WriteLine($"private key: {privateKey}"); var data = new JObject(); data["date"] = DateTime.Now.ToLongDateString(); data["text"] = Guid.NewGuid(); data["message"] = "I love you"; this.OutputHelper.WriteLine($"object: {data.ToString()}"); var node = new BlockChainNode(); node.Data = data; this.OutputHelper.WriteLine($"Node Hash: {node.Hash}"); //node.TokenMap.ToList().ForEach((x) => { Console.WriteLine(x.Key + " | " + x.Value); }); this.OutputHelper.WriteLine($"Domain: {domain}"); this.OutputHelper.WriteLine($"Public Key: {publicKey}"); service.Add(node, 1, domain, publicKey); Thread.Sleep(3000); var node2 = service.Get(node.Hash, 1, domain, privateKey); this.OutputHelper.WriteLine($"Result Hash: {node2.Hash}"); this.OutputHelper.WriteLine($"Result Data: {node2.Data.ToString()}"); var node3 = service.New(node2, publicKey, data); node3.Data["message"] = "Second Item in flow"; service.Add(node3, 1, domain, publicKey); timer.Start(); var node4 = service.Get(node3.Hash, 1, domain, privateKey); timer.Stop(); Console.WriteLine($"timing {timer.ElapsedMilliseconds}ms"); var emptylist = service.GetAncerstor(node2, 1, domain, privateKey); var parentList = service.GetAncerstor(node4, 1, domain, privateKey); var errors = service.Validate(node4.Data, node4.Hash, 1, domain, privateKey); }
static void Main(string[] args) { var Node = new BlockChainNode(); Node.Initiate(); var HttpHost = new BlockChainNodeHttpHost.HttpHost(Node); HttpHost.Start(); Console.WriteLine("Server running, press any key to quit"); Console.ReadLine(); }
public BlockChainNode New(BlockChainNode node2, byte[] publicKey, JObject data = null) { BlockChainNode newnode = new BlockChainNode(); if (data != null) { newnode.Data = data; } newnode.History.Add(node2.Hash); return(newnode); }
public void BlockChainNodeTests() { foreach (IChainStorageProvider store in stores) { ResetStore(store); var Node = new BlockChainNode(); Node.Initiate(null, store); Node.SubmitData("This is my first block of data! :-)"); Assert.Equal(2, Node.GetChainSize()); // (2 may seem unintuative but this includes the genesis block created as part of initiation } }
public void Add(BlockChainNode nodetoadd, int db, string domain, byte[] publicKey) { var password = SHA512.Create().ComputeHash(Guid.NewGuid().ToByteArray()); WriteLog($"password: {Convert.ToBase64String(password)}"); var decriptkey = this.cryptoService.EncodeKey(password, publicKey); var key64 = Convert.ToBase64String(decriptkey); WriteLog($"key64: {key64}"); WriteLog($"node 64: { nodetoadd.ToBase64()}"); var dataToEncode = UnicodeEncoding.Unicode.GetBytes(nodetoadd.Data.ToString(Formatting.None)); WriteLog($"node 64: { dataToEncode.Length}"); WriteLog($"datatoencode: {Convert.ToBase64String(dataToEncode)}"); WriteLogByteArray("decript key", password); WriteLogByteArray("decript key encoded", decriptkey); var encoded = this.cryptoService.EncodeData(dataToEncode, password); WriteLogByteArray($"encoded data:", encoded); WriteLog($"encoded: {encoded.Length}"); WriteLog($"encoded 64: {Convert.ToBase64String(encoded)}"); WriteLogByteArray("data encoded", encoded); var data64 = Convert.ToBase64String(encoded); //TODO is neede the double base 64 encoding? WriteLog($"data64 encoded: {data64}"); var fullObj = new JObject(); fullObj["key"] = key64; fullObj["data"] = data64; var fullObj64 = Convert.ToBase64String(UnicodeEncoding.Unicode.GetBytes(fullObj.ToString(Formatting.None))); WriteLog($"fullObj64: {fullObj64}"); var itemUrl = ComputeRecordUrl(db, nodetoadd.Hash, domain); var keyDomain = lookup.QueryAsync(itemUrl, QueryType.TXT).Result; //token count if (keyDomain == null || keyDomain.Questions.Count == 0) { client.AddRecord(new DNSEntry() { Domain = itemUrl, Value = fullObj64.Length.ToString() }); } WriteDNSFragmentedText(itemUrl, fullObj64, 254); }
public List <BlockChainNode> GetAncerstor(BlockChainNode node, int db, string domain, byte[] privateKey) { var result = new List <BlockChainNode>(); var anchestors = node.History; for (int i = 0; i < anchestors.Count; i++) { var parent = this.Get(anchestors[i], db, domain, privateKey); result.Add(parent); } return(result); }
public void CreateHostAndCheckResponsive() { var Node = new BlockChainNode(); Node.Initiate(); var HttpHost = new BlockChainNodeHttpHost.HttpHost(Node); HttpHost.Start(); using (var client = new HttpClient()) { var response = client.PostAsync("http://localhost:8080/count/", null).Result; Assert.Equal(true, response.IsSuccessStatusCode); } }
public void GetBlockCountWhenChainNew() { var Node = new BlockChainNode(); Node.Initiate(); var HttpHost = new BlockChainNodeHttpHost.HttpHost(Node); HttpHost.Start(); using (var client = new HttpClient()) { var response = client.PostAsync("http://localhost:8080/count/", null).Result; var contents = response.Content.ReadAsStringAsync().Result; Assert.Equal("1", contents); } }
public void InitiateAndAddOneThousandBlocks() { foreach (IChainStorageProvider store in stores) { ResetStore(store); var Node = new BlockChainNode(new System.Security.Cryptography.SHA512Managed()); Node.Initiate(null, store); for (int i = 1; i < 1001; i++) { Node.SubmitData($"Block number {i}"); } Assert.Equal(1001, Node.GetChainSize()); } }
public void InitiateAndAddFiftyBlocksAndValidate() { foreach (IChainStorageProvider store in stores) { ResetStore(store); var Node = new BlockChainNode(); Node.Initiate(null, store); for (int i = 1; i < 51; i++) { Node.SubmitData($"Block number {i}"); } Assert.Equal(true, Node.ValidateNode()); } }
public string HttpAction(BlockChainNode Node, String data) { Console.WriteLine($"Http host has received a request get block number {data}"); int BlockNumber; if (int.TryParse(data, out BlockNumber) != true) { return("Invalid block number. Block number must be integer"); } if (Node.GetBlockChain().Count < BlockNumber) { return("Invalid Block. Block number doesn't exist"); } return(Node.GetBlockChain()[BlockNumber].Data); }
public void SubmitValidData() { var Node = new BlockChainNode(); Node.Initiate(); var HttpHost = new BlockChainNodeHttpHost.HttpHost(Node); HttpHost.Start(); using (var client = new HttpClient()) { var myContent = "TestBlockData"; var buffer = System.Text.Encoding.UTF8.GetBytes(myContent); var byteContent = new ByteArrayContent(buffer); var response = client.PostAsync("http://localhost:8080/add/", byteContent).Result; var contents = response.Content.ReadAsStringAsync().Result; Assert.Equal("ADDED|2", contents); } }
public void SubmitInvalidData() { var Node = new BlockChainNode(); Node.Initiate(); var HttpHost = new BlockChainNodeHttpHost.HttpHost(Node); HttpHost.Start(); using (var client = new HttpClient()) { var myContent = ""; var buffer = System.Text.Encoding.UTF8.GetBytes(myContent); var byteContent = new ByteArrayContent(buffer); var response = client.PostAsync("http://localhost:8080/add/", byteContent).Result; var contents = response.Content.ReadAsStringAsync().Result; Assert.Equal("No data found, cannot add block with no data, that's pointless!", contents); } }
public string HttpAction(BlockChainNode Node, String data) { Console.WriteLine("Http host has received a request to add data to the block chain."); if (String.IsNullOrEmpty(data)) { return("No data found, cannot add block with no data, that's pointless!"); } long i = Node.SubmitData(data); if (i <= 0) { return("Data not added"); } else { return($"ADDED|{i}"); } }
public void InitiateAndAddOneHundredBlocksAndPersist() { foreach (IChainStorageProvider store in stores) { ResetStore(store); var Node = new BlockChainNode(new System.Security.Cryptography.SHA512Managed()); Node.Initiate(null, store); for (int i = 1; i < 101; i++) { Node.SubmitData($"Block number {i}"); } var FileName = DateTime.Now.Ticks.ToString(); Node.Persist(FileName); Assert.Equal(true, System.IO.File.Exists(FileName)); } }
public void AddFiftyBlocksAndCheckIndividualRetrieval() { foreach (IChainStorageProvider store in stores) { ResetStore(store); var Node = new BlockChainNode(new System.Security.Cryptography.SHA512Managed()); Node.Initiate(null, store); for (int i = 0; i < 50; i++) { Node.SubmitData($"Block number {i+1}"); } Block block = Node.RetrieveSingleBlock(22); // the reason we expect this to be block with data "21" // is because of the additional geness block Assert.Equal("Block number 21", block.Data); } }
public void SubmitNewBlocksAndGetCount() { var Node = new BlockChainNode(); Node.Initiate(); var HttpHost = new BlockChainNodeHttpHost.HttpHost(Node); HttpHost.Start(); using (var client = new HttpClient()) { var myContent = "TestBlockData"; var buffer = System.Text.Encoding.UTF8.GetBytes(myContent); var byteContent = new ByteArrayContent(buffer); for (int i = 0; i < 10; i++) { client.PostAsync("http://localhost:8080/add/", byteContent).Wait(); } var response = client.PostAsync("http://localhost:8080/count/", null).Result; var contents = response.Content.ReadAsStringAsync().Result; Assert.Equal("11", contents); } }
/// <summary> /// Validates the given node against all other connected nodes, if /// provided chain matches at least 80% of connected chains then /// the node is considered valid /// </summary> /// <param name="Node"></param> /// <returns></returns> public bool ValidateNode(BlockChainNode Node) { //todo: iterate through blocks in chain, calculate each hash afresh and check that they match the actual held chain return(true); }
public HttpHost(SimpleBlockChain.BlockChainNode Node) { _BlockChainNode = Node; AddHttpFunctions(); }
public void RegisterNode(BlockChainNode blockChainNode) { this.MyNode = blockChainNode; }