Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/invert client + providers to constructor params w/ specific interface #10

Conversation

mateodelnorte
Copy link
Contributor

@mateodelnorte mateodelnorte commented Jul 13, 2018

  • refactoring rest client to composition instead of inheritance
  • refactoring network providers be provided via constructor, allowing any number of providers
    • refactor this.providers to array
    • refactor all providers to match common interface
    • refactor connect() to cycle through all specified providers
  • refactor crypto usage to constructor-provided param
  • refactor appSecret to be provided by env vars to both tests/sdk and services

@alex-miller-0
Copy link
Contributor

I think we should remove the crypto.randomBytes fallback for the privKey generation and instead require the user to provider a private key hex string. crypto does not work with react native, which will require the user to generate bytes with something like this

@mateodelnorte
Copy link
Contributor Author

mateodelnorte commented Jul 14, 2018 via email

@mateodelnorte
Copy link
Contributor Author

Looks like there are 5 places where crypto is used:

const hash = crypto.createHash('sha256').update(preImage).digest();

const msg = crypto.createHash('sha256').update(`${id}${type}${req}`).digest();

this.appSecret = crypto.randomBytes(6).toString('hex');

return crypto.randomBytes(32).toString('hex');

return crypto.createHash('sha256').update(this.sharedSecret).digest().toString('hex');

Those uses consolidate to

crypto.createHash('sha256').update(x).digest(); and crypto.randomBytes(x).toString('hex');.

We could pass in a cryptoProvider imp via the SDKClient constructor, which it will pass into the RestClient. We could provide implementations that will work for node, web, and react-native. Users would just specify which to use in the constructor. The imps would be pretty simple:

export const node {
  createHash: (input) => { return crypto.createHash('sha256').update(input).digest(); },
  randomBytes: (num) => { return crypto.randomBytes(num).toString('hex'); },
}

Usage could be something like...

import { Client, crypto } from 'gridplus-sdk';
new Client({ crypto: crypto.node, ... })

… be refactored to separate package if needed.
@mateodelnorte
Copy link
Contributor Author

Took a swack at that, in commit 5355edc above.

@alex-miller-0
Copy link
Contributor

alex-miller-0 commented Jul 14, 2018

Looks great. There are several crypto libs for RN so we can just swap those in depending on the build.

Also I've had problems with elliptic in RN before. Don't think there's an action item here but we should just be aware of it.

Edit: looks like there may be a workaround within elliptic. I'll make an issue to ensure all elliptic functionality here works with RN

@mateodelnorte
Copy link
Contributor Author

Sounds good. Last refactor left on this is encapsulating the providers to conform to a specific interface and moving them to be passed in through the constructor. I should be able to get it done after the SmartGrid meeting.

@alex-miller-0
Copy link
Contributor

Two things that need to be updated in this API (whether in this PR or not):

  1. connect should take a serial argument (so that it can find a specific agent to connect with)
  2. pair should take a appSecret argument. I think this is more natural behavior than generating an app secret via the SDK and then calling pair. I think we can have it as an optional argument, but I do think many devs will want to generate their own secrets rather than relying on our mechanism.

test/agent.js Outdated
// Use React Native crypto for this series of tests.
// The node.js version is faster, but we want to test both
const privKey = crypto.randomBytes(32).toString('hex');
const rnCrypto = new ReactNativeCrypto(privKey);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alex-miller-0 shouldn't we be using a means other than crypto to provide entropy to crypto/react-native?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The origin of the entropy is irrelevant for cryptographic purposes. A RN app would use a different source, but I don't see any reason not to use crypto (which is simpler) in a test that will run via node

@mateodelnorte mateodelnorte changed the title NOT READY FOR MERGE: Feature/invert client + providers to constructor params w/ specific interface feature/invert client + providers to constructor params w/ specific interface Jul 20, 2018
@alex-miller-0 alex-miller-0 merged commit c21a31e into master Jul 20, 2018
@mateodelnorte mateodelnorte deleted the feature/invert-providers-to-constructor-param-and-match-interface branch July 23, 2018 16:22
mateodelnorte pushed a commit that referenced this pull request Nov 12, 2018
…tructor-param-and-match-interface

feature/invert client + providers to constructor params w/ specific interface
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants