-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcfilter.rb
35 lines (28 loc) · 906 Bytes
/
cfilter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Bitcoin
module Message
# cfilter message for BIP-157
# https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki#cfilter
class CFilter < Base
COMMAND = 'cfilter'
attr_accessor :filter_type
attr_accessor :block_hash # little endian
attr_accessor :filter # little endian
def initialize(filter_type, block_hash, filter)
@filter_type = filter_type
@block_hash = block_hash
@filter = filter
end
def self.parse_from_payload(payload)
buf = StringIO.new(payload)
type = buf.read(1).unpack1("C")
hash = buf.read(32).bth
len = Bitcoin.unpack_var_int_from_io(buf)
filter = buf.read(len).bth
self.new(type, hash, filter)
end
def to_payload
[filter_type, block_hash].pack('CH*') + Bitcoin.pack_var_string(filter.htb)
end
end
end
end