Skip to content

Commit

Permalink
refactor(keypair): extract keypair generation to separate module
Browse files Browse the repository at this point in the history
- Move keypair generation logic to utils/keypair.js
- Create generateAndSaveKeyPair function with configurable output path
- Update relay.js to use new module
- Add proper error handling and return values
- Improve code organization and separation of concerns

Makes keypair generation reusable and easier to maintain
  • Loading branch information
silkroadnomad committed Jan 20, 2025
1 parent 9bd9df3 commit eddf1bd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
2 changes: 1 addition & 1 deletion relay/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libp2p-relay",
"version": "0.12.27",
"version": "0.12.28",
"private": true,
"scripts": {
"start:no-restart": "node src/relay.js",
Expand Down
32 changes: 3 additions & 29 deletions relay/src/relay.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import 'dotenv/config';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import fs from 'fs/promises';
// External libraries
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
import {
generateKeyPair,
privateKeyToProtobuf,
privateKeyFromProtobuf,
} from '@libp2p/crypto/keys';
import { unixfs } from '@helia/unixfs';
import { CID } from 'multiformats/cid';

Expand All @@ -26,6 +18,7 @@ import { createHttpServer } from './httpServer.js';
import { createNode } from './nodeFactory.js';
import { setupPubsub } from './pubsubHandler.js';
import TipWatcher from './pinner/tipWatcher.js';
import { generateAndSaveKeyPair } from './utils/keypair.js';

export const CONTENT_TOPIC =
process.env.CONTENT_TOPIC || '/doichain-nfc/1/message/proto';
Expand Down Expand Up @@ -79,28 +72,9 @@ const argv = yargs(hideBin(process.argv))

if (argv['generate-keypair']) {
try {
const newKeyPair = await generateKeyPair('Ed25519');
const protobufKey = privateKeyToProtobuf(newKeyPair);
const privateKeyHex = uint8ArrayToString(protobufKey, 'hex');

console.log('New private key generated. Add this to your .env file:');
console.log(`RELAY_PRIVATE_KEY=${privateKeyHex}`);

// Optionally, write to a file
await fs.writeFile(
'./.env.privateKey',
`RELAY_PRIVATE_KEY=${privateKeyHex}`,
'utf8'
);
console.log('Private key has been saved to .env.privateKey');

// Verify the key can be correctly parsed back
privateKeyFromProtobuf(uint8ArrayFromString(privateKeyHex, 'hex'));
console.log('Verified: Key can be correctly parsed back from hex format');

process.exit(0); // Exit after generating the keypair
await generateAndSaveKeyPair();
process.exit(0);
} catch (error) {
console.error('Error generating keypair:', error);
process.exit(1);
}
}
Expand Down
31 changes: 31 additions & 0 deletions relay/src/utils/keypair.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { generateKeyPair, privateKeyToProtobuf, privateKeyFromProtobuf } from '@libp2p/crypto/keys';
import { fromString as uint8ArrayFromString, toString as uint8ArrayToString } from 'uint8arrays';
import fs from 'fs/promises';

export async function generateAndSaveKeyPair(outputPath = './.env.privateKey') {
try {
const newKeyPair = await generateKeyPair('Ed25519');
const protobufKey = privateKeyToProtobuf(newKeyPair);
const privateKeyHex = uint8ArrayToString(protobufKey, 'hex');

console.log('New private key generated. Add this to your .env file:');
console.log(`RELAY_PRIVATE_KEY=${privateKeyHex}`);

// Write to specified file
await fs.writeFile(
outputPath,
`RELAY_PRIVATE_KEY=${privateKeyHex}`,
'utf8'
);
console.log(`Private key has been saved to ${outputPath}`);

// Verify the key can be correctly parsed back
privateKeyFromProtobuf(uint8ArrayFromString(privateKeyHex, 'hex'));
console.log('Verified: Key can be correctly parsed back from hex format');

return privateKeyHex;
} catch (error) {
console.error('Error generating keypair:', error);
throw error;
}
}

0 comments on commit eddf1bd

Please sign in to comment.