1

I'm running this code:

import React, { Component } from "react";
import logo from "./logo.svg";
import "./App.css";
import sotez from "sotez";

class App extends Component {
  state = {
    pkh: "",
    mnemonic: "",
    sk: ""
  };

  async componentDidMount() {
    await this.main();
  }

  main = async () => {
    const password = "yanterzzz";
    await sotez.node
      .query("/chains/main/blocks/head")
      .then(response => console.log(response));
    const result = await sotez.crypto.generateMnemonic();
    const answer = await sotez.crypto.generateKeys(result, password);
    console.log(answer.mnemonic);
    this.setState({ mnemonic: answer.mnemonic });
    this.setState({ sk: answer.sk });
    this.setState({ pkh: answer.pkh });
  };
}

and it works for a while and generates keys properly, but then eventually I'll end up running into this error:

Unhandled Rejection (TypeError): library.sodium.crypto_sign_seed_keypair is not a function

Would my issue be with React and component reloading or a problem with the libsodium library?

Tholle
  • 108,070
  • 19
  • 198
  • 189
new2leGame
  • 37
  • 7
  • Don't know for sure, but I guess this is an error in the library. What *very likely* happens is that your random number generator has been called for a certain number of times or bytes and decides that it needs to request additional entropy from the operating system. When it does it finds out that `crypto_sign_seed_keypair` is not a function (it may be a property?), and it therefore fails. This could be a programming bug but it could be a library mismatch / configuration issue as well. – Maarten Bodewes Aug 07 '18 at 13:05
  • I'd check the library versions first because the org. program *did probably compile*. That said, they could have compiled it against a wrong version and forgot to check a newer version or something similar. This particular error will not popup during testing unless a lot of randomness was required or if it was explicitly tested. – Maarten Bodewes Aug 07 '18 at 13:10
  • Ahhhh okay. I've solved the issue by only generating keys onClick, so I'm assuming it has to do with the program not generating enough entropy on load and having to wait a second or two to do so. – new2leGame Aug 07 '18 at 22:18
  • and/or I could be massively overblowing that ^ and it was simply because I updated the package as well LOL – new2leGame Aug 07 '18 at 22:28
  • It is probably a difference between entropy being seeded automatically vs through a blocking call when you need it. *And* it can have to do with the package update. I cannot tell either of them to be true from here though. – Maarten Bodewes Aug 07 '18 at 22:51

1 Answers1

0

So what is likely happening is that the pseudo random number generator that is used for generateKeys requires seeding from an entropy pool. Generally this pool is updated with information automatically when it becomes available. The RNG will only output a specific amount of data before it decides reseeding is necessary.

Instead of blocking and waiting for the automatic reseeding to take place it will make an explicit call to retrieve the entropy as soon as possible. However, this specific call is not available within the runtime and at that time the program will crash with the given error. Generally the error is never generated unless the random values are retrieved in a tight loop.

Key pair generation requires at least as much high quality random data as the key size, so it is one of the known reasons that cause a system to run out of entropy.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263