Class: Sibit::Tx::Output

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

Overview

Transaction output.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright (c) 2019-2026 Yegor Bugayenko

License

MIT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, address, network = :mainnet) ⇒ Output

Returns a new instance of Output.



107
108
109
110
111
# File 'lib/sibit/tx.rb', line 107

def initialize(value, address, network = :mainnet)
  @value = value
  @address = address
  @network = network
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



105
106
107
# File 'lib/sibit/tx.rb', line 105

def value
  @value
end

Instance Method Details

#scriptObject

Raises:



113
114
115
116
117
118
119
120
121
122
# File 'lib/sibit/tx.rb', line 113

def script
  return segwit_script if segwit?
  return p2pkh_script if version == (@network == :mainnet ? '00' : '6f')
  return p2sh_script if version == (@network == :mainnet ? '05' : 'c4')
  raise(
    Sibit::Error,
    "Address '#{@address}' has version byte 0x#{version}, " \
    "which does not belong to the #{@network} network"
  )
end

#script_hexObject



136
137
138
# File 'lib/sibit/tx.rb', line 136

def script_hex
  script.unpack1('H*')
end

#segwit?Boolean

Returns:

  • (Boolean)


124
125
126
127
128
129
130
131
132
133
134
# File 'lib/sibit/tx.rb', line 124

def segwit?
  down = @address.downcase
  return true if down.start_with?(*(@network == :mainnet ? ['bc1'] : %w[tb1 bcrt1]))
  if down.start_with?(*(@network == :mainnet ? %w[tb1 bcrt1] : ['bc1']))
    raise(
      Sibit::Error,
      "Address '#{@address}' is a Bech32 address of a different network than #{@network}"
    )
  end
  false
end