lib/bitcoin/builder.rb in bitcoin-ruby-0.0.7 vs lib/bitcoin/builder.rb in bitcoin-ruby-0.0.8
- old
+ new
@@ -12,20 +12,18 @@
def build_block(target = "00".ljust(64, 'f'))
c = BlockBuilder.new
yield c
c.block(target)
end
- alias :blk :build_block
# build a Bitcoin::Protocol::Tx.
# see TxBuilder for details.
def build_tx opts = {}
c = TxBuilder.new
yield c
c.tx opts
end
- alias :tx :build_tx
# build a Bitcoin::Script.
# see ScriptBuilder for details.
def script
c = ScriptBuilder.new
@@ -93,11 +91,11 @@
# increment nonce/time to find a block hash matching the +target+.
def find_hash target
@block.bits = Bitcoin.encode_compact_bits(target)
t = Time.now
@block.recalc_block_hash
- until @block.hash < target
+ until @block.hash.to_i(16) < target.to_i(16)
@block.nonce += 1
@block.recalc_block_hash
if @block.nonce == 100000
if t
tt = 1 / ((Time.now - t) / 100000) / 1000
@@ -154,13 +152,15 @@
yield c
@ins << c
end
# add an output to the transaction (see TxOutBuilder).
- def output
+ def output value = nil, recipient = nil, type = :address
c = TxOutBuilder.new
- yield c
+ c.value(value) if value
+ c.to(recipient, type) if recipient
+ yield c if block_given?
@outs << c
end
# Create the transaction according to values specified via DSL.
# Sign each input that has a signature key specified. If there is
@@ -171,22 +171,24 @@
# 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(:+)
+ output_value = @tx.out.map(&:value).inject(:+) || 0
change_value = opts[:input_value] - output_value
if opts[:leave_fee]
- if change_value >= @tx.minimum_block_fee
- change_value -= @tx.minimum_block_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
@@ -217,11 +219,11 @@
end
def sig_hash_and_all_keys_exist?(inc, sig_script)
return false unless @sig_hash && inc.has_keys?
script = Bitcoin::Script.new(sig_script)
- return true if script.is_hash160? || script.is_pubkey?
+ return true if script.is_hash160? || script.is_pubkey? || (Bitcoin.namecoin? && script.is_namecoin?)
if script.is_multisig?
return inc.has_multiple_keys? && inc.key.size >= script.get_signatures_required
end
raise "Script type must be hash160, pubkey or multisig"
end
@@ -423,9 +425,12 @@
@script, @redeem_script = *Script.send("to_#{@type}_script", *data)
end
end
# Create a Bitcoin::Protocol::TxOut used by TxBuilder#output.
+ #
+ # t.output 12345, address
+ # t.output 12345, p2sh_address, :script_hash
#
# t.output {|o| o.value 12345; o.to address }
#
# t.output do |o|
# o.value 12345