-1

I originally posted this question on the bitcoin stack but was told I may have better luck reposting it here.

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 methodbytesize' 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)

Does anyone know how to solve this error?

Ethan
  • 1
  • 1
  • Using some debugging tool like pry https://github.com/nixme/pry-debugger may help you out. – maximus ツ Jul 14 '15 at 14:42
  • Find the first line in the stacktrace that references your code. – jcm Jul 14 '15 at 16:34
  • Are you able to figure this out? I'm also having a similar problem. I'm getting this when I'm trying to broadcast my transaction `Error validating transaction: Transaction` – 0xgoku Apr 27 '18 at 05:04

1 Answers1

0

It turned out to be a couple of issues. Here is what I learned along the way:

The tx json data that is used to build the prev_tx object needs to have a output value as a string. Some sites use a number instead.

The key object sometimes doesn't form correctly. For whatever reason, I have an address which can only be generated using the private key with Bitcoin::Key.new. If you supply a private and public key the address associated with the key object is wrong. For different addresses, this isn't the case. So it is important to check the address of the key object after generating the object.

It is also important to make sure the addresses / keys generated on testnet3 are only used with one another. I had a situation where I thought I generated a testnet3 private key, but didn't. Thus, the testnet3 tx wasn't working. And vice versa for the normal bitcoin network.

Lastly, it's very important to verify the output address position. It is usually 0 or 1, but this isn't always the case...

Ethan
  • 1
  • 1