bitcoinrb 0.3.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/.ruby-version +1 -1
  3. data/.travis.yml +5 -3
  4. data/README.md +17 -6
  5. data/bitcoinrb.gemspec +7 -7
  6. data/exe/bitcoinrbd +5 -0
  7. data/lib/bitcoin.rb +34 -11
  8. data/lib/bitcoin/bip85_entropy.rb +111 -0
  9. data/lib/bitcoin/block_filter.rb +14 -0
  10. data/lib/bitcoin/block_header.rb +2 -0
  11. data/lib/bitcoin/chain_params.rb +9 -8
  12. data/lib/bitcoin/chainparams/regtest.yml +1 -1
  13. data/lib/bitcoin/chainparams/signet.yml +39 -0
  14. data/lib/bitcoin/chainparams/testnet.yml +1 -1
  15. data/lib/bitcoin/constants.rb +45 -12
  16. data/lib/bitcoin/descriptor.rb +1 -1
  17. data/lib/bitcoin/errors.rb +19 -0
  18. data/lib/bitcoin/ext.rb +5 -0
  19. data/lib/bitcoin/ext/ecdsa.rb +31 -0
  20. data/lib/bitcoin/ext/json_parser.rb +46 -0
  21. data/lib/bitcoin/ext_key.rb +50 -19
  22. data/lib/bitcoin/key.rb +46 -29
  23. data/lib/bitcoin/key_path.rb +12 -5
  24. data/lib/bitcoin/message.rb +7 -0
  25. data/lib/bitcoin/message/base.rb +1 -0
  26. data/lib/bitcoin/message/cf_parser.rb +16 -0
  27. data/lib/bitcoin/message/cfcheckpt.rb +36 -0
  28. data/lib/bitcoin/message/cfheaders.rb +40 -0
  29. data/lib/bitcoin/message/cfilter.rb +35 -0
  30. data/lib/bitcoin/message/fee_filter.rb +1 -1
  31. data/lib/bitcoin/message/filter_load.rb +3 -3
  32. data/lib/bitcoin/message/get_cfcheckpt.rb +29 -0
  33. data/lib/bitcoin/message/get_cfheaders.rb +24 -0
  34. data/lib/bitcoin/message/get_cfilters.rb +25 -0
  35. data/lib/bitcoin/message/header_and_short_ids.rb +1 -1
  36. data/lib/bitcoin/message/inventory.rb +1 -1
  37. data/lib/bitcoin/message/merkle_block.rb +1 -1
  38. data/lib/bitcoin/message/network_addr.rb +3 -3
  39. data/lib/bitcoin/message/ping.rb +1 -1
  40. data/lib/bitcoin/message/pong.rb +1 -1
  41. data/lib/bitcoin/message/send_cmpct.rb +2 -2
  42. data/lib/bitcoin/message/version.rb +7 -0
  43. data/lib/bitcoin/mnemonic.rb +7 -7
  44. data/lib/bitcoin/network/peer.rb +9 -4
  45. data/lib/bitcoin/network/peer_discovery.rb +1 -1
  46. data/lib/bitcoin/node/cli.rb +14 -10
  47. data/lib/bitcoin/node/configuration.rb +3 -1
  48. data/lib/bitcoin/node/spv.rb +9 -1
  49. data/lib/bitcoin/opcodes.rb +14 -1
  50. data/lib/bitcoin/out_point.rb +7 -0
  51. data/lib/bitcoin/payment_code.rb +92 -0
  52. data/lib/bitcoin/psbt/hd_key_path.rb +1 -1
  53. data/lib/bitcoin/psbt/input.rb +8 -17
  54. data/lib/bitcoin/psbt/output.rb +1 -1
  55. data/lib/bitcoin/psbt/tx.rb +11 -16
  56. data/lib/bitcoin/rpc/bitcoin_core_client.rb +22 -12
  57. data/lib/bitcoin/rpc/request_handler.rb +2 -2
  58. data/lib/bitcoin/script/script.rb +68 -28
  59. data/lib/bitcoin/script/script_error.rb +27 -1
  60. data/lib/bitcoin/script/script_interpreter.rb +164 -67
  61. data/lib/bitcoin/script/tx_checker.rb +64 -14
  62. data/lib/bitcoin/secp256k1.rb +1 -0
  63. data/lib/bitcoin/secp256k1/native.rb +138 -25
  64. data/lib/bitcoin/secp256k1/rfc6979.rb +43 -0
  65. data/lib/bitcoin/secp256k1/ruby.rb +82 -54
  66. data/lib/bitcoin/sighash_generator.rb +156 -0
  67. data/lib/bitcoin/slip39/sss.rb +5 -2
  68. data/lib/bitcoin/store.rb +2 -1
  69. data/lib/bitcoin/store/chain_entry.rb +1 -0
  70. data/lib/bitcoin/store/db/level_db.rb +2 -2
  71. data/lib/bitcoin/store/utxo_db.rb +226 -0
  72. data/lib/bitcoin/tx.rb +17 -88
  73. data/lib/bitcoin/tx_in.rb +4 -5
  74. data/lib/bitcoin/tx_out.rb +2 -3
  75. data/lib/bitcoin/util.rb +43 -6
  76. data/lib/bitcoin/version.rb +1 -1
  77. data/lib/bitcoin/wallet.rb +1 -0
  78. data/lib/bitcoin/wallet/account.rb +2 -1
  79. data/lib/bitcoin/wallet/base.rb +3 -3
  80. data/lib/bitcoin/wallet/db.rb +1 -1
  81. data/lib/bitcoin/wallet/master_key.rb +1 -0
  82. data/lib/bitcoin/wallet/utxo.rb +37 -0
  83. metadata +50 -32
@@ -4,14 +4,21 @@ module Bitcoin
4
4
  # key path convert an array of derive number
5
5
  # @param [String] path_string
6
6
  # @return [Array[Integer]] key path numbers.
7
+ # @raise [ArgumentError] if invalid +path_string+.
7
8
  def parse_key_path(path_string)
8
- path_string.split('/').map.with_index do|p, index|
9
+ paths = path_string.split('/')
10
+ raise ArgumentError, "Invalid path." if path_string.include?(" ")
11
+ raise ArgumentError, "Invalid path." unless path_string.count("/") <= paths.size
12
+ paths.map.with_index do|p, index|
9
13
  if index == 0
10
- raise ArgumentError.new("#{path_string} is invalid format.") unless p == 'm'
11
- next
14
+ next if p == 'm'
15
+ raise ArgumentError, "Invalid path." unless p == 'm'
12
16
  end
13
- raise ArgumentError.new("#{path_string} is invalid format.") unless p.delete("'") =~ /^[0-9]+$/
14
- (p[-1] == "'" ? p.delete("'").to_i + Bitcoin::HARDENED_THRESHOLD : p.to_i)
17
+ raise ArgumentError, "Invalid path." if p.count("'") > 1 || (p.count("'") == 1 && p[-1] != "'")
18
+ raise ArgumentError, "Invalid path." unless p.delete("'") =~ /^[0-9]+$/
19
+ value = (p[-1] == "'" ? p.delete("'").to_i + Bitcoin::HARDENED_THRESHOLD : p.to_i)
20
+ raise ArgumentError, "Invalid path." if value > 4294967295 # 4294967295 = 0xFFFFFFFF (uint32 max)
21
+ value
15
22
  end[1..-1]
16
23
  end
17
24
 
@@ -37,6 +37,13 @@ module Bitcoin
37
37
  autoload :BlockTransactionRequest, 'bitcoin/message/block_transaction_request'
38
38
  autoload :BlockTxn, 'bitcoin/message/block_txn'
39
39
  autoload :BlockTransactions, 'bitcoin/message/block_transactions'
40
+ autoload :GetCFilters, 'bitcoin/message/get_cfilters'
41
+ autoload :GetCFHeaders, 'bitcoin/message/get_cfheaders'
42
+ autoload :CFParser, 'bitcoin/message/cf_parser'
43
+ autoload :GetCFCheckpt, 'bitcoin/message/get_cfcheckpt'
44
+ autoload :CFCheckpt, 'bitcoin/message/cfcheckpt'
45
+ autoload :CFilter, 'bitcoin/message/cfilter'
46
+ autoload :CFHeaders, 'bitcoin/message/cfheaders'
40
47
 
41
48
  USER_AGENT = "/bitcoinrb:#{Bitcoin::VERSION}/"
42
49
 
@@ -3,6 +3,7 @@ module Bitcoin
3
3
 
4
4
  # Base message class
5
5
  class Base
6
+ include Bitcoin::HexConverter
6
7
  include Bitcoin::Util
7
8
  extend Bitcoin::Util
8
9
 
@@ -0,0 +1,16 @@
1
+ module Bitcoin
2
+ module Message
3
+ module CFParser
4
+
5
+ def parse_from_payload(payload)
6
+ type, start, hash = payload.unpack('CLH*')
7
+ self.new(type, start, hash)
8
+ end
9
+
10
+ def to_payload
11
+ [filter_type, start_height, stop_hash].pack('CLH*')
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,36 @@
1
+ module Bitcoin
2
+ module Message
3
+
4
+ # cfcheckpt message for BIP-157
5
+ # https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki#cfcheckpt
6
+ class CFCheckpt < Base
7
+
8
+ COMMAND = 'cfcheckpt'
9
+
10
+ attr_accessor :filter_type
11
+ attr_accessor :stop_hash # little endian
12
+ attr_accessor :filter_headers # little endian
13
+
14
+ def initialize(filter_type, stop_hash, filter_headers)
15
+ @filter_type = filter_type
16
+ @stop_hash = stop_hash
17
+ @filter_headers = filter_headers
18
+ end
19
+
20
+ def self.parse_from_payload(payload)
21
+ buf = StringIO.new(payload)
22
+ type = buf.read(1).unpack1('C')
23
+ hash = buf.read(32).unpack1('H*')
24
+ count = Bitcoin.unpack_var_int_from_io(buf)
25
+ headers = count.times.map{buf.read(32).bth}
26
+ self.new(type, hash, headers)
27
+ end
28
+
29
+ def to_payload
30
+ [filter_type, stop_hash].pack('CH*') +
31
+ Bitcoin.pack_var_int(filter_headers.size) + filter_headers.map(&:htb).join
32
+ end
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,40 @@
1
+ module Bitcoin
2
+ module Message
3
+
4
+ # cfheaders message for BIP-157
5
+ # https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki#cfheaders
6
+ class CFHeaders < Base
7
+
8
+ COMMAND = 'cfheaders'
9
+
10
+ attr_accessor :filter_type
11
+ attr_accessor :stop_hash # little endian
12
+ attr_accessor :prev_filter_header # little endian
13
+ attr_accessor :filter_hashes # little endian
14
+
15
+ def initialize(filter_type, stop_hash, prev_filter_header, filter_hashes)
16
+ @filter_type = filter_type
17
+ @stop_hash = stop_hash
18
+ @prev_filter_header = prev_filter_header
19
+ @filter_hashes = filter_hashes
20
+ end
21
+
22
+ def self.parse_from_payload(payload)
23
+ buf = StringIO.new(payload)
24
+ type = buf.read(1).unpack1("C")
25
+ hash = buf.read(32).bth
26
+ header = buf.read(32).bth
27
+ count = Bitcoin.unpack_var_int_from_io(buf)
28
+ hashes = count.times.map{buf.read(32).bth}
29
+ self.new(type, hash, header, hashes)
30
+ end
31
+
32
+ def to_payload
33
+ [filter_type].pack('C') + stop_hash.htb + prev_filter_header.htb +
34
+ Bitcoin.pack_var_int(filter_hashes.size) + filter_hashes.map(&:htb).join
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,35 @@
1
+ module Bitcoin
2
+ module Message
3
+
4
+ # cfilter message for BIP-157
5
+ # https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki#cfilter
6
+ class CFilter < Base
7
+
8
+ COMMAND = 'cfilter'
9
+
10
+ attr_accessor :filter_type
11
+ attr_accessor :block_hash # little endian
12
+ attr_accessor :filter # little endian
13
+
14
+ def initialize(filter_type, block_hash, filter)
15
+ @filter_type = filter_type
16
+ @block_hash = block_hash
17
+ @filter = filter
18
+ end
19
+
20
+ def self.parse_from_payload(payload)
21
+ buf = StringIO.new(payload)
22
+ type = buf.read(1).unpack1("C")
23
+ hash = buf.read(32).bth
24
+ len = Bitcoin.unpack_var_int_from_io(buf)
25
+ filter = buf.read(len).bth
26
+ self.new(type, hash, filter)
27
+ end
28
+
29
+ def to_payload
30
+ [filter_type, block_hash].pack('CH*') + Bitcoin.pack_var_string(filter.htb)
31
+ end
32
+ end
33
+
34
+ end
35
+ end
@@ -15,7 +15,7 @@ module Bitcoin
15
15
  end
16
16
 
17
17
  def self.parse_from_payload(payload)
18
- new(payload.unpack('Q').first)
18
+ new(payload.unpack1('Q'))
19
19
  end
20
20
 
21
21
  def to_payload
@@ -23,9 +23,9 @@ module Bitcoin
23
23
  buf = StringIO.new(payload)
24
24
  filter_count = Bitcoin.unpack_var_int_from_io(buf)
25
25
  filter = buf.read(filter_count).unpack('C*')
26
- func_count = buf.read(4).unpack('V').first
27
- tweak = buf.read(4).unpack('V').first
28
- flag = buf.read(1).unpack('C').first
26
+ func_count = buf.read(4).unpack1('V')
27
+ tweak = buf.read(4).unpack1('V')
28
+ flag = buf.read(1).unpack1('C')
29
29
  FilterLoad.new(Bitcoin::BloomFilter.new(filter, func_count, tweak), flag)
30
30
  end
31
31
 
@@ -0,0 +1,29 @@
1
+ module Bitcoin
2
+ module Message
3
+
4
+ # getcfcheckpt message for BIP-157
5
+ # https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki#getcfcheckpt
6
+ class GetCFCheckpt < Base
7
+
8
+ COMMAND = 'getcfcheckpt'
9
+
10
+ attr_accessor :filter_type
11
+ attr_accessor :stop_hash # little endian
12
+
13
+ def initialize(filter_type, stop_hash)
14
+ @filter_type = filter_type
15
+ @stop_hash = stop_hash
16
+ end
17
+
18
+ def self.parse_from_payload(payload)
19
+ type, hash = payload.unpack('CH*')
20
+ self.new(type, hash)
21
+ end
22
+
23
+ def to_payload
24
+ [filter_type, stop_hash].pack('CH*')
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,24 @@
1
+ module Bitcoin
2
+ module Message
3
+
4
+ # getcfheaders message for BIP-157
5
+ # https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki#getcfheaders
6
+ class GetCFHeaders < Base
7
+ include CFParser
8
+ extend CFParser
9
+
10
+ COMMAND = 'getcfheaders'
11
+
12
+ attr_accessor :filter_type
13
+ attr_accessor :start_height
14
+ attr_accessor :stop_hash # little endian
15
+
16
+ def initialize(filter_type, start_height, stop_hash)
17
+ @filter_type = filter_type
18
+ @start_height = start_height
19
+ @stop_hash = stop_hash
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ module Bitcoin
2
+ module Message
3
+
4
+ # getcfilters message for BIP-157
5
+ # https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki#getcfilters
6
+ class GetCFilters < Base
7
+ include CFParser
8
+ extend CFParser
9
+
10
+ COMMAND = 'getcfilters'
11
+
12
+ attr_accessor :filter_type
13
+ attr_accessor :start_height
14
+ attr_accessor :stop_hash # little endian
15
+
16
+ def initialize(filter_type, start_height, stop_hash)
17
+ @filter_type = filter_type
18
+ @start_height = start_height
19
+ @stop_hash = stop_hash
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
@@ -22,7 +22,7 @@ module Bitcoin
22
22
  def self.parse_from_payload(payload)
23
23
  buf = StringIO.new(payload)
24
24
  header = Bitcoin::BlockHeader.parse_from_payload(buf.read(80))
25
- nonce = buf.read(8).unpack('q*').first
25
+ nonce = buf.read(8).unpack1('q*')
26
26
  short_ids_len = Bitcoin.unpack_var_int_from_io(buf)
27
27
  short_ids = short_ids_len.times.map do
28
28
  buf.read(6).reverse.bth.to_i(16)
@@ -26,7 +26,7 @@ module Bitcoin
26
26
  # parse inventory payload
27
27
  def self.parse_from_payload(payload)
28
28
  raise Error, 'invalid inventory size.' if payload.bytesize != 36
29
- identifier = payload[0..4].unpack('V').first
29
+ identifier = payload[0..4].unpack1('V')
30
30
  hash = payload[4..-1].bth # internal byte order
31
31
  new(identifier, hash)
32
32
  end
@@ -20,7 +20,7 @@ module Bitcoin
20
20
  m = new
21
21
  buf = StringIO.new(payload)
22
22
  m.header = Bitcoin::BlockHeader.parse_from_payload(buf.read(80))
23
- m.tx_count = buf.read(4).unpack('V').first
23
+ m.tx_count = buf.read(4).unpack1('V')
24
24
  hash_count = Bitcoin.unpack_var_int_from_io(buf)
25
25
  hash_count.times do
26
26
  m.hashes << buf.read(32).bth
@@ -31,10 +31,10 @@ module Bitcoin
31
31
  buf = payload.is_a?(String) ? StringIO.new(payload) : payload
32
32
  has_time = buf.size > 26
33
33
  addr = new(time: nil)
34
- addr.time = buf.read(4).unpack('V').first if has_time
35
- addr.services = buf.read(8).unpack('Q').first
34
+ addr.time = buf.read(4).unpack1('V') if has_time
35
+ addr.services = buf.read(8).unpack1('Q')
36
36
  addr.ip_addr = IPAddr::new_ntoh(buf.read(16))
37
- addr.port = buf.read(2).unpack('n').first
37
+ addr.port = buf.read(2).unpack1('n')
38
38
  addr
39
39
  end
40
40
 
@@ -14,7 +14,7 @@ module Bitcoin
14
14
  end
15
15
 
16
16
  def self.parse_from_payload(payload)
17
- new(payload.unpack('Q').first)
17
+ new(payload.unpack1('Q'))
18
18
  end
19
19
 
20
20
  def to_payload
@@ -14,7 +14,7 @@ module Bitcoin
14
14
  end
15
15
 
16
16
  def self.parse_from_payload(payload)
17
- new(payload.unpack('Q').first)
17
+ new(payload.unpack1('Q'))
18
18
  end
19
19
 
20
20
  def to_payload
@@ -21,8 +21,8 @@ module Bitcoin
21
21
 
22
22
  def self.parse_from_payload(payload)
23
23
  buf = StringIO.new(payload)
24
- mode = buf.read(1).unpack('c').first
25
- version = buf.read(8).unpack('Q').first
24
+ mode = buf.read(1).unpack1('c')
25
+ version = buf.read(8).unpack1('Q')
26
26
  new(mode, version)
27
27
  end
28
28
 
@@ -64,6 +64,13 @@ module Bitcoin
64
64
  ( version >= 70001 && payload ) ? unpack_boolean(payload) : [ true, nil ]
65
65
  end
66
66
 
67
+ # Check whether +service_flag+ support this version.
68
+ # @param [Integer] service_flag the service flags.
69
+ # @return [Boolean] whether support +service_flag+
70
+ def support?(service_flag)
71
+ (services & service_flag) != 0
72
+ end
73
+
67
74
  end
68
75
  end
69
76
  end
@@ -6,11 +6,11 @@ module Bitcoin
6
6
 
7
7
  WORD_DIR = "#{__dir__}/mnemonic/wordlist"
8
8
 
9
- attr_reader :word_list
9
+ attr_reader :language
10
10
 
11
- def initialize(word_list)
12
- raise ArgumentError, 'specified language is not supported.' unless Mnemonic.word_lists.include?(word_list)
13
- @word_list = word_list
11
+ def initialize(language)
12
+ raise ArgumentError, 'specified language is not supported.' unless Mnemonic.word_lists.include?(language)
13
+ @language = language
14
14
  end
15
15
 
16
16
  # get support language list
@@ -39,7 +39,7 @@ module Bitcoin
39
39
  # @return [Array] the array of mnemonic word.
40
40
  def to_mnemonic(entropy)
41
41
  raise ArgumentError, 'entropy is empty.' if entropy.nil? || entropy.empty?
42
- e = entropy.htb.unpack('B*').first
42
+ e = entropy.htb.unpack1('B*')
43
43
  seed = e + checksum(e)
44
44
  mnemonic_index = seed.chars.each_slice(11).map{|i|i.join.to_i(2)}
45
45
  word_master = load_words
@@ -61,7 +61,7 @@ module Bitcoin
61
61
  # @param [String] entropy an entropy with bit string format
62
62
  # @return [String] an entropy checksum with bit string format
63
63
  def checksum(entropy)
64
- b = Bitcoin.sha256([entropy].pack('B*')).unpack('B*').first
64
+ b = Bitcoin.sha256([entropy].pack('B*')).unpack1('B*')
65
65
  b.slice(0, (entropy.length/32))
66
66
  end
67
67
 
@@ -69,7 +69,7 @@ module Bitcoin
69
69
 
70
70
  # load word list contents
71
71
  def load_words
72
- File.readlines("#{WORD_DIR}/#{word_list}.txt").map(&:strip)
72
+ File.readlines("#{WORD_DIR}/#{language}.txt").map(&:strip)
73
73
  end
74
74
 
75
75
  end
@@ -83,10 +83,15 @@ module Bitcoin
83
83
 
84
84
  def post_handshake
85
85
  @connected = true
86
- pool.handle_new_peer(self)
87
- # require remote peer to use headers message instead fo inv message.
88
- conn.send_message(Bitcoin::Message::SendHeaders.new)
89
- EM.add_periodic_timer(PING_INTERVAL) {send_ping}
86
+ if remote_version.support?(Bitcoin::Message::SERVICE_FLAGS[:bloom])
87
+ pool.handle_new_peer(self)
88
+ # require remote peer to use headers message instead fo inv message.
89
+ conn.send_message(Bitcoin::Message::SendHeaders.new)
90
+ EM.add_periodic_timer(PING_INTERVAL) {send_ping}
91
+ else
92
+ close("peer does not support NODE_BLOOM.")
93
+ pool.pending_peers.delete(self)
94
+ end
90
95
  end
91
96
 
92
97
  # start block header download
@@ -30,7 +30,7 @@ module Bitcoin
30
30
  logger.debug 'discover peer address from DNS seeds.'
31
31
  dns_seeds.map { |seed|
32
32
  begin
33
- Socket.getaddrinfo(seed, Bitcoin.chain_params.default_port).map{|a|a[2]}.uniq
33
+ Socket.getaddrinfo("#{seed}", Bitcoin.chain_params.default_port).map{|a|a[2]}.uniq
34
34
  rescue SocketError => e
35
35
  logger.error "SocketError occurred when load DNS seed: #{seed}, error: #{e.message}"
36
36
  nil