Skip to content

Instantly share code, notes, and snippets.

@Hribek25
Last active September 24, 2019 10:30
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 Hribek25/691daf0a2b808cfba7963db0d80f7db9 to your computer and use it in GitHub Desktop.
Save Hribek25/691daf0a2b808cfba7963db0d80f7db9 to your computer and use it in GitHub Desktop.
Generating a seed: a general approach
// The snippet is a part of the IOTA Developer Essentials project. You can reach it at https://hribek25.github.io/IOTA101/
// Complete description and story behind the snippet is available at: https://hribek25.github.io/IOTA101/Allchapters_javascript.ipynb.html#67D98D069B61
//based on https://gist.github.com/SteveFromTheOffice/c8448a09352337386f135a16bbb20d93
//modified by Petr Zizka
var GenerateSeed = function () {
const length = 81; // The length of the seed and int array.
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ9"; // The allowed characters in the seed.
var result = new Array(length); // An empty array to store the seed characters.
var randomValues = Buffer.alloc(length)
// Generate random values and store them to array.
crypto.randomFillSync(randomValues);
var cursor = 0; // A cursor is introduced to remove modulus bias.
for (var i = 0; i < randomValues.length; i++) { // Loop through each of the 81 random values.
cursor += randomValues[i]; // Add them to the cursor.
result[i] = chars[cursor % chars.length]; // Assign a new character to the seed based on cursor mod 27.
}
return result.join(''); // Merge the array into a single string and return it.
};
var NewSeed = GenerateSeed();
console.log(NewSeed);
console.log("Length: %s", NewSeed.length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment