Sha256: 4d04eb8b20902f27fd79b43d5ae0b1565b113fb8d3a97ee6d3d3757c3eb85185

Contents?: true

Size: 1.45 KB

Versions: 18

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require 'digest'
require 'securerandom'

module Faker
  class Blockchain
    class Bitcoin < Base
      class << self
        # @private
        PROTOCOL_VERSIONS = {
          main: 0,
          testnet: 111
        }.freeze

        ##
        # Produces a Bitcoin wallet address
        #
        # @return [String]
        #
        # @example
        #   Faker::Blockchain::Bitcoin.address
        #     #=> "147nDP22h3pHrLt2qykTH4txUwQh1ccaXp"
        #
        # @faker.version 1.9.2
        def address
          address_for(:main)
        end

        ##
        # Produces a Bitcoin testnet address
        #
        # @return [String]
        #
        # @example
        #   Faker::Blockchain::Bitcoin.testnet_address
        #     #=> "n4YjRyYD6V6zREpk6opqESDqD3KYnMdVEB"
        #
        # @faker.version 1.9.2
        def testnet_address
          address_for(:testnet)
        end

        protected

        ##
        # Generates a random Bitcoin address for the given network
        #
        # @param network [Symbol] The name of network protocol to generate an address for
        # @return [String] A Bitcoin address
        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]
          Faker::Base58.encode(packed + checksum)
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
faker-2.16.0 lib/faker/blockchain/bitcoin.rb
faker-2.15.1 lib/faker/blockchain/bitcoin.rb
faker-2.15.0 lib/faker/blockchain/bitcoin.rb
faker-2.14.0 lib/faker/blockchain/bitcoin.rb
faker-2.13.0 lib/faker/blockchain/bitcoin.rb
faker-2.12.0 lib/faker/blockchain/bitcoin.rb
faker-2.11.0 lib/faker/blockchain/bitcoin.rb
faker-2.10.2 lib/faker/blockchain/bitcoin.rb
faker-2.10.1 lib/faker/blockchain/bitcoin.rb
faker-2.10.0 lib/faker/blockchain/bitcoin.rb
faker-2.9.0 lib/faker/blockchain/bitcoin.rb
faker-2.8.1 lib/faker/blockchain/bitcoin.rb
faker-2.8.0 lib/faker/blockchain/bitcoin.rb
faker-2.7.0 lib/faker/blockchain/bitcoin.rb
faker-2.6.0 lib/faker/blockchain/bitcoin.rb
faker-2.5.0 lib/faker/blockchain/bitcoin.rb
faker-2.4.0 lib/faker/blockchain/bitcoin.rb
faker-2.3.0 lib/faker/blockchain/bitcoin.rb