eth-custom 0.5.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/.github/dependabot.yml +18 -0
  3. data/.github/workflows/codeql.yml +48 -0
  4. data/.github/workflows/docs.yml +26 -0
  5. data/.github/workflows/spec.yml +52 -0
  6. data/.gitignore +43 -0
  7. data/.gitmodules +3 -0
  8. data/.rspec +4 -0
  9. data/.yardopts +1 -0
  10. data/AUTHORS.txt +29 -0
  11. data/CHANGELOG.md +218 -0
  12. data/Gemfile +17 -0
  13. data/LICENSE.txt +202 -0
  14. data/README.md +347 -0
  15. data/Rakefile +6 -0
  16. data/bin/console +10 -0
  17. data/bin/setup +9 -0
  18. data/codecov.yml +6 -0
  19. data/eth.gemspec +51 -0
  20. data/lib/eth/abi/event.rb +137 -0
  21. data/lib/eth/abi/type.rb +178 -0
  22. data/lib/eth/abi.rb +446 -0
  23. data/lib/eth/address.rb +106 -0
  24. data/lib/eth/api.rb +223 -0
  25. data/lib/eth/chain.rb +157 -0
  26. data/lib/eth/client/http.rb +63 -0
  27. data/lib/eth/client/ipc.rb +50 -0
  28. data/lib/eth/client.rb +499 -0
  29. data/lib/eth/constant.rb +71 -0
  30. data/lib/eth/contract/event.rb +42 -0
  31. data/lib/eth/contract/function.rb +57 -0
  32. data/lib/eth/contract/function_input.rb +38 -0
  33. data/lib/eth/contract/function_output.rb +37 -0
  34. data/lib/eth/contract/initializer.rb +47 -0
  35. data/lib/eth/contract.rb +143 -0
  36. data/lib/eth/eip712.rb +184 -0
  37. data/lib/eth/key/decrypter.rb +146 -0
  38. data/lib/eth/key/encrypter.rb +207 -0
  39. data/lib/eth/key.rb +167 -0
  40. data/lib/eth/rlp/decoder.rb +114 -0
  41. data/lib/eth/rlp/encoder.rb +78 -0
  42. data/lib/eth/rlp/sedes/big_endian_int.rb +66 -0
  43. data/lib/eth/rlp/sedes/binary.rb +97 -0
  44. data/lib/eth/rlp/sedes/list.rb +84 -0
  45. data/lib/eth/rlp/sedes.rb +74 -0
  46. data/lib/eth/rlp.rb +63 -0
  47. data/lib/eth/signature.rb +163 -0
  48. data/lib/eth/solidity.rb +75 -0
  49. data/lib/eth/tx/eip1559.rb +337 -0
  50. data/lib/eth/tx/eip2930.rb +329 -0
  51. data/lib/eth/tx/legacy.rb +297 -0
  52. data/lib/eth/tx.rb +322 -0
  53. data/lib/eth/unit.rb +49 -0
  54. data/lib/eth/util.rb +235 -0
  55. data/lib/eth/version.rb +20 -0
  56. data/lib/eth.rb +35 -0
  57. metadata +184 -0
