Skip to content

Commit c598736

Browse files
committed
chore: update build process and documentation for browser and npm usage
1 parent de8976a commit c598736

File tree

5 files changed

+103
-26
lines changed

5 files changed

+103
-26
lines changed

.github/workflows/deploy-npm.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,16 @@ jobs:
4545
4646
# Build the package
4747
- name: Build package
48-
run: npm run build
48+
run: npm run make
4949

5050
# Run tests
5151
- name: Run tests
5252
run: npm test
5353

54+
# Run coverage
55+
- name: Run coverage
56+
run: npm run coverage
57+
5458
# Create Git tag
5559
- name: Create Git tag
5660
run: |

README.md

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,98 @@
2222

2323
## Installation
2424

25-
To install `js-ecutils`, you can use npm:
25+
To install `js-ecutils`, you can use npm or include the script directly in your web application:
26+
27+
**Using npm:**
2628

2729
```bash
2830
npm install js-ecutils
2931
```
3032

33+
**Or, for web usage:**
34+
Include the following script in your HTML:
35+
36+
```html
37+
<script src="https://unpkg.com/js-ecutils@latest/dist/web/min.js"></script>
38+
```
39+
3140
## Usage
3241

33-
After installing, you can import the library into your JavaScript project:
42+
After installing the `js-ecutils` library, you can import it into your JavaScript project or use it directly in a browser. Below are the steps for using the library in both environments.
43+
44+
### Using in Node.js
45+
46+
To use the library in a Node.js project, import the required modules as shown below:
3447

3548
```javascript
49+
// Importing necessary classes
3650
const { core: { Point, EllipticCurve } } = require('js-ecutils');
3751

38-
// Example parameters
39-
let p = 23n; // The prime number defining the finite field's order
40-
let a = 1n; // The 'a' coefficient in the curve equation
41-
let b = 1n; // The 'b' coefficient in the curve equation
42-
let G = new Point(0n, 1n);
43-
let n = 28n;
44-
let h = 1n;
52+
// Defining parameters for the elliptic curve
53+
const p = 23n; // The prime number defining the finite field's order
54+
const a = 1n; // The 'a' coefficient in the curve equation
55+
const b = 1n; // The 'b' coefficient in the curve equation
56+
const G = new Point(0n, 1n); // Generator point
57+
const n = 28n; // Order of the point
58+
const h = 1n; // Cofactor
4559

60+
// Creating an instance of the elliptic curve
4661
const curve = new EllipticCurve(p=p, a=a, b=b, G=G, n=n, h=h);
4762

48-
// Define points on the curve
63+
// Defining points on the curve
4964
const point1 = new Point(6n, 19n);
5065
const point2 = new Point(3n, 13n);
5166

52-
// Add the points
67+
// Adding the points
5368
const sum_point = curve.add_points(point1, point2);
5469
console.log(`The sum of the points is (${sum_point.x}, ${sum_point.y}).`);
5570
```
5671

72+
### Using in Browsers
73+
74+
To use the library in a browser, include the JavaScript file in your HTML and utilize the classes available in the global `ecutils` object. Add the script as shown below:
75+
76+
```html
77+
<!DOCTYPE html>
78+
<html lang="en">
79+
<head>
80+
<meta charset="UTF-8">
81+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
82+
<title>Elliptic Curve Cryptography Example</title>
83+
<script src="https://unpkg.com/js-ecutils@latest/dist/web/min.js"></script>
84+
<script>
85+
window.onload = function() {
86+
// Importing necessary classes from the global object
87+
const Point = window.ecutils.core.Point;
88+
const EllipticCurve = window.ecutils.core.EllipticCurve;
89+
90+
// Defining parameters for the elliptic curve
91+
const p = 23n;
92+
const a = 1n;
93+
const b = 1n;
94+
const G = new Point(0n, 1n);
95+
const n = 28n;
96+
const h = 1n;
97+
98+
// Creating an instance of the elliptic curve
99+
const curve = new EllipticCurve(p, a, b, G, n, h);
100+
101+
// Defining points on the curve
102+
const point1 = new Point(6n, 19n);
103+
const point2 = new Point(3n, 13n);
104+
105+
// Adding the points
106+
const sum_point = curve.add_points(point1, point2);
107+
console.log(`The sum of the points is (${sum_point.x}, ${sum_point.y}).`);
108+
};
109+
</script>
110+
</head>
111+
<body>
112+
<h1>Example using js-ecutils Library</h1>
113+
</body>
114+
</html>
115+
```
116+
57117
## API Documentation
58118

59119
### Classes and Methods
@@ -242,11 +302,12 @@ console.log(`The sum of the points is (${sum_point.x}, ${sum_point.y}).`);
242302

243303
## Examples
244304

245-
Here are some examples of using the key exchange protocols and other features of `ecutils`.
305+
Here are some examples of using the key exchange protocols and other features of `js-ecutils`.
246306

247307
### Encoding and Decoding Messages with Koblitz
248308

249309
```js
310+
// Importing necessary classes
250311
const { algorithms: { Koblitz } } = require('js-ecutils');
251312

252313
// Initialize Koblitz with a specific curve
@@ -264,6 +325,7 @@ console.log(decoded_message);
264325
### Digital Signatures with ECDSA
265326

266327
```js
328+
// Importing necessary classes
267329
const { algorithms: { DigitalSignature } } = require('js-ecutils');
268330

269331
// Create a DigitalSignature instance with your private key
@@ -285,6 +347,7 @@ console.log(`Is the signature valid? ${isValid}`);
285347
### Diffie-Hellman Key Exchange
286348

287349
```js
350+
// Importing necessary classes
288351
const { protocols: { DiffieHellman } } = require('js-ecutils');
289352

290353
// Alice's side
@@ -308,6 +371,7 @@ console.log(`Are the shared secrets equal? ${isSharedSecretEqual}`);
308371
### Massey-Omura Key Exchange
309372

310373
```js
374+
// Importing necessary classes
311375
const { algorithms: { Koblitz }, protocols: { MasseyOmura } } = require('js-ecutils');
312376

313377
// Initialize the Koblitz instance for the elliptic curve 'secp192k1'
@@ -361,7 +425,7 @@ console.log(decodedMessage);
361425

362426
## Contributing
363427

364-
Contributions are welcome! If you’d like to contribute to `ecutils`, please follow these steps:
428+
Contributions are welcome! If you’d like to contribute to `js-ecutils`, please follow these steps:
365429

366430
1. Fork the repository.
367431
2. Create a new branch for your feature or bug fix.

package-lock.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "js-ecutils",
3-
"version": "0.0.1-alpha.1",
3+
"version": "0.0.1-alpha",
44
"description": "JavaScript library for Elliptic Curve Cryptography: key exchanges (Diffie-Hellman, Massey-Omura), ECDSA signatures, and Koblitz encoding. Suitable for crypto education and secure systems.",
55
"repository":"https://github.com/isakruas/js-ecutils",
66
"keywords": [
@@ -20,23 +20,23 @@
2020
],
2121
"author": "Isak Ruas",
2222
"license": "MIT",
23-
"main": "dist/index.js",
23+
"main": "dist/npm/index.js",
2424
"files": [
2525
"dist/",
2626
"LICENSE",
2727
"README.md"
2828
],
2929
"scripts": {
3030
"clean": "rm -rf dist/* && rm -rf .babel_cache && rm -rf .jest_cache",
31-
"build": "npm run clean && babel src -d dist;",
31+
"build:npm": "babel src -d dist/npm/",
32+
"build:web": "webpack --mode=production --config webpack.config.js && rm -rf .babel_cache && rm -rf .jest_cache",
3233
"format": "prettier --write \"src/**/*.js\"",
3334
"lint": "npx eslint src/**/*.js",
34-
"test": "jest dist",
35+
"test": "jest dist/npm",
3536
"start": "serve app/frontend/",
36-
"coverage": "jest dist --coverage",
37+
"coverage": "jest dist/npm --coverage",
3738
"tree": "tree --prune -I 'node_modules'",
38-
"pack": "npx webpack --mode=production --config webpack.config.js && find dist -type f ! -name 'bundle.js' -exec rm -f {} + && rm -rf .babel_cache && rm -rf .jest_cache",
39-
"make": "npm run format && npm run lint && npm run build && npm run test && npm run coverage && npm run pack && npm run start"
39+
"make": "npm run format && npm run lint && npm run clean && npm run build:npm && npm run build:web"
4040
},
4141
"devDependencies": {
4242
"@babel/cli": "^7.24.1",

webpack.config.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
const path = require('path')
22

33
module.exports = {
4-
entry: {
5-
bundle: './dist/index.js', // Use o arquivo principal de entrada do seu pacote
6-
},
4+
// entry: {
5+
// bundle: './dist/npm/index.js',
6+
// },
7+
entry: './src/index.js',
78
output: {
8-
filename: '[name].js', // Use o nome 'bundle.js' como padrão
9-
path: path.resolve(__dirname, 'dist'), // Diretório de saída
9+
filename: 'min.js',
10+
path: path.resolve(__dirname, 'dist/web/'),
1011
asyncChunks: true,
1112
library: 'ecutils',
1213
libraryTarget: 'window',
14+
globalObject: 'this',
15+
},
16+
optimization: {
17+
minimize: true,
1318
},
1419
}

0 commit comments

Comments
 (0)