-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae7ad48
commit 7026a80
Showing
90 changed files
with
25,911 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "Carthage/Checkouts/KeychainAccess"] | ||
path = Examples/Wallet/Carthage/Checkouts/KeychainAccess | ||
url = https://github.com/kishikawakatsumi/KeychainAccess.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Pod::Spec.new do |spec| | ||
spec.name = 'BitcoinKit' | ||
spec.version = '0.1.0' | ||
spec.summary = 'Bitcoin protocol toolkit for Swift' | ||
spec.description = <<-DESC | ||
BitcoinKit implements Bitcoin protocol in Swift. It is an implementation of the Bitcoin SPV protocol written (almost) entirely in swift. | ||
``` | ||
DESC | ||
spec.homepage = 'https://github.com/kishikawakatsumi/BitcoinKit' | ||
spec.license = { :type => 'Apache 2.0', :file => 'LICENSE' } | ||
spec.author = { 'Kishikawa Katsumi' => '[email protected]' } | ||
spec.social_media_url = 'https://twitter.com/k_katsumi' | ||
|
||
spec.requires_arc = true | ||
spec.source = { git: 'https://github.com/kishikawakatsumi/BitcoinKit.git', tag: "v#{spec.version}" } | ||
spec.source_files = 'BitcoinKit/*.{h,swift}' | ||
|
||
spec.pod_target_xcconfig = { 'SWIFT_WHOLE_MODULE_OPTIMIZATION' => 'YES', | ||
'APPLICATION_EXTENSION_API_ONLY' => 'YES' } | ||
|
||
spec.ios.deployment_target = '8.0' | ||
|
||
spec.preserve_paths = './setup' | ||
spec.prepare_command = 'sh ./setup/build_libraries.sh' | ||
end |
Large diffs are not rendered by default.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
BitcoinKit.xcodeproj/project.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
101 changes: 101 additions & 0 deletions
101
BitcoinKit.xcodeproj/xcshareddata/xcschemes/BitcoinKit.xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Scheme | ||
LastUpgradeVersion = "0920" | ||
version = "1.3"> | ||
<BuildAction | ||
parallelizeBuildables = "YES" | ||
buildImplicitDependencies = "YES"> | ||
<BuildActionEntries> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "YES" | ||
buildForArchiving = "YES" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "147494C6201F9A29006D1CF8" | ||
BuildableName = "BitcoinKit.framework" | ||
BlueprintName = "BitcoinKit" | ||
ReferencedContainer = "container:BitcoinKit.xcodeproj"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
</BuildActionEntries> | ||
</BuildAction> | ||
<TestAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
language = "" | ||
shouldUseLaunchSchemeArgsEnv = "YES"> | ||
<Testables> | ||
<TestableReference | ||
skipped = "NO"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "147494CF201F9A29006D1CF8" | ||
BuildableName = "BitcoinKitTests.xctest" | ||
BlueprintName = "BitcoinKitTests" | ||
ReferencedContainer = "container:BitcoinKit.xcodeproj"> | ||
</BuildableReference> | ||
</TestableReference> | ||
</Testables> | ||
<MacroExpansion> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "147494C6201F9A29006D1CF8" | ||
BuildableName = "BitcoinKit.framework" | ||
BlueprintName = "BitcoinKit" | ||
ReferencedContainer = "container:BitcoinKit.xcodeproj"> | ||
</BuildableReference> | ||
</MacroExpansion> | ||
<AdditionalOptions> | ||
</AdditionalOptions> | ||
</TestAction> | ||
<LaunchAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
language = "" | ||
launchStyle = "0" | ||
useCustomWorkingDirectory = "NO" | ||
ignoresPersistentStateOnLaunch = "NO" | ||
debugDocumentVersioning = "YES" | ||
debugServiceExtension = "internal" | ||
allowLocationSimulation = "YES"> | ||
<MacroExpansion> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "147494C6201F9A29006D1CF8" | ||
BuildableName = "BitcoinKit.framework" | ||
BlueprintName = "BitcoinKit" | ||
ReferencedContainer = "container:BitcoinKit.xcodeproj"> | ||
</BuildableReference> | ||
</MacroExpansion> | ||
<AdditionalOptions> | ||
</AdditionalOptions> | ||
</LaunchAction> | ||
<ProfileAction | ||
buildConfiguration = "Release" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
savedToolIdentifier = "" | ||
useCustomWorkingDirectory = "NO" | ||
debugDocumentVersioning = "YES"> | ||
<MacroExpansion> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "147494C6201F9A29006D1CF8" | ||
BuildableName = "BitcoinKit.framework" | ||
BlueprintName = "BitcoinKit" | ||
ReferencedContainer = "container:BitcoinKit.xcodeproj"> | ||
</BuildableReference> | ||
</MacroExpansion> | ||
</ProfileAction> | ||
<AnalyzeAction | ||
buildConfiguration = "Debug"> | ||
</AnalyzeAction> | ||
<ArchiveAction | ||
buildConfiguration = "Release" | ||
revealArchiveInOrganizer = "YES"> | ||
</ArchiveAction> | ||
</Scheme> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// | ||
// Address.swift | ||
// BitcoinKit | ||
// | ||
// Created by Kishikawa Katsumi on 2018/01/31. | ||
// Copyright © 2018 Kishikawa Katsumi. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A Bitcoin address looks like 1MsScoe2fTJoq4ZPdQgqyhgWeoNamYPevy and is derived from an elliptic curve public key | ||
/// plus a set of network parameters. | ||
/// A standard address is built by taking the RIPE-MD160 hash of the public key bytes, with a version prefix and a | ||
/// checksum suffix, then encoding it textually as base58. The version prefix is used to both denote the network for | ||
/// which the address is valid. | ||
public struct Address { | ||
public typealias Base58Check = String | ||
|
||
let network: Network | ||
|
||
let publicKeyHash: Data | ||
let base58: Base58Check | ||
|
||
public init(_ publicKey: PublicKey) { | ||
network = publicKey.network | ||
publicKeyHash = Crypto.sha256ripemd160(publicKey.raw) | ||
base58 = publicKey.toAddress() | ||
} | ||
|
||
public init(_ base58: Base58Check) throws { | ||
self.base58 = base58 | ||
|
||
let raw = Base58.decode(base58) | ||
let checksum = raw.suffix(4) | ||
let pubKeyHash = raw.dropLast(4) | ||
let checksumConfirm = Crypto.sha256sha256(pubKeyHash).prefix(4) | ||
guard checksum == checksumConfirm else { | ||
throw AddressError.invalid | ||
} | ||
|
||
let network: Network | ||
let addressPrefix = pubKeyHash[0] | ||
switch addressPrefix { | ||
case Network.mainnet.pubkeyhash: | ||
network = .mainnet | ||
case Network.testnet.pubkeyhash: | ||
network = .testnet | ||
default: | ||
throw AddressError.wrongNetwork | ||
} | ||
self.network = network | ||
|
||
self.publicKeyHash = pubKeyHash.dropFirst() | ||
} | ||
} | ||
|
||
extension Address : Equatable { | ||
public static func ==(lhs: Address, rhs: Address) -> Bool { | ||
return lhs.network == rhs.network && lhs.publicKeyHash == rhs.publicKeyHash | ||
} | ||
} | ||
|
||
extension Address : CustomStringConvertible { | ||
public var description: String { | ||
return base58 | ||
} | ||
} | ||
|
||
public enum AddressError: Error { | ||
case invalid | ||
case wrongNetwork | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// BitcoinKit.h | ||
// BitcoinKit | ||
// | ||
// Created by Kishikawa Katsumi on 2018/01/30. | ||
// Copyright © 2018 Kishikawa Katsumi. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
FOUNDATION_EXPORT double BitcoinKitVersionNumber; | ||
FOUNDATION_EXPORT const unsigned char BitcoinKitVersionString[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// BlockChain.swift | ||
// BitcoinKit | ||
// | ||
// Created by Kishikawa Katsumi on 2018/02/03. | ||
// Copyright © 2018 Kishikawa Katsumi. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public class BlockChain { | ||
let wallet: WalletProtocol | ||
let blockStore: BlockStore | ||
|
||
public init(wallet: WalletProtocol, blockStore: BlockStore) { | ||
self.wallet = wallet | ||
self.blockStore = blockStore | ||
} | ||
|
||
public func addBlock(_ block: BlockMessage, hash: Data) throws { | ||
try blockStore.addBlock(block, hash: hash) | ||
} | ||
|
||
public func addMerkleBlock(_ merkleBlock: MerkleBlockMessage, hash: Data) throws { | ||
try blockStore.addMerkleBlock(merkleBlock, hash: hash) | ||
} | ||
|
||
public func addTransaction(_ transaction: Transaction, hash: Data) throws { | ||
try blockStore.addTransaction(transaction, hash: hash) | ||
} | ||
|
||
public func calculateBlance() throws -> Int64 { | ||
return try blockStore.calculateBlance(address: Address(wallet.address)) | ||
} | ||
|
||
public func latestBlockHash() -> Data { | ||
var latestBlockHash: Data? | ||
do { | ||
latestBlockHash = try blockStore.latestBlockHash() | ||
} catch {} | ||
return latestBlockHash ?? wallet.network.checkpoints.last!.hash | ||
} | ||
} |
Oops, something went wrong.