forked from faker-ruby/faker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Blockchain namespace (faker-ruby#1481)
* Deprecate ::Bitcoin * Deprecate ::Ethereum * Deprecate ::Tezos * Update unreleased_README.md * Minor fixes
- Loading branch information
Showing
19 changed files
with
237 additions
and
98 deletions.
There are no files selected for viewing
0
doc/unreleased/default/bitcoin.md → doc/unreleased/blockchain/bitcoin.md
100644 → 100755
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
4 changes: 2 additions & 2 deletions
4
test/faker/default/test_faker_bitcoin.rb → test/deprecate/test_deprecate_bitcoin.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
4 changes: 2 additions & 2 deletions
4
test/faker/default/test_faker_tezos.rb → test/deprecate/test_deprecate_tezos.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.