0

I'm creating my monero wallets using the monero-javascript javascript library and this is the code I use

async function createWalletFull(password, mnemonic, id, network) {
  try {
    let wallet
    if (network == "TESTNET") {
      wallet = await monerojs.createWalletFull({
        networkType: MoneroNetworkType.TESTNET,
        password: password,
        mnemonic: mnemonic,
        serverUri: "http://127.0.0.1:28081",
        path: <path>
      });
    } else {
      wallet = await monerojs.createWalletFull({
        networkType: MoneroNetworkType.MAINNET,
        password: password,
        mnemonic: mnemonic,
        serverUri: "http://127.0.0.1:28081",
        path: <path>
      });
    }
    
    return wallet
    
  } catch (error) {
    throw new Error("Error during creation of the wallet: " + error.message);
  }
}

async function createAddressMonero(coin, path, child, network, id_utente,index=0) {
  const privateKey = child.privateKey.toString("hex");
  
  let wallet;
  const mnemonic = mnemonicGen.mn_encode(
    privateKey,
    "english"
  );

  if (network == "TESTNET") {
    wallet = await createWalletFull("password", mnemonic, id_utente, "TESTNET")
  } else {
    wallet = await createWalletFull("password", mnemonic, id_utente, "MAINNET")
  }

  console.log("\n---------Monero---------\n");
  let primaryAddress = await wallet.getPrimaryAddress();
  let publicSpendKey = await wallet.getPublicSpendKey();
  let publicViewKey = await wallet.getPublicViewKey();
  let privateSpendKey = await wallet.getPrivateSpendKey();
  let privateViewKey = await wallet.getPrivateViewKey();
  await saveOnDB() //function to save on DB the wallet 
}

async function generation(Masternode, network) {

    Object.keys(COIN_TYPE).forEach(async (coin) => {
    const path = `m/44'/${COIN_TYPE[coin]}'/${ACCOUNT_INDEX}'/${CHANGE_INDEX}/0`;
    const child = Masternode.derivePath(path);
    const privateKey = child.privateKey.toString("hex");
    const publicKey = child.publicKey.toString("hex");

console.log("child Private key:", privateKey);
console.log("child Public key:", publicKey);

// other code esecuted...

//address creation for MONERO Blockchain 
if (coin === "XMR") {
  await createAddressMonero(coin, path, child, network);
}

}); }

it's as if await didn't work with the createWalletFull function, as the Monero worker intervenes and immediately returns nullifying await. Is there any solution to make createWalletFull asynchronous?

While executing the program, the end of the execution of this function is not waited.

7
  • await in this context is the wallet being created, not that it has synchronized with the network or something else. Hence, what are you expecting await to be waiting on?
    – jtgrassie
    Jul 26, 2023 at 20:04
  • I'll explain better: In one function I perform several operations (among these operations is the creation of monero wallets as you see them in the question), once the operations are performed, I print it and return true. The problem is that the function, despite being set as asynchronous, returns true without waiting for the correct execution of the monero wallet and its assignment to the wallet variable.
    – AlexCav
    Jul 27, 2023 at 8:53
  • "without waiting for the correct execution of the monero wallet" I don't understand what you mean by this. Are you saying the wallet is not created? Perhaps expand your example code to show how/where something is not working as you intend/expect.
    – jtgrassie
    Jul 27, 2023 at 11:29
  • the await keyword is used to suspend the execution of the function until a promise is resolved. It doesn't work with monero's createWalletFull feature. Instead of waiting for assignment "wallet= monerojs.createWalletFull" Instead of waiting for the "wallet= monerojs.createWalletFull" assignment, the code proceeds as if that "await" wasn't there
    – AlexCav
    Jul 27, 2023 at 13:40
  • "the await keyword is used to suspend the execution of the function until a promise is resolved" yes. but are you saying it's returning a promise that is not resolved or something else? Perhaps expand your example code to show how/where something is not working as you intend/expect.
    – jtgrassie
    Jul 27, 2023 at 18:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.