ethereumjs / keythereum

Create, import and export Ethereum keys
MIT License
609 stars 163 forks source link

Replace Buffer with bops for webpacking into browser. #75

Closed learner-long-life closed 5 years ago

learner-long-life commented 5 years ago

currently, the otherwise excellent keythereum package, when being webpack'd, fails on the following operation in a Browser, since there is no Buffer global.

exports.toBuffer = function (v) {
  if (!Buffer.isBuffer(v)) {
    if (Array.isArray(v)) {
      v = Buffer.from(v);
    } else if (typeof v === 'string') {
      if (exports.isHexString(v)) {
        v = Buffer.from(exports.padToEven(exports.stripHexPrefix(v)), 'hex');
      } else {
        v = Buffer.from(v);
      }
    } else if (typeof v === 'number') {
      v = exports.intToBuffer(v);
    } else if (v === null || v === undefined) {
      v = Buffer.allocUnsafe(0);
    } else if (BN.isBN(v)) {
      v = v.toArrayLike(Buffer);
    } else if (v.toArray) {
      // converts a BN to a Buffer
      v = Buffer.from(v.toArray());
    } else {
      throw new Error('invalid type');
    }
  }
chikeichan commented 5 years ago

Thanks for the PR @cryptogoth !

I feel uncertain about bringing in bops as a dependency as the package has not been maintained for over 3 years (Last published was 3 years ago).

To find a better alternative, would you mind filing an issue with the error you get from webpack and start a discussion there?

holgerd77 commented 5 years ago

If I read this correctly from the last commit @cryptogoth has decided to go on with an own fork using the bops dependency, so I think we can close this PR.

Please feel free to reopen if it makes sense.

learner-long-life commented 5 years ago

Thanks for the response @holgerd77 sorry for the delay. I'm able to proceed with my fork.

In case it's useful to anyone else: webpacking with another package that provides 'bufferGlobal' as a plugin would let keythereum be used in a browser without modification.