<p>Due to a lack of responses here, I cross posted this question on the stackoverflow channel. Here is a link with the answer:</p>

<p><a href="https://stackoverflow.com/questions/31409720/using-bitcoin-ruby-to-make-a-transaction/31420786#31420786">https://stackoverflow.com/questions/31409720/using-bitcoin-ruby-to-make-a-transaction/31420786#31420786</a></p>

CC BY-SA 3.0
2015-07-15T03:04:17.897Z
2015-07-15T03:04:17.897Z
2017-05-23T12:34:59.703Z
Ethan
<p>I am having trouble with the bitcoin-ruby gem. I get the following error when trying to send money to another address I have on the test network:</p>

<blockquote>
  <p>/var/lib/gems/2.2.0/gems/bitcoin-ruby-0.0.7/lib/bitcoin/protocol/txout.rb:76:in
  <code>pk_script=': undefined method</code>bytesize' for nil:NilClass
  (NoMethodError)</p>
</blockquote>

<p>I am generating a private key with:</p>

<pre><code>def new_address
 Bitcoin::generate_address
end
</code></pre>

<p>And getting details for the private key with:</p>

<pre><code>def key_details(prikey, pubkey)
  #returns prikey, prikey_hash58, pubkey_hash58, pubkey_uncompressed, address as a hash
  my_key = Bitcoin::Key.new(prikey, pubkey)
  { prikey:prikey, 
    prikey_base58:my_key.to_base58, 
    pubkey_58:my_key.hash160, 
    pubkey: my_key.pub_uncompressed, 
    address:my_key.addr
  }
end
</code></pre>

<p>The code I have to send money to myself is as follows: </p>

<pre><code>require 'bitcoin'
require_relative 'utilities.rb'
require 'open-uri'

Bitcoin.network = :testnet3

def build_transaction(prev_tx, prev_out_index, key, value, addr, message)
  include Bitcoin::Builder

  new_tx = build_tx do |t|
    t.input do |i|
      i.prev_out prev_tx
      i.prev_out_index prev_out_index
      i.signature_key key
    end
    t.output do |o|
      o.value value 
      o.script {|s| s.type :address; s.recipient addr }
    end
  end
end

def prev_tx(prev_hash, network)
  if network == "testnet3"
    prev_tx = Bitcoin::P::Tx.from_json(open("http://test.webbtc.com/tx/#{prev_hash}.json"))
  else
    prev_tx = Bitcoin::P::Tx.from_json(open("http://webbtc.com/tx/#{prev_hash}.json"))
  end
end

def key(publ_key, priv_key)
  key = Bitcoin::Key.new(priv_key, publ_key)
end

def bin_to_hex(s)
  s.unpack('H*').first
end

#transaction inputs
priv_key = "private_key"
publ_key = "public_key_long_format"
address = "address"
previous_tx = "previous_tx_hash"

# generate tx info off inputs
key = Bitcoin::Key.new(priv_key, publ_key)
prev_tx = prev_tx(previous_tx, "testnet3")
prev_out_index = 1
tx_value = prev_tx.outputs[prev_out_index].value

# build new tx
tx = build_transaction(prev_tx, prev_out_index, key, tx_value, address, "hello")

#
# puts tx.to_json
puts bin_to_hex(tx.to_payload)
</code></pre>

<private-key><signature><public-key><ruby>
Bitcoin Testnet Trouble with bitcoin-ruby
Ethan
# Bitcoin Testnet Trouble with bitcoin-ruby

(Asked by: username_0 on 2015-07-13)

I am having trouble with the bitcoin-ruby gem. I get the following error when trying to send money to another address I have on the test network:

> /var/lib/gems/2.2.0/gems/bitcoin-ruby-0.0.7/lib/bitcoin/protocol/txout.rb:76:in
> `pk_script=': undefined method`bytesize' for nil:NilClass
> (NoMethodError)

I am generating a private key with:

```
def new_address
 Bitcoin::generate_address
end

```

And getting details for the private key with:

```
def key_details(prikey, pubkey)
  #returns prikey, prikey_hash58, pubkey_hash58, pubkey_uncompressed, address as a hash
  my_key = Bitcoin::Key.new(prikey, pubkey)
  { prikey:prikey, 
    prikey_base58:my_key.to_base58, 
    pubkey_58:my_key.hash160, 
    pubkey: my_key.pub_uncompressed, 
    address:my_key.addr
  }
end

```

The code I have to send money to myself is as follows:

```
require 'bitcoin'
require_relative 'utilities.rb'
require 'open-uri'

Bitcoin.network = :testnet3

def build_transaction(prev_tx, prev_out_index, key, value, addr, message)
  include Bitcoin::Builder

  new_tx = build_tx do |t|
    t.input do |i|
      i.prev_out prev_tx
      i.prev_out_index prev_out_index
      i.signature_key key
    end
    t.output do |o|
      o.value value 
      o.script {|s| s.type :address; s.recipient addr }
    end
  end
end

def prev_tx(prev_hash, network)
  if network == "testnet3"
    prev_tx = Bitcoin::P::Tx.from_json(open("http://test.webbtc.com/tx/#{prev_hash}.json"))
  else
    prev_tx = Bitcoin::P::Tx.from_json(open("http://webbtc.com/tx/#{prev_hash}.json"))
  end
end

def key(publ_key, priv_key)
  key = Bitcoin::Key.new(priv_key, publ_key)
end

def bin_to_hex(s)
  s.unpack('H*').first
end

#transaction inputs
priv_key = "private_key"
publ_key = "public_key_long_format"
address = "address"
previous_tx = "previous_tx_hash"

# generate tx info off inputs
key = Bitcoin::Key.new(priv_key, publ_key)
prev_tx = prev_tx(previous_tx, "testnet3")
prev_out_index = 1
tx_value = prev_tx.outputs[prev_out_index].value

# build new tx
tx = build_transaction(prev_tx, prev_out_index, key, tx_value, address, "hello")

#
# puts tx.to_json
puts bin_to_hex(tx.to_payload)

```

**Tags:** private-key, signature, public-key, ruby


---

## 1 answers

### Answer by username_0 on 2015-07-15. Score: 1

Due to a lack of responses here, I cross posted this question on the stackoverflow channel. Here is a link with the answer:

<https://stackoverflow.com/questions/31409720/using-bitcoin-ruby-to-make-a-transaction/31420786#31420786>


---

