Skip to content

Commit cd0421c

Browse files
committed
test: passing swift tests
1 parent 9a6d8a1 commit cd0421c

File tree

3 files changed

+31
-34
lines changed

3 files changed

+31
-34
lines changed

packages/swift/AlgoKitTransact/Tests/AlgoKitTransactTests/GenericTransactionTests.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ func genericTransactionMalformedBytes() throws {
1313
let simplePayment = testData.simplePayment
1414
let badBytes = Data(simplePayment.unsignedBytes[13..<37])
1515
do {
16-
_ = try decodeTransaction(bytes: badBytes)
16+
_ = try decodeTransaction(encodedTx: badBytes)
1717
#expect(Bool(false), "Expected DecodingError to be thrown")
18+
} catch AlgoKitTransactError.InputError {
19+
// Success - expected error was thrown
20+
#expect(Bool(true))
1821
} catch AlgoKitTransactError.DecodingError {
1922
// Success - expected error was thrown
2023
#expect(Bool(true))
@@ -24,9 +27,9 @@ func genericTransactionMalformedBytes() throws {
2427
@Test("Generic Transaction: encode 0 bytes")
2528
func genericTransactionEncode0Bytes() throws {
2629
do {
27-
_ = try decodeTransaction(bytes: Data())
30+
_ = try decodeTransaction(encodedTx: Data())
2831
#expect(Bool(false), "Expected DecodingError to be thrown")
29-
} catch AlgoKitTransactError.DecodingError(let message) {
32+
} catch AlgoKitTransactError.InputError(let message) {
3033
#expect(message == "attempted to decode 0 bytes")
3134
}
3235
}

packages/swift/AlgoKitTransact/Tests/AlgoKitTransactTests/PaymentTests.swift

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ import ed25519swift
1111
@Test("Payment: example")
1212
func paymentExample() throws {
1313
let aliceKeyPair = Ed25519.generateKeyPair()
14-
let alice = try addressFromPubKey(pubKey: Data(aliceKeyPair.publicKey))
15-
let bob = try addressFromString(
16-
address: "B72WNFFEZ7EOGMQPP7ROHYS3DSLL5JW74QASYNWGZGQXWRPJECJJLJIJ2Y"
17-
)
14+
let alice = try addressFromPublicKey(publicKey: Data(aliceKeyPair.publicKey))
15+
let bob = "B72WNFFEZ7EOGMQPP7ROHYS3DSLL5JW74QASYNWGZGQXWRPJECJJLJIJ2Y"
1816

1917
let txn: Transaction = Transaction(
2018
transactionType: .payment,
@@ -31,21 +29,23 @@ func paymentExample() throws {
3129
)
3230

3331
let sig = Ed25519.sign(
34-
message: [UInt8](try encodeTransaction(tx: txn)), secretKey: aliceKeyPair.secretKey)
32+
message: [UInt8](try encodeTransaction(transaction: txn)), secretKey: aliceKeyPair.secretKey)
3533

36-
let signedTxn = try attachSignature(
37-
encodedTx: try encodeTransaction(tx: txn),
34+
let signedTxn = SignedTransaction(
35+
transaction: txn,
3836
signature: Data(sig)
3937
)
4038

41-
#expect(signedTxn.count > 0)
39+
let encodedSignedTxn = try encodeSignedTransaction(signedTransaction: signedTxn)
40+
41+
#expect(!encodedSignedTxn.isEmpty)
4242
}
4343

4444
@Test("Payment: get encoded transaction type")
4545
func paymentGetEncodedTransactionType() throws {
4646
let testData = try loadTestData()
4747
let simplePayment = testData.simplePayment
48-
let txType = try getEncodedTransactionType(bytes: Data(simplePayment.unsignedBytes))
48+
let txType = try getEncodedTransactionType(encodedTransaction: Data(simplePayment.unsignedBytes))
4949
#expect(txType == .payment)
5050
}
5151

@@ -55,7 +55,7 @@ func paymentDecodeWithoutPrefix() throws {
5555
let simplePayment = testData.simplePayment
5656
let transaction = makeTransaction(from: simplePayment)
5757
let bytesWithoutPrefix = Data(simplePayment.unsignedBytes.dropFirst(2))
58-
let decoded = try decodeTransaction(bytes: bytesWithoutPrefix)
58+
let decoded = try decodeTransaction(encodedTx: bytesWithoutPrefix)
5959
#expect(decoded == transaction)
6060
}
6161

@@ -64,7 +64,7 @@ func paymentDecodeWithPrefix() throws {
6464
let testData = try loadTestData()
6565
let simplePayment = testData.simplePayment
6666
let transaction = makeTransaction(from: simplePayment)
67-
let decoded = try decodeTransaction(bytes: Data(simplePayment.unsignedBytes))
67+
let decoded = try decodeTransaction(encodedTx: Data(simplePayment.unsignedBytes))
6868
#expect(decoded == transaction)
6969
}
7070

@@ -74,10 +74,14 @@ func paymentEncodeWithSignature() throws {
7474
let simplePayment = testData.simplePayment
7575
let signature = Ed25519.sign(
7676
message: simplePayment.unsignedBytes, secretKey: simplePayment.signingPrivateKey)
77-
let signedTx = try attachSignature(
78-
encodedTx: Data(simplePayment.unsignedBytes),
79-
signature: Data(signature)
77+
78+
let signedTx = try encodeSignedTransaction(
79+
signedTransaction: SignedTransaction(
80+
transaction: makeTransaction(from: simplePayment),
81+
signature: Data(signature)
82+
)
8083
)
84+
8185
#expect([UInt8](signedTx) == simplePayment.signedBytes)
8286
}
8387

@@ -86,6 +90,6 @@ func paymentEncode() throws {
8690
let testData = try loadTestData()
8791
let simplePayment = testData.simplePayment
8892
let transaction = makeTransaction(from: simplePayment)
89-
let encoded = try encodeTransaction(tx: transaction)
93+
let encoded = try encodeTransaction(transaction: transaction)
9094
#expect([UInt8](encoded) == simplePayment.unsignedBytes)
9195
}

packages/swift/AlgoKitTransact/Tests/AlgoKitTransactTests/TestUtils.swift

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ struct TransactionTestData: Codable {
99
}
1010

1111
struct TransactionData: Codable {
12-
let sender: AddressData
12+
let sender: String
1313
let fee: UInt64
1414
let transactionType: String
1515
let firstValid: UInt64
1616
let lastValid: UInt64
1717
let genesisHash: [UInt8]
1818
let genesisId: String
1919
let note: [UInt8]?
20-
let rekeyTo: AddressData?
20+
let rekeyTo: String?
2121
let lease: [UInt8]?
2222
let group: [UInt8]?
2323
let payment: PaymentFieldsData
2424
}
2525

2626
struct PaymentFieldsData: Codable {
27-
let receiver: AddressData
27+
let receiver: String
2828
let amount: UInt64
2929
}
3030

@@ -50,28 +50,18 @@ func loadTestData() throws -> TestData {
5050
func makeTransaction(from testData: TransactionTestData) -> Transaction {
5151
return Transaction(
5252
transactionType: .payment,
53-
sender: Address(
54-
address: testData.transaction.sender.address,
55-
pubKey: Data(testData.transaction.sender.pubKey)
56-
),
53+
sender: testData.transaction.sender,
5754
fee: testData.transaction.fee,
5855
firstValid: testData.transaction.firstValid,
5956
lastValid: testData.transaction.lastValid,
6057
genesisHash: Data(testData.transaction.genesisHash),
6158
genesisId: testData.transaction.genesisId,
6259
note: testData.transaction.note != nil ? Data(testData.transaction.note!) : nil,
63-
rekeyTo: testData.transaction.rekeyTo != nil
64-
? Address(
65-
address: testData.transaction.rekeyTo!.address,
66-
pubKey: Data(testData.transaction.rekeyTo!.pubKey)
67-
) : nil,
60+
rekeyTo: testData.transaction.rekeyTo,
6861
lease: testData.transaction.lease != nil ? Data(testData.transaction.lease!) : nil,
6962
group: testData.transaction.group != nil ? Data(testData.transaction.group!) : nil,
7063
payment: PaymentTransactionFields(
71-
receiver: Address(
72-
address: testData.transaction.payment.receiver.address,
73-
pubKey: Data(testData.transaction.payment.receiver.pubKey)
74-
),
64+
receiver: testData.transaction.payment.receiver,
7565
amount: testData.transaction.payment.amount,
7666
closeRemainderTo: nil
7767
),

0 commit comments

Comments
 (0)