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

crypto.randomBytes is not a function #398

Closed
irelandscape opened this issue Aug 4, 2018 · 9 comments
Closed

crypto.randomBytes is not a function #398

irelandscape opened this issue Aug 4, 2018 · 9 comments

Comments

@irelandscape
Copy link

irelandscape commented Aug 4, 2018

Expected behavior

Should be able to import steem library into my vue,js project.

Actual behavior

Getting the following error:
[Vue warn]: Error in mounted hook: "TypeError: crypto.randomBytes is not a function"
on the following line:
var steem = require('steem')

The exception is thrown from the nodeRandom function in secure-random.js.
I put a breakpoint in that function and I can see that require('crypto') returns an empty object:

> crypto
<- {}

How to reproduce

I seem to be able to reproduce the problem by just invoking:
require('steem')

The reported error is in secure-random.js, in the function nodeRandom.

I have verified that I can invoke the crypto.randomBytes function directly from my application.
The following works:
console.log(require('crypto').randomBytes(64))
so I'm wondering if the steem library somehow messes up crypto.

Environment information

Using node 8.10 on Ubuntu 18.04
Application uses the Quasar UI framework with vue.js.

@Sekhmet
Copy link
Contributor

Sekhmet commented Aug 11, 2018

This seems to be vue-cli specific problem. If application is running or built using vue-cli process.pid is set to 1. secure-random checks for process.pid and if it is a number it assumes you are running inside of node, which causes the library not to work.

https://github.com/jprichardson/secure-random/blob/master/lib/secure-random.js#L20

@Sekhmet
Copy link
Contributor

Sekhmet commented Aug 11, 2018

I have filled a patch to secure-random. If it gets merged & released I will create a PR for steem-js to use new version. In other case I will release the new version myself so it is usable with vue-cli.

@BartolomeoItaliano
Copy link

BartolomeoItaliano commented Aug 11, 2018

I think I've solved your problem already, I release my version of library some time ago. Try to use this version of steem-js:

npm install steem-js-refactored
tell me if it resolves your problem

@irelandscape
Copy link
Author

Hi thanks. I'll try to confirm the fix.

@irelandscape
Copy link
Author

Hi BartolomeoItalia,

Unfortunately I still get the same error on the line:

var steem = require('steem-js-refactored')

@miikkikiikki
Copy link

Exactly the same problem, try to start my vue.js project with steem-js, but get an error.

@Sekhmet
Copy link
Contributor

Sekhmet commented Aug 18, 2018

You can use https://github.com/jnordberg/dsteem
It should be more than enough for most usecases.

@nothingismagick
Copy link

@irelandscape - I have resolved this by the way with dsteem (using quasar 0.17.15):

// /src/plugins/dsteem.js
import { Client } from 'dsteem'

export default ({ app, router, Vue }) => {
  const steemClient = new Client('https://api.steemit.com')
  Vue.prototype.$steemClient = steemClient
}

// script part in a vue SFC
export default {
  name: 'Index',
  data () {
    return {
      props: {
        maximum_block_size: this.main()
      }
    }
  },
  methods: {
    main: async function () {
      await this.$steemClient.database.getChainProperties().then((props) => {
        console.log(`Maximum blocksize consensus: ${props.maximum_block_size} bytes`)
        this.props = props
      })
    }
  }
}

@joticajulian
Copy link

The solution of @Sekhmet worked for me. I had to clone both secure-random and steem-js, make the changes in secure-random, install it on steem-js and build steem-js.
PS: I can not use dsteem because i want to use steem.memo.encode and steem.memo.decode, and these functions are not in dsteem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants