EIP-721 signature generation off chain using ruby and validation on chain using solidity

Viewed 159

I am working on a project where I need to airdrop tokens to our users. In backend the amount of tokens dropped for an address is identified. For validation of address and amount of tokens while claiming I need to have some validation in place. Similar to how it's done on these two contracts - https://etherscan.io/address/0xedfccec033521cbfb3a4aaa86824aaa6747ea35f#code https://etherscan.io/address/0x3b484b82567a09e2588a13d54d032153f0c0aee0#code

The question is how do i generate the eip-712 signatures on ruby on rails backend, so that it would be verifiable on smart contract

1 Answers

You can use the eth gem.

require "eth"

Create some EIP-712 typed data:

typed_data = {
  :types => {
    :EIP712Domain => [],
    :Message => [
      { :name => "data", :type => "string" },
    ],
  },
  :primaryType => "Message",
  :domain => {},
  :message => {
    :data => "test",
  },
}
# => {:types=>{:EIP712Domain=>[], :Message=>[{:name=>"data", :type=>"string"}]}, :primaryType=>"Message", :domain=>{}, :message=>{:data=>"test"}}

Sign it offline with your key, and check that the signature validates:


key = Eth::Key.new
# => #<Eth::Key:0x000055cd9df38d40 @private_key=#<Secp256k1::PrivateKey:0x000055cd9df38bd8 @data="\xD6\xFDn)\x11\x84YT\xB2\xCD\xFB\x98\xDF\x10}\x96\xEA\n\x1C,z'FsXY\xF1\xC4_\xD9\xDE\xBA">, @public_key=#<Secp256k1::PublicKey:0x000055cd9d...

signature = key.sign_typed_data typed_data
# => "31314053dfbcf5549ecc76aaa7292f5f681f7afca06d428e4645c5c813519b794e4e78907d367d563240fecb6fa0dfac981fb063c200ccd3eb3f22dda115e9601c"

valid = Eth::Signature.verify typed_data, signature, key.address
# => true

You can adjust typed_data for your use-case according to the EIP-712 specification.

Related