Module: C3D::Utility

Extended by:
Utility
Included in:
Utility
Defined in:
lib/c3d/util/util.rb

Instance Method Summary collapse

Instance Method Details

#dir_exist?(directry) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
# File 'lib/c3d/util/util.rb', line 65

def dir_exist? directry
  unless File.directory? directry
    FileUtils.mkdir_p directry
  end
end

#get_configObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/c3d/util/util.rb', line 21

def get_config
  dir_exist? File.join(ENV['HOME'], '.epm')
  config_file  = File.join(ENV['HOME'], '.epm', 'c3d-config.json')
  config_example = File.join(File.dirname(__FILE__), '..', '..', '..', 'settings', 'c3d-config.json')
  unless File.exists? config_file
    tmp = File.read config_example
    until ! tmp[/(\{\{USERHOME\}\})/]
      tmp.gsub!("{{USERHOME}}", ENV["HOME"])
    end
    print "Before we begin, I need to ask you two personal questions:\n"
    print "What is your primary account which I should be using to send transactions?\n\n"
     = STDIN.gets.chomp
    print "Thanks. #{} is what I will use.\n"
    print "Thanks. Now what is the private key for that account which I should be using?\n\n"
    secret = STDIN.gets.chomp
    print "Thanks. #{secret} is what I will use.\n"
     = "0x#{}" if [0..1] != '0x'
    secret = "0x#{secret}" if secret[0..1] != '0x'
    tmp.gsub!("{{0xACCT}}", )
    tmp.gsub!("{{0xSEC}}", secret)
    File.open(config_file, 'w'){|f| f.write(tmp)}
  end
  return JSON.load(File.read(config_file))
end

#save_keyObject



7
8
9
10
11
12
13
14
# File 'lib/c3d/util/util.rb', line 7

def save_key
  key = Celluloid::Actor[:eth].get_key
  config_file  = File.join(ENV['HOME'], '.epm', 'c3d-config.json')
  config = JSON.load(File.read(config_file))
  config["primary_account_key"] = key if config["primary_account_key"] != key
  ENV['ETH_KEY'] = config["primary_account_key"]
  File.open(config_file, 'w'){|f| f.write(JSON.pretty_generate(config))}
end

#set_configObject



16
17
18
19
# File 'lib/c3d/util/util.rb', line 16

def set_config
  config = get_config
  set_the_env config
end

#set_the_env(config) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/c3d/util/util.rb', line 46

def set_the_env config
  ENV['SWARM_DIR']     = config['swarm_dir']
  ENV['TORRENTS_DIR']  = config['torrents_dir']
  ENV['BLOBS_DIR']     = config['blobs_dir']
  ENV['WATCH_FILE']    = config['watch_file']
  ENV['IGNORE_FILE']   = config['ignore_file']
  ENV['TORRENT_RPC']   = config['torrent_rpc']
  ENV['TORRENT_USER']  = config['torrent_user']
  ENV['TORRENT_PASS']  = config['torrent_pass']
  ENV['UI_RESPOND']    = config['ui_respond']
  ENV['UI_ANNOUNCE']   = config['ui_announce']
  ENV['ETH_CONNECTOR'] = config['eth_connector']
  ENV['ETH_ZMQ_ADDR']  = config['eth_zmq_addr']
  ENV['ETH_HOST']      = config['eth_rpc_host']
  ENV['ETH_PORT']      = config['eth_rpc_port']
  ENV['ETH_KEY']       ||= config['primary_account_key']
  ENV['GAS_PRICE']     = config['gas_price'] || '100000000000000'
end