Class: SolanaRuby::Utils
- Inherits:
-
Object
- Object
- SolanaRuby::Utils
- Defined in:
- lib/solana_ruby/utils.rb
Class Method Summary collapse
-
.base58_to_bytes(base58_string) ⇒ Object
Converts a Base58-encoded string to a byte array.
-
.bytes_to_base58(bytes) ⇒ Object
Converts a byte array to a Base58-encoded string.
-
.decode_length(bytes) ⇒ Object
Decodes a length-prefixed byte array using a variable-length encoding.
-
.encode_length(length) ⇒ Object
Encodes a length as a variable-length byte array.
-
.sha256(data) ⇒ Object
Computes the SHA-256 hash of the given data and returns it as a hexadecimal string.
Class Method Details
.base58_to_bytes(base58_string) ⇒ Object
Converts a Base58-encoded string to a byte array.
50 51 52 53 54 55 56 |
# File 'lib/solana_ruby/utils.rb', line 50 def base58_to_bytes(base58_string) raise ArgumentError, "Input must be a non-empty string" unless base58_string.is_a?(String) && !base58_string.empty? Base58.base58_to_binary(base58_string, :bitcoin).bytes rescue ArgumentError raise "Invalid Base58 string: #{base58_string}" end |
.bytes_to_base58(bytes) ⇒ Object
Converts a byte array to a Base58-encoded string.
43 44 45 46 47 |
# File 'lib/solana_ruby/utils.rb', line 43 def bytes_to_base58(bytes) raise ArgumentError, "Input must be an array of bytes" unless bytes.is_a?(Array) Base58.binary_to_base58(bytes.pack('C*'), :bitcoin) end |
.decode_length(bytes) ⇒ Object
Decodes a length-prefixed byte array using a variable-length encoding.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/solana_ruby/utils.rb', line 8 def decode_length(bytes) raise ArgumentError, "Input must be an array of bytes" unless bytes.is_a?(Array) length = 0 size = 0 loop do raise "Unexpected end of bytes during length decoding" if bytes.empty? byte = bytes.shift length |= (byte & 0x7F) << (size * 7) size += 1 break if (byte & 0x80).zero? end length end |
.encode_length(length) ⇒ Object
Encodes a length as a variable-length byte array.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/solana_ruby/utils.rb', line 25 def encode_length(length) raise ArgumentError, "Length must be a non-negative integer" unless length.is_a?(Integer) && length >= 0 bytes = [] loop do byte = length & 0x7F length >>= 7 if length.zero? bytes << byte break else bytes << (byte | 0x80) end end bytes end |
.sha256(data) ⇒ Object
Computes the SHA-256 hash of the given data and returns it as a hexadecimal string.
59 60 61 62 63 |
# File 'lib/solana_ruby/utils.rb', line 59 def sha256(data) raise ArgumentError, "Data must be a string" unless data.is_a?(String) Digest::SHA256.hexdigest(data) end |