Skip to content

Commit

Permalink
♻️ Fix warnings
Browse files Browse the repository at this point in the history
- Unnecessary public/internal modifier
- Deprecated encodedOffset
- Switch fallthrough
  • Loading branch information
usatie committed Sep 13, 2019
1 parent e5de492 commit ab9a250
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
14 changes: 8 additions & 6 deletions Sources/BitcoinKit/Core/Keys/Encoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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])
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions Sources/BitcoinKit/Core/Serialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/BitcoinKit/Messages/VarInt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
30 changes: 15 additions & 15 deletions Sources/BitcoinKit/Scripts/ScriptFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,54 +38,54 @@ 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)
}
}

// 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)
.append(.OP_DROP)
.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
}
let lockDate = Date(timeIntervalSinceNow: lockIntervalSinceNow)
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
}
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down

0 comments on commit ab9a250

Please sign in to comment.