Skip to content

Commit

Permalink
terminal program created
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes67890 committed Dec 8, 2022
1 parent c3925ed commit 7f02308
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 14 deletions.
28 changes: 15 additions & 13 deletions package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^14.4.3",
"@types/jest": "^29.2.0",
"@types/node": "^18.11.0",
"@types/react": "^18.0.21",
"@types/react-dom": "^18.0.6",
"bn.js": "^5.2.1",
"buffer": "^6.0.3",
"chalk": "^4.1.2",
"crypto-browserify": "^3.12.0",
"process": "^0.11.10",
"react": "^18.2.0",
Expand All @@ -24,6 +24,7 @@
},
"scripts": {
"start": "react-app-rewired start",
"cdm": "ts-node ./src/cdm.ts",
"build": "webpack --mode=production --node-env=production",
"test": "jest",
"eject": "react-scripts eject",
Expand Down Expand Up @@ -51,12 +52,14 @@
},
"devDependencies": {
"@types/bn.js": "^5.1.1",
"@types/node": "^18.11.11",
"@webpack-cli/generators": "^2.5.0",
"hash.js": "^1.1.7",
"react-app-rewired": "^2.2.1",
"tailwindcss": "^3.1.8",
"ts-jest": "^29.0.3",
"ts-loader": "^9.2.5",
"tslib": "^2.4.1",
"typescript": "^4.9.3",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0"
Expand Down
51 changes: 51 additions & 0 deletions src/cdm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { EC } from './curve/EDDSA/EC';
import KeySet from "./curve/EDDSA/keyGeneration";
import Signature from "./curve/EDDSA/signature";
import { secp256k1 } from './curve/curvesDefined';
import { hashMsgSHA256 } from './util';
import chalk from 'chalk';

/**
* Run the CDM
* To run this script: `npm run cdm`
*/

function CDMrunECDSA() {
console.log(chalk.green("Elliptic Curve Digital Signature Algorithm"));
console.log(chalk.gray("Curve: ", secp256k1.name, "\n"));

// Create and initialize EC context & signature
const ec = new EC(secp256k1);
const signature = new Signature(secp256k1);

// Generate key pair
const keySet = new KeySet(secp256k1);
const {privateKey, publicKey } = keySet;

console.log("Private key: ", `(${privateKey.byteLength()} bytes)`, "\n", chalk.redBright(privateKey.toString("hex"),"\n"));
console.log("Public key: ", `(${publicKey.byteLength()} bytes)`, "\n",
chalk.blueBright( "X: ", ec.decompressPoint(publicKey).x.toString("hex"), "\n"),
chalk.redBright("Y: ", ec.decompressPoint(publicKey).y.toString("hex"), "\n"));

// Sign message
const message = "Hello World!";
console.log("Message: ",chalk.cyan(message), "\n");
// Hashed message with SHA256.
console.log("Message hash (SHA256):", `(${hashMsgSHA256(message).byteLength()} bytes)`, "\n", `${hashMsgSHA256(message).toString('hex')}`, "\n");


const signatureValue = signature.signMsg(message, ec.decompressPoint(privateKey)); // Decopmress private key to match signature function.
console.log(`Signature:`, signatureValue, "\n"); // Return signature {r,s} as hex string

// Verify signature
const verify = signature.verifyMsg(message, signatureValue, ec.decompressPoint(publicKey)); // Decopmress public key to match signature function.
console.log("Verify: ", verify, "\n"); // Return true or false
}




CDMrunECDSA();



7 changes: 7 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"ts-node": {
// these options are overrides used only by ts-node
"compilerOptions": {
"module": "commonjs"
}
},
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"moduleResolution": "node",
Expand All @@ -9,5 +15,6 @@
"allowJs": true,
"jsx": "react"
},
"include": ["src/**/*"],
"files": ["src/index.tsx"]
}

0 comments on commit 7f02308

Please sign in to comment.