Skip to content

Instantly share code, notes, and snippets.

@Groxan
Last active May 16, 2019 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Groxan/c0f11a896bcf9a43e0fff9ba2e46223b to your computer and use it in GitHub Desktop.
Save Groxan/c0f11a896bcf9a43e0fff9ba2e46223b to your computer and use it in GitHub Desktop.
Example of generation of a random seed from the revealed nonces in the Tezos blockchain
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Isopoh.Cryptography.Blake2b;
using Newtonsoft.Json.Linq;
using Netezos.Rpc;
namespace TezosRandomSeed
{
public static class TezosRandomSeed
{
public static async Task Test()
{
using (var rpc = new TezosRpc("https://mainnet.tezrpc.me"))
{
var nonces98 = await rpc
.Blocks[409599]
.Context
.Raw
.Cycles[98]
.Nonces
.GetAsync(1);
var seed104 = (await rpc
.Blocks[430081]
.Context
.Raw
.Cycles[104]
.RandomSeed
.GetAsync())
.Value<string>();
var seed105 = GetRandomSeed(
seed104,
nonces98
.Where(x => x[1] is JValue)
.Select(x => x[1].Value<string>()));
//"3a7c01910c7ad92bb7712733822c35c893077c105e0d05cdfa312eea2bfd3412"
}
}
public static void Test2()
{
var seed0 = B2B("");
var seed1 = GetRandomSeed(seed0, Enumerable.Empty<string>());
var seed2 = GetRandomSeed(seed1, Enumerable.Empty<string>());
}
public static string GetRandomSeed(string prevSeed, IEnumerable<string> nonces)
{
var seed = B2B(prevSeed + "0000000000000000000000000000000000000000000000000000000000000000");
foreach (var nonce in nonces) seed = B2B(seed + nonce);
return seed;
}
static string B2B(string data)
{
var bytes = Blake2B.ComputeHash(
Enumerable.Range(0, data.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(data.Substring(x, 2), 16))
.ToArray(),
new Blake2BConfig { OutputSizeInBytes = 32 },
null);
return BitConverter.ToString(bytes).Replace("-", "").ToLower();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment