tapyrus 0.2.3 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) 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 +2 -3
  12. data/lib/schnorr/signature.rb +3 -6
  13. data/lib/tapyrus.rb +7 -30
  14. data/lib/tapyrus/base58.rb +7 -6
  15. data/lib/tapyrus/block.rb +1 -2
  16. data/lib/tapyrus/block_header.rb +15 -9
  17. data/lib/tapyrus/bloom_filter.rb +5 -3
  18. data/lib/tapyrus/chain_params.rb +1 -4
  19. data/lib/tapyrus/chainparams/dev.yml +3 -2
  20. data/lib/tapyrus/chainparams/prod.yml +3 -2
  21. data/lib/tapyrus/constants.rb +29 -23
  22. data/lib/tapyrus/errors.rb +1 -3
  23. data/lib/tapyrus/ext.rb +1 -1
  24. data/lib/tapyrus/ext/ecdsa.rb +4 -4
  25. data/lib/tapyrus/ext/json_parser.rb +1 -4
  26. data/lib/tapyrus/ext_key.rb +38 -34
  27. data/lib/tapyrus/key.rb +31 -35
  28. data/lib/tapyrus/key_path.rb +15 -12
  29. data/lib/tapyrus/logger.rb +20 -16
  30. data/lib/tapyrus/merkle_tree.rb +19 -20
  31. data/lib/tapyrus/message.rb +14 -16
  32. data/lib/tapyrus/message/addr.rb +1 -7
  33. data/lib/tapyrus/message/base.rb +0 -3
  34. data/lib/tapyrus/message/block.rb +2 -9
  35. data/lib/tapyrus/message/block_transaction_request.rb +3 -6
  36. data/lib/tapyrus/message/block_transactions.rb +2 -6
  37. data/lib/tapyrus/message/block_txn.rb +0 -4
  38. data/lib/tapyrus/message/cmpct_block.rb +1 -7
  39. data/lib/tapyrus/message/error.rb +1 -4
  40. data/lib/tapyrus/message/fee_filter.rb +1 -4
  41. data/lib/tapyrus/message/filter_add.rb +0 -4
  42. data/lib/tapyrus/message/filter_clear.rb +0 -4
  43. data/lib/tapyrus/message/filter_load.rb +2 -5
  44. data/lib/tapyrus/message/get_addr.rb +0 -4
  45. data/lib/tapyrus/message/get_block_txn.rb +0 -4
  46. data/lib/tapyrus/message/get_blocks.rb +0 -3
  47. data/lib/tapyrus/message/get_data.rb +1 -4
  48. data/lib/tapyrus/message/get_headers.rb +1 -3
  49. data/lib/tapyrus/message/header_and_short_ids.rb +3 -9
  50. data/lib/tapyrus/message/headers.rb +0 -4
  51. data/lib/tapyrus/message/headers_parser.rb +3 -8
  52. data/lib/tapyrus/message/inv.rb +1 -4
  53. data/lib/tapyrus/message/inventories_parser.rb +2 -7
  54. data/lib/tapyrus/message/inventory.rb +12 -5
  55. data/lib/tapyrus/message/mem_pool.rb +0 -4
  56. data/lib/tapyrus/message/merkle_block.rb +4 -9
  57. data/lib/tapyrus/message/network_addr.rb +7 -6
  58. data/lib/tapyrus/message/not_found.rb +0 -3
  59. data/lib/tapyrus/message/ping.rb +0 -3
  60. data/lib/tapyrus/message/pong.rb +0 -3
  61. data/lib/tapyrus/message/prefilled_tx.rb +0 -4
  62. data/lib/tapyrus/message/reject.rb +0 -3
  63. data/lib/tapyrus/message/send_cmpct.rb +1 -3
  64. data/lib/tapyrus/message/send_headers.rb +0 -3
  65. data/lib/tapyrus/message/tx.rb +0 -4
  66. data/lib/tapyrus/message/ver_ack.rb +1 -5
  67. data/lib/tapyrus/message/version.rb +2 -5
  68. data/lib/tapyrus/mnemonic.rb +17 -15
  69. data/lib/tapyrus/network.rb +0 -2
  70. data/lib/tapyrus/network/connection.rb +0 -3
  71. data/lib/tapyrus/network/message_handler.rb +61 -60
  72. data/lib/tapyrus/network/peer.rb +13 -12
  73. data/lib/tapyrus/network/peer_discovery.rb +3 -5
  74. data/lib/tapyrus/network/pool.rb +12 -12
  75. data/lib/tapyrus/node.rb +1 -1
  76. data/lib/tapyrus/node/cli.rb +12 -14
  77. data/lib/tapyrus/node/configuration.rb +1 -3
  78. data/lib/tapyrus/node/spv.rb +2 -3
  79. data/lib/tapyrus/opcodes.rb +9 -7
  80. data/lib/tapyrus/out_point.rb +5 -5
  81. data/lib/tapyrus/rpc/http_server.rb +21 -22
  82. data/lib/tapyrus/rpc/request_handler.rb +42 -44
  83. data/lib/tapyrus/rpc/tapyrus_core_client.rb +53 -25
  84. data/lib/tapyrus/script/color.rb +20 -2
  85. data/lib/tapyrus/script/multisig.rb +13 -12
  86. data/lib/tapyrus/script/script.rb +104 -67
  87. data/lib/tapyrus/script/script_error.rb +1 -4
  88. data/lib/tapyrus/script/script_interpreter.rb +439 -399
  89. data/lib/tapyrus/script/tx_checker.rb +20 -10
  90. data/lib/tapyrus/secp256k1.rb +0 -4
  91. data/lib/tapyrus/secp256k1/native.rb +17 -18
  92. data/lib/tapyrus/secp256k1/rfc6979.rb +7 -4
  93. data/lib/tapyrus/secp256k1/ruby.rb +10 -12
  94. data/lib/tapyrus/slip39.rb +20 -5
  95. data/lib/tapyrus/slip39/share.rb +41 -29
  96. data/lib/tapyrus/slip39/sss.rb +101 -57
  97. data/lib/tapyrus/store.rb +1 -3
  98. data/lib/tapyrus/store/chain_entry.rb +0 -4
  99. data/lib/tapyrus/store/db.rb +0 -2
  100. data/lib/tapyrus/store/db/level_db.rb +5 -9
  101. data/lib/tapyrus/store/spv_chain.rb +11 -17
  102. data/lib/tapyrus/tx.rb +45 -37
  103. data/lib/tapyrus/tx_builder.rb +158 -0
  104. data/lib/tapyrus/tx_in.rb +1 -6
  105. data/lib/tapyrus/tx_out.rb +2 -7
  106. data/lib/tapyrus/util.rb +20 -7
  107. data/lib/tapyrus/validation.rb +12 -11
  108. data/lib/tapyrus/version.rb +1 -1
  109. data/lib/tapyrus/wallet/account.rb +22 -18
  110. data/lib/tapyrus/wallet/base.rb +12 -9
  111. data/lib/tapyrus/wallet/db.rb +6 -9
  112. data/lib/tapyrus/wallet/master_key.rb +2 -4
  113. data/tapyrusrb.gemspec +13 -16
  114. metadata +20 -31
  115. data/.travis.yml +0 -12
