bitcoinjs / ecpair

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

Why I can not use any the example code for version 2.1.0? #16

Closed zydjohnHotmail closed 1 year ago

zydjohnHotmail commented 1 year ago

Hi, I want to run some test for the latest version (2.1.0) of the repo, but I have to use ‘es6’, not ‘commonJS’. I can see the example code seem in TypeScript, since the variables have data type defined. However, I just want to write some simple JavaScript code, so just ignore the data types. I have installed the following npm packages: "ecpair": "^2.1.0", "safe-buffer": "^5.2.1", "tiny-secp256k1": "^2.2.1"

The following is my test-ecpair.js code:

import * as tinysecp from 'tiny-secp256k1'; import { ECPairFactory } from 'ecpair'; import crypto from 'crypto';

const { ECPair } = ECPairFactory(tinysecp);

const keyPair3 = ECPair.makeRandom(); console.log("keyPair3 = ", keyPair3);

const keyPair1 = ECPair.fromWIF('KynD8ZKdViVo5W82oyxvE18BbG6nZPVQ8Td8hYbwU94RmyUALUik'); console.log("keyPair1 =", keyPair1);

const random_bytes32 = crypto.randomBytes(32); console.log("random_bytes32 =", random_bytes32); const keyPair2 = ECPair.fromPrivateKey(Buffer.from(random_bytes32.buffer));

const customRandomBufferFunc = (size) => crypto.randomBytes(size); console.log(customRandomBufferFunc); const keyPair4 = ECPair.makeRandom({ rng: customRandomBufferFunc }); console.log("keyPair4 =", keyPair4);

For all the methods, I got the same error: E:\npms\qtumjs>node test-ecpair.js file:///E:/npms/qtumjs/test-ecpair.js:7 const keyPair3 = ECPair.makeRandom(); ^

TypeError: Cannot read properties of undefined (reading 'makeRandom') at ←[90mfile:///E:/npms/qtumjs/←[39mtest-ecpair.js:7:25 ←[90m at ModuleJob.run (node:internal/modules/esm/module_job:192:25)←[39m

Node.js v20.2.0

E:\npms\qtumjs> Please advise on how to fix the error? I am using Windows 10. Thanks,

junderw commented 1 year ago

const { ECPair } = ECPairFactory(tinysecp);

should be

const ECPair = ECPairFactory(tinysecp);

zydjohnHotmail commented 1 year ago

Hi, Thanks for your quick reply, your code works.
However, is there any tools like: ethers playground, where I can use the tools to try different privatekey or paraphrase, without writing a lot of code?