Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Naming fixes (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
amika-sq authored Jan 10, 2024
1 parent c45ce14 commit dcad8e7
Show file tree
Hide file tree
Showing 23 changed files with 196 additions and 196 deletions.
2 changes: 1 addition & 1 deletion Sources/tbDEX/Dids/Did.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

protocol Did {
protocol DID {
var uri: String { get }
var keyManager: KeyManager { get }
}
10 changes: 5 additions & 5 deletions Sources/tbDEX/Dids/DidDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
/// repository services.
///
/// A DID Document can be retrieved by _resolving_ a DID URI
struct DidDocument: Codable, Equatable {
struct DIDDocument: Codable, Equatable {

let context: OneOrMany<String>?

Expand Down Expand Up @@ -232,7 +232,7 @@ struct VerificationMethod: Codable, Equatable {
let id: String
let type: String
let controller: String
let publicKeyJwk: Jwk?
let publicKeyJWK: JWK?
let publicKeyMultibase: String?

/// Computed property that returns the absolute ID of the verification method.
Expand All @@ -248,13 +248,13 @@ struct VerificationMethod: Codable, Equatable {
id: String,
type: String,
controller: String,
publicKeyJwk: Jwk? = nil,
publicKeyJWK: JWK? = nil,
publicKeyMultibase: String? = nil
) {
self.id = id
self.type = type
self.controller = controller
self.publicKeyJwk = publicKeyJwk
self.publicKeyJWK = publicKeyJWK
self.publicKeyMultibase = publicKeyMultibase
}
}
Expand All @@ -276,7 +276,7 @@ enum EmbeddedOrReferencedVerificationMethod: Codable, Equatable {
case embedded(VerificationMethod)
case referenced(String)

func dereferenced(with didDocument: DidDocument) -> VerificationMethod? {
func dereferenced(with didDocument: DIDDocument) -> VerificationMethod? {
switch self {
case let .embedded(verificationMethod):
return verificationMethod
Expand Down
28 changes: 14 additions & 14 deletions Sources/tbDEX/Dids/DidResolution.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Foundation

enum DidResolution {
enum DIDResolution {

/// Errors that can occur during DID resolution process
enum Error: String {
case invalidDid
case invalidDID = "invalidDid"
case methodNotSupported
case notFound
}
Expand All @@ -18,40 +18,40 @@ enum DidResolution {
///
/// This includes information about the resolution process itself, such as any errors
/// that occurred. If not provided in the constructor, it defaults to an empty
/// `DidResolution.Metadata`.
let didResolutionMetadata: DidResolution.Metadata
/// `DIDResolution.Metadata`.
let didResolutionMetadata: DIDResolution.Metadata

/// The resolved DID document, if available.
///
/// This is the document that represents the resolved state of the DID. It may be `null`
/// if the DID could not be resolved or if the document is not available.
let didDocument: DidDocument?
let didDocument: DIDDocument?

/// The metadata associated with the DID document.
///
/// This includes information about the document such as when it was created and
/// any other relevant metadata. If not provided in the constructor, it defaults to an
/// empty `DidDocument.Metadata`.
let didDocumentMetadata: DidDocument.Metadata
/// empty `DIDDocument.Metadata`.
let didDocumentMetadata: DIDDocument.Metadata

init(
didResolutionMetadata: DidResolution.Metadata = DidResolution.Metadata(),
didDocument: DidDocument? = nil,
didDocumentMetadata: DidDocument.Metadata = DidDocument.Metadata()
didResolutionMetadata: DIDResolution.Metadata = DIDResolution.Metadata(),
didDocument: DIDDocument? = nil,
didDocumentMetadata: DIDDocument.Metadata = DIDDocument.Metadata()
) {
self.didResolutionMetadata = didResolutionMetadata
self.didDocument = didDocument
self.didDocumentMetadata = didDocumentMetadata
}

/// Convenience function to create a `DidResolution.Result` with an error
/// Convenience function to create a `DIDResolution.Result` with an error
/// - Parameter error: Specific error which caused DID to not resolve
/// - Returns: DidResolution.Result with appropriate error metadata
static func resolutionError(_ error: DidResolution.Error) -> Result {
/// - Returns: DIDResolution.Result with appropriate error metadata
static func resolutionError(_ error: DIDResolution.Error) -> Result {
Result(
didResolutionMetadata: Metadata(error: error.rawValue),
didDocument: nil,
didDocumentMetadata: DidDocument.Metadata()
didDocumentMetadata: DIDDocument.Metadata()
)
}
}
Expand Down
28 changes: 14 additions & 14 deletions Sources/tbDEX/Dids/Methods/Jwk/DidJwk.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Foundation

struct DidJwk: Did {
struct DIDJWK: DID {

struct Options {
let algorithm: Jwk.Algorithm
let curve: Jwk.Curve
let algorithm: JWK.Algorithm
let curve: JWK.Curve
}

let uri: String
Expand All @@ -19,28 +19,28 @@ struct DidJwk: Did {
self.keyManager = keyManager
}

/// Resolves a `did:jwk` URI into a `DidResolution.Result`
/// Resolves a `did:jwk` URI into a `DIDResolution.Result`
/// - Parameter didUri: The DID URI to resolve
/// - Returns: `DidResolution.Result` containing the resolved DID Document.
static func resolve(didUri: String) -> DidResolution.Result {
guard let parsedDid = try? ParsedDid(didUri: didUri),
let jwk = try? JSONDecoder().decode(Jwk.self, from: try parsedDid.methodSpecificId.decodeBase64Url())
/// - Returns: `DIDResolution.Result` containing the resolved DID Document.
static func resolve(didUri: String) -> DIDResolution.Result {
guard let parsedDID = try? ParsedDID(didUri: didUri),
let jwk = try? JSONDecoder().decode(JWK.self, from: try parsedDID.methodSpecificId.decodeBase64Url())
else {
return DidResolution.Result.resolutionError(.invalidDid)
return DIDResolution.Result.resolutionError(.invalidDID)
}

guard parsedDid.methodName == "jwk" else {
return DidResolution.Result.resolutionError(.methodNotSupported)
guard parsedDID.methodName == "jwk" else {
return DIDResolution.Result.resolutionError(.methodNotSupported)
}

let verifiationMethod = VerificationMethod(
id: "\(didUri)#0",
type: "JsonWebKey2020",
controller: didUri,
publicKeyJwk: jwk
publicKeyJWK: jwk
)

let didDocument = DidDocument(
let didDocument = DIDDocument(
context: .many([
"https://www.w3.org/ns/did/v1",
"https://w3id.org/security/suites/jws-2020/v1",
Expand All @@ -53,6 +53,6 @@ struct DidJwk: Did {
capabilityInvocation: [.referenced(verifiationMethod.id)]
)

return DidResolution.Result(didDocument: didDocument)
return DIDResolution.Result(didDocument: didDocument)
}
}
26 changes: 13 additions & 13 deletions Sources/tbDEX/Dids/Methods/Web/DidWeb.swift
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import Foundation

struct DidWeb {
struct DIDWeb {

// MARK: - Public Static

/// Resolves a `did:jwk` URI into a `DidResolution.Result`
/// Resolves a `did:jwk` URI into a `DIDResolution.Result`
/// - Parameter didUri: The DID URI to resolve
/// - Returns: `DidResolution.Result` containing the resolved DID Document.
static func resolve(didUri: String) async -> DidResolution.Result {
guard let parsedDid = try? ParsedDid(didUri: didUri),
let url = getDidDocumentUrl(methodSpecificId: parsedDid.methodSpecificId)
/// - Returns: `DIDResolution.Result` containing the resolved DID Document.
static func resolve(didUri: String) async -> DIDResolution.Result {
guard let parsedDID = try? ParsedDID(didUri: didUri),
let url = getDIDDocumentUrl(methodSpecificId: parsedDID.methodSpecificId)
else {
return DidResolution.Result.resolutionError(.invalidDid)
return DIDResolution.Result.resolutionError(.invalidDID)
}

guard parsedDid.methodName == "web" else {
return DidResolution.Result.resolutionError(.methodNotSupported)
guard parsedDID.methodName == "web" else {
return DIDResolution.Result.resolutionError(.methodNotSupported)
}

do {
let response = try await URLSession.shared.data(from: url)
let didDocument = try JSONDecoder().decode(DidDocument.self, from: response.0)
return DidResolution.Result(didDocument: didDocument)
let didDocument = try JSONDecoder().decode(DIDDocument.self, from: response.0)
return DIDResolution.Result(didDocument: didDocument)
} catch {
return DidResolution.Result.resolutionError(.notFound)
return DIDResolution.Result.resolutionError(.notFound)
}
}

Expand All @@ -32,7 +32,7 @@ struct DidWeb {
private static let wellKnownPath = "/.well-known"
private static let didDocumentFilename = "/did.json"

private static func getDidDocumentUrl(methodSpecificId: String) -> URL? {
private static func getDIDDocumentUrl(methodSpecificId: String) -> URL? {
let domainNameWithPath = methodSpecificId.replacingOccurrences(of: ":", with: "/")
guard let decodedDomain = domainNameWithPath.removingPercentEncoding,
var url = URL(string: "https://\(decodedDomain)")
Expand Down
12 changes: 6 additions & 6 deletions Sources/tbDEX/Dids/ParsedDid.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Foundation

enum ParsedDidError: Error {
enum ParsedDIDError: Error {
case invalidUri
case invalidMethodName
case invalidMethodSpecificId
}

/// Parsed Decentralized Identifier (DID), according to the specifications
/// defined by the [W3C DID Core specification](https://www.w3.org/TR/did-core).
struct ParsedDid {
struct ParsedDID {

/// The complete DID URI.
private(set) var uri: String
Expand All @@ -26,22 +26,22 @@ struct ParsedDid {
/// Parses a DID URI in accordance to the ABNF rules specified in the specification
/// [here](https://www.w3.org/TR/did-core/#did-syntax).
/// - Parameter didUri: URI of DID to parse
/// - Returns: `ParsedDid` instance if parsing was successful. Throws error otherwise.
/// - Returns: `ParsedDID` instance if parsing was successful. Throws error otherwise.
init(didUri: String) throws {
let components = didUri.components(separatedBy: ":")

guard components.count >= 3 else {
throw ParsedDidError.invalidUri
throw ParsedDIDError.invalidUri
}

let methodName = components[1]
guard Self.isValidMethodName(methodName) else {
throw ParsedDidError.invalidMethodName
throw ParsedDIDError.invalidMethodName
}

let methodSpecificId = components.dropFirst(2).joined(separator: ":")
guard Self.isValidMethodSpecificId(methodSpecificId) else {
throw ParsedDidError.invalidMethodSpecificId
throw ParsedDIDError.invalidMethodSpecificId
}

self.uri = didUri
Expand Down
22 changes: 11 additions & 11 deletions Sources/tbDEX/crypto/Crypto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ enum Crypto {
/// - algorithm: The JWA algorithm identifier.
/// - curve: The elliptic curve. Null for algorithms that do not use elliptic curves.
/// - Returns: The generated private key as a JWK object.
static func generatePrivateKey(algorithm: Jwk.Algorithm, curve: Jwk.Curve? = nil) throws -> Jwk {
static func generatePrivateKey(algorithm: JWK.Algorithm, curve: JWK.Curve? = nil) throws -> JWK {
let keyGenerator = try getKeyGenerator(algorithm: algorithm, curve: curve)
return try keyGenerator.generatePrivateKey()
}

/// Computes a public key from the given private key, utilizing relevant `KeyGenerator`.
/// - Parameter privateKey: The private key used to compute the public key.
/// - Returns: The computed public key as a JWK object.
static func computePublicKey(privateKey: Jwk) throws -> Jwk {
static func computePublicKey(privateKey: JWK) throws -> JWK {
let keyGenerator = try getKeyGenerator(algorithm: privateKey.algorithm, curve: privateKey.curve)
return try keyGenerator.computePublicKey(privateKey: privateKey)
}
Expand All @@ -29,7 +29,7 @@ enum Crypto {
/// - privateKey: The JWK private key to be used for generating the signature.
/// - payload: The data to be signed.
/// - Returns: The digital signature as a byte array.
static func sign<D>(privateKey: Jwk, payload: D) throws -> Data where D: DataProtocol {
static func sign<D>(privateKey: JWK, payload: D) throws -> Data where D: DataProtocol {
let signer = try getSigner(algorithm: privateKey.algorithm, curve: privateKey.curve)
return try signer.sign(privateKey: privateKey, payload: payload)
}
Expand All @@ -43,20 +43,20 @@ enum Crypto {
/// - algorithm: The algorithm used for signing/verification, only used if not provided in the JWK.
/// - Returns: Boolean indicating if the publicKey and signature are valid for the given payload.
static func verify<S, D>(
publicKey: Jwk,
publicKey: JWK,
signature: S,
signedPayload: D,
algorithm: Jwk.Algorithm? = nil
algorithm: JWK.Algorithm? = nil
) throws -> Bool where S: DataProtocol, D: DataProtocol {
let algorithm = publicKey.algorithm ?? algorithm
let verifier = try getVerifier(algorithm: algorithm, curve: publicKey.curve)
return try verifier.verify(publicKey: publicKey, signature: signature, signedPayload: signedPayload)
}

/// Converts a `Jwk` public key into its byte array representation.
/// - Parameter publicKey: `Jwk` object representing the public key to be converted.
/// Converts a `JWK` public key into its byte array representation.
/// - Parameter publicKey: `JWK` object representing the public key to be converted.
/// - Returns: Data representing the byte-level information of the provided public key
static func publicKeyToBytes(publicKey: Jwk) throws -> Data {
static func publicKeyToBytes(publicKey: JWK) throws -> Data {
let keyGenerator = try getKeyGenerator(algorithm: publicKey.algorithm, curve: publicKey.curve)
return try keyGenerator.publicKeyToBytes(publicKey)
}
Expand All @@ -68,7 +68,7 @@ enum Crypto {
/// - algorithm: The cryptographic algorithm to find a key generator for.
/// - curve: The cryptographic curve to find a key generator for.
/// - Returns: The corresponding `KeyGenerator`.
private static func getKeyGenerator(algorithm: Jwk.Algorithm?, curve: Jwk.Curve? = nil) throws -> KeyGenerator {
private static func getKeyGenerator(algorithm: JWK.Algorithm?, curve: JWK.Curve? = nil) throws -> KeyGenerator {
switch (algorithm, curve) {
case (nil, .secp256k1),
(Secp256k1.shared.algorithm, nil),
Expand All @@ -84,11 +84,11 @@ enum Crypto {
}
}

private static func getSigner(algorithm: Jwk.Algorithm?, curve: Jwk.Curve? = nil) throws -> Signer {
private static func getSigner(algorithm: JWK.Algorithm?, curve: JWK.Curve? = nil) throws -> Signer {
return try getKeyGenerator(algorithm: algorithm, curve: curve) as! Signer
}

private static func getVerifier(algorithm: Jwk.Algorithm?, curve: Jwk.Curve? = nil) throws -> Signer {
private static func getVerifier(algorithm: JWK.Algorithm?, curve: JWK.Curve? = nil) throws -> Signer {
return try getSigner(algorithm: algorithm, curve: curve)
}
}
Loading

0 comments on commit dcad8e7

Please sign in to comment.