Module: Eth::Utils
Instance Method Summary collapse
- #base256_to_int(str) ⇒ Object
- #bin_to_hex(string) ⇒ Object
- #bin_to_prefixed_hex(binary) ⇒ Object
- #format_address(address) ⇒ Object
- #from_rpc_signature_hex(signature) ⇒ Object
- #hash160(x) ⇒ Object
- #hex_to_bin(string) ⇒ Object
- #int_to_base256(int) ⇒ Object
- #keccak256(x) ⇒ Object
- #keccak256_rlp(x) ⇒ Object
- #keccak512(x) ⇒ Object
- #normalize_address(address) ⇒ Object
- #prefix_hex(hex) ⇒ Object
- #prefix_message(message) ⇒ Object
- #public_key_to_address(hex) ⇒ Object
- #remove_hex_prefix(s) ⇒ Object
- #ripemd160(x) ⇒ Object
- #sha256(x) ⇒ Object
- #v_r_s_for(signature) ⇒ Object
- #valid_address?(address) ⇒ Boolean
- #zpad(x, l) ⇒ Object
- #zpad_hex(s, l = 32) ⇒ Object
- #zpad_int(n, l = 32) ⇒ Object
- #zunpad(x) ⇒ Object
Instance Method Details
#base256_to_int(str) ⇒ Object
32 33 34 |
# File 'lib/eth/utils.rb', line 32 def base256_to_int(str) RLP::Sedes.big_endian_int.deserialize str.sub(/\A(\x00)+/, '') end |
#bin_to_hex(string) ⇒ Object
24 25 26 |
# File 'lib/eth/utils.rb', line 24 def bin_to_hex(string) RLP::Utils.encode_hex string end |
#bin_to_prefixed_hex(binary) ⇒ Object
71 72 73 |
# File 'lib/eth/utils.rb', line 71 def bin_to_prefixed_hex(binary) prefix_hex bin_to_hex(binary) end |
#format_address(address) ⇒ Object
125 126 127 |
# File 'lib/eth/utils.rb', line 125 def format_address(address) Address.new(address).checksummed end |
#from_rpc_signature_hex(signature) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/eth/utils.rb', line 40 def from_rpc_signature_hex(signature) buffer = Eth::Utils.hex_to_bin(signature).bytes if buffer.length != 65 raise ArgumentError.new('Invalid signature length') end v = buffer[64] v += 27 if v < 27 r = buffer[0..32] s = buffer[33..64] signature = [v, r, s].flatten.pack('C*') signature_hex = Eth::Util.bin_to_prefixed_hex(signature) payload = {v: v, r: r, s: s, signature: signature, signature_hex: signature_hex} return payload end |
#hash160(x) ⇒ Object
101 102 103 |
# File 'lib/eth/utils.rb', line 101 def hash160(x) ripemd160 sha256(x) end |
#hex_to_bin(string) ⇒ Object
28 29 30 |
# File 'lib/eth/utils.rb', line 28 def hex_to_bin(string) RLP::Utils.decode_hex remove_hex_prefix(string) end |
#int_to_base256(int) ⇒ Object
36 37 38 |
# File 'lib/eth/utils.rb', line 36 def int_to_base256(int) RLP::Sedes.big_endian_int.serialize int end |
#keccak256(x) ⇒ Object
85 86 87 |
# File 'lib/eth/utils.rb', line 85 def keccak256(x) Digest::SHA3.new(256).digest(x) end |
#keccak256_rlp(x) ⇒ Object
93 94 95 |
# File 'lib/eth/utils.rb', line 93 def keccak256_rlp(x) keccak256 RLP.encode(x) end |
#keccak512(x) ⇒ Object
89 90 91 |
# File 'lib/eth/utils.rb', line 89 def keccak512(x) Digest::SHA3.new(512).digest(x) end |
#normalize_address(address) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/eth/utils.rb', line 12 def normalize_address(address) if address.nil? || address == '' '' elsif address.size == 40 hex_to_bin address elsif address.size == 42 && address[0..1] == '0x' hex_to_bin address[2..-1] else address end end |
#prefix_hex(hex) ⇒ Object
63 64 65 |
# File 'lib/eth/utils.rb', line 63 def prefix_hex(hex) hex.match(/\A0x/) ? hex : "0x#{hex}" end |
#prefix_message(message) ⇒ Object
6 7 8 9 |
# File 'lib/eth/utils.rb', line 6 def () prefix = "\u0019Ethereum Signed Message:\n#{message.length}" return "#{prefix}#{message}" end |
#public_key_to_address(hex) ⇒ Object
75 76 77 78 79 |
# File 'lib/eth/utils.rb', line 75 def public_key_to_address(hex) bytes = hex_to_bin(hex) address_bytes = Utils.keccak256(bytes[1..-1])[-20..-1] format_address bin_to_prefixed_hex(address_bytes) end |
#remove_hex_prefix(s) ⇒ Object
67 68 69 |
# File 'lib/eth/utils.rb', line 67 def remove_hex_prefix(s) s[0,2] == '0x' ? s[2..-1] : s end |
#ripemd160(x) ⇒ Object
97 98 99 |
# File 'lib/eth/utils.rb', line 97 def ripemd160(x) Digest::RMD160.digest x end |
#sha256(x) ⇒ Object
81 82 83 |
# File 'lib/eth/utils.rb', line 81 def sha256(x) Digest::SHA256.digest x end |
#v_r_s_for(signature) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/eth/utils.rb', line 55 def v_r_s_for(signature) [ signature[0].bytes[0], Utils.base256_to_int(signature[1..32]), Utils.base256_to_int(signature[33..65]), ] end |
#valid_address?(address) ⇒ Boolean
121 122 123 |
# File 'lib/eth/utils.rb', line 121 def valid_address?(address) Address.new(address).valid? end |
#zpad(x, l) ⇒ Object
105 106 107 |
# File 'lib/eth/utils.rb', line 105 def zpad(x, l) lpad x, BYTE_ZERO, l end |
#zpad_hex(s, l = 32) ⇒ Object
117 118 119 |
# File 'lib/eth/utils.rb', line 117 def zpad_hex(s, l=32) zpad decode_hex(s), l end |
#zpad_int(n, l = 32) ⇒ Object
113 114 115 |
# File 'lib/eth/utils.rb', line 113 def zpad_int(n, l=32) zpad encode_int(n), l end |
#zunpad(x) ⇒ Object
109 110 111 |
# File 'lib/eth/utils.rb', line 109 def zunpad(x) x.sub(/\A\x00+/, '') end |