Module: Eth::Utils

Extended by:
Utils
Included in:
Key::Decrypter, Key::Encrypter, Utils
Defined in:
lib/eth/utils.rb

Instance Method Summary collapse

Instance Method Details

#base256_to_int(str) ⇒ Object



26
27
28
# File 'lib/eth/utils.rb', line 26

def base256_to_int(str)
  RLP::Sedes.big_endian_int.deserialize str.sub(/\A(\x00)+/, '')
end

#bin_to_hex(string) ⇒ Object



18
19
20
# File 'lib/eth/utils.rb', line 18

def bin_to_hex(string)
  RLP::Utils.encode_hex string
end

#bin_to_prefixed_hex(binary) ⇒ Object



50
51
52
# File 'lib/eth/utils.rb', line 50

def bin_to_prefixed_hex(binary)
  prefix_hex bin_to_hex(binary)
end

#format_address(address) ⇒ Object



108
109
110
# File 'lib/eth/utils.rb', line 108

def format_address(address)
  Address.new(address).checksummed
end

#hash160(x) ⇒ Object



84
85
86
# File 'lib/eth/utils.rb', line 84

def hash160(x)
  ripemd160 sha256(x)
end

#hex_to_bin(string) ⇒ Object



22
23
24
# File 'lib/eth/utils.rb', line 22

def hex_to_bin(string)
  RLP::Utils.decode_hex remove_hex_prefix(string)
end

#int_to_base256(int) ⇒ Object



30
31
32
# File 'lib/eth/utils.rb', line 30

def int_to_base256(int)
  RLP::Sedes.big_endian_int.serialize int
end

#keccak256(x) ⇒ Object



68
69
70
# File 'lib/eth/utils.rb', line 68

def keccak256(x)
  Digest::SHA3.new(256).digest(x)
end

#keccak256_rlp(x) ⇒ Object



76
77
78
# File 'lib/eth/utils.rb', line 76

def keccak256_rlp(x)
  keccak256 RLP.encode(x)
end

#keccak512(x) ⇒ Object



72
73
74
# File 'lib/eth/utils.rb', line 72

def keccak512(x)
  Digest::SHA3.new(512).digest(x)
end

#normalize_address(address) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/eth/utils.rb', line 6

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



42
43
44
# File 'lib/eth/utils.rb', line 42

def prefix_hex(hex)
  hex.match(/\A0x/) ? hex : "0x#{hex}"
end

#prefix_message(message) ⇒ Object



54
55
56
# File 'lib/eth/utils.rb', line 54

def prefix_message(message)
  "\x19Ethereum Signed Message:\n#{message.length}#{message}"
end

#public_key_to_address(hex) ⇒ Object



58
59
60
61
62
# File 'lib/eth/utils.rb', line 58

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



46
47
48
# File 'lib/eth/utils.rb', line 46

def remove_hex_prefix(s)
  s[0,2] == '0x' ? s[2..-1] : s
end

#ripemd160(x) ⇒ Object



80
81
82
# File 'lib/eth/utils.rb', line 80

def ripemd160(x)
  Digest::RMD160.digest x
end

#sha256(x) ⇒ Object



64
65
66
# File 'lib/eth/utils.rb', line 64

def sha256(x)
  Digest::SHA256.digest x
end

#v_r_s_for(signature) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/eth/utils.rb', line 34

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

Returns:

  • (Boolean)


104
105
106
# File 'lib/eth/utils.rb', line 104

def valid_address?(address)
  Address.new(address).valid?
end

#zpad(x, l) ⇒ Object



88
89
90
# File 'lib/eth/utils.rb', line 88

def zpad(x, l)
  lpad x, BYTE_ZERO, l
end

#zpad_hex(s, l = 32) ⇒ Object



100
101
102
# File 'lib/eth/utils.rb', line 100

def zpad_hex(s, l=32)
  zpad decode_hex(s), l
end

#zpad_int(n, l = 32) ⇒ Object



96
97
98
# File 'lib/eth/utils.rb', line 96

def zpad_int(n, l=32)
  zpad encode_int(n), l
end

#zunpad(x) ⇒ Object



92
93
94
# File 'lib/eth/utils.rb', line 92

def zunpad(x)
  x.sub(/\A\x00+/, '')
end