NativeScript: crypto broken?

Environment Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):

  • CLI: 6.8.0
  • Cross-platform modules: 6.5.15
  • Android Runtime: 6.5.3
  • iOS Runtime: 6.5.2
  • Plugin(s):

Describe the bug Trying to use the http-client-auth module for digest authentication brought me an error for missing function crypto.getHash. Adding a require for crypto or crypto-browserify to my code results in a long exception stacktrace at the require point:

System.err: An uncaught Exception occurred on "main" thread.
System.err: Unable to create application com.tns.NativeScriptApplication: com.tns.NativeScriptException: Error calling module function
System.err: TypeError: Cannot read property 'split' of undefined
System.err: File: (file: node_modules\pbkdf2\lib\default-encoding.js:6:47)
System.err:
System.err: StackTrace:
System.err: ../node_modules/pbkdf2/lib/default-encoding.js(file: node_modules\pbkdf2\lib\default-encoding.js:6:47)
System.err:     at __webpack_require__(file: app\webpack\bootstrap:750:0)
System.err:     at fn(file: app\webpack\bootstrap:120:0)
System.err:     at ../node_modules/pbkdf2/lib/sync.js(file: node_modules\pbkdf2\lib\sync.js:14:22)
System.err:     at __webpack_require__(file: app\webpack\bootstrap:750:0)
System.err:     at fn(file: app\webpack\bootstrap:120:0)
System.err:     at ../node_modules/pbkdf2/index.js(file: node_modules\pbkdf2\index.js:24:23)
System.err:     at __webpack_require__(file: app\webpack\bootstrap:750:0)
System.err:     at fn(file: app\webpack\bootstrap:120:0)
System.err:     at ../node_modules/crypto-browserify/index.js(file: node_modules\crypto-browserify\index.js:14:8)
System.err:     at __webpack_require__(file: app\webpack\bootstrap:750:0)
System.err:     at fn(file: app\webpack\bootstrap:120:0)
System.err:     at ./lib/tnsSrvConn.js(file: app\lib\tnsSrvConn.js:4:15)
System.err:     at __webpack_require__(file: app\webpack\bootstrap:750:0)
System.err:     at fn(file: app\webpack\bootstrap:120:0)
System.err:     at ../node_modules/babel-loader/lib/index.js!../node_modules/vue-loader/lib/index.js?!./components/MyComponent.vue?vue&type=script&lang=js&(file:///data/data/com.loytec.lweb804/files/app/bundle.js:1352:73)
...

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 22 (6 by maintainers)

Most upvoted comments

@keithoys But will this work with packages that depend on crypto? My problem was that I had a dependency on a package that uses crypto, and as such it was difficult to get running. Also, some parts will require a good random-number generator (which is not needed for DES). Is this the case with cryotp-js?

Well then, finally I managed to investigate and prepare a correct (hopefully, I think so) solution for crypto related libraries. Inspired with this StackOverflow answer and some researches.

  1. Let’s create a browserified version of native crypto module as a standalone library
    $ npx browserify -r crypto -s crypto -o src/shims/crypto-browserify.js
    
  2. After that, adjust your webpack.config.js file to use this package instead of a native one Add it to webpack.config.js in plugins configuration:
    plugins: [
      ..., // other plugins
      new webpack.ProvidePlugin({
            crypto: resolve(__dirname, 'src/shims/crypto-browserify.js')
        })
    ]
    
    Add resolve.alias for our version of crypto in webpack.config.js, so child dependencies require our crypto implementation:
    alias: {
      ..., // Other aliases
      'crypto': resolve(__dirname, 'src/shims/crypto-browserify.js'),
    }
    
  3. Should work. However, I’d advise to clean your node_modules, hooks, and platforms and restart the application

Sorry to hear that. I’ve implemented several crypto wallets on {N}5 and {N}6 versions using browserify approach and they successfully work in production. Moving to {N}7 may give some headache especially we have iOS14 updates at the same time but it’s not too overcomplicated I think. In any case, good luck in your undertakings.

I removed the node_modules folder and reinstalled with npm install, I even did a tns platform clean android, the problem persists.