class String

Public Instance Methods

bth() click to toggle source

binary convert to hex string

# File lib/bitcoin.rb, line 117
def bth
  unpack1('H*')
end
bti() click to toggle source

binary convert to integer

# File lib/bitcoin.rb, line 127
def bti
  bth.to_i(16)
end
htb() click to toggle source

hex string convert to binary

# File lib/bitcoin.rb, line 122
def htb
  [self].pack('H*')
end
opcode() click to toggle source

get opcode

# File lib/bitcoin.rb, line 137
def opcode
  force_encoding(Encoding::ASCII_8BIT).ord
end
opcode?() click to toggle source
# File lib/bitcoin.rb, line 141
def opcode?
  !pushdata?
end
push_opcode?() click to toggle source
# File lib/bitcoin.rb, line 145
def push_opcode?
  [Bitcoin::Opcodes::OP_PUSHDATA1, Bitcoin::Opcodes::OP_PUSHDATA2, Bitcoin::Opcodes::OP_PUSHDATA4].include?(opcode)
end
pushdata?() click to toggle source

whether data push only?

# File lib/bitcoin.rb, line 150
def pushdata?
  opcode <= Bitcoin::Opcodes::OP_PUSHDATA4 && opcode > Bitcoin::Opcodes::OP_0
end
pushed_data() click to toggle source
# File lib/bitcoin.rb, line 154
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() click to toggle source

reverse hex string endian

# File lib/bitcoin.rb, line 132
def rhex
  htb.reverse.bth
end
valid_hex?() click to toggle source

whether value is hex or not hex @return [Boolean] return true if data is hex

# File lib/bitcoin.rb, line 191
def valid_hex?
  !self[/\H/]
end
valid_pushdata_length?() click to toggle source
# File lib/bitcoin.rb, line 168
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