-
Notifications
You must be signed in to change notification settings - Fork 944
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
Encrypt deployer PK on .env file (when using hardhat) #1008
Conversation
We can encrypt the PK using AES from the crypto library and save the encrypted PK. Then, when the user runs a yarn account or yarn deploy, we must ask for the password first, decrypt the PK, and set the DEPLOYER_PRIVATE_KEY env var with the decrypted PK. Am I missing something? |
I think if you read carefully what I wrote and the current code, you'll see that what are you saying is the same thing that I'm trying. Most of the stuff is already working (password encryption/decryption, yarn generate, yarn account), but there is an issue with how hardhat config works (+ no await in the config). You can check it out if you try it locally. Feel free to suggest any code if you make it work! I'm currently trying an extra script to be run before any hardhat command to make it work (not the most fancy way) and load the env var. |
Yeah! Sorry, you are right, it's the same.
Will do!
Yes, this was what I was thinking about. I think this is the easier way because it should be transparent to hardhat what we are doing. |
I think this might be the only way... And maybe that's why https://github.com/smartcontractkit/env-enc/ has a weird workflow (they couldn't find a way around hardhat config) I'll update in a bit. Thanks Damu!! |
Update: pushed 3ae16c4 and it seems to be working. This is the simplest way that I found to load the decrypted PK in memory and run everything as usual. I think So everything runs as always, but when you deploy it check if the network is specified. If so, decrypt the PK, load it into the env, spaw a process and run the deploy command as usual. Added a ToDo on the top PR comment (yarn account:import) Please test, and let me know if you find a better way or if I'm missing something. Also @Pabl0cks , could you check on Windows? |
Tested it, works great to me! I'll play with it next days but for now I think it's a good solution! |
Tested and it's working great!! This will force to use the encrypted PK, right? If I want to use a wallet from a PK I won't be able to do it, right? |
Not yet! But I plan to add a I'll tackle it (+ little tweaks) next week! Also open to any other feedback. Thanks!! |
Yeah, sorry again, it was there on the PR text from the beginning. I think I didn't sleep well last night :-) |
Ok, this is ready to review! Pushed the Please test and let me know if I'm missing something. Left some comments in the code too A couple of things that were on my mind.
|
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.
Works great for me!
Added some nipticks/comments, but can be ignored or changed later. Thanks!
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.
It's working great!! And the code looks good to me.
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.
Awesome job @carletex !! This looks great!
On windows, as usual, there is some weird stuff, but I think it's easily fixed!
yarn account
works as expected. My git bash is a bit weird because it shows the raw password when you type it, but I will try on my laptop (the console on my desktop it's always been a bit buggy). On Windows Powershell the password is always hided 👌yarn deploy
throws error, but applied this solution and works great.
The error was:
Error: spawn hardhat ENOENT
at Process.ChildProcess._handle.onexit (node:internal/child_process:286:19)
at onErrorNT (node:internal/child_process:484:16)
at processTicksAndRejections (node:internal/process/task_queues:82:21) {
errno: -4058,
code: 'ENOENT',
syscall: 'spawn hardhat',
path: 'hardhat',
spawnargs: [ 'deploy' ]
}
Adding shell
to both spawn
in runHardhatDeployWithPK.ts
when it's windows, fixed it, and hopefully doesn't break the rest of OS behaviours.
stdio: "inherit",
env: process.env,
shell: process.platform === 'win32'
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.
Tysm @carletex this is great! And works nicely, tested with different cases like --tags
, import pk + generating and deployments in general everything seems to work smooth!
Hopefully it works nicely on windows and then update the docs 🙌
I knew it was going to fail on Windows! haha Thanks all for the review! Pushed c8b08a7. Thanks @Pabl0cks! It works for me. Let's see if it works for the MacOS guys too :) There are a couple of open things:
|
TODO:
yarn account:import
Update 1: Check #1008 (comment).
OG comment:
We have been talking about this for a while, and I'm drafting this PR to start the discussion and tinker. I think Hardhat v3 will come with a proper keystore (as foundry) but for now let's see if we can come with an easy solution.
I've been trying a bunch of things, but I've been finding roadblocks (maybe it's a good excuse to do our own simple package :D)
Let's be open about other options too... but I'm here presenting a simpler approach (which is not working yet, lol)
Goal
My goal is to have a simple password encryption system, so I went for the easies option: keep using the same
.env
file but store the encrypted account.yarn generate
prompts for a password* (+ repeat to validate), generates a random account as before, and saves the encrypted data** on.env
✔️
yarn account
prompts for a password and shows the account info as before🔴
hardhat.config.ts
needs to be able to prompt for the password (at least when --network is not hardhat) so it's able to parse the PK. This is not working at the moment (so I removed `process.env.DEPLOYER_PRIVATE_KEY for now, so we can test the other commands). I'll continue to tinker, but open to ideas!! (so far, unsuccessful haha)*
I'm using@inquirer/password
since it's really small**
I'm using wallet encrypt from ethers since we are already using ethers.We'd also need a
yarn account:import
in case you want to import your PK (prompts for your PK and password)What do you guys think?