Javascript, {Object Promise} Being Posted To API After Encrypting + Hex The Value

Viewed 114

Intro:

Hello. So I am encrypting soon to be properties of a JavaScript object so that I can send it to my API. I am using the ECIES scheme with secp256k1 keys. This returns a promise object before it's even sent to the API. I am not very experienced with the multi-threading "stuff" in browsers (async await) and I am wondering why it's doing this and how I can fix this. I believe this might be an issue to a larger issue, and that's not being able to appropriately compare the very same values that I retrieve from my API with client values since it's being compared to "{Object Promise}". I will post this in a separate question if this does not lead me to a feasible solution. In the second last posted code snippet, you will see the "encryptMes" function, this is what's returning the Object Promise

Focus: Why does this return {Object Promise}, and how can I make it just return a regular Object?

Relevant HTML:

       <form id="login-form" name ="signup-form">
            <input class="login-form-field" type="text" name="user" placeholder="username">
            <input class="login-form-field" type="text" name="email" placeholder="email">
            <input class="login-form-field" type="password" name="dob" placeholder="date of birth">
            <br>
            <!--<button class="actionButton"></button>-->
            <INPUT TYPE="button" class="button-success" NAME="button" Value="sign up" onClick="signupData(this.form)">
              <br>
              <div class="signup">
              <a href="login.html">login</a>
              </div>
        </form>

...

    <script  LANGUAGE="JavaScript">
    function signupData(form) //add to this script
    {
      console.log("signup data is starting");
      var user = form.user.value;
      var email = form.email.value;
      var dob = form.dob.value;

      console.log("checkpoint: 1");

      genSKey();

      console.log("checkpoint: 2");

      console.log("checkpoint: 3");

      var enUser = encryptMes(user);
    //var enEmail = encryptMes(email);
      var enDOB = encryptMes(dob);

      var data = {name:"LifeNet", members:{}} //you added members to the same area in the object so it is always replacing members since it's the field of data

      data.members[enUser] = {profilePic:{},enDOB, listeners:{}, listening:{}, friends:{}, requested:{}, blocked:{}, channel:false}

      console.log("checkpoint: 3");

      console.log({data});

      apiPost({data});
      //pass the signup function in here

      //hash the variables and send to celox network
      //console.log(JSON.stringify({data}));
      //alert (`copy and save your Private Key to somewhere safe: ${skey}`);
      //window.location.href= "login.html";
    }
  </script>

Relevant pre-Borwserify built Javascript:

window.encryptMes = function(data)
{
    //for this you need to get the sender's public key to encrypt the message
    console.log("encryptmes: began");
    var pkey = genPKey();

    if (pkey === null || undefined) 
    {
      
      console.log('You do not have a key pair');

    }

    var encryptedMes = eccrypto.encrypt(pkey, Buffer.from(data));

    var enMes = encryptedMes.toString('hex');

    console.log(encryptedMes); //could be this since it is not stringified when it goes into celox network
    console.log(enMes);

    return enMes;
}

...

window.getPKey = function()
{
    var PKey = localStorage.getItem("pkey");

    var pkey = JSON.parse(PKey);

    console.log(pkey);

    return pkey;
}

Of course in the script tags in my html file, it gets sent to a fetch post request. I know this is not the issue so I didn't include it.

Console:

signup data is starting
signup.html:188 checkpoint: 1
genKey.js:26600 opened
genKey.js:26607 Uint8Array(32) [185, 57, 181, 13, 182, 166, 40, 167, 153, 254, 37, 163, 186, 81, 215, 243, 93, 65, 225, 160, 171, 0, 39, 189, 187, 157, 148, 193, 123, 124, 19, 194]
genKey.js:26608 b939b50db6a628a799fe25a3ba51d7f35d41e1a0ab0027bdbb9d94c17b7c13c2
signup.html:192 checkpoint: 2
signup.html:194 checkpoint: 3
genKey.js:26684 encryptmes: began
genKey.js:26623 getSKey flag: 0
genKey.js:26629 getSKey flag: 1
genKey.js:26633 Uint8Array(32) [185, 57, 181, 13, 182, 166, 40, 167, 153, 254, 37, 163, 186, 81, 215, 243, 93, 65, 225, 160, 171, 0, 39, 189, 187, 157, 148, 193, 123, 124, 19, 194]
genKey.js:26635 getSKey flag: 2
genKey.js:26640 getSKey flag: 3
genKey.js:26698 Promise {<pending>}
genKey.js:26699 [object Promise]
genKey.js:26684 encryptmes: began
genKey.js:26623 getSKey flag: 0
genKey.js:26629 getSKey flag: 1
genKey.js:26633 Uint8Array(32) [185, 57, 181, 13, 182, 166, 40, 167, 153, 254, 37, 163, 186, 81, 215, 243, 93, 65, 225, 160, 171, 0, 39, 189, 187, 157, 148, 193, 123, 124, 19, 194]
genKey.js:26635 getSKey flag: 2
genKey.js:26640 getSKey flag: 3
genKey.js:26698 Promise {<pending>}
genKey.js:26699 [object Promise]
signup.html:204 checkpoint: 3
signup.html:206 {data: {…}}
1 Answers

You need to wait for the Promise to be resolved until you can use the result. Use async/await.

window.encryptMes = async function(data)
{
    // ...

    var encryptedMes = await eccrypto.encrypt(pkey, Buffer.from(data));
    var enMes = encryptedMes.toString('hex');

    // ...

    return enMes;
}
Related