Skip to content

Commit

Permalink
Force unwrap to remove optionals from the API.
Browse files Browse the repository at this point in the history
  • Loading branch information
keefertaylor committed Apr 26, 2019
1 parent 736aab3 commit 8e8d300
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
9 changes: 6 additions & 3 deletions Base58Swift/Base58.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public enum Base58 {
/// Encode the given bytes into a Base58Check encoded string.
/// - Parameter bytes: The bytes to encode.
/// - Returns: A base58check encoded string representing the given bytes, or nil if encoding failed.
public static func base58CheckEncode(_ bytes: [UInt8]) -> String? {
public static func base58CheckEncode(_ bytes: [UInt8]) -> String {
let checksum = calculateChecksum(bytes)
let checksummedBytes = bytes + checksum
return Base58.base58Encode(checksummedBytes)
Expand All @@ -43,7 +43,7 @@ public enum Base58 {
/// Encode the given bytes to a Base58 encoded string.
/// - Parameter bytes: The bytes to encode.
/// - Returns: A base58 encoded string representing the given bytes, or nil if encoding failed.
public static func base58Encode(_ bytes: [UInt8]) -> String? {
public static func base58Encode(_ bytes: [UInt8]) -> String {
var answer: [UInt8] = []
var integerBytes = BigUInt(Data(bytes))

Expand All @@ -56,7 +56,10 @@ public enum Base58 {
let prefix = Array(bytes.prefix { $0 == 0 }).map { _ in alphabet[0] }
answer.insert(contentsOf: prefix, at: 0)

return String(bytes: answer, encoding: String.Encoding.utf8)
// swiftlint:disable force_unwrapping
// Force unwrap as the given alphabet will always decode to UTF8.
return String(bytes: answer, encoding: String.Encoding.utf8)!
// swiftlint:enable force_unwrapping
}

/// Decode the given base58 encoded string to bytes.
Expand Down
11 changes: 2 additions & 9 deletions Base58SwiftTests/Base58Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ class Base58SwiftTests: XCTestCase {
public func testBase58EncodingForValidStrings() {
for (decoded, encoded) in validStringDecodedToEncodedTuples {
let bytes = [UInt8](decoded.utf8)
guard let result = Base58.base58Encode(bytes) else {
XCTFail()
return
}
let result = Base58.base58Encode(bytes)
XCTAssertEqual(result, encoded)
}
}
Expand Down Expand Up @@ -68,11 +65,7 @@ class Base58SwiftTests: XCTestCase {
6, 161, 159, 136, 34, 110, 33, 238, 14, 79, 14, 218, 133, 13, 109, 40, 194, 236, 153, 44, 61, 157, 254
]
let expectedOutput = "tz1Y3qqTg9HdrzZGbEjiCPmwuZ7fWVxpPtRw"

guard let actualOutput = Base58.base58CheckEncode(inputData) else {
XCTFail()
return
}
let actualOutput = Base58.base58CheckEncode(inputData)
XCTAssertEqual(actualOutput, expectedOutput)
}

Expand Down

0 comments on commit 8e8d300

Please sign in to comment.