使用比特币-卢比进行交易

问题描述 投票:0回答:2

我最初将这个问题发布在比特币堆栈上,但被告知我最好在这里重新发布它。

我在使用比特币-红宝石宝石时遇到麻烦。尝试汇款到测试网络上的另一个地址时,出现以下错误:

/ var / lib / gems / 2.2.0 / gems / bitcoin-ruby-0.0.7 / lib / bitcoin / protocol / txout.rb:76:inpk_script=': undefined methodbytesize'for 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)

有人知道如何解决此错误吗?

ruby private-key bitcoin public-key
2个回答
0
投票

原来是几个问题。这是我从中学到的方法:

用于构建prev_tx对象的tx json数据需要具有作为字符串的输出值。有些网站改用数字。

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

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

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


0
投票

var / lib / gems / 2.2.0 / gems / bitcoin-ruby-0.0.7 / lib / bitcoin / protocol / txout.rb:76:in pk_script =':undefined methodbytesize'untuk nihil:NilClass(NoMethodError)] >

© www.soinside.com 2019 - 2024. All rights reserved.