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

Add protocol and externalId to messages #64

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions Sources/tbDEX/Protocol/Models/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ public struct Message<D: MessageData>: Codable, Equatable {
/// An ephemeral JSON object used to transmit sensitive data (e.g. PII)
public let `private`: AnyCodable?

/// Default Initializer
/// Default Initializer. `protocol` defaults to "1.0" if nil
public init(
from: String,
to: String,
exchangeID: String,
data: D
data: D,
externalID: String?,
`protocol`: String?
) {
let now = Date()
self.metadata = MessageMetadata(
Expand All @@ -34,7 +36,9 @@ public struct Message<D: MessageData>: Codable, Equatable {
from: from,
to: to,
exchangeID: exchangeID,
createdAt: now
createdAt: now,
externalID: externalID,
protocol: `protocol` ?? "1.0"
)
self.data = data
self.signature = nil
Expand Down Expand Up @@ -112,6 +116,12 @@ public struct MessageMetadata: Codable, Equatable {

/// The time at which the message was created. Can be serialized to or from JSON with `tbDEXDateFormatter`. Use `tbDEXJSONDecoder` or `tbDEXJSONEncoder`.
public let createdAt: Date

/// Arbitrary ID for the caller to associate with the message. Optional */
public let externalID: String?

/// Version of the protocol in use (x.x format). Must be consistent with all other messages in a given exchange */
public let `protocol`: String

enum CodingKeys: String, CodingKey {
case id
Expand All @@ -120,5 +130,7 @@ public struct MessageMetadata: Codable, Equatable {
case to
case exchangeID = "exchangeId"
case createdAt
case externalID = "externalId"
case `protocol`
kirahsapong marked this conversation as resolved.
Show resolved Hide resolved
}
}
9 changes: 7 additions & 2 deletions Sources/tbDEX/Protocol/Models/Messages/RFQ.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ public typealias RFQ = Message<RFQData>

extension RFQ {

/// Default Initializer. `protocol` defaults to "1.0" if nil
public init(
to: String,
from: String,
data: RFQData
data: RFQData,
externalID: String?,
`protocol`: String?
) {
let id = TypeID(prefix: data.kind().rawValue)!
self.metadata = MessageMetadata(
Expand All @@ -18,7 +21,9 @@ extension RFQ {
from: from,
to: to,
exchangeID: id.rawValue,
createdAt: Date()
createdAt: Date(),
externalID: externalID,
protocol: `protocol` ?? "1.0"
)
self.data = data
self.private = nil
Expand Down
24 changes: 23 additions & 1 deletion Tests/tbDEXTests/Protocol/Models/Messages/CloseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ final class CloseTests: XCTestCase {
XCTAssertEqual(close.metadata.to, pfi.uri)
XCTAssertEqual(close.metadata.exchangeID, "exchange_123")
XCTAssertEqual(close.data.reason, "test reason")
XCTAssertEqual(close.metadata.protocol, "1.0")
}

func test_overrideProtocolVersion() {
let close = Close(
from: did.uri,
to: pfi.uri,
exchangeID: "exchange_123",
data: .init(
reason: "test reason"
),
externalID: nil,
protocol: "2.0"
)

XCTAssertEqual(close.metadata.id.prefix, "close")
XCTAssertEqual(close.metadata.from, did.uri)
XCTAssertEqual(close.metadata.to, pfi.uri)
XCTAssertEqual(close.metadata.exchangeID, "exchange_123")
XCTAssertEqual(close.metadata.protocol, "2.0")
}

func test_signAndVerify() async throws {
Expand Down Expand Up @@ -46,7 +66,9 @@ final class CloseTests: XCTestCase {
exchangeID: "exchange_123",
data: .init(
reason: "test reason"
)
),
externalID: nil,
protocol: nil
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ final class OrderStatusTests: XCTestCase {
XCTAssertEqual(orderStatus.metadata.exchangeID, "exchange_123")
XCTAssertEqual(orderStatus.data.orderStatus, "test status")
}

func test_overrideProtocolVersion() {
let orderstatus = OrderStatus(
from: did.uri,
to: pfi.uri,
exchangeID: "exchange_123",
data: .init(
orderStatus: "test status"
),
externalID: nil,
protocol: "2.0"
)

XCTAssertEqual(orderstatus.metadata.id.prefix, "orderstatus")
XCTAssertEqual(orderstatus.metadata.from, did.uri)
XCTAssertEqual(orderstatus.metadata.to, pfi.uri)
XCTAssertEqual(orderstatus.metadata.exchangeID, "exchange_123")
XCTAssertEqual(orderstatus.metadata.protocol, "2.0")
}

func test_signAndVerify() async throws {
let did = try DIDJWK.create(keyManager: InMemoryKeyManager())
Expand Down Expand Up @@ -46,7 +65,9 @@ final class OrderStatusTests: XCTestCase {
exchangeID: "exchange_123",
data: .init(
orderStatus: "test status"
)
),
externalID: nil,
protocol: nil
kirahsapong marked this conversation as resolved.
Show resolved Hide resolved
)
}
}
21 changes: 20 additions & 1 deletion Tests/tbDEXTests/Protocol/Models/Messages/OrderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ final class OrderTests: XCTestCase {
XCTAssertEqual(order.metadata.to, pfi.uri)
XCTAssertEqual(order.metadata.exchangeID, "exchange_123")
}

func test_overrideProtocolVersion() {
let order = Order(
from: did.uri,
to: pfi.uri,
exchangeID: "exchange_123",
data: .init(),
externalID: nil,
protocol: "2.0"
)

XCTAssertEqual(order.metadata.id.prefix, "order")
XCTAssertEqual(order.metadata.from, did.uri)
XCTAssertEqual(order.metadata.to, pfi.uri)
XCTAssertEqual(order.metadata.exchangeID, "exchange_123")
XCTAssertEqual(order.metadata.protocol, "2.0")
}

func test_signAndVerify() async throws {
let did = try DIDJWK.create(keyManager: InMemoryKeyManager())
Expand Down Expand Up @@ -43,7 +60,9 @@ final class OrderTests: XCTestCase {
from: from,
to: to,
exchangeID: "exchange_123",
data: .init()
data: .init(),
externalID: nil,
protocol: nil
)
}
}
36 changes: 35 additions & 1 deletion Tests/tbDEXTests/Protocol/Models/Messages/QuoteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,38 @@ final class QuoteTests: XCTestCase {
XCTAssertEqual(quote.data.payout.fee, "0.50")
XCTAssertNil(quote.data.payout.paymentInstruction)
}

func test_overrideProtocolVersion() {
let quote = Quote(
from: did.uri,
to: pfi.uri,
exchangeID: "exchange_123",
data: .init(
expiresAt: Date().addingTimeInterval(60),
payin: .init(
currencyCode: "USD",
amount: "1.00",
paymentInstruction: .init(
link: "https://example.com",
instruction: "test instruction"
)
),
payout: .init(
currencyCode: "AUD",
amount: "2.00",
fee: "0.50"
)
),
externalID: nil,
protocol: "2.0"
)

XCTAssertEqual(quote.metadata.id.prefix, "quote")
XCTAssertEqual(quote.metadata.from, did.uri)
XCTAssertEqual(quote.metadata.to, pfi.uri)
XCTAssertEqual(quote.metadata.exchangeID, "exchange_123")
XCTAssertEqual(quote.metadata.protocol, "2.0")
}

func test_signAndVerify() async throws {
var quote = createQuote(from: did.uri, to: pfi.uri)
Expand Down Expand Up @@ -70,7 +102,9 @@ final class QuoteTests: XCTestCase {
amount: "2.00",
fee: "0.50"
)
)
),
externalID: nil,
protocol: nil
)
}
}
30 changes: 29 additions & 1 deletion Tests/tbDEXTests/Protocol/Models/Messages/RFQTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@ final class RFQTests: XCTestCase {
XCTAssertEqual(rfq.data.payinMethod.kind, "DEBIT_CARD")
XCTAssertEqual(rfq.data.payoutMethod.kind, "BITCOIN_ADDRESS")
}

func test_overrideProtocolVersion() {
let rfq = RFQ(
to: pfi.uri,
from: did.uri,
data: .init(
offeringId: TypeID(rawValue:"offering_01hmz7ehw6e5k9bavj0ywypfpy")!,
payinAmount: "1.00",
payinMethod: .init(
kind: "DEBIT_CARD"
),
payoutMethod: .init(
kind: "BITCOIN_ADDRESS"
),
claims: []
),
externalID: nil,
protocol: "2.0"
)

XCTAssertEqual(rfq.metadata.id.prefix, "rfq")
XCTAssertEqual(rfq.metadata.from, did.uri)
XCTAssertEqual(rfq.metadata.to, pfi.uri)
XCTAssertEqual(rfq.metadata.exchangeID, rfq.metadata.id.rawValue)
XCTAssertEqual(rfq.metadata.protocol, "2.0")
}

func test_signAndVerify() async throws {
var rfq = createRFQ(from: did.uri, to: pfi.uri)
Expand Down Expand Up @@ -56,7 +82,9 @@ final class RFQTests: XCTestCase {
kind: "BITCOIN_ADDRESS"
),
claims: []
)
),
externalID: nil,
protocol: nil
)
}
}
Loading