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.



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

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::Base58.stellar.check_decode(:account_id, address)
  from_public_key(pk_bytes)
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::Base58.stellar.check_decode(:seed, seed)
  from_raw_seed seed_bytes
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

#addressObject



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

def address
  pk_bytes = public_key
  Util::Base58.stellar.check_encode(:account_id, pk_bytes)
end

#public_keyObject



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

def public_key
  @public_key.to_bytes
end

#public_key_hintObject



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

def public_key_hint
  public_key.slice(0, 4)
end

#raw_seedObject



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

def raw_seed
  @secret_key.to_bytes
end

#rbnacl_signing_keyObject



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

def rbnacl_signing_key
  @secret_key
end

#rbnacl_verify_keyObject



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

def rbnacl_verify_key
  @public_key
end

#seedObject



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

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

#sign(message) ⇒ Object



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

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)


67
68
69
# File 'lib/stellar/key_pair.rb', line 67

def sign?
  !@secret_key.nil?
end

#sign_decorated(message) ⇒ Object



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

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

#verify(signature, message) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/stellar/key_pair.rb', line 85

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