Class: Crypto::Keys::KeyRing

Inherits:
Object
  • Object
show all
Defined in:
lib/crypto/key_ring.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(private_key, public_key, wif, address, seed = nil) ⇒ KeyRing

Returns a new instance of KeyRing.



15
16
17
18
19
20
21
# File 'lib/crypto/key_ring.rb', line 15

def initialize(private_key, public_key, wif, address, seed = nil)
  @private_key = private_key
  @public_key = public_key
  @wif = wif
  @address = address
  @seed = seed
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



11
12
13
# File 'lib/crypto/key_ring.rb', line 11

def address
  @address
end

#private_keyObject (readonly)

Returns the value of attribute private_key.



8
9
10
# File 'lib/crypto/key_ring.rb', line 8

def private_key
  @private_key
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



9
10
11
# File 'lib/crypto/key_ring.rb', line 9

def public_key
  @public_key
end

#seedObject (readonly)

Returns the value of attribute seed.



13
14
15
# File 'lib/crypto/key_ring.rb', line 13

def seed
  @seed
end

#wifObject (readonly)

Returns the value of attribute wif.



10
11
12
# File 'lib/crypto/key_ring.rb', line 10

def wif
  @wif
end

Class Method Details

.generate(network = MAINNET) ⇒ Object



23
24
25
26
27
28
# File 'lib/crypto/key_ring.rb', line 23

def self.generate(network = MAINNET)
  key_pair = KeyUtils.create_new_keypair
  private_key = PrivateKey.new(key_pair[:hex_private_key], network)
  public_key = PublicKey.new(key_pair[:hex_public_key], network)
  KeyRing.new(private_key, public_key, private_key.wif, public_key.address)
end

.generate_hd(seed = nil, derivation = nil, network = MAINNET) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/crypto/key_ring.rb', line 30

def self.generate_hd(seed = nil, derivation = nil, network = MAINNET)
  _seed = seed.nil? ? SecureRandom.hex(64) : seed
  keys = (derivation.nil? || derivation.nil? && derivation == "m") ?
    HDKEY::KeyRing.get_master_key_from_seed(_seed) : HDKEY::KeyRing.derive_path(derivation, _seed, HDKEY::HARDENED_AXENTRO)

  private_key = PrivateKey.new(keys.private_key, network)
  _public_key = HDKEY::KeyRing.get_public_key(keys.private_key)[2..-1]
  public_key = PublicKey.new(_public_key, network)
  KeyRing.new(private_key, public_key, private_key.wif, public_key.address, _seed)
end

.is_valid?(public_key, wif, address) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/crypto/key_ring.rb', line 41

def self.is_valid?(public_key, wif, address)
  address = Address.from(address)
  wif = Wif.new(wif)

  raise AxentroError, "network mismatch between address and wif" if address.network != wif.network

  public_key = PublicKey.from_hex(public_key, address.network)
  raise AxentroError, "public key mismatch between public key and wif" if public_key.as_hex != wif.public_key.as_hex

  true
end