tapyrus 0.2.13 → 0.3.2

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.
@@ -1,12 +1,10 @@
1
1
  module Tapyrus
2
2
  class TxChecker
3
- attr_reader :tx
4
- attr_reader :input_index
5
- attr_reader :amount
3
+ attr_accessor :tx
4
+ attr_accessor :input_index
6
5
 
7
- def initialize(tx: nil, amount: 0, input_index: nil)
6
+ def initialize(tx: nil, input_index: nil)
8
7
  @tx = tx
9
- @amount = amount
10
8
  @input_index = input_index
11
9
  end
12
10
 
@@ -14,15 +12,13 @@ module Tapyrus
14
12
  # @param [String] script_sig a signature with hex format.
15
13
  # @param [String] pubkey a public key with hex format.
16
14
  # @param [Tapyrus::Script] script_code
17
- # @param [Integer] sig_version
18
15
  # @return [Boolean] if check is passed return true, otherwise false.
19
- def check_sig(script_sig, pubkey, script_code, sig_version)
16
+ def check_sig(script_sig, pubkey, script_code)
20
17
  return false if script_sig.empty?
21
18
  script_sig = script_sig.htb
22
19
  hash_type = script_sig[-1].unpack('C').first
23
20
  sig = script_sig[0..-2]
24
- sighash =
25
- tx.sighash_for_input(input_index, script_code, hash_type: hash_type, amount: amount, sig_version: sig_version)
21
+ sighash = tx.sighash_for_input(input_index, script_code, hash_type: hash_type)
26
22
  verify_sig(sig.bth, pubkey, sighash)
27
23
  end
28
24
 
data/lib/tapyrus/tx.rb CHANGED
@@ -103,34 +103,23 @@ module Tapyrus
103
103
  to_payload.bytesize
104
104
  end
105
105
 
106
- # get signature hash
106
+ # Generate signature hash
107
107
  # @param [Integer] input_index input index.
108
108
  # @param [Integer] hash_type signature hash type
109
- # @param [Tapyrus::Script] output_script script pubkey or script code. if script pubkey is P2WSH, set witness script to this.
110
- # @param [Integer] amount tapyrus amount locked in input. required for witness input only.
111
- # @param [Integer] skip_separator_index If output_script is P2WSH and output_script contains any OP_CODESEPARATOR,
112
- # the script code needs is the witnessScript but removing everything up to and including the last executed OP_CODESEPARATOR before the signature checking opcode being executed.
113
- def sighash_for_input(
114
- input_index,
115
- output_script,
116
- hash_type: SIGHASH_TYPE[:all],
117
- sig_version: :base,
118
- amount: nil,
119
- skip_separator_index: 0
120
- )
109
+ # @param [Tapyrus::Script] output_script script pubkey or script code. if script pubkey is P2SH, set redeem script to this.
110
+ # @return [String] sighash
111
+ def sighash_for_input(input_index, output_script, hash_type: SIGHASH_TYPE[:all])
121
112
  raise ArgumentError, 'input_index must be specified.' unless input_index
122
113
  raise ArgumentError, 'does not exist input corresponding to input_index.' if input_index >= inputs.size
123
114
  raise ArgumentError, 'script_pubkey must be specified.' unless output_script
124
- raise ArgumentError, 'unsupported sig version specified.' unless SIG_VERSION.include?(sig_version)
125
115
  sighash_for_legacy(input_index, output_script, hash_type)
126
116
  end
127
117
 
128
118
  # verify input signature.
129
119
  # @param [Integer] input_index
130
120
  # @param [Tapyrus::Script] script_pubkey the script pubkey for target input.
131
- # @param [Integer] amount the amount of tapyrus, require for witness program only.
132
121
  # @param [Array] flags the flags used when execute script interpreter.
133
- def verify_input_sig(input_index, script_pubkey, amount: nil, flags: STANDARD_SCRIPT_VERIFY_FLAGS)
122
+ def verify_input_sig(input_index, script_pubkey, flags: STANDARD_SCRIPT_VERIFY_FLAGS)
134
123
  flags << SCRIPT_VERIFY_P2SH if script_pubkey.p2sh?
135
124
  verify_input_sig_for_legacy(input_index, script_pubkey, flags)
136
125
  end
@@ -207,7 +196,9 @@ module Tapyrus
207
196
  checker = Tapyrus::TxChecker.new(tx: self, input_index: input_index)
208
197
  interpreter = Tapyrus::ScriptInterpreter.new(checker: checker, flags: flags)
209
198
 
210
- interpreter.verify_script(script_sig, script_pubkey)
199
+ result = interpreter.verify_script(script_sig, script_pubkey)
200
+ warn("Verify failed. Cause: #{interpreter.error}") unless result
201
+ result
211
202
  end
212
203
  end
213
204
  end
@@ -89,9 +89,11 @@ module Tapyrus
89
89
  script_pubkey = script_pubkey.add_color(color_id)
90
90
  end
91
91
 
92
+ output = Tapyrus::TxOut.new(script_pubkey: script_pubkey, value: value)
93
+ raise ArgumentError, 'The transaction amount is too small' if color_id.default? && output.dust?
92
94
  @outgoings[color_id] ||= 0
93
95
  @outgoings[color_id] += value
94
- @outputs << Tapyrus::TxOut.new(script_pubkey: script_pubkey, value: value)
96
+ @outputs << output
95
97
  self
96
98
  end
97
99
 
@@ -136,13 +138,16 @@ module Tapyrus
136
138
  def add_change(tx)
137
139
  @incomings.each do |color_id, in_amount|
138
140
  out_amount = @outgoings[color_id] || 0
