lib/bitcoin/builder.rb
# Create the transaction according to values specified via DSL.
# Sign each input that has a signature key specified. If there is
# no key, store the sig_hash in the input, so it can easily be
# signed later.
#
# When :change_address and :input_value options are given, it will
# automatically create a change output sending the remaining funds
# to the given address. The :leave_fee option can be used in this
# case to specify a tx fee that should be left unclaimed by the
# change output.
# rubocop:disable CyclomaticComplexity,PerceivedComplexity
Create the transaction according to values specified via DSL.
Sign each input that has a signature key specified. If there is
no key, store the sig_hash in the input, so it can easily be
signed later.

When :change_address and :input_value options are given, it will
automatically create a change output sending the remaining funds
to the given address. The :leave_fee option can be used in this
case to specify a tx fee that should be left unclaimed by the
change output.
Create the transaction according to values specified via DSL . Sign each input that has a signature key specified . If there is no key store the sig_hash in the input so it can easily be signed later . When : change_address and : input_value options are given it will automatically create a change output sending the remaining funds to the given address . The : leave_fee option can be used in this case to specify a tx fee that should be left unclaimed by the change output .
def tx(opts = {})
        return @tx  if @tx.hash
        if opts[:change_address] && !opts[:input_value]
          raise "Must give 'input_value' when auto-generating change output!"
        end
        @ins.each { |i| @tx.add_in(i.txin) }
        @outs.each { |o| @tx.add_out(o.txout) }
        if opts[:change_address]
          output_value = @tx.out.map(&:value).inject(:+) || 0
          change_value = opts[:input_value] - output_value
          if opts[:leave_fee]
            fee = @tx.minimum_block_fee + (opts[:extra_fee] || 0)
            if change_value >= fee
              change_value -= fee
            else
              change_value = 0
            end
          end
          if change_value > 0
            script = Script.to_address_script(opts[:change_address])
            @tx.add_out(P::TxOut.new(change_value, script))
          end
        end
        @ins.each_with_index do |inc, i|
          sign_input(i, inc)
        end
        raise 'Payload Error' unless P::Tx.new(@tx.to_witness_payload).to_payload == @tx.to_payload
        @tx.instance_eval do
          @payload = to_payload
          @hash = hash_from_payload(@payload)
        end
        @tx
      end
def tx ( opts = { } ) return @tx if @tx . hash if opts [ :change_address ] && ! opts [ :input_value ] raise "Must give 'input_value' when auto-generating change output!" end @ins . each { | i | @tx . add_in ( i . txin ) } @outs . each { | o | @tx . add_out ( o . txout ) } if opts [ :change_address ] output_value = @tx . out . map ( & :value ) . inject ( :+ ) || 0 change_value = opts [ :input_value ] - output_value if opts [ :leave_fee ] fee = @tx . minimum_block_fee + ( opts [ :extra_fee ] || 0 ) if change_value >= fee change_value -= fee else change_value = 0 end end if change_value > 0 script = Script . to_address_script ( opts [ :change_address ] ) @tx . add_out ( P :: TxOut . new ( change_value , script ) ) end end @ins . each_with_index do | inc , i | sign_input ( i , inc ) end raise 'Payload Error' unless P :: Tx . new ( @tx . to_witness_payload ) . to_payload == @tx . to_payload @tx . instance_eval do @payload = to_payload @hash = hash_from_payload ( @payload ) end @tx end
Create the transaction according to values specified via DSL.
Create the transaction according to values specified via DSL .
# run our tx through an encode/decode cycle to make sure that the binary format is sane
opts
<not_specific>