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
|
@ -4,6 +4,7 @@ module Pigeon
|
|||
# changes.
|
||||
class KeyPair
|
||||
HEADER, FOOTER = ["@", ".ed25519"]
|
||||
SEED_CONFIG_KEY = "SEED"
|
||||
|
||||
def self.strip_headers(identity)
|
||||
identity.sub(HEADER, "").sub(FOOTER, "")
|
||||
|
@ -13,6 +14,11 @@ module Pigeon
|
|||
[HEADER, urlsafe_b64_no_headers, FOOTER].join("")
|
||||
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
|
||||
# the key should be derived
|
||||
def initialize(seed = SecureRandom.random_bytes(Ed25519::KEY_SIZE))
|
||||
|
@ -31,17 +37,8 @@ module Pigeon
|
|||
@public_key ||= KeyPair.add_headers(b64)
|
||||
end
|
||||
|
||||
def to_h
|
||||
{
|
||||
public_key: public_key,
|
||||
private_key: private_key,
|
||||
}
|
||||
end
|
||||
|
||||
def save!
|
||||
self.to_h.map do |k, v|
|
||||
Pigeon::Storage.current.set_conf(k, v)
|
||||
end
|
||||
Pigeon::Storage.current.set_conf(SEED_CONFIG_KEY, @seed)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -17,9 +17,9 @@ module Pigeon
|
|||
@body = body
|
||||
end
|
||||
|
||||
def self.create(author:, kind:, previous: nil, body: [])
|
||||
def self.create(kind:, previous: nil, body: [])
|
||||
# instantiate
|
||||
msg = self.new(author: author,
|
||||
msg = self.new(author: KeyPair.current.public_key,
|
||||
kind: kind,
|
||||
previous: previous,
|
||||
body: body)
|
||||
|
|
|
@ -27,6 +27,7 @@ module Pigeon
|
|||
def set_conf(key, value)
|
||||
path = conf_path_for(key)
|
||||
maybe_touch(path)
|
||||
|
||||
File.write(path, value.to_s)
|
||||
end
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ module Pigeon
|
|||
desc "show", "Prints a base64 identiy string to STDOUT"
|
||||
|
||||
def show
|
||||
puts Pigeon::Storage.current.get_conf("public_key")
|
||||
puts Pigeon::KeyPair.current.public_key
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -4,10 +4,6 @@ RSpec.describe Pigeon::KeyPair do
|
|||
FAKE_SEED = "\x15\xB1\xA8\x1D\xE1\x1Cx\xF0" \
|
||||
"\xC6\xDCK\xDE\x9A\xB7>\x86o\x92\xEF\xB7\x17" \
|
||||
")\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) }
|
||||
|
||||
it "generates a pair from a seed" do
|
||||
|
@ -28,14 +24,18 @@ RSpec.describe Pigeon::KeyPair do
|
|||
expect(result).to eq(whatever)
|
||||
end
|
||||
|
||||
it "converts to a Hash" do
|
||||
expect(kp.to_h).to eq(TO_H)
|
||||
end
|
||||
|
||||
it "saves to disk" do
|
||||
TO_H.to_a.map do |pair|
|
||||
expect(Pigeon::Storage.current).to receive(:set_conf).with(*pair)
|
||||
argss = [
|
||||
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
|
||||
kp.save!
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue