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

dynamic arrays and mutliple outputs and inputs #84

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ config :ex_plasma,
name: "OMG Network",
salt: "0xfad5c7f626d80f9256ef01929f3beb96e058b8b4b0e3fe52d84f054c0e2a7a83",
verifying_contract: "0xd17e1233a03affb9092d5109179b43d6a8828607",
version: "1"
version: "2"
}
```

Expand Down
4 changes: 2 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use Mix.Config

config :ex_plasma,
# 168 for v2
exit_id_size: 160,
exit_id_size: 168,
eip_712_domain: %{
name: "OMG Network",
salt: "0xfad5c7f626d80f9256ef01929f3beb96e058b8b4b0e3fe52d84f054c0e2a7a83",
verifying_contract: "0xd17e1233a03affb9092d5109179b43d6a8828607",
version: "1"
version: "2"
}

# This configuration is loaded before any dependency and is restricted
Expand Down
11 changes: 5 additions & 6 deletions lib/ex_plasma/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,11 @@ defmodule ExPlasma.Builder do
0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>,
outputs: [],
tx_data: 0,
sigs: [
<<129, 213, 32, 15, 183, 218, 255, 22, 82, 95, 22, 86, 103, 227, 92, 109, 9,
89, 7, 142, 235, 107, 203, 29, 20, 231, 91, 168, 255, 119, 204, 239, 44,
125, 76, 109, 200, 196, 204, 230, 224, 241, 84, 75, 9, 3, 160, 177, 37,
181, 174, 98, 51, 15, 136, 235, 47, 96, 15, 209, 45, 85, 153, 2, 28>>
],
sigs: [<<74, 172, 40, 90, 141, 61, 61, 67, 59, 242, 28, 172, 15, 2, 68, 246, 222,
52, 250, 67, 15, 50, 14, 127, 137, 234, 238, 210, 95, 224, 4, 45, 101,
211, 238, 252, 225, 118, 109, 43, 83, 124, 30, 109, 217, 248, 3, 78, 160,
94, 182, 219, 144, 119, 148, 5, 163, 191, 6, 214, 247, 178, 192, 108,
27>>],
tx_type: 1
}}
"""
Expand Down
6 changes: 3 additions & 3 deletions lib/ex_plasma/configuration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule ExPlasma.Configuration do
name: "OMG Network",
salt: "0xfad5c7f626d80f9256ef01929f3beb96e058b8b4b0e3fe52d84f054c0e2a7a83",
verifying_contract: "0xd17e1233a03affb9092d5109179b43d6a8828607",
version: "1"
version: "2"
}

Returns the domain if valid, or raise an exception otherwise.
Expand All @@ -28,14 +28,14 @@ defmodule ExPlasma.Configuration do
...> name: "OMG Network",
...> salt: "0xfad5c7f626d80f9256ef01929f3beb96e058b8b4b0e3fe52d84f054c0e2a7a83",
...> verifying_contract: "0xd17e1233a03affb9092d5109179b43d6a8828607",
...> version: "1"
...> version: "2"
...>})
iex> ExPlasma.Configuration.eip_712_domain()
%{
name: "OMG Network",
salt: "0xfad5c7f626d80f9256ef01929f3beb96e058b8b4b0e3fe52d84f054c0e2a7a83",
verifying_contract: "0xd17e1233a03affb9092d5109179b43d6a8828607",
version: "1"
version: "2"
}
"""
@spec eip_712_domain() :: Validator.eip_712_domain_t() | no_return()
Expand Down
8 changes: 4 additions & 4 deletions lib/ex_plasma/configuration/validator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule ExPlasma.Configuration.Validator do
name: "OMG Network",
salt: "0xfad5c7f626d80f9256ef01929f3beb96e058b8b4b0e3fe52d84f054c0e2a7a83",
verifying_contract: "0xd17e1233a03affb9092d5109179b43d6a8828607",
version: "1"
version: "2"
}

Returns the domain if valid, or raise an exception otherwise.
Expand All @@ -28,13 +28,13 @@ defmodule ExPlasma.Configuration.Validator do
...> name: "OMG Network",
...> salt: "0xfad5c7f626d80f9256ef01929f3beb96e058b8b4b0e3fe52d84f054c0e2a7a83",
...> verifying_contract: "0xd17e1233a03affb9092d5109179b43d6a8828607",
...> version: "1"
...> version: "2"
...>})
%{
name: "OMG Network",
salt: "0xfad5c7f626d80f9256ef01929f3beb96e058b8b4b0e3fe52d84f054c0e2a7a83",
verifying_contract: "0xd17e1233a03affb9092d5109179b43d6a8828607",
version: "1"
version: "2"
}
"""
@spec validate_eip_712_domain(any()) :: eip_712_domain_t() | no_return()
Expand All @@ -50,7 +50,7 @@ defmodule ExPlasma.Configuration.Validator do
name: "OMG Network",
salt: "0xfad5c7f626d80f9256ef01929f3beb96e058b8b4b0e3fe52d84f054c0e2a7a83",
verifying_contract: "0xd17e1233a03affb9092d5109179b43d6a8828607",
version: "1"
version: "2"
}
"""
end
Expand Down
6 changes: 1 addition & 5 deletions lib/ex_plasma/signature.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ defmodule ExPlasma.Signature do
def signature_digest(hash_digest, private_key_hash) do
private_key_binary = Encoding.to_binary!(private_key_hash)

{:ok, {r, s, recovery_id}} =
ExSecp256k1.sign(
hash_digest,
private_key_binary
)
{:ok, {r, s, recovery_id}} = ExSecp256k1.sign(hash_digest, private_key_binary)

# EIP-155
# See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
Expand Down
11 changes: 6 additions & 5 deletions lib/ex_plasma/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -350,15 +350,16 @@ defmodule ExPlasma.Transaction do

## Example

iex> key = "0x79298b0292bbfa9b15705c56b6133201c62b798f102d7d096d31d7637f9b2382"
iex> key = "0x99d483781114bc01e264a3da892760fdb84555927a331e0f305531e97f9a20cf"
iex> txn = %ExPlasma.Transaction{tx_type: 1}
iex> ExPlasma.Transaction.sign(txn, [key])
{:ok, %ExPlasma.Transaction{
sigs: [
<<129, 213, 32, 15, 183, 218, 255, 22, 82, 95, 22, 86, 103, 227, 92, 109, 9,
89, 7, 142, 235, 107, 203, 29, 20, 231, 91, 168, 255, 119, 204, 239, 44,
125, 76, 109, 200, 196, 204, 230, 224, 241, 84, 75, 9, 3, 160, 177, 37,
181, 174, 98, 51, 15, 136, 235, 47, 96, 15, 209, 45, 85, 153, 2, 28>>
<<47, 217, 63, 72, 91, 172, 102, 200, 31, 43, 199, 3, 219, 234, 6, 15, 1,
252, 13, 119, 153, 73, 196, 179, 219, 251, 81, 227, 110, 3, 242, 136, 14,
160, 132, 214, 183, 80, 62, 103, 221, 113, 209, 196, 116, 44, 210, 187,
10, 97, 166, 177, 243, 190, 250, 154, 228, 67, 15, 103, 12, 53, 101, 246,
28>>
],
inputs: [],
metadata: <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Expand Down
4 changes: 2 additions & 2 deletions lib/ex_plasma/typed_data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ defprotocol ExPlasma.TypedData do
@doc """
The EIP712 encoded type data structure.
"""
@spec encode(any(), maybe_improper_list()) :: maybe_improper_list()
@spec encode(any(), maybe_improper_list()) :: maybe_improper_list() | binary()
def encode(data, options \\ [])

@doc """
The keccak hash of the encoded data type.
"""
@spec hash(any(), maybe_improper_list()) :: binary()
@spec hash(any(), maybe_improper_list()) :: binary() | :not_implemented
def hash(data, options \\ [])
end
45 changes: 24 additions & 21 deletions lib/ex_plasma/typed_data/output.ex
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
defimpl ExPlasma.TypedData, for: ExPlasma.Output do
import ExPlasma.Crypto, only: [keccak_hash: 1]
import ABI.TypeEncoder, only: [encode_raw: 2]

alias ExPlasma.Crypto
alias ABI.TypeEncoder
alias ExPlasma.Output

@output_signature "Output(uint256 outputType,bytes20 outputGuard,address currency,uint256 amount)"
@input_signature "Input(uint256 blknum,uint256 txindex,uint256 oindex)"
@output_signature Crypto.keccak_hash("Output(uint256 outputType,bytes20 outputGuard,address currency,uint256 amount)")
@input_signature Crypto.keccak_hash("Input(uint256 blknum,uint256 txindex,uint256 oindex)")

@spec encode(Output.t(), as: atom()) :: list()
@spec encode(Output.t(), as: atom()) :: binary()
def encode(output, as: :input), do: do_to_rlp_id(output.output_id)
def encode(output, as: :output), do: do_encode(output)

@spec hash(Output.t(), [{:as, :input | :output}, ...]) :: <<_::256>>
def hash(output, options), do: output |> encode(options) |> do_hash(options)
@spec hash(Output.t(), [{:as, :input | :output}, ...]) :: :not_implemented
def hash(_output, _options), do: :not_implemented

defp do_encode(%{output_type: type, output_data: data}) do
[
@output_signature,
encode_raw([type], [{:uint, 256}]),
encode_raw([data.output_guard], [{:bytes, 20}]),
encode_raw([data.token], [:address]),
encode_raw([data.amount], [{:uint, 256}])
TypeEncoder.encode_raw([type], [{:uint, 256}]),
TypeEncoder.encode_raw([data.output_guard], [{:bytes, 20}]),
TypeEncoder.encode_raw([data.token], [:address]),
TypeEncoder.encode_raw([data.amount], [{:uint, 256}])
]
|> Enum.join()
|> Crypto.keccak_hash()
end

defp do_to_rlp_id(%{blknum: blknum, txindex: txindex, oindex: oindex}) do
[
@input_signature,
encode_raw([blknum], [{:uint, 256}]),
encode_raw([txindex], [{:uint, 256}]),
encode_raw([oindex], [{:uint, 256}])
TypeEncoder.encode_raw([blknum], [{:uint, 256}]),
TypeEncoder.encode_raw([txindex], [{:uint, 256}]),
TypeEncoder.encode_raw([oindex], [{:uint, 256}])
]
|> Enum.join()
|> Crypto.keccak_hash()
end

defp do_hash([signature | encoded_list], _options) do
data = [keccak_hash(signature) | encoded_list]
# defp do_hash([signature | encoded_list], _options) do
# data = [Crypto.keccak_hash(signature) | encoded_list]

data
|> Enum.join()
|> keccak_hash()
end
# data
# |> Enum.join()
# |> Crypto.keccak_hash()
# end
end
Loading