bitcoinjs / ecpair

The ECPair module for bitcoinjs-lib
MIT License
15 stars 22 forks source link

ECPairFactory(tinysecp) -> TypeError: ecc.privateNegate is not a function #14

Closed educob closed 1 year ago

educob commented 1 year ago

Hi.

Running this code:

import { ECPairFactory } from 'ecpair';
import tinysecp from 'tiny-secp256k1';
const ECPair = ECPairFactory(tinysecp);

Throws error: TypeError: ecc.privateNegate is not a function

Thanks.

junderw commented 1 year ago

Update your tiny-secp256k1 version.

educob commented 1 year ago

Thanks for answering.

I updated to version 2.2.1 and now I get error: TypeError: Cannot read properties of undefined (reading 'isPoint') And ecpair release is v2.1.0

Thanks.

landabaso commented 1 year ago

I updated to version 2.2.1 and now I get error: TypeError: Cannot read properties of undefined (reading 'isPoint') And

For me it is working fine.

isPoint is exported here: https://github.com/bitcoinjs/tiny-secp256k1/blob/master/src_ts/index.ts#L75

I have a feeling that you may not be importing the library correctly. Are you using webpack/browser? In webpack, it is imported as a Promise because it is packaged as WASM. Maybe that. Please share a small code snippet if you like.

educob commented 1 year ago

Thanks for answering.

I am working in a react native project. Don't know if webpack is used (I guess not). The code I am using (taken from the doc) is the one I pasted above:


import { ECPairFactory } from 'ecpair';
import tinysecp from 'tiny-secp256k1';
const ECPair = ECPairFactory(tinysecp);

Thanks.

landabaso commented 1 year ago

You can't use tiny-secp256k1 in react-native (or at least easily). It uses WASM which was not very well supported (if at all) by react-native.

Your easiest way out is:

You'll need to use tiny-sepc256k1 version 1 (that version was using elliptic pure javascript library as fallback) and switch back to ecpair 2.0 (elliptic doesn't implement all the methods required by ecpair 2.1).

You can also use noble. It was not possible until a couple of weeks ago because it used BigInt which was not supported in react native. This changed in react-native starting from 0.7 (using the new Javascript engine Hermes) but integrating it all requires a bit more work. The good part is it's fast as hell once it works.

I would suggest tiny-secp256k1 v1 + ecpair 2.0 as the easiest way to get it working.

educob commented 1 year ago

Hi.

I tried with tiny at v1 and ecpair v2 but I get TypeError: ecc.privateNegate is not a function I'll try with noble.

Thanks.

landabaso commented 1 year ago

Make sure it's ecpair 2.0.1 Note that it does not test against privateNegate: https://github.com/bitcoinjs/ecpair/blob/v2.0.1/src/testecc.js

vs. 2.1.0 that does: https://github.com/bitcoinjs/ecpair/blob/v2.1.0/src/testecc.js

vesparny commented 1 year ago

2.1.0 breaks compatibility with tiny-secp256k1@1.* I'm wondering if this should be considered a breaking change.

As of today, tiny-secp256k1@2.* isn't usable in some environments (e.g. React Native)

educob commented 1 year ago

Make sure it's ecpair 2.0.1

Thanks. It works now!!