Skip to content

Commit

Permalink
Fix: Wallet name not being saved on the import trustwallet#880
Browse files Browse the repository at this point in the history
  • Loading branch information
vikmeup committed Aug 7, 2018
1 parent 3b1b0bd commit da817af
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
12 changes: 7 additions & 5 deletions Trust/EtherClient/EtherKeystore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ class EtherKeystore: Keystore {
) { result in
switch result {
case .success(let account):
completion(.success(account))
let type = WalletType.privateKey(account)
completion(.success(WalletInfo(type: type, info: self.storage.get(for: type))))
case .failure(let error):
completion(.failure(error))
}
Expand Down Expand Up @@ -147,11 +148,12 @@ class EtherKeystore: Keystore {
return completion(.failure(.duplicateAccount))
}
storage.store(address: [watchAddress])
completion(.success(WalletInfo(type: .address(coin, address))))
let type = WalletType.address(coin, address)
completion(.success(WalletInfo(type: type, info: storage.get(for: type))))
}
}

func importKeystore(value: String, password: String, newPassword: String, coin: Coin, completion: @escaping (Result<WalletInfo, KeystoreError>) -> Void) {
func importKeystore(value: String, password: String, newPassword: String, coin: Coin, completion: @escaping (Result<Wallet, KeystoreError>) -> Void) {
DispatchQueue.global(qos: .userInitiated).async {
let result = self.importKeystore(value: value, password: password, newPassword: newPassword, coin: coin)
DispatchQueue.main.async {
Expand Down Expand Up @@ -186,15 +188,15 @@ class EtherKeystore: Keystore {
}
}

func importKeystore(value: String, password: String, newPassword: String, coin: Coin) -> Result<WalletInfo, KeystoreError> {
func importKeystore(value: String, password: String, newPassword: String, coin: Coin) -> Result<Wallet, KeystoreError> {
guard let data = value.data(using: .utf8) else {
return (.failure(.failedToParseJSON))
}
do {
//TODO: Blockchain. Pass blockchain ID
let wallet = try keyStore.import(json: data, password: password, newPassword: newPassword, coin: coin)
let _ = setPassword(newPassword, for: wallet)
return .success(WalletInfo(type: .hd(wallet)))
return .success(wallet)
} catch {
if case KeyStore.Error.accountAlreadyExists = error {
return .failure(.duplicateAccount)
Expand Down
2 changes: 0 additions & 2 deletions Trust/EtherClient/Keystore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ protocol Keystore {
@available(iOS 10.0, *)
func createAccount(with password: String, completion: @escaping (Result<Wallet, KeystoreError>) -> Void)
func importWallet(type: ImportType, coin: Coin, completion: @escaping (Result<WalletInfo, KeystoreError>) -> Void)
func importKeystore(value: String, password: String, newPassword: String, coin: Coin, completion: @escaping (Result<WalletInfo, KeystoreError>) -> Void)
func createAccout(password: String) -> Wallet
func importKeystore(value: String, password: String, newPassword: String, coin: Coin) -> Result<WalletInfo, KeystoreError>
func importPrivateKey(privateKey: PrivateKey, password: String, coin: Coin) -> Result<WalletInfo, KeystoreError>
func export(account: Account, password: String, newPassword: String) -> Result<String, KeystoreError>
func export(account: Account, password: String, newPassword: String, completion: @escaping (Result<String, KeystoreError>) -> Void)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ final class ImportWalletViewController: FormViewController {
}

func didImport(account: WalletInfo, name: String) {
delegate?.didImportAccount(account: account, fields: [.name(name)], in: self)
delegate?.didImportAccount(account: account, fields: [
.name(name),
.backup(true),
], in: self)
}

func importWallet() {
Expand Down
24 changes: 12 additions & 12 deletions TrustTests/EtherClient/EtherKeystoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class EtherKeystoreTests: XCTestCase {
return XCTFail()
}

XCTAssertEqual("0x5E9c27156a612a2D516C74c7a80af107856F8539", account.address.description)
XCTAssertEqual("0x5E9c27156a612a2D516C74c7a80af107856F8539", account.accounts[0].address.description)
XCTAssertEqual(1, keystore.wallets.count)
}

Expand All @@ -97,8 +97,8 @@ class EtherKeystoreTests: XCTestCase {
return XCTFail()
}

XCTAssertEqual("0x5E9c27156a612a2D516C74c7a80af107856F8539", account1.address.description)
XCTAssertEqual("0x5E9c27156a612a2D516C74c7a80af107856F8539", account2.address.description)
XCTAssertEqual("0x5E9c27156a612a2D516C74c7a80af107856F8539", account1.accounts[0].address.description)
XCTAssertEqual("0x5E9c27156a612a2D516C74c7a80af107856F8539", account2.accounts[0].address.description)
XCTAssertEqual(2, keystore.wallets.count)
}

Expand All @@ -111,7 +111,7 @@ class EtherKeystoreTests: XCTestCase {
switch result {
case .success(let wallet):
XCTAssertEqual(1, keystore.wallets.count)
XCTAssertEqual("0x33F44330cc4253cCd4ce4224186DB9baCe2190ea", wallet.address.description)
XCTAssertEqual("0x33F44330cc4253cCd4ce4224186DB9baCe2190ea", wallet.accounts[0].address.description)
case .failure:
XCTFail()
}
Expand All @@ -127,7 +127,7 @@ class EtherKeystoreTests: XCTestCase {
switch result {
case .success(let wallet):
XCTAssertEqual(1, keystore.wallets.count)
XCTAssertEqual("0x5f9763AF89b1De8d44F3739d55C00dD6a21C2Cb6", wallet.address.description)
XCTAssertEqual("0x5f9763AF89b1De8d44F3739d55C00dD6a21C2Cb6", wallet.accounts[0].address.description)
case .failure:
XCTFail()
}
Expand Down Expand Up @@ -167,13 +167,13 @@ class EtherKeystoreTests: XCTestCase {
return XCTFail()
}

let retreivePassword = keystore.getPassword(for: account.currentWallet!)
let retreivePassword = keystore.getPassword(for: account)

XCTAssertEqual(newPassword, retreivePassword)
XCTAssertEqual("0x5E9c27156a612a2D516C74c7a80af107856F8539", account.address.description)
XCTAssertEqual("0x5E9c27156a612a2D516C74c7a80af107856F8539", account.accounts[0].address.description)
XCTAssertEqual(1, keystore.wallets.count)

let exportResult = keystore.export(account: account.currentAccount, password: newPassword, newPassword: "test2")
let exportResult = keystore.export(account: account.accounts[0], password: newPassword, newPassword: "test2")

guard case .success = exportResult else {
return XCTAssertFalse(true)
Expand Down Expand Up @@ -243,7 +243,7 @@ class EtherKeystoreTests: XCTestCase {
return XCTFail()
}

let signResult = keystore.signPersonalMessage("Some data".data(using: .utf8)!, for: account.currentAccount)
let signResult = keystore.signPersonalMessage("Some data".data(using: .utf8)!, for: account.accounts[0])

guard case let .success(data) = signResult else {
return XCTFail()
Expand All @@ -268,7 +268,7 @@ class EtherKeystoreTests: XCTestCase {
return XCTFail()
}

let signResult = keystore.signPersonalMessage("".data(using: .utf8)!, for: account.currentAccount)
let signResult = keystore.signPersonalMessage("".data(using: .utf8)!, for: account.accounts[0])

guard case let .success(data) = signResult else {
return XCTFail()
Expand All @@ -293,7 +293,7 @@ class EtherKeystoreTests: XCTestCase {
return XCTFail()
}

let signResult = keystore.signPersonalMessage("0x3f44c2dfea365f01c1ada3b7600db9e2999dfea9fe6c6017441eafcfbc06a543".data(using: .utf8)!, for: account.currentAccount)
let signResult = keystore.signPersonalMessage("0x3f44c2dfea365f01c1ada3b7600db9e2999dfea9fe6c6017441eafcfbc06a543".data(using: .utf8)!, for: account.accounts[0])

guard case let .success(data) = signResult else {
return XCTFail()
Expand Down Expand Up @@ -321,7 +321,7 @@ class EtherKeystoreTests: XCTestCase {
let typedData = EthTypedData(type: "string", name: "Message", value: .string(value: "Hi, Alice!"))
let typedData2 = EthTypedData(type: "uint32", name: "A number", value: .uint(value: 1337))

let signResult = keystore.signTypedMessage([typedData, typedData2], for: account.currentAccount)
let signResult = keystore.signTypedMessage([typedData, typedData2], for: account.accounts[0])
guard case let .success(data) = signResult else {
return XCTFail()
}
Expand Down

0 comments on commit da817af

Please sign in to comment.