lian / bitcoin-ruby

bitcoin utils and protocol in ruby.
Other
922 stars 322 forks source link

create raw Transactions - cant parse prev_tx #234

Closed SFzxc closed 6 years ago

SFzxc commented 6 years ago

i followed this tutorial https://github.com/lian/bitcoin-ruby#create-transactions In step

prev_tx = Bitcoin::P::Tx.from_json(open("http://test.webbtc.com/tx/#{prev_hash}.json"))

http://test.webbtc.com/tx is no longer can use so i used blockcypher api or Blockchain.transaction(tx_id) instead.

Bitcoin.network = :testnet3
private_key = "598ba1001a580737b8f7672cb7a3e5ce3413eb508b92de3b155ca4e904423736"
public_key="0498bd9486a4459fd48e7b962293da11ce035fc492946e178974316fd4389b1170269d0032007b2f03a51ec4689699e635b9d89b704aa676c1e76d8746a0a291e5"
prev_hash = "0ed5e5794ed9378037e76b18a92b8e562fee61fb3cbb538e42c7def1f605e442"

i fetched prev_tx infor

tx_hash = blockcypher.blockchain_transaction('0ed5e5794ed9378037e76b18a92b8e562fee61fb3cbb538e42c7def1f605e442')
{"block_hash"=>"00000000000000d180f215781514a616a1195236ddf748be01d39841bf2758ef",
 "block_height"=>1256842,
 "block_index"=>28,
 "hash"=>"0ed5e5794ed9378037e76b18a92b8e562fee61fb3cbb538e42c7def1f605e442",
 "addresses"=>
  ["2Mxs76uEDiWGLcQ6AuvorjFAJmwGd4H9FHZ", "2MzL8LnNW1dwuPP7E4QsFFfJM1WNXB5hjht", "mpuwUdk1gys4fyqZVrA2VxaCUjikKEvudi"],
 "total"=>190391273314,
 "fees"=>100000,
 "size"=>140,
 "preference"=>"high",
 "relayed_by"=>"81.169.237.132:18333",
 "confirmed"=>"2017-12-31T01:35:43Z",
 "received"=>"2017-12-31T01:33:36.188Z",
 "ver"=>1,
 "double_spend"=>false,
 "vin_sz"=>1,
 "vout_sz"=>2,
 "confirmations"=>3,
 "confidence"=>1,
 "inputs"=>
  [{"prev_hash"=>"450def07a140aa1289bee4210cbbd19943986008c6ebfe9676b40c90c616da46",
    "output_index"=>1,
    "script"=>"160014c1d3f92a55e6506422c6550434615f2e34265ae7",
    "output_value"=>190391373314,
    "sequence"=>4294967295,
    "addresses"=>["2Mxs76uEDiWGLcQ6AuvorjFAJmwGd4H9FHZ"],
    "script_type"=>"pay-to-script-hash",
    "age"=>1256830,
    "witness"=>
     ["3045022100b6c18b7b3a3cf0689e806b2eb1dba6811d37fa152f461f60a3b1d9ec439c2aaa0220184465e4c16219f4582f48a2fcd0a214d8d56607896e86f86ffa07188807014c01",
      "0358ea03d324d68fe8164dff922a5502c297a7e91d6a97381c81d8ed1211130848"]}],
 "outputs"=>
  [{"value"=>130000000,
    "script"=>"76a91467120445f4ffdd1b9ca95b8dcd343611d65f6fb788ac",
    "addresses"=>["mpuwUdk1gys4fyqZVrA2VxaCUjikKEvudi"],
    "script_type"=>"pay-to-pubkey-hash"},
   {"value"=>190261273314,
    "script"=>"a9144db63f2ce3412854d0718bbf0bc3e80e8a24d0de87",
    "addresses"=>["2MzL8LnNW1dwuPP7E4QsFFfJM1WNXB5hjht"],
    "script_type"=>"pay-to-script-hash"}]}

then i parsed:

Bitcoin::P::Tx.from_hash(tx_hash)

but because a above hash dont have any keys such as ['previous_transaction_hash'] or ['prev_out'] https://github.com/lian/bitcoin-ruby/blob/master/lib/bitcoin/protocol/txin.rb#L99 then i got error

NoMethodError: undefined method `[]' for nil:NilClass
bitcoin/protocol/txin.rb:99:in `from_hash'

Do we have any solutions for this problems.

I also tried import from hex transaction string but sometime this way is not work should i do like this way.

  1. I get hex_transaction using https://testnet.blockchain.info/tx/0ed5e5794ed9378037e76b18a92b8e562fee61fb3cbb538e42c7def1f605e442?format=hex
  2. Bitcoin::P::Tx.new('0100000000010146da16c6900cb47696feebc60860984399d1bb0c21e4be8912aa40a107ef0d450100000017160014c1d3f92a55e6506422c6550434615f2e34265ae7ffffffff0280a4bf07000000001976a91467120445f4ffdd1b9ca95b8dcd343611d65f6fb788ace2a2744c2c00000017a9144db63f2ce3412854d0718bbf0bc3e80e8a24d0de8702483045022100b6c18b7b3a3cf0689e806b2eb1dba6811d37fa152f461f60a3b1d9ec439c2aaa0220184465e4c16219f4582f48a2fcd0a214d8d56607896e86f86ffa07188807014c01210358ea03d324d68fe8164dff922a5502c297a7e91d6a97381c81d8ed121113084800000000') I dont like hex_transaction solution then looking for a help for above issue, thanks you so much
SFzxc commented 6 years ago

@azuchi -san, Looking forward to your help, please forgive the tag

azuchi commented 6 years ago

Bitcoin::P::Tx.newaccepts only raw binary. So you should write

Bitcoin::P::Tx.new('0100000000010146da16c6900cb47696feebc60860984399d1bb0c21e4be8912aa40a107ef0d450100000017160014c1d3f92a55e6506422c6550434615f2e34265ae7ffffffff0280a4bf07000000001976a91467120445f4ffdd1b9ca95b8dcd343611d65f6fb788ace2a2744c2c00000017a9144db63f2ce3412854d0718bbf0bc3e80e8a24d0de8702483045022100b6c18b7b3a3cf0689e806b2eb1dba6811d37fa152f461f60a3b1d9ec439c2aaa0220184465e4c16219f4582f48a2fcd0a214d8d56607896e86f86ffa07188807014c01210358ea03d324d68fe8164dff922a5502c297a7e91d6a97381c81d8ed121113084800000000'.htb)
SFzxc commented 6 years ago

thanks @azuchi -san, but is there anyway to let me keep import from hash by Bitcoin::P::Tx.from_hash(tx_hash) ?!