Skip to content

Commit

Permalink
Merge pull request #8 from Sajjon/xcode11_swift51
Browse files Browse the repository at this point in the history
Xcode11 swift51
  • Loading branch information
Sajjon authored Sep 17, 2019
2 parents 0129f1c + 9a20386 commit eab0f19
Show file tree
Hide file tree
Showing 64 changed files with 2,859 additions and 485 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ playground.xcworkspace
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
Package.resolved
.build/

# CocoaPods
Expand Down
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
language: objective-c
osx_image: xcode9.4
osx_image: xcode10.3
cache:
directories:
- Libraries
script:
- xcodebuild -workspace "$WORKSPACE" -scheme "$SCHEME" -configuration "$CONFIGURATION"
-sdk "$SDK" -destination "$DESTINATION" -derivedDataPath build
-enableCodeCoverage YES ENABLE_TESTABILITY=YES "$ACTION"
- swift -version
- swift test
after_success:
- ruby scripts/coverage.rb "$SCHEME"
Expand Down
4 changes: 2 additions & 2 deletions BitcoinKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'BitcoinKit'
spec.version = '1.0.2'
spec.version = '1.1.0-beta5'
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.
Expand All @@ -17,7 +17,7 @@ Pod::Spec.new do |spec|
spec.exclude_files = 'Sources/**/LinuxSupport.swift'
spec.module_map = 'BitcoinKit/BitcoinKit.modulemap'
spec.ios.deployment_target = '8.0'
spec.swift_version = '4.1'
spec.swift_version = '5.0'

