generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add `OrderInstructions` message * remove `PaymentInstruction` from `Quote` * add `createOrderInstructions` in `DevTools` * remove `PaymentInstruction` checks in `QuoteTests` * add `OrderInstructionsTests`
- Loading branch information
Showing
7 changed files
with
139 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
Sources/tbDEX/Protocol/Models/Messages/OrderInstructions.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import Foundation | ||
|
||
public typealias OrderInstructions = Message<OrderInstructionsData> | ||
|
||
/// Data that makes up a OrderInstructions Message. | ||
/// | ||
/// [Specification Reference](https://github.com/TBD54566975/tbdex/blob/main/specs/protocol/README.md#orderinstructions) | ||
public struct OrderInstructionsData: MessageData { | ||
|
||
/// The amount of payin currency that the PFI will receive | ||
public let payin: PaymentInstruction | ||
|
||
/// The amount of payout currency that Alice will receive | ||
public let payout: PaymentInstruction | ||
|
||
/// Returns the MessageKind of quote | ||
public func kind() -> MessageKind { | ||
return .orderInstructions | ||
} | ||
|
||
/// Default Initializer | ||
public init( | ||
payin: PaymentInstruction, | ||
payout: PaymentInstruction | ||
) { | ||
self.payin = payin | ||
self.payout = payout | ||
} | ||
} | ||
|
||
/// Instruction about how to pay or be paid by the PFI | ||
/// | ||
/// [Specification Reference](https://github.com/TBD54566975/tbdex/tree/main/specs/protocol#paymentinstruction) | ||
public struct PaymentInstruction: Codable, Equatable { | ||
|
||
/// Link to allow Alice to pay PFI, or be paid by the PFI | ||
public let link: String? | ||
|
||
/// Instruction on how Alice can pay PFI, or how Alice can be paid by the PFI | ||
public let instruction: String? | ||
|
||
/// Default Initializer | ||
public init( | ||
link: String? = nil, | ||
instruction: String? = nil | ||
) { | ||
self.link = link | ||
self.instruction = instruction | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
Tests/tbDEXTests/Protocol/Models/Messages/OrderInstructionsTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Web5 | ||
import XCTest | ||
|
||
@testable import tbDEX | ||
|
||
final class OrderInstructionsTests: XCTestCase { | ||
|
||
let did = try! DIDJWK.create(keyManager: InMemoryKeyManager()) | ||
let pfi = try! DIDJWK.create(keyManager: InMemoryKeyManager()) | ||
|
||
func test_init() { | ||
let orderInstructions = DevTools.createOrderInstructions(from: pfi.uri, to: did.uri) | ||
|
||
XCTAssertEqual(orderInstructions.metadata.id.prefix, "orderinstructions") | ||
XCTAssertEqual(orderInstructions.metadata.from, pfi.uri) | ||
XCTAssertEqual(orderInstructions.metadata.to, did.uri) | ||
XCTAssertEqual(orderInstructions.metadata.exchangeID, "exchange_123") | ||
|
||
XCTAssertEqual(orderInstructions.data.payin.link, "https://example.com") | ||
XCTAssertEqual(orderInstructions.data.payin.instruction, "test instruction") | ||
|
||
XCTAssertEqual(orderInstructions.data.payout.link, "https://example.com") | ||
XCTAssertEqual(orderInstructions.data.payout.instruction, "test instruction") | ||
} | ||
|
||
func test_verifySuccess() async throws { | ||
var orderInstructions = DevTools.createOrderInstructions(from: pfi.uri, to: did.uri) | ||
try orderInstructions.sign(did: pfi) | ||
|
||
let isValid = try await orderInstructions.verify() | ||
XCTAssertTrue(isValid) | ||
} | ||
|
||
func test_verifyWithoutSigningFailure() async throws { | ||
let orderInstructions = DevTools.createOrderInstructions(from: pfi.uri, to: did.uri) | ||
|
||
await XCTAssertThrowsErrorAsync(try await orderInstructions.verify()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters