Skip to content

Commit

Permalink
Fix SwiftLint errors from latest SwiftLint release (#180)
Browse files Browse the repository at this point in the history
* Fix SwiftLint errors from latest SwiftLint release

* Fix GitHub action workflows to trigger on only pull_request
  • Loading branch information
tyiu authored Sep 18, 2024
1 parent 8b40eb9 commit 69d4f51
Show file tree
Hide file tree
Showing 15 changed files with 41 additions and 29 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
name: SwiftLint

on:
push:
branches: [ '**' ]
pull_request_target:
branches: [ '**' ]
pull_request:
types: [opened, synchronize, reopened]
workflow_dispatch:

jobs:
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/unit.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
name: Unit Tests

on:
push:
branches: [ '**' ]
pull_request_target:
branches: [ '**' ]
pull_request:
types: [opened, synchronize, reopened]
workflow_dispatch:

jobs:
Expand Down
4 changes: 2 additions & 2 deletions Sources/NostrSDK/EventSerializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ public enum EventSerializer {

let tagsString: String
if let tagsData = try? encoder.encode(tags) {
tagsString = String(decoding: tagsData, as: UTF8.self)
tagsString = String(data: tagsData, encoding: .utf8) ?? "[]"
} else {
tagsString = "[]"
}

let contentString: String
if let contentData = try? encoder.encode(content) {
contentString = String(decoding: contentData, as: UTF8.self)
contentString = String(data: contentData, encoding: .utf8) ?? "\"\""
} else {
contentString = "\"\""
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/NostrSDK/Events/BookmarksListEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ public extension EventCreating {
var encryptedContent: String?
if !privateTags.isEmpty {
let rawPrivateTags = privateTags.map { $0.raw }
if let unencryptedData = try? JSONSerialization.data(withJSONObject: rawPrivateTags) {
let unencryptedContent = String(decoding: unencryptedData, as: UTF8.self)
if let unencryptedData = try? JSONSerialization.data(withJSONObject: rawPrivateTags),
let unencryptedContent = String(data: unencryptedData, encoding: .utf8) {
encryptedContent = try legacyEncrypt(content: unencryptedContent,
privateKey: keypair.privateKey,
publicKey: keypair.publicKey)
Expand Down
4 changes: 3 additions & 1 deletion Sources/NostrSDK/Events/GenericRepostEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public extension EventCreating {
/// See [NIP-18](https://github.com/nostr-protocol/nips/blob/master/18.md#reposts).
func repost(event: NostrEvent, signedBy keypair: Keypair) throws -> GenericRepostEvent {
let jsonData = try JSONEncoder().encode(event)
let stringifiedJSON = String(decoding: jsonData, as: UTF8.self)
guard let stringifiedJSON = String(data: jsonData, encoding: .utf8) else {
throw EventCreatingError.invalidInput
}
var tags: [Tag] = [
.event(event.id),
.pubkey(event.pubkey)
Expand Down
4 changes: 3 additions & 1 deletion Sources/NostrSDK/Events/GiftWrap/GiftWrapEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ public extension EventCreating {
signedBy keypair: Keypair
) throws -> GiftWrapEvent {
let jsonData = try JSONEncoder().encode(seal)
let stringifiedJSON = String(decoding: jsonData, as: UTF8.self)
guard let stringifiedJSON = String(data: jsonData, encoding: .utf8) else {
throw GiftWrapError.utf8EncodingFailed
}

guard let randomKeypair = Keypair() else {
throw GiftWrapError.keypairGenerationFailed
Expand Down
5 changes: 4 additions & 1 deletion Sources/NostrSDK/Events/GiftWrap/SealEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ public extension EventCreating {
}

let jsonData = try JSONEncoder().encode(rumor)
let stringifiedJSON = String(decoding: jsonData, as: UTF8.self)
guard let stringifiedJSON = String(data: jsonData, encoding: .utf8) else {
throw SealEventError.utf8EncodingFailed
}

let encryptedRumor = try encrypt(plaintext: stringifiedJSON, privateKeyA: keypair.privateKey, publicKeyB: recipient)
return try SealEvent(content: encryptedRumor, createdAt: createdAt, signedBy: keypair)
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/NostrSDK/Events/MetadataEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ public extension MetadataEvent {
allUserMetadataAsData = try JSONSerialization.data(withJSONObject: userMetadataAsDictionary, options: .sortedKeys)
}

let allUserMetadataAsString = String(decoding: allUserMetadataAsData, as: UTF8.self)
guard let allUserMetadataAsString = String(data: allUserMetadataAsData, encoding: .utf8) else {
throw EventCreatingError.invalidInput
}

content(allUserMetadataAsString)

return self
Expand Down
4 changes: 2 additions & 2 deletions Sources/NostrSDK/Events/MuteListEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public extension EventCreating {
var encryptedContent: String?
if !privateTags.isEmpty {
let rawPrivateTags = privateTags.map { $0.raw }
if let unencryptedData = try? JSONSerialization.data(withJSONObject: rawPrivateTags) {
let unencryptedContent = String(decoding: unencryptedData, as: UTF8.self)
if let unencryptedData = try? JSONSerialization.data(withJSONObject: rawPrivateTags),
let unencryptedContent = String(data: unencryptedData, encoding: .utf8) {
encryptedContent = try legacyEncrypt(content: unencryptedContent,
privateKey: keypair.privateKey,
publicKey: keypair.publicKey)
Expand Down
5 changes: 3 additions & 2 deletions Sources/NostrSDK/LegacyDirectMessageEncrypting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ public extension LegacyDirectMessageEncrypting {
let ivContentTrimmed = ivContent.dropFirst(3)

guard let ivContentData = Data(base64Encoded: String(ivContentTrimmed)),
let decryptedContentData = AESDecrypt(data: encryptedContentData.bytes, iv: ivContentData.bytes, sharedSecret: sharedSecret) else {
let decryptedContentData = AESDecrypt(data: encryptedContentData.bytes, iv: ivContentData.bytes, sharedSecret: sharedSecret),
let decodedContent = String(data: decryptedContentData, encoding: .utf8) else {
throw LegacyDirectMessageEncryptingError.decryptionError
}

return String(decoding: decryptedContentData, as: UTF8.self)
return decodedContent
}

private func getSharedSecret(privateKey: PrivateKey, recipient pubkey: PublicKey) throws -> [UInt8] {
Expand Down
6 changes: 3 additions & 3 deletions Sources/NostrSDK/NIP44v2Encrypting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ extension NIP44v2Encrypting {

guard unpaddedLength > 0,
unpadded.count == unpaddedLength,
padded.count == 2 + paddedLength
else {
padded.count == 2 + paddedLength,
let result = String(data: Data(unpadded), encoding: .utf8) else {
throw NIP44v2EncryptingError.paddingInvalid
}

return String(decoding: Data(unpadded), as: UTF8.self)
return result
}

func decodePayload(_ payload: String) throws -> DecodedPayload {
Expand Down
5 changes: 3 additions & 2 deletions Sources/NostrSDK/RelayRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ enum RelayRequest {
payload = [AnyEncodable("COUNT"), AnyEncodable(subscriptionId), AnyEncodable(filter)]
}

guard let data = try? JSONEncoder().encode(payload) else {
guard let data = try? JSONEncoder().encode(payload),
let decoded = String(data: data, encoding: .utf8) else {
return nil
}
return String(decoding: data, as: UTF8.self).trimmingCharacters(in: .whitespacesAndNewlines)
return decoded.trimmingCharacters(in: .whitespacesAndNewlines)
}
}
2 changes: 1 addition & 1 deletion Sources/NostrSDK/WebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ final class WebSocket: NSObject, URLSessionWebSocketDelegate {

let reasonString: String?
if let reason {
reasonString = String(decoding: reason, as: UTF8.self)
reasonString = String(data: reason, encoding: .utf8)
} else {
reasonString = nil
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/NostrSDKTests/FilterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class FilterTests: XCTestCase, FixtureLoading, JSONTesting {

let encoder = JSONEncoder()
let result = try encoder.encode(filter)
let resultString = String(decoding: result, as: UTF8.self)
let resultString = try XCTUnwrap(String(data: result, encoding: .utf8))

XCTAssertTrue(areEquivalentJSONObjectStrings(expected, resultString))
}
Expand All @@ -39,7 +39,7 @@ final class FilterTests: XCTestCase, FixtureLoading, JSONTesting {

let encoder = JSONEncoder()
let result = try encoder.encode(filter)
let resultString = String(decoding: result, as: UTF8.self)
let resultString = try XCTUnwrap(String(data: result, encoding: .utf8))

XCTAssertTrue(areEquivalentJSONObjectStrings(expected, resultString))
}
Expand Down
6 changes: 5 additions & 1 deletion Tests/NostrSDKTests/FixtureLoading.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ extension FixtureLoading {

func loadFixtureString(_ filename: String) throws -> String? {
let data = try loadFixtureData(filename)
let originalString = String(decoding: data, as: UTF8.self)

guard let originalString = String(data: data, encoding: .utf8) else {
throw FixtureLoadingError.decodingError
}

let trimmedString = originalString.filter { !"\n\t\r".contains($0) }
return trimmedString
}
Expand Down

0 comments on commit 69d4f51

Please sign in to comment.