Class: Stellar::KeyPair
- Inherits:
-
Object
- Object
- Stellar::KeyPair
- Defined in:
- lib/stellar/key_pair.rb
Class Method Summary collapse
- .from_address(address) ⇒ Object
- .from_network_passphrase(passphrase) ⇒ Object
- .from_public_key(pk_bytes) ⇒ Object
- .from_raw_seed(seed_bytes) ⇒ Object
- .from_seed(seed) ⇒ Object
- .master ⇒ Object
- .random ⇒ Object
Instance Method Summary collapse
- #account_id ⇒ Object
- #address ⇒ Object
-
#initialize(public_key, secret_key = nil) ⇒ KeyPair
constructor
A new instance of KeyPair.
- #public_key ⇒ Object
- #raw_public_key ⇒ Object
- #raw_seed ⇒ Object
- #rbnacl_signing_key ⇒ Object
- #rbnacl_verify_key ⇒ Object
- #seed ⇒ Object
- #sign(message) ⇒ Object
- #sign? ⇒ Boolean
- #sign_decorated(message) ⇒ Object
- #signature_hint ⇒ Object
- #verify(signature, message) ⇒ Object
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 |
.master ⇒ Object
35 36 37 |
# File 'lib/stellar/key_pair.rb', line 35 def self.master from_raw_seed(Stellar.current_network_id) 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
#account_id ⇒ Object
44 45 46 |
# File 'lib/stellar/key_pair.rb', line 44 def account_id Stellar::AccountID.new :key_type_ed25519, raw_public_key end |
#address ⇒ Object
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_key ⇒ Object
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_key ⇒ Object
52 53 54 |
# File 'lib/stellar/key_pair.rb', line 52 def raw_public_key @public_key.to_bytes end |
#raw_seed ⇒ Object
61 62 63 |
# File 'lib/stellar/key_pair.rb', line 61 def raw_seed @secret_key.to_bytes end |
#rbnacl_signing_key ⇒ Object
65 66 67 |
# File 'lib/stellar/key_pair.rb', line 65 def rbnacl_signing_key @secret_key end |
#rbnacl_verify_key ⇒ Object
69 70 71 |
# File 'lib/stellar/key_pair.rb', line 69 def rbnacl_verify_key @public_key end |
#seed ⇒ Object
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() raise "no private key" if @secret_key.nil? #TODO: improve the error class above @secret_key.sign() end |
#sign? ⇒ 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() raw_signature = sign() Stellar::DecoratedSignature.new({ hint: signature_hint, signature: raw_signature }) end |
#signature_hint ⇒ Object
56 57 58 59 |
# File 'lib/stellar/key_pair.rb', line 56 def signature_hint # take last 4 bytes account_id.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, ) @public_key.verify(signature, ) rescue RbNaCl::LengthError false rescue RbNaCl::BadSignatureError false end |