-
Notifications
You must be signed in to change notification settings - Fork 9
feat: create account if it doesnt exists #214
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 5 commits into
main
from
edinaldo/dxp-99-feat-create-account-if-it-doesnt-exists
Apr 24, 2025
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b4bba7f
chore: updating versions
epsjunior 8bb848e
feat: create an account if it doesnt exists
epsjunior 03c6d22
Merge branch 'main' of github.com:yeagerai/genlayer-cli
epsjunior 26ec384
Merge branch 'main' into edinaldo/dxp-99-feat-create-account-if-it-do…
epsjunior 6eec877
feat: adding getclient to baseactions
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
|---|---|---|
| @@ -1,42 +1,22 @@ | ||
| import { writeFileSync, existsSync } from "fs"; | ||
| import { ethers } from "ethers"; | ||
| import { BaseAction } from "../../lib/actions/BaseAction"; | ||
|
|
||
| export interface CreateKeypairOptions { | ||
| output: string; | ||
| overwrite: boolean; | ||
| } | ||
|
|
||
| export class KeypairCreator extends BaseAction{ | ||
|
|
||
| export class KeypairCreator extends BaseAction { | ||
| constructor() { | ||
| super() | ||
| super(); | ||
| } | ||
|
|
||
| createKeypairAction(options: CreateKeypairOptions) { | ||
| try { | ||
| this.startSpinner(`Creating keypair...`); | ||
| const outputPath = this.getFilePath(options.output); | ||
|
|
||
| if(existsSync(outputPath) && !options.overwrite) { | ||
| this.failSpinner( | ||
| `The file at ${outputPath} already exists. Use the '--overwrite' option to replace it.` | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| const wallet = ethers.Wallet.createRandom(); | ||
| const keypairData = { | ||
| address: wallet.address, | ||
| privateKey: wallet.privateKey, | ||
| }; | ||
|
|
||
| writeFileSync(outputPath, JSON.stringify(keypairData, null, 2)); | ||
|
|
||
| this.writeConfig('keyPairPath', outputPath); | ||
| this.succeedSpinner(`Keypair successfully created and saved to: ${outputPath}`); | ||
| this.keypairManager.createKeypair(options.output, options.overwrite); | ||
| this.succeedSpinner(`Keypair successfully created and saved to: ${options.output}`); | ||
| } catch (error) { | ||
| this.failSpinner("Failed to generate keypair:", error); | ||
| this.failSpinner("Failed to generate keypair", 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,43 @@ | ||
| import { writeFileSync, existsSync, readFileSync } from "fs"; | ||
| import { ethers } from "ethers"; | ||
| import path from "path"; | ||
| import { ConfigFileManager } from "../config/ConfigFileManager"; | ||
|
|
||
| export class KeypairManager extends ConfigFileManager { | ||
| constructor() { | ||
| super(); | ||
| } | ||
|
|
||
| getPrivateKey(): string | undefined { | ||
| const keypairPath = this.getConfigByKey("keyPairPath"); | ||
|
|
||
| if (!keypairPath || !existsSync(keypairPath)) { | ||
| return "" | ||
| } | ||
|
|
||
| const keypairData = JSON.parse(readFileSync(keypairPath, "utf-8")); | ||
|
|
||
| if (!keypairData.privateKey) { | ||
| return "" | ||
| } | ||
|
|
||
| return keypairData.privateKey; | ||
| } | ||
|
|
||
| createKeypair(outputPath = "./keypair.json", overwrite: boolean = false): void { | ||
| const finalOutputPath = this.getFilePath(outputPath); | ||
|
|
||
| if(existsSync(finalOutputPath) && !overwrite) { | ||
| throw new Error(`The file at ${finalOutputPath} already exists. Use the '--overwrite' option to replace it.`); | ||
| } | ||
|
|
||
| const wallet = ethers.Wallet.createRandom(); | ||
| const keypairData = { | ||
| address: wallet.address, | ||
| privateKey: wallet.privateKey, | ||
| }; | ||
|
|
||
| writeFileSync(finalOutputPath, JSON.stringify(keypairData, null, 2)); | ||
| this.writeConfig('keyPairPath', finalOutputPath); | ||
| } | ||
| } |
This file was deleted.
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@epsjunior Can we implement this getClient method in the base action and reuse it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds better, I'll do it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done