Class: Neb::Account

Inherits:
Object
  • Object
show all
Defined in:
lib/neb/account.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(private_key:, password: nil) ⇒ Account

Returns a new instance of Account.



11
12
13
14
15
16
# File 'lib/neb/account.rb', line 11

def initialize(private_key:, password: nil)
  @private_key_obj = PrivateKey.new(private_key)
  @public_key_obj  = @private_key_obj.to_pubkey_obj
  @address_obj     = @public_key_obj.to_address_obj
  @password        = password
end

Instance Attribute Details

#address_objObject (readonly)

Returns the value of attribute address_obj.



6
7
8
# File 'lib/neb/account.rb', line 6

def address_obj
  @address_obj
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/neb/account.rb', line 7

def password
  @password
end

#private_key_objObject (readonly)

Returns the value of attribute private_key_obj.



6
7
8
# File 'lib/neb/account.rb', line 6

def private_key_obj
  @private_key_obj
end

#public_key_objObject (readonly)

Returns the value of attribute public_key_obj.



6
7
8
# File 'lib/neb/account.rb', line 6

def public_key_obj
  @public_key_obj
end

Class Method Details

.create(password: nil) ⇒ Object



19
20
21
# File 'lib/neb/account.rb', line 19

def create(password: nil)
  new(private_key: PrivateKey.random.to_s, password: password)
end

.from_key(key:, password:) ⇒ Object



23
24
25
# File 'lib/neb/account.rb', line 23

def from_key(key:, password:)
  new(private_key: Key.decrypt(key, password), password: password)
end

.from_key_file(key_file:, password:) ⇒ Object



27
28
29
# File 'lib/neb/account.rb', line 27

def from_key_file(key_file:, password:)
  from_key(key: File.read(key_file), password: password)
end

Instance Method Details

#addressObject



40
41
42
# File 'lib/neb/account.rb', line 40

def address
  @address_obj.to_s
end

#private_keyObject



32
33
34
# File 'lib/neb/account.rb', line 32

def private_key
  @private_key_obj.to_s
end

#public_keyObject



36
37
38
# File 'lib/neb/account.rb', line 36

def public_key
  @public_key_obj.to_s
end

#to_keyObject

Raises:

  • (ArgumentError)


44
45
46
47
# File 'lib/neb/account.rb', line 44

def to_key
  raise ArgumentError.new("must set_password first") if password.blank?
  Key.encrypt(address, private_key, password)
end

#to_key_file(file_path: nil) ⇒ Object



49
50
51
52
53
# File 'lib/neb/account.rb', line 49

def to_key_file(file_path: nil)
  file_path = Neb.root.join('tmp', "#{address}.json") if file_path.blank?
  File.open(file_path, 'w+') { |f| f << Utils.to_json(to_key) }
  file_path.to_s
end