Skip to content

Instantly share code, notes, and snippets.

@icostan
Created November 14, 2018 08:09
Show Gist options
  • Save icostan/c005e824b52d02af11ed04ce1ac8819c to your computer and use it in GitHub Desktop.
Save icostan/c005e824b52d02af11ed04ce1ac8819c to your computer and use it in GitHub Desktop.
def bitcoin_base58_encode(ripe160_hash)
alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
value = ripe160_hash.to_i 16
output = ''
while value > 0
remainder = value % 58
value /= 58
output += alphabet[remainder]
end
output += alphabet[0] * [ripe160_hash].pack('H*').bytes.find_index{|b| b != 0}
output.reverse
end
def bitcoin_base58_decode(address)
alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
int_val = 0
address.reverse.chars.each_with_index do |char, index|
char_index = alphabet.index(char)
int_val += char_index * 58**index
end
bignum_to_bytes(int_val, 25).unpack('H*').first
end
def bitcoin_address_decode(address)
wrap_encode = bitcoin_base58_decode address
wrap_encode[2, 40]
end
def bitcoin_script(address)
hash160 = bitcoin_address_decode address
if address.start_with? '2'
"OP_HASH160 #{hash160} OP_EQUAL"
else
"OP_DUP OP_HASH160 #{hash160} OP_EQUALVERIFY OP_CHECKSIG"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment