lian / bitcoin-ruby

bitcoin utils and protocol in ruby.
Other
923 stars 323 forks source link

Best source to get Tx information? #250

Closed juanfer closed 6 years ago

juanfer commented 6 years ago

I am trying to create a new LTC transaction. I tried several json sources to get previous transaction information, but as far as I could see, there isn't an standard json answer, and Tx parser is strongly dependent on json structure coming from third party sources. I could change some structure in the answer to match some of the structure I've seen it consumes, but I got stucked in lib/bitcoin/protocol/tx.rb, line 409:

@hash = hash_from_payload(to_old_payload)

This is the error:

TypeError in WalletController#apiBuyPhi

no implicit conversion of nil into Integer
Extracted source (around line #149):

#147         @out.each{|output| pout << output.to_payload }
#148 
*149         [@ver].pack("V") << Protocol.pack_var_int(@in.size) << pin << Protocol.pack_var_int(@out.size) << pout << [@lock_time].pack("V")
#150       end
#151 
#152       # output transaction in raw binary format with witness

Extracted source (around line #409):

#407         outs.each{|output|  tx.add_out TxOut.from_hash(output) }
#408         tx.instance_eval{
*409           @hash = hash_from_payload(to_old_payload)
#410           @payload = to_payload
#411         }
#412         if h['hash'] && (h['hash'] != tx.hash)

As you can see, I could parse Tx ins and outs, but something is broken in tx.instance_eval and can't understand what it is.

The last JSON source I've tried is: https://chainz.cryptoid.info/ltc/api.dws?q=txinfo&t=fb22e52bd27359d7639bcaa94d7b13700fc2634bb64edb64dda1ab3f4d3bfb9a

(current Tx I am working with). Any ideas? I think it's some problem with Tx JSON format itself, but can't see what it is. The way I've rewritten incoming JSON to match Tx parser is a bit rough: simply doing gsub on string before converting it to hash. Before doing this replacement I was stuck in lines 404 and 407 (ins.each and outs.each). Maybe there's a best source to get JSON correctly formed to be consumed by this gem?

lian commented 6 years ago

just get the raw hex data of the transactions and convert to binary, usually every api provides those. can easily run tx = Tx.new([hex_string].pack("H*")) then.

juanfer commented 6 years ago

Thanks very much, worked fine.