Class: Sibit::Bitcoin::Script

Inherits:
Object
  • Object
show all
Defined in:
lib/sibit/bitcoin/script.rb

Overview

Bitcoin Script parser.

Parses standard P2PKH scripts to extract addresses.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2019-2025 Yegor Bugayenko

License

MIT

Constant Summary collapse

OP_DUP =
0x76
OP_HASH160 =
0xa9
OP_EQUALVERIFY =
0x88
OP_CHECKSIG =
0xac

Instance Method Summary collapse

Constructor Details

#initialize(hex) ⇒ Script

Returns a new instance of Script.



24
25
26
# File 'lib/sibit/bitcoin/script.rb', line 24

def initialize(hex)
  @bytes = [hex].pack('H*').bytes
end

Instance Method Details

#addressObject



28
29
30
31
# File 'lib/sibit/bitcoin/script.rb', line 28

def address
  return p2pkh_address if p2pkh?
  nil
end

#hash160Object



42
43
44
45
# File 'lib/sibit/bitcoin/script.rb', line 42

def hash160
  return nil unless p2pkh?
  @bytes[3, 20].pack('C*').unpack1('H*')
end

#p2pkh?Boolean

Returns:



33
34
35
36
37
38
39
40
# File 'lib/sibit/bitcoin/script.rb', line 33

def p2pkh?
  @bytes.length == 25 &&
    @bytes[0] == OP_DUP &&
    @bytes[1] == OP_HASH160 &&
    @bytes[2] == 20 &&
    @bytes[23] == OP_EQUALVERIFY &&
    @bytes[24] == OP_CHECKSIG
end