paulmillr / noble-secp256k1

Fastest 4KB JS implementation of secp256k1 signatures and ECDH
https://paulmillr.com/noble
MIT License
757 stars 114 forks source link

{Help wanted} TypeError: undefined is not a function #79

Closed theGrape1337 closed 1 year ago

theGrape1337 commented 1 year ago

Im new to cryptography and just wanted to try this package out in React Native but it only returns "TypeError: undefined is not a function". So two questions:

1) is this package compatible with RN? I thought it would be since its browser compatible.

2) If it is compatible then what am I missing? Example:

import * as secp from '@noble/secp256k1';
/..../
  const testHash = async () => {
    const msgHash = await secp.utils.sha256('hello world');
    console.log({msgHash});
  }

Response:

TypeError: undefined is not a function
TypeError: undefined is not a function
    at ?anon_0_ (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:104329:32)
    at next (native)
    at asyncGeneratorStep (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:21963:26)
    at _next (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:21982:29)
    at anonymous (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:21987:14)
    at tryCallTwo (/root/react-native/ReactAndroid/hermes-engine/.cxx/Release/443z3r4v/arm64-v8a/lib/InternalBytecode/InternalBytecode.js:61:9)
    at doResolve (/root/react-native/ReactAndroid/hermes-engine/.cxx/Release/443z3r4v/arm64-v8a/lib/InternalBytecode/InternalBytecode.js:216:25)
    at Promise (/root/react-native/ReactAndroid/hermes-engine/.cxx/Release/443z3r4v/arm64-v8a/lib/InternalBytecode/InternalBytecode.js:82:14)
    at anonymous (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:21979:25)
    at apply (native)
    at sha256 (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:104339:26)
    at ?anon_0_ (http://localhost:8081/src/screens/Home.bundle?platform=android&app=com.getin&modulesOnly=true&dev=true&minify=false&runModule=true&shallow=true:42:46)
    at next (native)
    at asyncGeneratorStep (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:21963:26)
    at _next (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:21982:29)
    at anonymous (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:21987:14)
    at tryCallTwo (/root/react-native/ReactAndroid/hermes-engine/.cxx/Release/443z3r4v/arm64-v8a/lib/InternalBytecode/InternalBytecode.js:61:9)
    at doResolve (/root/react-native/ReactAndroid/hermes-engine/.cxx/Release/443z3r4v/arm64-v8a/lib/InternalBytecode/InternalBytecode.js:216:25)
    at Promise (/root/react-native/ReactAndroid/hermes-engine/.cxx/Release/443z3r4v/arm64-v8a/lib/InternalBytecode/InternalBytecode.js:82:14)
    at anonymous (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:21979:25)
    at apply (native)
    at testHash (http://localhost:8081/src/screens/Home.bundle?platform=android&app=com.getin&modulesOnly=true&dev=true&minify=false&runModule=true&shallow=true:48:27)
    at Home (http://localhost:8081/src/screens/Home.bundle?platform=android&app=com.getin&modulesOnly=true&dev=true&minify=false&runModule=true&shallow=true:97:13)
    at renderWithHooks (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:9525:33)
    at updateFunctionComponent (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:11893:41)
    at beginWork (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:13128:45)
    at beginWork$1 (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:16707:29)
    at performUnitOfWork (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:16187:29)
    at workLoopSync (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:16131:28)
    at renderRootSync (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:16112:25)
    at performSyncWorkOnRoot (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:15888:40)
    at flushSyncCallbacks (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:6866:36)
    at flushSync (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:15953:31)
    at scheduleRefresh (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:16979:20)
    at anonymous (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:48507:38)
    at forEach (native)
    at performReactRefresh (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:48501:31)
    at performReactRefresh (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:48319:48)
    at anonymous (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:461:40)
    at apply (native)
    at anonymous (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:27671:26)
    at _callTimer (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:27606:17)
    at callTimers (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:27766:19)
    at apply (native)
    at __callFunction (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:2515:36)
    at anonymous (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:2265:31)
    at __guard (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:2463:15)
    at callFunctionReturnFlushedQueue (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.getin&modulesOnly=false&runModule=true:2264:21)
theGrape1337 commented 1 year ago

I fixed it. It was a problem with compatibility with nodes crypto package. Solved it by adding this to your metro.config.js file

/**
 * Metro configuration for React Native
 * https://github.com/facebook/react-native
 *
 * @format
 */

module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: true,
      },
    }),
  },
//add this
  resolver: {
    extraNodeModules: require('node-libs-react-native'),
  },
};

And then run npm install node-libs-react-native