Skip to content

Commit

Permalink
feat: remove libsodium (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach authored Sep 3, 2022
1 parent 3893e0b commit 5d96e40
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 32 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ jobs:
matrix:
stable: [true]
crystal:
- 1.2.2
- 1.3.2
- latest
include:
- crystal: nightly
stable: false
Expand Down
13 changes: 4 additions & 9 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: placeos-pulse
version: 0.13.2
version: 0.14.0
crystal: ">= 1.1.1"
license: MIT
authors:
Expand All @@ -11,10 +11,6 @@ dependencies:
hashcash:
github: place-labs/hashcash

openapi-generator:
github: place-labs/openapi-generator
branch: master

placeos-models:
github: placeos/models
version: ">= 6.0"
Expand All @@ -28,17 +24,16 @@ dependencies:
secrets-env:
github: spider-gazelle/secrets-env

sodium:
github: place-labs/sodium.cr
branch: fix/aead-chalsa-signature

tasker:
github: spider-gazelle/tasker
version: ~> 2.0

ulid:
github: place-labs/ulid

ed25519:
github: spider-gazelle/ed25519

development_dependencies:
ameba:
github: crystal-ameba/ameba
Expand Down
4 changes: 2 additions & 2 deletions spec/pulse/message_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ module PlaceOS::Pulse
describe Message do
describe "#signature" do
it "returns a signature of the message's contents" do
message.signature.should eq private_key.sign_detached(register_message.to_json).hexstring
message.signature.should eq private_key.sign(register_message.to_json).hexstring
end
end

describe ".verify_signature" do
it "verifies the message signature" do
PlaceOS::Pulse::Message.verify_signature(private_key.public_key.to_slice.hexstring, register_message.to_json, message.signature).should be_nil
PlaceOS::Pulse::Message.verify_signature(private_key.verify_key.to_slice.hexstring, register_message.to_json, message.signature).should be_nil
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/spec_constants.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module PlaceOS::Pulse
API_BASE = "#{PLACE_PORTAL_URI}/api/portal/v1"
ROUTE_BASE = "/api/portal/v1/"

class_getter private_key : Sodium::Sign::SecretKey do
Sodium::Sign::SecretKey.new(MOCK_PRIVATE_KEY.hexbytes)
class_getter private_key : Ed25519::SigningKey do
Ed25519::SigningKey.new(MOCK_PRIVATE_KEY.hexbytes)
end

class_getter register_message : Register do
Expand All @@ -18,7 +18,7 @@ module PlaceOS::Pulse
name: MOCK_INSTANCE_NAME,
email: MOCK_INSTANCE_EMAIL,
instance_id: MOCK_INSTANCE_ID,
public_key: private_key.public_key.to_slice.hexstring
public_key: private_key.verify_key.to_slice.hexstring
)
end

Expand Down
12 changes: 6 additions & 6 deletions src/placeos-pulse/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require "http/client"
require "json"
require "tasker"
require "ulid"
require "sodium"
require "ed25519"

require "responsible"

Expand All @@ -28,7 +28,7 @@ module PlaceOS::Pulse
private getter heartbeat_task : Tasker::Repeat(HTTP::Client::Response)?
private getter heartbeat_interval : Time::Span

private getter private_key : Sodium::Sign::SecretKey
private getter private_key : Ed25519::SigningKey

getter api_base : String

Expand All @@ -37,7 +37,7 @@ module PlaceOS::Pulse
def initialize(
@instance_token : String?,
@email : String,
private_key : String | Sodium::Sign::SecretKey,
private_key : String | Ed25519::SigningKey,
@instance_id : String = ULID.generate,
@saas : Bool = false,
portal_uri : String = PLACE_PORTAL_URI,
Expand All @@ -46,8 +46,8 @@ module PlaceOS::Pulse
@heartbeat_interval : Time::Span = 6.hours
)
@private_key = case private_key
in Sodium::Sign::SecretKey then private_key
in String then Sodium::Sign::SecretKey.new(private_key.hexbytes)
in Ed25519::SigningKey then private_key
in String then Ed25519::SigningKey.new(private_key.hexbytes)
end

@api_base = File.join(portal_uri, ROUTE_BASE)
Expand Down Expand Up @@ -80,7 +80,7 @@ module PlaceOS::Pulse
name: @name,
email: email,
instance_id: instance_id,
public_key: @private_key.public_key.to_slice.hexstring
public_key: @private_key.verify_key.to_slice.hexstring
)

post("/register", RegisterRequest.new(instance_id, saas?, register_message, @private_key))
Expand Down
15 changes: 5 additions & 10 deletions src/placeos-pulse/message.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require "json"
require "sodium"
require "openapi-generator/serializable"

require "ed25519"
require "./constants"
require "./request"
require "./response"
Expand All @@ -19,28 +17,25 @@ module PlaceOS::Pulse
@instance_id : String,
@saas : Bool,
@message : T,
private_key : Sodium::Sign::SecretKey
private_key : Ed25519::SigningKey
)
@signature = private_key.sign_detached(@message.to_json).hexstring
@signature = private_key.sign(@message.to_json).hexstring
end

def self.verify_signature(public_key : String, message : String, signature : String)
Sodium::Sign::PublicKey.new(public_key.hexbytes).verify_detached(message, signature.hexbytes)
Ed25519::VerifyKey.new(public_key.hexbytes).verify!(signature.hexbytes, message)
end
end

# Generate classes as using the generic Message(T) struct directly does not work with
# elbywan's `openapi-generator`.
# Generate classes as using the generic Message(T) struct
{% begin %}
{% for request in Request.subclasses %}
struct {{ request.name }}Request < Message({{ request }})
extend OpenAPI::Generator::Serializable
end
{% end %}

{% for response in Response.subclasses %}
struct {{ request.name }}Response < Message({{ response }})
extend OpenAPI::Generator::Serializable
end
{% end %}
{% end %}
Expand Down
2 changes: 2 additions & 0 deletions src/placeos-pulse/request/heartbeat.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ module PlaceOS::Pulse

def self.feature_count
PlaceOS::Model::Metadata.all.each_with_object(Hash(Feature, Int32).new(0)) do |metadata, count|
# ignore historic versions of the metadata
next if metadata.is_version?
# Select for Zone metadata
next unless metadata.parent_id.try(&.starts_with? PlaceOS::Model::Zone.table_name)
# Select for valid feature name
Expand Down

0 comments on commit 5d96e40

Please sign in to comment.