[英]How to broadcast raw transaction using bitcoin-ruby?
如何使用比特幣-盧比廣播原始交易? 我使用了https://github.com/lian/bitcoin-ruby的 bitcoin-ruby gem
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.
交易費用只是輸入值與輸出值之差。 在這種情況下,您的輸出值為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)
您可以使用sibit ,這是我的Ruby gem:
require 'sibit'
sibit = Sibit.new
pkey = sibit.generate
address = sibit.create(pkey)
balance = sibit.balance(address)
target = sibit.create(pkey) # where to send coins to
change = sibit.create(pkey) # where the change will sent to
tx = sibit.pay(pkey, 10_000_000, 'XL', [address], target, change)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.