#!/usr/bin/env ruby # Copyright (c) 2019 Yegor Bugayenko # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the 'Software'), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. STDOUT.sync = true require 'slop' require 'backtrace' require_relative '../lib/sibit' require_relative '../lib/sibit/version' begin begin opts = Slop.parse(ARGV, strict: true, help: true) do |o| o.banner = "Usage (#{Sibit::VERSION}): sibit [options] command [args] Commands are: price: Get current price of BTC in USD latest: Get hash of the latest block generate: Generate a new private key create: Create a public Bitcoin address from the key balance: Check the balance of the Bitcoin address pay: Send a new Bitcoin transaction Options are:" o.bool '--help', 'Read this: https://github.com/yegor256/sibit' do puts o exit end o.bool '--verbose', 'Print all possible debug messages' end rescue Slop::Error => ex raise "#{ex.message}" end raise 'Try --help' if opts.arguments.empty? sibit = Sibit.new(log: opts[:verbose] ? STDOUT : nil) case opts.arguments[0] when 'price' puts sibit.price when 'latest' puts sibit.latest when 'generate' puts sibit.generate when 'create' pvt = opts.arguments[1] raise 'Private key argument is required' if pvt.nil? puts sibit.create(pvt) when 'balance' address = opts.arguments[1] raise 'Address argument is required' if address.nil? puts sibit.balance(address) when 'pay' amount = opts.arguments[1] raise 'Amount argument is required' if amount.nil? fee = opts.arguments[2] raise 'Miners fee argument is required' if fee.nil? sources = opts.arguments[3] raise 'Addresses argument is required' if sources.nil? target = opts.arguments[4] raise 'Target argument is required' if target.nil? change = opts.arguments[5] raise 'Change argument is required' if change.nil? puts sibit.pay( amount, fee, sources.split(',').map { |p| p.split(':') }.to_h, target, change ) else raise "Command #{opts.arguments[0]} is not supported" end rescue StandardError => ex if opts[:verbose] puts Backtrace.new(ex).to_s else puts "ERROR: #{ex.message}" end exit(255) end