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: rename functions prefixed with is_ #182

Merged
merged 8 commits into from
Dec 21, 2022
Merged
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
18 changes: 9 additions & 9 deletions lib/eth/abi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def encode(types, args)

# encode types and arguments
args.each_with_index do |arg, i|
if parsed_types[i].is_dynamic?
if parsed_types[i].dynamic?
head += encode_type Type.size_type, head_size + tail.size
tail += encode_type parsed_types[i], arg
else
Expand All @@ -81,7 +81,7 @@ def encode_type(type, arg)
size = encode_type Type.size_type, arg.size
padding = Constant::BYTE_ZERO * (Util.ceil32(arg.size) - arg.size)
return "#{size}#{arg}#{padding}"
elsif type.is_dynamic?
elsif type.dynamic?
raise EncodingError, "Argument must be an Array" unless arg.instance_of? Array

# encodes dynamic-sized arrays
Expand Down Expand Up @@ -163,7 +163,7 @@ def encode_primitive_type(type, arg)
def decode(types, data)

# accept hex abi but decode it first
data = Util.hex_to_bin data if Util.is_hex? data
data = Util.hex_to_bin data if Util.hex? data

# parse all types
parsed_types = types.map { |t| Type.parse(t) }
Expand All @@ -173,7 +173,7 @@ def decode(types, data)
start_positions = [nil] * types.size + [data.size]
pos = 0
parsed_types.each_with_index do |t, i|
if t.is_dynamic?
if t.dynamic?

# record start position for dynamic type
start_positions[i] = Util.deserialize_big_endian_to_int(data[pos, 32])
Expand Down Expand Up @@ -201,7 +201,7 @@ def decode(types, data)

# add dynamic types
parsed_types.each_with_index do |t, i|
if t.is_dynamic?
if t.dynamic?
offset, next_offset = start_positions[i, 2]
outputs[i] = data[offset...next_offset]
end
Expand All @@ -225,12 +225,12 @@ def decode_type(type, arg)

# decoded strings and bytes
return data[0, l]
elsif type.is_dynamic?
elsif type.dynamic?
l = Util.deserialize_big_endian_to_int arg[0, 32]
nested_sub = type.nested_sub

# ref https://github.com/ethereum/tests/issues/691
raise NotImplementedError, "Decoding dynamic arrays with nested dynamic sub-types is not implemented for ABI." if nested_sub.is_dynamic?
raise NotImplementedError, "Decoding dynamic arrays with nested dynamic sub-types is not implemented for ABI." if nested_sub.dynamic?

# decoded dynamic-sized arrays
return (0...l).map { |i| decode_type(nested_sub, arg[32 + nested_sub.size * i, nested_sub.size]) }
Expand Down Expand Up @@ -428,8 +428,8 @@ def encode_address(arg)
# The ABI encoder needs to be able to determine between a hex `"123"`
# and a binary `"123"` string.
def handle_hex_string(arg, type)
if Util.is_prefixed? arg or
(arg.size === type.sub_type.to_i * 2 and Util.is_hex? arg)
if Util.prefixed? arg or
(arg.size === type.sub_type.to_i * 2 and Util.hex? arg)

# There is no way telling whether a string is hex or binary with certainty
# in Ruby. Therefore, we assume a `0x` prefix to indicate a hex string.
Expand Down
4 changes: 2 additions & 2 deletions lib/eth/abi/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def size
end
else
unless dimensions.last == 0
unless nested_sub.is_dynamic?
unless nested_sub.dynamic?
s = dimensions.last * nested_sub.size
end
end
Expand All @@ -114,7 +114,7 @@ def size
# Helpes to determine whether array is of dynamic size.
#
# @return [Boolean] true if array is of dynamic size.
def is_dynamic?
def dynamic?
size.nil?
end