@@ -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
@@ -1,23 +1,18 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # Common message parser which only handle multiple inventory as payload.
5
4
  module InventoriesParser
6
-
7
5
  def parse_from_payload(payload)
8
6
  size, payload = Tapyrus.unpack_var_int(payload)
9
7
  buf = StringIO.new(payload)
10
8
  i = new
11
- size.times do
12
- i.inventories << Inventory.parse_from_payload(buf.read(36))
13
- end
9
+ size.times { i.inventories << Inventory.parse_from_payload(buf.read(36)) }
14
10
  i
15
11
  end
16
12
 
17
13
  def to_payload
18
14
  Tapyrus.pack_var_int(inventories.length) << inventories.map(&:to_payload).join
19
15
  end
20
-
21
16
  end
22
17
  end
23
- end
18
+ end
@@ -3,7 +3,6 @@ module Tapyrus
3
3
  # inventory class. inventory is a part of message.
4
4
  # https://bitcoin.org/en/developer-reference#term-inventory
5
5
  class Inventory
6
-
7
6
  SEGWIT_FLAG = 1 << 30
8
7
 
9
8
  MSG_TX = 1
@@ -18,7 +17,9 @@ module Tapyrus
18
17
  attr_accessor :hash
19
18
 
20
19
  def initialize(identifier, hash)
21
- raise Error, "invalid type identifier specified. identifier = #{identifier}" unless valid_identifier?(identifier)
20
+ unless valid_identifier?(identifier)
21
+ raise Error, "invalid type identifier specified. identifier = #{identifier}"
22
+ end
22
23
  @identifier = identifier
23
24
  @hash = hash
24
25
  end
@@ -42,10 +43,16 @@ module Tapyrus
42
43
  private
43
44
 
44
45
  def valid_identifier?(identifier)