139
- change, script_pubkey =
141
+ output =
140
142
  if color_id.default?
141
- [in_amount - out_amount - estimated_fee, @change_script_pubkey]
143
+ change = in_amount - out_amount - estimated_fee
144
+ change_output = Tapyrus::TxOut.new(script_pubkey: @change_script_pubkey, value: change)
145
+ change_output unless change_output.dust?
142
146
  else
143
- [in_amount - out_amount, @change_script_pubkey.add_color(color_id)]
147
+ change = in_amount - out_amount
148
+ Tapyrus::TxOut.new(script_pubkey: @change_script_pubkey.add_color(color_id), value: change) if change > 0
144
149
  end
145
- tx.outputs << Tapyrus::TxOut.new(script_pubkey: script_pubkey, value: change) if change > 0
150
+ tx.outputs << output if output
146
151
  end
147
152
  end
148
153
 
@@ -1,3 +1,3 @@
1
1
  module Tapyrus
2
- VERSION = '0.2.13'
2
+ VERSION = '0.3.2'
3
3
  end
data/lib/tapyrus.rb CHANGED
@@ -156,22 +156,6 @@ module Tapyrus
156
156
  end
157
157
  end
158
158
 
159
- class ::Object
160
- def build_json
161
- self.is_a?(Array) ? "[#{self.map { |o| o.to_h.to_json }.join(',')}]" : to_h.to_json
162
- end
163
-
164
- def to_h
165
- return self if self.is_a?(String)
166
- instance_variables.inject({}) do |result, var|
167
- key = var.to_s
168
- key.slice!(0) if key.start_with?('@')
169
- value = instance_variable_get(var)
170
- value.is_a?(Array) ? result.update(key => value.map { |v| v.to_h }) : result.update(key => value)
171
- end
172
- end
173
- end
174
-
175
159
  class ::Integer
176
160
  def to_even_length_hex
177
161
  hex = to_s(16)
data/tapyrusrb.gemspec CHANGED
@@ -31,12 +31,13 @@ Gem::Specification.new do |spec|
31
31
  spec.add_runtime_dependency 'siphash'
32
32
  spec.add_runtime_dependency 'activesupport', '>= 5.2.3'
33
33
  spec.add_runtime_dependency 'json_pure', '>= 2.3.1'
34
+ spec.add_runtime_dependency 'terminal-table', '~> 3.0.2'
34
35
 
35
36
  # for options
36
37
  spec.add_development_dependency 'leveldb-native'
37
38
 
38
39
  spec.add_development_dependency 'bundler'
39
- spec.add_development_dependency 'prettier'
40
+ spec.add_development_dependency 'prettier', '2.0.0'
40
41
  spec.add_development_dependency 'rake', '>= 12.3.3'
41
42
  spec.add_development_dependency 'rspec', '~> 3.0'
42
43
  spec.add_development_dependency 'timecop'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tapyrus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.13
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - azuchi
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-03 00:00:00.000000000 Z
11
+ date: 2022-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ecdsa
@@ -178,6 +178,20 @@ dependencies:
178
178
  - - ">="
179
179
  - !ruby/object:Gem::Version
180
180
  version: 2.3.1
181
+ - !ruby/object:Gem::Dependency
182
+ name: terminal-table
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: 3.0.2
188
+ type: :runtime
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: 3.0.2
181
195
  - !ruby/object:Gem::Dependency
182
196
  name: leveldb-native
183
197
  requirement: !ruby/object:Gem::Requirement
@@ -210,16 +224,16 @@ dependencies:
210
224
  name: prettier
211
225
  requirement: !ruby/object:Gem::Requirement
212
226
  requirements:
213
- - - ">="
227
+ - - '='
214
228
  - !ruby/object:Gem::Version
215
- version: '0'
229
+ version: 2.0.0
216
230
  type: :development
217
231
  prerelease: false
218
232
  version_requirements: !ruby/object:Gem::Requirement
219
233
  requirements:
220
- - - ">="
234
+ - - '='
221
235
  - !ruby/object:Gem::Version
222
- version: '0'
236
+ version: 2.0.0
223
237
  - !ruby/object:Gem::Dependency
224
238
  name: rake
225
239
  requirement: !ruby/object:Gem::Requirement
@@ -280,6 +294,7 @@ description: The implementation of Tapyrus Protocol for Ruby.
280
294
  email:
281
295
  - azuchi@chaintope.com
282
296
  executables:
297
+ - tapyrus-script-debugger
283
298
  - tapyrusrb-cli
284
299
  - tapyrusrbd
285
300
  extensions: []
@@ -299,6 +314,7 @@ files:
299
314
  - Rakefile
300
315
  - bin/console
301
316
  - bin/setup
317
+ - exe/tapyrus-script-debugger
302
318
  - exe/tapyrusrb-cli
303
319
  - exe/tapyrusrbd
304
320
  - lib/openassets.rb
@@ -425,7 +441,7 @@ homepage: https://github.com/chaintope/tapyrusrb
425
441
  licenses:
426
442
  - MIT
427
443
  metadata: {}
428
- post_install_message:
444
+ post_install_message:
429
445
  rdoc_options: []
430
446
  require_paths:
431
447
  - lib
@@ -440,8 +456,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
440
456
  - !ruby/object:Gem::Version
441
457
  version: '0'
442
458
  requirements: []
443
- rubygems_version: 3.2.22
444
- signing_key:
459
+ rubygems_version: 3.3.21
460
+ signing_key:
445
461
  specification_version: 4
446
462
  summary: The implementation of Tapyrus Protocol for Ruby.
447
463
  test_files: []