spec.pod_target_xcconfig = { 'SWIFT_WHOLE_MODULE_OPTIMIZATION' => 'YES',
'APPLICATION_EXTENSION_API_ONLY' => 'YES',
Expand Down
152 changes: 134 additions & 18 deletions BitcoinKit.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
codeCoverageEnabled = "YES"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down
16 changes: 1 addition & 15 deletions BitcoinKit/BitcoinKitPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ NS_ASSUME_NONNULL_BEGIN

+ (NSData *)sha1:(NSData *)data;
+ (NSData *)sha256:(NSData *)data;
+ (NSData *)sha256ripemd160:(NSData *)data;
+ (NSData *)ripemd160:(NSData *)data;
+ (NSData *)hmacsha512:(NSData *)data key:(NSData *)key;

@end

@interface _Key : NSObject
+ (NSData *)computePublicKeyFromPrivateKey:(NSData *)privateKey compression:(BOOL)compression;
+ (NSData *)deriveKey:(NSData *)password salt:(NSData *)salt iterations:(NSInteger)iterations keyLength:(NSInteger)keyLength;

@end
Expand All @@ -47,20 +47,6 @@ NS_ASSUME_NONNULL_BEGIN
+ (NSData *)decodePointOnCurveForCompressedPublicKey:(NSData *)publicKeyCompressed;
@end

@interface _HDKey : NSObject

@property (nonatomic, readonly, nullable) NSData *privateKey;
@property (nonatomic, readonly, nullable) NSData *publicKey;
@property (nonatomic, readonly) NSData *chainCode;
@property (nonatomic, readonly) uint8_t depth;
@property (nonatomic, readonly) uint32_t fingerprint;
@property (nonatomic, readonly) uint32_t childIndex;

- (instancetype)initWithPrivateKey:(nullable NSData *)privateKey publicKey:(nullable NSData *)publicKey chainCode:(NSData *)chainCode depth:(uint8_t)depth fingerprint:(uint32_t)fingerprint childIndex:(uint32_t)childIndex;
- (nullable _HDKey *)derivedAtIndex:(uint32_t)childIndex hardened:(BOOL)hardened;

@end

@interface _Crypto : NSObject
+ (NSData *)signMessage:(NSData *)message withPrivateKey:(NSData *)privateKey;
+ (BOOL)verifySignature:(NSData *)signature message:(NSData *)message publicKey:(NSData *)publicKey;
Expand Down
130 changes: 0 additions & 130 deletions BitcoinKit/BitcoinKitPrivate.m
Original file line number Diff line number Diff line change
Expand Up @@ -129,41 +129,6 @@ + (NSData *)decodePointOnCurveForCompressedPublicKey:(NSData *)publicKeyCompress

@implementation _Key

+ (NSData *)computePublicKeyFromPrivateKey:(NSData *)privateKey compression:(BOOL)compression {
BN_CTX *ctx = BN_CTX_new();
EC_KEY *key = EC_KEY_new_by_curve_name(NID_secp256k1);
const EC_GROUP *group = EC_KEY_get0_group(key);

BIGNUM *prv = BN_new();
BN_bin2bn(privateKey.bytes, (int)privateKey.length, prv);

EC_POINT *pub = EC_POINT_new(group);
EC_POINT_mul(group, pub, prv, nil, nil, ctx);
EC_KEY_set_private_key(key, prv);
EC_KEY_set_public_key(key, pub);

NSMutableData *result;
if (compression) {
EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
unsigned char *bytes = NULL;
int length = i2o_ECPublicKey(key, &bytes);
result = [NSMutableData dataWithBytesNoCopy:bytes length:length];
} else {
result = [NSMutableData dataWithLength:65];
BIGNUM *n = BN_new();
EC_POINT_point2bn(group, pub, POINT_CONVERSION_UNCOMPRESSED, n, ctx);
BN_bn2bin(n, result.mutableBytes);
BN_free(n);
}

EC_POINT_free(pub);
BN_free(prv);
EC_KEY_free(key);
BN_CTX_free(ctx);

return result;
}

+ (NSData *)deriveKey:(NSData *)password salt:(NSData *)salt iterations:(NSInteger)iterations keyLength:(NSInteger)keyLength {
NSMutableData *result = [NSMutableData dataWithLength:keyLength];
PKCS5_PBKDF2_HMAC(password.bytes, (int)password.length, salt.bytes, (int)salt.length, (int)iterations, EVP_sha512(), (int)keyLength, result.mutableBytes);
Expand All @@ -172,101 +137,6 @@ + (NSData *)deriveKey:(NSData *)password salt:(NSData *)salt iterations:(NSInteg

@end

@implementation _HDKey

- (instancetype)initWithPrivateKey:(NSData *)privateKey publicKey:(NSData *)publicKey chainCode:(NSData *)chainCode depth:(uint8_t)depth fingerprint:(uint32_t)fingerprint childIndex:(uint32_t)childIndex {
self = [super init];
if (self) {
_privateKey = privateKey;
_publicKey = publicKey;
_chainCode = chainCode;
_depth = depth;
_fingerprint = fingerprint;
_childIndex = childIndex;
}
return self;
}

- (_HDKey *)derivedAtIndex:(uint32_t)index hardened:(BOOL)hardened {
BN_CTX *ctx = BN_CTX_new();

NSMutableData *data = [NSMutableData data];
if (hardened) {
uint8_t padding = 0;
[data appendBytes:&padding length:1];
[data appendData:self.privateKey];
} else {
[data appendData:self.publicKey];
}

uint32_t childIndex = OSSwapHostToBigInt32(hardened ? (0x80000000 | index) : index);
[data appendBytes:&childIndex length:sizeof(childIndex)];

NSData *digest = [_Hash hmacsha512:data key:self.chainCode];
NSData *derivedPrivateKey = [digest subdataWithRange:NSMakeRange(0, 32)];
NSData *derivedChainCode = [digest subdataWithRange:NSMakeRange(32, 32)];

BIGNUM *curveOrder = BN_new();
BN_hex2bn(&curveOrder, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141");

BIGNUM *factor = BN_new();
BN_bin2bn(derivedPrivateKey.bytes, (int)derivedPrivateKey.length, factor);
// Factor is too big, this derivation is invalid.
if (BN_cmp(factor, curveOrder) >= 0) {
return nil;
}

NSMutableData *result;
if (self.privateKey) {
BIGNUM *privateKey = BN_new();
BN_bin2bn(self.privateKey.bytes, (int)self.privateKey.length, privateKey);

BN_mod_add(privateKey, privateKey, factor, curveOrder, ctx);
// Check for invalid derivation.
if (BN_is_zero(privateKey)) {
return nil;
}

int numBytes = BN_num_bytes(privateKey);
result = [NSMutableData dataWithLength:numBytes];
BN_bn2bin(privateKey, result.mutableBytes);

BN_free(privateKey);
} else {
BIGNUM *publicKey = BN_new();
BN_bin2bn(self.publicKey.bytes, (int)self.publicKey.length, publicKey);
EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_secp256k1);

EC_POINT *point = EC_POINT_new(group);
EC_POINT_bn2point(group, publicKey, point, ctx);
EC_POINT_mul(group, point, factor, point, BN_value_one(), ctx);
// Check for invalid derivation.
if (EC_POINT_is_at_infinity(group, point) == 1) {
return nil;
}

BIGNUM *n = BN_new();
result = [NSMutableData dataWithLength:33];

EC_POINT_point2bn(group, point, POINT_CONVERSION_COMPRESSED, n, ctx);
BN_bn2bin(n, result.mutableBytes);

BN_free(n);
BN_free(publicKey);
EC_POINT_free(point);
EC_GROUP_free(group);
}

BN_free(factor);
BN_free(curveOrder);
BN_CTX_free(ctx);

uint32_t *fingerPrint = (uint32_t *)[_Hash sha256ripemd160:self.publicKey].bytes;
return [[_HDKey alloc] initWithPrivateKey:result publicKey:result chainCode:derivedChainCode depth:self.depth + 1 fingerprint:*fingerPrint childIndex:childIndex];
}

@end

@implementation _Crypto

+ (NSData *)signMessage:(NSData *)message withPrivateKey:(NSData *)privateKey {
Expand Down
61 changes: 61 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"object": {
"pins": [
{
"package": "Bits",
"repositoryURL": "https://github.com/vapor/bits.git",
"state": {
"branch": null,
"revision": "03079476b04139a8b744ad96f975176df5ae12bc",
"version": "1.1.1"
}
},
{
"package": "COpenSSL",
"repositoryURL": "https://github.com/vapor-community/copenssl.git",
"state": {
"branch": null,
"revision": "a347e9ec038b134aecf42e0c21f2deb9035adf3f",
"version": "1.0.0-rc.1"
}
},
{
"package": "Core",
"repositoryURL": "https://github.com/vapor/core.git",
"state": {
"branch": null,
"revision": "aa15871311b497a24a7f0e96b1ae6865dd600818",
"version": "2.2.1"
}
},
{
"package": "Debugging",
"repositoryURL": "https://github.com/vapor/debugging.git",
"state": {
"branch": null,
"revision": "fc5a27d6eb236141dc24e5f14eedaa2e035ae7b3",
"version": "1.1.1"
}
},
{
"package": "Random",
"repositoryURL": "https://github.com/vapor-community/random.git",
"state": {
"branch": null,
"revision": "d7c4397d125caba795d14d956efacfe2a27a63d0",
"version": "1.2.0"
}
},
{
"package": "secp256k1",
"repositoryURL": "https://github.com/Boilertalk/secp256k1.swift",
"state": {
"branch": null,
"revision": "823281fe9def21b384099b72a9a53ca988317b20",
"version": "0.1.4"
}
}
]
},
"version": 1
}
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:4.0
// swift-tools-version:5.0
import PackageDescription

let package = Package(
Expand All @@ -25,5 +25,5 @@ let package = Package(
dependencies: ["BitcoinKit"]
)
],
swiftLanguageVersions: [4]
swiftLanguageVersions: [.v5]
)
40 changes: 26 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
![BitcoinKit: Let’s Play with Bitcoin in Swift!](https://user-images.githubusercontent.com/23519083/44261174-cc64aa00-a251-11e8-85b6-145e0bcae102.jpg)
<p align="center">
<img src="https://user-images.githubusercontent.com/23519083/44261174-cc64aa00-a251-11e8-85b6-145e0bcae102.jpg" alt="BitcoinKit: Let’s Play with Bitcoin in Swift!">
<a href="https://travis-ci.org/yenom/BitcoinKit">
<img src="http://img.shields.io/travis/yenom/BitcoinKit.svg" alt="TravisCI">
</a>
<a href="https://codecov.io/gh/yenom/BitcoinKit">
<img src="https://codecov.io/gh/yenom/BitcoinKit/branch/master/graph/badge.svg" />
</a>
<a href="https://swift.org">
<img src="http://img.shields.io/badge/swift-5.0+-brightgreen.svg" alt="Swift 5.0+">
</a>
<a href="https://github.com/Carthage/Carthage">
<img src="https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat" alt="Carthage">
</a>
<a href="https://swift.org/package-manager">
<img src="https://img.shields.io/badge/spm-compatible-brightgreen.svg?style=flat" alt="Swift Package Manager" />
</a>
<a href="http://cocoadocs.org/docsets/BitcoinKit">
<img src="https://img.shields.io/cocoapods/v/BitcoinKit.svg" alt="CococaPods" />
</a>
</p>

[![CI Status](http://img.shields.io/travis/yenom/BitcoinKit.svg)](https://travis-ci.org/yenom/BitcoinKit)
[![codecov](https://codecov.io/gh/yenom/BitcoinKit/branch/master/graph/badge.svg)](https://codecov.io/gh/yenom/BitcoinKit)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![Version](https://img.shields.io/cocoapods/v/BitcoinKit.svg)](http://cocoadocs.org/docsets/BitcoinKit)
[![Platform](https://img.shields.io/cocoapods/p/BitcoinKit.svg)](http://cocoadocs.org/docsets/BitcoinKit)
[![Backers on Open Collective](https://opencollective.com/BitcoinKit/backers/badge.svg)](#backers)
[![Sponsors on Open Collective](https://opencollective.com/BitcoinKit/sponsors/badge.svg)](#sponsors)

### Welcome to BitcoinKit

The BitcoinKit library is a Swift implementation of the Bitcoin protocol, supporting both of BCH and BTC.
Our mission is improving the mobile ecosystem for Bitcoin developers.
The BitcoinKit library is a Swift implementation of the Bitcoin protocol which support both BCH and BTC. Improving the mobile ecosystem for Bitcoin developers is our mission.

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.
BitcoinKit allows maintaining a wallet, sending or receiving transactions without a full blockchain node. Following is a wallet app that demonstrates the way to use it.

Release notes are [here](CHANGELOG.md).

Expand Down Expand Up @@ -117,8 +129,8 @@ let peerGroup.start()
Requirements
------------
- iOS 9.0+ / Mac OS X 10.11+ / tvOS 9.0+ / watchOS 2.0+
- Xcode 9.0+
- Swift 4.1+
- Xcode 10.0+
- Swift 5.0+

Installation
------------
Expand Down Expand Up @@ -176,7 +188,7 @@ BitcoinKit is available through [Swift Package Manager](https://github.com/apple
it, simply add the following lines to dependencies of your Package.swift:

```swift
.package(url: "https://github.com/yenom/BitcoinKit.git", .upToNextMinor(from: "0.1.0"))
.package(url: "https://github.com/yenom/BitcoinKit.git", .upToNextMinor(from: "1.0.0"))
```

Note that following data types and features are currently not supported on Linux platform.
Expand Down
Loading

0 comments on commit eab0f19

Please sign in to comment.