Skip to content

Commit 889a07f

Browse files
committed
wip: format swift
1 parent c7112db commit 889a07f

File tree

4 files changed

+164
-163
lines changed

4 files changed

+164
-163
lines changed

packages/swift/AlgoKitTransact/Package.swift

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,37 @@
44
import PackageDescription
55

66
let package = Package(
7-
name: "AlgoKitTransact",
8-
products: [
9-
// Products define the executables and libraries a package produces, making them visible to other packages.
10-
.library(
11-
name: "AlgoKitTransact",
12-
targets: ["AlgoKitTransact"])
13-
],
14-
dependencies: [
15-
.package(url: "https://github.com/pebble8888/ed25519swift.git", from: "1.2.7")
16-
],
17-
targets: [
18-
// Targets are the basic building blocks of a package, defining a module or a test suite.
19-
// Targets can depend on other targets in this package and products from dependencies.
20-
.binaryTarget(
21-
name: "algokit_transactFFI",
22-
path: "Frameworks/algokit_transact.xcframework"
23-
),
24-
.target(
25-
name: "AlgoKitTransact",
26-
dependencies: ["algokit_transactFFI"],
27-
path: "Sources/AlgoKitTransact"
28-
),
29-
.testTarget(
30-
name: "AlgoKitTransactTests",
31-
dependencies: [
32-
"AlgoKitTransact",
33-
"ed25519swift",
34-
],
35-
resources: [
36-
.process("Resources/test_data.json")
37-
]
38-
),
39-
]
7+
name: "AlgoKitTransact",
8+
products: [
9+
// Products define the executables and libraries a package produces, making them visible to other packages.
10+
.library(
11+
name: "AlgoKitTransact",
12+
targets: ["AlgoKitTransact"])
13+
],
14+
dependencies: [
15+
.package(url: "https://github.com/pebble8888/ed25519swift.git", from: "1.2.7")
16+
],
17+
targets: [
18+
// Targets are the basic building blocks of a package, defining a module or a test suite.
19+
// Targets can depend on other targets in this package and products from dependencies.
20+
.binaryTarget(
21+
name: "algokit_transactFFI",
22+
path: "Frameworks/algokit_transact.xcframework"
23+
),
24+
.target(
25+
name: "AlgoKitTransact",
26+
dependencies: ["algokit_transactFFI"],
27+
path: "Sources/AlgoKitTransact"
28+
),
29+
.testTarget(
30+
name: "AlgoKitTransactTests",
31+
dependencies: [
32+
"AlgoKitTransact",
33+
"ed25519swift",
34+
],
35+
resources: [
36+
.process("Resources/test_data.json")
37+
]
38+
),
39+
]
4040
)

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@ import Testing
99

1010
@Test("Generic Transaction: malformed bytes")
1111
func genericTransactionMalformedBytes() throws {
12-
let testData = try loadTestData()
13-
let simplePayment = testData.simplePayment
14-
let badBytes = Data(simplePayment.unsignedBytes[13..<37])
15-
do {
16-
_ = try decodeTransaction(bytes: badBytes)
17-
#expect(Bool(false), "Expected DecodingError to be thrown")
18-
} catch AlgoKitTransactError.DecodingError {
19-
// Success - expected error was thrown
20-
#expect(Bool(true))
21-
}
12+
let testData = try loadTestData()
13+
let simplePayment = testData.simplePayment
14+
let badBytes = Data(simplePayment.unsignedBytes[13..<37])
15+
do {
16+
_ = try decodeTransaction(bytes: badBytes)
17+
#expect(Bool(false), "Expected DecodingError to be thrown")
18+
} catch AlgoKitTransactError.DecodingError {
19+
// Success - expected error was thrown
20+
#expect(Bool(true))
21+
}
2222
}
2323

