Class: Ipfs::Base58
- Inherits:
-
Object
- Object
- Ipfs::Base58
- Defined in:
- lib/ruby-ipfs-http-client/base.rb
Constant Summary collapse
- ALPHABET =
'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
- BASE =
ALPHABET.length
Class Method Summary collapse
- .decode(number) ⇒ Object
- .encode(base10_number) ⇒ Object
- .to_base10(base58_number) ⇒ Object
- .to_base58(base10_number) ⇒ Object
- .valid?(number) ⇒ Boolean
Class Method Details
.decode(number) ⇒ Object
6 7 8 9 10 |
# File 'lib/ruby-ipfs-http-client/base.rb', line 6 def self.decode(number) valid?(number) \ ? to_base10(number) : 0 end |
.encode(base10_number) ⇒ Object
26 27 28 29 30 |
# File 'lib/ruby-ipfs-http-client/base.rb', line 26 def self.encode(base10_number) base10_number.is_a?(Integer) \ ? to_base58(base10_number) \ : '' end |
.to_base10(base58_number) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/ruby-ipfs-http-client/base.rb', line 12 def self.to_base10(base58_number) base58_number .reverse .split(//) .each_with_index .reduce(0) do |base10_number, (base58_numeral, index)| base10_number + ALPHABET.index(base58_numeral) * (BASE**index) end end |
.to_base58(base10_number) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ruby-ipfs-http-client/base.rb', line 32 def self.to_base58(base10_number) base58_number = '' begin base58_number << ALPHABET[base10_number % BASE] base10_number /= BASE end while base10_number > 0 base58_number.reverse end |
.valid?(number) ⇒ Boolean
22 23 24 |
# File 'lib/ruby-ipfs-http-client/base.rb', line 22 def self.valid?(number) number.match?(/\A[#{ALPHABET}]+\z/) end |