q9f / eth.rb

a straightforward library to build, sign, and broadcast ethereum transactions anywhere you can run ruby.
https://q9f.github.io/eth.rb
Apache License 2.0
200 stars 86 forks source link

ruby-eth to eth conversion #208

Closed JustinSeidl closed 1 year ago

JustinSeidl commented 1 year ago

Our team is converting from the previous ruby-eth gem to this version of the gem.

In our code we had the following: Eth::Util.bin_to_hex(key.sign(message)).bytes.rotate(1).pack('c*')

We are now receiving an error that we don't know how to solve for:

Secp256k1::Error:
       in_hash32 is not 32 bytes in length

Any help on what we are missing to make the conversion?

JustinSeidl commented 1 year ago

@q9f is this something you could assist with?

q9f commented 1 year ago

Yes, what are you trying to do? What is message?

We can sign, personal sign, and sign typed data out of the box:

https://github.com/q9f/eth.rb/wiki/Signatures-&-Recovery

Without any code, I would guess you are looking for personal_sign

JustinSeidl commented 1 year ago

I appreciate the response and assistance. This is a little outside of my comfort zone and our previous developer is not available to assist us with the transition to your version of the gem.

We are working with a payment processor, Sila, who requires authentication of API requests with private ETH keys. https://docs.silamoney.com/docs/process-overview

We wrapped their API in a Ruby gem and have been using it for a little over two years. The error posted above is the response we get after transitioning to your version of the gem (which we desperately need and welcome).

Here is a gist of the file the code lives in. This file generates the headers for the API calls. https://gist.github.com/JustinSeidl/9d67f2ae3c2200bc86daf5842189b57d

q9f commented 1 year ago

Thanks for sharing. I think you have to rewrite the function to:

def sila_personal_sign(key, message)
  key.sign(Eth::Util.keccak256(message))
end

I'm pretty certain the rotation of the v and the conversion to hex is no longer required. Hopefully you have tests :)

q9f commented 1 year ago

Just checked the Sila spec:

  1. A hash is generated from the message (using SHA3/Keccak-256).

I'm fairly confident with the fix above.

If this does not work, you can also add me to a private repo (if you feel confident with this) and I can see if I can patch it.

JustinSeidl commented 1 year ago

@q9f changed the code to your suggestion and the tests passed! This is much cleaner than the previous version.

I can't express how much we appreciate your timely help.

Happy to sponsor the project to show and continue to show my gratitude for your work if you decide to open up sponsorships on this repo. Let me know if you do!

q9f commented 1 year ago

Glad it worked!

There is currently no way to sponsor me, but I appreciate it!