Closed selvaraman closed 6 years ago
just fetch the binary or hex version of said tx and feed it to Bitcoin::Protocol::Tx.new(binary) instead. only the webbtc json format works. ist best to ignore it and use binary/hex instead. also please don't spam OP_RETURN data in the blockchain ;) and test it in testnet first
Thanks @lian While using hex version of tx, I am getting empty outputs.
hex_tx = '01000000013542a7313111c814f35bea27ebf38bc9155ef1ad574ca443911a0e5792207049040000006b483045022100aedc2702a79acfc31b9874d331d4314b259e0dc7bc65e604631dabde764e19800220296c93e9250c5ec9348fdaa0a3f256ad10eedd08f01f9923599ba2072e147dbe0121035ef1c06deb3d74fc0f177da633fd18dc2cd8bb01341dde36212309252ccdbbe0feffffff1bd7220100000000001976a914b96894bdb2c791eb54e7905a04fd3544675ff65388ac13d91a000000000017a914fec5020d284b9d1e79bef155c55938728cfdf5d5873faa1e000000000017a914b910aa63eed5f8565b5f64b5bf541a3cbe16b5aa8772ed2c00000000001976a914c83b906199bf4bca25867bdd253918beae801fd988ac36921600000000001976a914eddda481a141396f4beae20a0d4ce1569b39b36888ac705510000000000017a914a55bd1266269cc355efdedcb73e6bae2450fc5cf8720f17400000000001976a9146e1de35ffa8ed3207668ef25814d63901fe36d9088ac204e0000000000001976a9149b0552a7ee0a46d0dd63563d705ed7c99248607588ac6ebc0000000000001976a9145b15d20b730bc7b8bac1c5751185a0ddc6a5e84b88ac9c820400000000001976a9146483d0c22793b02ca913b9d4cdd77b6fd11beef188acd8ba0d00000000001976a914aeb396e8c15fe8ccd98a48a95efb1711f928667988ac53ab8101000000001976a9144e4e0589e9e2e5cbbd08b8735dd86023cdb7776388acc0a90800000000001976a914c501696212c69a539661eec7a9be110b0e95bf3688acd60c1700000000001976a91400d1c4c86e07a1ca51d22fc2c5c9be7bc18de66388ac175e0100000000001976a914bcdc38ca84908728fb517dcfc39ddf255c98ab7e88ac000215000000000017a9149c2ae176ad505275fa9b0dc4dcaa8199c96143ea87b9130100000000001976a91464f5e8707053510e7edef90b187a511031ee51e288ace5104000000000001976a91428943bc0aec7c9f96c4199c46a071cf9eca2739588ac0c8b08000000000017a9149bf1c3e8e8cacf62381be7abd6e81066351147e987679f0900000000001976a914dc2814782770cd9b55f20ef71c9ac7b2248d613f88ac08870c00000000001976a9144f8ced0dd17f045dc38789a75f341b6c2d211c5988ac0b2a0300000000001976a914054d3d29d910576e023a435009dc85fb3e80a63a88ac25bf0d00000000001976a914de03193bfc08d90b5541dda254ee4972e7f740e488ac376b70000000000017a914c6707ca3ca1bb3693fcd198560d7e4070aa1b36f8764b61701000000001976a914572f90b0087004e504a723be4c1082d91b56d91c88acc02f5501000000001976a914b0271c737db4f88864ef7d529f1ea6862b31eb0a88acbbc7d501000000001976a914e1a10f15699e404a449c4c906e0fd368d11f923788ac8bb70700'
prev_tx = Bitcoin::Protocol::Tx.new(hex_tx)
prev_out_index = 0
prev_tx.outputs
=> []
What is wrong here?
convert hex to binary ['0100...'].pack("H*")
Thanks @lian It's working. Can you please help me to resolve this problem. I am getting Transaction not found for my tx hash 05890e185b40d94586081af4236ab27b412dd19ad26fbe3c2380d61f336a722a
prev_hash = "655e480ea5b86158ed322fee71309a562718ec671126842612b7da66199590ec"
hex_tx = "01000000013542a7313111c814f35bea27ebf38bc9155ef1ad574ca443911a0e5792207049040000006b483045022100aedc2702a79acfc31b9874d331d4314b259e0dc7bc65e604631dabde764e19800220296c93e9250c5ec9348fdaa0a3f256ad10eedd08f01f9923599ba2072e147dbe0121035ef1c06deb3d74fc0f177da633fd18dc2cd8bb01341dde36212309252ccdbbe0feffffff1bd7220100000000001976a914b96894bdb2c791eb54e7905a04fd3544675ff65388ac13d91a000000000017a914fec5020d284b9d1e79bef155c55938728cfdf5d5873faa1e000000000017a914b910aa63eed5f8565b5f64b5bf541a3cbe16b5aa8772ed2c00000000001976a914c83b906199bf4bca25867bdd253918beae801fd988ac36921600000000001976a914eddda481a141396f4beae20a0d4ce1569b39b36888ac705510000000000017a914a55bd1266269cc355efdedcb73e6bae2450fc5cf8720f17400000000001976a9146e1de35ffa8ed3207668ef25814d63901fe36d9088ac204e0000000000001976a9149b0552a7ee0a46d0dd63563d705ed7c99248607588ac6ebc0000000000001976a9145b15d20b730bc7b8bac1c5751185a0ddc6a5e84b88ac9c820400000000001976a9146483d0c22793b02ca913b9d4cdd77b6fd11beef188acd8ba0d00000000001976a914aeb396e8c15fe8ccd98a48a95efb1711f928667988ac53ab8101000000001976a9144e4e0589e9e2e5cbbd08b8735dd86023cdb7776388acc0a90800000000001976a914c501696212c69a539661eec7a9be110b0e95bf3688acd60c1700000000001976a91400d1c4c86e07a1ca51d22fc2c5c9be7bc18de66388ac175e0100000000001976a914bcdc38ca84908728fb517dcfc39ddf255c98ab7e88ac000215000000000017a9149c2ae176ad505275fa9b0dc4dcaa8199c96143ea87b9130100000000001976a91464f5e8707053510e7edef90b187a511031ee51e288ace5104000000000001976a91428943bc0aec7c9f96c4199c46a071cf9eca2739588ac0c8b08000000000017a9149bf1c3e8e8cacf62381be7abd6e81066351147e987679f0900000000001976a914dc2814782770cd9b55f20ef71c9ac7b2248d613f88ac08870c00000000001976a9144f8ced0dd17f045dc38789a75f341b6c2d211c5988ac0b2a0300000000001976a914054d3d29d910576e023a435009dc85fb3e80a63a88ac25bf0d00000000001976a914de03193bfc08d90b5541dda254ee4972e7f740e488ac376b70000000000017a914c6707ca3ca1bb3693fcd198560d7e4070aa1b36f8764b61701000000001976a914572f90b0087004e504a723be4c1082d91b56d91c88acc02f5501000000001976a914b0271c737db4f88864ef7d529f1ea6862b31eb0a88acbbc7d501000000001976a914e1a10f15699e404a449c4c906e0fd368d11f923788ac8bb70700"
binary = [hex_tx].pack("H*")
prev_tx = Bitcoin::Protocol::Tx.new(binary)
prev_out_index = 0
print prev_tx
value = prev_tx.outputs[prev_out_index].value - 10_000
recipient = "1ACqAAkGcHefeJDrWguH2sCXTYS9yFxmRT"
key = Bitcoin::Key.from_base58("KEY")
# Build the transaction
new_tx = build_tx do |t|
# Construct the input to the transaction using the previous transaction we loaded from tx.json
t.input do |i|
i.prev_out prev_tx.hash
i.prev_out_index prev_out_index
i.signature_key key
end
# Create first output to define amount of BTC to be sent
t.output do |o|
# Specify value of first output (in Satoshis)
# IMPORTANT!!! ANY DIFFERENCE BETWEEN THE PREVIOUS OUTPUT VALUE AND THIS VALUE WILL
# BE SENT TO THE MINER.
o.value value
# Specify the recipient of this transaction (make it o)
o.script { |s| s.recipient recipient }
end
# Create output with secret message using OP_RETURN
t.output do |o|
# specify our "secret message" to encode in the blockchain
o.to "secret message".unpack("H*"), :op_return
# specify the value of this output (zero)
o.value 0
end
end
# print hex version of new signed transaction
puts "Hex Encoded Transaction:\n\n"
puts new_tx.to_payload.unpack("H*")[0]
puts "\n\n"
# print JSON version of new signed transaction
puts "JSON:\n\n"
puts new_tx.to_json
For this I am getting following JSON
{
"hash":"05890e185b40d94586081af4236ab27b412dd19ad26fbe3c2380d61f336a722a",
"ver":1,
"vin_sz":1,
"vout_sz":2,
"lock_time":0,
"size":110,
"in":[
{
"prev_out":{
"hash":"655e480ea5b86158ed322fee71309a562718ec671126842612b7da66199590ec",
"n":0
},
"scriptSig":""
}
],
"out":[
{
"value":"0.00064455",
"scriptPubKey":"OP_DUP OP_HASH160 64f5e8707053510e7edef90b187a511031ee51e2 OP_EQUALVERIFY OP_CHECKSIG"
},
{
"value":"0.00000000",
"scriptPubKey":"OP_RETURN 736563726574206d657373616765"
}
]
}
I am about to store some data in bitcoin blockchain using OP_RETURN opcode. For that I am following this blog It needs JSON representation of the transaction with hash prevout_hash from here But I am getting error for my prevout_hash like http://webbtc.com/tx/655e480ea5b86158ed322fee71309a562718ec671126842612b7da66199590ec.json. So I took my JSON representation from blockchain.info Bitcoin::Protocol::Tx.from_json_file method gives the following error while using this json representation
prev_tx = Bitcoin::Protocol::Tx.from_json_file('tx.json') NoMethodError: undefined method
each' for "":String from /home/hhhh/.rvm/gems/ruby-2.2.2/gems/bitcoin-ruby-0.0.14/lib/bitcoin/protocol/txin.rb:108:infrom_hash'
What is difference between webbtc json format and blockchain.info json format? How can I get webbtc json format to load my 655e480ea5b86158ed322fee71309a562718ec671126842612b7da66199590ec transaction ?