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.

Parameters:

  • public_key (RbNaCl::VerifyKey)
  • secret_key (RbNaCl::SigningKey, nil) (defaults to: nil)

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

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

Instance Method Details

#account_idObject


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

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

#addressObject


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

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

#muxed_accountObject


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

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

#public_keyObject


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

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

#raw_public_keyObject


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

def raw_public_key
  @public_key.to_bytes
end

#raw_seedObject


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

def raw_seed
  raise "no private key" if @secret_key.nil?
  @secret_key.to_bytes
end

#rbnacl_signing_keyObject


88
89
90
# File 'lib/stellar/key_pair.rb', line 88

def rbnacl_signing_key
  @secret_key
end

#rbnacl_verify_keyObject


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

def rbnacl_verify_key
  @public_key
end

#seedObject


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

def seed
  Util::StrKey.check_encode(:seed, raw_seed)
end

#sign(message) ⇒ Object

Raises:

  • (NotImplementedError)

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

def sign(message)
  raise NotImplementedError, "no private key, signing is not available" unless sign?

  @secret_key.sign(message)
end

#sign?Boolean

Returns:

  • (Boolean)

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

def sign?
  !@secret_key.nil?
end

#sign_decorated(message) ⇒ Object


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

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

#signature_hintObject


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

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

#signer_keyObject


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

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

#to_keypairObject


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

def to_keypair
  self
end

#verify(signature, message) ⇒ Object


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

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