private key - Using Bitcoin-ruby to make a transaction -
i posted question on bitcoin stack told may have better luck reposting here.
i having trouble bitcoin-ruby gem. following error when trying send money address have on 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' nil:nilclass (nomethoderror)
i generating private key with:
def new_address bitcoin::generate_address end
and getting details private key with:
def key_details(prikey, pubkey) #returns prikey, prikey_hash58, pubkey_hash58, pubkey_uncompressed, address 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 have send money myself 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 |t| t.input |i| i.prev_out prev_tx i.prev_out_index prev_out_index i.signature_key key end t.output |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 know how solve error?
it turned out couple of issues. here learned along way:
the tx json data used build prev_tx object needs have output value string. sites use number instead.
the key object doesn't form correctly. whatever reason, have address can generated using private key bitcoin::key.new. if supply private , public key address associated key object wrong. different addresses, isn't case. important check address of key object after generating object.
it important make sure addresses / keys generated on testnet3 used 1 another. had situation thought generated testnet3 private key, didn't. thus, testnet3 tx wasn't working. , vice versa normal bitcoin network.
lastly, it's important verify output address position. 0 or 1, isn't case...
Comments
Post a Comment