Class: Stellar::KeyPair

Inherits:
Object
  • Object
show all
Defined in:
lib/stellar/key_pair.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(public_key, secret_key = nil) ⇒ KeyPair

Returns a new instance of KeyPair.



39
40
41
42
# File 'lib/stellar/key_pair.rb', line 39

def initialize(public_key, secret_key=nil)
  @public_key = public_key
  @secret_key = secret_key
end

Class Method Details

.from_address(address) ⇒ Object



19
20
21
22
# File 'lib/stellar/key_pair.rb', line 19

def self.from_address(address)
  pk_bytes = Util::StrKey.check_decode(:account_id, address)
  from_public_key(pk_bytes)
end

.from_network_passphrase(passphrase) ⇒ Object



30
31
32
33
# File 'lib/stellar/key_pair.rb', line 30

def self.from_network_passphrase(passphrase)
  network_id = Digest::SHA256.digest(passphrase)
  from_raw_seed network_id
end

.from_public_key(pk_bytes) ⇒ Object



14
15
16
17
# File 'lib/stellar/key_pair.rb', line 14

def self.from_public_key(pk_bytes)
  public_key = RbNaCl::VerifyKey.new(pk_bytes)
  new(public_key)
end

.from_raw_seed(seed_bytes) ⇒ Object



8
9
10
11
12
# File 'lib/stellar/key_pair.rb', line 8

def self.from_raw_seed(seed_bytes)
  secret_key = RbNaCl::SigningKey.new(seed_bytes)
  public_key = secret_key.verify_key
  new(public_key, secret_key)
end

.from_seed(seed) ⇒ Object



3
4
5
6
# File 'lib/stellar/key_pair.rb', line 3

def self.from_seed(seed)
  seed_bytes = Util::StrKey.check_decode(:seed, seed)
  from_raw_seed seed_bytes
end

.masterObject



35
36
37
# File 'lib/stellar/key_pair.rb', line 35

def self.master
  from_raw_seed(Stellar.current_network_id)
end

.randomObject



24
25
26
27
28
# File 'lib/stellar/key_pair.rb', line 24

def self.random
  secret_key = RbNaCl::SigningKey.generate
  public_key = secret_key.verify_key
  new(public_key, secret_key)
end

Instance Method Details

#account_idObject



44
45
46
# File 'lib/stellar/key_pair.rb', line 44

def 
  Stellar::AccountID.new :key_type_ed25519, raw_public_key
end

#addressObject



73
74
75
76
# File 'lib/stellar/key_pair.rb', line 73

def address
  pk_bytes = raw_public_key
  Util::StrKey.check_encode(:account_id, pk_bytes)
end

#public_keyObject



48
49
50
# File 'lib/stellar/key_pair.rb', line 48

def public_key
  Stellar::PublicKey.new :key_type_ed25519, raw_public_key
end

#raw_public_keyObject



52
53
54
# File 'lib/stellar/key_pair.rb', line 52

def raw_public_key
  @public_key.to_bytes
end

#raw_seedObject



61
62
63
# File 'lib/stellar/key_pair.rb', line 61

def raw_seed
  @secret_key.to_bytes
end

#rbnacl_signing_keyObject



65
66
67
# File 'lib/stellar/key_pair.rb', line 65

def rbnacl_signing_key
  @secret_key
end

#rbnacl_verify_keyObject



69
70
71
# File 'lib/stellar/key_pair.rb', line 69

def rbnacl_verify_key
  @public_key
end

#seedObject



78
79
80
81
82
83
# File 'lib/stellar/key_pair.rb', line 78

def seed
  raise "no private key" if @secret_key.nil?
  #TODO: improve the error class above
  seed_bytes = raw_seed
  encoder = Util::StrKey.check_encode(:seed, seed_bytes)
end

#sign(message) ⇒ Object



89
90
91
92
93
# File 'lib/stellar/key_pair.rb', line 89

def sign(message)
  raise "no private key" if @secret_key.nil?
  #TODO: improve the error class above
  @secret_key.sign(message)
end

#sign?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/stellar/key_pair.rb', line 85

def sign?
  !@secret_key.nil?
end

#sign_decorated(message) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/stellar/key_pair.rb', line 95

def sign_decorated(message)
  raw_signature = sign(message)
  Stellar::DecoratedSignature.new({
    hint:      signature_hint,
    signature: raw_signature
  })
end

#signature_hintObject



56
57
58
59
# File 'lib/stellar/key_pair.rb', line 56

def signature_hint
  # take last 4 bytes
  .to_xdr.slice(-4, 4)
end

#verify(signature, message) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/stellar/key_pair.rb', line 103

def verify(signature, message)
  @public_key.verify(signature, message)
rescue RbNaCl::LengthError
  false
rescue RbNaCl::BadSignatureError
  false
end