0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.