【问题标题】:Using Bitcoin-ruby to make a transaction使用 Bitcoin-ruby 进行交易
【发布时间】: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


【解决方案1】:

原来是几个问题。以下是我一路上学到的东西:

用于构建 prev_tx 对象的 tx json 数据需要有一个字符串形式的输出值。有些网站使用数字代替。

键对象有时不能正确形成。无论出于何种原因,我都有一个地址,只能使用带有 Bitcoin::Key.new 的私钥生成。如果您提供私钥和公钥,则与密钥对象关联的地址是错误的。对于不同的地址,情况并非如此。所以生成对象后检查关键对象的地址很重要。

确保在 testnet3 上生成的地址/密钥只能相互使用也很重要。我有一种情况,我以为我生成了一个 testnet3 私钥,但没有。因此,testnet3 tx 无法正常工作。对于普通的比特币网络,反之亦然。

最后,验证输出地址位置非常重要。它通常是 0 或 1,但并非总是如此......

【讨论】:

  • 你是如何让它工作的?可以分享一下代码吗? @Ethan
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 2013-03-29
  • 1970-01-01
  • 1970-01-01
  • 2011-04-07
  • 2016-09-10
相关资源
最近更新 更多