Class: Stellar::KeyPair
- Inherits:
-
Object
- Object
- Stellar::KeyPair
- Defined in:
- lib/stellar/key_pair.rb
Class Method Summary collapse
- .from_address(address) ⇒ Object
- .from_public_key(pk_bytes) ⇒ Object
- .from_raw_seed(seed_bytes) ⇒ Object
- .from_seed(seed) ⇒ Object
- .random ⇒ Object
Instance Method Summary collapse
- #address ⇒ Object
-
#initialize(public_key, secret_key = nil) ⇒ KeyPair
constructor
A new instance of KeyPair.
- #public_key ⇒ Object
- #public_key_hint ⇒ Object
- #raw_seed ⇒ Object
- #rbnacl_signing_key ⇒ Object
- #rbnacl_verify_key ⇒ Object
- #seed ⇒ Object
- #sign(message) ⇒ Object
- #sign? ⇒ Boolean
- #sign_decorated(message) ⇒ Object
- #verify(signature, message) ⇒ Object
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 |
.random ⇒ Object
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
#address ⇒ Object
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_key ⇒ Object
35 36 37 |
# File 'lib/stellar/key_pair.rb', line 35 def public_key @public_key.to_bytes end |
#public_key_hint ⇒ Object
39 40 41 |
# File 'lib/stellar/key_pair.rb', line 39 def public_key_hint public_key.slice(0, 4) end |
#raw_seed ⇒ Object
43 44 45 |
# File 'lib/stellar/key_pair.rb', line 43 def raw_seed @secret_key.to_bytes end |
#rbnacl_signing_key ⇒ Object
47 48 49 |
# File 'lib/stellar/key_pair.rb', line 47 def rbnacl_signing_key @secret_key end |
#rbnacl_verify_key ⇒ Object
51 52 53 |
# File 'lib/stellar/key_pair.rb', line 51 def rbnacl_verify_key @public_key end |
#seed ⇒ Object
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() raise "no private key" if @secret_key.nil? #TODO: improve the error class above @secret_key.sign() end |
#sign? ⇒ 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() raw_signature = sign() 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, ) @public_key.verify(signature, ) rescue RbNaCl::LengthError false rescue RbNaCl::BadSignatureError false end |