class Silkey::Utils
Public Class Methods
add0x(string)
click to toggle source
# File lib/silkey/utils.rb, line 14 def add0x(string) return string if string.to_s[0..1] == '0x' "0x#{string}" end
current_timestamp()
click to toggle source
# File lib/silkey/utils.rb, line 52 def current_timestamp Time.now.getutc.to_i end
empty?(string)
click to toggle source
# File lib/silkey/utils.rb, line 6 def empty?(string) string.nil? || string.to_s == '' end
empty_hex?(string)
click to toggle source
# File lib/silkey/utils.rb, line 33 def empty_hex?(string) return true if empty?(string) return false unless hex?(string) !remove0x(string).split('').reduce(false) { |non_zero, c| c != '0' || non_zero } end
ethereum_address?(string)
click to toggle source
# File lib/silkey/utils.rb, line 44 def ethereum_address?(string) hex_and_length?(string, 40) end
hex?(string)
click to toggle source
# File lib/silkey/utils.rb, line 20 def hex?(string) return false if empty?(string) !remove0x(string)[/\H/] end
int_to_hex(int)
click to toggle source
# File lib/silkey/utils.rb, line 62 def int_to_hex(int) return '' if empty?(int) hex = int.to_i.to_s(16) return hex if hex.length.even? "0#{hex}" end
private_key?(string)
click to toggle source
# File lib/silkey/utils.rb, line 40 def private_key?(string) hex_and_length?(string, 64) end
remove0x(string)
click to toggle source
# File lib/silkey/utils.rb, line 10 def remove0x(string) string.to_s.sub('0x', '') end
sign_message(private_key, message)
click to toggle source
# File lib/silkey/utils.rb, line 76 def sign_message(private_key, message) wallet = Eth::Key.new priv: Silkey::Utils.remove0x(private_key) Silkey::Utils.add0x(wallet.personal_sign(message)) end
sign_plain_message(private_key, message)
click to toggle source
# File lib/silkey/utils.rb, line 81 def sign_plain_message(private_key, message) wallet = Eth::Key.new priv: Silkey::Utils.remove0x(private_key) bin_signature = wallet.sign(message) Eth::Utils.bin_to_hex(bin_signature) end
signature?(string)
click to toggle source
# File lib/silkey/utils.rb, line 48 def signature?(string) hex_and_length?(string, 130) end
str_to_bytes32(string)
click to toggle source
# File lib/silkey/utils.rb, line 26 def str_to_bytes32(string) return '' if empty?(string) hex = string.to_s.unpack('H*')[0] "0x#{hex}#{['0'].cycle(64 - hex.length).to_a.join('')}" end
strings_to_hex(arr)
click to toggle source
# File lib/silkey/utils.rb, line 72 def strings_to_hex(arr) arr.map { |str| str.to_s.unpack('H*') }.join('') end
timestamp?(tmstp)
click to toggle source
# File lib/silkey/utils.rb, line 56 def timestamp?(tmstp) return false if tmstp.nil? tmstp.positive? && tmstp.to_s(10).length == 10 end
verify_message(message, signature)
click to toggle source
# File lib/silkey/utils.rb, line 87 def verify_message(message, signature) public_key = Eth::Key.personal_recover(message, signature) return nil if public_key.nil? Eth::Utils.public_key_to_address(public_key) end
verify_plain_message(message, signature)
click to toggle source
# File lib/silkey/utils.rb, line 94 def verify_plain_message(message, signature) bin_signature = Eth::Utils.hex_to_bin(signature) hash = Eth::Utils.keccak256(message) public_key = Eth::OpenSsl.recover_compact(hash, bin_signature) return nil if public_key.nil? Eth::Utils.public_key_to_address(public_key) end
Private Class Methods
hex_and_length?(string, len)
click to toggle source
# File lib/silkey/utils.rb, line 105 def hex_and_length?(string, len) return false if empty_hex?(string) return false unless hex?(string) remove0x(string).length == len end