如何使用比特币 - 红宝石广播原始交易?

时间:2018-01-10 12:59:13

标签: ruby transactions broadcast bitcoin bitcoin-testnet

如何使用比特币红宝石广播原始交易? 我使用了来自https://github.com/lian/bitcoin-ruby

的比特币 - 红宝石宝石
    Bitcoin.network = :testnet

    address="muLGqMS6tkqb9sBQ4sqeXjMzaV8rk8o3gv"

    url = "https://testnet.blockexplorer.com/api/addr/#{address}"
    uri = URI(url)
    response = Net::HTTP.get(uri)
    if Connect.valid_json(response)
      response = JSON.parse(response)
      $transactions = response["transactions"]
    end

    prev_hash = $transactions[0]

    url = "https://testnet.blockexplorer.com/api/rawtx/#{prev_hash}"
    uri = URI(url)
    response = Net::HTTP.get(uri)
    if Connect.valid_json(response)
      response = JSON.parse(response)
      response = response['rawtx'].to_s
      $prev_tx = Bitcoin::P::Tx.new(response.htb)
    end

    prev_tx = $prev_tx
    prev_tx_output_index = 0
    value = 50000 #0.0005 btc

    tx = Bitcoin::Protocol::Tx.new
    tx.add_in Bitcoin::Protocol::TxIn.new(prev_tx.binary_hash, prev_tx_output_index, 0)

    tx.add_out Bitcoin::Protocol::TxOut.value_to_address(value, "msPHTrHSktDLMwPXcMYwWTqth3ZyykN17H") # <- dest address (our donation address)

    # if all in and outputs are defined, start signing inputs.
    key = Bitcoin.open_key(Connect.get_private_key) # <- privkey
    sig = Bitcoin.sign_data(key, tx.signature_hash_for_input(0, prev_tx))
    tx.in[0].script_sig = Bitcoin::Script.to_signature_pubkey_script(sig, [key.public_key_hex].pack("H*"))
    #tx.in[0].add_signature_pubkey_script(sig, key.public_key_hex)

    # finish check
    tx = Bitcoin::Protocol::Tx.new( tx.to_payload )
    p tx.verify_input_signature(0, prev_tx) == true
    hex =  tx.to_payload.unpack("H*")[0] # hex binary
    puts hex.to_s
#Error sending transaction: Transaction 4a762238529450737b85ad481deae0e836e623afd63e04a29a5b90363c0345fd has too high fees: 12372992.

2 个答案:

答案 0 :(得分:0)

交易费用只是输入和输出值之间的差额。在您的情况下,您的输出值是0.0005 BTC。如果您选择的输入大于您要发送的输出以及费用,您必须创建一个更改输出以将其余输出发送回您的钱包,否则它将全部作为费用交给矿工。例如,如果您的输入为0.25 BTC,并且您想发送0.15 BTC并且仅支付0.01 BTC费用,则您的交易需要有两个输出,一个用于0.15 BTC,一个用于0.09 BTC返回您拥有的地址,所以总费用只是(0.25-(0.15 + 0.9)= 0.01)

答案 1 :(得分:0)

您可以使用我的Ruby gem sibit

 http.request -> express.handler -> [async] -> [try] -> nested call -> [await]
 <-                     <-               <-
 // synchronous code returned, the [async] stack will be kept
 [async] -> [try] -> nested call -> [await]

// somewhen, the promise resolves, execution coninues:
[async] -> [try] -> nested call -> Throw!
<-       <- [catch]  <--------------