Skip to content

Commit

Permalink
Fix SwiftLint errors from latest SwiftLint release
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiu committed Sep 18, 2024
1 parent 8b40eb9 commit a5d8f55
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 21 deletions.
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) {

Check failure on line 153 in Sources/NostrSDK/Events/BookmarksListEvent.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Optional Data -> String Conversion Violation: Prefer failable `String(data:encoding:)` initializer when converting `Data` to `String` (optional_data_string_conversion)
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 {

Check failure on line 83 in Sources/NostrSDK/Events/GenericRepostEvent.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Optional Data -> String Conversion Violation: Prefer failable `String(data:encoding:)` initializer when converting `Data` to `String` (optional_data_string_conversion)
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 {

Check failure on line 122 in Sources/NostrSDK/Events/GiftWrap/GiftWrapEvent.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Optional Data -> String Conversion Violation: Prefer failable `String(data:encoding:)` initializer when converting `Data` to `String` (optional_data_string_conversion)
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 {

Check failure on line 99 in Sources/NostrSDK/Events/GiftWrap/SealEvent.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Optional Data -> String Conversion Violation: Prefer failable `String(data:encoding:)` initializer when converting `Data` to `String` (optional_data_string_conversion)
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 {

Check failure on line 187 in Sources/NostrSDK/Events/MetadataEvent.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Optional Data -> String Conversion Violation: Prefer failable `String(data:encoding:)` initializer when converting `Data` to `String` (optional_data_string_conversion)
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) {

Check failure on line 112 in Sources/NostrSDK/Events/MuteListEvent.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Optional Data -> String Conversion Violation: Prefer failable `String(data:encoding:)` initializer when converting `Data` to `String` (optional_data_string_conversion)
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
}

Check failure on line 35 in Sources/NostrSDK/RelayRequest.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Optional Data -> String Conversion Violation: Prefer failable `String(data:encoding:)` initializer when converting `Data` to `String` (optional_data_string_conversion)
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))

Check failure on line 22 in Tests/NostrSDKTests/FilterTests.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Optional Data -> String Conversion Violation: Prefer failable `String(data:encoding:)` initializer when converting `Data` to `String` (optional_data_string_conversion)

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))

Check failure on line 42 in Tests/NostrSDKTests/FilterTests.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Optional Data -> String Conversion Violation: Prefer failable `String(data:encoding:)` initializer when converting `Data` to `String` (optional_data_string_conversion)

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)

Check failure on line 30 in Tests/NostrSDKTests/FixtureLoading.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Optional Data -> String Conversion Violation: Prefer failable `String(data:encoding:)` initializer when converting `Data` to `String` (optional_data_string_conversion)
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 a5d8f55

Please sign in to comment.