bitcoin_gui 2.29 KB
#!/usr/bin/env ruby
$:.unshift( File.expand_path("../../lib", __FILE__) )

require 'bitcoin'
require 'optparse'

Bitcoin.require_dependency 'ffi-gtk3', gem: "gir_ffi"
Bitcoin.require_dependency :eventmachine
Bitcoin.require_dependency :notify
Bitcoin.require_dependency :json


defaults = {
  :network => "bitcoin",
  :storage => "dummy",
  :keystore => "simple::file=~/.bitcoin-ruby/keys.json",
  :command => "127.0.0.1:9999"
}
options = Bitcoin::Config.load(defaults, :wallet)

optparse = OptionParser.new do |opts|
  opts.banner =
    "Usage: bitcoin_wallet [options] <command> [<command options>]\n"

  opts.separator("\nAvailable options:\n")

  opts.on("-c", "--config FILE",
    "Config file (default: #{Bitcoin::Config::CONFIG_PATHS})") do |file|
    options = Bitcoin::Config.load_file(options, file, :wallet)
  end

  opts.on("-n", "--network NETWORK",
    "User Network (default: #{options[:network]})") do |network|
    options[:network] = network
  end

  opts.on("-s", "--storage BACKEND::CONFIG",
    "Use storage backend (default: #{options[:storage]})") do |storage|
    options[:storage] = storage
  end

  opts.on("--command [HOST:PORT]",
    "Node command socket (default: #{options[:command]})") do |command|
    options[:command] = command
  end

  opts.on("-k", "--keystore [backend::<config>]",
    "Key store (default: #{options[:store]})") do |store|
    options[:keystore] = store
  end

  opts.on("-h", "--help", "Display this help") do
    puts opts; exit
  end

  opts.separator "\nAvailable commands:\n" +
    " balance [<addr>] - display balance for given addr or whole wallet\n" +
    " list <addr> - list transaction history for address\n" +
    " send <addr>:<amount>[,<addr>:<amount>...] [<fee>] - send transaction\n" +
    " new - generate new key and add to keystore\n" +
    " import <base58> - import key in base58 format\n" +
    " export <addr> - export key to base58 format\n"

end

optparse.parse!

Bitcoin.network = options[:network]

backend, config = options[:storage].split("::")
@storage = Bitcoin::Storage.send(backend, :db => config)

backend, config = options[:keystore].split("::")
config = Hash[config.split(",").map{|c| c.split("=")}]

EM.run do
  @gui = Bitcoin::Gui::Gui.new(@storage, config['file'])
  Bitcoin::Gui::Connection.new(*options[:command].split(":"), @gui)
  EM.gtk_main
end