I am wondering if there is a best practice when using web3py to generate an account, i.e private key, and save to to a database. Which format should I use to save it to a database? does web3py have some built in methods that can simplify this process?
1 Answer
I am wondering if there is a best practice when using web3py to generate an account
Web3 does not generate the private key itself. I uses a client's newAccount's RPC call to generate one, but that's all it has. If you want to generate a private key yourself you'll have to do it manually. I'm personally using pywallet in my projects, and I recommend you to take a look.
Which format should I use to save it to a database?
Firstly, I don't recommend you saving the private key in the database. Better to secure your private key as a seed or JSON file and derive as many private keys as needed. But if you really have to store the private keys in the database, why not simply in hexadecimal format? For instance:
>>> b'my super secret private key'.hex()
'6d79207375706572207365637265742070726976617465206b6579'
That being said, I'd at least encrypt those bytes with symmetric encryption, i.e. a strong password. If the result is too long for your database and you plan to save some space, you could store it as base64-encoded.
does web3py have some built in methods that can simplify this process?
Short answer: no.