Skip to content

Commit 471b872

Browse files
committed
♻️ Update to Swift5
iOS/Linux - Data(bytes:) to Data() - index(of:) to firstIndex(of:) - Refactor deprecated withUnsafeBytes() methods. UnsafePointer<T> -> UnsafeRawBufferPointer Linux - RunLoopMode to RunLoop.Mode - Update Package.swift swift-tools-version to 5.0
1 parent ab9a250 commit 471b872

File tree

12 files changed

+75
-76
lines changed

12 files changed

+75
-76
lines changed

BitcoinKit.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@
209209
6E20AED72112D417008A9810 /* MurmurHashTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E20AED62112D417008A9810 /* MurmurHashTests.swift */; };
210210
6E797C452116C8A5003BEDFD /* OpCodeFactoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E797C442116C8A5003BEDFD /* OpCodeFactoryTests.swift */; };
211211
6EE789DB2112C1E500EAB620 /* CryptoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EE789DA2112C1E500EAB620 /* CryptoTests.swift */; };
212+
A201ABB6222ACF7100DEFE13 /* BitcoinKitPrivateSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201ABB5222ACF7100DEFE13 /* BitcoinKitPrivateSwift.swift */; };
212213
A20B537F211C72F9009D147A /* UInt256Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A20B537E211C72F9009D147A /* UInt256Tests.swift */; };
213214
A246ED102118621B0000418D /* MerkleTree.swift in Sources */ = {isa = PBXBuildFile; fileRef = A246ED0F2118621B0000418D /* MerkleTree.swift */; };
214215
A246ED12211864500000418D /* UInt256.swift in Sources */ = {isa = PBXBuildFile; fileRef = A246ED11211864500000418D /* UInt256.swift */; };
@@ -220,7 +221,6 @@
220221
A2DE5110212AFCF200F54EA0 /* UInt256+BitcoinTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2DE510F212AFCF200F54EA0 /* UInt256+BitcoinTests.swift */; };
221222
A2E11231212C400A00A0A40A /* ProofOfWorkTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2E11230212C400A00A0A40A /* ProofOfWorkTests.swift */; };
222223
A2F06FBA2126785200DFF652 /* UInt32+Utility.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2F06FB92126785200DFF652 /* UInt32+Utility.swift */; };
223-
A201ABB6222ACF7100DEFE13 /* BitcoinKitPrivateSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201ABB5222ACF7100DEFE13 /* BitcoinKitPrivateSwift.swift */; };
224224
CF432AF220F0CF9100AD4020 /* Base58Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CF432AF120F0CF9100AD4020 /* Base58Tests.swift */; };
225225
CF432AF420F0DFAC00AD4020 /* Bech32Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CF432AF320F0DFAC00AD4020 /* Bech32Tests.swift */; };
226226
CF432AF620F0ED4500AD4020 /* AddressTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CF432AF520F0ED4500AD4020 /* AddressTests.swift */; };
@@ -455,6 +455,7 @@
455455
6E20AED62112D417008A9810 /* MurmurHashTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MurmurHashTests.swift; sourceTree = "<group>"; };
456456
6E797C442116C8A5003BEDFD /* OpCodeFactoryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpCodeFactoryTests.swift; sourceTree = "<group>"; };
457457
6EE789DA2112C1E500EAB620 /* CryptoTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CryptoTests.swift; sourceTree = "<group>"; };
458+
A201ABB5222ACF7100DEFE13 /* BitcoinKitPrivateSwift.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = BitcoinKitPrivateSwift.swift; path = Core/BitcoinKitPrivateSwift.swift; sourceTree = "<group>"; };
458459
A20B537E211C72F9009D147A /* UInt256Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UInt256Tests.swift; sourceTree = "<group>"; };
459460
A246ED0F2118621B0000418D /* MerkleTree.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MerkleTree.swift; sourceTree = "<group>"; };
460461
A246ED11211864500000418D /* UInt256.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UInt256.swift; sourceTree = "<group>"; };
@@ -466,7 +467,6 @@
466467
A2DE510F212AFCF200F54EA0 /* UInt256+BitcoinTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UInt256+BitcoinTests.swift"; sourceTree = "<group>"; };
467468
A2E11230212C400A00A0A40A /* ProofOfWorkTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProofOfWorkTests.swift; sourceTree = "<group>"; };
468469
A2F06FB92126785200DFF652 /* UInt32+Utility.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UInt32+Utility.swift"; sourceTree = "<group>"; };
469-
A201ABB5222ACF7100DEFE13 /* BitcoinKitPrivateSwift.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = BitcoinKitPrivateSwift.swift; path = Core/BitcoinKitPrivateSwift.swift; sourceTree = "<group>"; };
470470
CF432AF120F0CF9100AD4020 /* Base58Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base58Tests.swift; sourceTree = "<group>"; };
471471
CF432AF320F0DFAC00AD4020 /* Bech32Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Bech32Tests.swift; sourceTree = "<group>"; };
472472
CF432AF520F0ED4500AD4020 /* AddressTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddressTests.swift; sourceTree = "<group>"; };
@@ -1488,7 +1488,7 @@
14881488
SKIP_INSTALL = YES;
14891489
SWIFT_INCLUDE_PATHS = "$(SRCROOT)/Libraries";
14901490
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
1491-
SWIFT_VERSION = 4.2;
1491+
SWIFT_VERSION = 5.0;
14921492
TARGETED_DEVICE_FAMILY = "1,2";
14931493
};
14941494
name = Debug;
@@ -1526,7 +1526,7 @@
15261526
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
15271527
SKIP_INSTALL = YES;
15281528
SWIFT_INCLUDE_PATHS = "$(SRCROOT)/Libraries";
1529-
SWIFT_VERSION = 4.2;
1529+
SWIFT_VERSION = 5.0;
15301530
TARGETED_DEVICE_FAMILY = "1,2";
15311531
};
15321532
name = Release;

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:4.0
1+
// swift-tools-version:5.0
22
import PackageDescription
33

44
let package = Package(
@@ -25,5 +25,5 @@ let package = Package(
2525
dependencies: ["BitcoinKit"]
2626
)
2727
],
28-
swiftLanguageVersions: [4]
28+
swiftLanguageVersions: [.v5]
2929
)

Sources/BitcoinKit/Core/BitcoinKitPrivateSwift.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class _SwiftKey {
5050
if outputlen != 33 {
5151
return Data()
5252
}
53-
return Data(bytes: serializedPubkey)
53+
return Data(serializedPubkey)
5454
} else {
5555
var serializedPubkey = [UInt8](repeating: 0, count: 65)
5656
var outputlen = 65
@@ -60,7 +60,7 @@ class _SwiftKey {
6060
if outputlen != 65 {
6161
return Data()
6262
}
63-
return Data(bytes: serializedPubkey)
63+
return Data(serializedPubkey)
6464
}
6565
}
6666
}
@@ -110,7 +110,7 @@ class _HDKey {
110110
if secp256k1_ec_privkey_tweak_add(ctx, &privateKeyBytes, &derivedPrivateKeyBytes) == 0 {
111111
return nil
112112
}
113-
result = Data(bytes: privateKeyBytes)
113+
result = Data(privateKeyBytes)
114114
} else {
115115
guard let ctx = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_VERIFY)) else {
116116
return nil
@@ -129,10 +129,10 @@ class _HDKey {
129129
if secp256k1_ec_pubkey_serialize(ctx, &compressedPublicKeyBytes, &compressedPublicKeyBytesLen, &secpPubkey, UInt32(SECP256K1_EC_COMPRESSED)) == 0 {
130130
return nil
131131
}
132-
result = Data(bytes: compressedPublicKeyBytes)
132+
result = Data(compressedPublicKeyBytes)
133133
}
134-
let fingerPrint: UInt32 = _Hash.sha256ripemd160(publicKey).withUnsafeBytes { $0.pointee }
135-
return _HDKey(privateKey: result, publicKey: result, chainCode: Data(bytes: derivedChainCode), depth: self.depth + 1, fingerprint: fingerPrint, childIndex: childIndex)
134+
let fingerPrint: UInt32 = _Hash.sha256ripemd160(publicKey).to(type: UInt32.self)
135+
return _HDKey(privateKey: result, publicKey: result, chainCode: Data(derivedChainCode), depth: self.depth + 1, fingerprint: fingerPrint, childIndex: childIndex)
136136
}
137137
}
138138
#endif

Sources/BitcoinKit/Core/BlockStore.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,10 @@ public class SQLiteBlockStore: BlockStore {
291291
public func addBlock(_ block: BlockMessage, hash: Data) throws {
292292
let stmt = statements["addBlock"]
293293

294-
try execute { hash.withUnsafeBytes { sqlite3_bind_blob(stmt, 1, $0, Int32(hash.count), SQLITE_TRANSIENT) } }
294+
try execute { hash.withUnsafeBytes { sqlite3_bind_blob(stmt, 1, $0.baseAddress.unsafelyUnwrapped, Int32(hash.count), SQLITE_TRANSIENT) } }
295295
try execute { sqlite3_bind_int64(stmt, 2, sqlite3_int64(bitPattern: UInt64(truncatingIfNeeded: block.version))) }
296-
try execute { block.prevBlock.withUnsafeBytes { sqlite3_bind_blob(stmt, 3, $0, Int32(block.prevBlock.count), SQLITE_TRANSIENT) } }
297-
try execute { block.merkleRoot.withUnsafeBytes { sqlite3_bind_blob(stmt, 4, $0, Int32(block.merkleRoot.count), SQLITE_TRANSIENT) } }
296+
try execute { block.prevBlock.withUnsafeBytes { sqlite3_bind_blob(stmt, 3, $0.baseAddress.unsafelyUnwrapped, Int32(block.prevBlock.count), SQLITE_TRANSIENT) } }
297+
try execute { block.merkleRoot.withUnsafeBytes { sqlite3_bind_blob(stmt, 4, $0.baseAddress.unsafelyUnwrapped, Int32(block.merkleRoot.count), SQLITE_TRANSIENT) } }
298298
try execute { sqlite3_bind_int64(stmt, 5, sqlite3_int64(bitPattern: UInt64(truncatingIfNeeded: block.timestamp))) }
299299
try execute { sqlite3_bind_int64(stmt, 6, sqlite3_int64(bitPattern: UInt64(truncatingIfNeeded: block.bits))) }
300300
try execute { sqlite3_bind_int64(stmt, 7, sqlite3_int64(bitPattern: UInt64(truncatingIfNeeded: block.nonce))) }
@@ -307,20 +307,20 @@ public class SQLiteBlockStore: BlockStore {
307307
public func addMerkleBlock(_ merkleBlock: MerkleBlockMessage, hash: Data) throws {
308308
let stmt = statements["addMerkleBlock"]
309309

310-
try execute { hash.withUnsafeBytes { sqlite3_bind_blob(stmt, 1, $0, Int32(hash.count), SQLITE_TRANSIENT) } }
310+
try execute { hash.withUnsafeBytes { sqlite3_bind_blob(stmt, 1, $0.baseAddress.unsafelyUnwrapped, Int32(hash.count), SQLITE_TRANSIENT) } }
311311
try execute { sqlite3_bind_int64(stmt, 2, sqlite3_int64(bitPattern: UInt64(truncatingIfNeeded: merkleBlock.version))) }
312-
try execute { merkleBlock.prevBlock.withUnsafeBytes { sqlite3_bind_blob(stmt, 3, $0, Int32(merkleBlock.prevBlock.count), SQLITE_TRANSIENT) } }
313-
try execute { merkleBlock.merkleRoot.withUnsafeBytes { sqlite3_bind_blob(stmt, 4, $0, Int32(merkleBlock.merkleRoot.count), SQLITE_TRANSIENT) } }
312+
try execute { merkleBlock.prevBlock.withUnsafeBytes { sqlite3_bind_blob(stmt, 3, $0.baseAddress.unsafelyUnwrapped, Int32(merkleBlock.prevBlock.count), SQLITE_TRANSIENT) } }
313+
try execute { merkleBlock.merkleRoot.withUnsafeBytes { sqlite3_bind_blob(stmt, 4, $0.baseAddress.unsafelyUnwrapped, Int32(merkleBlock.merkleRoot.count), SQLITE_TRANSIENT) } }
314314
try execute { sqlite3_bind_int64(stmt, 5, sqlite3_int64(bitPattern: UInt64(truncatingIfNeeded: merkleBlock.timestamp))) }
315315
try execute { sqlite3_bind_int64(stmt, 6, sqlite3_int64(bitPattern: UInt64(truncatingIfNeeded: merkleBlock.bits))) }
316316
try execute { sqlite3_bind_int64(stmt, 7, sqlite3_int64(bitPattern: UInt64(truncatingIfNeeded: merkleBlock.nonce))) }
317317
try execute { sqlite3_bind_int64(stmt, 8, sqlite3_int64(bitPattern: UInt64(truncatingIfNeeded: merkleBlock.totalTransactions))) }
318318
try execute { sqlite3_bind_int64(stmt, 9, sqlite3_int64(bitPattern: merkleBlock.numberOfHashes.underlyingValue)) }
319319
let hashes = Data(merkleBlock.hashes.flatMap { $0 })
320-
try execute { hashes.withUnsafeBytes { sqlite3_bind_blob(stmt, 10, $0, Int32(hashes.count), SQLITE_TRANSIENT) } }
320+
try execute { hashes.withUnsafeBytes { sqlite3_bind_blob(stmt, 10, $0.baseAddress.unsafelyUnwrapped, Int32(hashes.count), SQLITE_TRANSIENT) } }
321321
try execute { sqlite3_bind_int64(stmt, 11, sqlite3_int64(bitPattern: merkleBlock.numberOfFlags.underlyingValue)) }
322322
let flags = Data(merkleBlock.flags)
323-
try execute { flags.withUnsafeBytes { sqlite3_bind_blob(stmt, 12, $0, Int32(flags.count), SQLITE_TRANSIENT) } }
323+
try execute { flags.withUnsafeBytes { sqlite3_bind_blob(stmt, 12, $0.baseAddress.unsafelyUnwrapped, Int32(flags.count), SQLITE_TRANSIENT) } }
324324

325325
try executeUpdate { sqlite3_step(stmt) }
326326
try execute { sqlite3_reset(stmt) }
@@ -329,7 +329,7 @@ public class SQLiteBlockStore: BlockStore {
329329
public func addTransaction(_ transaction: Transaction, hash: Data) throws {
330330
let stmt = statements["addTransaction"]
331331

332-
try execute { hash.withUnsafeBytes { sqlite3_bind_blob(stmt, 1, $0, Int32(hash.count), SQLITE_TRANSIENT) } }
332+
try execute { hash.withUnsafeBytes { sqlite3_bind_blob(stmt, 1, $0.baseAddress.unsafelyUnwrapped, Int32(hash.count), SQLITE_TRANSIENT) } }
333333
try execute { sqlite3_bind_int64(stmt, 2, sqlite3_int64(bitPattern: UInt64(truncatingIfNeeded: transaction.version))) }
334334
try execute { sqlite3_bind_int(stmt, 3, 0) } // Not supported 'flag' currently
335335
try execute { sqlite3_bind_int64(stmt, 4, sqlite3_int64(bitPattern: transaction.txInCount.underlyingValue)) }
@@ -353,10 +353,10 @@ public class SQLiteBlockStore: BlockStore {
353353
let stmt = statements["addTransactionInput"]
354354

355355
try execute { sqlite3_bind_int64(stmt, 1, sqlite3_int64(bitPattern: input.scriptLength.underlyingValue)) }
356-
try execute { input.signatureScript.withUnsafeBytes { sqlite3_bind_blob(stmt, 2, $0, Int32(input.signatureScript.count), SQLITE_TRANSIENT) } }
356+
try execute { input.signatureScript.withUnsafeBytes { sqlite3_bind_blob(stmt, 2, $0.baseAddress.unsafelyUnwrapped, Int32(input.signatureScript.count), SQLITE_TRANSIENT) } }
357357
try execute { sqlite3_bind_int64(stmt, 3, sqlite3_int64(bitPattern: UInt64(truncatingIfNeeded: input.sequence))) }
358-
try execute { txId.withUnsafeBytes { sqlite3_bind_blob(stmt, 4, $0, Int32(txId.count), SQLITE_TRANSIENT) } }
359-
try execute { input.previousOutput.hash.withUnsafeBytes { sqlite3_bind_blob(stmt, 5, $0, Int32(input.previousOutput.hash.count), SQLITE_TRANSIENT) } }
358+
try execute { txId.withUnsafeBytes { sqlite3_bind_blob(stmt, 4, $0.baseAddress.unsafelyUnwrapped, Int32(txId.count), SQLITE_TRANSIENT) } }
359+
try execute { input.previousOutput.hash.withUnsafeBytes { sqlite3_bind_blob(stmt, 5, $0.baseAddress.unsafelyUnwrapped, Int32(input.previousOutput.hash.count), SQLITE_TRANSIENT) } }
360360

361361
try executeUpdate { sqlite3_step(stmt) }
362362
try execute { sqlite3_reset(stmt) }
@@ -368,8 +368,8 @@ public class SQLiteBlockStore: BlockStore {
368368
try execute { sqlite3_bind_int64(stmt, 1, sqlite3_int64(bitPattern: UInt64(truncatingIfNeeded: index))) }
369369
try execute { sqlite3_bind_int64(stmt, 2, sqlite3_int64(bitPattern: UInt64(truncatingIfNeeded: output.value))) }
370370
try execute { sqlite3_bind_int64(stmt, 3, sqlite3_int64(bitPattern: output.scriptLength.underlyingValue)) }
371-
try execute { output.lockingScript.withUnsafeBytes { sqlite3_bind_blob(stmt, 4, $0, Int32(output.lockingScript.count), SQLITE_TRANSIENT) } }
372-
try execute { txId.withUnsafeBytes { sqlite3_bind_blob(stmt, 5, $0, Int32(txId.count), SQLITE_TRANSIENT) } }
371+
try execute { output.lockingScript.withUnsafeBytes { sqlite3_bind_blob(stmt, 4, $0.baseAddress.unsafelyUnwrapped, Int32(output.lockingScript.count), SQLITE_TRANSIENT) } }
372+
try execute { txId.withUnsafeBytes { sqlite3_bind_blob(stmt, 5, $0.baseAddress.unsafelyUnwrapped, Int32(txId.count), SQLITE_TRANSIENT) } }
373373
if Script.isPublicKeyHashOut(output.lockingScript) {
374374
let pubKeyHash = Script.getPublicKeyHash(from: output.lockingScript)
375375
let address = publicKeyHashToAddress(Data([network.pubkeyhash]) + pubKeyHash)
@@ -382,14 +382,14 @@ public class SQLiteBlockStore: BlockStore {
382382

383383
private func deleteTransactionInput(txId: Data) throws {
384384
let stmt = statements["deleteTransactionInput"]
385-
try execute { txId.withUnsafeBytes { sqlite3_bind_blob(stmt, 1, $0, Int32(txId.count), SQLITE_TRANSIENT) } }
385+
try execute { txId.withUnsafeBytes { sqlite3_bind_blob(stmt, 1, $0.baseAddress.unsafelyUnwrapped, Int32(txId.count), SQLITE_TRANSIENT) } }
386386
try executeUpdate { sqlite3_step(stmt) }
387387
try execute { sqlite3_reset(stmt) }
388388
}
389389

390390
private func deleteTransactionOutput(txId: Data) throws {
391391
let stmt = statements["deleteTransactionOutput"]
392-
try execute { txId.withUnsafeBytes { sqlite3_bind_blob(stmt, 1, $0, Int32(txId.count), SQLITE_TRANSIENT) } }
392+
try execute { txId.withUnsafeBytes { sqlite3_bind_blob(stmt, 1, $0.baseAddress.unsafelyUnwrapped, Int32(txId.count), SQLITE_TRANSIENT) } }
393393
try executeUpdate { sqlite3_step(stmt) }
394394
try execute { sqlite3_reset(stmt) }
395395
}

Sources/BitcoinKit/Core/Helpers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func pton(_ address: String) -> Data {
3939
inet_pton(AF_INET6, address, UnsafeMutablePointer($0))
4040
}
4141
var buffer = Data(count: 16)
42-
_ = buffer.withUnsafeMutableBytes { memcpy($0, &addr, 16) }
42+
_ = buffer.withUnsafeMutableBytes { memcpy($0.baseAddress.unsafelyUnwrapped, &addr, 16) }
4343
return buffer
4444
}
4545

Sources/BitcoinKit/Core/Keys/Encoding.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ extension Encoding {
134134
let size = sizeFromBase(size: string.lengthOfBytes(using: .utf8) - zerosCount)
135135
var decodedBytes: [UInt8] = Array(repeating: 0, count: size)
136136
for c in string {
137-
guard let baseIndex: Int = baseAlphabets.index(of: c)?.utf16Offset(in: baseAlphabets) else { return nil }
137+
guard let baseIndex: Int = baseAlphabets.firstIndex(of: c)?.utf16Offset(in: baseAlphabets) else { return nil }
138138
var carry = baseIndex
139139
var i = 0
140140
for j in (0...decodedBytes.count - 1).reversed() where carry != 0 || i < length {
@@ -195,15 +195,14 @@ public struct Bech32 {
195195
var decodedIn5bit: [UInt8] = [UInt8]()
196196
for c in base32.lowercased() {
197197
// We can't have characters other than base32 alphabets.
198-
print(base32Alphabets)
199-
guard let baseIndex = base32Alphabets.index(of: c)?.utf16Offset(in: base32Alphabets) else {
198+
guard let baseIndex = base32Alphabets.firstIndex(of: c)?.utf16Offset(in: base32Alphabets) else {
200199
return nil
201200
}
202201
decodedIn5bit.append(UInt8(baseIndex))
203202
}
204203

205204
// We can't have invalid checksum
206-
let payload = Data(bytes: decodedIn5bit)
205+
let payload = Data(decodedIn5bit)
207206
guard verifyChecksum(prefix: prefix, payload: payload) else {
208207
return nil
209208
}
@@ -272,7 +271,7 @@ public struct Bech32 {
272271
if pad && bits > 0 {
273272
converted.append(lastBits)
274273
}
275-
return Data(bytes: converted)
274+
return Data(converted)
276275
}
277276

278277
internal static func convertFrom5bit(data: Data) throws -> Data {
@@ -298,7 +297,7 @@ public struct Bech32 {
298297
throw DecodeError.invalidBits
299298
}
300299

301-
return Data(bytes: converted)
300+
return Data(converted)
302301
}
303302

304303
private enum DecodeError: Error {

Sources/BitcoinKit/Core/Keys/PrivateKey.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public struct PrivateKey {
7272
var key = Data(count: count)
7373
var status: Int32 = 0
7474
repeat {
75-
status = key.withUnsafeMutableBytes { SecRandomCopyBytes(kSecRandomDefault, count, $0) }
75+
status = key.withUnsafeMutableBytes { SecRandomCopyBytes(kSecRandomDefault, count, $0.baseAddress.unsafelyUnwrapped) }
7676
} while (status != 0 || !check([UInt8](key)))
7777

7878
self.data = key

Sources/BitcoinKit/Core/Mnemonic.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public struct Mnemonic {
5353
public static func generate(strength: Strength = .default, language: Language = .english) throws -> [String] {
5454
let byteCount = strength.rawValue / 8
5555
var bytes = Data(count: byteCount)
56-
let status = bytes.withUnsafeMutableBytes { SecRandomCopyBytes(kSecRandomDefault, byteCount, $0) }
56+
let status = bytes.withUnsafeMutableBytes { SecRandomCopyBytes(kSecRandomDefault, byteCount, $0.baseAddress.unsafelyUnwrapped) }
5757
guard status == errSecSuccess else { throw MnemonicError.randomBytesError }
5858
return generate(entropy: bytes, language: language)
5959
}

Sources/BitcoinKit/Core/Serialization.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,10 @@ extension Data {
9191
}
9292

9393
func to<T>(type: T.Type) -> T {
94-
var data = self
95-
while data.count < MemoryLayout<T>.size {
96-
data.append(0)
97-
}
98-
return data.withUnsafeBytes { $0.pointee }
94+
var data = Data(count: MemoryLayout<T>.size)
95+
// Doing this for aligning memory layout
96+
_ = data.withUnsafeMutableBytes { self.copyBytes(to: $0) }
97+
return data.withUnsafeBytes { $0.load(as: T.self) }
9998
}
10099

101100
func to(type: String.Type) -> String {

0 commit comments

Comments
 (0)