bitcoinjs / tiny-secp256k1

A tiny secp256k1 native/JS wrapper
MIT License
92 stars 55 forks source link

How do I import? #57

Closed ahtremblay closed 3 years ago

ahtremblay commented 3 years ago

I have run

npm install tiny-secp256k1

How do I then import it into a typescript file? This does not work.

import * as secp256k1 from "secp256k1";

This does not work either:

import * as any from "tiny-secp256k1";

Finally, I have tried

npm i --save-dev @types/tiny-secp256k1

but it outputs: npm WARN deprecated @types/tiny-secp256k1@2.0.1: This is a stub types definition. tiny-secp256k1 provides its own type definitions, so you do not need this installed.

junderw commented 3 years ago
  1. Make sure you have node version v14 or higher
  2. Install the latest typescript as a dependency
  3. Make sure your tsconfig.json contains the following two attributes under compilerOptions:
"module": "ES2020",
"moduleResolution": "node",
  1. Import the module as follows under something.ts:
import * as secp256k1 from 'tiny-secp256k1';

console.log(
  // One of the methods of the library, will return true if working.
  secp256k1.isPrivate(
    Buffer.from(
      // Random 32 byte key
      'f816cde505015173a81858e5969ac9f2' +
      '99b3d15ad9f1d9806c230cfdf0e7739f',
      'hex',
    ),
  ),
);
  1. Compile to JS
  2. run node something.js (if you named it something.ts)

Note: ts-node is broken in regards to ES modules, so running something.ts via ts-node something.ts doesn't work.

ahtremblay commented 3 years ago

@junderw

Thank you for the response.

Your example code on line 4 return 'true', so it is working. However the line:

import * as secp256k1 from 'tiny-secp256k1';

produces this error in the IDE:

TS7016: Could not find a declaration file for module 'tiny-secp256k1'. '/Users/alexandreharvey-tremblay/Dropbox/software/cryptocom/node_modules/tiny-secp256k1/index.js' implicitly has an 'any' type.   Trynpm i --save-dev @types/tiny-secp256k1if it exists or add a new declaration (.d.ts) file containingdeclare module 'tiny-secp256k1';``

Something to worry about?

junderw commented 3 years ago

Are you sure you're using v2.0.0 of this library? Also, is "moduleResolution": "node", in your tsconfig?

v2.0.0 has type definitions and you don't need to install any types separately.

ahtremblay commented 3 years ago

@junderw

You are correct it has installed "tiny-secp256k1": "^1.1.6"

but I did run

npm install tiny-secp256k1

now having run

npm install tiny-secp256k1@latest

it has installed "tiny-secp256k1": "^2.0.0"

and the error is gone.

Thanks.