Cannot create a raw transaction using bitcoin-ruby

Cannot create a raw transaction using bitcoin-ruby

Im using bitcoin-ruby to create a raw transaction then broadcast it through block-cypher. I follow their tutorial

# use testnet so you don't accidentally blow your whole money!
Bitcoin.network = :testnet3

# make the DSL methods available in your scope
include Bitcoin::Builder

# the previous transaction that has an output to your address
prev_hash = "6c44b284c20fa22bd69c57a9dbff91fb71deddc8c54fb2f5aa41fc78c96c1ad1"

# the number of the output you want to use
prev_out_index = 0

# fetch the tx from whereever you like and parse it
prev_tx = Bitcoin::P::Tx.from_json(open("http://ift.tt/2BZU9Ti"))

# the key needed to sign an input that spends the previous output
key = Bitcoin::Key.from_base58("92ZRu28m2GHSKaaF2W7RswJ2iJYpTzVhBaN6ZLs7TENCs4b7ML8")

# create a new transaction (and sign the inputs)
new_tx = build_tx do |t|

  # add the input you picked out earlier
  t.input do |i|
    i.prev_out prev_tx
    i.prev_out_index prev_out_index
    i.signature_key key
  end

  # add an output that sends some bitcoins to another address
  t.output do |o|
    o.value 50000000 # 0.5 BTC in satoshis
    o.script {|s| s.recipient "mugwYJ1sKyr8EDDgXtoh8sdDQuNWKYNf88" }
  end

  # add another output spending the remaining amount back to yourself
  # if you want to pay a tx fee, reduce the value of this output accordingly
  # if you want to keep your financial history private, use a different address
  t.output do |o|
    o.value 49000000 # 0.49 BTC, leave 0.01 BTC as fee
    o.script {|s| s.recipient key.addr }
  end

end

# examine your transaction. you can relay it through http://ift.tt/1KiU6To
# that will also give you a hint on the error if something goes wrong
puts new_tx.to_json

http://ift.tt/2q7EkIP 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'] http://ift.tt/2Ejrav9 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.

I get hex_transaction using

http://ift.tt/2Eo1rS7
Bitcoin::P::Tx.new('0100000000010146da16c6900cb47696feebc60860984399d1bb0c21e4be8912aa40a107ef0d450100000017160014c1d3f92a55e6506422c6550434615f2e34265ae7ffffffff0280a4bf07000000001976a91467120445f4ffdd1b9ca95b8dcd343611d65f6fb788ace2a2744c2c00000017a9144db63f2ce3412854d0718bbf0bc3e80e8a24d0de8702483045022100b6c18b7b3a3cf0689e806b2eb1dba6811d37fa152f461f60a3b1d9ec439c2aaa0220184465e4c16219f4582f48a2fcd0a214d8d56607896e86f86ffa07188807014c01210358ea03d324d68fe8164dff922a5502c297a7e91d6a97381c81d8ed121113084800000000')

I dont like hex_transaction solution then looking for a help for above issue, thanks you so much

http://ift.tt/2zS3v1h

Comments

Popular posts from this blog

bitcoin node: what is the difference between simnet and regtest?

How to check if Electrum is masking my IP with the Tor proxy?

Need help to recover blpckchain.info wallet, my wife forgot her password and the brute force with btcrecover is not catching the password