GithubHelp home page GithubHelp logo

Comments (10)

DreaminDani avatar DreaminDani commented on July 18, 2024 2

Will do :) Might be a while since I'm going on vacation tomorrow. But I'll be sure to link a gist without monsoose refs for easy debugging.

Who knows, maybe just rewriting it will fix the thing? ;)

from signal-protocol.

elsehow avatar elsehow commented on July 18, 2024 1

Thanks for the additional detail.

Any chance you could put together a repo with the relevant code? Or perhaps a gist, if it makes sense.

from signal-protocol.

elsehow avatar elsehow commented on July 18, 2024 1

hey @d3sandoval

Thanks for getting back with this code sample!

Please don't take this the wrong way, but I think you should study up on promises a bit. For example, you have a lot of if/else's to catch errors inside of promises themselves. Promises actually have a nice mechanism for dealing with errors, which will clean up your code quite a bit:

executeMyPromise()
.then(function (result) { return someOtherPromise(result)  })
.then(function (result2) { ... })
.catch(function (someErr) { ... })

Errors propagate all the way down the promise chain!

For an example, check out https://github.com/elsehow/signal-stream/blob/master/test/helpers.js#L14-L46

Lines 14-46 may give you a starting point.

Let me know when you've got another sample and I'll take another look at it!

from signal-protocol.

elsehow avatar elsehow commented on July 18, 2024

hey @d3sandoval!

I am trying to run your code as a minimal, stand-alone example so I can debug.

Could you help me by showing how you are generating defaultEndpoint?

from signal-protocol.

DreaminDani avatar DreaminDani commented on July 18, 2024

@elsehow defaultEndpoint is a mongoose schema object:
I generate it with this function:

// retrieves or creates a defaultEndpoint using mongoose
// TODO use pre-generated keys every time from db
var getDefaultEndpoint = new Promise(function(resolve, reject) {
  // define endpoint schema
  var endpointSchema = new mongoose.Schema({
      name: String,
      number: Number,
      registrationId: Number,
      signature: String
  });

  // use mongoose encryption
  endpointSchema.plugin(encrypt, {
      encryptionKey: encKey,
      signingKey: sigKey,
      excludeFromEncryption: ['name', 'number']
  });
  // This adds _ct and _ac fields to the schema, as well as pre 'init' and pre 'save' middleware,
  // and encrypt, decrypt, sign, and authenticate instance methods

  // define "Endpoint" model
  Endpoint = mongoose.model('Endpoint', endpointSchema);

  defaultEndpoint = new Endpoint ({ name: 'Default' });
  if (defaultEndpoint) {
    // Store registrationId somewhere durable and safe.
    defaultEndpoint.registrationId = KeyHelper.generateRegistrationId();
    resolve(defaultEndpoint);
  }
  else {
    reject(Error("could not create default endpoint"));
  }
});

I know it's ugly... but I'm still prototyping ;)

This is called by a subroutine of my main function, defined below:

// Generate Identity + PreKeys
// gets/stores registration ID and KeyPair from/to database
// generates PreKeys and saves in local store
var generateIdentity = new Promise(function(resolve, reject) {
  getDefaultEndpoint.then(function(defaultEndpoint) {
    // TODO break this out into separate function
    KeyHelper.generateIdentityKeyPair().then(function(identityKeyPair) {
      // keyPair -> { pubKey: ArrayBuffer, privKey: ArrayBuffer }
      // Store identityKeyPair somewhere durable and safe (defaultEndpoint)
      store.saveIdentity(defaultEndpoint.registrationId, identityKeyPair);

      KeyHelper.generatePreKey(defaultEndpoint.registrationId).then(function(preKey) {
        store.storePreKey(preKey.keyId, preKey.keyPair);
        // defaultEndpoint.preKey = preKey;
      });

      KeyHelper.generateSignedPreKey(identityKeyPair, defaultEndpoint.registrationId).then(function(signedPreKey) {
        store.storeSignedPreKey(signedPreKey.keyId, signedPreKey.keyPair);
        defaultEndpoint.signature = ab2str(signedPreKey.signature);
        // defaultEndpoint.signedPK = signedPreKey;
      });

      if (store.loadIdentityKey(defaultEndpoint.registrationId)) {
        // Store registrationId somewhere durable and safe.
        resolve(defaultEndpoint);
      }
      else {
        reject(Error("could not generateIdentity"));
      }
    });
  });

  getDefaultEndpoint.catch(function onerror(error) {
    console.log('error getting or creating default endpoint');
  });
});

from signal-protocol.

DreaminDani avatar DreaminDani commented on July 18, 2024

If you can't get it up and running, I can create a standalone instance and give you access. Since I'm running from the node command line, and not the browser, it may be difficult to showcase without a server running it.

from signal-protocol.

elsehow avatar elsehow commented on July 18, 2024

It might also be helpful if you could factor out the mongoose-related code, so it is just native js and signal-protocol.

from signal-protocol.

elsehow avatar elsehow commented on July 18, 2024

@d3sandoval any updates on this front?

from signal-protocol.

elsehow avatar elsehow commented on July 18, 2024

@d3sandoval I'm closing this issue for now. Please feel free to re-open at any time!!

from signal-protocol.

DreaminDani avatar DreaminDani commented on July 18, 2024

Hi @elsehow sorry it took so long to get back to you. I'm new to javascript-land and I think Promises are throwing off my groove.

I've removed all the references to mongodb but now I can't seem to get the signedPreKey to load when I need it:
(node:15417) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'pubKey' of undefined

Here's a gist of what I have, so far. The error is occurring at line 79 of my code, where I attempt to assemble the signedPreKey. I've commented out the rest of the example (encrypting a message and handling identity key conflicts, since I can't seem to properly build the Object that sessionBuilder.processPreKey expects.

I fear I'm overcomplicating things since the example the README.md makes things look so straightforward! I tried borrowing from the test of InMemorySignalProtocolStore.js since that was the closest I could get to a working example of the call/response model that Signal uses to register clients... but I think I'm just circling around the problem of not setting and getting variables in the proper places.

Thanks for the help!

from signal-protocol.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.