bitcoinjs / bitcoinjs-lib

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

React-native use RELEASE scheme crash, HDNode.fromSeedBuffer #1008

Closed yinchengvy closed 6 years ago

yinchengvy commented 6 years ago

React-native, iOS, RELEASE

Hi, I got a crash on iOS platform only use release scheme.

function generatorBIP44() {
    const mnemonic = bip39.generateMnemonic();
    assert(bip39.validateMnemonic(mnemonic));

    const seed = bip39.mnemonicToSeed(mnemonic);
    const bitcoinNetwork = bitcoin.networks.bitcoin;
    const master = bitcoin.HDNode.fromSeedBuffer(seed, bitcoinNetwork);

    const derivePath = `m/44'/0'/0'/0/0`;
    const node = master.derivePath(derivePath);

    const keyPair = node.keyPair;

    const privateKey = keyPair.toWIF();

    const publicKeyBuffer = keyPair.getPublicKeyBuffer();
    const publicKey = publicKeyBuffer.toString('hex');

    const address = keyPair.getAddress();

    return {
        mnemonic,
        address,
        privateKey,
        publicKey,
    };
}

I found crash here

    const master = bitcoin.HDNode.fromSeedBuffer(seed, bitcoinNetwork);

    typeforce(types.tuple('ECPair', types.Buffer256bit), arguments)

If i remove typeforce check code, it's OK.

It's amazing when i run it OK on debug scheme, but always crash on release scheme. Someone please help me? Thx.

dabura667 commented 6 years ago

Read the README, please.

Add reserve rules to uglifyjs.

yinchengvy commented 6 years ago

Thx, and I fixed this issue.

Modify node_modules/metro-bundler/src/JSTransformer/worker/minify.js

mangle: {
      toplevel: true,
      reserved: [
        'Buffer',
        'BigInteger',
        'Point',
        'ECPubKey',
        'ECKey',
        'sha512_asm',
        'asm',
        'ECPair',
        'HDNode'
      ]
    },
dcousens commented 6 years ago

@dabura667 hopefully this is less of an issue in 4.0.0.

JamesZhange commented 5 years ago

where the "README" talk about "uglifyjs"?

junderw commented 5 years ago

@JamesZhange Currently released version is 3.3.2

https://github.com/bitcoinjs/bitcoinjs-lib/blob/v3.3.2/README.md

NOTE: If you uglify the javascript, you must exclude the following variable names from being mangled: BigInteger, ECPair, Point. This is because of the function-name-duck-typing used in typeforce.

Example:

uglifyjs ... --mangle reserved=['BigInteger','ECPair','Point']

NOTE: This library tracks Node LTS features, if you need strict ES5, use --transform babelify in conjunction with your browserify step (using an es2015 preset).