@@ -0,0 +1,97 @@
1
+ # Copyright (c) 2016-2022 The Ruby-Eth Contributors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ # -*- encoding : ascii-8bit -*-
16
+
17
+ # Provides the {Eth} module.
18
+ module Eth
19
+
20
+ # Provides an recursive-length prefix (RLP) encoder and decoder.
21
+ module Rlp
22
+
23
+ # Provides serializable and deserializable types (SeDes).
24
+ module Sedes
25
+
26
+ # A sedes type for binary values.
27
+ class Binary
28
+
29
+ # A singleton class for binary values of fixed length.
30
+ class << self
31
+
32
+ # Create a serializable binary of fixed size.
33
+ #
34
+ # @param l [Integer] the fixed size of the binary.
35
+ # @param allow_empty [Boolean] indicator wether empty binaries should be allowed.
36
+ # @return [Eth::Rlp::Sedes::Binary] a serializable binary of fixed size.
37
+ def fixed_length(l, allow_empty: false)
38
+ new(min_length: l, max_length: l, allow_empty: allow_empty)
39
+ end
40
+
41
+ # Checks wether the given object is of a valid binary type.
42
+ #
43
+ # @param obj [Object] the supposed binary item to check.
44
+ # @return [Boolean] true if valid.
45
+ def valid_type?(obj)
46
+ obj.instance_of? String
47
+ end
48
+ end
49
+
50
+ # Create a serializable binary of variable size.
51
+ #
52
+ # @param min_length [Integer] the minimum size of the binary.
53
+ # @param max_length [Integer] the maximum size of the binary.
54
+ # @param allow_empty [Boolean] indicator wether empty binaries should be allowed.
55
+ def initialize(min_length: 0, max_length: Constant::INFINITY, allow_empty: false)
56
+ @min_length = min_length
57
+ @max_length = max_length
58
+ @allow_empty = allow_empty
59
+ end
60
+
61
+ # Serializes a binary.
62
+ #
63
+ # @param obj [String] the binary to serialize.
64
+ # @return [Object] a serialized binary.
65
+ # @raise [SerializationError] if provided object is of invalid type.
66
+ # @raise [SerializationError] if provided binary is of invalid length.
67
+ def serialize(obj)
68
+ raise SerializationError, "Object is not a serializable (#{obj.class})" unless self.class.valid_type? obj
69
+ serial = Util.str_to_bytes obj
70
+ raise SerializationError, "Object has invalid length" unless valid_length? serial.size
71
+ serial
72
+ end
73
+
74
+ # Deserializes a binary.
75
+ #
76
+ # @param serial [Object] the serialized binary.
77
+ # @return [String] a deserialized binary.
78
+ # @raise [DeserializationError] if provided serial is of wrong type.
79
+ # @raise [DeserializationError] if provided serial is of wrong length.
80
+ def deserialize(serial)
81
+ raise DeserializationError, "Objects of type #{serial.class} cannot be deserialized" unless Util.is_primitive? serial
82
+ raise DeserializationError, "#{serial.class} has invalid length" unless valid_length? serial.size
83
+ serial
84
+ end
85
+
86
+ # Checks wether the given length fits the defined size boundaries of the
87
+ # binary type.
88
+ #
89
+ # @param length [Integer] the supposed length of the binary item.
90
+ # @return [Boolean] true if valid.
91
+ def valid_length?(length)
92
+ (@min_length <= length && length <= @max_length) || (@allow_empty && length == 0)
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,84 @@
1
+ # Copyright (c) 2016-2022 The Ruby-Eth Contributors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ # -*- encoding : ascii-8bit -*-
16
+
17
+ # Provides the {Eth} module.
18
+ module Eth
19
+
20
+ # Provides an recursive-length prefix (RLP) encoder and decoder.
21
+ module Rlp
22
+
23
+ # Provides serializable and deserializable types (SeDes).
24
+ module Sedes
25
+
26
+ # A sedes type for lists of fixed length.
27
+ class List < Array
28
+
29
+ # Create a serializable list of fixed size.
30
+ #
31
+ # @param elements [Array] an array indicating the structure of the list.
32
+ # @param strict [Boolean] an option to enforce the given structure.
33
+ def initialize(elements: [], strict: true)
34
+ super()
35
+ @strict = strict
36
+ elements.each do |e|
37
+ if Sedes.is_sedes?(e)
38
+ push e
39
+ elsif Util.is_list?(e)
40
+ push List.new(elements: e)
41
+ else
42
+ raise TypeError, "Instances of List must only contain sedes objects or nested sequences thereof."
43
+ end
44
+ end
45
+ end
46
+
47
+ # Serialize an array.
48
+ #
49
+ # @param obj [Array] the array to be serialized.
50
+ # @return [Array] a serialized list.
51
+ # @raise [SerializationError] if provided array is not a sequence.
52
+ # @raise [SerializationError] if provided array is of wrong length.
53
+ def serialize(obj)
54
+ raise SerializationError, "Can only serialize sequences" unless Util.is_list?(obj)
55
+ raise SerializationError, "List has wrong length" if (@strict && self.size != obj.size) || self.size < obj.size
56
+ result = []
57
+ obj.zip(self).each_with_index do |(element, sedes), i|
58
+ result.push sedes.serialize(element)
59
+ end
60
+ result
61
+ end
62
+
63
+ # Deserializes a list.
64
+ #
65
+ # @param serial [Array] the serialized list.
66
+ # @return [Array] a deserialized list.
67
+ # @raise [DeserializationError] if provided serial is not a sequence.
68
+ # @raise [DeserializationError] if provided serial is of wrong length.
69
+ def deserialize(serial)
70
+ raise DeserializationError, "Can only deserialize sequences" unless Util.is_list?(serial)
71
+ raise DeserializationError, "List has wrong length" if @strict && serial.size != self.size
72
+ result = []
73
+ len = [serial.size, self.size].min
74
+ len.times do |i|
75
+ sedes = self[i]
76
+ element = serial[i]
77
+ result.push sedes.deserialize(element)
78
+ end
79
+ result.freeze
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,74 @@
1
+ # Copyright (c) 2016-2022 The Ruby-Eth Contributors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ # -*- encoding : ascii-8bit -*-
16
+
17
+ require "eth/rlp/sedes/big_endian_int"
18
+ require "eth/rlp/sedes/binary"
19
+ require "eth/rlp/sedes/list"
20
+
21
+ # Provides the {Eth} module.
22
+ module Eth
23
+
24
+ # Provides an recursive-length prefix (RLP) encoder and decoder.
25
+ module Rlp
26
+
27
+ # Provides serializable and deserializable types (SeDes).
28
+ module Sedes
29
+
30
+ # Provides a singleton {Eth::Rlp::Sedes} class to infer objects and types.
31
+ class << self
32
+
33
+ # Tries to find a sedes objects suitable for a given Ruby object.
34
+ #
35
+ # The sedes objects considered are `obj`'s class, {big_endian_int} and
36
+ # {binary}. If `obj` is a list, an {Eth::Rlp::Sedes::List} will be
37
+ # constructed recursively.
38
+ #
39
+ # @param obj [Object] the Ruby object for which to find a sedes object.
40
+ # @raise [TypeError] if no appropriate sedes could be found.
41
+ def infer(obj)
42
+ return obj.class if is_sedes? obj.class
43
+ return big_endian_int if obj.is_a?(Integer) && obj >= 0
44
+ return binary if Binary.valid_type? obj
45
+ return List.new(elements: obj.map { |item| infer item }) if Util.is_list? obj
46
+ raise TypeError, "Did not find sedes handling type #{obj.class.name}"
47
+ end
48
+
49
+ # Determines if an object is a sedes object.
50
+ #
51
+ # @param obj [Object] the object to check.
52
+ # @return [Boolean] true if it's serializable and deserializable.
53
+ def is_sedes?(obj)
54
+ obj.respond_to?(:serialize) && obj.respond_to?(:deserialize)
55
+ end
56
+
57
+ # A utility to use a big-endian, unsigned integer sedes type with
58
+ # unspecified length.
59
+ #
60
+ # @return [Eth::Rlp::Sedes::BigEndianInt] a big-endian, unsigned integer sedes.
61
+ def big_endian_int
62
+ @big_endian_int ||= BigEndianInt.new
63
+ end
64
+
65
+ # A utility to use a binary sedes type.
66
+ #
67
+ # @return [Eth::Rlp::Sedes::Binary] a binary sedes.
68
+ def binary
69
+ @binary ||= Binary.new
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
data/lib/eth/rlp.rb ADDED
@@ -0,0 +1,63 @@
1
+ # Copyright (c) 2016-2022 The Ruby-Eth Contributors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ # -*- encoding : ascii-8bit -*-
16
+
17
+ require "eth/rlp/decoder"
18
+ require "eth/rlp/encoder"
19
+ require "eth/rlp/sedes"
20
+ require "eth/util"
21
+
22
+ # Provides the {Eth} module.
23
+ module Eth
24
+
25
+ # Provides an recursive-length prefix (RLP) encoder and decoder.
26
+ module Rlp
27
+ extend self
28
+
29
+ # The Rlp module exposes a variety of exceptions grouped as {RlpException}.
30
+ class RlpException < StandardError; end
31
+
32
+ # An error-type to point out RLP-encoding errors.
33
+ class EncodingError < RlpException; end
34
+
35
+ # An error-type to point out RLP-decoding errors.
36
+ class DecodingError < RlpException; end
37
+
38
+ # An error-type to point out RLP-type serialization errors.
39
+ class SerializationError < RlpException; end
40
+
41
+ # An error-type to point out RLP-type serialization errors.
42
+ class DeserializationError < RlpException; end
43
+
44
+ # A wrapper to represent already RLP-encoded data.
45
+ class Data < String; end
46
+
47
+ # Performes an {Eth::Rlp::Encoder} on any ruby object.
48
+ #
49
+ # @param obj [Object] any ruby object.
50
+ # @return [String] a packed, RLP-encoded item.
51
+ def encode(obj)
52
+ Rlp::Encoder.perform obj
53
+ end
54
+
55
+ # Performes an {Eth::Rlp::Decoder} on any RLP-encoded item.
56
+ #
57
+ # @param rlp [String] a packed, RLP-encoded item.
58
+ # @return [Object] a decoded ruby object.
59
+ def decode(rlp)
60
+ Rlp::Decoder.perform rlp
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,163 @@
1
+ # Copyright (c) 2016-2022 The Ruby-Eth Contributors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "rbsecp256k1"
16
+
17
+ # Provides the {Eth} module.
18
+ module Eth
19
+
20
+ # Defines handy tools for verifying and recovering signatures.
21
+ module Signature
22
+ extend self
23
+
24
+ # Provides a special signature error if signature is invalid.
25
+ class SignatureError < StandardError; end
26
+
27
+ # EIP-191 prefix byte 0x19
28
+ EIP191_PREFIX_BYTE = "\x19".freeze
29
+
30
+ # EIP-712 version byte 0x01
31
+ EIP712_VERSION_BYTE = "\x01".freeze
32
+
33
+ # Prefix message as per EIP-191 with `0x19` to ensure the data is not
34
+ # valid RLP and thus not mistaken for a transaction.
35
+ # EIP-191 Version byte: `0x45` (`E`)
36
+ # Ref: https://eips.ethereum.org/EIPS/eip-191
37
+ #
38
+ # @param message [String] the message string to be prefixed.
39
+ # @return [String] an EIP-191 prefixed string.
40
+ def prefix_message(message)
41
+ "#{EIP191_PREFIX_BYTE}Ethereum Signed Message:\n#{message.bytesize}#{message}"
42
+ end
43
+
44
+ # Dissects a signature blob of 65+ bytes into its `r`, `s`, and `v`
45
+ # values.
46
+ #
47
+ # @param signature [String] a concatenated Secp256k1 signature string.
48
+ # @return [String, String, String] the `r`, `s`, and `v` values.
49
+ # @raise [SignatureError] if signature is of unknown size.
50
+ def dissect(signature)
51
+ signature = Util.bin_to_hex signature unless Util.is_hex? signature
52
+ signature = Util.remove_hex_prefix signature
53
+ if signature.size < 130
54
+ raise SignatureError, "Unknown signature length #{signature.size}!"
55
+ end
56
+ r = signature[0, 64]
57
+ s = signature[64, 64]
58
+ v = signature[128..]
59
+ return r, s, v
60
+ end
61
+
62
+ # Recovers a signature from arbitrary data without validation on a given chain.
63
+ #
64
+ # @param blob [String] that arbitrary data to be recovered.
65
+ # @param signature [String] the hex string containing the signature.
66
+ # @param chain_id [Integer] the chain ID the signature should be recovered from.
67
+ # @return [String] a hexa-decimal, uncompressed public key.
68
+ # @raise [SignatureError] if signature is of invalid size or invalid v.
69
+ def recover(blob, signature, chain_id = Chain::ETHEREUM)
70
+ context = Secp256k1::Context.new
71
+ r, s, v = dissect signature
72
+ v = v.to_i(16)
73
+ raise SignatureError, "Invalid signature v byte #{v} for chain ID #{chain_id}!" if v < chain_id
74
+ recovery_id = Chain.to_recovery_id v, chain_id
75
+ signature_rs = Util.hex_to_bin "#{r}#{s}"
76
+ recoverable_signature = context.recoverable_signature_from_compact signature_rs, recovery_id
77
+ public_key = recoverable_signature.recover_public_key blob
78
+ Util.bin_to_hex public_key.uncompressed
79
+ end
80
+
81
+ # Recovers a public key from a prefixed, personal message and
82
+ # a signature on a given chain. (EIP-191)
83
+ # Ref: https://eips.ethereum.org/EIPS/eip-191
84
+ #
85
+ # @param message [String] the message string.
86
+ # @param signature [String] the hex string containing the signature.
87
+ # @param chain_id [Integer] the chain ID the signature should be recovered from.
88
+ # @return [String] a hexa-decimal, uncompressed public key.
89
+ def personal_recover(message, signature, chain_id = Chain::ETHEREUM)
90
+ prefixed_message = prefix_message message
91
+ hashed_message = Util.keccak256 prefixed_message
92
+ recover hashed_message, signature, chain_id
93
+ end
94
+
95
+ # Recovers a public key from a typed data structure and a signature
96
+ # on a given chain. (EIP-712)
97
+ # Ref: https://eips.ethereum.org/EIPS/eip-712
98
+ #
99
+ # @param typed_data [Array] all the data in the typed data structure to be recovered.
100
+ # @param signature [String] the hex string containing the signature.
101
+ # @param chain_id [Integer] the chain ID the signature should be recovered from.
102
+ # @return [String] a hexa-decimal, uncompressed public key.
103
+ def recover_typed_data(typed_data, signature, chain_id = Chain::ETHEREUM)
104
+ hash_to_sign = Eip712.hash typed_data
105
+ recover hash_to_sign, signature, chain_id
106
+ end
107
+
108
+ # Verifies a signature for a given public key or address.
109
+ #
110
+ # @param blob [String] that arbitrary data to be verified.
111
+ # @param signature [String] the hex string containing the signature.
112
+ # @param public_key [String] either a public key or an Ethereum address.
113
+ # @param chain_id [Integer] the chain ID used to sign.
114
+ # @return [Boolean] true if signature matches provided public key.
115
+ # @raise [SignatureError] if it cannot determine the type of data or public key.
116
+ def verify(blob, signature, public_key, chain_id = Chain::ETHEREUM)
117
+ recovered_key = nil
118
+ if blob.instance_of? Array or blob.instance_of? Hash
119
+
120
+ # recover Array from sign_typed_data
121
+ recovered_key = recover_typed_data blob, signature, chain_id
122
+ elsif blob.instance_of? String and blob.encoding != Encoding::ASCII_8BIT
123
+
124
+ # recover message from personal_sign
125
+ recovered_key = personal_recover blob, signature, chain_id
126
+ elsif blob.instance_of? String and (Util.is_hex? blob or blob.encoding == Encoding::ASCII_8BIT)
127
+
128
+ # if nothing else, recover from arbitrary signature
129
+ recovered_key = recover blob, signature, chain_id
130
+ end
131
+
132
+ # raise if we cannot determine the data format
133
+ raise SignatureError, "Unknown data format to verify: #{blob}" if recovered_key.nil?
134
+
135
+ if public_key.instance_of? Address
136
+
137
+ # recovering using an Eth::Address
138
+ address = public_key.to_s
139
+ recovered_address = Util.public_key_to_address(recovered_key).to_s
140
+ return address == recovered_address
141
+ elsif public_key.instance_of? Secp256k1::PublicKey
142
+
143
+ # recovering using an Secp256k1::PublicKey
144
+ public_hex = Util.bin_to_hex public_key.uncompressed
145
+ return public_hex == recovered_key
146
+ elsif public_key.size == 42
147
+
148
+ # recovering using an address String
149
+ address = Address.new(public_key).to_s
150
+ recovered_address = Util.public_key_to_address(recovered_key).to_s
151
+ return address == recovered_address
152
+ elsif public_key.size == 130
153
+
154
+ # recovering using an uncompressed public key String
155
+ return public_key == recovered_key
156
+ else
157
+
158
+ # raise if we cannot determine the public key format used
159
+ raise SignatureError, "Invalid public key or address supplied #{public_key}!"
160
+ end
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,75 @@
1
+ # Copyright (c) 2016-2022 The Ruby-Eth Contributors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "open3"
16
+
17
+ # Provides the {Eth} module.
18
+ module Eth
19
+
20
+ # Class to create {Solidity} compiler bingings for Ruby.
21
+ class Solidity
22
+
23
+ # Provides a Compiler Error in case the contract does not compile.
24
+ class CompilerError < StandardError; end
25
+
26
+ # Solidity compiler binary path.
27
+ attr_reader :compiler
28
+
29
+ # Instantiates a Solidity `solc` system compiler binding that can be
30
+ # used to compile Solidity contracts.
31
+ def initialize
32
+
33
+ # Currently only supports `solc`.
34
+ solc = get_compiler_path
35
+ raise SystemCallError, "Unable to find the solc compiler path!" if solc.nil?
36
+ @compiler = solc
37
+ end
38
+
39
+ # Use the bound Solidity executable to compile the given contract.
40
+ #
41
+ # @param contract [String] path of the contract to compile.
42
+ # @return [Array] JSON containing the compiled contract and ABI for all contracts.
43
+ def compile(contract)
44
+ raise Errno::ENOENT, "Contract file not found: #{contract}" unless File.exist? contract
45
+ command = "#{@compiler} --optimize --combined-json bin,abi #{contract}"
46
+ output, error, status = Open3.capture3 command
47
+ raise SystemCallError, "Unable to run solc compiler!" if status.exitstatus === 127
48
+ raise CompilerError, error unless status.success?
49
+ json = JSON.parse output
50
+ result = {}
51
+ json["contracts"].each do |key, value|
52
+ _file, name = key.split ":"
53
+ result[name] = {}
54
+ result[name]["abi"] = value["abi"]
55
+ result[name]["bin"] = value["bin"]
56
+ end
57
+ return result
58
+ end
59
+
60
+ private
61
+
62
+ # Tries to find a system executable path for the given compiler binary name.
63
+ def get_compiler_path(name = "solc")
64
+ extensions = [""]
65
+ extensions = ENV["PATHEXT"].split(";") unless ENV["PATHEXT"].nil?
66
+ ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
67
+ extensions.each do |ext|
68
+ executable = File.join path, "#{name}#{ext}"
69
+ return executable if File.executable? executable and !File.directory? executable
70
+ end
71
+ end
72
+ return nil
73
+ end
74
+ end
75
+ end