-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(keypair): extract keypair generation to separate module
- 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
1 parent
9bd9df3
commit eddf1bd
Showing
3 changed files
with
35 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |