From 999af42410789b98064d16a9ca3630843b622880 Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Sat, 12 Oct 2019 15:25:17 -0500 Subject: [PATCH] Derive pub/priv key from stored seed. Co-authored-by: Michael Christenson II --- dist/pigeon/key_pair.rb | 17 +++++++---------- dist/pigeon/message.rb | 4 ++-- dist/pigeon/storage.rb | 1 + pigeon-cli | 2 +- spec/pigeon/key_pair_spec.rb | 22 +++++++++++----------- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/dist/pigeon/key_pair.rb b/dist/pigeon/key_pair.rb index 408cf60..a64ae78 100644 --- a/dist/pigeon/key_pair.rb +++ b/dist/pigeon/key_pair.rb @@ -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 diff --git a/dist/pigeon/message.rb b/dist/pigeon/message.rb index bed4614..f69018e 100644 --- a/dist/pigeon/message.rb +++ b/dist/pigeon/message.rb @@ -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) diff --git a/dist/pigeon/storage.rb b/dist/pigeon/storage.rb index 9c2e044..0814268 100644 --- a/dist/pigeon/storage.rb +++ b/dist/pigeon/storage.rb @@ -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 diff --git a/pigeon-cli b/pigeon-cli index 104d548..d337af7 100755 --- a/pigeon-cli +++ b/pigeon-cli @@ -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 diff --git a/spec/pigeon/key_pair_spec.rb b/spec/pigeon/key_pair_spec.rb index 115a2e4..f06843a 100644 --- a/spec/pigeon/key_pair_spec.rb +++ b/spec/pigeon/key_pair_spec.rb @@ -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