lib/bitcoin/wallet/txdp.rb in bitcoin-ruby-0.0.1 vs lib/bitcoin/wallet/txdp.rb in bitcoin-ruby-0.0.2
- old
+ new
@@ -1,15 +1,17 @@
+# encoding: ascii-8bit
+
class Bitcoin::Wallet::TxDP
attr_accessor :id, :tx, :inputs
def initialize tx = []
@id = Bitcoin.int_to_base58(rand(1e14))
@tx = tx
@inputs = []
return unless tx.any?
@tx[0].in.each_with_index do |input, i|
- prev_out_hash = input.prev_out.reverse.unpack("H*")[0]
+ prev_out_hash = input.prev_out.reverse_hth
prev_tx = @tx[1..-1].find {|tx| tx.hash == prev_out_hash}
raise "prev tx #{prev_out_hash} not found" unless prev_tx
prev_out = prev_tx.out[input.prev_out_index]
raise "prev out ##{input.prev_out_index} not found in tx #{@tx.hash}" unless prev_out
out_script = Bitcoin::Script.new(prev_out.pk_script)
@@ -25,11 +27,11 @@
end
def sign_inputs
@inputs.each_with_index do |txin, i|
input = @tx[0].in[i]
- prev_out_hash = input.prev_out.reverse.unpack("H*")[0]
+ prev_out_hash = input.prev_out.reverse_hth
prev_tx = @tx[1..-1].find {|tx| tx.hash == prev_out_hash}
raise "prev tx #{prev_out_hash} not found" unless prev_tx
prev_out = prev_tx.out[input.prev_out_index]
raise "prev out ##{input.prev_out_index} not found in tx #{@tx.hash}" unless prev_out
out_script = Bitcoin::Script.new(prev_out.pk_script)
@@ -45,20 +47,20 @@
end
def serialize
lines = []
lines << "-----BEGIN-TRANSACTION-#{@id}".ljust(80, '-')
- size = [@tx.first.to_payload.bytesize].pack("C").ljust(2, "\x00").reverse.unpack("H*")[0]
+ size = [@tx.first.to_payload.bytesize].pack("C").ljust(2, "\x00").reverse_hth
lines << "_TXDIST_#{Bitcoin.network[:magic_head].unpack("H*")[0]}_#{@id}_#{size}"
tx = @tx.map(&:to_payload).join.unpack("H*")[0]
tx_str = ""; tx.split('').each_with_index{|c,i| tx_str << (i % 80 == 0 ? "\n#{c}" : c)}
lines << tx_str.strip
@inputs.each_with_index do |input, idx|
lines << "_TXINPUT_#{idx.to_s.rjust(2, '0')}_#{"%.8f" % (input[0].to_f / 1e8)}"
next unless input[1]
input[1].each do |sig|
size = [sig[1]].pack("H*").bytesize
- size = [size].pack("C").ljust(2, "\x00").reverse.unpack("H*")[0]
+ size = [size].pack("C").ljust(2, "\x00").reverse_hth
lines << "_SIG_#{sig[0]}_#{idx.to_s.rjust(2, '0')}_#{size}"
sig_str = ""; sig[1].split('').each_with_index{|c,i| sig_str << (i % 80 == 0 ? "\n#{c}" : c)}
lines << sig_str.strip
end
end