Pigeon-Ruby/lib/pigeon/local_identity.rb

34 lines
831 B
Ruby
Raw Normal View History

2019-09-22 02:30:03 +00:00
module Pigeon
# A public and private key pair that represents the local
# user who "owns" the database.
#
# Provides a wrapper around the `ed25519` gem to
2019-09-22 02:30:03 +00:00
# help us maintain our sanity when the Gem's API
# changes.
class LocalIdentity
2019-09-22 02:30:03 +00:00
# `seed` is a 32-byte seed value from which
# the key should be derived
def initialize(seed)
2019-09-22 02:30:03 +00:00
@seed = seed
2020-03-12 12:34:22 +00:00
@signing_key = Ed25519::SigningKey.new(@seed)
2019-09-22 02:30:03 +00:00
end
def private_key
2020-04-06 00:38:37 +00:00
@private_key ||= Helpers.b32_encode(@seed)
2019-09-22 02:30:03 +00:00
end
def multihash
2020-03-12 12:34:22 +00:00
bytes = @signing_key.verify_key.to_bytes
2020-04-06 00:38:37 +00:00
b64 = Helpers.b32_encode(bytes)
2019-09-22 02:30:03 +00:00
@multihash ||= [IDENTITY_SIGIL, b64, IDENTITY_FOOTER].join("")
2019-09-22 02:30:03 +00:00
end
2019-10-21 02:11:16 +00:00
def sign(string)
2020-03-12 12:34:22 +00:00
hex = @signing_key.sign(string)
2020-04-06 00:38:37 +00:00
b64 = Helpers.b32_encode(hex)
2020-04-25 15:11:25 +00:00
b64 + SIG_FOOTER
2019-10-21 02:11:16 +00:00
end
2019-09-22 02:30:03 +00:00
end
end