Add ::Helpers module

This commit is contained in:
Netscape Navigator 2020-03-27 07:07:48 -05:00
parent 3226b1515c
commit 17d863eb22
6 changed files with 33 additions and 15 deletions

View File

@ -36,7 +36,11 @@ Eg: `pigeon identity show` becomes `./pigeon-cli show`.
- [X] pigeon message find
- [X] pigeon message find-all for local feed.
- [ ] pigeon bundle consume (We are minimally feature complete at this point)
- [ ] Fix the diagram in the spec document
- [ ] Put all the [HEADER, string, FOOTER].join("") nonsense into Pigeon::Helpers
- [ ] Change all the `{40,90}` values in ::Lexer to real length values
- [ ] Update the bundles.md document once `bundle consume` works.
- [ ] Add `.pigeon` file extensions
- [ ] Rename `message find` to `message read`, since other finders return a multihash.
- [ ] Don't allow carriage return in `kind`. Write a test for this.
- [ ] Create regexes in ::Lexer using strings and Regexp.new() for cleaner regexes.

21
dist/pigeon.rb vendored
View File

@ -7,7 +7,6 @@ require "securerandom"
require "pry"
module Pigeon
HEADER, FOOTER = ["@", ".ed25519"]
SEED_CONFIG_KEY = "SEED"
VERSION = "0.0.1"
TPL_DIR = "views"
@ -45,6 +44,7 @@ module Pigeon
MESSAGE_SIGIL = "%"
IDENTITY_SIGIL = "@"
STRING_SIGIL = "\""
IDENTITY_FOOTER = ".ed25519"
BLOB_FOOTER = ".sha256"
SIG_FOOTER = ".sig.ed25519"
@ -53,6 +53,25 @@ module Pigeon
NO_DRAFT_FOUND = "NO DRAFT FOUND"
STRING_KEYS_ONLY = "String keys only"
MISSING_BODY = "BODY CANT BE EMPTY"
# Constants for internal use only:
FOOTERS_REGEX = Regexp.new("#{SIG_FOOTER}|#{IDENTITY_FOOTER}")
SIG_RANGE = (SIG_FOOTER.length * -1)..-1
# /Constants for internal use only
class Helpers
def self.decode_multihash(string)
case string[0]
when BLOB_SIGIL, MESSAGE_SIGIL, IDENTITY_SIGIL
inner = string[1..-1].gsub(FOOTERS_REGEX, "")
Base64.urlsafe_decode64(inner)
else
if signature[SIG_RANGE] == SIG_FOOTER
Base64.urlsafe_decode64(signature.gsub(SIG_FOOTER, ""))
end
end
end
end
end
require_relative File.join("pigeon", "local_identity.rb")

View File

@ -34,7 +34,7 @@ module Pigeon
bytes = @signing_key.verify_key.to_bytes
b64 = Base64.urlsafe_encode64(bytes)
@public_key ||= [HEADER, b64, FOOTER].join("")
@public_key ||= [IDENTITY_SIGIL, b64, IDENTITY_FOOTER].join("")
end
def sign(string)
@ -51,11 +51,5 @@ module Pigeon
Pigeon::Storage.current.set_config(SEED_CONFIG_KEY, @seed)
self
end
private
def self.strip_headers(identity)
identity.sub(HEADER, "").sub(FOOTER, "")
end
end
end

View File

@ -7,9 +7,10 @@ module Pigeon
# changes.
class RemoteIdentity
attr_reader :public_key
def initialize(multihash)
b64 = Base64.urlsafe_encode64(multihash)
@public_key = [HEADER, b64, FOOTER].join("")
@public_key = [IDENTITY_SIGIL, b64, IDENTITY_FOOTER].join("")
end
def verify(signature, string)

View File

@ -93,7 +93,7 @@ module Pigeon
end
def add_peer(identity)
path = LocalIdentity.strip_headers(identity)
path = Helpers.decode_multihash(identity)
store.transaction do
store[PEER_NS].add(identity)
end
@ -101,7 +101,7 @@ module Pigeon
end
def remove_peer(identity)
path = LocalIdentity.strip_headers(identity)
path = Helpers.decode_multihash(identity)
store.transaction do
store[PEER_NS].delete(identity)
end

View File

@ -25,12 +25,12 @@ RSpec.describe Pigeon::LocalIdentity do
it "strips headers" do
whatever = "af697f3063d46fe9546f651c08c378f8"
example = [
Pigeon::HEADER,
Pigeon::IDENTITY_SIGIL,
whatever,
Pigeon::FOOTER,
Pigeon::IDENTITY_FOOTER,
].join("")
result = Pigeon::LocalIdentity.strip_headers(example)
expect(result).to eq(whatever)
result = Pigeon::Helpers.decode_multihash(example)
expect(result).to eq(Base64.urlsafe_decode64(whatever))
end
it "caches LocalIdentity.current" do