2424
@Test("Generic Transaction: encode 0 bytes")
2525
func genericTransactionEncode0Bytes() throws {
26-
do {
27-
_ = try decodeTransaction(bytes: Data())
28-
#expect(Bool(false), "Expected DecodingError to be thrown")
29-
} catch AlgoKitTransactError.DecodingError(let message) {
30-
#expect(message == "attempted to decode 0 bytes")
31-
}
26+
do {
27+
_ = try decodeTransaction(bytes: Data())
28+
#expect(Bool(false), "Expected DecodingError to be thrown")
29+
} catch AlgoKitTransactError.DecodingError(let message) {
30+
#expect(message == "attempted to decode 0 bytes")
31+
}
3232
}

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

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,82 +10,82 @@ import ed25519swift
1010

1111
@Test("Payment: example")
1212
func paymentExample() throws {
13-
let aliceKeyPair = Ed25519.generateKeyPair()
14-
let alice = try addressFromPubKey(pubKey: Data(aliceKeyPair.publicKey))
15-
let bob = try addressFromString(
16-
address: "B72WNFFEZ7EOGMQPP7ROHYS3DSLL5JW74QASYNWGZGQXWRPJECJJLJIJ2Y"
17-
)
13+
let aliceKeyPair = Ed25519.generateKeyPair()
14+
let alice = try addressFromPubKey(pubKey: Data(aliceKeyPair.publicKey))
15+
let bob = try addressFromString(
16+
address: "B72WNFFEZ7EOGMQPP7ROHYS3DSLL5JW74QASYNWGZGQXWRPJECJJLJIJ2Y"
17+
)
1818

19-
let txn: Transaction = Transaction(
20-
transactionType: .payment,
21-
sender: alice,
22-
fee: 1000,
23-
firstValid: 1337,
24-
lastValid: 1347,
25-
genesisHash: Data(repeating: 65, count: 32), // pretend this is a valid hash
26-
genesisId: "localnet",
27-
payment: PaymentTransactionFields(
28-
receiver: bob,
29-
amount: 1337
30-
)
19+
let txn: Transaction = Transaction(
20+
transactionType: .payment,
21+
sender: alice,
22+
fee: 1000,
23+
firstValid: 1337,
24+
lastValid: 1347,
25+
genesisHash: Data(repeating: 65, count: 32), // pretend this is a valid hash
26+
genesisId: "localnet",
27+
payment: PaymentTransactionFields(
28+
receiver: bob,
29+
amount: 1337
3130
)
31+
)
3232

33-
let sig = Ed25519.sign(
34-
message: [UInt8](try encodeTransaction(tx: txn)), secretKey: aliceKeyPair.secretKey)
33+
let sig = Ed25519.sign(
34+
message: [UInt8](try encodeTransaction(tx: txn)), secretKey: aliceKeyPair.secretKey)
3535

36-
let signedTxn = try attachSignature(
37-
encodedTx: try encodeTransaction(tx: txn),
38-
signature: Data(sig)
39-
)
36+
let signedTxn = try attachSignature(
37+
encodedTx: try encodeTransaction(tx: txn),
38+
signature: Data(sig)
39+
)
4040

41-
#expect(signedTxn.count > 0)
41+
#expect(signedTxn.count > 0)
4242
}
4343

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

5252
@Test("Payment: decode without prefix")
5353
func paymentDecodeWithoutPrefix() throws {
54-
let testData = try loadTestData()
55-
let simplePayment = testData.simplePayment
56-
let transaction = makeTransaction(from: simplePayment)
57-
let bytesWithoutPrefix = Data(simplePayment.unsignedBytes.dropFirst(2))
58-
let decoded = try decodeTransaction(bytes: bytesWithoutPrefix)
59-
#expect(decoded == transaction)
54+
let testData = try loadTestData()
55+
let simplePayment = testData.simplePayment
56+
let transaction = makeTransaction(from: simplePayment)
57+
let bytesWithoutPrefix = Data(simplePayment.unsignedBytes.dropFirst(2))
58+
let decoded = try decodeTransaction(bytes: bytesWithoutPrefix)
59+
#expect(decoded == transaction)
6060
}
6161

6262
@Test("Payment: decode with prefix")
6363
func paymentDecodeWithPrefix() throws {
64-
let testData = try loadTestData()
65-
let simplePayment = testData.simplePayment
66-
let transaction = makeTransaction(from: simplePayment)
67-
let decoded = try decodeTransaction(bytes: Data(simplePayment.unsignedBytes))
68-
#expect(decoded == transaction)
64+
let testData = try loadTestData()
65+
let simplePayment = testData.simplePayment
66+
let transaction = makeTransaction(from: simplePayment)
67+
let decoded = try decodeTransaction(bytes: Data(simplePayment.unsignedBytes))
68+
#expect(decoded == transaction)
6969
}
7070

7171
@Test("Payment: encode with signature")
7272
func paymentEncodeWithSignature() throws {
73-
let testData = try loadTestData()
74-
let simplePayment = testData.simplePayment
75-
let signature = Ed25519.sign(
76-
message: simplePayment.unsignedBytes, secretKey: simplePayment.signingPrivateKey)
77-
let signedTx = try attachSignature(
78-
encodedTx: Data(simplePayment.unsignedBytes),
79-
signature: Data(signature)
80-
)
81-
#expect([UInt8](signedTx) == simplePayment.signedBytes)
73+
let testData = try loadTestData()
74+
let simplePayment = testData.simplePayment
75+
let signature = Ed25519.sign(
76+
message: simplePayment.unsignedBytes, secretKey: simplePayment.signingPrivateKey)
77+
let signedTx = try attachSignature(
78+
encodedTx: Data(simplePayment.unsignedBytes),
79+
signature: Data(signature)
80+
)
81+
#expect([UInt8](signedTx) == simplePayment.signedBytes)
8282
}
8383

8484
@Test("Payment: encode")
8585
func paymentEncode() throws {
86-
let testData = try loadTestData()
87-
let simplePayment = testData.simplePayment
88-
let transaction = makeTransaction(from: simplePayment)
89-
let encoded = try encodeTransaction(tx: transaction)
90-
#expect([UInt8](encoded) == simplePayment.unsignedBytes)
86+
let testData = try loadTestData()
87+
let simplePayment = testData.simplePayment
88+
let transaction = makeTransaction(from: simplePayment)
89+
let encoded = try encodeTransaction(tx: transaction)
90+
#expect([UInt8](encoded) == simplePayment.unsignedBytes)
9191
}

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

Lines changed: 62 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,78 @@ import Foundation
33
@testable import AlgoKitTransact
44

55
struct TransactionTestData: Codable {
6-
struct AddressData: Codable {
7-
let address: String
8-
let pubKey: [UInt8]
9-
}
6+
struct AddressData: Codable {
7+
let address: String
8+
let pubKey: [UInt8]
9+
}
1010

11-
struct TransactionData: Codable {
12-
let sender: AddressData
13-
let fee: UInt64
14-
let transactionType: String
15-
let firstValid: UInt64
16-
let lastValid: UInt64
17-
let genesisHash: [UInt8]
18-
let genesisId: String
19-
let note: [UInt8]?
20-
let rekeyTo: AddressData?
21-
let lease: [UInt8]?
22-
let group: [UInt8]?
23-
let payment: PaymentFieldsData
24-
}
11+
struct TransactionData: Codable {
12+
let sender: AddressData
13+
let fee: UInt64
14+
let transactionType: String
15+
let firstValid: UInt64
16+
let lastValid: UInt64
17+
let genesisHash: [UInt8]
18+
let genesisId: String
19+
let note: [UInt8]?
20+
let rekeyTo: AddressData?
21+
let lease: [UInt8]?
22+
let group: [UInt8]?
23+
let payment: PaymentFieldsData
24+
}
2525

26-
struct PaymentFieldsData: Codable {
27-
let receiver: AddressData
28-
let amount: UInt64
29-
}
26+
struct PaymentFieldsData: Codable {
27+
let receiver: AddressData
28+
let amount: UInt64
29+
}
3030

31-
let transaction: TransactionData
32-
let id: String
33-
let idRaw: [UInt8]
34-
let unsignedBytes: [UInt8];
35-
let signedBytes: [UInt8];
36-
let signingPrivateKey: [UInt8];
31+
let transaction: TransactionData
32+
let id: String
33+
let idRaw: [UInt8]
34+
let unsignedBytes: [UInt8]
35+
let signedBytes: [UInt8]
36+
let signingPrivateKey: [UInt8]
3737
}
3838

3939
struct TestData: Codable {
40-
let simplePayment: TransactionTestData
40+
let simplePayment: TransactionTestData
4141
}
4242

4343
func loadTestData() throws -> TestData {
44-
let testDataURL = Bundle.module.url(forResource: "test_data", withExtension: "json")!
45-
let data = try Data(contentsOf: testDataURL)
46-
let decoder = JSONDecoder()
47-
return try decoder.decode(TestData.self, from: data)
44+
let testDataURL = Bundle.module.url(forResource: "test_data", withExtension: "json")!
45+
let data = try Data(contentsOf: testDataURL)
46+
let decoder = JSONDecoder()
47+
return try decoder.decode(TestData.self, from: data)
4848
}
4949

5050
func makeTransaction(from testData: TransactionTestData) -> Transaction {
51-
return Transaction(
52-
transactionType: .payment,
53-
sender: Address(
54-
address: testData.transaction.sender.address,
55-
pubKey: Data(testData.transaction.sender.pubKey)
56-
),
57-
fee: testData.transaction.fee,
58-
firstValid: testData.transaction.firstValid,
59-
lastValid: testData.transaction.lastValid,
60-
genesisHash: Data(testData.transaction.genesisHash),
61-
genesisId: testData.transaction.genesisId,
62-
note: testData.transaction.note != nil ? Data(testData.transaction.note!) : nil,
63-
rekeyTo: testData.transaction.rekeyTo != nil ? Address(
64-
address: testData.transaction.rekeyTo!.address,
65-
pubKey: Data(testData.transaction.rekeyTo!.pubKey)
66-
) : nil,
67-
lease: testData.transaction.lease != nil ? Data(testData.transaction.lease!) : nil,
68-
group: testData.transaction.group != nil ? Data(testData.transaction.group!) : nil,
69-
payment: PaymentTransactionFields(
70-
receiver: Address(
71-
address: testData.transaction.payment.receiver.address,
72-
pubKey: Data(testData.transaction.payment.receiver.pubKey)
73-
),
74-
amount: testData.transaction.payment.amount,
75-
closeRemainderTo: nil
76-
),
77-
assetTransfer: nil
78-
)
51+
return Transaction(
52+
transactionType: .payment,
53+
sender: Address(
54+
address: testData.transaction.sender.address,
55+
pubKey: Data(testData.transaction.sender.pubKey)
56+
),
57+
fee: testData.transaction.fee,
58+
firstValid: testData.transaction.firstValid,
59+
lastValid: testData.transaction.lastValid,
60+
genesisHash: Data(testData.transaction.genesisHash),
61+
genesisId: testData.transaction.genesisId,
62+
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,
68+
lease: testData.transaction.lease != nil ? Data(testData.transaction.lease!) : nil,
69+
group: testData.transaction.group != nil ? Data(testData.transaction.group!) : nil,
70+
payment: PaymentTransactionFields(
71+
receiver: Address(
72+
address: testData.transaction.payment.receiver.address,
73+
pubKey: Data(testData.transaction.payment.receiver.pubKey)
74+
),
75+
amount: testData.transaction.payment.amount,
76+
closeRemainderTo: nil
77+
),
78+
assetTransfer: nil
79+
)
7980
}

0 commit comments

Comments
 (0)