tapyrus 0.2.7 → 0.2.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +37 -0
- data/.prettierignore +3 -0
- data/.prettierrc.yaml +3 -0
- data/.ruby-version +1 -1
- data/CODE_OF_CONDUCT.md +7 -7
- data/README.md +14 -17
- data/Rakefile +3 -3
- data/lib/openassets/marker_output.rb +0 -4
- data/lib/openassets/payload.rb +4 -10
- data/lib/openassets.rb +0 -2
- data/lib/schnorr/sign_to_contract.rb +51 -0
- data/lib/schnorr/signature.rb +3 -6
- data/lib/schnorr.rb +14 -9
- data/lib/tapyrus/base58.rb +7 -6
- data/lib/tapyrus/bip175.rb +78 -0
- data/lib/tapyrus/block.rb +1 -2
- data/lib/tapyrus/block_header.rb +15 -9
- data/lib/tapyrus/bloom_filter.rb +5 -3
- data/lib/tapyrus/chain_params.rb +1 -4
- data/lib/tapyrus/chainparams/dev.yml +3 -2
- data/lib/tapyrus/chainparams/prod.yml +3 -2
- data/lib/tapyrus/constants.rb +29 -23
- data/lib/tapyrus/errors.rb +1 -3
- data/lib/tapyrus/ext/ecdsa.rb +4 -4
- data/lib/tapyrus/ext/json_parser.rb +1 -4
- data/lib/tapyrus/ext.rb +1 -1
- data/lib/tapyrus/ext_key.rb +44 -32
- data/lib/tapyrus/key.rb +31 -35
- data/lib/tapyrus/key_path.rb +15 -12
- data/lib/tapyrus/logger.rb +20 -16
- data/lib/tapyrus/merkle_tree.rb +22 -20
- data/lib/tapyrus/message/addr.rb +1 -7
- data/lib/tapyrus/message/base.rb +0 -3
- data/lib/tapyrus/message/block.rb +2 -9
- data/lib/tapyrus/message/block_transaction_request.rb +3 -6
- data/lib/tapyrus/message/block_transactions.rb +2 -6
- data/lib/tapyrus/message/block_txn.rb +0 -4
- data/lib/tapyrus/message/cmpct_block.rb +1 -7
- data/lib/tapyrus/message/error.rb +1 -4
- data/lib/tapyrus/message/fee_filter.rb +1 -4
- data/lib/tapyrus/message/filter_add.rb +0 -4
- data/lib/tapyrus/message/filter_clear.rb +0 -4
- data/lib/tapyrus/message/filter_load.rb +2 -5
- data/lib/tapyrus/message/get_addr.rb +0 -4
- data/lib/tapyrus/message/get_block_txn.rb +0 -4
- data/lib/tapyrus/message/get_blocks.rb +0 -3
- data/lib/tapyrus/message/get_data.rb +1 -4
- data/lib/tapyrus/message/get_headers.rb +1 -3
- data/lib/tapyrus/message/header_and_short_ids.rb +3 -9
- data/lib/tapyrus/message/headers.rb +0 -4
- data/lib/tapyrus/message/headers_parser.rb +3 -8
- data/lib/tapyrus/message/inv.rb +1 -4
- data/lib/tapyrus/message/inventories_parser.rb +2 -7
- data/lib/tapyrus/message/inventory.rb +12 -5
- data/lib/tapyrus/message/mem_pool.rb +0 -4
- data/lib/tapyrus/message/merkle_block.rb +4 -9
- data/lib/tapyrus/message/network_addr.rb +7 -6
- data/lib/tapyrus/message/not_found.rb +0 -3
- data/lib/tapyrus/message/ping.rb +0 -3
- data/lib/tapyrus/message/pong.rb +0 -3
- data/lib/tapyrus/message/prefilled_tx.rb +0 -4
- data/lib/tapyrus/message/reject.rb +0 -3
- data/lib/tapyrus/message/send_cmpct.rb +1 -3
- data/lib/tapyrus/message/send_headers.rb +0 -3
- data/lib/tapyrus/message/tx.rb +0 -4
- data/lib/tapyrus/message/ver_ack.rb +1 -5
- data/lib/tapyrus/message/version.rb +2 -5
- data/lib/tapyrus/message.rb +14 -16
- data/lib/tapyrus/mnemonic.rb +17 -15
- data/lib/tapyrus/network/connection.rb +0 -3
- data/lib/tapyrus/network/message_handler.rb +61 -60
- data/lib/tapyrus/network/peer.rb +13 -12
- data/lib/tapyrus/network/peer_discovery.rb +10 -9
- data/lib/tapyrus/network/pool.rb +12 -12
- data/lib/tapyrus/network.rb +0 -2
- data/lib/tapyrus/node/cli.rb +12 -14
- data/lib/tapyrus/node/configuration.rb +1 -3
- data/lib/tapyrus/node/spv.rb +2 -3
- data/lib/tapyrus/node.rb +1 -1
- data/lib/tapyrus/opcodes.rb +9 -7
- data/lib/tapyrus/out_point.rb +5 -5
- data/lib/tapyrus/rpc/http_server.rb +21 -22
- data/lib/tapyrus/rpc/request_handler.rb +16 -21
- data/lib/tapyrus/rpc/tapyrus_core_client.rb +67 -25
- data/lib/tapyrus/rpc.rb +1 -0
- data/lib/tapyrus/script/color.rb +10 -0
- data/lib/tapyrus/script/multisig.rb +13 -12
- data/lib/tapyrus/script/script.rb +93 -88
- data/lib/tapyrus/script/script_error.rb +1 -4
- data/lib/tapyrus/script/script_interpreter.rb +439 -399
- data/lib/tapyrus/script/tx_checker.rb +20 -10
- data/lib/tapyrus/secp256k1/native.rb +14 -15
- data/lib/tapyrus/secp256k1/rfc6979.rb +7 -4
- data/lib/tapyrus/secp256k1/ruby.rb +10 -12
- data/lib/tapyrus/secp256k1.rb +0 -4
- data/lib/tapyrus/slip39/share.rb +41 -29
- data/lib/tapyrus/slip39/sss.rb +92 -49
- data/lib/tapyrus/slip39.rb +20 -5
- data/lib/tapyrus/store/chain_entry.rb +0 -4
- data/lib/tapyrus/store/db/level_db.rb +5 -9
- data/lib/tapyrus/store/db.rb +0 -2
- data/lib/tapyrus/store/spv_chain.rb +11 -17
- data/lib/tapyrus/store.rb +1 -3
- data/lib/tapyrus/tx.rb +45 -37
- data/lib/tapyrus/tx_builder.rb +160 -0
- data/lib/tapyrus/tx_in.rb +1 -6
- data/lib/tapyrus/tx_out.rb +2 -7
- data/lib/tapyrus/util.rb +7 -9
- data/lib/tapyrus/validation.rb +12 -11
- data/lib/tapyrus/version.rb +1 -1
- data/lib/tapyrus/wallet/account.rb +22 -18
- data/lib/tapyrus/wallet/base.rb +12 -9
- data/lib/tapyrus/wallet/db.rb +6 -9
- data/lib/tapyrus/wallet/master_key.rb +2 -4
- data/lib/tapyrus.rb +7 -22
- data/tapyrusrb.gemspec +13 -14
- metadata +26 -7
- data/.travis.yml +0 -14
data/lib/tapyrus/merkle_tree.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
module Tapyrus
|
2
|
-
|
3
2
|
# merkle tree
|
4
3
|
class MerkleTree
|
5
|
-
|
6
4
|
attr_accessor :root
|
7
5
|
|
8
6
|
def initialize(root = nil)
|
@@ -17,11 +15,15 @@ module Tapyrus
|
|
17
15
|
if txids.size == 1
|
18
16
|
nodes = [Node.new(txids.first)]
|
19
17
|
else
|
20
|
-
nodes =
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
18
|
+
nodes =
|
19
|
+
txids
|
20
|
+
.each_slice(2)
|
21
|
+
.map do |m|
|
22
|
+
left = Node.new(m[0])
|
23
|
+
right = Node.new(m[1] ? m[1] : m[0])
|
24
|
+
[left, right]
|
25
|
+
end
|
26
|
+
.flatten
|
25
27
|
end
|
26
28
|
new(build_initial_tree(nodes))
|
27
29
|
end
|
@@ -29,7 +31,7 @@ module Tapyrus
|
|
29
31
|
# https://bitcoin.org/en/developer-reference#creating-a-merkleblock-message
|
30
32
|
def self.build_partial(tx_count, hashes, flags)
|
31
33
|
flags = flags.each_char.map(&:to_i)
|
32
|
-
root = build_initial_tree(
|
34
|
+
root = build_initial_tree(Array.new(tx_count) { Node.new })
|
33
35
|
current_node = root
|
34
36
|
hash_index = 0
|
35
37
|
flags.each do |f|
|
@@ -40,9 +42,7 @@ module Tapyrus
|
|
40
42
|
end
|
41
43
|
current_node = current_node.next_partial
|
42
44
|
if hash_index == hashes.size
|
43
|
-
if current_node&.leaf?
|
44
|
-
current_node.value = hashes.last
|
45
|
-
end
|
45
|
+
current_node.value = hashes.last if current_node&.leaf?
|
46
46
|
break
|
47
47
|
end
|
48
48
|
end
|
@@ -51,12 +51,15 @@ module Tapyrus
|
|
51
51
|
|
52
52
|
def self.build_initial_tree(nodes)
|
53
53
|
while nodes.size != 1
|
54
|
-
nodes =
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
54
|
+
nodes =
|
55
|
+
nodes
|
56
|
+
.each_slice(2)
|
57
|
+
.map do |m|
|
58
|
+
parent = Node.new
|
59
|
+
parent.left = m[0]
|
60
|
+
parent.right = m[1] ? m[1] : m[0].dup
|
61
|
+
parent
|
62
|
+
end
|
60
63
|
end
|
61
64
|
nodes.first
|
62
65
|
end
|
@@ -67,7 +70,6 @@ module Tapyrus
|
|
67
70
|
|
68
71
|
# node of merkle tree
|
69
72
|
class Node
|
70
|
-
|
71
73
|
attr_accessor :flag
|
72
74
|
attr_accessor :value
|
73
75
|
attr_accessor :parent
|
@@ -118,7 +120,7 @@ module Tapyrus
|
|
118
120
|
def depth
|
119
121
|
d = 0
|
120
122
|
current_node = self
|
121
|
-
until current_node.root?
|
123
|
+
until current_node.root?
|
122
124
|
current_node = current_node.parent
|
123
125
|
d += 1
|
124
126
|
end
|
@@ -137,7 +139,7 @@ module Tapyrus
|
|
137
139
|
i = 0
|
138
140
|
d = 1
|
139
141
|
current_node = self
|
140
|
-
until current_node.root?
|
142
|
+
until current_node.root?
|
141
143
|
i += d if current_node.parent.right == current_node
|
142
144
|
current_node = current_node.parent
|
143
145
|
d *= 2
|
data/lib/tapyrus/message/addr.rb
CHANGED
@@ -2,11 +2,9 @@ require 'ipaddr'
|
|
2
2
|
|
3
3
|
module Tapyrus
|
4
4
|
module Message
|
5
|
-
|
6
5
|
# addr message
|
7
6
|
# https://bitcoin.org/en/developer-reference#addr
|
8
7
|
class Addr < Base
|
9
|
-
|
10
8
|
COMMAND = 'addr'
|
11
9
|
|
12
10
|
attr_reader :addrs
|
@@ -19,17 +17,13 @@ module Tapyrus
|
|
19
17
|
buf = StringIO.new(payload)
|
20
18
|
addr_count = Tapyrus.unpack_var_int_from_io(buf)
|
21
19
|
addr = new
|
22
|
-
addr_count.times
|
23
|
-
addr.addrs << NetworkAddr.parse_from_payload(buf)
|
24
|
-
end
|
20
|
+
addr_count.times { addr.addrs << NetworkAddr.parse_from_payload(buf) }
|
25
21
|
addr
|
26
22
|
end
|
27
23
|
|
28
24
|
def to_payload
|
29
25
|
Tapyrus.pack_var_int(addrs.length) << addrs.map(&:to_payload).join
|
30
26
|
end
|
31
|
-
|
32
27
|
end
|
33
|
-
|
34
28
|
end
|
35
29
|
end
|
data/lib/tapyrus/message/base.rb
CHANGED
@@ -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
|
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
|
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,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,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*') <<
|
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,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,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
|
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
|
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
|
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
|
data/lib/tapyrus/message/inv.rb
CHANGED
@@ -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
|
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
|
-
|
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
|
-
[
|
46
|
-
|
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
|