Class: Tapyrus::Message::HeaderAndShortIDs
- Inherits:
-
Object
- Object
- Tapyrus::Message::HeaderAndShortIDs
- Defined in:
- lib/tapyrus/message/header_and_short_ids.rb
Overview
BIP-152 Compact Block’s data format. github.com/bitcoin/bips/blob/master/bip-0152.mediawiki#HeaderAndShortIDs
Instance Attribute Summary collapse
-
#header ⇒ Object
Returns the value of attribute header.
-
#nonce ⇒ Object
Returns the value of attribute nonce.
-
#prefilled_txn ⇒ Object
Returns the value of attribute prefilled_txn.
-
#short_ids ⇒ Object
Returns the value of attribute short_ids.
-
#siphash_key ⇒ Object
Returns the value of attribute siphash_key.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(header, nonce, short_ids = [], prefilled_txn = []) ⇒ HeaderAndShortIDs
constructor
A new instance of HeaderAndShortIDs.
-
#short_id(txid) ⇒ Integer
calculate short transaction id which specified by BIP-152.
- #to_payload ⇒ Object
Constructor Details
#initialize(header, nonce, short_ids = [], prefilled_txn = []) ⇒ HeaderAndShortIDs
Returns a new instance of HeaderAndShortIDs.
13 14 15 16 17 18 19 |
# File 'lib/tapyrus/message/header_and_short_ids.rb', line 13 def initialize(header, nonce, short_ids = [], prefilled_txn = []) @header = header @nonce = nonce @short_ids = short_ids @prefilled_txn = prefilled_txn @siphash_key = Tapyrus.sha256(header.to_payload << [nonce].pack("q*"))[0...16] end |
Instance Attribute Details
#header ⇒ Object
Returns the value of attribute header.
7 8 9 |
# File 'lib/tapyrus/message/header_and_short_ids.rb', line 7 def header @header end |
#nonce ⇒ Object
Returns the value of attribute nonce.
8 9 10 |
# File 'lib/tapyrus/message/header_and_short_ids.rb', line 8 def nonce @nonce end |
#prefilled_txn ⇒ Object
Returns the value of attribute prefilled_txn.
10 11 12 |
# File 'lib/tapyrus/message/header_and_short_ids.rb', line 10 def prefilled_txn @prefilled_txn end |
#short_ids ⇒ Object
Returns the value of attribute short_ids.
9 10 11 |
# File 'lib/tapyrus/message/header_and_short_ids.rb', line 9 def short_ids @short_ids end |
#siphash_key ⇒ Object
Returns the value of attribute siphash_key.
11 12 13 |
# File 'lib/tapyrus/message/header_and_short_ids.rb', line 11 def siphash_key @siphash_key end |
Class Method Details
.parse_from_payload(payload) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/tapyrus/message/header_and_short_ids.rb', line 21 def self.parse_from_payload(payload) buf = StringIO.new(payload) header = Tapyrus::BlockHeader.parse_from_payload(buf) nonce = buf.read(8).unpack("q*").first short_ids_len = Tapyrus.unpack_var_int_from_io(buf) short_ids = short_ids_len.times.map { buf.read(6).reverse.bth.to_i(16) } prefilled_txn_len = Tapyrus.unpack_var_int_from_io(buf) prefilled_txn = prefilled_txn_len.times.map { PrefilledTx.parse_from_io(buf) } self.new(header, nonce, short_ids, prefilled_txn) end |
Instance Method Details
#short_id(txid) ⇒ Integer
calculate short transaction id which specified by BIP-152.
45 46 47 48 |
# File 'lib/tapyrus/message/header_and_short_ids.rb', line 45 def short_id(txid) hash = SipHash.digest(siphash_key, txid.htb.reverse).to_even_length_hex [hash].pack("H*")[2...8].bth.to_i(16) end |
#to_payload ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/tapyrus/message/header_and_short_ids.rb', line 32 def to_payload p = header.to_payload p << [nonce].pack("q*") p << Tapyrus.pack_var_int(short_ids.size) p << short_ids.map { |id| sprintf("%12x", id).htb.reverse }.join p << Tapyrus.pack_var_int(prefilled_txn.size) p << prefilled_txn.map(&:to_payload).join p end |