Expand Down
2 changes: 1 addition & 1 deletion lib/eth/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CheckSumError < StandardError; end
#
# @param address [String] hex string representing an ethereum address.
def initialize(address)
unless Util.is_hex? address
unless Util.hex? address
raise CheckSumError, "Unknown address type #{address}!"
end
@address = Util.prefix_hex address
Expand Down
12 changes: 6 additions & 6 deletions lib/eth/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,9 @@ def transact_and_wait(contract, function, *args, **kwargs)
# @raise [ArgumentError] in case the contract cannot be called yet.
def is_valid_signature(contract, hash, signature, magic = "1626ba7e")
raise ArgumentError, "Contract not deployed yet." if contract.address.nil?
hash = Util.hex_to_bin hash if Util.is_hex? hash
signature = Util.hex_to_bin signature if Util.is_hex? signature
magic = Util.hex_to_bin magic if Util.is_hex? magic
hash = Util.hex_to_bin hash if Util.hex? hash
signature = Util.hex_to_bin signature if Util.hex? signature
magic = Util.hex_to_bin magic if Util.hex? magic
result = call(contract, "isValidSignature", hash, signature)
return result === magic
end
Expand All @@ -381,7 +381,7 @@ def reset_id
#
# @param hash [String] the transaction hash.
# @return [Boolean] true if included in a block.
def is_mined_tx?(hash)
def mined?(hash)
mined_tx = eth_get_transaction_by_hash hash
!mined_tx.nil? && !mined_tx["result"].nil? && !mined_tx["result"]["blockNumber"].nil?
end
Expand All @@ -397,7 +397,7 @@ def wait_for_tx(hash)
retry_rate = 0.1
loop do
raise Timeout::Error if ((Time.now - start_time) > timeout)
return hash if is_mined_tx? hash
return hash if mined? hash
sleep retry_rate
end
end
Expand Down Expand Up @@ -470,7 +470,7 @@ def marshal(params)
return Util.prefix_hex "#{params.to_i.to_s(16)}"
elsif params.is_a? Address
return params.to_s
elsif Util.is_hex? params
elsif Util.hex? params
return Util.prefix_hex params
else
return params
Expand Down
2 changes: 1 addition & 1 deletion lib/eth/eip712.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def hash_data(primary_type, data, types)
# @return [Array] the data in the data structure we want to hash.
# @raise [TypedDataError] if the data fails validation.
def enforce_typed_data(data)
data = JSON.parse data if Util.is_hex? data
data = JSON.parse data if Util.hex? data
raise TypedDataError, "Data is missing, try again with data." if data.nil? or data.empty?
raise TypedDataError, "Data types are missing." if data[:types].nil? or data[:types].empty?
raise TypedDataError, "Data primaryType is missing." if data[:primaryType].nil? or data[:primaryType].empty?
Expand Down
8 changes: 4 additions & 4 deletions lib/eth/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def initialize(priv: nil)
unless priv.nil?

# Converts hex private keys to binary strings.
priv = Util.hex_to_bin priv if Util.is_hex? priv
priv = Util.hex_to_bin priv if Util.hex? priv

# Creates a keypair from existing private key data.
key = ctx.key_pair_from_private_key priv
Expand All @@ -74,10 +74,10 @@ def sign(blob, chain_id = nil)
compact, recovery_id = context.sign_recoverable(@private_key, blob).compact
signature = compact.bytes
v = Chain.to_v recovery_id, chain_id
is_leading_zero = true
leading_zero = true
[v].pack("N").unpack("C*").each do |byte|
is_leading_zero = false if byte > 0 and is_leading_zero
signature.append byte unless is_leading_zero and byte === 0
leading_zero = false if byte > 0 and leading_zero
signature.append byte unless leading_zero and byte === 0
end
Util.bin_to_hex signature.pack "c*"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/eth/rlp/decoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module Decoder
# @raise [Eth::Rlp::DecodingError] if the input string does not end after
# the root item.
def perform(rlp)
rlp = Util.hex_to_bin rlp if Util.is_hex? rlp
rlp = Util.hex_to_bin rlp if Util.hex? rlp
rlp = Util.str_to_bytes rlp
begin
item, next_start = consume_item rlp, 0
Expand Down
4 changes: 2 additions & 2 deletions lib/eth/rlp/encoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def perform(obj)
# Encodes the raw item.
def encode_raw(item)
return item if item.instance_of? Rlp::Data
return encode_primitive item if Util.is_primitive? item
return encode_list item if Util.is_list? item
return encode_primitive item if Util.primitive? item
return encode_list item if Util.list? item
raise EncodingError "Cannot encode object of type #{item.class.name}"
end

