Sha256: e7ecbe27f09cbab85e752b15193434e02fb70d97bfc5265ba17531574da1950d

Contents?: true

Size: 1.02 KB

Versions: 7

Compression:

Stored size: 1.02 KB

Contents

require 'digest'
require 'securerandom'

module Faker
  class Bitcoin < Base
    class << self

      PROTOCOL_VERSIONS = {
        main: 0,
        testnet: 111
      }

      def address
        address_for(:main)
      end

      def testnet_address
        address_for(:testnet)
      end

      protected

      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 = ''
        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

      def address_for(network)
        version = PROTOCOL_VERSIONS.fetch(network)
        packed = version.chr + Faker::Config.random.bytes(20)
        checksum = Digest::SHA2.digest(Digest::SHA2.digest(packed))[0..3]
        base58(packed + checksum)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
faker-1.8.7 lib/faker/bitcoin.rb
faker-1.8.6 lib/faker/bitcoin.rb
faker-1.8.5 lib/faker/bitcoin.rb
faker-1.8.4 lib/faker/bitcoin.rb
faker-1.8.3 lib/faker/bitcoin.rb
faker-1.8.2 lib/faker/bitcoin.rb
faker-1.8.0 lib/faker/bitcoin.rb