Sha256: 530487c46225bd0c1f552ddf75865ebc81a0d93c5df6dbb6e31138e0bee020a0

Contents?: true

Size: 1.48 KB

Versions: 20

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

require 'openssl'
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 = OpenSSL::Digest::SHA256.digest(OpenSSL::Digest::SHA256.digest(packed))[0..3]
          Faker::Base58.encode(packed + checksum)
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
faker-3.5.1 lib/faker/blockchain/bitcoin.rb
faker-3.4.2 lib/faker/blockchain/bitcoin.rb
faker-3.4.1 lib/faker/blockchain/bitcoin.rb
faker-3.3.1 lib/faker/blockchain/bitcoin.rb
faker-3.3.0 lib/faker/blockchain/bitcoin.rb
faker-3.2.3 lib/faker/blockchain/bitcoin.rb
faker-3.2.2 lib/faker/blockchain/bitcoin.rb
faker-3.2.1 lib/faker/blockchain/bitcoin.rb
faker-3.2.0 lib/faker/blockchain/bitcoin.rb
faker-3.1.1 lib/faker/blockchain/bitcoin.rb
faker-3.1.0 lib/faker/blockchain/bitcoin.rb
faker-3.0.0 lib/faker/blockchain/bitcoin.rb
faker-2.23.0 lib/faker/blockchain/bitcoin.rb
faker-2.22.0 lib/faker/blockchain/bitcoin.rb
faker-2.21.0 lib/faker/blockchain/bitcoin.rb
faker-2.20.0 lib/faker/blockchain/bitcoin.rb
faker-2.19.0 lib/faker/blockchain/bitcoin.rb
logstash-output-scalyr-0.1.7 vendor/bundle/jruby/2.5.0/bundler/gems/faker-e1bd4a5a5777/lib/faker/blockchain/bitcoin.rb
faker-2.18.0 lib/faker/blockchain/bitcoin.rb
faker-2.17.0 lib/faker/blockchain/bitcoin.rb