File: tezos.rb

package info (click to toggle)
ruby-faker 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,360 kB
  • sloc: ruby: 20,654; makefile: 6; sh: 6
file content (134 lines) | stat: -rw-r--r-- 3,364 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# frozen_string_literal: true

require 'securerandom'

module Faker
  class Blockchain
    class Tezos < Base
      class << self
        # @private
        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],
          B: [1, 52],
          o: [5, 116]
        }.freeze

        ##
        # Produces a random Tezos account address
        #
        # @return [String]
        #
        # @example
        #   Faker::Blockchain::Tezos.account
        #     #=> "tz1eUsgK6aj752Fbxwk5sAoEFvSDnPjZ4qvk"
        #
        # @faker.version 1.9.2
        def account
          encode_tz(:tz1, 20)
        end

        ##
        # Produces a random Tezos contract
        #
        # @return [String]
        #
        # @example
        #   Faker::Blockchain::Tezos.contract
        #     #=> "KT1MroqeP15nnitB4CnNfkqHYa2NErhPPLWF"
        #
        # @faker.version 1.9.2
        def contract
          encode_tz(:KT1, 20)
        end

        ##
        # Produces a random Tezos operation
        #
        # @return [String]
        #
        # @example
        #   Faker::Blockchain::Tezos.operation
        #     #=> "onygWYXJX3xNstFLv9PcCrhQdCkENC795xwSinmTEc1jsDN4VDa"
        #
        # @faker.version 1.9.2
        def operation
          encode_tz(:o, 32)
        end

        ##
        # Produces a random Tezos block
        #
        # @return [String]
        #
        # @example
        #   Faker::Blockchain::Tezos.block
        #     #=> "BMbhs2rkY1dvAkAyRytvPsjFQ2RiPrBhYkxvWpY65dzkdSuw58a"
        #
        # @faker.version 1.9.4
        def block
          encode_tz(:B, 32)
        end

        ##
        # Produces a random Tezos signature
        #
        # @return [String]
        #
        # @example
        #   Faker::Blockchain::Tezos.signature
        #     #=> "edsigu165B7VFf3Dpw2QABVzEtCxJY2gsNBNcE3Ti7rRxtDUjqTFRpg67EdAQmY6YWPE5tKJDMnSTJDFu65gic8uLjbW2YwGvAZ"
        #
        # @faker.version 1.9.2
        def signature
          encode_tz(:edsig, 64)
        end

        ##
        # Produces a random Tezos public key
        #
        # @return [String]
        #
        # @example
        #   Faker::Blockchain::Tezos.public_key
        #     #=> "edpkuib9x8QXRc5nWwHUg7U1dXsVmaUrUNU5sX9pVEEvwbMVdfMCeq"
        #
        # @faker.version 2.15.2
        def public_key
          encode_tz(:edpk, 32)
        end

        ##
        # Produces a random Tezos public key
        #
        # @return [String]
        #
        # @example
        #   Faker::Blockchain::Tezos.secret_key
        #     #=> "edsk3HZCAGEGpzQPnQUwQeFY4ESanFhQCgLpKriQw8GHyhKCrjHawv"
        #
        # @faker.version 2.15.2
        def secret_key
          encode_tz(:edsk, 32)
        end

        protected

        ##
        # @param prefix [Symbol]
        # @param payload_size [Integer] The size of the payload
        #
        # @return [String]
        def encode_tz(prefix, payload_size)
          prefix = PREFIXES.fetch(prefix)
          packed = prefix.map(&:chr).join + Faker::Config.random.bytes(payload_size)
          checksum = OpenSSL::Digest::SHA256.digest(OpenSSL::Digest::SHA256.digest(packed))[0..3]
          Faker::Base58.encode(packed + checksum)
        end
      end
    end
  end
end