bitcoin-ruby 0.0.12 → 0.0.13

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e6e9a50f3285c39f0a718769237121861b3e03a5
4
- data.tar.gz: d7a08090502517977afd036fd825267673ceda18
3
+ metadata.gz: 0d147f8bf63febaaeb3a38004c78f7b143284de5
4
+ data.tar.gz: f3fc9dcebed29a46a5ad850e19eb21468c9c6915
5
5
  SHA512:
6
- metadata.gz: bf14f375a775ec99c019689dd6c75ec87e676d02001d12d6131e782e412e53204afc9e6d4a4b28ffb2e9ecb015db6d8aa0381e82e952ab1e718ea1f080815a08
7
- data.tar.gz: 35b1ff06e6f2c212157d75fe07fe5ab7a0805df6d9dde15b2b3e5359cfe662c3fbd9a047fc15547942d8957b12e50ce0d8e0fde000f141d5b2886169bd037aaf
6
+ metadata.gz: '0896c7cac52a99f68a6b6aee884d87a10516a8d419182b00f44b7e95fc2b1a9676e600d5c8bd1c373af19e17c1c72ba110c9449d4b8a8ef5bcce7918fb2cb12b'
7
+ data.tar.gz: d33dca0010b3dab6181602b8b27f4099b0814659882a64e3e298b22a9b1a71c3ac82402df5119e33dcee63224f421b1a06104ed179544c39b50ba1e43574f8c5
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bitcoin-ruby (0.0.12)
4
+ bitcoin-ruby (0.0.13)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -81,23 +81,18 @@ module Bitcoin
81
81
 
82
82
  return false if buf.eof?
83
83
 
84
- in_size = Protocol.unpack_var_int_from_io(buf)
85
-
86
84
  # segwit serialization format is defined by https://github.com/bitcoin/bips/blob/master/bip-0144.mediawiki
87
- witness = false
88
- if in_size.zero?
89
- @marker = 0
90
- @flag = buf.read(1).unpack('c').first
91
-
92
- # marker must be zero and flag must be non-zero to be valid segwit
93
- if @marker == 0 && @flag != 0
94
- in_size = Protocol.unpack_var_int_from_io(buf)
95
- witness = true
96
- else
97
- # Undo the last read in case this isn't a segwit transaction
98
- buf.seek(buf.pos - 1)
99
- end
100
- end
85
+ # Also note that it is impossible to parse 0 input transactions. Regular transactions with 0 inputs look
86
+ # like malformed segwit transactions.
87
+ @marker = buf.read(1).unpack('c').first
88
+ @flag = buf.read(1).unpack('c').first
89
+
90
+ witness = @marker == 0 && @flag != 0
91
+
92
+ # Non-segwit format does not contain marker or flag fields.
93
+ buf.seek(buf.pos - 2) unless witness
94
+
95
+ in_size = Protocol.unpack_var_int_from_io(buf)
101
96
 
102
97
  @in = []
103
98
  in_size.times{
@@ -271,12 +266,12 @@ module Bitcoin
271
266
 
272
267
  # verify input signature +in_idx+ against the corresponding
273
268
  # output in +outpoint_tx+
274
- # outpoint
269
+ # outpoint. This arg can also be a Script or TxOut.
275
270
  #
276
271
  # options are: verify_sigpushonly, verify_minimaldata, verify_cleanstack, verify_dersig, verify_low_s, verify_strictenc, fork_id
277
- def verify_input_signature(in_idx, outpoint_tx_or_script, block_timestamp=Time.now.to_i, opts={})
272
+ def verify_input_signature(in_idx, outpoint_data, block_timestamp=Time.now.to_i, opts={})
278
273
  if @enable_bitcoinconsensus
279
- return bitcoinconsensus_verify_script(in_idx, outpoint_tx_or_script, block_timestamp, opts)
274
+ return bitcoinconsensus_verify_script(in_idx, outpoint_data, block_timestamp, opts)
280
275
  end
281
276
 
282
277
  # If FORKID is enabled, we also ensure strict encoding.
@@ -285,21 +280,12 @@ module Bitcoin
285
280
  outpoint_idx = @in[in_idx].prev_out_index
286
281
  script_sig = @in[in_idx].script_sig
287
282
 
288
- amount = nil
289
- script_pubkey = nil
290
- if outpoint_tx_or_script.respond_to?(:out)
291
- # If given an entire previous transaction, take the script from it
292
- prevout = outpoint_tx_or_script.out[outpoint_idx]
293
- amount = prevout.value
294
- script_pubkey = prevout.pk_script
295
- else
296
- if opts[:fork_id]
297
- raise "verify_input_signature must be called with a previous transaction if " \
298
- "SIGHASH_FORKID is enabled"
299
- end
283
+ amount = amount_from_outpoint_data(outpoint_data, outpoint_idx)
284
+ script_pubkey = script_pubkey_from_outpoint_data(outpoint_data, outpoint_idx)
300
285
 
301
- # Otherwise, it's already a script.
302
- script_pubkey = outpoint_tx_or_script
286
+ if opts[:fork_id] && amount.nil?
287
+ raise "verify_input_signature must be called with a previous transaction or " \
288
+ "transaction output if SIGHASH_FORKID is enabled"
303
289
  end
304
290
 
305
291
  @scripts[in_idx] = Bitcoin::Script.new(script_sig, script_pubkey)
@@ -317,24 +303,19 @@ module Bitcoin
317
303
 
318
304
  # verify witness input signature +in_idx+ against the corresponding
319
305
  # output in +outpoint_tx+
320
- # outpoint
306
+ # outpoint. This arg can also be a Script or TxOut
321
307
  #
322
308
  # options are: verify_sigpushonly, verify_minimaldata, verify_cleanstack, verify_dersig, verify_low_s, verify_strictenc
323
- def verify_witness_input_signature(in_idx, outpoint_tx_or_script, prev_out_amount, block_timestamp=Time.now.to_i, opts={})
309
+ def verify_witness_input_signature(in_idx, outpoint_data, prev_out_amount, block_timestamp=Time.now.to_i, opts={})
324
310
  if @enable_bitcoinconsensus
325
- return bitcoinconsensus_verify_script(in_idx, outpoint_tx_or_script, block_timestamp, opts)
311
+ return bitcoinconsensus_verify_script(in_idx, outpoint_data, block_timestamp, opts)
326
312
  end
327
313
 
328
314
  outpoint_idx = @in[in_idx].prev_out_index
329
315
  script_sig = ''
330
316
 
331
- # If given an entire previous transaction, take the script from it
332
- script_pubkey = if outpoint_tx_or_script.respond_to?(:out)
333
- Bitcoin::Script.new(outpoint_tx_or_script.out[outpoint_idx].pk_script)
334
- else
335
- # Otherwise, it's already a script.
336
- Bitcoin::Script.new(outpoint_tx_or_script)
337
- end
317
+ script_pubkey = script_pubkey_from_outpoint_data(outpoint_data, outpoint_idx)
318
+ script_pubkey = Bitcoin::Script.new(script_pubkey)
338
319
 
339
320
  if script_pubkey.is_p2sh?
340
321
  redeem_script = Bitcoin::Script.new(@in[in_idx].script_sig).get_pubkey
@@ -372,17 +353,11 @@ module Bitcoin
372
353
  return sig_valid
373
354
  end
374
355
 
375
- def bitcoinconsensus_verify_script(in_idx, outpoint_tx_or_script, block_timestamp=Time.now.to_i, opts={})
356
+ def bitcoinconsensus_verify_script(in_idx, outpoint_data, block_timestamp=Time.now.to_i, opts={})
376
357
  raise "Bitcoin::BitcoinConsensus shared library not found" unless Bitcoin::BitcoinConsensus.lib_available?
377
358
 
378
- # If given an entire previous transaction, take the script from it
379
- script_pubkey = if outpoint_tx_or_script.respond_to?(:out)
380
- outpoint_idx = @in[in_idx].prev_out_index
381
- outpoint_tx_or_script.out[outpoint_idx].pk_script
382
- else
383
- # Otherwise, it's already a script.
384
- outpoint_tx_or_script
385
- end
359
+ outpoint_idx = @in[in_idx].prev_out_index
360
+ script_pubkey = script_pubkey_from_outpoint_data(outpoint_data, outpoint_idx)
386
361
 
387
362
  flags = Bitcoin::BitcoinConsensus::SCRIPT_VERIFY_NONE
388
363
  flags |= Bitcoin::BitcoinConsensus::SCRIPT_VERIFY_P2SH if block_timestamp >= 1333238400
@@ -583,6 +558,30 @@ module Bitcoin
583
558
 
584
559
  Digest::SHA256.digest( Digest::SHA256.digest( buf ) )
585
560
  end
561
+
562
+ def script_pubkey_from_outpoint_data(outpoint_data, outpoint_idx)
563
+ if outpoint_data.respond_to?(:out)
564
+ # If given an entire previous transaction, take the script from it
565
+ outpoint_data.out[outpoint_idx].pk_script
566
+ elsif outpoint_data.respond_to?(:pk_script)
567
+ # If given an transaction output, take the script
568
+ outpoint_data.pk_script
569
+ else
570
+ # Otherwise, we assume it's already a script.
571
+ outpoint_data
572
+ end
573
+ end
574
+
575
+ def amount_from_outpoint_data(outpoint_data, outpoint_idx)
576
+ if outpoint_data.respond_to?(:out)
577
+ # If given an entire previous transaction, take the amount from the
578
+ # output at the outpoint_idx
579
+ outpoint_data.out[outpoint_idx].amount
580
+ elsif outpoint_data.respond_to?(:pk_script)
581
+ # If given an transaction output, take the amount
582
+ outpoint_data.amount
583
+ end
584
+ end
586
585
  end
587
586
  end
588
587
  end
@@ -1435,8 +1435,10 @@ class Bitcoin::Script
1435
1435
  def sighash_subscript(drop_sigs, opts = {})
1436
1436
  if opts[:fork_id]
1437
1437
  drop_sigs.reject! do |signature|
1438
- _, hash_type = parse_sig(signature)
1439
- (hash_type&SIGHASH_TYPE[:forkid]) != 0
1438
+ if signature && signature.size > 0
1439
+ _, hash_type = parse_sig(signature)
1440
+ (hash_type&SIGHASH_TYPE[:forkid]) != 0
1441
+ end
1440
1442
  end
1441
1443
  end
1442
1444
 
@@ -1,3 +1,3 @@
1
1
  module Bitcoin
2
- VERSION = "0.0.12"
2
+ VERSION = "0.0.13"
3
3
  end
@@ -316,6 +316,9 @@ describe 'Tx' do
316
316
 
317
317
  tx.verify_input_signature(0, outpoint_tx).should == true
318
318
 
319
+ # Only one test where we provide the TxOut is needed since when providing
320
+ # the full outpoint_tx the verification logic doesn't change.
321
+ tx.verify_input_signature(0, outpoint_tx.out[0]).should == true
319
322
 
320
323
  tx = Tx.from_json( fixtures_file('rawtx-c99c49da4c38af669dea436d3e73780dfdb6c1ecf9958baa52960e8baee30e73.json') )
321
324
  tx.hash.should == 'c99c49da4c38af669dea436d3e73780dfdb6c1ecf9958baa52960e8baee30e73'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitcoin-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - lian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-24 00:00:00.000000000 Z
11
+ date: 2017-11-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This is a ruby library for interacting with the bitcoin protocol/network
14
14
  email: