Derive pub/priv key from stored seed.
Co-authored-by: Michael Christenson II <m3talsmith@gmail.com>
This commit is contained in:
parent
9953bcee2c
commit
999af42410
17
dist/pigeon/key_pair.rb
vendored
17
dist/pigeon/key_pair.rb
vendored
|
@ -4,6 +4,7 @@ module Pigeon
|
||||||
# changes.
|
# changes.
|
||||||
class KeyPair
|
class KeyPair
|
||||||
HEADER, FOOTER = ["@", ".ed25519"]
|
HEADER, FOOTER = ["@", ".ed25519"]
|
||||||
|
SEED_CONFIG_KEY = "SEED"
|
||||||
|
|
||||||
def self.strip_headers(identity)
|
def self.strip_headers(identity)
|
||||||
identity.sub(HEADER, "").sub(FOOTER, "")
|
identity.sub(HEADER, "").sub(FOOTER, "")
|
||||||
|
@ -13,6 +14,11 @@ module Pigeon
|
||||||
[HEADER, urlsafe_b64_no_headers, FOOTER].join("")
|
[HEADER, urlsafe_b64_no_headers, FOOTER].join("")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.current
|
||||||
|
storage = Pigeon::Storage.current
|
||||||
|
self.new(storage.get_conf(SEED_CONFIG_KEY))
|
||||||
|
end
|
||||||
|
|
||||||
# `seed` is a 32-byte seed value from which
|
# `seed` is a 32-byte seed value from which
|
||||||
# the key should be derived
|
# the key should be derived
|
||||||
def initialize(seed = SecureRandom.random_bytes(Ed25519::KEY_SIZE))
|
def initialize(seed = SecureRandom.random_bytes(Ed25519::KEY_SIZE))
|
||||||
|
@ -31,17 +37,8 @@ module Pigeon
|
||||||
@public_key ||= KeyPair.add_headers(b64)
|
@public_key ||= KeyPair.add_headers(b64)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_h
|
|
||||||
{
|
|
||||||
public_key: public_key,
|
|
||||||
private_key: private_key,
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def save!
|
def save!
|
||||||
self.to_h.map do |k, v|
|
Pigeon::Storage.current.set_conf(SEED_CONFIG_KEY, @seed)
|
||||||
Pigeon::Storage.current.set_conf(k, v)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
4
dist/pigeon/message.rb
vendored
4
dist/pigeon/message.rb
vendored
|
@ -17,9 +17,9 @@ module Pigeon
|
||||||
@body = body
|
@body = body
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.create(author:, kind:, previous: nil, body: [])
|
def self.create(kind:, previous: nil, body: [])
|
||||||
# instantiate
|
# instantiate
|
||||||
msg = self.new(author: author,
|
msg = self.new(author: KeyPair.current.public_key,
|
||||||
kind: kind,
|
kind: kind,
|
||||||
previous: previous,
|
previous: previous,
|
||||||
body: body)
|
body: body)
|
||||||
|
|
1
dist/pigeon/storage.rb
vendored
1
dist/pigeon/storage.rb
vendored
|
@ -27,6 +27,7 @@ module Pigeon
|
||||||
def set_conf(key, value)
|
def set_conf(key, value)
|
||||||
path = conf_path_for(key)
|
path = conf_path_for(key)
|
||||||
maybe_touch(path)
|
maybe_touch(path)
|
||||||
|
|
||||||
File.write(path, value.to_s)
|
File.write(path, value.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@ module Pigeon
|
||||||
desc "show", "Prints a base64 identiy string to STDOUT"
|
desc "show", "Prints a base64 identiy string to STDOUT"
|
||||||
|
|
||||||
def show
|
def show
|
||||||
puts Pigeon::Storage.current.get_conf("public_key")
|
puts Pigeon::KeyPair.current.public_key
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -4,10 +4,6 @@ RSpec.describe Pigeon::KeyPair do
|
||||||
FAKE_SEED = "\x15\xB1\xA8\x1D\xE1\x1Cx\xF0" \
|
FAKE_SEED = "\x15\xB1\xA8\x1D\xE1\x1Cx\xF0" \
|
||||||
"\xC6\xDCK\xDE\x9A\xB7>\x86o\x92\xEF\xB7\x17" \
|
"\xC6\xDCK\xDE\x9A\xB7>\x86o\x92\xEF\xB7\x17" \
|
||||||
")\xFF\x01E\b$b)\xC9\x82\b"
|
")\xFF\x01E\b$b)\xC9\x82\b"
|
||||||
TO_H = {
|
|
||||||
private_key: "FbGoHeEcePDG3Evemrc-hm-S77cXKf8BRQgkYinJggg=",
|
|
||||||
public_key: "@7n_g0ca9FFWvMkXy2TMwM7bdMn6tNiEHKzrFX-CzAmQ=.ed25519",
|
|
||||||
}
|
|
||||||
let(:kp) { Pigeon::KeyPair.new(FAKE_SEED) }
|
let(:kp) { Pigeon::KeyPair.new(FAKE_SEED) }
|
||||||
|
|
||||||
it "generates a pair from a seed" do
|
it "generates a pair from a seed" do
|
||||||
|
@ -28,14 +24,18 @@ RSpec.describe Pigeon::KeyPair do
|
||||||
expect(result).to eq(whatever)
|
expect(result).to eq(whatever)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "converts to a Hash" do
|
|
||||||
expect(kp.to_h).to eq(TO_H)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "saves to disk" do
|
it "saves to disk" do
|
||||||
TO_H.to_a.map do |pair|
|
argss = [
|
||||||
expect(Pigeon::Storage.current).to receive(:set_conf).with(*pair)
|
Pigeon::KeyPair::SEED_CONFIG_KEY,
|
||||||
|
FAKE_SEED,
|
||||||
|
]
|
||||||
|
FakeFS.with_fresh do
|
||||||
|
lol = receive(:set_conf).with(*argss).and_call_original
|
||||||
|
expect(Pigeon::Storage.current).to lol
|
||||||
|
kp.save!
|
||||||
|
new_kp = Pigeon::KeyPair.current
|
||||||
|
expect(new_kp.public_key).to eq(kp.public_key)
|
||||||
|
expect(new_kp.private_key).to eq(kp.private_key)
|
||||||
end
|
end
|
||||||
kp.save!
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user