Class: Bitcoin::Message::Base
- Inherits:
-
Object
- Object
- Bitcoin::Message::Base
- Extended by:
- Util
- Includes:
- HexConverter, Util
- Defined in:
- lib/bitcoin/message/base.rb
Overview
Base message class
Direct Known Subclasses
Addr, AddrV2, Block, BlockTxn, CFCheckpt, CFHeaders, CFilter, CmpctBlock, FeeFilter, FilterAdd, FilterClear, FilterLoad, GetAddr, GetBlockTxn, GetBlocks, GetCFCheckpt, GetCFHeaders, GetCFilters, GetData, GetHeaders, Headers, Inv, MemPool, MerkleBlock, NotFound, Ping, Pong, Reject, SendAddrV2, SendCmpct, SendHeaders, Tx, VerAck, Version
Class Method Summary collapse
-
.from_pkt(message) ⇒ Bitcoin::Message::XXX
Decode message data to message object.
Instance Method Summary collapse
-
#to_payload ⇒ Object
abstract method.
-
#to_pkt ⇒ Object
generate message header (binary format) bitcoin.org/en/developer-reference#message-headers.
Methods included from Util
byte_to_bit, calc_checksum, decode_base58_address, double_sha256, encode_base58_address, hash160, hkdf_sha256, hmac_sha256, pack_boolean, pack_var_int, pack_var_string, padding_zero, sha256, tagged_hash, unpack_boolean, unpack_var_int, unpack_var_int_from_io, unpack_var_string, valid_address?
Methods included from HexConverter
Class Method Details
.from_pkt(message) ⇒ Bitcoin::Message::XXX
Decode message data to message object.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bitcoin/message/base.rb', line 30 def self.from_pkt() buf = StringIO.new() magic = buf.read(4) raise ArgumentError, 'Invalid magic.' unless magic == Bitcoin.chain_params.magic_head.htb command = buf.read(12).delete("\x00") length = buf.read(4).unpack1('V') checksum = buf.read(4) payload = buf.read(length) raise ArgumentError, 'Checksum do not match.' unless checksum == Bitcoin.double_sha256(payload)[0...4] Bitcoin::Message.decode(command, payload&.bth) end |
Instance Method Details
#to_payload ⇒ Object
abstract method
22 23 24 |
# File 'lib/bitcoin/message/base.rb', line 22 def to_payload raise 'to_payload must be implemented in a child class.' end |
#to_pkt ⇒ Object
generate message header (binary format) bitcoin.org/en/developer-reference#message-headers
12 13 14 15 16 17 18 19 |
# File 'lib/bitcoin/message/base.rb', line 12 def to_pkt payload = to_payload magic = Bitcoin.chain_params.magic_head.htb command_name = self.class.const_get(:COMMAND, false).ljust(12, "\x00") payload_size = [payload.bytesize].pack('V') checksum = Bitcoin.double_sha256(payload)[0...4] magic << command_name << payload_size << checksum << payload end |