Skip to content

Instantly share code, notes, and snippets.

View cyberpirate92's full-sized avatar
🖖
live long and prosper

Ravi Theja cyberpirate92

🖖
live long and prosper
View GitHub Profile
public void ConfigureServices(IServiceCollection services)
{
//...
services.AddSingleton<GithubService>();
}
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>();
@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>
@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>
using System.ComponentModel.DataAnnotations;
public class UsernameFormModel
{
[Required(AllowEmptyStrings = false)]
[MinLength(3)]
public string Username { get; set; }
}
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
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);
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;
}
}
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);
}
public class BlockChain {
public List<Block> chain;
private int difficulty;
public BlockChain(int difficulty) {
chain = new ArrayList<>();
this.difficulty =
difficulty;
}