Skip to content

Commit d917bd9

Browse files
feat(sample): Fifth batch of sample app code
This also introduces documentation on multiple public models and functions.
1 parent d55b511 commit d917bd9

36 files changed

+845
-442
lines changed

Apollo/Sources/ApolloImpl+Public.swift

+2-4
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,11 @@ returns random mnemonics nerver returns invalid mnemonics
100100
switch publicKey.curve {
101101
case "secp256k1":
102102
let verifier = JWTVerifier.es256(publicKey: publicKey.value)
103-
let decoder = JWTDecoder.init(jwtVerifier: verifier)
104-
let jwt = try decoder.decode(JWT<MyClaims>.self, fromString: jwk)
103+
let decoder = JWTDecoder(jwtVerifier: verifier)
105104
return jwk
106105
default:
107106
let verifier = JWTVerifier.none
108-
let decoder = JWTDecoder.init(jwtVerifier: verifier)
109-
let jwt = try decoder.decode(JWT<MyClaims>.self, fromString: jwk)
107+
let decoder = JWTDecoder(jwtVerifier: verifier)
110108
return jwk
111109
}
112110
}

Builders/Sources/PolluxBuilder.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import Pollux
21
import Domain
2+
import Pollux
33

44
public struct PolluxBuilder {
55
let castor: Castor

Domain/Sources/BBs/Apollo.swift

+80
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,100 @@
11
import Foundation
22

33
public protocol Apollo {
4+
/// createRandomMnemonics creates a random set of mnemonic phrases that can be used as a seed for generating a private key.
5+
///
6+
/// - Returns: An array of mnemonic phrases
47
func createRandomMnemonics() -> [String]
8+
9+
/// createSeed takes in a set of mnemonics and a passphrase, and returns a seed object used to generate a private key.
10+
/// This function may throw an error if the mnemonics or passphrase are invalid.
11+
///
12+
/// - Parameters:
13+
/// - mnemonics: An array of mnemonic phrases
14+
/// - passphrase: A passphrase used to enhance the security of the seed
15+
/// - Returns: A seed object
16+
/// - Throws: An error if the mnemonics or passphrase are invalid
517
func createSeed(mnemonics: [String], passphrase: String) throws -> Seed
18+
19+
/// createRandomSeed creates a random seed and a corresponding set of mnemonic phrases.
20+
///
21+
/// - Returns: A tuple containing an array of mnemonic phrases and a seed object
622
func createRandomSeed() -> (mnemonic: [String], seed: Seed)
23+
24+
/// createKeyPair creates a key pair (a private and public key) using a given seed and key curve.
25+
///
26+
/// - Parameters:
27+
/// - seed: A seed object used to generate the key pair
28+
/// - curve: The key curve to use for generating the key pair
29+
/// - Returns: A key pair object containing a private and public key
730
func createKeyPair(seed: Seed, curve: KeyCurve) -> KeyPair
31+
32+
/// createKeyPair creates a key pair using a given seed and a specified private key. This function may throw an error if the private key is invalid.
33+
///
34+
/// - Parameters:
35+
/// - seed: A seed object used to generate the key pair
36+
/// - privateKey: The private key to use for generating the key pair
37+
/// - Returns: A key pair object containing a private and public key
38+
/// - Throws: An error if the private key is invalid
839
func createKeyPair(seed: Seed, privateKey: PrivateKey) throws -> KeyPair
40+
41+
/// compressedPublicKey compresses a given public key into a shorter, more efficient form.
42+
///
43+
/// - Parameter publicKey: The public key to compress
44+
/// - Returns: The compressed public key
945
func compressedPublicKey(publicKey: PublicKey) -> CompressedPublicKey
46+
47+
/// compressedPublicKey decompresses a given compressed public key into its original form.
48+
///
49+
/// - Parameter compressedData: The compressed public key data
50+
/// - Returns: The decompressed public key
1051
func compressedPublicKey(compressedData: Data) -> CompressedPublicKey
52+
53+
/// signMessage signs a message using a given private key, returning the signature.
54+
///
55+
/// - Parameters:
56+
/// - privateKey: The private key to use for signing the message
57+
/// - message: The message to sign, in binary data form
58+
/// - Returns: The signature of the message
1159
func signMessage(privateKey: PrivateKey, message: Data) -> Signature
60+
61+
/// signMessage signs a message using a given private key, returning the signature. This function may throw an error if the message is invalid.
62+
///
63+
/// - Parameters:
64+
/// - privateKey: The private key to use for signing the message
65+
/// - message: The message to sign, in string form
66+
/// - Returns: The signature of the message
67+
/// - Throws: An error if the message is invalid
1268
func signMessage(privateKey: PrivateKey, message: String) throws -> Signature
69+
70+
/// verifySignature verifies the authenticity of a signature using the corresponding public key, challenge, and signature. This function returns a boolean value indicating whether the signature is valid or not.
71+
///
72+
/// - Parameters:
73+
/// - publicKey: The public key associated with the signature
74+
/// - challenge: The challenge used to generate the signature
75+
/// - signature: The signature to verify
76+
/// - Returns: A boolean value indicating whether the signature is valid or not
1377
func verifySignature(
1478
publicKey: PublicKey,
1579
challenge: Data,
1680
signature: Signature
1781
) -> Bool
82+
83+
/// getPrivateJWKJson converts a private key pair into a JSON Web Key (JWK) format with a given ID. This function may throw an error if the key pair is invalid.
84+
///
85+
/// - Parameters:
86+
/// - id: The ID to use for the JWK
87+
/// - keyPair: The private key pair to convert to JWK format
88+
/// - Returns: The private key pair in JWK format, as a string
89+
/// - Throws: An error if the key pair is invalid
1890
func getPrivateJWKJson(id: String, keyPair: KeyPair) throws -> String
91+
92+
/// getPublicJWKJson converts a public key pair into a JSON Web Key (JWK) format with a given ID. This function may throw an error if the key pair is invalid.
93+
///
94+
/// - Parameters:
95+
/// - id: The ID to use for the JWK
96+
/// - keyPair: The public key pair to convert to JWK format
97+
/// - Returns: The public key pair in JWK format, as a string
98+
/// - Throws: An error if the key pair is invalid
1999
func getPublicJWKJson(id: String, keyPair: KeyPair) throws -> String
20100
}

Domain/Sources/BBs/Castor.swift

+49
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,81 @@
11
import Foundation
22

33
public protocol Castor {
4+
/// parseDID parses a string representation of a Decentralized Identifier (DID) into a DID object. This function may throw an error if the string is not a valid DID.
5+
///
6+
/// - Parameter str: The string representation of the DID
7+
/// - Returns: The DID object
8+
/// - Throws: An error if the string is not a valid DID
49
func parseDID(str: String) throws -> DID
10+
11+
/// createPrismDID creates a DID for a prism (a device or server that acts as a DID owner and controller) using a given master public key and list of services. This function may throw an error if the master public key or services are invalid.
12+
///
13+
/// - Parameters:
14+
/// - masterPublicKey: The master public key of the prism
15+
/// - services: The list of services offered by the prism
16+
/// - Returns: The DID of the prism
17+
/// - Throws: An error if the master public key or services are invalid
518
func createPrismDID(
619
masterPublicKey: PublicKey,
720
services: [DIDDocument.Service]
821
) throws -> DID
922

23+
/// createPeerDID creates a DID for a peer (a device or server that acts as a DID subject) using given key agreement and authentication key pairs and a list of services. This function may throw an error if the key pairs or services are invalid.
24+
///
25+
/// - Parameters:
26+
/// - keyAgreementKeyPair: The key pair used for key agreement (establishing secure communication between peers)
27+
/// - authenticationKeyPair: The key pair used for authentication (verifying the identity of a peer)
28+
/// - services: The list of services offered by the peer
29+
/// - Returns: The DID of the peer
30+
/// - Throws: An error if the key pairs or services are invalid
1031
func createPeerDID(
1132
keyAgreementKeyPair: KeyPair,
1233
authenticationKeyPair: KeyPair,
1334
services: [DIDDocument.Service]
1435
) throws -> DID
1536

37+
/// resolveDID asynchronously resolves a DID to its corresponding DID Document. This function may throw an error if the DID is invalid or the document cannot be retrieved.
38+
///
39+
/// - Parameter did: The DID to resolve
40+
/// - Returns: The DID Document associated with the DID
41+
/// - Throws: An error if the DID is invalid or the document cannot be retrieved
1642
func resolveDID(did: DID) async throws -> DIDDocument
1743

44+
/// verifySignature asynchronously verifies the authenticity of a signature using the corresponding DID, challenge, and signature data. This function returns a boolean value indicating whether the signature is valid or not. This function may throw an error if the DID or signature data are invalid.
45+
///
46+
/// - Parameters:
47+
/// - did: The DID associated with the signature
48+
/// - challenge: The challenge used to generate the signature
49+
/// - signature: The signature data to verify
50+
/// - Returns: A boolean value indicating whether the signature is valid or not
51+
/// - Throws: An error if the DID or signature data are invalid
1852
func verifySignature(
1953
did: DID,
2054
challenge: Data,
2155
signature: Data
2256
) async throws -> Bool
2357

58+
/// verifySignature verifies the authenticity of a signature using the corresponding DID Document, challenge, and signature data. This function returns a boolean value indicating whether the signature is valid or not. This function may throw an error if the DID Document or signature data are invalid.
59+
///
60+
/// - Parameters:
61+
/// - document: The DID Document associated with the signature
62+
/// - challenge: The challenge used to generate the signature
63+
/// - signature: The signature data to verify
64+
/// - Returns: A boolean value indicating whether the signature is valid or not
65+
/// - Throws: An error if the DID Document or signature data are invalid
2466
func verifySignature(
2567
document: DIDDocument,
2668
challenge: Data,
2769
signature: Data
2870
) throws -> Bool
2971

72+
/// getEcnumbasis generates a unique ECNUM basis string for a given DID and key pair. This function may throw an error if the DID or key pair are invalid.
73+
///
74+
/// - Parameters:
75+
/// - did: The DID associated with the key pair
76+
/// - keyPair: The key pair to use for generating the ECNUM basis
77+
/// - Returns: The ECNUM basis string
78+
/// - Throws: An error if the DID or key pair are invalid
3079
func getEcnumbasis(did: DID, keyPair: KeyPair) throws -> String
3180
}
3281

Domain/Sources/BBs/Mercury.swift

+23
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
11
import Foundation
22

33
public protocol Mercury {
4+
/// packMessage asynchronously packs a given message object into a string representation. This function may throw an error if the message object is invalid.
5+
///
6+
/// - Parameter msg: The message object to pack
7+
/// - Returns: The string representation of the packed message
8+
/// - Throws: An error if the message object is invalid
49
func packMessage(msg: Domain.Message) async throws -> String
10+
11+
/// unpackMessage asynchronously unpacks a given string representation of a message into a message object. This function may throw an error if the string is not a valid message representation.
12+
///
13+
/// - Parameter msg: The string representation of the message to unpack
14+
/// - Returns: The message object
15+
/// - Throws: An error if the string is not a valid message representation
516
func unpackMessage(msg: String) async throws -> Domain.Message
17+
18+
/// sendMessage asynchronously sends a given message and returns the response data. This function may throw an error if the message is invalid or the send operation fails.
19+
///
20+
/// - Parameter msg: The message to send
21+
/// - Returns: The response data
22+
/// - Throws: An error if the message is invalid or the send operation fails
623
@discardableResult
724
func sendMessage(msg: Message) async throws -> Data?
25+
26+
/// sendMessageParseMessage asynchronously sends a given message and returns the response message object. This function may throw an error if the message is invalid, the send operation fails, or the response message is invalid.
27+
///
28+
/// - Parameter msg: The message to send
29+
/// - Returns: The response message object
30+
/// - Throws: An error if the message is invalid, the send operation fails, or the response message is invalid
831
@discardableResult
932
func sendMessageParseMessage(msg: Message) async throws -> Message?
1033
}

Domain/Sources/Models/DID.swift

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import Foundation
22

3+
/// A type alias representing a DID method (a specific protocol or process used to resolve and manage DIDs) as a string.
34
public typealias DIDMethod = String
5+
6+
/// A type alias representing a DID method ID (a unique identifier within a DID method) as a string.
47
public typealias DIDMethodId = String
58

6-
/// Represents a DID with ``DIDMethod`` and ``DIDMethodId``
7-
/// As specified in [w3 standards](https://www.w3.org/TR/did-core/#dfn-did-schemes)
9+
/// A DID is a unique and persistent identifier for a subject or object, such as a person, organization, or device. It is created and managed using a specific DID method, and consists of a schema, method, and method ID. The schema indicates the type of DID (e.g. "did"), the method indicates the specific protocol or process used to resolve and manage the DID (e.g. "prism"), and the method ID is a unique identifier within the DID method.
10+
/// As specified in the [W3C DID standards](https://www.w3.org/TR/did-core/#dfn-did-schemes).
811
public struct DID: Equatable {
12+
/// The schema of the DID (e.g. "did")
913
public let schema: String
14+
15+
/// The method of the DID (e.g. "prism")
1016
public let method: DIDMethod
17+
18+
/// The method ID of the DID
1119
public let methodId: DIDMethodId
1220

1321
/// Initializes a standard DID
@@ -26,6 +34,7 @@ public struct DID: Equatable {
2634
}
2735

2836
/// String representation of this DID as specified in [w3 standards](https://www.w3.org/TR/did-core/#dfn-did-schemes)
37+
/// This is a combination of the schema, method, and method ID, separated by colons (e.g. "did:prism:0xabc123").
2938
public var string: String { "\(schema):\(method):\(methodId)" }
3039

3140
/// Simple initializer that receives a String and returns a DID

0 commit comments

Comments
 (0)