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

Add module: "key management" #9

Merged
merged 20 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
aa0da02
Add key management module
abhi3700 May 30, 2024
5e0641d
Add tests along with keyToPem function with examples
abhi3700 May 31, 2024
b87753e
Add more functions to auto-id & its arch diagram
abhi3700 Jun 3, 2024
c6282db
Add new functions & organize tests removing redundant code snippets
abhi3700 Jun 3, 2024
8ac9d7b
Add all the functions of key module & also added a few tests
abhi3700 Jun 3, 2024
ff5d538
Add tests for saveKey function
abhi3700 Jun 4, 2024
40cd653
Add tests for loading private/public keys
abhi3700 Jun 4, 2024
c2a154e
Add some examples for showing usage of functions of key management mo…
abhi3700 Jun 4, 2024
2ad656c
Add a README for Auto ID package
abhi3700 Jun 4, 2024
2b4ef3e
Removed .vscode from ignoring & Add index.ts
abhi3700 Jun 4, 2024
7613079
shift the relevant package to dependencies
abhi3700 Jun 4, 2024
80ab436
Removed the examples as it's already included in tests
abhi3700 Jun 4, 2024
53733ed
Remove examples' scripts from package.json
abhi3700 Jun 4, 2024
fdbe0ac
Remove unncessary inclusion of tests/ folder
abhi3700 Jun 4, 2024
c446b07
Merge branch 'main' into add-key-module
abhi3700 Jun 4, 2024
67d9744
Merge branch 'main' of https://github.com/subspace/auto-sdk into add-…
abhi3700 Jun 5, 2024
d679460
Modify saveKey function & its tests of auto-id package
abhi3700 Jun 5, 2024
1d9cf2f
Incorporate save function from auto-utils into Load key function & tests
abhi3700 Jun 5, 2024
f7c1adc
Modify load key functions considering the dynamic read function added…
abhi3700 Jun 5, 2024
d825e21
Update lock file
abhi3700 Jun 5, 2024
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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Thumbs.db
*.temp

# IDE/editor files
.vscode/
abhi3700 marked this conversation as resolved.
Show resolved Hide resolved
.idea/
*.swp
*.swo
Expand All @@ -33,3 +34,9 @@ tsconfig.tsbuildinfo

# Environment variables
.env

# ignore private key certs
*.pem

# ignore .yarn
.yarn/
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
],
"scripts": {
"build": "yarn workspaces run build",
"clean": "rm -rf dist",
"test": "yarn workspaces run test"
},
"devDependencies": {
"eslint": "^8.57.0",
"prettier": "^3.2.5",
"typescript": "^5.4.5"
}
}
}
25 changes: 25 additions & 0 deletions packages/auto-id/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Auto ID

## Build

```sh
yarn
yarn build
```

## Test

```sh
yarn test
```

## Examples

```sh
yarn eg-1
yarn eg-2
yarn eg-3
yarn eg-4
yarn eg-5
yarn eg-6
```
10 changes: 10 additions & 0 deletions packages/auto-id/examples/eg1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Simple RSA key pair generation
*/

import { generateRsaKeyPair, loadPrivateKey, saveKey } from '../src/keyManagement'

// Example usage:
const [privateKeyRsa, publicKeyRsa] = generateRsaKeyPair()
console.log(`Private key: ${privateKeyRsa}`)
console.log(`Public key: ${publicKeyRsa}`)
10 changes: 10 additions & 0 deletions packages/auto-id/examples/eg2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Simple Ed25519 key pair generation
*/

import { generateEd25519KeyPair, loadPrivateKey, saveKey } from '../src/keyManagement'

// Example usage:
const [privateKey, publicKey] = generateEd25519KeyPair()
console.log(`Private key: ${privateKey}`)
console.log(`Public key: ${publicKey}`)
23 changes: 23 additions & 0 deletions packages/auto-id/examples/eg3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
This example demonstrates how to generate a RSA key pair and export it as PEM format.
The private key can be exported with or without password.
*/

import { createPrivateKey, createPublicKey } from 'crypto'
import { generateRsaKeyPair, keyToPem } from '../src/keyManagement'

const [privateKeyRsa, publicKeyRsa] = generateRsaKeyPair()
console.log(`Private key: ${privateKeyRsa}`)
console.log(`Public key: ${publicKeyRsa}`)
console.log(`========================`)

const privateKeyObjectRsa = createPrivateKey({
key: privateKeyRsa,
format: 'pem', // Input can still be PEM
})

const publicKey2 = createPublicKey(privateKeyObjectRsa)

console.log(keyToPem(privateKeyObjectRsa)) // Export without password
console.log(keyToPem(privateKeyObjectRsa, 'subspace')) // Export with password
console.log(keyToPem(publicKey2)) // Export public key
23 changes: 23 additions & 0 deletions packages/auto-id/examples/eg4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
This example demonstrates how to generate a Ed25519 key pair and export it as PEM format.
The private key can be exported with or without password.
*/

import { createPrivateKey, createPublicKey } from 'crypto'
import { generateEd25519KeyPair, keyToPem } from '../src/keyManagement'

const [privateKeyEd25519, publicKeyEd25519] = generateEd25519KeyPair()
console.log(`Private key: ${privateKeyEd25519}`)
console.log(`Public key: ${publicKeyEd25519}`)
console.log(`========================`)

const privateKey2 = createPrivateKey({
key: privateKeyEd25519,
format: 'pem', // Input can still be PEM
})

const publicKey2 = createPublicKey(privateKeyEd25519)

console.log(keyToPem(privateKey2)) // Export without password
console.log(keyToPem(privateKey2, 'subspace')) // Export with password
console.log(keyToPem(publicKey2)) // Export public key
38 changes: 38 additions & 0 deletions packages/auto-id/examples/eg5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
This example demonstrates how to generate a RSA key pair and export it as PEM format.
The private key can be exported with or without password.
*/

import { createPrivateKey, createPublicKey } from 'crypto'
import { generateRsaKeyPair, keyToPem, loadPrivateKey, saveKey } from '../src/keyManagement'

const [privateKeyRsa, publicKeyRsa] = generateRsaKeyPair()
console.log(`Private key: ${privateKeyRsa}`)
console.log(`Public key: ${publicKeyRsa}`)
console.log(`========================`)

const privateKeyObject = createPrivateKey({
key: privateKeyRsa,
format: 'pem', // Input can still be PEM
})

const publicKeyKeyObject = createPublicKey(privateKeyObject)

console.log(keyToPem(privateKeyObject)) // Export without password
console.log(keyToPem(privateKeyObject, 'subspace')) // Export with password
console.log(keyToPem(publicKeyKeyObject)) // Export public key

// w/o password
saveKey(privateKeyObject, './privateKey.pem')
.then(() => console.log('Key saved successfully'))
.catch((err) => console.error('Error saving key:', err))
// with password i.e. encrypted key
saveKey(privateKeyObject, './privateKeyPassword.pem', 'subspace')
.then(() => console.log('Encrypted Key (with password) saved successfully'))
.catch((err) => console.error('Error saving key:', err))

loadPrivateKey('./privateKey.pem')
.then((key) => {
console.log(`Private keys match: ${key.toString() === privateKeyObject.toString()}`)
})
.catch((err) => console.error('Error loading key:', err))
35 changes: 35 additions & 0 deletions packages/auto-id/examples/eg6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
This example demonstrates how to generate a Ed25519 key pair and export it as PEM format.
The private key can be exported with or without password.
*/

import { createPrivateKey, createPublicKey } from 'crypto'
import { generateEd25519KeyPair, keyToPem, saveKey } from '../src/keyManagement'

const [privateKeyEd25519, publicKeyEd25519] = generateEd25519KeyPair()
console.log(`Private key: ${privateKeyEd25519}`)
console.log(`Public key: ${publicKeyEd25519}`)
console.log(`========================`)

const privateKeyObject = createPrivateKey({
key: privateKeyEd25519,
format: 'pem', // Input can still be PEM
})

const publicKeyObject = createPublicKey(privateKeyEd25519)

console.log(keyToPem(privateKeyObject)) // Export without password
console.log(keyToPem(privateKeyObject, 'subspace')) // Export with password
console.log(keyToPem(publicKeyObject)) // Export public key

// w/o password
saveKey(privateKeyObject, './privateKey.pem')
.then(() => console.log('Key saved successfully'))
.catch((err) => console.error('Error saving key:', err))
// with password i.e. encrypted key
saveKey(privateKeyObject, './privateKeyPassword.pem', 'subspace')
.then(() => console.log('Key (with password) saved successfully'))
.catch((err) => console.error('Error saving key:', err))

// const loadedPrivateKey = loadPrivateKey('./privateKey.pem')
// console.log(`Private keys match: ${loadedPrivateKey.toString() === privateKey.toString()}`)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/auto-id/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
preset: 'ts-jest',
}
18 changes: 16 additions & 2 deletions packages/auto-id/package.json
abhi3700 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@
"version": "0.1.0",
"main": "dist/index.js",
"scripts": {
"build": "tsc"
"build": "tsc",
"clean": "rm -rf dist",
"eg-1": "tsc && node dist/examples/eg1.js",
abhi3700 marked this conversation as resolved.
Show resolved Hide resolved
"eg-2": "tsc && node dist/examples/eg2.js",
"eg-3": "tsc && node dist/examples/eg3.js",
"eg-4": "tsc && node dist/examples/eg4.js",
"eg-5": "tsc && node dist/examples/eg5.js",
"eg-6": "tsc && node dist/examples/eg6.js",
"format": "prettier --write \"src/**/*.ts\"",
"test": "jest"
},
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/node": "^20.12.12",
"jest": "^29.7.0",
"ts-jest": "^29.1.4",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
}
}
}
4 changes: 3 additions & 1 deletion packages/auto-id/src/index.ts
abhi3700 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const helloWorld = () => {
// TODO: Complete once all the modules are ready
/* export const helloWorld = () => {
console.log('Hello World');
};
*/
Loading