Expand Down
6 changes: 3 additions & 3 deletions lib/eth/rlp/sedes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ class << self
# @param obj [Object] the Ruby object for which to find a sedes object.
# @raise [TypeError] if no appropriate sedes could be found.
def infer(obj)
return obj.class if is_sedes? obj.class
return obj.class if sedes? obj.class
return big_endian_int if obj.is_a?(Integer) && obj >= 0
return binary if Binary.valid_type? obj
return List.new(elements: obj.map { |item| infer item }) if Util.is_list? obj
return List.new(elements: obj.map { |item| infer item }) if Util.list? obj
raise TypeError, "Did not find sedes handling type #{obj.class.name}"
end

# Determines if an object is a sedes object.
#
# @param obj [Object] the object to check.
# @return [Boolean] true if it's serializable and deserializable.
def is_sedes?(obj)
def sedes?(obj)
obj.respond_to?(:serialize) && obj.respond_to?(:deserialize)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/eth/rlp/sedes/binary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def serialize(obj)
# @raise [DeserializationError] if provided serial is of wrong type.
# @raise [DeserializationError] if provided serial is of wrong length.
def deserialize(serial)
raise DeserializationError, "Objects of type #{serial.class} cannot be deserialized" unless Util.is_primitive? serial
raise DeserializationError, "Objects of type #{serial.class} cannot be deserialized" unless Util.primitive? serial
raise DeserializationError, "#{serial.class} has invalid length" unless valid_length? serial.size
serial
end
Expand Down
8 changes: 4 additions & 4 deletions lib/eth/rlp/sedes/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def initialize(elements: [], strict: true)
super()
@strict = strict
elements.each do |e|
if Sedes.is_sedes?(e)
if Sedes.sedes?(e)
push e
elsif Util.is_list?(e)
elsif Util.list?(e)
push List.new(elements: e)
else
raise TypeError, "Instances of List must only contain sedes objects or nested sequences thereof."
Expand All @@ -51,7 +51,7 @@ def initialize(elements: [], strict: true)
# @raise [SerializationError] if provided array is not a sequence.
# @raise [SerializationError] if provided array is of wrong length.
def serialize(obj)
raise SerializationError, "Can only serialize sequences" unless Util.is_list?(obj)
raise SerializationError, "Can only serialize sequences" unless Util.list?(obj)
raise SerializationError, "List has wrong length" if (@strict && self.size != obj.size) || self.size < obj.size
result = []
obj.zip(self).each_with_index do |(element, sedes), i|
Expand All @@ -67,7 +67,7 @@ def serialize(obj)
# @raise [DeserializationError] if provided serial is not a sequence.
# @raise [DeserializationError] if provided serial is of wrong length.
def deserialize(serial)
raise DeserializationError, "Can only deserialize sequences" unless Util.is_list?(serial)
raise DeserializationError, "Can only deserialize sequences" unless Util.list?(serial)
raise DeserializationError, "List has wrong length" if @strict && serial.size != self.size
result = []
len = [serial.size, self.size].min
Expand Down
4 changes: 2 additions & 2 deletions lib/eth/signature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def prefix_message(message)
# @return [String, String, String] the `r`, `s`, and `v` values.
# @raise [SignatureError] if signature is of unknown size.
def dissect(signature)
signature = Util.bin_to_hex signature unless Util.is_hex? signature
signature = Util.bin_to_hex signature unless Util.hex? signature
signature = Util.remove_hex_prefix signature
if signature.size < 130
raise SignatureError, "Unknown signature length #{signature.size}!"
Expand Down Expand Up @@ -126,7 +126,7 @@ def verify(blob, signature, public_key, chain_id = Chain::ETHEREUM)

