<p>Try to use API from testnet.blockexplorer. The sample code is below:</p>

<pre><code>url = "https://testnet.blockexplorer.com/api/rawtx/#{prev_hash}"
uri = URI(url)
response = Net::HTTP.get(uri)
response = JSON.parse(response)
response = response['rawtx'].to_s
$prev_tx = Bitcoin::P::Tx.new(response.htb)
prev_tx = $prev_tx
</code></pre>

CC BY-SA 3.0
2018-02-22T06:58:45.023Z
2018-02-22T06:58:45.023Z
m2pathan
<p>You can use <a href="https://github.com/yegor256/sibit" rel="nofollow noreferrer">sibit</a>, my Ruby gem:</p>

<pre><code>require 'sibit'
sibit = Sibit.new
pkey = sibit.generate
address = sibit.create(pkey)
balance = sibit.balance(address)
target = sibit.create(pkey) # where to send coins to
change = sibit.create(pkey) # where the change will sent to
tx = sibit.pay(pkey, 10_000_000, 'XL', [address], target, change)
</code></pre>

CC BY-SA 4.0
2019-04-10T17:20:31.887Z
2019-04-10T17:20:31.887Z
yegor256
<p>Im using <a href="https://github.com/lian/bitcoin-ruby" rel="nofollow noreferrer">bitcoin-ruby</a> to create a raw transaction then broadcast it through block-cypher.
I follow their tutorial </p>

<pre><code># 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://test.webbtc.com/tx/#{prev_hash}.json"))

# 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://test.webbtc.com/relay_tx
# that will also give you a hint on the error if something goes wrong
puts new_tx.to_json
</code></pre>

<p><a href="http://test.webbtc.com/tx" rel="nofollow noreferrer">http://test.webbtc.com/tx</a> is no longer can use so i used blockcypher api or Blockchain.transaction(tx_id) instead.</p>

<pre><code>Bitcoin.network = :testnet3
private_key = "598ba1001a580737b8f7672cb7a3e5ce3413eb508b92de3b155ca4e904423736"
public_key="0498bd9486a4459fd48e7b962293da11ce035fc492946e178974316fd4389b1170269d0032007b2f03a51ec4689699e635b9d89b704aa676c1e76d8746a0a291e5"
prev_hash = "0ed5e5794ed9378037e76b18a92b8e562fee61fb3cbb538e42c7def1f605e442"
</code></pre>

<p>i fetched prev_tx infor</p>

<pre><code>tx_hash = blockcypher.blockchain_transaction('0ed5e5794ed9378037e76b18a92b8e562fee61fb3cbb538e42c7def1f605e442')

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

<p>then i parsed:</p>

<pre><code>Bitcoin::P::Tx.from_hash(tx_hash)
</code></pre>

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

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

<p>Do we have any solutions for this problems.</p>

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

<p>I get hex_transaction using</p>

<pre><code>https://testnet.blockchain.info/tx/0ed5e5794ed9378037e76b18a92b8e562fee61fb3cbb538e42c7def1f605e442?format=hex
Bitcoin::P::Tx.new('0100000000010146da16c6900cb47696feebc60860984399d1bb0c21e4be8912aa40a107ef0d450100000017160014c1d3f92a55e6506422c6550434615f2e34265ae7ffffffff0280a4bf07000000001976a91467120445f4ffdd1b9ca95b8dcd343611d65f6fb788ace2a2744c2c00000017a9144db63f2ce3412854d0718bbf0bc3e80e8a24d0de8702483045022100b6c18b7b3a3cf0689e806b2eb1dba6811d37fa152f461f60a3b1d9ec439c2aaa0220184465e4c16219f4582f48a2fcd0a214d8d56607896e86f86ffa07188807014c01210358ea03d324d68fe8164dff922a5502c297a7e91d6a97381c81d8ed121113084800000000')
</code></pre>

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

<blockchain><raw-transaction><ruby>
Cannot create a raw transaction using bitcoin-ruby
NoobFromVN
# Cannot create a raw transaction using bitcoin-ruby

(Asked by: username_0 on 2017-12-31)

Im using [bitcoin-ruby](https://github.com/lian/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://test.webbtc.com/tx/#{prev_hash}.json"))

# 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://test.webbtc.com/relay_tx
# that will also give you a hint on the error if something goes wrong
puts new_tx.to_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.

I get hex\_transaction using

```
https://testnet.blockchain.info/tx/0ed5e5794ed9378037e76b18a92b8e562fee61fb3cbb538e42c7def1f605e442?format=hex
Bitcoin::P::Tx.new('0100000000010146da16c6900cb47696feebc60860984399d1bb0c21e4be8912aa40a107ef0d450100000017160014c1d3f92a55e6506422c6550434615f2e34265ae7ffffffff0280a4bf07000000001976a91467120445f4ffdd1b9ca95b8dcd343611d65f6fb788ace2a2744c2c00000017a9144db63f2ce3412854d0718bbf0bc3e80e8a24d0de8702483045022100b6c18b7b3a3cf0689e806b2eb1dba6811d37fa152f461f60a3b1d9ec439c2aaa0220184465e4c16219f4582f48a2fcd0a214d8d56607896e86f86ffa07188807014c01210358ea03d324d68fe8164dff922a5502c297a7e91d6a97381c81d8ed121113084800000000')

```

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

**Tags:** blockchain, raw-transaction, ruby


---

## 2 answers

### Answer by username_1 on 2018-02-22. Score: 1

Try to use API from testnet.blockexplorer. The sample code is below:

```
url = "https://testnet.blockexplorer.com/api/rawtx/#{prev_hash}"
uri = URI(url)
response = Net::HTTP.get(uri)
response = JSON.parse(response)
response = response['rawtx'].to_s
$prev_tx = Bitcoin::P::Tx.new(response.htb)
prev_tx = $prev_tx

```


---

### Answer by username_2 on 2019-04-10. Score: 0

You can use [sibit](https://github.com/username_2/sibit), my Ruby gem:

```
require 'sibit'
sibit = Sibit.new
pkey = sibit.generate
address = sibit.create(pkey)
balance = sibit.balance(address)
target = sibit.create(pkey) # where to send coins to
change = sibit.create(pkey) # where the change will sent to
tx = sibit.pay(pkey, 10_000_000, 'XL', [address], target, change)

```


---

