module Bluzelle::Utils
Public Instance Methods
bech32_convert_bits(bytes, from_bits: 8, to_bits: 5, pad: false)
click to toggle source
# File lib/bluzelle/utils.rb, line 57 def bech32_convert_bits(bytes, from_bits: 8, to_bits: 5, pad: false) Bitcoin::Bech32.convert_bits(bytes, from_bits: from_bits, to_bits: to_bits, pad: pad) end
bech32_encode(prefix, word)
click to toggle source
# File lib/bluzelle/utils.rb, line 53 def bech32_encode(prefix, word) Bitcoin::Bech32.encode(prefix, word) end
bip32_from_seed(seed)
click to toggle source
# File lib/bluzelle/utils.rb, line 81 def bip32_from_seed(seed) MoneyTree::Master.new(seed_hex: seed) end
bip39_mnemonic_to_seed(mnemonic)
click to toggle source
# File lib/bluzelle/utils.rb, line 61 def bip39_mnemonic_to_seed(mnemonic) BipMnemonic.to_seed(mnemonic: mnemonic) end
compressed_pub_key(key)
click to toggle source
# File lib/bluzelle/utils.rb, line 75 def compressed_pub_key(key) public_key = key.public_key public_key.group.point_conversion_form = :compressed public_key.to_hex.rjust(66, '0') end
convert_lease(lease)
click to toggle source
# File lib/bluzelle/utils.rb, line 102 def convert_lease(lease) return '0' if lease.nil? seconds = 0 seconds += lease.dig(:days).nil? ? 0 : (lease.dig(:days).to_i * 24 * 60 * 60) seconds += lease.dig(:hours).nil? ? 0 : (lease.dig(:hours).to_i * 60 * 60) seconds += lease.dig(:minutes).nil? ? 0 : (lease.dig(:minutes).to_i * 60) seconds += lease.dig(:seconds).nil? ? 0 : lease.dig(:seconds).to_i (seconds / Constants::BLOCK_TIME_IN_SECONDS).to_s end
create_ec_pair(private_key)
click to toggle source
# File lib/bluzelle/utils.rb, line 85 def create_ec_pair(private_key) Bitcoin::Key.new(private_key, nil, { compressed: false }) end
decode_json(str)
click to toggle source
# File lib/bluzelle/utils.rb, line 161 def decode_json(str) JSON.parse(str) end
ecdsa_sign(payload, private_key)
click to toggle source
# File lib/bluzelle/utils.rb, line 149 def ecdsa_sign(payload, private_key) pk = Secp256k1::PrivateKey.new(privkey: hex_to_bin(private_key), raw: true) rs = pk.ecdsa_sign(payload) r = rs.slice(0, 32).read_string.reverse s = rs.slice(32, 32).read_string.reverse "#{r}#{s}" end
encode_json(obj)
click to toggle source
# File lib/bluzelle/utils.rb, line 157 def encode_json(obj) JSON.generate(obj) end
extract_error_message(str)
click to toggle source
# File lib/bluzelle/utils.rb, line 169 def extract_error_message(str) offset1 = str.index(': ') return str if offset1.nil? prefix = str.slice(0, offset1) case prefix when 'insufficient fee' return str.slice(offset1 + 2, str.length) end offset2 = str.index(': ', offset1 + 1) str[(offset1 + 2)..(offset2 - 1)] end
get_address(pub_key)
click to toggle source
# File lib/bluzelle/utils.rb, line 35 def get_address(pub_key) hash = rmd_160_digest(sha_256_digest([pub_key].pack('H*'))) word = bech32_convert_bits(to_bytes(hash)) bech32_encode(Constants::PREFIX, word) end
get_ec_private_key(mnemonic)
click to toggle source
# File lib/bluzelle/utils.rb, line 13 def get_ec_private_key(mnemonic) seed = bip39_mnemonic_to_seed(mnemonic) node = bip32_from_seed(seed) child = node.node_for_path(Constants::PATH) ec_pair = create_ec_pair(child.private_key.to_hex) ec_pair.priv end
get_ec_public_key_from_priv(priv)
click to toggle source
# File lib/bluzelle/utils.rb, line 21 def get_ec_public_key_from_priv(priv) key = open_key(priv) compressed_pub_key(key) end
hex_to_bin(hex_str)
click to toggle source
# File lib/bluzelle/utils.rb, line 165 def hex_to_bin(hex_str) [hex_str].pack('H*') end
make_random_string(length = 32)
click to toggle source
# File lib/bluzelle/utils.rb, line 89 def make_random_string(length = 32) random_string = '' chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.chars 1.upto(length) { random_string << chars.sample } random_string end
open_key(priv)
click to toggle source
# File lib/bluzelle/utils.rb, line 65 def open_key(priv) group = OpenSSL::PKey::EC::Group.new('secp256k1') key = OpenSSL::PKey::EC.new(group) key.private_key = OpenSSL::BN.new(priv, 16) key.public_key = group.generator.mul(key.private_key) key end
rmd_160_digest(hex)
click to toggle source
# File lib/bluzelle/utils.rb, line 45 def rmd_160_digest(hex) Digest::RMD160.digest hex end
sha_256_digest(hex)
click to toggle source
# File lib/bluzelle/utils.rb, line 49 def sha_256_digest(hex) Digest::SHA256.digest hex end
sort_hash(hash)
click to toggle source
# File lib/bluzelle/utils.rb, line 115 def sort_hash(hash) hash_clone = hash.clone hash_clone.each do |key, value| hash_clone[key] = sort_hash(value) if value.is_a?(Hash) next unless value.is_a?(Array) arr = [] hash_clone[key].each do |el| arr << sort_hash(el) end hash_clone[key] = arr end hash_clone.sort.to_h end
stringify_keys(hash)
click to toggle source
# File lib/bluzelle/utils.rb, line 135 def stringify_keys(hash) res = {} hash.each do |key, value| if value.is_a?(Hash) res[key.to_s] = stringify_keys(value) next end res[key.to_s] = value end res end
to_base64(str)
click to toggle source
# File lib/bluzelle/utils.rb, line 98 def to_base64(str) Base64.strict_encode64(str) end
to_bytes(obj)
click to toggle source
# File lib/bluzelle/utils.rb, line 41 def to_bytes(obj) obj.bytes end
validate_address(address, mnemonic)
click to toggle source
# File lib/bluzelle/utils.rb, line 26 def validate_address(address, mnemonic) priv_key = get_ec_private_key(mnemonic) pub_key = get_ec_public_key_from_priv(priv_key) if get_address(pub_key) != address raise ArgumentError, 'Bad credentials - verify your address and mnemonic' end end