【发布时间】:2015-07-14 14:36:48
【问题描述】:
我最初在比特币堆栈上发布了这个问题,但被告知我在这里重新发布它可能会更好。
我在使用比特币红宝石宝石时遇到了问题。尝试向我在测试网络上的另一个地址汇款时收到以下错误:
/var/lib/gems/2.2.0/gems/bitcoin-ruby-0.0.7/lib/bitcoin/protocol/txout.rb:76:in
pk_script=': undefined methodbytesize' 为 nil:NilClass (NoMethodError)
我正在生成一个私钥:
def new_address
Bitcoin::generate_address
end
并通过以下方式获取私钥的详细信息:
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
我要给自己汇款的代码如下:
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)
有谁知道如何解决这个错误?
【问题讨论】:
-
使用诸如 pry github.com/nixme/pry-debugger 之类的调试工具可能会对您有所帮助。
-
在堆栈跟踪中找到引用您的代码的第一行。
-
你能解决这个问题吗?我也有类似的问题。当我尝试广播我的交易
Error validating transaction: Transaction时,我得到了这个
标签: ruby private-key bitcoin public-key