Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed missing .js to imports #25

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/ecpair.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/prettier.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 19 additions & 5 deletions fixup.cjs
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
const fs = require('fs');
const path = require('path');

const updateRequires = (filePath) => {
const updateRequireStatements = (filePath) => {
let content = fs.readFileSync(filePath, 'utf8');
//replace local imports eg. require('./ecpair') to require('ecpair.cjs')
content = content.replace(/require\('\.\/([^']*)'\)/g, "require('./$1.cjs')");

// Replace require('./something.js') with require('./something.cjs')
content = content.replace(/require\('\.\/([^']*)\.js'\)/g, "require('./$1.cjs')");

// Replace import/export in .d.ts files
content = content.replace(/from '\.\/([^']*)\.js'/g, "from './$1.cjs'");

fs.writeFileSync(filePath, content, 'utf8');
};

const processFiles = (dir) => {
fs.readdirSync(dir).forEach((file) => {
const filePath = path.join(dir, file);
const newPath = filePath.replace('.js', '.cjs');

if (fs.lstatSync(filePath).isDirectory()) {
processFiles(filePath);
} else if (filePath.endsWith('.cjs')) {
updateRequires(filePath);
} else if (path.extname(file) === '.js') {
// Rename the .js file to .cjs
updateRequireStatements(filePath);

fs.renameSync(filePath, newPath);
}

// Update .d.ts files to replace .js references with .cjs
if (path.extname(file) === '.ts') {
updateRequireStatements(filePath);
}
});
};
Expand Down
22 changes: 12 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ecpair",
"version": "3.0.0-rc.0",
"version": "3.0.1-rc.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3.0.0-rc.1

