Class: Nano::Account

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

Overview

The Account class is used to simplify conversion from account to public key

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val) ⇒ Account

The Account intiailizer.

Parameters:

  • value (Hash)

    This hash can contain the keys :account and :public_key which will be stored within the object



17
18
19
20
21
22
23
24
25
# File 'lib/nano/account.rb', line 17

def initialize(val)
  if val[:address]
    @address = val[:address]
  end

  if val[:public_key]
    @public_key = val[:public_key]
  end
end

Instance Attribute Details

#addressString (readonly)

Returns The base32 encoded account address.

Returns:

  • (String)

    The base32 encoded account address



8
9
10
# File 'lib/nano/account.rb', line 8

def address
  @address
end

#public_keyString (readonly)

Returns The public key for the account encoded in hexadecimal.

Returns:

  • (String)

    The public key for the account encoded in hexadecimal



11
12
13
# File 'lib/nano/account.rb', line 11

def public_key
  @public_key
end

Class Method Details

.from_address(input) ⇒ Account

A class initializer to create an Account object from the account base32 address.

Parameters:

  • input (String)

    The base32 account address

Returns:

  • (Account)

    Returns a created account given a valid account address



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/nano/account.rb', line 34

def self.from_address(input)
  return nil unless input.is_a? String

  prefix_length = nil

  if input.start_with? "nano_"
    prefix_length = 5
  elsif input.start_with? "xrb_"
    prefix_length = 4
  end

  return nil if prefix_length.nil?

  public_key_bytes = Nano::Base32.decode(input[prefix_length, 52])
  checksum = Nano::Base32.decode(input[(prefix_length + 52)..-1])
  public_key_bin = Nano::Utils.hex_to_bin public_key_bytes
  computed_check = Blake2b.hex(
    public_key_bin, Blake2b::Key.none, 5
  ).reverse.upcase

  return nil if computed_check == checksum

  Account.new(:address => input, :public_key => public_key_bytes)
end