public BlockChain Build() { PrincipleId.VerifyNotEmpty(nameof(PrincipleId)); DataBlock genesisBlock = DataBlockBuilder.CreateGenesisBlock(PrincipleId); return(new BlockChain() .Add(genesisBlock)); }
public void GivenBlockData_WhenValuesSet_VerifyNoChangeAndSignature() { const string issuer = "*****@*****.**"; var now = DateTime.UtcNow; IPrincipalSignature principleSignature = new PrincipalSignature(issuer, issuer, "test.com"); var dataPayload = new { Name = "Name", Type = "Type", Author = "Author", Data = "Data" }; string payloadJson = dataPayload.ToJson(); DataBlock data = new DataBlockBuilder() .SetTimeStamp(now) .SetBlockType("blockType") .SetBlockId("blockId") .SetData(payloadJson) .SetPrincipleId(issuer) .Build(); data = data with { JwtSignature = principleSignature.Sign(data.Digest) }; principleSignature.ValidateSignature(data.JwtSignature); string json = data.ToJson(); DataBlock received = Json.Default.Deserialize <DataBlock>(json).VerifyNotNull("Json is null"); data.TimeStamp.Should().Be(now.ToUnixDate().TimeStamp); received.BlockType.Should().Be("blockType"); received.BlockId.Should().Be("blockId"); received.Data.Should().Be(payloadJson); received.JwtSignature.Should().Be(data.JwtSignature); received.Digest.Should().Be(received.GetDigest()); Dictionary <string, string>?readData = Json.Default.Deserialize <Dictionary <string, string> >(data.Data) !; readData["name"].Should().Be(dataPayload.Name); readData["type"].Should().Be(dataPayload.Type); readData["author"].Should().Be(dataPayload.Author); readData["data"].Should().Be(dataPayload.Data); }
public static BlockChain Add <T>(this BlockChain blockChain, T value, string principleId) { blockChain.VerifyNotNull(nameof(blockChain)); value.VerifyNotNull(nameof(value)); principleId.VerifyNotEmpty(nameof(principleId)); blockChain += new DataBlockBuilder() .SetTimeStamp(DateTime.UtcNow) .SetBlockType(value.GetType().Name) .SetBlockId(blockChain.Blocks.Count.ToString()) .SetData(value.ToJson()) .SetPrincipleId(principleId) .Build(); return(blockChain); }
public void GivenBlockChain_AppendSingleNode_ShouldVerify() { const string issuer = "*****@*****.**"; var now = DateTime.UtcNow; IPrincipalSignature principleSignature = new PrincipalSignature(issuer, issuer, "*****@*****.**"); var dataPayload = new { Name = "Name", Type = "Type", Author = "Author", Data = "Data" }; string payloadJson = dataPayload.ToJson(); DataBlock data = new DataBlockBuilder() .SetTimeStamp(now) .SetBlockType("blockType") .SetBlockId("blockId") .SetData(payloadJson) .SetPrincipleId(issuer) .Build(); BlockChain blockChain = new BlockChainBuilder() .SetPrincipleId(issuer) .Build() .Add(data) .Sign(x => principleSignature); blockChain.Validate(x => principleSignature); // Get payload of data block blockChain.Blocks.Count.Should().Be(2); DataBlock receiveBlock = blockChain.Blocks[1].DataBlock; TestBlockNode(receiveBlock, "blockType", "blockId"); Dictionary <string, string> receivedPayload = receiveBlock.Data.ToObject <Dictionary <string, string> >().VerifyNotNull("payload failed"); (receivedPayload["name"] == "Name").Should().BeTrue(); (receivedPayload["type"] == "Type").Should().BeTrue(); (receivedPayload["author"] == "Author").Should().BeTrue(); (receivedPayload["data"] == "Data").Should().BeTrue(); }
public void GivenBlockData_WhenTestForEqual_ShouldPass() { const string issuer = "*****@*****.**"; var now = DateTime.UtcNow; IPrincipalSignature principleSignature = new PrincipalSignature(issuer, issuer, "*****@*****.**"); var dataPayload = new { Name = "Name", Type = "Type", Author = "Author", Data = "Data" }; string payloadJson = dataPayload.ToJson(); DataBlock data1 = new DataBlockBuilder() .SetTimeStamp(now) .SetBlockType("blockType") .SetBlockId("blockId") .SetData(payloadJson) .SetPrincipleId(issuer) .Build(); DataBlock data2 = new DataBlockBuilder() .SetTimeStamp(now) .SetBlockType("blockType") .SetBlockId("blockId") .SetData(payloadJson) .SetPrincipleId(issuer) .Build(); data1.TimeStamp.Should().Be(data2.TimeStamp); data1.BlockType.Should().Be(data2.BlockType); data1.BlockId.Should().Be(data2.BlockId); data1.Data.Should().Be(data2.Data); data1.Digest.Should().Be(data2.Digest); (data1 == data2).Should().BeTrue(); // Will fail because of timestamp on JwtSignature }
public void ProcessTag(ITag bitmapTag) { // I don't feel like writing structure definitions, poaching extraction code // #TODO: Wrap all of this up under a neat function for other uses bitmapTagData = null; processedPages.Clear(); string groupName = VariousFunctions.SterilizeTagGroupName(CharConstant.ToString(bitmapTag.Group.Magic)).Trim(); string pluginPath = string.Format("{0}\\{1}\\{2}.xml", VariousFunctions.GetApplicationLocation() + @"Plugins", _buildInfo.Settings.GetSetting <string>("plugins"), groupName); if (!File.Exists(pluginPath) && _buildInfo.Settings.PathExists("fallbackPlugins")) { pluginPath = string.Format("{0}\\{1}\\{2}.xml", VariousFunctions.GetApplicationLocation() + @"Plugins", _buildInfo.Settings.GetSetting <string>("fallbackPlugins"), groupName); } if (pluginPath == null || !File.Exists(pluginPath)) { StatusUpdater.Update("Plugin doesn't exist for 'bitmap', yet somehow you managed to get to the bitmap tab..."); return; } using (IReader reader = _streamManager.OpenRead()) { bitmapTagData = new DataBlockBuilder(reader, bitmapTag, _cache, _buildInfo); using (XmlReader pluginReader = XmlReader.Create(pluginPath)) { AssemblyPluginLoader.LoadPlugin(pluginReader, bitmapTagData); } } bool multiResouceMagic = false; // debugging 8k textures spread across space and time // Resource check if (bitmapTagData.ReferencedResources.Count == 0) { StatusUpdater.Update("Unable to find any resources in the current tag to get bitmap data from!"); return; } else { if (bitmapTagData.ReferencedResources.Count > 1) { multiResouceMagic = true; } if (_cache.Resources != null) // mandrill compiled debugging cache { using (IReader reader = _streamManager.OpenRead()) { resourceTable = _cache.Resources.LoadResourceTable(reader); } } else { StatusUpdater.Update("Unable to find any resources in the cache file at all! Failed to display bitmap."); return; } } // Grab all the raw pages required foreach (DatumIndex resourceDatum in bitmapTagData.ReferencedResources) { Resource currentResource = resourceTable.Resources[resourceDatum.Index]; if (currentResource.Location == null) { StatusUpdater.Update("A bitmap resource had a null location, bad doo doo! Fix yo compiler nerd."); return; } foreach (ResourcePage currentPage in currentResource.Location.PagesToArray()) { if (currentPage == null) { continue; } using (FileStream fileStream = File.OpenRead(_cacheLocation)) { ThirdGenCacheFile resourceFile = (ThirdGenCacheFile)_cache; Stream resourceStream = fileStream; if (currentPage.FilePath != null) // Mandrill compiles everything into a single cache { ResourceCacheInfo resourceCacheInfo = App.AssemblyStorage.AssemblySettings.HalomapResourceCachePaths.FirstOrDefault(r => r.EngineName == _buildInfo.Name); string resourceCachePath = (resourceCacheInfo != null && resourceCacheInfo.ResourceCachePath != "") ? resourceCacheInfo.ResourceCachePath : Path.GetDirectoryName(_cacheLocation); resourceCachePath = Path.Combine(resourceCachePath ?? "", Path.GetFileName(currentPage.FilePath)); if (!File.Exists(resourceCachePath)) { StatusUpdater.Update("Bitmap exists outside of the local cache, was unable to find this cache: " + Path.GetFileName(resourceCachePath)); return; } resourceStream = File.OpenRead(resourceCachePath); resourceFile = new ThirdGenCacheFile(new EndianReader(resourceStream, _cache.Endianness), _buildInfo, Path.GetFileName(_cacheLocation), _cache.BuildString); } byte[] pageData; ResourcePageExtractor pageExtractor = new ResourcePageExtractor(resourceFile); using (MemoryStream pageStream = new MemoryStream()) { pageExtractor.ExtractPage(currentPage, resourceStream, pageStream); pageData = new byte[pageStream.Length]; Buffer.BlockCopy(pageStream.GetBuffer(), 0, pageData, 0, (int)pageStream.Length); } processedPages.Add(pageData); // Store the page for use } } } }