# recover message from personal_sign
recovered_key = personal_recover blob, signature, chain_id
elsif blob.instance_of? String and (Util.is_hex? blob or blob.encoding == Encoding::ASCII_8BIT)
elsif blob.instance_of? String and (Util.hex? blob or blob.encoding == Encoding::ASCII_8BIT)

# if nothing else, recover from arbitrary signature
recovered_key = recover blob, signature, chain_id
Expand Down
8 changes: 4 additions & 4 deletions lib/eth/tx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def unsigned_copy(tx)
def estimate_intrinsic_gas(data = "", list = [])
gas = DEFAULT_GAS_LIMIT
unless data.nil? or data.empty?
data = Util.hex_to_bin data if Util.is_hex? data
data = Util.hex_to_bin data if Util.hex? data

# count zero bytes
zero = data.count ZERO_BYTE
Expand Down Expand Up @@ -288,7 +288,7 @@ def sanitize_data(data)
data = "" if data.nil?

# ensure payload to be binary if it's hex, otherwise we'll treat it raw
data = Util.hex_to_bin data if Util.is_hex? data
data = Util.hex_to_bin data if Util.hex? data
return data
end

Expand All @@ -305,7 +305,7 @@ def sanitize_list(list)

# recursively check the entire array
list[index] = sanitize_list value
elsif Util.is_hex? value
elsif Util.hex? value

# only modify if we find a hex value
list[index] = Util.hex_to_bin value
Expand All @@ -317,7 +317,7 @@ def sanitize_list(list)
# Allows to check wether a transaction is signed already.
#
# @return [Bool] true if transaction is already signed.
def is_signed?(tx)
def signed?(tx)
!tx.signature_r.nil? and tx.signature_r != 0 and
!tx.signature_s.nil? and tx.signature_s != 0
end
Expand Down
4 changes: 2 additions & 2 deletions lib/eth/tx/eip1559.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def unsigned_copy(tx)
# @raise [Signature::SignatureError] if transaction is already signed.
# @raise [Signature::SignatureError] if sender address does not match signing key.
def sign(key)
if Tx.is_signed? self
if Tx.signed? self
raise Signature::SignatureError, "Transaction is already signed!"
end

Expand All @@ -258,7 +258,7 @@ def sign(key)
# @return [String] a raw, RLP-encoded EIP-1559 type transaction object.
# @raise [Signature::SignatureError] if the transaction is not yet signed.
def encoded
unless Tx.is_signed? self
unless Tx.signed? self
raise Signature::SignatureError, "Transaction is not signed!"
end
tx_data = []
Expand Down
4 changes: 2 additions & 2 deletions lib/eth/tx/eip2930.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def unsigned_copy(tx)
# @raise [Signature::SignatureError] if transaction is already signed.
# @raise [Signature::SignatureError] if sender address does not match signing key.
def sign(key)
if Tx.is_signed? self
if Tx.signed? self
raise Signature::SignatureError, "Transaction is already signed!"
end

Expand All @@ -252,7 +252,7 @@ def sign(key)
# @return [String] a raw, RLP-encoded EIP-2930 type transaction object.
# @raise [Signature::SignatureError] if the transaction is not yet signed.
def encoded
unless Tx.is_signed? self
unless Tx.signed? self
raise Signature::SignatureError, "Transaction is not signed!"
end
tx_data = []
Expand Down
4 changes: 2 additions & 2 deletions lib/eth/tx/legacy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def unsigned_copy(tx)
# @raise [Signature::SignatureError] if transaction is already signed.
# @raise [Signature::SignatureError] if sender address does not match signing key.
def sign(key)
if Tx.is_signed? self
if Tx.signed? self
raise Signature::SignatureError, "Transaction is already signed!"
end

Expand All @@ -230,7 +230,7 @@ def sign(key)
# @return [String] a raw, RLP-encoded legacy transaction.
# @raise [Signature::SignatureError] if the transaction is not yet signed.
def encoded
unless Tx.is_signed? self
unless Tx.signed? self
raise Signature::SignatureError, "Transaction is not signed!"
end
tx_data = []
Expand Down
Loading