lib/stellar/key_pair.rb in stellar-base-0.24.0 vs lib/stellar/key_pair.rb in stellar-base-0.25.0

- old
+ new

@@ -4,10 +4,15 @@ def from_seed(seed) seed_bytes = Util::StrKey.check_decode(:seed, seed) from_raw_seed seed_bytes end + def from_address(address) + pk_bytes = Util::StrKey.check_decode(:account_id, address) + from_public_key(pk_bytes) + end + def from_raw_seed(seed_bytes) secret_key = RbNaCl::SigningKey.new(seed_bytes) public_key = secret_key.verify_key new(public_key, secret_key) end @@ -15,15 +20,10 @@ def from_public_key(pk_bytes) public_key = RbNaCl::VerifyKey.new(pk_bytes) new(public_key) end - def from_address(address) - pk_bytes = Util::StrKey.check_decode(:account_id, address) - from_public_key(pk_bytes) - end - def random secret_key = RbNaCl::SigningKey.generate public_key = secret_key.verify_key new(public_key, secret_key) end @@ -38,15 +38,25 @@ end end extend FactoryMethods + # @param [RbNaCl::VerifyKey] public_key + # @param [RbNaCl::SigningKey, nil] secret_key def initialize(public_key, secret_key = nil) @public_key = public_key @secret_key = secret_key end + def address + Util::StrKey.check_encode(:account_id, raw_public_key) + end + + def seed + Util::StrKey.check_encode(:seed, raw_seed) + end + def account_id Stellar::AccountID.new :public_key_type_ed25519, raw_public_key end def muxed_account @@ -59,20 +69,21 @@ def signer_key Stellar::SignerKey.new :signer_key_type_ed25519, raw_public_key end - def raw_public_key - @public_key.to_bytes - end - def signature_hint # take last 4 bytes account_id.to_xdr.slice(-4, 4) end + def raw_public_key + @public_key.to_bytes + end + def raw_seed + raise "no private key" if @secret_key.nil? @secret_key.to_bytes end def rbnacl_signing_key @secret_key @@ -80,28 +91,16 @@ def rbnacl_verify_key @public_key end - def address - pk_bytes = raw_public_key - Util::StrKey.check_encode(:account_id, pk_bytes) - end - - 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 - def sign? !@secret_key.nil? end def sign(message) - raise "no private key" if @secret_key.nil? - # TODO: improve the error class above + raise NotImplementedError, "no private key, signing is not available" unless sign? + @secret_key.sign(message) end def sign_decorated(message) raw_signature = sign(message)