Class: String
Instance Method Summary collapse
-
#bth ⇒ Object
binary convert to hex string.
-
#bti ⇒ Object
binary convert to integer.
-
#htb ⇒ Object
hex string convert to binary.
-
#opcode ⇒ Object
get opcode.
- #opcode? ⇒ Boolean
- #push_opcode? ⇒ Boolean
-
#pushdata? ⇒ Boolean
whether data push only?.
- #pushed_data ⇒ Object
-
#rhex ⇒ Object
reverse hex string endian.
-
#valid_hex? ⇒ Boolean
whether value is hex or not hex.
Instance Method Details
#bth ⇒ Object
binary convert to hex string
107 108 109 |
# File 'lib/bitcoin.rb', line 107 def bth unpack('H*').first end |
#bti ⇒ Object
binary convert to integer
117 118 119 |
# File 'lib/bitcoin.rb', line 117 def bti bth.to_i(16) end |
#htb ⇒ Object
hex string convert to binary
112 113 114 |
# File 'lib/bitcoin.rb', line 112 def htb [self].pack('H*') end |
#opcode ⇒ Object
get opcode
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/bitcoin.rb', line 127 def opcode case encoding when Encoding::ASCII_8BIT each_byte.next when Encoding::US_ASCII ord else to_i end end |
#opcode? ⇒ Boolean
138 139 140 |
# File 'lib/bitcoin.rb', line 138 def opcode? !pushdata? end |
#push_opcode? ⇒ Boolean
142 143 144 |
# File 'lib/bitcoin.rb', line 142 def push_opcode? [Bitcoin::Opcodes::OP_PUSHDATA1, Bitcoin::Opcodes::OP_PUSHDATA2, Bitcoin::Opcodes::OP_PUSHDATA4].include?(opcode) end |
#pushdata? ⇒ Boolean
whether data push only?
147 148 149 |
# File 'lib/bitcoin.rb', line 147 def pushdata? opcode <= Bitcoin::Opcodes::OP_PUSHDATA4 && opcode > Bitcoin::Opcodes::OP_0 end |
#pushed_data ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/bitcoin.rb', line 151 def pushed_data return nil unless pushdata? offset = 1 case opcode when Bitcoin::Opcodes::OP_PUSHDATA1 offset += 1 when Bitcoin::Opcodes::OP_PUSHDATA2 offset += 2 when Bitcoin::Opcodes::OP_PUSHDATA4 offset += 4 end self[offset..-1] end |
#rhex ⇒ Object
reverse hex string endian
122 123 124 |
# File 'lib/bitcoin.rb', line 122 def rhex htb.reverse.bth end |
#valid_hex? ⇒ Boolean
whether value is hex or not hex
167 168 169 |
# File 'lib/bitcoin.rb', line 167 def valid_hex? !self[/\H/] end |