Class: Stellar::KeyPair

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

Defined Under Namespace

Modules: FactoryMethods

Instance Method Summary collapse

Methods included from FactoryMethods

from_address, from_network_passphrase, from_public_key, from_raw_seed, from_seed, master, random

Constructor Details

#initialize(public_key, secret_key = nil) ⇒ KeyPair

Returns a new instance of KeyPair.



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

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

Instance Method Details

#account_idObject



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

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

#addressObject



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

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

#muxed_accountObject



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

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

#public_keyObject



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

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

#raw_public_keyObject



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

def raw_public_key
  @public_key.to_bytes
end

#raw_seedObject



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

def raw_seed
  @secret_key.to_bytes
end

#rbnacl_signing_keyObject



77
78
79
# File 'lib/stellar/key_pair.rb', line 77

def rbnacl_signing_key
  @secret_key
end

#rbnacl_verify_keyObject



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

def rbnacl_verify_key
  @public_key
end

#seedObject



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

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

#sign(message) ⇒ Object



101
102
103
104
105
# File 'lib/stellar/key_pair.rb', line 101

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)


97
98
99
# File 'lib/stellar/key_pair.rb', line 97

def sign?
  !@secret_key.nil?
end

#sign_decorated(message) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/stellar/key_pair.rb', line 107

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

#signature_hintObject



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

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

#signer_keyObject



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

def signer_key
  Stellar::SignerKey.new :signer_key_type_ed25519, raw_public_key
end

#to_keypairObject



123
124
125
# File 'lib/stellar/key_pair.rb', line 123

def to_keypair
  self
end

#verify(signature, message) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/stellar/key_pair.rb', line 115

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