diff --git a/lib/eth/eip712.rb b/lib/eth/eip712.rb index 7d091b98..7af75d03 100644 --- a/lib/eth/eip712.rb +++ b/lib/eth/eip712.rb @@ -114,9 +114,13 @@ def encode_data(primary_type, data, types) value = data[field[:name].to_sym] type = field[:type] raise NotImplementedError, "Arrays currently unimplemented for EIP-712." if type.end_with? "]" - if type == "string" or type == "bytes" + if type == "string" encoded_types.push "bytes32" encoded_values.push Util.keccak256 value + elsif type == "bytes" + encoded_types.push "bytes32" + value = Util.hex_to_bin value + encoded_values.push Util.keccak256 value elsif !types[type.to_sym].nil? encoded_types.push "bytes32" value = encode_data type, value, types diff --git a/spec/eth/eip712_spec.rb b/spec/eth/eip712_spec.rb index 0d28498a..896eba6c 100644 --- a/spec/eth/eip712_spec.rb +++ b/spec/eth/eip712_spec.rb @@ -127,4 +127,47 @@ expect(Eip712.hash typed_data).to eq Util.hex_to_bin "0xbe609aee343fb3c4b28e1df9e632fca64fcfaede20f02e86244efddf30957bd2" end end + + context "end to end" do + it "can hash the correct data type" do + key = Key.new(priv: "0x8e589ba6280400cfa426229684f7c2ac9ebf132f7ad658a82ed57553a0a9dee8") + data = "0xa2d6eae3" + domain = { + name: "Complex Data", + version: "1", + chainId: 421613, + verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC", + } + + type_bytes = [{ name: "data", type: "bytes" }] + data_bytes = { + types: { + EIP712Domain: eip712_domain, + Data: type_bytes, + }, + primaryType: "Data", + domain: domain, + message: { + data: data, + }, + } + sig_bytes = "ddefbbe703f59949a87ece451321924bb9297100dda63e1f39559b72db3ec9e83dae2056c25b52ddb8bd53ab536e84d2e4f70d98219ed14e46b021a59aefb4eb1c" + expect(key.sign_typed_data data_bytes).to eq sig_bytes + + type_string = [{ name: "data", type: "string" }] + data_string = { + types: { + EIP712Domain: eip712_domain, + Data: type_string, + }, + primaryType: "Data", + domain: domain, + message: { + data: data, + }, + } + sig_string = "8255c17ce6be5fb6ee3430784a52a5163c63fc87e2dcae32251d9c49ba849fad7067454b0d7e694698c02e552fd7af283dcaadc754d58ecba978856de8742e361b" + expect(key.sign_typed_data data_string).to eq sig_string + end + end end