Module: Platon::Utils

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

Instance Method Summary collapse

Instance Method Details

#base256_to_int(str) ⇒ Object



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

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

#bech32_to_bin(bech32Address) ⇒ Object

bech32 address to bin



66
67
68
69
# File 'lib/platon/utils.rb', line 66

def bech32_to_bin(bech32Address)
  address = decode_bech32_address(bech32Address)
  hex_to_bin address
end

#bin_to_hex(string) ⇒ Object



18
19
20
# File 'lib/platon/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/platon/utils.rb', line 50

def bin_to_prefixed_hex(binary)
  prefix_hex bin_to_hex(binary)
end

#decode_bech32_address(bech32Address) ⇒ Object

Resolve the bech32 address

@method decode_bech32_address
@param {String} bech32Address
@return {String} formatted address
eg: Platon::Utils.decode_bech32_address("atp1kh9dktnszj04zn6d8ae9edhqfmt4awx94n6h4m")


85
86
87
88
89
90
91
92
93
94
95
# File 'lib/platon/utils.rb', line 85

def decode_bech32_address(bech32Address)
  if is_bech32_address?(bech32Address) ## is_bech32_address? ## TODO
    
    segwit_addr = SegwitAddr.new(bech32Address)
    address = segwit_addr.to_script_pubkey
    if address
      return "0x" + address
    end
  end
  return ''
end

#format_address(address) ⇒ Object



158
159
160
# File 'lib/platon/utils.rb', line 158

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

#hash160(x) ⇒ Object



134
135
136
# File 'lib/platon/utils.rb', line 134

def hash160(x)
  ripemd160 sha256(x)
end

#hex_to_bin(string) ⇒ Object



22
23
24
# File 'lib/platon/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/platon/utils.rb', line 30

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

#is_bech32_address?(bech32Address) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
# File 'lib/platon/utils.rb', line 71

def is_bech32_address?(bech32Address)
  return false if bech32Address.length != 42
  hrp,data,spec = Bech32.decode bech32Address
  return false if data == nil
  return true
end

#keccak256(x) ⇒ Object



118
119
120
# File 'lib/platon/utils.rb', line 118

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

#keccak256_rlp(x) ⇒ Object



126
127
128
# File 'lib/platon/utils.rb', line 126

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

#keccak512(x) ⇒ Object



122
123
124
# File 'lib/platon/utils.rb', line 122

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/platon/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/platon/utils.rb', line 42

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

#prefix_message(message) ⇒ Object



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

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

#public_key_to_address(hex) ⇒ Object



59
60
61
62
63
# File 'lib/platon/utils.rb', line 59

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/platon/utils.rb', line 46

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

#ripemd160(x) ⇒ Object



130
131
132
# File 'lib/platon/utils.rb', line 130

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

#sha256(x) ⇒ Object



114
115
116
# File 'lib/platon/utils.rb', line 114

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

#to_bech32_address(hrp, address) ⇒ Object

Platon::Utils.to_bech32_address(“atp”,“0xb5cadb2e70149f514f4d3f725cb6e04ed75eb8c5”)



# File 'lib/platon/utils.rb', line 97


#v_r_s_for(signature) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/platon/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)


154
155
156
# File 'lib/platon/utils.rb', line 154

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

#zpad(x, l) ⇒ Object



138
139
140
# File 'lib/platon/utils.rb', line 138

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

#zpad_hex(s, l = 32) ⇒ Object



150
151
152
# File 'lib/platon/utils.rb', line 150

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

#zpad_int(n, l = 32) ⇒ Object



146
147
148
# File 'lib/platon/utils.rb', line 146

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

#zunpad(x) ⇒ Object



142
143
144
# File 'lib/platon/utils.rb', line 142

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