Class: Nanook::PrivateKey

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/nanook/private_key.rb

Overview

The Nanook::PrivateKey class lets you manage your node's keys.

Constant Summary

Constants included from Util

Util::STEP

Instance Method Summary collapse

Constructor Details

#initialize(rpc, key = nil) ⇒ PrivateKey

Returns a new instance of PrivateKey.



10
11
12
13
# File 'lib/nanook/private_key.rb', line 10

def initialize(rpc, key = nil)
  @rpc = rpc
  @key = key.to_s if key
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Returns true if keys are equal.

Parameters:

Returns:

  • (Boolean)

    true if keys are equal



21
22
23
24
# File 'lib/nanook/private_key.rb', line 21

def ==(other)
  other.class == self.class &&
    other.id == id
end

#accountObject

Returns the Account that matches this private key. The account may not exist yet in the ledger.

Returns:

  • Nanook::Account



71
72
73
# File 'lib/nanook/private_key.rb', line 71

def 
  (memoized_key_expand[:account])
end

#create(seed: nil, index: nil) ⇒ Object

Generate a new private public key pair. Returns the new Nanook::PrivateKey. The public key can be retrieved by calling `#public_key` on the private key.

Examples:

private_key = nanook.private_key.create
private_key.public_key # => Nanook::PublicKey pair for the private key

deterministic_private_key = nanook.private_key.create(seed: seed, index: 0)

Parameters:

  • seed (String) (defaults to: nil)

    optional seed to generate a deterministic private key.

  • index (Integer) (defaults to: nil)

    optional (but required if seed is given) index to generate a deterministic private key.

Returns:

  • Nanook::PrivateKey



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nanook/private_key.rb', line 48

def create(seed: nil, index: nil)
  skip_key_required!

  params = {
    _access: :private,
    _coerce: Hash
  }

  @key = if seed.nil?
           rpc(:key_create, params)
         else
           raise ArgumentError, 'index argument is required when seed is given' if index.nil?

           rpc(:deterministic_key, params.merge(seed: seed, index: index))
         end

  self
end

#hashInteger

The hash value is used along with #eql? by the Hash class to determine if two objects reference the same hash key.

Returns:

  • (Integer)


31
32
33
# File 'lib/nanook/private_key.rb', line 31

def hash
  id.hash
end

#idObject



15
16
17
# File 'lib/nanook/private_key.rb', line 15

def id
  @key
end

#public_keyObject

Returns the Nanook::PublicKey pair for this private key.

Returns:

  • Nanook::PublicKey



78
79
80
# File 'lib/nanook/private_key.rb', line 78

def public_key
  as_public_key(memoized_key_expand[:public])
end

#to_sString Also known as: inspect

Returns:

  • (String)


83
84
85
# File 'lib/nanook/private_key.rb', line 83

def to_s
  "#{self.class.name}(id: \"#{short_id}\")"
end