0

I am able to get the randombytes using react-native-crypto-js const words = CryptoJS.lib.WordArray.random(32); Is that possible to generate mnemonics using random bytes.

0

good question and we wish it'd be that easy but this is unfortunately not possible. Here what you are looking for is basically a cryptographic function that, given an arbitrary input, would provide a set of 12 random valid English words. To my knowledge this does not exist as a native function defined only by maths and crypto, as that function would have to know about the whole English dictionary so that it only spits out valid words.

Basically you are thus looking for a mathematical relationship between an quasi-inifinite set of bytes arrays and a finite set of words combination with strong constraint on acceptable words. The strong disconnection between these domains makes it so that it is very unlikely that such a function would exist in a library of cryptographic functions. Hope this helps!

  • Thanks thomas for your reply. I am looking for generating the mnemonics there are some libraries but none of them helped. – Trinu Oct 28 '18 at 11:24
0

You can do easily the opposite: choose a mnemonic, pack it, hash it and enjoy your pseudorandom byte string!

  • Inorder to choose a mnemonic first I want to generate it. – Trinu Oct 28 '18 at 11:23
  • That is not possible if not fixing rules. And implementing them. If I want to fly with my boots I need magic boots. We have no magic boots in coding ;-) – Rick Park Oct 28 '18 at 12:02
  • Its possible i did it. Please check my answer. – Trinu Oct 28 '18 at 18:55
  • It is possible, but fixing rules. You did it. For instance you load a limited vocabulary. – Rick Park Oct 28 '18 at 19:02
  • ya copied vocabulary from bip39 library that is standard – Trinu Oct 28 '18 at 19:13
0

Finally able to generate mnemonics from randombytes in reactnative using this.

const CryptoJS = require("react-native-crypto-js");
 const words = CryptoJS.lib.WordArray.random(16);
const hexStr = bytesToHex(words.words);
  const byteArr = hexStringToByte(hexStr);


 // const wordlist = words from bip39 js;

const mnemonics = entropyToMnemonic(byteArr, wordlist) //12 word mnemonics here.



  function entropyToMnemonic (entropy, wordlist) {
  if (!Buffer.isBuffer(entropy)) entropy = Buffer.from(entropy, 'hex')
  wordlist = wordlist || DEFAULT_WORDLIST

  // 128 <= ENT <= 256
  if (entropy.length < 16) throw new TypeError(INVALID_ENTROPY)
  if (entropy.length > 32) throw new TypeError(INVALID_ENTROPY)
  if (entropy.length % 4 !== 0) throw new TypeError(INVALID_ENTROPY)

  var entropyBits = bytesToBinary([].slice.call(entropy))
  var checksumBits = deriveChecksumBits(entropy)

  var bits = entropyBits + checksumBits
  var chunks = bits.match(/(.{1,11})/g)
  var words = chunks.map(function (binary) {
    var index = binaryToByte(binary)
    return wordlist[index]
  })

  return words.join(' ')
}

    // Convert a byte array to a hex string
    function bytesToHex(bytes) {
      for (var hex = [], i = 0; i < bytes.length; i++) {
        hex.push((bytes[i] >>> 4).toString(16));
        hex.push((bytes[i] & 0xF).toString(16));
      }
      return hex.join("");
    }
function hexStringToByte(str) {
  if (!str) {
    return new Uint8Array();
  }

  var a = [];
  for (var i = 0, len = str.length; i < len; i += 2) {
    a.push(parseInt(str.substr(i, 2), 16));
  }

  return new Uint8Array(a);
}

function deriveChecksumBits (entropyBuffer) {
 var ENT = entropyBuffer.length * 8
 var CS = ENT / 32
 var hash = createHash('sha256').update(entropyBuffer).digest()

return bytesToBinary([].slice.call(hash)).slice(0, CS)

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.