tapyrus 0.2.4 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +37 -0
  3. data/.prettierignore +3 -0
  4. data/.prettierrc.yaml +3 -0
  5. data/CODE_OF_CONDUCT.md +7 -7
  6. data/README.md +14 -17
  7. data/Rakefile +3 -3
  8. data/lib/openassets.rb +0 -2
  9. data/lib/openassets/marker_output.rb +0 -4
  10. data/lib/openassets/payload.rb +4 -10
  11. data/lib/schnorr.rb +14 -9
  12. data/lib/schnorr/sign_to_contract.rb +51 -0
  13. data/lib/schnorr/signature.rb +3 -6
  14. data/lib/tapyrus.rb +8 -30
  15. data/lib/tapyrus/base58.rb +7 -6
  16. data/lib/tapyrus/bip175.rb +67 -0
  17. data/lib/tapyrus/block.rb +1 -2
  18. data/lib/tapyrus/block_header.rb +15 -9
  19. data/lib/tapyrus/bloom_filter.rb +5 -3
  20. data/lib/tapyrus/chain_params.rb +1 -4
  21. data/lib/tapyrus/chainparams/dev.yml +3 -2
  22. data/lib/tapyrus/chainparams/prod.yml +3 -2
  23. data/lib/tapyrus/constants.rb +29 -23
  24. data/lib/tapyrus/errors.rb +1 -3
  25. data/lib/tapyrus/ext.rb +1 -1
  26. data/lib/tapyrus/ext/ecdsa.rb +4 -4
  27. data/lib/tapyrus/ext/json_parser.rb +1 -4
  28. data/lib/tapyrus/ext_key.rb +44 -32
  29. data/lib/tapyrus/key.rb +31 -35
  30. data/lib/tapyrus/key_path.rb +15 -12
  31. data/lib/tapyrus/logger.rb +20 -16
  32. data/lib/tapyrus/merkle_tree.rb +19 -20
  33. data/lib/tapyrus/message.rb +14 -16
  34. data/lib/tapyrus/message/addr.rb +1 -7
  35. data/lib/tapyrus/message/base.rb +0 -3
  36. data/lib/tapyrus/message/block.rb +2 -9
  37. data/lib/tapyrus/message/block_transaction_request.rb +3 -6
  38. data/lib/tapyrus/message/block_transactions.rb +2 -6
  39. data/lib/tapyrus/message/block_txn.rb +0 -4
  40. data/lib/tapyrus/message/cmpct_block.rb +1 -7
  41. data/lib/tapyrus/message/error.rb +1 -4
  42. data/lib/tapyrus/message/fee_filter.rb +1 -4
  43. data/lib/tapyrus/message/filter_add.rb +0 -4
  44. data/lib/tapyrus/message/filter_clear.rb +0 -4
  45. data/lib/tapyrus/message/filter_load.rb +2 -5
  46. data/lib/tapyrus/message/get_addr.rb +0 -4
  47. data/lib/tapyrus/message/get_block_txn.rb +0 -4
  48. data/lib/tapyrus/message/get_blocks.rb +0 -3
  49. data/lib/tapyrus/message/get_data.rb +1 -4
  50. data/lib/tapyrus/message/get_headers.rb +1 -3
  51. data/lib/tapyrus/message/header_and_short_ids.rb +3 -9
  52. data/lib/tapyrus/message/headers.rb +0 -4
  53. data/lib/tapyrus/message/headers_parser.rb +3 -8
  54. data/lib/tapyrus/message/inv.rb +1 -4
  55. data/lib/tapyrus/message/inventories_parser.rb +2 -7
  56. data/lib/tapyrus/message/inventory.rb +12 -5
  57. data/lib/tapyrus/message/mem_pool.rb +0 -4
  58. data/lib/tapyrus/message/merkle_block.rb +4 -9
  59. data/lib/tapyrus/message/network_addr.rb +7 -6
  60. data/lib/tapyrus/message/not_found.rb +0 -3
  61. data/lib/tapyrus/message/ping.rb +0 -3
  62. data/lib/tapyrus/message/pong.rb +0 -3
  63. data/lib/tapyrus/message/prefilled_tx.rb +0 -4
  64. data/lib/tapyrus/message/reject.rb +0 -3
  65. data/lib/tapyrus/message/send_cmpct.rb +1 -3
  66. data/lib/tapyrus/message/send_headers.rb +0 -3
  67. data/lib/tapyrus/message/tx.rb +0 -4
  68. data/lib/tapyrus/message/ver_ack.rb +1 -5
  69. data/lib/tapyrus/message/version.rb +2 -5
  70. data/lib/tapyrus/mnemonic.rb +17 -15
  71. data/lib/tapyrus/network.rb +0 -2
  72. data/lib/tapyrus/network/connection.rb +0 -3
  73. data/lib/tapyrus/network/message_handler.rb +61 -60
  74. data/lib/tapyrus/network/peer.rb +13 -12
  75. data/lib/tapyrus/network/peer_discovery.rb +3 -5
  76. data/lib/tapyrus/network/pool.rb +12 -12
  77. data/lib/tapyrus/node.rb +1 -1
  78. data/lib/tapyrus/node/cli.rb +12 -14
  79. data/lib/tapyrus/node/configuration.rb +1 -3
  80. data/lib/tapyrus/node/spv.rb +2 -3
  81. data/lib/tapyrus/opcodes.rb +9 -7
  82. data/lib/tapyrus/out_point.rb +5 -5
  83. data/lib/tapyrus/rpc.rb +1 -0
  84. data/lib/tapyrus/rpc/http_server.rb +21 -22
  85. data/lib/tapyrus/rpc/request_handler.rb +42 -44
  86. data/lib/tapyrus/rpc/tapyrus_core_client.rb +67 -25
  87. data/lib/tapyrus/script/color.rb +20 -2
  88. data/lib/tapyrus/script/multisig.rb +13 -12
  89. data/lib/tapyrus/script/script.rb +104 -67
  90. data/lib/tapyrus/script/script_error.rb +1 -4
  91. data/lib/tapyrus/script/script_interpreter.rb +439 -399
  92. data/lib/tapyrus/script/tx_checker.rb +20 -10
  93. data/lib/tapyrus/secp256k1.rb +0 -4
  94. data/lib/tapyrus/secp256k1/native.rb +14 -15
  95. data/lib/tapyrus/secp256k1/rfc6979.rb +7 -4
  96. data/lib/tapyrus/secp256k1/ruby.rb +10 -12
  97. data/lib/tapyrus/slip39.rb +20 -5
  98. data/lib/tapyrus/slip39/share.rb +41 -29
  99. data/lib/tapyrus/slip39/sss.rb +101 -57
  100. data/lib/tapyrus/store.rb +1 -3
  101. data/lib/tapyrus/store/chain_entry.rb +0 -4
  102. data/lib/tapyrus/store/db.rb +0 -2
  103. data/lib/tapyrus/store/db/level_db.rb +5 -9
  104. data/lib/tapyrus/store/spv_chain.rb +11 -17
  105. data/lib/tapyrus/tx.rb +45 -37
  106. data/lib/tapyrus/tx_builder.rb +158 -0
  107. data/lib/tapyrus/tx_in.rb +1 -6
  108. data/lib/tapyrus/tx_out.rb +2 -7
  109. data/lib/tapyrus/util.rb +20 -7
  110. data/lib/tapyrus/validation.rb +12 -11
  111. data/lib/tapyrus/version.rb +1 -1
  112. data/lib/tapyrus/wallet/account.rb +22 -18
  113. data/lib/tapyrus/wallet/base.rb +12 -9
  114. data/lib/tapyrus/wallet/db.rb +6 -9
  115. data/lib/tapyrus/wallet/master_key.rb +2 -4
  116. data/tapyrusrb.gemspec +13 -16
  117. metadata +22 -31
  118. data/.travis.yml +0 -12
@@ -1,10 +1,8 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # block message
5
4
  # https://bitcoin.org/en/developer-reference#block
6
5
  class Block < Base
7
-
8
6
  attr_accessor :header
9
7
  attr_accessor :transactions
10
8
  attr_accessor :use_segwit
@@ -23,24 +21,19 @@ module Tapyrus
23
21
  transactions = []
24
22
  unless buf.eof?
25
23
  tx_count = Tapyrus.unpack_var_int_from_io(buf)
26
- tx_count.times do
27
- transactions << Tapyrus::Tx.parse_from_payload(buf)
28
- end
24
+ tx_count.times { transactions << Tapyrus::Tx.parse_from_payload(buf) }
29
25
  end
30
26
  new(header, transactions)
31
27
  end
32
28
 
33
29
  def to_payload
34
- header.to_payload << Tapyrus.pack_var_int(transactions.size) <<
35
- transactions.map(&:to_payload).join
30
+ header.to_payload << Tapyrus.pack_var_int(transactions.size) << transactions.map(&:to_payload).join
36
31
  end
37
32
 
38
33
  # generate Tapyrus::Block object.
39
34
  def to_block
40
35
  Tapyrus::Block.new(header, transactions)
41
36
  end
42
-
43
37
  end
44
-
45
38
  end
46
39
  end
@@ -1,10 +1,8 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # BIP-152 Compact Block's data format.
5
4
  # https://github.com/bitcoin/bips/blob/master/bip-0152.mediawiki#BlockTransactionsRequest
6
5
  class BlockTransactionRequest
7
-
8
6
  attr_accessor :block_hash # When matching with Tapyrus::BlockHeader#hash It is necessary to reverse the byte order.
9
7
  attr_accessor :indexes
10
8
 
@@ -17,7 +15,8 @@ module Tapyrus
17
15
  buf = StringIO.new(payload)
18
16
  block_hash = buf.read(32).bth
19
17
  index_len = Tapyrus.unpack_var_int_from_io(buf)
20
- indexes = index_len.times.map{Tapyrus.unpack_var_int_from_io(buf)}
18
+ indexes = index_len.times.map { Tapyrus.unpack_var_int_from_io(buf) }
19
+
21
20
  # index data differentially encoded
22
21
  offset = 0
23
22
  index_len.times do |i|
@@ -33,13 +32,11 @@ module Tapyrus
33
32
  p = block_hash.htb << Tapyrus.pack_var_int(indexes.size)
34
33
  indexes.size.times do |i|
35
34
  index = indexes[i]
36
- index -= indexes[i-1] + 1 if i > 0
35
+ index -= indexes[i - 1] + 1 if i > 0
37
36
  p << Tapyrus.pack_var_int(index)
38
37
  end
39
38
  p
40
39
  end
41
-
42
40
  end
43
-
44
41
  end
45
42
  end
@@ -1,10 +1,8 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # BIP-152 Compact Block's data format.
5
4
  # https://github.com/bitcoin/bips/blob/master/bip-0152.mediawiki#BlockTransactions
6
5
  class BlockTransactions
7
-
8
6
  attr_accessor :block_hash
9
7
  attr_accessor :transactions
10
8
 
@@ -17,15 +15,13 @@ module Tapyrus
17
15
  buf = StringIO.new(payload)
18
16
  block_hash = buf.read(32).bth
19
17
  tx_count = Tapyrus.unpack_var_int_from_io(buf)
20
- txn = tx_count.times.map{Tapyrus::Tx.parse_from_payload(buf)}
18
+ txn = tx_count.times.map { Tapyrus::Tx.parse_from_payload(buf) }
21
19
  self.new(block_hash, txn)
22
20
  end
23
21
 
24
22
  def to_payload
25
23
  block_hash.htb << Tapyrus.pack_var_int(transactions.size) << transactions.map(&:to_payload).join
26
24
  end
27
-
28
25
  end
29
-
30
26
  end
31
- end
27
+ end
@@ -1,10 +1,8 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # blocktxn message.
5
4
  # https://github.com/bitcoin/bips/blob/master/bip-0152.mediawiki#blocktxn
6
5
  class BlockTxn < Base
7
-
8
6
  COMMAND = 'blocktxn'
9
7
 
10
8
  attr_accessor :block_transactions
@@ -20,8 +18,6 @@ module Tapyrus
20
18
  def to_payload
21
19
  block_transactions.to_payload
22
20
  end
23
-
24
21
  end
25
-
26
22
  end
27
23
  end
@@ -1,10 +1,8 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # cmpctblock message. support only version 1.
5
4
  # https://github.com/bitcoin/bips/blob/master/bip-0152.mediawiki
6
5
  class CmpctBlock < Base
7
-
8
6
  COMMAND = 'cmpctblock'
9
7
 
10
8
  attr_accessor :header_and_short_ids
@@ -19,9 +17,7 @@ module Tapyrus
19
17
  # @return [Tapyrus::Message::CmpctBlock]
20
18
  def self.from_block(block, nonce = SecureRandom.hex(8).to_i(16))
21
19
  h = HeaderAndShortIDs.new(block.header, nonce)
22
- block.transactions[1..-1].each do |tx|
23
- h.short_ids << h.short_id(tx.txid)
24
- end
20
+ block.transactions[1..-1].each { |tx| h.short_ids << h.short_id(tx.txid) }
25
21
  h.prefilled_txn << PrefilledTx.new(0, block.transactions.first)
26
22
  self.new(h)
27
23
  end
@@ -33,8 +29,6 @@ module Tapyrus
33
29
  def to_payload
34
30
  header_and_short_ids.to_payload
35
31
  end
36
-
37
32
  end
38
-
39
33
  end
40
34
  end
@@ -1,10 +1,7 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # P2P message layer Error
5
4
  class Error < StandardError
6
-
7
5
  end
8
-
9
6
  end
10
- end
7
+ end
@@ -1,10 +1,8 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # feefilter message
5
4
  # https://bitcoin.org/en/developer-reference#feefilter
6
5
  class FeeFilter < Base
7
-
8
6
  COMMAND = 'feefilter'
9
7
 
10
8
  # The fee rate (in satoshis per kilobyte)
@@ -21,7 +19,6 @@ module Tapyrus
21
19
  def to_payload
22
20
  [fee_rate].pack('Q')
23
21
  end
24
-
25
22
  end
26
23
  end
27
- end
24
+ end
@@ -1,10 +1,8 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # filteradd message
5
4
  # https://bitcoin.org/en/developer-reference#filteradd
6
5
  class FilterAdd < Base
7
-
8
6
  COMMAND = 'filteradd'
9
7
 
10
8
  # element must be sent in the byte order they would use when appearing in a raw transaction;
@@ -21,8 +19,6 @@ module Tapyrus
21
19
  def to_payload
22
20
  Tapyrus.pack_var_string(element.htb)
23
21
  end
24
-
25
22
  end
26
-
27
23
  end
28
24
  end
@@ -1,17 +1,13 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # filterclear message
5
4
  # https://bitcoin.org/en/developer-reference#filterclear
6
5
  class FilterClear < Base
7
-
8
6
  COMMAND = 'filterclear'
9
7
 
10
8
  def to_payload
11
9
  ''
12
10
  end
13
-
14
11
  end
15
-
16
12
  end
17
13
  end
@@ -1,10 +1,8 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # filterload message
5
4
  # https://bitcoin.org/en/developer-reference#filterload
6
5
  class FilterLoad < Base
7
-
8
6
  COMMAND = 'filterload'
9
7
 
10
8
  BLOOM_UPDATE_NONE = 0
@@ -30,10 +28,9 @@ module Tapyrus
30
28
  end
31
29
 
32
30
  def to_payload
33
- Tapyrus.pack_var_int(filter.filter.size) << filter.filter.pack('C*') << [filter.hash_funcs, filter.tweak, flag].pack('VVC')
31
+ Tapyrus.pack_var_int(filter.filter.size) << filter.filter.pack('C*') <<
32
+ [filter.hash_funcs, filter.tweak, flag].pack('VVC')
34
33
  end
35
-
36
34
  end
37
-
38
35
  end
39
36
  end
@@ -1,17 +1,13 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # getaddr message
5
4
  # https://bitcoin.org/en/developer-reference#getaddr
6
5
  class GetAddr < Base
7
-
8
6
  COMMAND = 'getaddr'
9
7
 
10
8
  def to_payload
11
9
  ''
12
10
  end
13
-
14
11
  end
15
-
16
12
  end
17
13
  end
@@ -1,10 +1,8 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # getblocktxn message.
5
4
  # https://github.com/bitcoin/bips/blob/master/bip-0152.mediawiki
6
5
  class GetBlockTxn < Base
7
-
8
6
  COMMAND = 'getblocktxn'
9
7
 
10
8
  attr_accessor :request
@@ -20,8 +18,6 @@ module Tapyrus
20
18
  def to_payload
21
19
  request.to_payload
22
20
  end
23
-
24
21
  end
25
-
26
22
  end
27
23
  end
@@ -1,6 +1,5 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # block message
5
4
  # https://bitcoin.org/en/developer-reference#getblocks
6
5
  class GetBlocks < Base
@@ -22,8 +21,6 @@ module Tapyrus
22
21
  @hashes = hashes
23
22
  @stop_hash = stop_hash
24
23
  end
25
-
26
24
  end
27
-
28
25
  end
29
26
  end
@@ -1,21 +1,18 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # getdadta message
5
4
  # https://bitcoin.org/en/developer-reference#getdata
6
5
  class GetData < Base
7
6
  include InventoriesParser
8
7
  extend InventoriesParser
9
8
 
10
- COMMAND ='getdata'
9
+ COMMAND = 'getdata'
11
10
 
12
11
  attr_reader :inventories
13
12
 
14
13
  def initialize(inventories = [])
15
14
  @inventories = inventories
16
15
  end
17
-
18
16
  end
19
-
20
17
  end
21
18
  end
@@ -1,6 +1,5 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # getheaders message
5
4
  # https://bitcoin.org/en/developer-reference#getheaders
6
5
  class GetHeaders < Base
@@ -22,7 +21,6 @@ module Tapyrus
22
21
  @hashes = hashes
23
22
  @stop_hash = stop_hash
24
23
  end
25
-
26
24
  end
27
25
  end
28
- end
26
+ end
@@ -1,7 +1,6 @@
1
1
  require 'siphash'
2
2
  module Tapyrus
3
3
  module Message
4
-
5
4
  # BIP-152 Compact Block's data format.
6
5
  # https://github.com/bitcoin/bips/blob/master/bip-0152.mediawiki#HeaderAndShortIDs
7
6
  class HeaderAndShortIDs
@@ -24,13 +23,9 @@ module Tapyrus
24
23
  header = Tapyrus::BlockHeader.parse_from_payload(buf)
25
24
  nonce = buf.read(8).unpack('q*').first
26
25
  short_ids_len = Tapyrus.unpack_var_int_from_io(buf)
27
- short_ids = short_ids_len.times.map do
28
- buf.read(6).reverse.bth.to_i(16)
29
- end
26
+ short_ids = short_ids_len.times.map { buf.read(6).reverse.bth.to_i(16) }
30
27
  prefilled_txn_len = Tapyrus.unpack_var_int_from_io(buf)
31
- prefilled_txn = prefilled_txn_len.times.map do
32
- PrefilledTx.parse_from_io(buf)
33
- end
28
+ prefilled_txn = prefilled_txn_len.times.map { PrefilledTx.parse_from_io(buf) }
34
29
  self.new(header, nonce, short_ids, prefilled_txn)
35
30
  end
36
31
 
@@ -38,7 +33,7 @@ module Tapyrus
38
33
  p = header.to_payload
39
34
  p << [nonce].pack('q*')
40
35
  p << Tapyrus.pack_var_int(short_ids.size)
41
- p << short_ids.map{|id|sprintf('%12x', id).htb.reverse}.join
36
+ p << short_ids.map { |id| sprintf('%12x', id).htb.reverse }.join
42
37
  p << Tapyrus.pack_var_int(prefilled_txn.size)
43
38
  p << prefilled_txn.map(&:to_payload).join
44
39
  p
@@ -51,7 +46,6 @@ module Tapyrus
51
46
  hash = SipHash.digest(siphash_key, txid.htb.reverse).to_even_length_hex
52
47
  [hash].pack('H*')[2...8].bth.to_i(16)
53
48
  end
54
-
55
49
  end
56
50
  end
57
51
  end
@@ -1,10 +1,8 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # headers message
5
4
  # https://bitcoin.org/en/developer-reference#headers
6
5
  class Headers < Base
7
-
8
6
  COMMAND = 'headers'
9
7
 
10
8
  # Array[Tapyrus::BlockHeader]
@@ -28,8 +26,6 @@ module Tapyrus
28
26
  def to_payload
29
27
  Tapyrus.pack_var_int(headers.size) << headers.map { |h| h.to_payload << 0x00 }.join
30
28
  end
31
-
32
29
  end
33
-
34
30
  end
35
31
  end
@@ -1,24 +1,19 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # Common message parser which handle multiple block headers as a payload.
5
4
  module HeadersParser
6
-
7
5
  def parse_from_payload(payload)
8
6
  ver, payload = payload.unpack('Va*')
9
7
  size, payload = Tapyrus.unpack_var_int(payload)
10
8
  hashes = []
11
9
  buf = StringIO.new(payload)
12
- size.times do
13
- hashes << buf.read(32).bth
14
- end
10
+ size.times { hashes << buf.read(32).bth }
15
11
  new(ver, hashes, buf.read(32).bth)
16
12
  end
17
13
 
18
14
  def to_payload
19
- [version].pack('V') << Tapyrus.pack_var_int(hashes.length) << hashes.map{|h|h.htb}.join << stop_hash.htb
15
+ [version].pack('V') << Tapyrus.pack_var_int(hashes.length) << hashes.map { |h| h.htb }.join << stop_hash.htb
20
16
  end
21
-
22
17
  end
23
18
  end
24
- end
19
+ end
@@ -1,6 +1,5 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # inv message
5
4
  # https://bitcoin.org/en/developer-reference#inv
6
5
  class Inv < Base
@@ -14,8 +13,6 @@ module Tapyrus
14
13
  def initialize(inventories = [])
15
14
  @inventories = inventories
16
15
  end
17
-
18
16
  end
19
-
20
17
  end
21
- end
18
+ end