Skip to content

Commit

Permalink
Merge pull request #98 from yuzushioh/documentation
Browse files Browse the repository at this point in the history
Add more method documentation
  • Loading branch information
yuzushioh authored May 13, 2018
2 parents b9302be + accb238 commit b93be3c
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 4 deletions.
4 changes: 4 additions & 0 deletions EthereumKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -152,6 +153,7 @@
AE1BF3362028B5B000344FB0 /* Crypto.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Crypto.swift; sourceTree = "<group>"; };
AE1F8C20208CDA0C0088A043 /* MessageSigningTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageSigningTests.swift; sourceTree = "<group>"; };
AE22CA2220A7711A0033FECD /* RawTransaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RawTransaction.swift; sourceTree = "<group>"; };
AE22CA2420A831620033FECD /* SentTransaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentTransaction.swift; sourceTree = "<group>"; };
AE295DE620615AC500F774A6 /* SMP.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SMP.swift; sourceTree = "<group>"; };
AE295DEA20654D8A00F774A6 /* JSONRPCRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONRPCRequest.swift; sourceTree = "<group>"; };
AE295DEC206554E500F774A6 /* BatchElement.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BatchElement.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -279,6 +281,7 @@
AE8387EC2038041B00FAC88F /* BlockParameter.swift */,
AE22CA2220A7711A0033FECD /* RawTransaction.swift */,
AE838800203B236300FAC88F /* Transaction.swift */,
AE22CA2420A831620033FECD /* SentTransaction.swift */,
);
path = Model;
sourceTree = "<group>";
Expand Down Expand Up @@ -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;
};
Expand Down
9 changes: 9 additions & 0 deletions EthereumKit/Key/PrivateKey.swift
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down
10 changes: 10 additions & 0 deletions EthereumKit/Key/PublicKey.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/// Represents a public key
public struct PublicKey {

/// Public key in data format
public let raw: Data

public init(raw: Data) {
Expand All @@ -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)
}
Expand Down
5 changes: 5 additions & 0 deletions EthereumKit/Model/Address.swift
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions EthereumKit/Model/Balance.swift
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions EthereumKit/Model/SentTransaction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// Represents send transaction
public struct SentTransaction {

/// Transaction ID published when broadcasting raw tx
public let id: String
}
44 changes: 40 additions & 4 deletions EthereumKit/Model/Transaction.swift
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -94,7 +127,10 @@ extension Transaction: Codable {
}
}

/// Represents an array of transactions
public struct Transactions {

// Transactions
public let elements: [Transaction]
}

Expand Down
14 changes: 14 additions & 0 deletions EthereumKit/Wallet/Wallet.swift
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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()
}
Expand Down
2 changes: 2 additions & 0 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,7 @@ class ViewController: UIViewController {
print("Error: \(error.localizedDescription)")
}
}

geth.getTransactions(address: address) { print($0) }
}
}

0 comments on commit b93be3c

Please sign in to comment.