elliptic icon indicating copy to clipboard operation
elliptic copied to clipboard

genKeyPair() not implemented error

Open omniwuyi opened this issue 6 years ago • 7 comments

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:

  • node v9.8.0
  • mac High Sierra 10.13.3 (17D47)

--------------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'); }; }


omniwuyi avatar Mar 21 '18 06:03 omniwuyi

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.

mahrud avatar Mar 21 '18 20:03 mahrud

any update on the fix?

aman-parnami avatar Jun 02 '18 09:06 aman-parnami

+1

ko91h avatar Jun 05 '18 19:06 ko91h

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.

nickvamvou avatar Jun 11 '18 14:06 nickvamvou

Copy-pasta ready

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

(package.json)

hfossli avatar Nov 26 '19 11:11 hfossli

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

anuranBarman avatar May 22 '21 09:05 anuranBarman

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

lightyear15 avatar Jul 15 '21 10:07 lightyear15