Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eth/eip712: prepare tests for packed encoding #216

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/eth/abi/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def parse(type, components = nil, component_name = nil)
@base_type = base_type
@sub_type = sub_type
@dimensions = dims.map { |x| x[1...-1].to_i }
@components = components.map { |component| Abi::Type.parse(component["type"], component.dig("components"), component.dig("name")) } unless components.nil?
@components = components.map { |component| Abi::Type.parse(component["type"], component.dig("components"), component.dig("name")) } if components&.any?
@name = component_name
end

Expand Down
3 changes: 2 additions & 1 deletion lib/eth/eip712.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ def encode_data(primary_type, data, types)
types[primary_type.to_sym].each do |field|
value = data[field[:name].to_sym]
type = field[:type]
raise NotImplementedError, "Arrays currently unimplemented for EIP-712." if type.end_with? "]"
if type == "string"
encoded_types.push "bytes32"
encoded_values.push Util.keccak256 value
Expand All @@ -125,6 +124,8 @@ def encode_data(primary_type, data, types)
encoded_types.push "bytes32"
value = encode_data type, value, types
encoded_values.push Util.keccak256 value
elsif type.end_with? "]"
raise NotImplementedError, "Arrays currently unimplemented for EIP-712."
else
encoded_types.push type
encoded_values.push value
Expand Down
74 changes: 71 additions & 3 deletions spec/eth/eip712_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
:name => "Ether Mail",
:version => "1",
:chainId => Chain::ETHEREUM,
:verifyingContract => Address.new("0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC").checksummed,
:verifyingContract => Address.new("0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"),
}
}

Expand All @@ -44,11 +44,11 @@
{
:from => {
:name => "Cow",
:wallet => Address.new("0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826").checksummed,
:wallet => Address.new("0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"),
},
:to => {
:name => "Bob",
:wallet => Address.new("0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB").checksummed,
:wallet => Address.new("0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"),
},
:contents => "Hello, Bob!",
}
Expand Down Expand Up @@ -128,6 +128,74 @@
end
end

context "eip 712 arrays" do
subject(:domain) {
[
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
{ name: "salt", type: "bytes32" },
]
}

subject(:sell_orders) {
[
{ name: "id", type: "uint256[]" },
{ name: "tokenId", type: "uint256[]" },
{ name: "price", type: "uint256[]" },
{ name: "proto", type: "uint256[]" },
{ name: "purity", type: "uint256[]" },
{ name: "seller", type: "address" },
]
}

subject(:card_exchange_contract) { Address.new("0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC") }

subject(:domain_data) {
{
name: "app",
version: "1",
chainId: 3,
verifyingContract: card_exchange_contract,
salt: "0xa222082684812afae4e093416fff16bc218b569abe4db590b6a058e1f2c1cd3e",
}
}

subject(:address) { Address.new "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" }

subject(:message) {
{
id: [1],
tokenId: [1],
price: [1],
proto: [1],
purity: [1],
seller: address,
}
}

subject(:data) {
{
:types => {
:EIP712Domain => domain,
:SellOrders => sell_orders,
},
:primaryType => "SellOrders",
:domain => domain_data,
:message => message,
}
}

it "encodes arrays in an eip 712 domain" do
expect(Eip712.encode_type "EIP712Domain", data[:types]).to eq "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract,bytes32 salt)"
expect(Eip712.encode_type "SellOrders", data[:types]).to eq "SellOrders(uint256[] id,uint256[] tokenId,uint256[] price,uint256[] proto,uint256[] purity,address seller)"

expect(Util.bin_to_hex(Eip712.encode_data("EIP712Domain", domain_data, data[:types]))).to eq "d87cd6ef79d4e2b95e15ce8abf732db51ec771f1ca2edccf22a46c729ac56472d6f028ca0e8edb4a8c9757ca4fdccab25fa1e0317da1188108f7d2dee14902fbc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc60000000000000000000000000000000000000000000000000000000000000003000000000000000000000000cccccccccccccccccccccccccccccccccccccccca222082684812afae4e093416fff16bc218b569abe4db590b6a058e1f2c1cd3e"
expect(Util.bin_to_hex(Eip712.encode_data("SellOrders", message, data[:types]))).to eq "58682cd513b67511a4ef89156104cf2bc7835c7289308bb112fdafe49897324200000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e0000000000000000000000000cd2a3d9f938e13cd947ec05abc7fe734df8dd8260000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001"
end
end

context "end to end" do
it "can hash the correct data type" do
key = Key.new(priv: "0x8e589ba6280400cfa426229684f7c2ac9ebf132f7ad658a82ed57553a0a9dee8")
Expand Down
Loading