diff --git a/._bitcoin_issue_fix.md b/._bitcoin_issue_fix.md new file mode 100644 index 00000000..2afebcce Binary files /dev/null and b/._bitcoin_issue_fix.md differ diff --git a/BitcoinKit.podspec b/BitcoinKit.podspec index ac39d03f..262a38f6 100644 --- a/BitcoinKit.podspec +++ b/BitcoinKit.podspec @@ -3,15 +3,14 @@ Pod::Spec.new do |spec| spec.version = '1.1.0' spec.summary = 'Bitcoin(BCH/BTC) protocol toolkit for Swift' spec.description = <<-DESC - The BitcoinKit library is a Swift implementation of the Bitcoin(BCH/BTC) protocol. This library was originally made by Katsumi Kishikawa, and now is maintained by Yenom Inc. It allows maintaining a wallet and sending/receiving transactions without needing a full blockchain node. It comes with a simple wallet app showing how to use it. - ``` - DESC - spec.homepage = 'https://github.com/yenom/BitcoinKit' + The BitcoinKit library is a Swift implementation of the Bitcoin(BCH/BTC) protocol. This library was originally made by Katsumi Kishikawa, and now is maintained by Yenom Inc. It allows maintaining a wallet and sending/receiving transactions without needing a full blockchain node. It comes with a simple wallet app showing how to use it. + DESC + spec.homepage = 'https://github.com/Whitehare2023/BitcoinKit' spec.license = { :type => 'MIT', :file => 'LICENSE' } spec.author = { 'BitcoinKit developers' => 'usatie@yenom.tech' } spec.requires_arc = true - spec.source = { git: 'https://github.com/yenom/BitcoinKit.git', tag: "v#{spec.version}" } + spec.source = { git: 'https://github.com/Whitehare2023/BitcoinKit.git', branch: 'fix-secp256k1-build' } spec.source_files = 'BitcoinKit/**/*.{h,m,swift}', 'Sources/BitcoinKit/**/*.{h,m,swift}' spec.private_header_files = 'BitcoinKit/**/BitcoinKitPrivate.h' spec.exclude_files = 'Sources/**/LinuxSupport.swift' @@ -19,12 +18,14 @@ Pod::Spec.new do |spec| spec.ios.deployment_target = '8.0' spec.swift_version = '5.0' - spec.pod_target_xcconfig = { 'SWIFT_WHOLE_MODULE_OPTIMIZATION' => 'YES', - 'APPLICATION_EXTENSION_API_ONLY' => 'YES', - 'SWIFT_INCLUDE_PATHS' => '${PODS_ROOT}/BitcoinKit/Libraries', - 'HEADER_SEARCH_PATHS' => '"${PODS_ROOT}/BitcoinKit/Libraries/openssl/include" "${PODS_ROOT}/BitcoinKit/Libraries/secp256k1/include"', - 'LIBRARY_SEARCH_PATHS' => '"${PODS_ROOT}/BitcoinKit/Libraries/openssl/lib" "${PODS_ROOT}/BitcoinKit/Libraries/secp256k1/lib"', - 'OTHER_SWIFT_FLAGS' => '-D BitcoinKitXcode' } + spec.pod_target_xcconfig = { + 'SWIFT_WHOLE_MODULE_OPTIMIZATION' => 'YES', + 'APPLICATION_EXTENSION_API_ONLY' => 'YES', + 'SWIFT_INCLUDE_PATHS' => '${PODS_ROOT}/BitcoinKit/Libraries', + 'HEADER_SEARCH_PATHS' => '"${PODS_ROOT}/BitcoinKit/Libraries/openssl/include" "${PODS_ROOT}/BitcoinKit/Libraries/secp256k1/include"', + 'LIBRARY_SEARCH_PATHS' => '"${PODS_ROOT}/BitcoinKit/Libraries/openssl/lib" "${PODS_ROOT}/BitcoinKit/Libraries/secp256k1/lib"', + 'OTHER_SWIFT_FLAGS' => '-D BitcoinKitXcode' + } spec.preserve_paths = ['setup', 'Libraries'] spec.prepare_command = 'sh setup/build_libraries.sh' end diff --git a/Sources/BitcoinKit/Core/Mnemonic/BitArray.swift b/Sources/BitcoinKit/Core/Mnemonic/BitArray.swift index 45916242..861f0449 100644 --- a/Sources/BitcoinKit/Core/Mnemonic/BitArray.swift +++ b/Sources/BitcoinKit/Core/Mnemonic/BitArray.swift @@ -46,6 +46,43 @@ public struct BitArray: Hashable, RangeReplaceableCollection { /// Constructs an empty bit array. public init() {} + public mutating func replaceSubrange(_ subrange: Range, with newElements: C) where C : Collection, Bool == C.Element { + let removeCount = subrange.count + let insertCount = newElements.count + let shift = insertCount - removeCount + + if shift > 0 { + bits.reserveCapacity(bits.count + (shift / Constants.IntSize) + 1) + } + + // Shift existing elements to make space or close the gap + for i in stride(from: count - 1, through: subrange.lowerBound, by: -1) { + let targetIndex = i + shift + if targetIndex < count { + let value = valueAtIndex(i) + setValue(value, atIndex: targetIndex) + } + } + + // Insert new elements + var currentIndex = subrange.lowerBound + for element in newElements { + setValue(element, atIndex: currentIndex) + currentIndex += 1 + } + + // Update count + count += shift + + // Recalculate cardinality + cardinality = 0 + for i in 0..(_ elements: S) where S.Iterator.Element == Bool { for value in elements { diff --git a/bitcoin_issue_fix.md b/bitcoin_issue_fix.md new file mode 100644 index 00000000..5c616895 --- /dev/null +++ b/bitcoin_issue_fix.md @@ -0,0 +1,54 @@ + +# BitcoinKit Issue Fix + +## 问题 / Issue +在运行 `pod install` 时,构建 `secp256k1` 库时出现了以下错误: +While running `pod install`, the following error occurred during the `secp256k1` library build: + +``` +fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: /var/folders/jp/86j3w3fx08j54wjdp81ln0n80000gn/T/tmp.SKt6tdknEr/.build/iphoneos/lib/libsecp256k1.a and /var/folders/jp/86j3w3fx08j54wjdp81ln0n80000gn/T/tmp.SKt6tdknEr/.build/iphonesimulator/lib/libsecp256k1.a have the same architectures (x86_64) and can't be in the same fat output file +``` + +## 原因 / Cause +这是因为构建的库在 `iphoneos` 和 `iphonesimulator` 中包含相同的架构(如 x86_64),导致 `lipo` 命令无法合并这些库。 +This is because the built libraries for `iphoneos` and `iphonesimulator` contain the same architecture (e.g., x86_64), causing the `lipo` command to fail to merge these libraries. + +## 解决方法 / Solution +修改 `build_secp256k1.sh` 脚本,确保正确处理架构。具体更改如下: +Modify the `build_secp256k1.sh` script to correctly handle the architectures. The specific changes are as follows: + +```sh +#!/bin/sh +set -ex + +SCRIPT_DIR=`dirname "$0"` + +TDIR=`mktemp -d` +trap "{ cd - ; rm -rf $TDIR; exit 255; }" SIGINT + +cd $TDIR + +git clone https://github.com/bitcoin-core/secp256k1.git src + +CURRENTPATH=`pwd` + +TARGETDIR_IPHONEOS="$CURRENTPATH/.build/iphoneos" +mkdir -p "$TARGETDIR_IPHONEOS" + +TARGETDIR_SIMULATOR="$CURRENTPATH/.build/iphonesimulator" +mkdir -p "$TARGETDIR_SIMULATOR" + +(cd src && ./autogen.sh) +(cd src && ./configure --host=arm-apple-darwin CC=`xcrun -find clang` CFLAGS="-O3 -arch armv7 -arch armv7s -arch arm64 -isysroot `xcrun -sdk iphoneos --show-sdk-path` -fembed-bitcode -mios-version-min=8.0" CXX=`xcrun -find clang++` CXXFLAGS="-O3 -arch armv7 -arch armv7s -arch arm64 -isysroot `xcrun -sdk iphoneos --show-sdk-path` -fembed-bitcode -mios-version-min=8.0" --prefix="$TARGETDIR_IPHONEOS" && make install) +(cd src && ./configure --host=x86_64-apple-darwin CC=`xcrun -find clang` CFLAGS="-O3 -arch x86_64 -isysroot `xcrun -sdk iphonesimulator --show-sdk-path` -fembed-bitcode-marker -mios-simulator-version-min=8.0" CXX=`xcrun -find clang++` CXXFLAGS="-O3 -arch x86_64 -isysroot `xcrun -sdk iphonesimulator --show-sdk-path` -fembed-bitcode-marker -mios-simulator-version-min=8.0" --prefix="$TARGETDIR_SIMULATOR" && make install) + +cd - + +mkdir -p "$SCRIPT_DIR/../Libraries/secp256k1/lib" +xcrun lipo -create "$TARGETDIR_IPHONEOS/lib/libsecp256k1.a" "$TARGETDIR_SIMULATOR/lib/libsecp256k1.a" -o "$SCRIPT_DIR/../Libraries/secp256k1/lib/libsecp256k1.a" +cp -rf $TDIR/src/include "$SCRIPT_DIR/../Libraries/secp256k1" + +rm -rf $TDIR + +exit 0 +``` diff --git a/setup/build_secp256k1.sh b/setup/build_secp256k1.sh index 631dde0f..11955bdf 100644 --- a/setup/build_secp256k1.sh +++ b/setup/build_secp256k1.sh @@ -19,8 +19,9 @@ TARGETDIR_SIMULATOR="$CURRENTPATH/.build/iphonesimulator" mkdir -p "$TARGETDIR_SIMULATOR" (cd src && ./autogen.sh) -(cd src && ./configure --host=x86_64-apple-darwin CC=`xcrun -find clang` CFLAGS="-O3 -arch i386 -arch x86_64 -isysroot `xcrun -sdk iphonesimulator --show-sdk-path` -fembed-bitcode-marker -mios-simulator-version-min=8.0" CXX=`xcrun -find clang++` CXXFLAGS="-O3 -arch i386 -arch x86_64 -isysroot `xcrun -sdk iphonesimulator --show-sdk-path` -fembed-bitcode-marker -mios-simulator-version-min=8.0" --prefix="$TARGETDIR_IPHONEOS" && make install) -(cd src && ./configure --host=arm-apple-darwin CC=`xcrun -find clang` CFLAGS="-O3 -arch armv7 -arch armv7s -arch arm64 -isysroot `xcrun -sdk iphoneos --show-sdk-path` -fembed-bitcode -mios-version-min=8.0" CXX=`xcrun -find clang++` CXXFLAGS="-O3 -arch armv7 -arch armv7s -arch arm64 -isysroot `xcrun -sdk iphoneos --show-sdk-path` -fembed-bitcode -mios-version-min=8.0" --prefix="$TARGETDIR_SIMULATOR" && make install) +(cd src && ./configure --host=arm-apple-darwin CC=`xcrun -find clang` CFLAGS="-O3 -arch armv7 -arch armv7s -arch arm64 -isysroot `xcrun -sdk iphoneos --show-sdk-path` -fembed-bitcode -mios-version-min=8.0" CXX=`xcrun -find clang++` CXXFLAGS="-O3 -arch armv7 -arch armv7s -arch arm64 -isysroot `xcrun -sdk iphoneos --show-sdk-path` -fembed-bitcode -mios-version-min=8.0" --prefix="$TARGETDIR_IPHONEOS" && make install) +(cd src && make clean) +(cd src && ./configure --host=x86_64-apple-darwin CC=`xcrun -find clang` CFLAGS="-O3 -arch x86_64 -isysroot `xcrun -sdk iphonesimulator --show-sdk-path` -fembed-bitcode-marker -mios-simulator-version-min=8.0" CXX=`xcrun -find clang++` CXXFLAGS="-O3 -arch x86_64 -isysroot `xcrun -sdk iphonesimulator --show-sdk-path` -fembed-bitcode-marker -mios-simulator-version-min=8.0" --prefix="$TARGETDIR_SIMULATOR" && make install) cd -