codex-storage/nim-ethers

How to work with private keys

Closed this issue · 13 comments

In the testmodule examples I cannot find a single example of using a private key to get and use a signer. There also doesn't appear to be any functions similar to ether.js wallet features. Is this currently not supported?

That's correct, the wallet isn't there yet.
We do accept pull requests ;)

Just to let you know, I have started to create the Wallet functionality but it is not going to have every feature of ethers.js and it will probably take some time for me to have anything if I can even do it.
I currently do not plan to support Mnemoniks and keystores and I ran into some problems with signing Messages so I will have to see.
I plan to use the implementation of nim-web3 to sign Transactions but I will only support Legacy and Eip1559 Transactions

That sounds great! Thanks for taking this on @JackieWeiss! Let me know if you need anything from me, I'm happy to help. Feel free to create a draft PR with your changes so far, if you feel comfortable doing so.

So I currently have a basic wallet working. I can create Wallets from private keys, with or without a provider or with random private keys and correctly create and sign and send manual transactions. The only thing I won't be able to do alone is use the current call syntax for transactions. I am not familiar enough with macros to change the "addContractCall" function to use a different "send" function when the signer is a wallet and pass a Transaction into the "send" so it actually uses the parameters you want it to use instead of creating a default populated Transaction. For that I would require your help. I don't yet have a public repository for my changes but I will do that soon so you can look at my current changes.

Sounds like you made some nice progress! I did not anticipate any changes in the addContractCall macro, because it uses a Signer to populate and send the transaction. When the Wallet implements the Signer interface (like it does in ethers.js) it should just work, but maybe I'm missing something.

You are right in that it does work with the wallet type inheriting from the Signer but I don't see any way to pass a custom transaction to it except via "populateTransaction" and that seems to be a rather strange way of doing it. If I explicitly create a Transaction with a custom nonce, gasPrice and gasLimit but only require the encoded calldata I don't see a good way to do this currently. Calling the function normally would create a Transaction with the correct Address and calldata via "createTransaction" and then use the wallet "populateTransaction" to fill all the other fields. How do make sure my custom values go into that Transaction?
The problem is that I cannot find a single example of this in the ethers.js docs, only in the javascript and python web3 libraries. So is this not something this library is supposed to support?

Right, so if I understand correctly you want to call a contract function with custom nonce, gasPrice and gasLimit. In ethers.js you can do this by specifying these values using the overrides parameter.

For example:

await erc20.transfer(recipient, amount, { gasPrice: x, gasLimit: y })

I have not yet implemented this mechanism in Nim.

Yes exactly, my initial idea was passing an additional Transaction parameter, that defaults to an empty Transaction, to every call so you can set every value you want before calling the function. Something like

let tx = Transaction(nonce: some 50.u256)
await writableToken.transfer(address, value, tx)

But as far as I understand the library I would have to change the "addContractCall" function to include that and the "send" to use that additional parameter. Just that I don't know how to change the "addContractCall" function because I have never worked with macros before.

That's a good idea. I can't make any promises about a timeline, but I'll do my best to add the extra parameter to contract calls.

Here's a first version of the additional Transaction parameter: #27. Does this work for you @JackieWeiss?

I have just published my own Wallet branch with your new changes added. I think generally it already should work, just some stuff is still missing, the tests are probably not complete/badly written and as this is my first project, the overall code quality may not be good.
The tests all work locally on my machine but feel free to test them as well to make sure they really work.
Also please suggest any changes still required.

That's great! Could you perhaps create a pull request with these changes? This allows me to suggest changes and make comments more easily.

Implemented in #28.