Class: Sibit::Tx

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

Overview

Bitcoin Transaction structure.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright (c) 2019-2026 Yegor Bugayenko

License

MIT

Defined Under Namespace

Classes: Input, Output

Constant Summary collapse

SIGHASH_ALL =
0x01
VERSION =
1
SEQUENCE =
0xffffffff

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(network: :mainnet) ⇒ Tx

Returns a new instance of Tx.



27
28
29
30
31
# File 'lib/sibit/tx.rb', line 27

def initialize(network: :mainnet)
  @inputs = []
  @outputs = []
  @network = network
end

Instance Attribute Details

#inputsObject (readonly)

Returns the value of attribute inputs.



25
26
27
# File 'lib/sibit/tx.rb', line 25

def inputs
  @inputs
end

#outputsObject (readonly)

Returns the value of attribute outputs.



25
26
27
# File 'lib/sibit/tx.rb', line 25

def outputs
  @outputs
end

Instance Method Details

#add_input(hash:, index:, script:, key:, value: 0) ⇒ Object



33
34
35
# File 'lib/sibit/tx.rb', line 33

def add_input(hash:, index:, script:, key:, value: 0)
  @inputs << Input.new(hash, index, script, key, value)
end

#add_output(value, address) ⇒ Object



37
38
39
# File 'lib/sibit/tx.rb', line 37

def add_output(value, address)
  @outputs << Output.new(value, address, @network)
end

#hashObject



41
42
43
44
45
46
# File 'lib/sibit/tx.rb', line 41

def hash
  sign_inputs
  Digest::SHA256.hexdigest(
    Digest::SHA256.digest(serialize(witness: false))
  ).scan(/../).reverse.join
end

#hexObject



54
55
56
# File 'lib/sibit/tx.rb', line 54

def hex
  payload.unpack1('H*')
end

#inObject



58
59
60
# File 'lib/sibit/tx.rb', line 58

def in
  @inputs
end

#outObject



62
63
64
# File 'lib/sibit/tx.rb', line 62

def out
  @outputs
end

#payloadObject



48
49
50
51
52
# File 'lib/sibit/tx.rb', line 48

def payload
  return @payload if @payload
  sign_inputs
  @payload = serialize
end