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 Faker::Tezos #1359

Merged
merged 6 commits into from
Sep 17, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- [PR #1329](https://github.com/stympy/faker/pull/1329) Update docs on behavior of price [@softwaregravy](https://github.com/softwaregravy)

### Feature Request
- [PR #1359](https://github.com/stympy/faker/pull/1359) Add Faker::Tezos [@Pierre-Michard](https://github.com/Pierre-Michard)
- [PR #1366](https://github.com/stympy/faker/pull/1366) Add Faker::Seinfeld.business [@dsgraham](https://github.com/dsgraham)
- [PR #1358](https://github.com/stympy/faker/pull/1358) Add cat breed for Japanese [@yizknn](https://github.com/yizknn)
- [PR #1365](https://github.com/stympy/faker/pull/1365) Add Faker::Number.within [@QuantumWaver](https://github.com/QuantumWaver)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ Contents
- [Faker::Superhero](doc/superhero.md)
- [Faker::SwordArtOnline](doc/sword_art_online.md)
- [Faker::Team](doc/team.md)
- [Faker::Tezos](doc/tezos.md)
- [Faker::TheFreshPrinceOfBelAir](doc/the_fresh_prince_of_bel_air.md)
- [Faker::TheITCrowd](doc/the_it_crowd.md)
- [Faker::TheThickOfIt](doc/the_thick_of_it.md)
Expand Down
13 changes: 13 additions & 0 deletions doc/tezos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Faker::Tezos

It might be available in the next version.

```ruby
Faker::Tezos.account #=> "tz1eUsgK6aj752Fbxwk5sAoEFvSDnPjZ4qvk"

Faker::Tezos.contract #=> "KT1MroqeP15nnitB4CnNfkqHYa2NErhPPLWF"

Faker::Tezos.operation #=> "onygWYXJX3xNstFLv9PcCrhQdCkENC795xwSinmTEc1jsDN4VDa"

Faker::Tezos.signature #=> "edsigu165B7VFf3Dpw2QABVzEtCxJY2gsNBNcE3Ti7rRxtDUjqTFRpg67EdAQmY6YWPE5tKJDMnSTJDFu65gic8uLjbW2YwGvAZ"
```
1 change: 1 addition & 0 deletions lib/faker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,4 @@ def rand(max = nil)

require 'helpers/char'
require 'helpers/unique_generator'
require 'helpers/base58'
19 changes: 1 addition & 18 deletions lib/faker/bitcoin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,11 @@ def testnet_address

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.positive?
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)
Faker::Base58.encode(packed + checksum)
end
end
end
Expand Down
44 changes: 44 additions & 0 deletions lib/faker/tezos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

require 'digest'
require 'securerandom'

module Faker
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
22 changes: 22 additions & 0 deletions lib/helpers/base58.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Faker
module Base58
def self.encode(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.positive?
lv, mod = lv.divmod(base)
ret << alphabet[mod]
end

npad = str.match(/^#{0.chr}*/)[0].to_s.size
'1' * npad + ret.reverse
end
end
end
2 changes: 1 addition & 1 deletion test/test_determinism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def all_methods

def subclasses
Faker.constants.delete_if do |subclass|
%i[Base Bank Char ChileRut Config Date Internet Time VERSION].include?(subclass)
%i[Base Bank Char Base58 ChileRut Config Date Internet Time VERSION].include?(subclass)
end.sort
end

Expand Down
28 changes: 28 additions & 0 deletions test/test_faker_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::Tezos.contract.match(/^KT1[1-9A-Za-z][^OIl]{20,40}/)
end

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

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

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

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