Crypto Flexs
  • DIRECTORY
  • CRYPTO
    • ETHEREUM
    • BITCOIN
    • ALTCOIN
  • BLOCKCHAIN
  • EXCHANGE
  • ADOPTION
  • TRADING
  • HACKING
  • SLOT
Crypto Flexs
  • DIRECTORY
  • CRYPTO
    • ETHEREUM
    • BITCOIN
    • ALTCOIN
  • BLOCKCHAIN
  • EXCHANGE
  • ADOPTION
  • TRADING
  • HACKING
  • SLOT
Crypto Flexs
Home»BITCOIN NEWS»wallet – Create and sign transactions locally in Ruby
BITCOIN NEWS

wallet – Create and sign transactions locally in Ruby

By Crypto FlexsMay 13, 20242 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Email
wallet – Create and sign transactions locally in Ruby
Share
Facebook Twitter LinkedIn Pinterest Email

So I’m using public node for Bitcoin node integration and previously I used sendtoaddress method to build, sign and broadcast transactions on the node. But now the sendtoaddress method is not allowed on public nodes due to privacy reasons due to private key disclosure, so we have to create, sign and broadcast the transaction locally offline. I’m using Ruby on Rails (openware/peatio). Here is my code so far: I am using the Bitcoinrb gem ( https://rubygems.org/gems/bitcoinrb ).

TESTNET_BASE_URL = "https://blockstream.info/testnet/api/address/".freeze
MAINNET_BASE_URL = "https://blockstream.info/api/address/".freeze



def create_transaction!(transaction, options = )
  env = @currency.dig(:key)&.include?('testnet') ? "testnet" : "mainnet"
  ::Bitcoin.chain_params=(env)
  key = ::Bitcoin::Key.new(priv_key: @wallet.fetch(:secret))
  url = @currency.dig(:key)&.include?('testnet') ? TESTNET_BASE_URL : MAINNET_BASE_URL
  response = URI.open(url + "#@wallet(:address)/utxo")
  utxos = JSON.parse(response.read)
  trx = build_trx(utxos.first, transaction.to_address, transaction.amount)
  trx = sign_transaction(trx, @wallet.fetch(:secret))


  signed_trx = client.json_rpc(:sendrawtransaction, "hexstring": trx.to_payload.bth, maxfeerate: 0)
  Rails.logger.warn  "-=-=-=sendrawtransaction-=-=- #signed_trx.inspect -=-=-=-=-=-=-=-=" 
  signed_trx
rescue Bitcoind::Client::Error => e 
  raise Peatio::Wallet::ClientError, e
end

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

def build_trx(utxo, recipient_address, amount)
  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(value: amount.to_f, script_pubkey: Bitcoin::Script.to_p2pkh(recipient_address))
  tx.outputs << tx_out
  tx
end

However, the output of sendrawtrasnaction is:

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

Please let me know if I need to do it any other way. If I’m doing something wrong or missing something, I’d appreciate some help.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email

Related Posts

Binance: 80%of Asian encryption users adopt 2FA but advanced security delay

May 15, 2025

Bitcoin on ‘diversifier’ more than Safe-haven assets

May 14, 2025

It is not well understood and is not widely accepted. Bitcoin-$ 100,000 opportunities

May 13, 2025
Add A Comment

Comments are closed.

Recent Posts

Manta Network reveals Stargate’s ETH pool for smooth cross chain transactions.

May 15, 2025

Nexpace is a chart of new chapters of MAPLESTORY Universe by launching MAPLESTORY N and NXPC tokens.

May 15, 2025

Bitcoin’s six signs of predicting $ 140K to the next price

May 15, 2025

Ethereum, Solana and other chains Vaneck and Securitize tokenized Treasury Fund

May 15, 2025

ETH PECTRA upgrade: Impact on idiot and roll -up costs

May 15, 2025

NY Federal Reserve taps token assets, not CBDC, to the future of finance.

May 15, 2025

XRP Elliott Wave is a hint when modifying -Why is the support of $ 2.34 important?

May 15, 2025

Is the XRP price over now?

May 15, 2025

Are the courts hinder the encryption?

May 15, 2025

SportsBet.io launched a million USDT prizes to display the Champions League finale

May 15, 2025

Chainalysis CEO provides clues to the recent Paris encryption attack.

May 15, 2025

Crypto Flexs is a Professional Cryptocurrency News Platform. Here we will provide you only interesting content, which you will like very much. We’re dedicated to providing you the best of Cryptocurrency. We hope you enjoy our Cryptocurrency News as much as we enjoy offering them to you.

Contact Us : Partner(@)Cryptoflexs.com

Top Insights

Manta Network reveals Stargate’s ETH pool for smooth cross chain transactions.

May 15, 2025

Nexpace is a chart of new chapters of MAPLESTORY Universe by launching MAPLESTORY N and NXPC tokens.

May 15, 2025

Bitcoin’s six signs of predicting $ 140K to the next price

May 15, 2025
Most Popular

Ethereum developer Virgil Griffiths’ sentence reduced by seven months.

July 18, 2024

In the Vegas Sphere Plan, Dogwifhat insists that there is no misunderstanding.

February 1, 2025

Silvergate exec says ‘sudden regulatory changes’ caused bank to close

September 23, 2024
  • Home
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions
© 2025 Crypto Flexs

Type above and press Enter to search. Press Esc to cancel.