diff --git a/EthereumKit.xcodeproj/project.pbxproj b/EthereumKit.xcodeproj/project.pbxproj index 629a71e..2b3b223 100644 --- a/EthereumKit.xcodeproj/project.pbxproj +++ b/EthereumKit.xcodeproj/project.pbxproj @@ -39,6 +39,7 @@ AE1BF3372028B5B000344FB0 /* Crypto.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE1BF3362028B5B000344FB0 /* Crypto.swift */; }; AE1F8C21208CDA0C0088A043 /* MessageSigningTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE1F8C20208CDA0C0088A043 /* MessageSigningTests.swift */; }; AE22CA2320A7711A0033FECD /* RawTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE22CA2220A7711A0033FECD /* RawTransaction.swift */; }; + AE22CA2520A831620033FECD /* SentTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE22CA2420A831620033FECD /* SentTransaction.swift */; }; AE295DE720615AC500F774A6 /* SMP.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE295DE620615AC500F774A6 /* SMP.swift */; }; AE295DEB20654D8B00F774A6 /* JSONRPCRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE295DEA20654D8A00F774A6 /* JSONRPCRequest.swift */; }; AE295DED206554E500F774A6 /* BatchElement.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE295DEC206554E500F774A6 /* BatchElement.swift */; }; @@ -152,6 +153,7 @@ AE1BF3362028B5B000344FB0 /* Crypto.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Crypto.swift; sourceTree = ""; }; AE1F8C20208CDA0C0088A043 /* MessageSigningTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageSigningTests.swift; sourceTree = ""; }; AE22CA2220A7711A0033FECD /* RawTransaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RawTransaction.swift; sourceTree = ""; }; + AE22CA2420A831620033FECD /* SentTransaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentTransaction.swift; sourceTree = ""; }; AE295DE620615AC500F774A6 /* SMP.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SMP.swift; sourceTree = ""; }; AE295DEA20654D8A00F774A6 /* JSONRPCRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONRPCRequest.swift; sourceTree = ""; }; AE295DEC206554E500F774A6 /* BatchElement.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BatchElement.swift; sourceTree = ""; }; @@ -279,6 +281,7 @@ AE8387EC2038041B00FAC88F /* BlockParameter.swift */, AE22CA2220A7711A0033FECD /* RawTransaction.swift */, AE838800203B236300FAC88F /* Transaction.swift */, + AE22CA2420A831620033FECD /* SentTransaction.swift */, ); path = Model; sourceTree = ""; @@ -682,6 +685,7 @@ 1A371091204E8FC0003A40BB /* String+Extension.swift in Sources */, AE8387DC20374A5600FAC88F /* Geth.swift in Sources */, AEA346CD209CD76F00E53536 /* JSONBodySerialization.swift in Sources */, + AE22CA2520A831620033FECD /* SentTransaction.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/EthereumKit/Key/PrivateKey.swift b/EthereumKit/Key/PrivateKey.swift index c4c8c70..1e1f37f 100644 --- a/EthereumKit/Key/PrivateKey.swift +++ b/EthereumKit/Key/PrivateKey.swift @@ -1,16 +1,25 @@ import EthereumKit.Private +/// Represents a private key public struct PrivateKey { + + // Private key in data format public let raw: Data public init(raw: Data) { self.raw = raw } + /// Publish key derived from private key public var publicKey: PublicKey { return PublicKey(privateKey: self) } + /// Sign signs provided hash data with private key by Elliptic Curve, Secp256k1 + /// + /// - Parameter hash: hash in data format + /// - Returns: signiture in data format + /// - Throws: .cryptoError(.failedToSign) when failed to sign public func sign(hash: Data) throws -> Data { return try Crypto.sign(hash, privateKey: raw) } diff --git a/EthereumKit/Key/PublicKey.swift b/EthereumKit/Key/PublicKey.swift index 7e490dc..e36552f 100644 --- a/EthereumKit/Key/PublicKey.swift +++ b/EthereumKit/Key/PublicKey.swift @@ -1,5 +1,7 @@ +/// Represents a public key public struct PublicKey { + /// Public key in data format public let raw: Data public init(raw: Data) { @@ -10,14 +12,22 @@ public struct PublicKey { self.init(raw: Data(hex: "0x") + PublicKey.from(data: privateKey.raw, compressed: false)) } + /// Address data generated from public key in data format private var addressData: Data { return Crypto.hashSHA3_256(raw.dropFirst()).suffix(20) } + /// generates address from its public key + /// + /// - Returns: address in string format public func generateAddress() -> String { return Address(data: addressData).string } + /// Generates public key from specified private key, + /// + /// - Parameters: data of private key and compressed + /// - Returns: Public key in data format public static func from(data: Data, compressed: Bool) -> Data { return Crypto.generatePublicKey(data: data, compressed: compressed) } diff --git a/EthereumKit/Model/Address.swift b/EthereumKit/Model/Address.swift index 9bd9819..d85514d 100644 --- a/EthereumKit/Model/Address.swift +++ b/EthereumKit/Model/Address.swift @@ -1,5 +1,10 @@ +/// Represents an address public struct Address { + + /// Address in data format public let data: Data + + /// Address in string format, EIP55 encoded public let string: String public init(data: Data) { diff --git a/EthereumKit/Model/Balance.swift b/EthereumKit/Model/Balance.swift index e41503c..cd1db30 100644 --- a/EthereumKit/Model/Balance.swift +++ b/EthereumKit/Model/Balance.swift @@ -1,6 +1,10 @@ +/// Represents a balance public struct Balance { + + /// User's balance in wei unit public let wei: Wei + /// User's balance in ether unit public var ether: Ether { return Converter.toEther(wei: wei) } diff --git a/EthereumKit/Model/SentTransaction.swift b/EthereumKit/Model/SentTransaction.swift new file mode 100644 index 0000000..f54e995 --- /dev/null +++ b/EthereumKit/Model/SentTransaction.swift @@ -0,0 +1,6 @@ +/// Represents send transaction +public struct SentTransaction { + + /// Transaction ID published when broadcasting raw tx + public let id: String +} diff --git a/EthereumKit/Model/Transaction.swift b/EthereumKit/Model/Transaction.swift index d688352..71b4c5d 100644 --- a/EthereumKit/Model/Transaction.swift +++ b/EthereumKit/Model/Transaction.swift @@ -1,25 +1,58 @@ -public struct SentTransaction { - public let id: String -} - +/// Represents transaction public struct Transaction { + + /// Blockhash of the block which includes this tx public let blockHash: String + + /// Block number of the block which includes this tx public let blockNumber: String + + /// Tx id public let hash: String + + /// Input value of this tx public let input: String + + /// Confirmations which this tx has public let confirmations: String + + /// Nonce of this tx public let nonce: String + + /// Timestamp of this tx public let timeStamp: String + + /// Contract address if exists public let contractAddress: String + + /// Sender of this tx public let from: String + + /// Receiver of this tx public let to: String + + /// Gas limit public let gas: String + + /// Gas price in wei public let gasPrice: String + + /// Gas actually used in tx public let gasUsed: String + + /// Remained gas public let cumulativeGasUsed: String + + /// Whether any error occured during broadcasting public let isError: String + + /// Index of this transaction public let transactionIndex: String + + /// public let txReceiptStatus: String + + /// Value of this tx, unit of wei public let value: String public var balance: Balance { @@ -94,7 +127,10 @@ extension Transaction: Codable { } } +/// Represents an array of transactions public struct Transactions { + + // Transactions public let elements: [Transaction] } diff --git a/EthereumKit/Wallet/Wallet.swift b/EthereumKit/Wallet/Wallet.swift index b7c0659..d5c75b0 100644 --- a/EthereumKit/Wallet/Wallet.swift +++ b/EthereumKit/Wallet/Wallet.swift @@ -1,6 +1,13 @@ +/// Wallet handles all the logic necessary for storing keys public final class Wallet { + /// Network which this wallet is connecting to + /// Basiclly Mainnet or Ropsten private let network: Network + + /// Private key which this wallet mainly use. + /// This is either provided by user or generated from HD wallet. + /// for HD wallet, path is m/44'/coin_type'/0'/0 private let privateKey: PrivateKey public init(seed: Data, network: Network) throws { @@ -25,10 +32,17 @@ public final class Wallet { // MARK: - Public Methods + /// Generates address from main private key. + /// + /// - Returns: Address in string format public func generateAddress() -> String { return privateKey.publicKey.generateAddress() } + /// Reveal private key of this wallet in string format + /// Be careful when calling this method. + /// + /// - Returns: Private key in string format public func dumpPrivateKey() -> String { return privateKey.raw.toHexString() } diff --git a/Example/ViewController.swift b/Example/ViewController.swift index 3dcf16b..28d6548 100644 --- a/Example/ViewController.swift +++ b/Example/ViewController.swift @@ -77,5 +77,7 @@ class ViewController: UIViewController { print("Error: \(error.localizedDescription)") } } + + geth.getTransactions(address: address) { print($0) } } }