Skip to content

Commit

Permalink
refactoring enlist!
Browse files Browse the repository at this point in the history
  • Loading branch information
Janis Erdmanis committed Feb 16, 2024
1 parent 054a20b commit 0fa646b
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 230 deletions.
2 changes: 1 addition & 1 deletion Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

julia_version = "1.10.0"
manifest_format = "2.0"
project_hash = "b6587dab63f4ddbf3260cd751519cd41715c44d9"
project_hash = "82fc55dbc8bb3715435e026b7e5738b71f4871cd"

[[deps.ArgTools]]
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"
Expand Down
1 change: 0 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
Nettle = "49dea1ee-f6fa-5aa6-9a11-8816cee7d4b9"
Oxygen = "df9a0d86-3283-4920-82dc-4555fc0d1d8b"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
RelocatableFolders = "05181044-ff0b-4ac5-8273-598c1e38db00"
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
ShuffleProofs = "31a120cc-b3cb-4d07-bbdb-d498660ddfd8"
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
Expand Down
60 changes: 15 additions & 45 deletions src/Client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using ..Model
using ..Model: Member, Pseudonym, Proposal, Vote, bytes, TicketID, HMAC, Admission, isbinding, verify, Digest, Hash, AckConsistency, AckInclusion, CastAck, DemeSpec, Signer, TicketStatus, Commit, ChainState, Proposal, BallotBoxState, isbinding, isopen
using Base: UUID

using ..Model: id, hasher, pseudonym, isbinding, generator, isadmitted, state, verify, crypto, index, root, commit, isconsistent, istallied, issuer
using ..Model: id, hasher, pseudonym, isbinding, generator, isadmitted, state, verify, crypto, index, root, commit, isconsistent, istallied, issuer, Invite

using HTTP: Router, Request, Response, Handler, HTTP, iserror, StatusError

Expand Down Expand Up @@ -80,26 +80,27 @@ function get_deme(server::Route)
end


function enlist_ticket(server::Route, ticketid::TicketID, hmac::HMAC; dest = destination(server))
# REFACTOR: needs a special HMAC authetification at Service layer
# function enlist_ticket(server::Route, ticketid::TicketID, hmac::HMAC; dest = destination(server))

timestamp = Dates.now()
ticket_auth_code = Model.auth(ticketid, timestamp, hmac)
body = marshal((ticketid, timestamp, ticket_auth_code))
# timestamp = Dates.now()
# ticket_auth_code = Model.auth(ticketid, timestamp, hmac)
# body = marshal((ticketid, timestamp, ticket_auth_code))

response = post(server, "/tickets", body)
# response = post(server, "/tickets", body)

metadata, salt, reply_auth_code = unmarshal(response.body, Tuple{Vector{UInt8}, Vector{UInt8}, Digest})
# metadata, salt, reply_auth_code = unmarshal(response.body, Tuple{Vector{UInt8}, Vector{UInt8}, Digest})

@assert isbinding(metadata, ticketid, salt, reply_auth_code, hmac)
# @assert isbinding(metadata, ticketid, salt, reply_auth_code, hmac)

if salt == UInt8[]
error("TicketID with $(bytes2hex(ticketid)) is already admitted.")
end
# if salt == UInt8[]
# error("TicketID with $(bytes2hex(ticketid)) is already admitted.")
# end

invite = Invite(Digest(metadata), ticketid, Model.token(ticketid, salt, hmac), hasher(hmac), dest)
# invite = Invite(Digest(metadata), ticketid, Model.token(ticketid, salt, hmac), hasher(hmac), dest)

return invite
end
# return invite
# end


function seek_admission(server::Route, id::Pseudonym, ticketid::TicketID, token::Digest, hasher::Hash)
Expand Down Expand Up @@ -752,37 +753,6 @@ function blame(voter::DemeAccount, uuid::UUID)
end


struct Invite
demehash::Digest
ticketid::TicketID
token::Digest
hasher::Hash # HashSpec
route::URI
end

Base.:(==)(x::Invite, y::Invite) = x.demehash == y.demehash && x.ticketid == y.ticketid && x.token == y.token && x.hasher == y.hasher && x.route == y.route

# This gives a nasty error for some reason when CryptoGroups are imported.
#@batteries Invite

Model.isbinding(spec::DemeSpec, invite::Invite) = Model.digest(spec, invite.hasher) == invite.demehash

# Parsing to string and back
StructTypes.StructType(::Type{Invite}) = StructTypes.CustomStruct()

StructTypes.lower(invite::Invite) = Dict(:demehash => invite.demehash, :ticketid => invite.ticketid, :token => invite.token, :hasher => invite.hasher, :route => string(invite.route))

function StructTypes.construct(::Type{Invite}, data::Dict)

demehash = StructTypes.constructfrom(Digest, data["demehash"])
ticketid = StructTypes.constructfrom(TicketID, data["ticketid"])
token = StructTypes.constructfrom(Digest, data["token"])
hasher = StructTypes.constructfrom(Hash, data["hasher"])
route = URI(data["route"])

return Invite(demehash, ticketid, token, hasher, route)
end


# Parser.marshal, Parser.unmarshal ; Client.enroll method seems like a good fit where to do parsing

Expand Down
7 changes: 6 additions & 1 deletion src/Mapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ get_recruit_key() = Model.key(RECRUITER[])

get_deme() = BRAID_CHAIN[].spec

enlist_ticket(ticketid::TicketID, timestamp::DateTime, auth_code::Digest; expiration_time = nothing) = Model.enlist!(RECRUITER[], ticketid, timestamp, auth_code)
#enlist_ticket(ticketid::TicketID, timestamp::DateTime, auth_code::Digest; expiration_time = nothing) = Model.enlist!(RECRUITER[], ticketid, timestamp, auth_code)

enlist_ticket(ticketid::TicketID, timestamp::DateTime; expiration_time = nothing) = Model.enlist!(RECRUITER[], ticketid, timestamp)
enlist_ticket(ticketid::TicketID; expiration_time = nothing) = enlist_ticket(ticketid, Dates.now(); expiration_time)

# Useful for an admin
#delete_ticket!(ticketid::TicketID) = Model.remove!(RECRUITER[], ticketid) #

get_ticket_ids() = Model.ticket_ids(RECRUITER[])
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ end


include("crypto.jl")
include("admissions.jl")
include("braidchains.jl")
include("registrar.jl")
include("proposals.jl")
include("dealer.jl")
include("braids.jl")
Expand Down
94 changes: 88 additions & 6 deletions src/Model/braidchains.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,6 @@ isbinding(ack::AckInclusion{ChainState}, deme::DemeSpec) = issuer(ack) == deme.r

isbinding(record::Transaction, ack::AckInclusion{ChainState}, deme::DemeSpec) = isbinding(ack, deme) && isbinding(record, ack, hasher(deme))

"""
isbinding(admission::Admission, spec::DemeSpec)
Check whether issuer of `admission` is a recruiter set in `spec`.
"""
isbinding(admission::Admission, deme::DemeSpec) = issuer(admission) == deme.recruiter

isbinding(commit::Commit{ChainState}, deme::DemeSpec) = issuer(commit) == deme.recorder

Expand Down Expand Up @@ -433,6 +427,94 @@ end

members(chain::BraidChain, state::ChainState) = members(chain, state.index)


"""
struct TicketID
id::Vector{UInt8}
end
Represents a unique identifier for which a recruit tooken is issued. In case of necessity `id` can contain
a full document, for instance, registration form, proof of identity and etc. In case a privacy is an issue
the `id` can contain a unique identifier which can be matched to an identity in an external database.
"""
struct TicketID
id::Vector{UInt8}
end


bytes(ticketid::TicketID) = ticketid.id
Base.bytes2hex(ticketid::TicketID) = bytes2hex(bytes(ticketid))

Base.:(==)(x::TicketID, y::TicketID) = x.id == y.id

TicketID(x::String) = TicketID(copy(Vector{UInt8}(x)))

"""
struct Admission
ticketid::TicketID
id::Pseudonym
timestamp::DateTime
approval::Union{Seal, Nothing}
end
Represents an admission certificate for a pseudonym `id`.
**Interface:** [`approve`](@ref), [`issuer`](@ref), [`id`](@ref), [`ticket`](@ref), [`isadmitted`](@ref)
"""
struct Admission
ticketid::TicketID # document on which basis recruiter have decided to approve the member
id::Pseudonym
timestamp::DateTime # Timestamp could be used as a deadline
approval::Union{Seal, Nothing}
# demespec::Digest # To prevent malicios guardian to downgrade cryptographic parameters, set a selective route compromising anonimity. Uppon receiving admission member would test that demespec is the one as sent in the invite.
end



Admission(ticketid::TicketID, id::Pseudonym, timestamp::DateTime) = Admission(ticketid, id, timestamp, nothing)

Base.:(==)(x::Admission, y::Admission) = x.ticketid == y.ticketid && x.id == y.id && x.timestamp == y.timestamp && x.approval == y.approval

"""
isbinding(admission::Admission, spec::DemeSpec)
Check whether issuer of `admission` is a recruiter set in `spec`.
"""
isbinding(admission::Admission, deme::DemeSpec) = issuer(admission) == deme.recruiter


"""
approve(x::T, signer::Signer)::T
Cryptographically sign a document `x::T` and returns a signed document with the same type. To check whether a document
is signed see `issuer` method.
"""
approve(admission::Admission, signer::Signer) = @set admission.approval = seal(admission, signer)

issuer(admission::Admission) = isnothing(admission.approval) ? nothing : pseudonym(admission.approval)

id(admission::Admission) = admission.id

"""
ticket(x::Admission)
Return a TicketID which is admitted.
"""
ticket(admission::Admission) = admission.ticketid


function Base.show(io::IO, admission::Admission)

println(io, "Admission:")
println(io, " ticket : $(string(admission.ticketid))")
println(io, " identity : $(string(admission.id))")
println(io, " timestamp : $(admission.timestamp)")
print(io, " issuer : $(string(issuer(admission)))")

end



"""
struct Member <: Transaction
admission::Admission
Expand Down
Loading

0 comments on commit 0fa646b

Please sign in to comment.