Skip to content

Commit

Permalink
eth/eip721: fix data type bug for bytes, fix #251 (#252)
Browse files Browse the repository at this point in the history
* eth/eip721: fix data type bug for bytes, fix #251

* spec/eip721: clean up
  • Loading branch information
q9f authored Nov 6, 2023
1 parent 4e6dec8 commit a527415
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/eth/eip712.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions spec/eth/eip712_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a527415

Please sign in to comment.