bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.6k stars 2.08k forks source link

how can get the bitcoinjs-lib in react-native #1093

Closed jiangbo0216 closed 6 years ago

jiangbo0216 commented 6 years ago

i use the code as follows in reactnative: let testnet = require ('bitcoinjs-lib').networks.testnet;

but a problem occur to me, Cannot read property 'prototype' of undefined Requiring module "node_modules\bitcoinjs-lib\src\index.js", which threw an exception: TypeError: Cannot read property 'prototype' of undefined how can get the bitcoinjs-lib in react-native

thank you all!

jiangbo0216 commented 6 years ago

I found something,in reactnative, when i degug the programme ,the require is refer to node_modules\metro\src\lib\polyfills\require.js the code as follows:

function require(moduleId) {
  if (__DEV__ && typeof moduleId === 'string') {
    const verboseName = moduleId;
    moduleId = verboseNamesToModuleIds[verboseName];
    if (moduleId == null) {
      throw new Error(`Unknown named module: '${verboseName}'`);
    } else {
      console.warn(
      `Requiring module '${verboseName}' by name is only supported for ` +
      'debugging purposes and will BREAK IN PRODUCTION!');

    }
  }

  //$FlowFixMe: at this point we know that moduleId is a number
  const moduleIdReallyIsNumber = moduleId;
  const module = modules[moduleIdReallyIsNumber];
  **return module && module.isInitialized ?
  module.exports :
  guardedLoadModule(moduleIdReallyIsNumber, module);**
}

and the problem is at the end .i bold it

  **return module && module.isInitialized ?
  module.exports :
  guardedLoadModule(moduleIdReallyIsNumber, module);**

module = {dependencyMap: Array(13), exports: undefined, factory: ƒ, hasError: false, isInitialized: false} as you see, isInitialized is false,so the problem maybe is here

jiangbo0216 commented 6 years ago

i know the reason . in the reactnative ,we don't have the module named stream ,so i install this module from npm . the terrible thing is this module is not compatible. in the route 'node_modules\hash-base\index.js' has code like this:

var Buffer = require('safe-buffer').Buffer
var Transform = require('stream').Transform
var inherits = require('inherits')
..................
inherits(HashBase, Transform)

node_modules\util\node_modules\inherits\inherits_browser.js

module.exports = function inherits(ctor, superCtor) {
    ctor.super_ = superCtor
    ctor.prototype = Object.create(superCtor.prototype, {
      constructor: {
        value: ctor,
        enumerable: false,
        writable: true,
        configurable: true
      }
    });

and i use this module https://www.npmjs.com/package/stream. so i got this problem.

in the end,i want to know if i want to use the bitcoinjs-lib in reactnative .what should i do .???? thanks

dcousens commented 6 years ago

@JQiMeng have you read through https://github.com/bitcoinjs/bitcoinjs-lib/issues/976 ?

dcousens commented 6 years ago

Maybe @wmpedersen or @jackylimel could assist

dcousens commented 6 years ago

@JQiMeng did you find a solution?