This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
//... | |
services.AddSingleton<GithubService>(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GithubService { | |
private IHttpClientFactory httpClientFactory; | |
private static string GITHUB_API_URL = "https://api.github.com"; | |
public GithubService(IHttpClientFactory clientFactory) { | |
httpClientFactory = clientFactory; | |
} | |
public async Task<List<GithubRepository>> FetchUserRepositories(string username) { | |
var repositories = new List<GithubRepository>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@page "/" | |
@using TodoListApp.Data | |
@inject IJSRuntime JsRuntime; | |
<EditForm Model="@usernameFormModel" OnValidSubmit="@HandleValidSubmit"> | |
<DataAnnotationsValidator /> | |
<ValidationSummary /> | |
<InputText id="name" bind-Value="@usernameFormModel.Username" /> | |
<button type="submit">Fetch Repositories</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@page "/" | |
@using TodoListApp.Data | |
<EditForm Model="@usernameFormModel" OnValidSubmit="@HandleValidSubmit"> | |
<DataAnnotationsValidator /> | |
<ValidationSummary /> | |
<InputText id="name" bind-Value="@usernameFormModel.Username" /> | |
<button type="submit">Fetch Repositories</button> | |
</EditForm> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel.DataAnnotations; | |
public class UsernameFormModel | |
{ | |
[Required(AllowEmptyStrings = false)] | |
[MinLength(3)] | |
public string Username { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Previous Block's hash Timestamp Nonce Transaction Data | |
5FECEB66FFC86F38D952786C6D696C79C2DBC239DD4E91B46729D73A27FB57E9 1527709815253 511316 Hello | |
00000BE462BE07F21AC57939DC46310322F8ACF170B1C16CC03E3DCC431A59BE 1527709815253 1362348 World | |
00000E3D3CFFAEE5E8360EB8E7FF8AB39F270D39BCD5830A8505974632B98DC4 1527709815253 792204 Whatever | |
Is valid chain : true | |
Is valid chain : false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
try { | |
var difficulty = 5; | |
var blockChain = new BlockChain(difficulty); | |
Block block1 = new Block("Hello"); | |
Block block2 = new Block("World"); | |
Block block3 = new Block("Whatever"); | |
blockChain.addBlock(block1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean isValid() throws NoSuchAlgorithmException { | |
var pre = ""; | |
for(;pre.length() < difficulty; pre += "0"); | |
for (var i=1; i < chain.size(); ++i) { | |
var previousBlock = chain.get(i-1); | |
var currentBlock = chain.get(i); | |
if (!currentBlock.getHash().startsWith(pre) || !currentBlock.previousBlockHash.equals(previousBlock.getHash())) { | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void addBlock(Block block) throws NoSuchAlgorithmException { | |
block.previousBlockHash = chain.size() == 0 ? CryptoHelper.sha256("0") : chain.get(chain.size()-1).getHash(); | |
// before adding a block to the blockchain | |
// mine block for the magic nonce value | |
block.mine(difficulty); | |
chain.add(block); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BlockChain { | |
public List<Block> chain; | |
private int difficulty; | |
public BlockChain(int difficulty) { | |
chain = new ArrayList<>(); | |
this.difficulty = | |
difficulty; | |
} |
NewerOlder