Skip to content

Commit

Permalink
do not use computed property, use function instead
Browse files Browse the repository at this point in the history
  • Loading branch information
yuzushioh committed Mar 10, 2018
1 parent e4ce86c commit 42beab6
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 18 deletions.
10 changes: 5 additions & 5 deletions EthereumKit/Key/HDPrivateKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public struct HDPrivateKey {
self.network = network
}

public var privateKey: PrivateKey {
public func privateKey() -> PrivateKey {
return PrivateKey(raw: raw)
}

public var hdPublicKey: HDPublicKey {
public func hdPublicKey() -> HDPublicKey {
return HDPublicKey(hdPrivateKey: self, chainCode: chainCode, network: network, depth: depth, fingerprint: fingerprint, index: childIndex)
}

public var extended: String {
public func extended() -> String {
var extendedPrivateKeyData = Data()
extendedPrivateKeyData += network.privateKeyPrefix.bigEndian
extendedPrivateKeyData += depth.littleEndian
Expand All @@ -45,14 +45,14 @@ public struct HDPrivateKey {
return Base58.encode(extendedPrivateKeyData)
}

public func derived(at index: UInt32, hardens: Bool = false) throws -> HDPrivateKey {
internal func derived(at index: UInt32, hardens: Bool = false) throws -> HDPrivateKey {
guard (0x80000000 & index) == 0 else {
fatalError("Invalid index \(index)")
}

let keyDeriver = KeyDerivation(
privateKey: raw,
publicKey: hdPublicKey.raw,
publicKey: hdPublicKey().raw,
chainCode: chainCode,
depth: depth,
fingerprint: fingerprint,
Expand Down
9 changes: 4 additions & 5 deletions EthereumKit/Key/HDPublicKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ public struct HDPublicKey {

private let hdPrivateKey: HDPrivateKey

init(hdPrivateKey: HDPrivateKey, chainCode: Data, network: Network, depth: UInt8, fingerprint: UInt32, index: UInt32) {
let compressed = Crypto.generatePublicKey(data: hdPrivateKey.raw, compressed: true)
self.raw = Data(hex: "0x") + compressed
public init(hdPrivateKey: HDPrivateKey, chainCode: Data, network: Network, depth: UInt8, fingerprint: UInt32, index: UInt32) {
self.raw = Data(hex: "0x") + PublicKey.from(data: hdPrivateKey.raw, compressed: true)
self.chainCode = chainCode
self.depth = depth
self.fingerprint = fingerprint
Expand All @@ -19,7 +18,7 @@ public struct HDPublicKey {
self.hdPrivateKey = hdPrivateKey
}

public var publicKey: PublicKey {
return PublicKey(raw: Crypto.generatePublicKey(data: hdPrivateKey.raw, compressed: false))
public func publicKey() -> PublicKey {
return PublicKey(privateKey: hdPrivateKey.privateKey())
}
}
2 changes: 1 addition & 1 deletion EthereumKit/Key/PrivateKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public struct PrivateKey {
}

public var publicKey: PublicKey {
return PublicKey(raw: Crypto.generatePublicKey(data: raw, compressed: false))
return PublicKey(privateKey: self)
}

public func sign(hash: Data) throws -> Data {
Expand Down
8 changes: 8 additions & 0 deletions EthereumKit/Key/PublicKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ public struct PublicKey {
self.raw = raw
}

public init(privateKey: PrivateKey) {
self.init(raw: PublicKey.from(data: privateKey.raw, compressed: false))
}

private var addressData: Data {
let hash = raw.dropFirst().sha3(.keccak256)
return hash.suffix(20)
Expand All @@ -14,4 +18,8 @@ public struct PublicKey {
public func generateAddress() -> String {
return Address(data: addressData).string
}

public static func from(data: Data, compressed: Bool) -> Data {
return Crypto.generatePublicKey(data: data, compressed: compressed)
}
}
2 changes: 1 addition & 1 deletion EthereumKit/Wallet/HDWallet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public final class HDWallet {
// MARK: - Public Methods

public func generatePrivateKey(at index: UInt32) throws -> PrivateKey {
return try privateKey(change: .external).derived(at: index).privateKey
return try privateKey(change: .external).derived(at: index).privateKey()
}

public func generateAddress(at index: UInt32) throws -> String {
Expand Down
2 changes: 1 addition & 1 deletion EthereumKit/Wallet/Wallet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public final class Wallet {
.derived(at: 0, hardens: true)
.derived(at: 0) // 0 for external
.derived(at: 0)
.privateKey
.privateKey()
}

public init(network: Network, privateKey: String) {
Expand Down
2 changes: 1 addition & 1 deletion EthereumKitTests/EthereumKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class EthereumKitTests: XCTestCase {
// m/44'/60'/0'/0
let firstPrivateKey = try! change.derived(at: 0)
XCTAssertEqual(
firstPrivateKey.hdPublicKey.publicKey.generateAddress(),
firstPrivateKey.hdPublicKey().publicKey().generateAddress(),
"0x83f1caAdaBeEC2945b73087F803d404F054Cc2B7"
)

Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ let mnemonic = Mnemonic.create()
let seed = Mnemonic.createSeed(mnemonic: mnemonic)

// BIP32: Key derivation and address generation
let wallet = Wallet(seed: seed, network: .main)
let address = wallet.generateAddress(at: 0)
let wallet = try HDWallet(seed: seed, network: .main)
let address = try wallet.generateAddress(at: 0)

// Send some ether
let rawTransaction = RawTransaction(ether: "0.15", address: "0x88b44BC83add758A3642130619D61682282850Df", nonce: 2)
let tx = wallet.signTransaction(rawTransaction)
let tx = try wallet.signTransaction(rawTransaction)

let geth = Geth(network: .main)
geth.sendRawTransaction(rawTransaction: tx) { result in
// Do something...
}
Expand Down

0 comments on commit 42beab6

Please sign in to comment.