Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Blockchain namespace #1481

Merged
merged 5 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
34 changes: 34 additions & 0 deletions lib/faker/blockchain/bitcoin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require 'digest'
require 'securerandom'

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

def address
address_for(:main)
end

def testnet_address
address_for(:testnet)
end

protected

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
16 changes: 16 additions & 0 deletions lib/faker/blockchain/ethereum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Faker
class Blockchain
class Ethereum < Base
class << self
def address
hex_alphabet = '0123456789abcdef'
var = +'0x'
40.times { var << sample(shuffle(hex_alphabet.split(''))) }
var
end
end
end
end
end
46 changes: 46 additions & 0 deletions lib/faker/blockchain/tezos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

require 'digest'
require 'securerandom'

module Faker
class Blockchain
class Tezos < Base
class << self
PREFIXES = {
tz1: [6, 161, 159],
KT1: [2, 90, 121],
edpk: [13, 15, 37, 217],
edsk: [13, 15, 58, 7],
edsig: [9, 245, 205, 134, 18],
o: [5, 116]
}.freeze

def account
encode_tz(:tz1, 20)
end

def contract
encode_tz(:KT1, 20)
end

def operation
encode_tz(:o, 32)
end

def signature
encode_tz(:edsig, 64)
end

protected

def encode_tz(prefix, payload_size)
prefix = PREFIXES.fetch(prefix)
packed = prefix.map(&:chr).join('') + Faker::Config.random.bytes(payload_size)
checksum = Digest::SHA2.digest(Digest::SHA2.digest(packed))[0..3]
Faker::Base58.encode(packed + checksum)
end
end
end
end
end
32 changes: 0 additions & 32 deletions lib/faker/default/bitcoin.rb

This file was deleted.

14 changes: 0 additions & 14 deletions lib/faker/default/ethereum.rb

This file was deleted.

44 changes: 0 additions & 44 deletions lib/faker/default/tezos.rb

This file was deleted.

20 changes: 20 additions & 0 deletions lib/faker/deprecate/bitcoin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Faker
class Bitcoin < Base
class << self
extend Gem::Deprecate

def address
Faker::Blockchain::Bitcoin.address
end

def testnet_address
Faker::Blockchain::Bitcoin.testnet_address
end

deprecate :address, 'Faker::Blockchain::Bitcoin.address', 2018, 12
deprecate :testnet_address, 'Faker::Blockchain::Bitcoin.testnet_address', 2018, 12
end
end
end
15 changes: 15 additions & 0 deletions lib/faker/deprecate/ethereum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Faker
class Ethereum < Base
class << self
extend Gem::Deprecate

def address
Faker::Blockchain::Ethereum.address
end

deprecate :address, 'Faker::Blockchain::Tezos.address', 2018, 12
end
end
end
33 changes: 33 additions & 0 deletions lib/faker/deprecate/tezos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require 'digest'
require 'securerandom'

module Faker
class Tezos < Base
class << self
extend Gem::Deprecate

def account
Faker::Blockchain::Tezos.account
end

def contract
Faker::Blockchain::Tezos.contract
end

def operation
Faker::Blockchain::Tezos.operation
end

def signature
Faker::Blockchain::Tezos.signature
end

deprecate :account, 'Faker::Blockchain::Tezos.account', 2018, 12
deprecate :contract, 'Faker::Blockchain::Tezos.contract', 2018, 12
deprecate :operation, 'Faker::Blockchain::Tezos.operation', 2018, 12
deprecate :signature, 'Faker::Blockchain::Tezos.signature', 2018, 12
end
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

require_relative '../../test_helper'
require_relative '../test_helper'

class TestFakerBitcoin < Test::Unit::TestCase
class TestDeprecateBitcoin < Test::Unit::TestCase
def test_address
assert Faker::Bitcoin.address.match(/^[13][1-9A-Za-z][^OIl]{20,40}/)
end
Expand Down
13 changes: 13 additions & 0 deletions test/deprecate/test_deprecate_ethereum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require_relative '../test_helper'

class TestDeprecateEthereum < Test::Unit::TestCase
def setup
@tester = Faker::Ethereum
end

def test_address
assert @tester.address.match(/0x([a-fA-F0-9]{40})/)
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

require_relative '../../test_helper'
require_relative '../test_helper'

class TestFakerTezos < Test::Unit::TestCase
class TestDeprecateTezos < Test::Unit::TestCase
def test_contract
assert Faker::Tezos.contract.match(/^KT1[1-9A-Za-z][^OIl]{20,40}/)
end
Expand Down
21 changes: 21 additions & 0 deletions test/faker/blockchain/bitcoin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require_relative '../../test_helper'

class TestFakerBitcoin < Test::Unit::TestCase
def test_address
assert Faker::Blockchain::Bitcoin.address.match(/^[13][1-9A-Za-z][^OIl]{20,40}/)
end

def test_deterministic_address
Faker::Config.random = Random.new(42)
v = Faker::Blockchain::Bitcoin.address
Faker::Config.random = Random.new(42)
assert v == Faker::Blockchain::Bitcoin.address
end

def test_testnet_address
assert_match(/\A[mn][1-9A-Za-z]{32,34}\Z/, Faker::Blockchain::Bitcoin.testnet_address)
assert_not_match(/[OIl]/, Faker::Blockchain::Bitcoin.testnet_address)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class TestFakerEthereum < Test::Unit::TestCase
def setup
@tester = Faker::Ethereum
@tester = Faker::Blockchain::Ethereum
end

def test_address
Expand Down
28 changes: 28 additions & 0 deletions test/faker/blockchain/tezos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require_relative '../../test_helper'

class TestFakerTezos < Test::Unit::TestCase
def test_contract
assert Faker::Blockchain::Tezos.contract.match(/^KT1[1-9A-Za-z][^OIl]{20,40}/)
end

def test_account
assert Faker::Blockchain::Tezos.account.match(/^tz1[1-9A-Za-z][^OIl]{20,40}/)
end

def test_operation
assert Faker::Blockchain::Tezos.operation.match(/^o[1-9A-Za-z][^OIl]{20,40}/)
end

def test_signature
assert Faker::Blockchain::Tezos.signature.match(/^edsig[1-9A-Za-z][^OIl]{20,40}/)
end

def test_deterministic_contract
Faker::Config.random = Random.new(42)
v = Faker::Blockchain::Tezos.contract
Faker::Config.random = Random.new(42)
assert v == Faker::Blockchain::Tezos.contract
end
end
Loading