cryptoconditions_ruby 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.gitmodules +3 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +26 -0
  6. data/.travis.yml +5 -0
  7. data/CODE_OF_CONDUCT.md +74 -0
  8. data/Gemfile +4 -0
  9. data/LICENSE.txt +201 -0
  10. data/README.md +22 -0
  11. data/Rakefile +6 -0
  12. data/bin/console +14 -0
  13. data/bin/setup +8 -0
  14. data/cryptoconditions_ruby.gemspec +34 -0
  15. data/lib/cryptoconditions_ruby/condition.rb +129 -0
  16. data/lib/cryptoconditions_ruby/crypto.rb +168 -0
  17. data/lib/cryptoconditions_ruby/exceptions.rb +8 -0
  18. data/lib/cryptoconditions_ruby/fulfillment.rb +138 -0
  19. data/lib/cryptoconditions_ruby/type_registry.rb +27 -0
  20. data/lib/cryptoconditions_ruby/types/base_sha_256_fulfillment.rb +15 -0
  21. data/lib/cryptoconditions_ruby/types/ed25519_fulfillment.rb +79 -0
  22. data/lib/cryptoconditions_ruby/types/inverted_threshold_sha_256_fulfillment.rb +15 -0
  23. data/lib/cryptoconditions_ruby/types/preimage_sha_256_fulfillment.rb +64 -0
  24. data/lib/cryptoconditions_ruby/types/threshold_sha_256_fulfillment.rb +343 -0
  25. data/lib/cryptoconditions_ruby/types/timeout_fulfillment.rb +46 -0
  26. data/lib/cryptoconditions_ruby/utils/base16.rb +28 -0
  27. data/lib/cryptoconditions_ruby/utils/base58.rb +53 -0
  28. data/lib/cryptoconditions_ruby/utils/byte_array.rb +16 -0
  29. data/lib/cryptoconditions_ruby/utils/bytes.rb +13 -0
  30. data/lib/cryptoconditions_ruby/utils/hasher.rb +27 -0
  31. data/lib/cryptoconditions_ruby/utils/hexlify.rb +13 -0
  32. data/lib/cryptoconditions_ruby/utils/predictor.rb +58 -0
  33. data/lib/cryptoconditions_ruby/utils/reader.rb +167 -0
  34. data/lib/cryptoconditions_ruby/utils/writer.rb +81 -0
  35. data/lib/cryptoconditions_ruby/version.rb +3 -0
  36. data/lib/cryptoconditions_ruby.rb +28 -0
  37. metadata +191 -0
@@ -0,0 +1,167 @@
1
+ class CryptoconditionsRuby::Utils::Reader
2
+ HIGH_BIT = 0x80
3
+ LOWER_SEVEN_BITS = 0x7F
4
+ MAX_INT_BYTES = 6
5
+
6
+ attr_accessor :bookmarks, :buffer, :cursor
7
+ def initialize(buffer)
8
+ @buffer = buffer
9
+ @cursor = 0
10
+ @bookmarks = []
11
+ end
12
+
13
+ def self.from_source(source)
14
+ source.is_a?(self) ? source : new(source)
15
+ end
16
+
17
+ def bookmark
18
+ self.bookmarks << cursor
19
+ end
20
+
21
+ def restore
22
+ self.cursor = bookmarks.pop
23
+ end
24
+
25
+ def ensure_available(num_bytes)
26
+ if buffer.length < cursor + num_bytes
27
+ raise RangeError.new("Tried to read #{num_bytes} bytes, but only #{buffer.length - cursor} bytes available")
28
+ end
29
+ end
30
+
31
+ def read_uint(length, peek: false)
32
+ if length > MAX_INT_BYTES
33
+ raise RangeError.new("Tried to read too large integer (requested: #{length}, max: #{MAX_INT_BYTES})")
34
+ end
35
+ ensure_available(length)
36
+ value = buffer[cursor...(cursor + length)]
37
+ self.cursor += length unless peek
38
+ CryptoconditionsRuby::Utils::Bytes.new(value).to_i(16)
39
+ end
40
+
41
+ def peek_uint(length)
42
+ read_uint(length, peek: true)
43
+ end
44
+
45
+ def skip_uint(length)
46
+ skip(length)
47
+ end
48
+
49
+ def read_var_uint
50
+ buffer = read_var_octet_string
51
+ if buffer.length > MAX_INT_BYTES
52
+ raise RangeError.new("UInt of length #{butter.length} too large to parse as integer(#{MAX_INT_BYTES})")
53
+ end
54
+ value = buffer[0...buffer.length]
55
+ CryptoconditionsRuby::Utils::Bytes.new(value).to_i(16)
56
+ end
57
+
58
+ def peek_var_uint
59
+ bookmark
60
+ read_var_uint.tap { restore }
61
+ end
62
+
63
+ def skip_var_uint
64
+ skip_var_octet_string
65
+ end
66
+
67
+ def read_octet_string(length)
68
+ read(length)
69
+ end
70
+
71
+ def peek_octet_string(length)
72
+ peek(length)
73
+ end
74
+
75
+ def skip_octet_string(length)
76
+ skip(length)
77
+ end
78
+
79
+ def read_length_prefix
80
+ length = read_uint8
81
+
82
+ if length & HIGH_BIT > 0
83
+ return read_uint(length & LOWER_SEVEN_BITS)
84
+ end
85
+ length
86
+ end
87
+
88
+ def read_var_octet_string
89
+ length = read_length_prefix
90
+ read(length)
91
+ end
92
+
93
+ def peek_var_octet_string
94
+ bookmark
95
+ read_var_octet_string.tap { restore }
96
+ end
97
+
98
+ def skip_var_octet_string
99
+ length = read_length_prefix
100
+ skip(length)
101
+ end
102
+
103
+ def read(num_bytes, peek: false)
104
+ ensure_available(num_bytes)
105
+
106
+ value = buffer[cursor...(cursor + num_bytes)]
107
+ self.cursor += num_bytes unless peek
108
+ value
109
+ end
110
+
111
+ def peek(num_bytes)
112
+ read(num_bytes, peek: true)
113
+ end
114
+
115
+ def skip(num_bytes)
116
+ ensure_available(num_bytes)
117
+ self.cursor += num_bytes
118
+ end
119
+
120
+ def read_uint8
121
+ read_uint(1)
122
+ end
123
+
124
+ def read_uint16
125
+ read_uint(2)
126
+ end
127
+
128
+ def read_uint32
129
+ read_uint(4)
130
+ end
131
+
132
+ def read_uint64
133
+ read_uint(8)
134
+ end
135
+
136
+ def peek_uint8
137
+ peek_uint(1)
138
+ end
139
+
140
+ def peek_uint16
141
+ peek_uint(2)
142
+ end
143
+
144
+ def peek_uint32
145
+ peek_uint(4)
146
+ end
147
+
148
+ def peek_uint64
149
+ peek_uint(8)
150
+ end
151
+
152
+ def skip_uint8
153
+ skip_uint(1)
154
+ end
155
+
156
+ def skip_uint16
157
+ skip_uint(2)
158
+ end
159
+
160
+ def skip_uint32
161
+ skip_uint(4)
162
+ end
163
+
164
+ def skip_uint64
165
+ skip_uint(8)
166
+ end
167
+ end
@@ -0,0 +1,81 @@
1
+ module CryptoconditionsRuby
2
+ module Utils
3
+ class Writer
4
+ attr_accessor :components
5
+ def initialize
6
+ @components = []
7
+ end
8
+
9
+ def write_uint(value, length)
10
+ raise TypeError.new('UInt must be an integer') unless value.is_a?(Integer)
11
+ raise TypeError.new('UInt must be positive') unless value >= 0
12
+ raise TypeError.new("UInt '#{value}' does not fit in '#{length}' bytes") if sprintf('%02b', value).length > length * 8
13
+
14
+ _buffer = (length - 1).times.map { 0 }.push(value).pack('C*')
15
+ write(_buffer)
16
+ end
17
+
18
+ def write_var_uint(value)
19
+ if value.is_a?(String)
20
+ write_var_octet_string(value)
21
+ else
22
+ raise TypeError.new('UInt must be an integer') unless value.is_a?(Integer)
23
+ raise TypeError.new('UInt must be positive') unless value >= 0
24
+
25
+ length_of_value = (sprintf('%02b', value).length / 8.0).ceil.to_i
26
+ _buffer = (length_of_value - 1).times.map { 0 }.push(value).pack('C*')
27
+ write_var_octet_string(_buffer)
28
+ end
29
+ end
30
+
31
+ def write_octet_string(_buffer, length)
32
+ raise TypeError, '_buffer must be an array of bytes' unless _buffer.respond_to?(:bytes)
33
+ raise ArgumentError, "Incorrect length for octet string (actual: #{_buffer.length}, expected: #{length})" unless _buffer.length == length
34
+
35
+ write(_buffer)
36
+ end
37
+
38
+ def write_var_octet_string(_buffer)
39
+ msb = 0x80
40
+ if _buffer.length <= 127
41
+ write_uint8(_buffer.length)
42
+ else
43
+ length_of_length = (format('%02b', _buffer.length).length / 8.0).ceil.to_i
44
+ write_uint8(msb | length_of_length)
45
+ write_uint(_buffer.length, length_of_length)
46
+ end
47
+ write(_buffer)
48
+ end
49
+
50
+ def write(in_bytes)
51
+ components.push(in_bytes)
52
+ end
53
+
54
+ def buffer
55
+ components.join
56
+ end
57
+
58
+ def write_uint8(value)
59
+ write_uint(value, 1)
60
+ end
61
+
62
+ def write_uint16(value)
63
+ write_uint(value, 2)
64
+ end
65
+
66
+ def write_uint32(value)
67
+ write_uint(value, 4)
68
+ end
69
+
70
+ def write_uint64(value)
71
+ write_uint(value, 8)
72
+ end
73
+
74
+ private
75
+
76
+ def write_out(in_bytes)
77
+ ByteArray.new(in_bytes).map { |x| format('%02x', x) }.join
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,3 @@
1
+ module CryptoconditionsRuby
2
+ VERSION = '0.5.1'.freeze
3
+ end
@@ -0,0 +1,28 @@
1
+ require 'cryptoconditions_ruby/version'
2
+
3
+ module CryptoconditionsRuby
4
+ module Utils
5
+ autoload :Writer, 'cryptoconditions_ruby/utils/writer'
6
+ autoload :Hasher, 'cryptoconditions_ruby/utils/hasher'
7
+ autoload :Predictor, 'cryptoconditions_ruby/utils/predictor'
8
+ autoload :Reader, 'cryptoconditions_ruby/utils/reader'
9
+ autoload :Bytes, 'cryptoconditions_ruby/utils/bytes'
10
+ autoload :Base58, 'cryptoconditions_ruby/utils/base58'
11
+ autoload :Base16, 'cryptoconditions_ruby/utils/base16'
12
+ autoload :Hexlify, 'cryptoconditions_ruby/utils/hexlify'
13
+ autoload :ByteArray, 'cryptoconditions_ruby/utils/byte_array'
14
+ end
15
+ module Types
16
+ autoload :BaseSha256Fulfillment, 'cryptoconditions_ruby/types/base_sha_256_fulfillment'
17
+ autoload :InvertedThresholdSha256Fulfillment, 'cryptoconditions_ruby/types/inverted_threshold_sha_256_fulfillment'
18
+ autoload :PreimageSha256Fulfillment, 'cryptoconditions_ruby/types/preimage_sha_256_fulfillment'
19
+ autoload :TimeoutFulfillment, 'cryptoconditions_ruby/types/timeout_fulfillment'
20
+ autoload :Ed25519Fulfillment, 'cryptoconditions_ruby/types/ed25519_fulfillment'
21
+ autoload :ThresholdSha256Fulfillment, 'cryptoconditions_ruby/types/threshold_sha_256_fulfillment'
22
+ end
23
+ autoload :TypeRegistry, 'cryptoconditions_ruby/type_registry'
24
+ autoload :Crypto, 'cryptoconditions_ruby/crypto'
25
+ autoload :Fulfillment, 'cryptoconditions_ruby/fulfillment'
26
+ autoload :Condition, 'cryptoconditions_ruby/condition'
27
+ autoload :Exceptions, 'cryptoconditions_ruby/exceptions'
28
+ end
metadata ADDED
@@ -0,0 +1,191 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cryptoconditions_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.1
5
+ platform: ruby
6
+ authors:
7
+ - Adam Groves
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rbnacl
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: base32
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.3.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: duplicate
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.1.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.1.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.13'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.13'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.10.4
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.10.4
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.48'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.48'
125
+ description: Cryptoconditions gem based on the python cryptoconditions library
126
+ email:
127
+ - adam.groves@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".gitmodules"
134
+ - ".rspec"
135
+ - ".rubocop.yml"
136
+ - ".travis.yml"
137
+ - CODE_OF_CONDUCT.md
138
+ - Gemfile
139
+ - LICENSE.txt
140
+ - README.md
141
+ - Rakefile
142
+ - bin/console
143
+ - bin/setup
144
+ - cryptoconditions_ruby.gemspec
145
+ - lib/cryptoconditions_ruby.rb
146
+ - lib/cryptoconditions_ruby/condition.rb
147
+ - lib/cryptoconditions_ruby/crypto.rb
148
+ - lib/cryptoconditions_ruby/exceptions.rb
149
+ - lib/cryptoconditions_ruby/fulfillment.rb
150
+ - lib/cryptoconditions_ruby/type_registry.rb
151
+ - lib/cryptoconditions_ruby/types/base_sha_256_fulfillment.rb
152
+ - lib/cryptoconditions_ruby/types/ed25519_fulfillment.rb
153
+ - lib/cryptoconditions_ruby/types/inverted_threshold_sha_256_fulfillment.rb
154
+ - lib/cryptoconditions_ruby/types/preimage_sha_256_fulfillment.rb
155
+ - lib/cryptoconditions_ruby/types/threshold_sha_256_fulfillment.rb
156
+ - lib/cryptoconditions_ruby/types/timeout_fulfillment.rb
157
+ - lib/cryptoconditions_ruby/utils/base16.rb
158
+ - lib/cryptoconditions_ruby/utils/base58.rb
159
+ - lib/cryptoconditions_ruby/utils/byte_array.rb
160
+ - lib/cryptoconditions_ruby/utils/bytes.rb
161
+ - lib/cryptoconditions_ruby/utils/hasher.rb
162
+ - lib/cryptoconditions_ruby/utils/hexlify.rb
163
+ - lib/cryptoconditions_ruby/utils/predictor.rb
164
+ - lib/cryptoconditions_ruby/utils/reader.rb
165
+ - lib/cryptoconditions_ruby/utils/writer.rb
166
+ - lib/cryptoconditions_ruby/version.rb
167
+ homepage: https://github.com/LicenseRocks/cryptoconditions_ruby
168
+ licenses:
169
+ - Apache License 2.0
170
+ metadata: {}
171
+ post_install_message:
172
+ rdoc_options: []
173
+ require_paths:
174
+ - lib
175
+ required_ruby_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ required_rubygems_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ requirements: []
186
+ rubyforge_project:
187
+ rubygems_version: 2.5.2
188
+ signing_key:
189
+ specification_version: 4
190
+ summary: Cryptoconditions for Ruby
191
+ test_files: []