lian / bitcoin-ruby

bitcoin utils and protocol in ruby.
Other
922 stars 322 forks source link

RuntimeError: Script type must be hash160, pubkey, p2wpkh or multisig #287

Open yegor256 opened 5 years ago

yegor256 commented 5 years ago

What is wrong with my code?

    txn = build_tx do |tx|
      tx.input do |i|
        i.prev_out(txn)
        i.prev_out_index(0)
        i.signature_key(key)
      end
      tx.output do |o|
        o.value(satoshi)
        o.script { |s| s.recipient('1GzqkR2zNQUzHLpE7PLPjVNJ51FHC3bpDH') }
      end
    end

I'm getting:

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'

I suspect there is something wrong with the format of the signature key but would be great to get a more meaningful message or at least fail sometime earlier when I'm building the transaction.

Cross-posted to SO: https://stackoverflow.com/questions/55595070/why-i-cant-sign-a-bitcoin-transaction-in-ruby

yegor256 commented 5 years ago

I guess I know what's wrong. The tx['script'] is not a text, but a hash. That's why it can't be parsed. How do I convert hash160 script into its text format?

pierrenoizat commented 3 years ago

Sorry for necroposting but your prev_out in the tx.input block cannot be the new tx instance: it must be the tx id of the utxo being spent, in hex format.