Class: Sibit::TxBuilder
- Inherits:
-
Object
- Object
- Sibit::TxBuilder
- Defined in:
- lib/sibit/txbuilder.rb
Overview
Bitcoin Transaction Builder.
Provides a similar interface to Bitcoin::Builder::TxBuilder for building and signing Bitcoin transactions.
- Author
Yegor Bugayenko (yegor256@gmail.com)
- Copyright
Copyright (c) 2019-2026 Yegor Bugayenko
- License
MIT
Defined Under Namespace
Constant Summary collapse
- DUST =
546
Instance Method Summary collapse
-
#initialize(network = :mainnet) ⇒ TxBuilder
constructor
A new instance of TxBuilder.
- #input {|inp| ... } ⇒ Object
- #output(value, address) ⇒ Object
- #tx(input_value:, leave_fee:, extra_fee:, change_address:) ⇒ Object
Constructor Details
#initialize(network = :mainnet) ⇒ TxBuilder
Returns a new instance of TxBuilder.
23 24 25 26 27 |
# File 'lib/sibit/txbuilder.rb', line 23 def initialize(network = :mainnet) @inputs = [] @outputs = [] @network = network end |
Instance Method Details
#input {|inp| ... } ⇒ Object
29 30 31 32 33 |
# File 'lib/sibit/txbuilder.rb', line 29 def input inp = Input.new yield(inp) @inputs << inp end |
#output(value, address) ⇒ Object
35 36 37 |
# File 'lib/sibit/txbuilder.rb', line 35 def output(value, address) @outputs << { value: value, address: address } end |
#tx(input_value:, leave_fee:, extra_fee:, change_address:) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/sibit/txbuilder.rb', line 39 def tx(input_value:, leave_fee:, extra_fee:, change_address:) txn = Tx.new(network: @network) @inputs.each do |inp| txn.add_input( hash: inp.prev_out_hash, index: inp.prev_out_idx, script: inp.script, key: inp.key, value: inp.amount ) end total = @outputs.sum { |o| o[:value] } @outputs.each { |o| txn.add_output(o[:value], o[:address]) } if leave_fee change = input_value - total - extra_fee if change.negative? raise( Sibit::Error, "The inputs of #{input_value} cannot cover outputs of #{total} plus fee of #{extra_fee}" ) end if change.positive? && change < DUST raise(Sibit::Error, "The change of #{change} is below the dust limit of #{DUST}") end txn.add_output(change, change_address) if change.positive? end Built.new(txn, @inputs, @outputs) end |