"description": "Client-side Bitcoin JavaScript library ECPair",
"type": "module",
"main": "src/cjs/index.cjs",
Expand All @@ -25,26 +25,26 @@
],
"scripts": {
"audit": "NPM_AUDIT_IGNORE_DEV=1 NPM_AUDIT_IGNORE_LEVEL=low npm-audit-whitelister .npm-audit-whitelister.json",
"build": "npm run clean && tsc -p ./tsconfig.json && tsc -p tsconfig.cjs.json && npm run formatjs",
"postbuild": "find src/cjs -type f -name \"*.js\" -exec bash -c 'mv \"$0\" \"${0%.js}.cjs\"' {} \\; && chmod +x ./fixup.cjs && node fixup.cjs",
"build": "npm run clean && tsc -p ./tsconfig.json && tsc -p tsconfig.cjs.json && npm run formatjs && node fixup.cjs",
"clean": "rimraf src",
"clean:jstests": "rimraf 'test/**/*.js'",
"coverage-report": "npm run build && npm run nobuild:coverage-report",
"coverage-html": "npm run build && npm run nobuild:coverage-html",
"coverage": "npm run build && npm run nobuild:coverage",
"format": "npm run prettier -- --write",
"formatjs": "npm run prettierjs -- --write",
"format:ci": "npm run prettier -- --check && npm run prettierjs -- --check",
"format:ci": "npm run prettier -- --check && npm run prettiercjs -- --check && npm run prettierjs -- --check",
"gitdiff:ci": "npm run build && git diff --exit-code",
"lint": "tslint -p tsconfig.json -c tslint.json",
"lint:tests": "tslint -p test/tsconfig.json -c tslint.json",
"mocha:ts": "mocha --recursive",
"nobuild:coverage-report": "c8 report --reporter=lcov",
"nobuild:coverage-html": "c8 report --reporter=html",
"nobuild:coverage": "c8 --check-coverage --exclude='**/*.cjs' --branches 90 --functions 90 --lines 90 npm run unit",
"nobuild:coverage": "c8 --check-coverage --exclude='**/*.js' --branches 90 --functions 90 --lines 90 npm run unit",
"nobuild:unit": "npm run mocha:ts -- 'test/*.ts'",
"prettier": "prettier \"ts_src/**/*.ts\" \"test/**/*.ts\" --ignore-path ./.prettierignore",
"prettierjs": "prettier \"src/**/*.js\" --ignore-path ./.prettierignore",
"prettiercjs": "prettier \"src/**/*.cjs\" --ignore-path ./.prettierignore",
"test": "npm run build && npm run format:ci && npm run lint && npm run nobuild:coverage",
"unit": "npm run build && npm run nobuild:unit"
},
Expand All @@ -71,8 +71,8 @@
"rimraf": "^2.6.3",
"tiny-secp256k1": "^2.2.3",
"tslint": "^6.1.3",
"tsx": "^4.16.5",
"typescript": "^5.0.4"
"tsx": "^4.19.1",
"typescript": "^5.6.3"
},
"license": "MIT"
}
23 changes: 14 additions & 9 deletions src/cjs/ecpair.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const networks = __importStar(require('./networks.cjs'));
exports.networks = networks;
const types = __importStar(require('./types.cjs'));
const wif = __importStar(require('wif'));
const testecc_1 = require('./testecc.cjs');
const testecc_js_1 = require('./testecc.cjs');
const v = __importStar(require('valibot'));
const tools = __importStar(require('uint8array-tools'));
const ECPairOptionsSchema = v.optional(
Expand All @@ -65,8 +65,7 @@ const ECPairOptionsSchema = v.optional(
return (arg) => {
const parsedArg = v.parse(v.optional(v.number()), arg);
const returnedValue = func(parsedArg);
const parsedReturn = v.parse(v.instance(Uint8Array), returnedValue);
return parsedReturn;
return v.parse(v.instance(Uint8Array), returnedValue);
};
}),
),
Expand All @@ -76,7 +75,7 @@ const ECPairOptionsSchema = v.optional(
const toXOnly = (pubKey) =>
pubKey.length === 32 ? pubKey : pubKey.subarray(1, 33);
function ECPairFactory(ecc) {
(0, testecc_1.testEcc)(ecc);
(0, testecc_js_1.testEcc)(ecc);
function isPoint(maybePoint) {
return ecc.isPoint(maybePoint);
}
Expand Down Expand Up @@ -115,6 +114,17 @@ function ECPairFactory(ecc) {
network: network,
});
}
/**
* Generates a random ECPairInterface.
*
* Uses `crypto.getRandomValues` under the hood for options.rng function, which is still an experimental feature as of Node.js 18.19.0. To work around this you can do one of the following:
* 1. Use a polyfill for crypto.getRandomValues()
* 2. Use the `--experimental-global-webcrypto` flag when running node.js.
* 3. Pass in a custom rng function to generate random values.
*
* @param {ECPairOptions} options - Options for the ECPairInterface.
* @return {ECPairInterface} A random ECPairInterface.
*/
function makeRandom(options) {
v.parse(ECPairOptionsSchema, options);
if (options === undefined) options = {};
Expand All @@ -128,11 +138,6 @@ function ECPairFactory(ecc) {
return fromPrivateKey(d, options);
}
class ECPair {
__D;
__Q;
compressed;
network;
lowR;
constructor(__D, __Q, options) {
this.__D = __D;
this.__Q = __Q;
Expand Down
6 changes: 3 additions & 3 deletions src/cjs/ecpair.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Network } from './networks';
import * as networks from './networks';
export { networks };
import * as networks from './networks.cjs';
import { Network } from './networks.cjs';
import * as v from 'valibot';
export { networks };
declare const ECPairOptionsSchema: v.OptionalSchema<v.ObjectSchema<{
readonly compressed: v.OptionalSchema<v.BooleanSchema<undefined>, never>;
readonly network: v.OptionalSchema<v.ObjectSchema<{
Expand Down
8 changes: 4 additions & 4 deletions src/cjs/index.cjs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.networks = exports.ECPairFactory = exports.default = void 0;
var ecpair_1 = require('./ecpair.cjs');
var ecpair_js_1 = require('./ecpair.cjs');
Object.defineProperty(exports, 'default', {
enumerable: true,
get: function () {
return ecpair_1.ECPairFactory;
return ecpair_js_1.ECPairFactory;
},
});
Object.defineProperty(exports, 'ECPairFactory', {
enumerable: true,
get: function () {
return ecpair_1.ECPairFactory;
return ecpair_js_1.ECPairFactory;
},
});
Object.defineProperty(exports, 'networks', {
enumerable: true,
get: function () {
return ecpair_1.networks;
return ecpair_js_1.networks;
},
});
2 changes: 1 addition & 1 deletion src/cjs/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { ECPairFactory as default, ECPairFactory, type Signer, type SignerAsync, type ECPairAPI, type ECPairInterface, type TinySecp256k1Interface, networks, } from './ecpair';
export { ECPairFactory as default, ECPairFactory, type Signer, type SignerAsync, type ECPairAPI, type ECPairInterface, type TinySecp256k1Interface, networks, } from './ecpair.cjs';
2 changes: 1 addition & 1 deletion src/cjs/networks.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as v from 'valibot';
import { NetworkSchema } from './types';
import { NetworkSchema } from './types.cjs';
export type Network = v.InferOutput<typeof NetworkSchema>;
export declare const bitcoin: Network;
export declare const testnet: Network;
2 changes: 1 addition & 1 deletion src/cjs/testecc.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import { TinySecp256k1Interface } from './ecpair';
import { TinySecp256k1Interface } from './ecpair.cjs';
export declare function testEcc(ecc: TinySecp256k1Interface): void;
Loading
Loading