transactions – Easy methods to ship Bitcoin within the Testnet?

0
72


I am utilizing this library: https://github.com/lian/bitcoin-ruby

And this is my code:

require 'open-uri'
require 'web/http'
require 'web/https'
require 'json'

require 'bitcoin'

Bitcoin.community = :testnet3

def get_response(prev_tx)
  url = "https://testnet.blockexplorer.com/api/rawtx/#{prev_tx}"
  uri = URI(url)
  response = Web::HTTP.get(uri)
  response = JSON.parse(response)
  response = response['rawtx'].to_s
  return response
finish

def build_transaction(prev_tx, prev_out_index, key, satoshi_to_spend, addr, message)
  embrace Bitcoin::Builder

  new_tx = build_tx do |t|
    t.enter do |i|
      i.prev_out prev_tx
      i.prev_out_index prev_out_index
      i.signature_key key
    finish
    t.output do |o|
      o.worth satoshi_to_spend
      o.script s
    finish
  finish
  places ""
  places "--- new tx ---"
  places new_tx.to_json
  return new_tx
finish

def bin_to_hex(s)
  s.unpack('H*').first
finish

bitcoin_key_1_pub = "0367e6251a89c5afc1212c3e2169866ccfe9d6059a4cfc15dc139b534b974fd7ca"
bitcoin_key_1_priv = "*****24c56528becd02d23076123a2edc2f3cf52c3c0202178c1b7fa9ac*****"
bitcoin_key_1_addr = "mxz4hu6JrTHevn22W4GDVjS3ko9MwXuHND"

# the earlier transaction that has an output to your tackle #1
prev_tx_1 = "427138ed5f2443fbfbf8065e990f1bf9ce0236e14376e8abca0b2876eabca653"

# the variety of the output you wish to use
prev_out_index = 1

# 0.5 BTC in satoshis
btc_to_spend = 0.5
satoshi_to_spend = btc_to_spend * 100000000

bitcoin_key_2_pub = "02989d0578f715a1980d4d6e968bcba487077e74aaf8a219d11ee4384ea1fc1eda"
bitcoin_key_2_priv = "*****cb521a27f9782cc503b461bf5c6b470469ca00ba1d7dc26f4ab61b*****"
bitcoin_key_2_addr = "n4SYP6TJyK7pUouW65fnC1hDBrbb7Za8RR"

# fetch the tx from whereever you want and parse it
response = get_response(prev_tx_1)
$prev_tx = Bitcoin::P::Tx.new(response.htb)
prev_tx = $prev_tx

tx_value = prev_tx.outputs[prev_out_index].worth
places "tx_value: #{tx_value}"

key = Bitcoin::Key.new(bitcoin_key_1_priv, bitcoin_key_1_pub)
tx = build_transaction(prev_tx, prev_out_index, key, satoshi_to_spend, bitcoin_key_2_addr, "good day")

places ""
places "--- bin_to_hex ---"
places bin_to_hex(tx.to_payload)

Here is the output:

➜  examples git:(grasp) ✗ ruby sandbox.rb

tx_value: 75972791
/Customers/glaksmono/.rvm/gems/ruby-2.4.2/gems/bitcoin-ruby-0.0.18/lib/bitcoin/script.rb:281: warning: fixed ::Fixnum is deprecated
/Customers/glaksmono/.rvm/gems/ruby-2.4.2/gems/bitcoin-ruby-0.0.18/lib/bitcoin/script.rb:455: warning: fixed ::Fixnum is deprecated

--- new tx ---
/Customers/glaksmono/.rvm/gems/ruby-2.4.2/gems/bitcoin-ruby-0.0.18/lib/bitcoin/script.rb:261: warning: fixed ::Fixnum is deprecated
{
  "hash":"88217866a40829f144e2d2c19b2e506812ae013d08d9335ce8fa839582f1b18e",
  "ver":1,
  "vin_sz":1,
  "vout_sz":1,
  "lock_time":0,
  "dimension":191,
  "in":[
    {
      "prev_out":{
        "hash":"427138ed5f2443fbfbf8065e990f1bf9ce0236e14376e8abca0b2876eabca653",
        "n":1
      },
      "scriptSig":"304402201cf2d99b7f10c71a1c77ae629ec134a3f255c0027f47815aff3e43d500e6da46022008409ea148d6cce6876910c7a7218b8197c3a827a6a7bf3d5f064a3ed3a0616701 0367e6251a89c5afc1212c3e2169866ccfe9d6059a4cfc15dc139b534b974fd7ca"
    }
  ],
  "out":[
    {
      "value":"0.50000000",
      "scriptPubKey":"OP_DUP OP_HASH160 fb75447d6038b05bb736e5b05f18a3c9da2b34db OP_EQUALVERIFY OP_CHECKSIG"
    }
  ]
}

--- bin_to_hex ---
010000000153a6bcea76280bcaabe87643e13602cef91b0f995e06f8fbfb43245fed387142010000006a47304402201cf2d99b7f10c71a1c77ae629ec134a3f255c0027f47815aff3e43d500e6da46022008409ea148d6cce6876910c7a7218b8197c3a827a6a7bf3d5f064a3ed3a0616701210367e6251a89c5afc1212c3e2169866ccfe9d6059a4cfc15dc139b534b974fd7caffffffff0180f0fa02000000001976a914fb75447d6038b05bb736e5b05f18a3c9da2b34db88ac00000000

It appears to be profitable, however I do not see the BTC truly transferred from bitcoin_key_1_addr to bitcoin_key_2_addr on the Blockcypher Explorer:

As you discover within the Blockcypher Explorer, the BTC quantity shouldn’t be but transferred from bitcoin_key_1_addr to bitcoin_key_2_addr. Concepts?

LEAVE A REPLY

Please enter your comment!
Please enter your name here