Friday, November 22, 2024

pockets – Create and Signal transaction regionally in Ruby

So I’m utilizing the general public node for my Bitcoin node integration, and earlier we used sendtoaddress technique to construct, signal, and broadcast the transaction within the node.
However now because the sendtoaddress technique isn’t allowed in public nodes as a result of privateness causes of unveiling the personal keys, So i’ve to create, signal, and broadcast the transaction in my native offline. and I’m utilizing ruby on rails ( openware/peatio ).
my code to date is:
I’m utilizing the Bitcoinrb gem ( https://rubygems.org/gems/bitcoinrb )

TESTNET_BASE_URL = "https://blockstream.information/testnet/api/deal with/".freeze
MAINNET_BASE_URL = "https://blockstream.information/api/deal with/".freeze



def create_transaction!(transaction, choices = {})
  env = @forex.dig(:key)&.embody?('testnet') ? "testnet" : "mainnet"
  ::Bitcoin.chain_params=(env)
  key = ::Bitcoin::Key.new(priv_key: @pockets.fetch(:secret))
  url = @forex.dig(:key)&.embody?('testnet') ? TESTNET_BASE_URL : MAINNET_BASE_URL
  response = URI.open(url + "#{@pockets[:address]}/utxo")
  utxos = JSON.parse(response.learn)
  trx = build_trx(utxos.first, transaction.to_address, transaction.quantity)
  trx = sign_transaction(trx, @pockets.fetch(:secret))


  signed_trx = consumer.json_rpc(:sendrawtransaction, {"hexstring": trx.to_payload.bth, maxfeerate: 0})
  Rails.logger.warn { "-=-=-=sendrawtransaction-=-=- #{signed_trx.examine} -=-=-=-=-=-=-=-=" }
  signed_trx
rescue Bitcoind::Consumer::Error => e 
  elevate Peatio::Pockets::ClientError, e
finish

def sign_transaction(tx, private_key)
  key = Bitcoin::Key.new(priv_key: private_key)
  tx.inputs.each_with_index do |enter, index|
    script_pubkey = Bitcoin::Script.to_p2pkh(key.pubkey)
    enter.script_sig = script_pubkey
  finish
finish

def build_trx(utxo, recipient_address, quantity)
  tx = Bitcoin::Tx.new
  tx_in = Bitcoin::TxIn.new(out_point: Bitcoin::OutPoint.new(utxo["txid"], utxo["vout"].to_i))
  tx.inputs << tx_in
  tx_out = Bitcoin::TxOut.new(worth: quantity.to_f, script_pubkey: Bitcoin::Script.to_p2pkh(recipient_address))
  tx.outputs << tx_out
  tx
finish

However the output from sendrawtrasnaction is like:

{"statusCode":403,"errorCode":"btc.blockchain.broadcast.error","message":"Unable to broadcast transaction.","trigger":"Request failed with standing code 500","dashboardLog":"https://dashboard.tatum.io/logs?id=663f683a798bb0f969fc3760"}

please let me know if I ought to do it one other manner, or if I’m doing something mistaken or lacking one thing, any assist is extremely appreciated.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles