Class: MastercoinWallet::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/mastercoin-wallet/config.rb

Constant Summary collapse

DEFAULT =
ActiveSupport::HashWithIndifferentAccess.new(balance: 0, test_balance: 0, spendable_outputs: [], bought_transactions:[], created_transactions: [], sold_transactions: [], bitcoin_transactions: [], exodus_transactions: [], sent_transactions: [], received_transactions: [], btc_balance: 0)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/mastercoin-wallet/config.rb', line 6

def initialize
  self.ensure_config!

  config = File.open(MastercoinWallet::FILE_PATH).read()

  if config.empty?
    self.options = DEFAULT
  else
    self.options = ActiveSupport::HashWithIndifferentAccess.new(JSON.load(config)).reverse_merge(DEFAULT)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/mastercoin-wallet/config.rb', line 18

def method_missing(method, *args)
  if self.options.has_key?(method)
    self.get_key(method)
  else
    super(method, args)
  end
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/mastercoin-wallet/config.rb', line 3

def options
  @options
end

Instance Method Details

#get_encrypted_key(key, password) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/mastercoin-wallet/config.rb', line 26

def get_encrypted_key(key, password)
  aes = OpenSSL::Cipher.new("AES-256-CFB") 
  digest_key = Digest::SHA2.hexdigest(password)
  aes.key = digest_key

  begin
    aes.update(self.get_key(key).unpack('m')[0])
  rescue ArgumentError
  end
end

#get_key(key) ⇒ Object



63
64
65
# File 'lib/mastercoin-wallet/config.rb', line 63

def get_key(key)
  return self.options[key] if has_key?(key)
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/mastercoin-wallet/config.rb', line 67

def has_key?(key)
  self.options.has_key?(key)
end

#has_options?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/mastercoin-wallet/config.rb', line 77

def has_options?
  !self.options.empty?
end

#saveObject



71
72
73
74
75
# File 'lib/mastercoin-wallet/config.rb', line 71

def save
  config = File.open(MastercoinWallet::FILE_PATH, "w")
  config.write( JSON.dump(self.options) )
  config.close
end

#set_encrypted_key(key, value, password) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mastercoin-wallet/config.rb', line 37

def set_encrypted_key(key, value, password)
  aes = OpenSSL::Cipher.new("AES-256-CFB") 
  digest_key = Digest::SHA2.hexdigest(password)

  aes.encrypt
  aes.key = digest_key

  encrypted_value = [aes.update(value)].pack('m')

  set_key(key, encrypted_value)
end

#set_key(key, value) ⇒ Object



59
60
61
# File 'lib/mastercoin-wallet/config.rb', line 59

def set_key(key, value)
  self.options[key] = value
end

#set_key!(key, value) ⇒ Object



54
55
56
57
# File 'lib/mastercoin-wallet/config.rb', line 54

def set_key!(key, value)
  set_key(key, value)
  self.save
end

#set_keys!(options = {}) ⇒ Object



49
50
51
52
# File 'lib/mastercoin-wallet/config.rb', line 49

def set_keys!(options = {})
  self.options.merge!(options)
  self.save
end