Module: GasfreeSdk::Base58
- Defined in:
- lib/gasfree_sdk/base58.rb
Overview
Base58 implementation for TRON address encoding/decoding
Constant Summary collapse
- ALPHABET =
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
Class Method Summary collapse
-
.base58_to_binary(string) ⇒ String
Convert Base58 string to binary data.
-
.binary_to_base58(bytes) ⇒ String
Convert binary data to Base58 string.
Class Method Details
.base58_to_binary(string) ⇒ String
Convert Base58 string to binary data
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/gasfree_sdk/base58.rb', line 12 def base58_to_binary(string) int_val = 0 base = ALPHABET.size string.each_char do |char| int_val = (int_val * base) + ALPHABET.index(char) end # Convert to bytes bytes = [] while int_val.positive? bytes.unshift(int_val & 0xff) int_val >>= 8 end # Handle leading zeros string.each_char do |char| break if char != "1" bytes.unshift(0) end bytes.pack("C*") end |
.binary_to_base58(bytes) ⇒ String
Convert binary data to Base58 string
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/gasfree_sdk/base58.rb', line 39 def binary_to_base58(bytes) # Convert bytes to integer int_val = 0 bytes.each_byte do |byte| int_val = (int_val << 8) + byte end # Convert to base58 result = "" base = ALPHABET.size while int_val.positive? int_val, remainder = int_val.divmod(base) result = ALPHABET[remainder] + result end # Handle leading zeros bytes.each_byte do |byte| break if byte != 0 result = "1#{result}" end result.empty? ? "1" : result end |