-
Notifications
You must be signed in to change notification settings - Fork 13
feat: implement os keychain support for secure private key storage #243
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
Merged
cristiam86
merged 21 commits into
main
from
dxp-542-implement-os-keychain-support-for-secure-private-key-storage
Sep 1, 2025
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d8dc99c
feat: adding keystar to external config
epsjunior b85e1c7
feat: adding keystar to external config prod
epsjunior 2354990
feat: new keytar lib
epsjunior 6e167f6
feat: new lock and unlock commands
epsjunior 4c0c554
feat: new baseaction keychain logic
epsjunior 222ca70
feat: new Keychain manager
epsjunior 4aac02c
chore: moving actions files
epsjunior b086b22
feat: new lock action
epsjunior 4f3ed98
feat: new unlock action
epsjunior 0228a6d
chore: removing unused temp files logic
epsjunior d862751
test: removing temp logic from config gile manager
epsjunior e61205c
test: removing temp files logic from base action
epsjunior ec4521d
test: keygen command
epsjunior 5a862d8
test: lock command
epsjunior e56be9c
test: unlock command
epsjunior a89dc48
test: keychain manager tests
epsjunior 68623d3
chore: libsrecret note
epsjunior 2c1dfef
fix: github workflow
epsjunior b68183c
fix: tests failing in headless environments
epsjunior d1572f9
chore: is valida keystore format validation recommended by coderabbit
epsjunior 508a488
test: adding valid key format validation to unit test
epsjunior File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 { BaseAction } from "../../lib/actions/BaseAction"; | ||
|
|
||
| export class LockAction extends BaseAction { | ||
| async execute(): Promise<void> { | ||
| this.startSpinner("Checking keychain availability..."); | ||
|
|
||
| const keychainAvailable = await this.keychainManager.isKeychainAvailable(); | ||
| if (!keychainAvailable) { | ||
| this.failSpinner("OS keychain is not available. This command requires a supported keychain (e.g. macOS Keychain, Windows Credential Manager, or GNOME Keyring)."); | ||
| return; | ||
| } | ||
|
|
||
| this.setSpinnerText("Checking for cached private key..."); | ||
|
|
||
| const hasCachedKey = await this.keychainManager.getPrivateKey(); | ||
| if (!hasCachedKey) { | ||
| this.succeedSpinner("Wallet is already locked (no cached key found in OS keychain)."); | ||
| return; | ||
| } | ||
|
|
||
| this.setSpinnerText("Removing private key from OS keychain..."); | ||
|
|
||
| try { | ||
| await this.keychainManager.removePrivateKey(); | ||
|
|
||
| this.succeedSpinner("Wallet locked successfully! Your private key has been removed from the OS keychain."); | ||
| } catch (error) { | ||
| this.failSpinner("Failed to lock wallet.", error); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,41 @@ | ||
| import { BaseAction } from "../../lib/actions/BaseAction"; | ||
| import { readFileSync, existsSync } from "fs"; | ||
| import { ethers } from "ethers"; | ||
|
|
||
| export class UnlockAction extends BaseAction { | ||
| async execute(): Promise<void> { | ||
| this.startSpinner("Checking keychain availability..."); | ||
|
|
||
| const keychainAvailable = await this.keychainManager.isKeychainAvailable(); | ||
| if (!keychainAvailable) { | ||
| this.failSpinner("OS keychain is not available. This command requires a supported keychain (e.g. macOS Keychain, Windows Credential Manager, or GNOME Keyring)."); | ||
| return; | ||
| } | ||
|
|
||
| this.setSpinnerText("Checking for existing keystore..."); | ||
|
|
||
| const keypairPath = this.getConfigByKey("keyPairPath"); | ||
| if (!keypairPath || !existsSync(keypairPath)) { | ||
| this.failSpinner("No keystore file found. Please create a keypair first using 'genlayer keygen create'."); | ||
| return; | ||
| } | ||
|
|
||
| const keystoreData = JSON.parse(readFileSync(keypairPath, "utf-8")); | ||
| if (!this.isValidKeystoreFormat(keystoreData)) { | ||
| this.failSpinner("Invalid keystore format. Expected encrypted keystore file."); | ||
| return; | ||
| } | ||
|
|
||
| this.stopSpinner(); | ||
|
|
||
| try { | ||
| const password = await this.promptPassword("Enter password to decrypt keystore:"); | ||
| const wallet = await ethers.Wallet.fromEncryptedJson(keystoreData.encrypted, password); | ||
|
|
||
| await this.keychainManager.storePrivateKey(wallet.privateKey); | ||
| this.succeedSpinner("Wallet unlocked successfully! Your private key is now stored securely in the OS keychain."); | ||
| } catch (error) { | ||
| this.failSpinner("Failed to unlock wallet.", error); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,29 @@ | ||
| import {default as keytar} from 'keytar'; | ||
|
|
||
| export class KeychainManager { | ||
| private static readonly SERVICE = 'genlayer-cli'; | ||
| private static readonly ACCOUNT = 'default-user'; | ||
|
|
||
| constructor() {} | ||
|
|
||
| async isKeychainAvailable(): Promise<boolean> { | ||
| try { | ||
| await keytar.findCredentials('test-service'); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| async storePrivateKey(privateKey: string): Promise<void> { | ||
| return await keytar.setPassword(KeychainManager.SERVICE, KeychainManager.ACCOUNT, privateKey); | ||
| } | ||
|
epsjunior marked this conversation as resolved.
|
||
|
|
||
| async getPrivateKey(): Promise<string | null> { | ||
| return await keytar.getPassword(KeychainManager.SERVICE, KeychainManager.ACCOUNT); | ||
| } | ||
|
|
||
| async removePrivateKey(): Promise<boolean> { | ||
| return await keytar.deletePassword(KeychainManager.SERVICE, KeychainManager.ACCOUNT); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.