45
- [MSG_TX, MSG_BLOCK, MSG_FILTERED_BLOCK, MSG_CMPCT_BLOCK, MSG_WITNESS_TX,
46
- MSG_WITNESS_BLOCK, MSG_FILTERED_WITNESS_BLOCK].include?(identifier)
46
+ [
47
+ MSG_TX,
48
+ MSG_BLOCK,
49
+ MSG_FILTERED_BLOCK,
50
+ MSG_CMPCT_BLOCK,
51
+ MSG_WITNESS_TX,
52
+ MSG_WITNESS_BLOCK,
53
+ MSG_FILTERED_WITNESS_BLOCK
54
+ ].include?(identifier)
47
55
  end
48
-
49
56
  end
50
57
  end
51
58
  end
@@ -1,17 +1,13 @@
1
1
  module Tapyrus
2
2
  module Message
3
-
4
3
  # mempool message
5
4
  # https://bitcoin.org/en/developer-reference#mempool
6
5
  class MemPool < Base
7
-
8
6
  COMMAND = 'mempool'
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
  # merckleblock message
5
4
  # https://bitcoin.org/en/developer-reference#merkleblock
6
5
  class MerkleBlock < Base
7
-
8
6
  COMMAND = 'merkleblock'
9
7
 
10
8
  attr_accessor :header
@@ -22,21 +20,18 @@ module Tapyrus
22
20
  m.header = Tapyrus::BlockHeader.parse_from_payload(buf)
23
21
  m.tx_count = buf.read(4).unpack('V').first
24
22
  hash_count = Tapyrus.unpack_var_int_from_io(buf)
25
- hash_count.times do
26
- m.hashes << buf.read(32).bth
27
- end
23
+ hash_count.times { m.hashes << buf.read(32).bth }
28
24
  flag_count = Tapyrus.unpack_var_int_from_io(buf)
25
+
29
26
  # A sequence of bits packed eight in a byte with the least significant bit first.
30
27
  m.flags = buf.read(flag_count).bth
31
28
  m
32
29
  end
33
30
 
34
31
  def to_payload
35
- header.to_payload << [tx_count].pack('V') << Tapyrus.pack_var_int(hashes.size) <<
36
- hashes.map(&:htb).join << Tapyrus.pack_var_int(flags.htb.bytesize) << flags.htb
32
+ header.to_payload << [tx_count].pack('V') << Tapyrus.pack_var_int(hashes.size) << hashes.map(&:htb).join <<
33
+ Tapyrus.pack_var_int(flags.htb.bytesize) << flags.htb
37
34
  end
38
-
39
35
  end
40
-
41
36
  end
42
37
  end
@@ -2,9 +2,7 @@ require 'ipaddr'
2
2
 
3
3
  module Tapyrus
4
4
  module Message
5
-
6
5
  class NetworkAddr
7
-
8
6
  # unix time.
9
7
  # Nodes advertising their own IP address set this to the current time.
10
8
  # Nodes advertising IP addresses they’ve connected to set this to the last time they connected to that node.
@@ -20,7 +18,12 @@ module Tapyrus
20
18
 
21
19
  attr_reader :skip_time
22
20
 
23
- def initialize(ip: '127.0.0.1', port: Tapyrus.chain_params.default_port, services: DEFAULT_SERVICE_FLAGS, time: Time.now.to_i)
21
+ def initialize(
22
+ ip: '127.0.0.1',
23
+ port: Tapyrus.chain_params.default_port,
24
+ services: DEFAULT_SERVICE_FLAGS,
25
+ time: Time.now.to_i
26
+ )
24
27
  @time = time
25
28
  @ip_addr = IPAddr.new(ip)
26
29
  @port = port
@@ -33,7 +36,7 @@ module Tapyrus
33
36
  addr = new(time: nil)
34
37
  addr.time = buf.read(4).unpack('V').first if has_time
35
38
  addr.services = buf.read(8).unpack('Q').first
36
- addr.ip_addr = IPAddr::new_ntoh(buf.read(16))
39
+ addr.ip_addr = IPAddr.new_ntoh(buf.read(16))
37
40
  addr.port = buf.read(2).unpack('n').first
38
41
  addr
39
42
  end
@@ -56,8 +59,6 @@ module Tapyrus
56
59
  addr = ip_addr.ipv4? ? ip_addr.ipv4_mapped : ip_addr
57
60
  p << [services].pack('Q') << addr.hton << [port].pack('n')
58
61
  end
59
-
60
62
  end
61
-
62
63
  end
63
64
  end