为什么我不能在Ruby中签署比特币交易?

时间:2019-04-09 14:22:05

标签: ruby bitcoin

我的代码有什么问题(我正在使用this gem)?

require 'bitcoin'
include Bitcoin::Builder
key = Bitcoin::Key.from_base58('...')
txn = build_tx do |tx|
  tx.input do |i|
    i.prev_out(Bitcoin::P::Tx.from_hash(...))
    i.prev_out_index(0)
    i.signature_key(Bitcoin::Key.from_base58(...))
  end
  tx.output do |o|
    o.value(50000)
    o.to('1GzqkR2zNQUzHLpE7PLPjVNJ51FHC3bpDH')
  end
end

我得到:

RuntimeError: Script type must be hash160, pubkey, p2wpkh or multisig
    /Users/yegor/.rvm/gems/ruby-2.6.0/gems/bitcoin-ruby-0.0.19/lib/bitcoin/builder.rb:239:in `sig_hash_and_all_keys_exist?'
    /Users/yegor/.rvm/gems/ruby-2.6.0/gems/bitcoin-ruby-0.0.19/lib/bitcoin/builder.rb:308:in `sign_input'
    /Users/yegor/.rvm/gems/ruby-2.6.0/gems/bitcoin-ruby-0.0.19/lib/bitcoin/builder.rb:208:in `block in tx'
    /Users/yegor/.rvm/gems/ruby-2.6.0/gems/bitcoin-ruby-0.0.19/lib/bitcoin/builder.rb:207:in `each'
    /Users/yegor/.rvm/gems/ruby-2.6.0/gems/bitcoin-ruby-0.0.19/lib/bitcoin/builder.rb:207:in `each_with_index'
    /Users/yegor/.rvm/gems/ruby-2.6.0/gems/bitcoin-ruby-0.0.19/lib/bitcoin/builder.rb:207:in `tx'
    /Users/yegor/.rvm/gems/ruby-2.6.0/gems/bitcoin-ruby-0.0.19/lib/bitcoin/builder.rb:21:in `build_tx'

我在做什么错?顺便说一句,无论我向signature_key提供什么私钥,我都会遇到相同的错误。如果删除带有singature_key的行,则不会出错。

我也向他们的仓库提交了一张票证:https://github.com/lian/bitcoin-ruby/issues/287

1 个答案:

答案 0 :(得分:0)

问题出在脚本的格式上。这段代码有效:

utxo = # coming from https://www.blockchain.info/api/unspent
  builder.input do |i|
    i.prev_out(utxo['tx_hash_big_endian'])
    i.prev_out_index(utxo['tx_output_n'])
    i.prev_out_script = [utxo['script']].pack('H*')
    i.signature_key(key)
  end