Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/bitcoin.rb
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.
- #valid_pushdata_length? ⇒ Boolean
Instance Method Details
#bth ⇒ Object
binary convert to hex string
124 125 126 |
# File 'lib/bitcoin.rb', line 124 def bth unpack1('H*') end |
#bti ⇒ Object
binary convert to integer
134 135 136 |
# File 'lib/bitcoin.rb', line 134 def bti bth.to_i(16) end |
#htb ⇒ Object
hex string convert to binary
129 130 131 |
# File 'lib/bitcoin.rb', line 129 def htb [self].pack('H*') end |
#opcode ⇒ Object
get opcode
144 145 146 |
# File 'lib/bitcoin.rb', line 144 def opcode force_encoding(Encoding::ASCII_8BIT).ord end |
#opcode? ⇒ Boolean
148 149 150 |
# File 'lib/bitcoin.rb', line 148 def opcode? !pushdata? end |
#push_opcode? ⇒ Boolean
152 153 154 |
# File 'lib/bitcoin.rb', line 152 def push_opcode? [Bitcoin::Opcodes::OP_PUSHDATA1, Bitcoin::Opcodes::OP_PUSHDATA2, Bitcoin::Opcodes::OP_PUSHDATA4].include?(opcode) end |
#pushdata? ⇒ Boolean
whether data push only?
157 158 159 |
# File 'lib/bitcoin.rb', line 157 def pushdata? opcode <= Bitcoin::Opcodes::OP_PUSHDATA4 && opcode > Bitcoin::Opcodes::OP_0 end |
#pushed_data ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/bitcoin.rb', line 161 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
139 140 141 |
# File 'lib/bitcoin.rb', line 139 def rhex htb.reverse.bth end |
#valid_hex? ⇒ Boolean
whether value is hex or not hex
198 199 200 |
# File 'lib/bitcoin.rb', line 198 def valid_hex? !self[/\H/] end |
#valid_pushdata_length? ⇒ Boolean
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/bitcoin.rb', line 175 def valid_pushdata_length? buf = StringIO.new(self) opcode = buf.read(1).ord offset = 1 return false if buf.eof? len = case opcode when Bitcoin::Opcodes::OP_PUSHDATA1 offset += 1 buf.read(1).unpack1('C') when Bitcoin::Opcodes::OP_PUSHDATA2 offset += 2 buf.read(2).unpack1('v') when Bitcoin::Opcodes::OP_PUSHDATA4 offset += 4 buf.read(4).unpack1('V') else opcode end self.bytesize == len + offset end |