Class: String

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

Instance Method Summary collapse

Instance Method Details

#bthObject

binary convert to hex string



118
119
120
# File 'lib/bitcoin.rb', line 118

def bth
  unpack1('H*')
end

#btiObject

binary convert to integer



128
129
130
# File 'lib/bitcoin.rb', line 128

def bti
  bth.to_i(16)
end

#htbObject

hex string convert to binary



123
124
125
# File 'lib/bitcoin.rb', line 123

def htb
  [self].pack('H*')
end

#opcodeObject

get opcode



138
139
140
# File 'lib/bitcoin.rb', line 138

def opcode
  force_encoding(Encoding::ASCII_8BIT).ord
end

#opcode?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/bitcoin.rb', line 142

def opcode?
  !pushdata?
end

#push_opcode?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/bitcoin.rb', line 146

def push_opcode?
  [Bitcoin::Opcodes::OP_PUSHDATA1, Bitcoin::Opcodes::OP_PUSHDATA2, Bitcoin::Opcodes::OP_PUSHDATA4].include?(opcode)
end

#pushdata?Boolean

whether data push only?

Returns:

  • (Boolean)


151
152
153
# File 'lib/bitcoin.rb', line 151

def pushdata?
  opcode <= Bitcoin::Opcodes::OP_PUSHDATA4 && opcode > Bitcoin::Opcodes::OP_0
end

#pushed_dataObject



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/bitcoin.rb', line 155

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

#rhexObject

reverse hex string endian



133
134
135
# File 'lib/bitcoin.rb', line 133

def rhex
  htb.reverse.bth
end

#valid_hex?Boolean

whether value is hex or not hex

Returns:

  • (Boolean)

    return true if data is hex



192
193
194
# File 'lib/bitcoin.rb', line 192

def valid_hex?
  !self[/\H/]
end

#valid_pushdata_length?Boolean

Returns:

  • (Boolean)


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/bitcoin.rb', line 169

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