Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

Commit

Permalink
Make account URL required to avoid delete bug (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandro-isaza committed Jan 31, 2018
1 parent b89f295 commit 55c0df5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
6 changes: 3 additions & 3 deletions Sources/Account.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public struct Account: Hashable {
/// Ethereum 20-byte account address derived from the key.
public var address: Address

/// Optional URL for the key file on disk.
public var url: URL?
/// URL for the key file on disk.
public var url: URL

/// Creates an `Account` with an Ethereum address and a `Key`.
public init(address: Address, url: URL? = nil) {
public init(address: Address, url: URL) {
self.address = address
self.url = url
}
Expand Down
21 changes: 10 additions & 11 deletions Sources/KeyStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public final class KeyStore {
let key = try Key(password: password)
keysByAddress[key.address] = key

let account = Account(address: key.address)
let url = makeAccountURL(for: key)
let account = Account(address: key.address, url: url)
try save(account: account, in: keydir)
accountsByAddress[key.address] = account
return account
Expand All @@ -87,7 +88,8 @@ public final class KeyStore {
let newKey = try Key(password: newPassword, key: privateKey)
keysByAddress[newKey.address] = newKey

let account = Account(address: newKey.address)
let url = makeAccountURL(for: key)
let account = Account(address: newKey.address, url: url)
try save(account: account, in: keydir)
accountsByAddress[newKey.address] = account

Expand Down Expand Up @@ -160,9 +162,7 @@ public final class KeyStore {
privateKey.resetBytes(in: 0..<privateKey.count)
}

if let url = account.url {
try FileManager.default.removeItem(at: url)
}
try FileManager.default.removeItem(at: account.url)
accountsByAddress[account.address] = nil
keysByAddress[account.address] = nil
}
Expand All @@ -184,17 +184,16 @@ public final class KeyStore {

// MARK: Helpers

private func makeAccountURL(for key: Key) -> URL {
return keydir.appendingPathComponent(key.generateFileName())
}

/// Saves the account to the given directory.
private func save(account: Account, in directory: URL) throws {
guard let key = keysByAddress[account.address] else {
fatalError("Missing account key")
}
if let url = account.url {
try save(key: key, to: url)
} else {
let url = directory.appendingPathComponent(key.generateFileName())
try save(key: key, to: url)
}
try save(key: key, to: account.url)
}

private func save(key: Key, to url: URL) throws {
Expand Down

0 comments on commit 55c0df5

Please sign in to comment.