From ab9a250c7e5a3eae0c80d060c886b795fb514042 Mon Sep 17 00:00:00 2001 From: Shun Usami Date: Fri, 13 Sep 2019 13:53:19 +0900 Subject: [PATCH] :recycle: Fix warnings - Unnecessary public/internal modifier - Deprecated encodedOffset - Switch fallthrough --- Sources/BitcoinKit/Core/Keys/Encoding.swift | 14 +++++---- Sources/BitcoinKit/Core/Serialization.swift | 4 +-- Sources/BitcoinKit/Messages/VarInt.swift | 4 +-- .../BitcoinKit/Scripts/ScriptFactory.swift | 30 +++++++++---------- .../BitcoinKitDataStoreProtocol.swift | 4 +-- 5 files changed, 29 insertions(+), 27 deletions(-) diff --git a/Sources/BitcoinKit/Core/Keys/Encoding.swift b/Sources/BitcoinKit/Core/Keys/Encoding.swift index 0bae9caa..f0be9ac7 100644 --- a/Sources/BitcoinKit/Core/Keys/Encoding.swift +++ b/Sources/BitcoinKit/Core/Keys/Encoding.swift @@ -115,7 +115,8 @@ extension Encoding { } for b in encodedBytes { - str += String(baseAlphabets[String.Index(encodedOffset: Int(b))]) + let index = String.Index(utf16Offset: Int(b), in: baseAlphabets) + str += String(baseAlphabets[index]) } return str @@ -133,9 +134,8 @@ extension Encoding { let size = sizeFromBase(size: string.lengthOfBytes(using: .utf8) - zerosCount) var decodedBytes: [UInt8] = Array(repeating: 0, count: size) for c in string { - guard let baseIndex = baseAlphabets.index(of: c) else { return nil } - - var carry = baseIndex.encodedOffset + guard let baseIndex: Int = baseAlphabets.index(of: c)?.utf16Offset(in: baseAlphabets) else { return nil } + var carry = baseIndex var i = 0 for j in (0...decodedBytes.count - 1).reversed() where carry != 0 || i < length { carry += base * Int(decodedBytes[j]) @@ -170,7 +170,8 @@ public struct Bech32 { let combined: Data = payload + checksum // Data of [UInt5] var base32 = "" for b in combined { - base32 += String(base32Alphabets[String.Index(encodedOffset: Int(b))]) + let index = String.Index(utf16Offset: Int(b), in: base32Alphabets) + base32 += String(base32Alphabets[index]) } return prefix + seperator + base32 @@ -194,7 +195,8 @@ public struct Bech32 { var decodedIn5bit: [UInt8] = [UInt8]() for c in base32.lowercased() { // We can't have characters other than base32 alphabets. - guard let baseIndex = base32Alphabets.index(of: c)?.encodedOffset else { + print(base32Alphabets) + guard let baseIndex = base32Alphabets.index(of: c)?.utf16Offset(in: base32Alphabets) else { return nil } decodedIn5bit.append(UInt8(baseIndex)) diff --git a/Sources/BitcoinKit/Core/Serialization.swift b/Sources/BitcoinKit/Core/Serialization.swift index d3bd1b17..a23c8970 100644 --- a/Sources/BitcoinKit/Core/Serialization.swift +++ b/Sources/BitcoinKit/Core/Serialization.swift @@ -113,9 +113,9 @@ extension Data { case 0xfe: value = UInt64(self[1...4].to(type: UInt32.self)) case 0xff: - fallthrough - default: value = self[1...8].to(type: UInt64.self) + default: + fatalError("This switch statement should be exhaustive without default clause") } return VarInt(value) } diff --git a/Sources/BitcoinKit/Messages/VarInt.swift b/Sources/BitcoinKit/Messages/VarInt.swift index 5e8403ba..6eaad4c2 100644 --- a/Sources/BitcoinKit/Messages/VarInt.swift +++ b/Sources/BitcoinKit/Messages/VarInt.swift @@ -63,10 +63,10 @@ public struct VarInt: ExpressibleByIntegerLiteral { length = 4 data = Data() + UInt8(0xfe).littleEndian + UInt32(value).littleEndian case 0x100000000...0xffffffffffffffff: - fallthrough - default: length = 8 data = Data() + UInt8(0xff).littleEndian + UInt64(value).littleEndian + default: + fatalError("This switch statement should be exhaustive without default clause") } } diff --git a/Sources/BitcoinKit/Scripts/ScriptFactory.swift b/Sources/BitcoinKit/Scripts/ScriptFactory.swift index c29f85e0..f765fc3f 100644 --- a/Sources/BitcoinKit/Scripts/ScriptFactory.swift +++ b/Sources/BitcoinKit/Scripts/ScriptFactory.swift @@ -38,24 +38,24 @@ public struct ScriptFactory { // MARK: - Standard public extension ScriptFactory.Standard { - public static func buildP2PK(publickey: PublicKey) -> Script? { + static func buildP2PK(publickey: PublicKey) -> Script? { return try? Script() .appendData(publickey.data) .append(.OP_CHECKSIG) } - public static func buildP2PKH(address: Address) -> Script? { + static func buildP2PKH(address: Address) -> Script? { return Script(address: address) } - public static func buildP2SH(script: Script) -> Script { + static func buildP2SH(script: Script) -> Script { return script.toP2SH() } - public static func buildMultiSig(publicKeys: [PublicKey]) -> Script? { + static func buildMultiSig(publicKeys: [PublicKey]) -> Script? { return Script(publicKeys: publicKeys, signaturesRequired: UInt(publicKeys.count)) } - public static func buildMultiSig(publicKeys: [PublicKey], signaturesRequired: UInt) -> Script? { + static func buildMultiSig(publicKeys: [PublicKey], signaturesRequired: UInt) -> Script? { return Script(publicKeys: publicKeys, signaturesRequired: signaturesRequired) } } @@ -63,7 +63,7 @@ public extension ScriptFactory.Standard { // MARK: - LockTime public extension ScriptFactory.LockTime { // Base - public static func build(script: Script, lockDate: Date) -> Script? { + static func build(script: Script, lockDate: Date) -> Script? { return try? Script() .appendData(lockDate.bigNumData) .append(.OP_CHECKLOCKTIMEVERIFY) @@ -71,13 +71,13 @@ public extension ScriptFactory.LockTime { .appendScript(script) } - public static func build(script: Script, lockIntervalSinceNow: TimeInterval) -> Script? { + static func build(script: Script, lockIntervalSinceNow: TimeInterval) -> Script? { let lockDate = Date(timeIntervalSinceNow: lockIntervalSinceNow) return build(script: script, lockDate: lockDate) } // P2PKH + LockTime - public static func build(address: Address, lockIntervalSinceNow: TimeInterval) -> Script? { + static func build(address: Address, lockIntervalSinceNow: TimeInterval) -> Script? { guard let p2pkh = Script(address: address) else { return nil } @@ -85,7 +85,7 @@ public extension ScriptFactory.LockTime { return build(script: p2pkh, lockDate: lockDate) } - public static func build(address: Address, lockDate: Date) -> Script? { + static func build(address: Address, lockDate: Date) -> Script? { guard let p2pkh = Script(address: address) else { return nil } @@ -95,7 +95,7 @@ public extension ScriptFactory.LockTime { // MARK: - OpReturn public extension ScriptFactory.OpReturn { - public static func build(text: String) -> Script? { + static func build(text: String) -> Script? { let MAX_OP_RETURN_DATA_SIZE: Int = 220 guard let data = text.data(using: .utf8), data.count <= MAX_OP_RETURN_DATA_SIZE else { return nil @@ -108,7 +108,7 @@ public extension ScriptFactory.OpReturn { // MARK: - Condition public extension ScriptFactory.Condition { - public static func build(scripts: [Script]) -> Script? { + static func build(scripts: [Script]) -> Script? { guard !scripts.isEmpty else { return nil @@ -160,7 +160,7 @@ public extension ScriptFactory.Condition { */ public extension ScriptFactory.HashedTimeLockedContract { // Base - public static func build(recipient: Address, sender: Address, lockDate: Date, hash: Data, hashOp: HashOperator) -> Script? { + static func build(recipient: Address, sender: Address, lockDate: Date, hash: Data, hashOp: HashOperator) -> Script? { guard hash.count == hashOp.hashSize else { return nil } @@ -186,18 +186,18 @@ public extension ScriptFactory.HashedTimeLockedContract { } // convenience - public static func build(recipient: Address, sender: Address, lockIntervalSinceNow: TimeInterval, hash: Data, hashOp: HashOperator) -> Script? { + static func build(recipient: Address, sender: Address, lockIntervalSinceNow: TimeInterval, hash: Data, hashOp: HashOperator) -> Script? { let lockDate = Date(timeIntervalSinceNow: lockIntervalSinceNow) return build(recipient: recipient, sender: sender, lockDate: lockDate, hash: hash, hashOp: hashOp) } - public static func build(recipient: Address, sender: Address, lockIntervalSinceNow: TimeInterval, secret: Data, hashOp: HashOperator) -> Script? { + static func build(recipient: Address, sender: Address, lockIntervalSinceNow: TimeInterval, secret: Data, hashOp: HashOperator) -> Script? { let hash = hashOp.hash(secret) let lockDate = Date(timeIntervalSinceNow: lockIntervalSinceNow) return build(recipient: recipient, sender: sender, lockDate: lockDate, hash: hash, hashOp: hashOp) } - public static func build(recipient: Address, sender: Address, lockDate: Date, secret: Data, hashOp: HashOperator) -> Script? { + static func build(recipient: Address, sender: Address, lockDate: Date, secret: Data, hashOp: HashOperator) -> Script? { let hash = hashOp.hash(secret) return build(recipient: recipient, sender: sender, lockDate: lockDate, hash: hash, hashOp: hashOp) } diff --git a/Sources/BitcoinKit/Wallet/Protocol/BitcoinKitDataStoreProtocol.swift b/Sources/BitcoinKit/Wallet/Protocol/BitcoinKitDataStoreProtocol.swift index d7e4cee6..520df5e1 100644 --- a/Sources/BitcoinKit/Wallet/Protocol/BitcoinKitDataStoreProtocol.swift +++ b/Sources/BitcoinKit/Wallet/Protocol/BitcoinKitDataStoreProtocol.swift @@ -37,10 +37,10 @@ internal enum DataStoreKey: String { } internal extension BitcoinKitDataStoreProtocol { - internal func getString(forKey key: DataStoreKey) -> String? { + func getString(forKey key: DataStoreKey) -> String? { return getString(forKey: key.rawValue) } - internal func setString(_ value: String, forKey key: DataStoreKey) { + func setString(_ value: String, forKey key: DataStoreKey) { setString(value, forKey: key.rawValue) } func getData(forKey key: DataStoreKey) -> Data? {