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

Commit 8890665

Browse files
authored
Update messages with latest spec changes, add tests for all of them (#37)
1 parent fe74531 commit 8890665

File tree

16 files changed

+414
-29
lines changed

16 files changed

+414
-29
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ let package = Package(
1616
dependencies: [
1717
.package(url: "https://github.com/Frizlab/swift-typeid.git", from: "0.3.0"),
1818
.package(url: "https://github.com/flight-school/anycodable.git", from: "0.6.7"),
19-
.package(url: "https://github.com/TBD54566975/web5-swift", exact: "0.0.3"),
19+
.package(url: "https://github.com/TBD54566975/web5-swift", exact: "0.0.4"),
2020
.package(url: "https://github.com/allegro/swift-junit.git", from: "2.1.0"),
2121
.package(url: "https://github.com/pointfreeco/swift-custom-dump.git", from: "1.1.2"),
2222
],

Sources/tbDEX/HttpClient/HttpClient.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ public enum HttpClient {
6565
private static func getPFIServiceEndpoint(pfiDIDURI: String) async -> String? {
6666
let resolutionResult = await DIDResolver.resolve(didURI: pfiDIDURI)
6767
if let service = resolutionResult.didDocument?.service?.first(where: { $0.type == "PFI" }) {
68-
return service.serviceEndpoint
68+
switch service.serviceEndpoint {
69+
case let .one(uri):
70+
return uri
71+
case let .many(uris):
72+
return uris.first
73+
}
6974
} else {
7075
return nil
7176
}

Sources/tbDEX/Protocol/Models/Message.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,18 @@ public struct Message<D: MessageData>: Codable, Equatable {
4545
try CryptoUtils.digest(data: data, metadata: metadata)
4646
}
4747

48-
mutating func sign(did: BearerDID, keyAlias: String? = nil) throws {
48+
public mutating func sign(did: BearerDID, keyAlias: String? = nil) throws {
4949
signature = try JWS.sign(
5050
did: did,
5151
payload: try digest(),
52-
detached: true,
53-
verificationMethodID: keyAlias
52+
options: .init(
53+
detached: true,
54+
verificationMethodID: keyAlias
55+
)
5456
)
5557
}
5658

57-
func verify() async throws -> Bool {
59+
public func verify() async throws -> Bool {
5860
return try await JWS.verify(
5961
compactJWS: signature,
6062
detachedPayload: try digest(),
@@ -74,6 +76,8 @@ public enum MessageKind: String, Codable {
7476
case orderStatus = "orderstatus"
7577
}
7678

79+
// MARK: - MessageData
80+
7781
/// The actual content for a `Message`.
7882
public protocol MessageData: Codable, Equatable {
7983

Sources/tbDEX/Protocol/Models/Messages/Close.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,11 @@ public struct CloseData: MessageData {
1313
public func kind() -> MessageKind {
1414
return .close
1515
}
16+
17+
/// Default Initializer
18+
public init(
19+
reason: String? = nil
20+
) {
21+
self.reason = reason
22+
}
1623
}

Sources/tbDEX/Protocol/Models/Messages/Order.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ public struct OrderData: MessageData {
1111
return .order
1212
}
1313

14+
/// Default Initializer
15+
public init() {}
16+
1417
}

Sources/tbDEX/Protocol/Models/Messages/OrderStatus.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,11 @@ public struct OrderStatusData: MessageData {
1313
public func kind() -> MessageKind {
1414
return .orderStatus
1515
}
16+
17+
/// Default Initializer
18+
public init(
19+
orderStatus: String
20+
) {
21+
self.orderStatus = orderStatus
22+
}
1623
}

Sources/tbDEX/Protocol/Models/Messages/Quote.swift

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,20 @@ public struct QuoteData: MessageData {
1616
/// The amount of payout currency that Alice will receive
1717
public let payout: QuoteDetails
1818

19-
/// Object that describes how to pay the PFI, and how to get paid by the PFI (e.g. BTC address, payment link)
20-
public let paymentInstructions: PaymentInstructions?
21-
2219
public func kind() -> MessageKind {
2320
return .quote
2421
}
22+
23+
/// Default Initializer
24+
public init(
25+
expiresAt: Date,
26+
payin: QuoteDetails,
27+
payout: QuoteDetails
28+
) {
29+
self.expiresAt = expiresAt
30+
self.payin = payin
31+
self.payout = payout
32+
}
2533
}
2634

2735
/// Details about a quoted amount
@@ -38,19 +46,21 @@ public struct QuoteDetails: Codable, Equatable {
3846
/// The amount paid in fees
3947
public let fee: String?
4048

41-
}
42-
43-
/// Instructions about how one can pay or be paid by the PFI
44-
///
45-
/// [Specification Reference](https://github.com/TBD54566975/tbdex/tree/main/specs/protocol#paymentinstructions)
46-
public struct PaymentInstructions: Codable, Equatable {
47-
48-
/// Link or Instruction describing how to pay the PFI.
49-
public let payin: PaymentInstruction?
50-
51-
/// Link or Instruction describing how to get paid by the PFI
52-
public let payout: PaymentInstruction?
53-
49+
/// Object that describes how to pay the PFI, and how to get paid by the PFI (e.g. BTC address, payment link)
50+
public let paymentInstruction: PaymentInstruction?
51+
52+
/// Default Initializer
53+
public init(
54+
currencyCode: String,
55+
amount: String,
56+
fee: String? = nil,
57+
paymentInstruction: PaymentInstruction? = nil
58+
) {
59+
self.currencyCode = currencyCode
60+
self.amount = amount
61+
self.fee = fee
62+
self.paymentInstruction = paymentInstruction
63+
}
5464
}
5565

5666
/// Instruction about how to pay or be paid by the PFI
@@ -64,4 +74,12 @@ public struct PaymentInstruction: Codable, Equatable {
6474
/// Instruction on how Alice can pay PFI, or how Alice can be paid by the PFI
6575
public let instruction: String?
6676

77+
/// Default Initializer
78+
public init(
79+
link: String? = nil,
80+
instruction: String? = nil
81+
) {
82+
self.link = link
83+
self.instruction = instruction
84+
}
6785
}

Sources/tbDEX/Protocol/Models/Messages/RFQ.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
11
import AnyCodable
22
import Foundation
3+
import TypeID
34

45
public typealias RFQ = Message<RFQData>
56

7+
extension RFQ {
8+
9+
public init(
10+
to: String,
11+
from: String,
12+
data: RFQData
13+
) {
14+
let id = TypeID(prefix: data.kind().rawValue)!
15+
self.metadata = MessageMetadata(
16+
id: id,
17+
kind: data.kind(),
18+
from: from,
19+
to: to,
20+
exchangeID: id.rawValue,
21+
createdAt: Date()
22+
)
23+
self.data = data
24+
self.private = nil
25+
}
26+
27+
}
28+
629
/// Data that makes up a RFQ Message.
730
///
831
/// [Specification Reference](https://github.com/TBD54566975/tbdex/tree/main/specs/protocol#rfq-request-for-quote)
@@ -27,6 +50,19 @@ public struct RFQData: MessageData {
2750
return .rfq
2851
}
2952

53+
public init(
54+
offeringId: String,
55+
payinAmount: String,
56+
claims: [String],
57+
payinMethod: SelectedPaymentMethod,
58+
payoutMethod: SelectedPaymentMethod
59+
) {
60+
self.offeringId = offeringId
61+
self.payinAmount = payinAmount
62+
self.claims = claims
63+
self.payinMethod = payinMethod
64+
self.payoutMethod = payoutMethod
65+
}
3066
}
3167

3268
/// Details about a selected payment method
@@ -39,4 +75,12 @@ public struct SelectedPaymentMethod: Codable, Equatable {
3975

4076
/// An object containing the properties defined in an Offering's `requiredPaymentDetails` json schema
4177
public let paymentDetails: AnyCodable?
78+
79+
public init(
80+
kind: String,
81+
paymentDetails: AnyCodable? = nil
82+
) {
83+
self.kind = kind
84+
self.paymentDetails = paymentDetails
85+
}
4286
}

Sources/tbDEX/Protocol/Models/Resource.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,18 @@ public struct Resource<D: ResourceData>: Codable, Equatable {
3838
try CryptoUtils.digest(data: data, metadata: metadata)
3939
}
4040

41-
mutating func sign(did: BearerDID, keyAlias: String? = nil) async throws {
41+
public mutating func sign(did: BearerDID, keyAlias: String? = nil) throws {
4242
self.signature = try JWS.sign(
4343
did: did,
4444
payload: try digest(),
45-
detached: true,
46-
verificationMethodID: keyAlias
45+
options: .init(
46+
detached: true,
47+
verificationMethodID: keyAlias
48+
)
4749
)
4850
}
4951

50-
func verify() async throws -> Bool {
52+
public func verify() async throws -> Bool {
5153
return try await JWS.verify(
5254
compactJWS: signature,
5355
detachedPayload: try digest(),

Sources/tbDEX/Protocol/Models/Resources/Offering.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ public struct OfferingData: ResourceData {
2121
/// Details about the currency that the PFI is selling.
2222
public let payoutCurrency: CurrencyDetails
2323

24-
/// A list of payment methods the counterparty (Alice) can choose to send payment to the PFI from in order to
25-
/// qualify for this offering.
24+
/// A list of payment methods the counterparty (Alice) can choose to send payment
25+
/// to the PFI from in order to qualify for this offering.
26+
public let payinMethods: [PaymentMethod]
27+
28+
/// A list of payment methods the counterparty (Alice) can choose to receive payment
29+
/// from the PFI in order to qualify for this offering.
2630
public let payoutMethods: [PaymentMethod]
2731

2832
// TODO: amika - Update to PresentationDefinitionV2, requires third-party or custom implementation

0 commit comments

Comments
 (0)