class FactoryHelper::Bitcoin
Constants
- PROTOCOL_VERSIONS
Public Class Methods
address()
click to toggle source
# File lib/factory-helper/bitcoin.rb, line 14 def address address_for(:main) end
testnet_address()
click to toggle source
# File lib/factory-helper/bitcoin.rb, line 18 def testnet_address address_for(:testnet) end
Private Class Methods
address_for(network)
click to toggle source
# File lib/factory-helper/bitcoin.rb, line 41 def address_for(network) hex_160_bit = 0xffffffffffffffffffffffffffffffffffffffff version = PROTOCOL_VERSIONS.fetch(network) hash = FactoryHelper::Config.random.rand(hex_160_bit).to_s(16) packed = version.chr + [hash].pack("H*") checksum = Digest::SHA2.digest(Digest::SHA2.digest(packed))[0..3] base58(packed + checksum) end
base58(str)
click to toggle source
# File lib/factory-helper/bitcoin.rb, line 24 def base58(str) alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' base = alphabet.size lv = 0 str.split('').reverse.each_with_index { |v,i| lv += v.unpack('C')[0] * 256**i } ret = ''.dup while lv > 0 do lv, mod = lv.divmod(base) ret << alphabet[mod] end npad = str.match(/^#{0.chr}*/)[0].to_s.size '1'*npad + ret.reverse end