indutny / elliptic

Fast Elliptic Curve Cryptography in plain javascript
1.7k stars 376 forks source link

genKeyPair() not implemented error #149

Open omniwuyi opened 6 years ago

omniwuyi commented 6 years ago

Hi there

I'm new here, hope this is the right place to raise an issue.

I am using this lib for a node.js backend project (no browser involved), but when calling the genKeyPair() function, it throws the error below. Looking into the brorand index.js, it throws an error if it is safari. I have been searching for a solution, but couldn't find one. Hope you guys can shed some light. Thanks!

my env:

--------------Error for invoking genKeyPair -------------------------------------- Not implemented yet

   5 | class ChainUtil {
   6 |     static genKeyPair(){
>  7 |         return ec.genKeyPair();
   8 |     }
   9 | 
  10 |     static id(){

  at Rand.Object.<anonymous>.Rand._rand (node_modules/brorand/index.js:50:13)
  at Rand.generate (node_modules/brorand/index.js:16:15)
  at Object.rand (node_modules/brorand/index.js:7:12)
  at EC.genKeyPair (node_modules/elliptic/lib/elliptic/ec/index.js:62:42)
  at Function.genKeyPair (chain-util.js:7:19)
  at new Wallet (wallet/index.js:7:34)
  at Object.beforeEach (wallet/transaction.test.js:8:18)

-------------- The brorand code throws the above error ------

// Safari's WebWorkers do not have crypto } else if (typeof window === 'object') { // Old junk Rand.prototype._rand = function() { throw new Error('Not implemented yet'); }; }


mahrud commented 6 years ago

Easiest solution is to generate randomness separately and pass it in directly. See for example: https://github.com/openpgpjs/openpgpjs/blob/c9d837cf8a316d2b4041c209e7fd815edbd38890/src/crypto/public_key/elliptic/curves.js#L196-L207 ps: it's not the best idea that the randomness length is hardcoded in there. I'll fix it soon.

aman-parnami commented 6 years ago

any update on the fix?

ko91h commented 6 years ago

+1

nickvamvou commented 6 years ago

It happens because the testing framework you are using assumes you are running the test in the browser, but you are running the code in the node environment. Go to package.json and if you are running the tests with jest for example type

"jest" : { "testEnvironment : "node" }

Replace it according to the test framework you are using.

hfossli commented 4 years ago

Copy-pasta ready

{
  "name": ...,
  "scripts": {
    ...
  },
  "dependencies": {
    ...
  },
  "devDependencies": {
    ...
  },
  "jest": {
    "testEnvironment": "node"
  }
}

(package.json)

anuranBarman commented 3 years ago

It happens because the testing framework you are using assumes you are running the test in the browser, but you are running the code in the node environment. Go to package.json and if you are running the tests with jest for example type

"jest" : { "testEnvironment : "node" }

Replace it according to the test framework you are using.

Unfortunately getting the same error event after trying your solution. Using node version 12.14.0

lightyear15 commented 3 years ago

I am having the same issue developing an app in electron using Vue My package.json file includes this:

  "jest": {
    "setupFiles": [
      "<rootDir>/jest.init.ts"
    ],
    "preset": "@vue/cli-plugin-unit-jest/presets/typescript-and-babel",
    "testEnvironment": "node"
  }

and the jest.init.ts is

import Vuetify from "vuetify";
import Vue from "vue";
import development from "./src/config/development";
import { EnvironmentConfig } from "./src/types/EnvironmentConfigInterface";
import axios, { AxiosInstance } from "axios";
import endpoints from "./src/shared/endpoints";
import Endpoints from "./src/types/EndpointsInterface";
import VueApexCharts from "vue-apexcharts";

Vue.prototype.$axios = axios;
Vue.prototype.$endpoints = endpoints;
Vue.prototype.$config = Object.freeze(development);
import store from "./src/store";
Vue.prototype.$store = store;
declare module "vue/types/vue" {
  interface Vue {
    $config: EnvironmentConfig;
    $axios: AxiosInstance;
    $endpoints: Endpoints;
  }
}
Vue.component("apexchart", VueApexCharts);
Vue.use(Vuetify);
Vue.config.productionTip = false;

If I remove the ""setupFiles" key in the package.json, then the error disappears

I hope this may be of help to debug and fix the issue