Skip to content

Commit

Permalink
Enable TypedMessageEncodingTests
Browse files Browse the repository at this point in the history
  • Loading branch information
vikmeup committed Jul 26, 2018
1 parent 802caa7 commit 32fac30
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 31 deletions.
3 changes: 0 additions & 3 deletions Trust.xcodeproj/xcshareddata/xcschemes/Trust.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@
<Test
Identifier = "AppCoordinatorTests">
</Test>
<Test
Identifier = "TypedMessageEncodingTests">
</Test>
</SkippedTests>
</TestableReference>
<TestableReference
Expand Down
2 changes: 1 addition & 1 deletion Trust/EtherClient/CoinMarket/Types/CoinTicker.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright SIX DAY LLC. All rights reserved.
// Copyright DApps Platform Inc. All rights reserved.

import Foundation
import RealmSwift
Expand Down
8 changes: 6 additions & 2 deletions Trust/EtherClient/EtherKeystore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,12 @@ class EtherKeystore: Keystore {
guard let password = getPassword(for: wallet) else {
return .failure(.failedToDeleteAccount)
}
try? keyStore.addAccounts(wallet: wallet, derivationPaths: derivationPaths, password: password)
return .success(())
do {
let _ = try keyStore.addAccounts(wallet: wallet, derivationPaths: derivationPaths, password: password)
return .success(())
} catch {
return .failure(KeystoreError.failedToAddAccounts)
}
}

func update(wallet: Wallet) -> Result<Void, KeystoreError> {
Expand Down
3 changes: 3 additions & 0 deletions Trust/EtherClient/KeyStoreError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum KeystoreError: LocalizedError {
case failedToSignTypedMessage
case failedToExportPrivateKey
case invalidMnemonicPhrase
case failedToAddAccounts

var errorDescription: String? {
switch self {
Expand Down Expand Up @@ -48,6 +49,8 @@ enum KeystoreError: LocalizedError {
return "Failed to export private key"
case .invalidMnemonicPhrase:
return "Invalid mnemonic phrase"
case .failedToAddAccounts:
return "Faield to add accounts"
}
}
}
2 changes: 1 addition & 1 deletion Trust/Extensions/Double.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright SIX DAY LLC. All rights reserved.
// Copyright DApps Platform Inc. All rights reserved.

import Foundation

Expand Down
2 changes: 1 addition & 1 deletion Trust/Settings/Storage/RPCStore.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright SIX DAY LLC. All rights reserved.
// Copyright DApps Platform Inc. All rights reserved.

import Foundation
import RealmSwift
Expand Down
4 changes: 2 additions & 2 deletions Trust/Settings/ViewControllers/DeveloperViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ final class DeveloperViewController: FormViewController {
// self?.preferencesController.set(value: enabled, for: .testNetworks)
// }

<<< AppFormAppearance.button { [weak self] in
<<< AppFormAppearance.button {
$0.title = "Clear Transactions"
}.onCellSelection { [weak self] _, _ in
guard let `self` = self else { return }
Expand All @@ -100,7 +100,7 @@ final class DeveloperViewController: FormViewController {
cell.textLabel?.textColor = .black
}

<<< AppFormAppearance.button { [weak self] in
<<< AppFormAppearance.button {
$0.title = "Clear Tokens"
}.onCellSelection { [weak self] _, _ in
guard let `self` = self else { return }
Expand Down
2 changes: 1 addition & 1 deletion Trust/Wallet/Views/WalletViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class WalletViewCell: UITableViewCell {
@IBOutlet weak var addressLabel: UILabel!
@IBOutlet weak var identiconImageView: TokenImageView!
@IBOutlet weak var selectedImageView: UIImageView!

weak var delegate: WalletViewCellDelegate?

var viewModel: WalletAccountViewModel? {
Expand Down
46 changes: 26 additions & 20 deletions TrustTests/EtherClient/TypedMessageEncodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,10 @@ import KeychainSwift

class TypedMessageEncodingTests: XCTestCase {

var account: Account!
let keystore = FakeEtherKeystore()

override func setUp() {
super.setUp()
let keystore = FakeEtherKeystore()

let privateKey = PrivateKey(data: Data(hexString: "0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318")!)!
let result = keystore.importPrivateKey(privateKey: privateKey, password: TestKeyStore.password, coin: .ethereum)

guard case let .success(account) = result else {
return XCTFail()
}
self.account = account.currentAccount!
}

func testValue_none() {
let privateKey = PrivateKey(data: Data(hexString: "0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318")!)!
let result = keystore.importPrivateKey(privateKey: privateKey, password: TestKeyStore.password, coin: .ethereum)
guard case let .success(account1) = result else {
return XCTFail()
}

let account = importAccount()
let typedData = EthTypedData(type: "string", name: "test test", value: .none)
let signResult = keystore.signTypedMessage([typedData], for: account)
guard case let .success(data) = signResult else {
Expand All @@ -40,6 +21,7 @@ class TypedMessageEncodingTests: XCTestCase {
}

func testValues() {
let account = importAccount()
let typedData = EthTypedData(type: "string", name: "Message", value: .string(value: "Hi, Alice!"))
let typedData2 = EthTypedData(type: "uint8", name: "value", value: .uint(value: 10))
let signResult = keystore.signTypedMessage([typedData, typedData2], for: account)
Expand All @@ -50,6 +32,7 @@ class TypedMessageEncodingTests: XCTestCase {
}

func testType_uint_Value_uint() {
let account = importAccount()
// from https://beta.adex.network/
let typedData = EthTypedData(type: "uint", name: "Auth token", value: .uint(value: 1498316044249108))

Expand All @@ -61,6 +44,7 @@ class TypedMessageEncodingTests: XCTestCase {
}

func testType_uint_Value_string() {
let account = importAccount()
// from https://beta.adex.network/
let typedData = EthTypedData(type: "uint", name: "Auth token", value: .string(value: "1498316044249108"))

Expand All @@ -72,6 +56,7 @@ class TypedMessageEncodingTests: XCTestCase {
}

func testType_bool_Value_bool() {
let account = importAccount()
let typedData = EthTypedData(type: "bool", name: "email valid", value: .bool(value: false))
let signResult = keystore.signTypedMessage([typedData], for: account)
guard case let .success(data) = signResult else {
Expand All @@ -81,6 +66,7 @@ class TypedMessageEncodingTests: XCTestCase {
}

func testType_address_Value_string() {
let account = importAccount()
let typedData = EthTypedData(type: "address", name: "my address", value: .address(value: "0x2c7536e3605d9c16a7a3d7b1898e529396a65c23"))
let signResult = keystore.signTypedMessage([typedData], for: account)
guard case let .success(data) = signResult else {
Expand All @@ -90,6 +76,7 @@ class TypedMessageEncodingTests: XCTestCase {
}

func testType_string_Value_string() {
let account = importAccount()
let typedData = EthTypedData(type: "string", name: "This is a message", value: .string(value: "hello bob"))
let signResult = keystore.signTypedMessage([typedData], for: account)
guard case let .success(data) = signResult else {
Expand All @@ -99,6 +86,7 @@ class TypedMessageEncodingTests: XCTestCase {
}

func testType_string_Value_emptyString() {
let account = importAccount()
let typedData = EthTypedData(type: "string", name: "empty string here!", value: .string(value: ""))
let signResult = keystore.signTypedMessage([typedData], for: account)
guard case let .success(data) = signResult else {
Expand All @@ -108,6 +96,7 @@ class TypedMessageEncodingTests: XCTestCase {
}

func testType_int_Value_int() {
let account = importAccount()
let typedData = EthTypedData(type: "int32", name: "how much?", value: .int(value: 1200))
let signResult = keystore.signTypedMessage([typedData], for: account)
guard case let .success(data) = signResult else {
Expand All @@ -117,6 +106,7 @@ class TypedMessageEncodingTests: XCTestCase {
}

func testType_int_Value_bigInt() {
let account = importAccount()
// from https://like.co/
let typedData = EthTypedData(type: "int256", name: "how much?", value: .string(value: "-57896044618658097711785492504343953926634992332820282019728792003956564819968"))
let signResult = keystore.signTypedMessage([typedData], for: account)
Expand All @@ -127,6 +117,7 @@ class TypedMessageEncodingTests: XCTestCase {
}

func testType_int_Value_bigUInt() {
let account = importAccount()
// from https://like.co/
let typedData = EthTypedData(type: "uint256", name: "how much?", value: .string(value: "6739986666787659948666753771754907668409286105635143120275902562304"))
let signResult = keystore.signTypedMessage([typedData], for: account)
Expand All @@ -137,6 +128,7 @@ class TypedMessageEncodingTests: XCTestCase {
}

func testType_bytes_Value_hexString() {
let account = importAccount()
let typedData = EthTypedData(type: "bytes", name: "your address", value: .string(value: "0x2c7536e3605d9c16a7a3d7b1898e529396a65c23"))
let signResult = keystore.signTypedMessage([typedData], for: account)
guard case let .success(data) = signResult else {
Expand All @@ -147,6 +139,7 @@ class TypedMessageEncodingTests: XCTestCase {
}

func testType_bytes_Value_hexShortString() {
let account = importAccount()
let typedData = EthTypedData(type: "bytes", name: "short hex", value: .string(value: "0xdeadbeaf"))
let signResult = keystore.signTypedMessage([typedData], for: account)
guard case let .success(data) = signResult else {
Expand All @@ -157,6 +150,7 @@ class TypedMessageEncodingTests: XCTestCase {
}

func testType_bytes_Value_hexEmptyString() {
let account = importAccount()
let typedData = EthTypedData(type: "bytes", name: "empty hex", value: .string(value: "0x"))
let signResult = keystore.signTypedMessage([typedData], for: account)
guard case let .success(data) = signResult else {
Expand All @@ -167,6 +161,7 @@ class TypedMessageEncodingTests: XCTestCase {
}

func testType_bytes_Value_string() {
let account = importAccount()
let typedData = EthTypedData(type: "bytes", name: "+bytes in string", value: .string(value: "this is a raw string"))
let signResult = keystore.signTypedMessage([typedData], for: account)
guard case let .success(data) = signResult else {
Expand All @@ -177,6 +172,7 @@ class TypedMessageEncodingTests: XCTestCase {
}

func testType_bytes_Value_emptyString() {
let account = importAccount()
let typedData = EthTypedData(type: "bytes", name: "nothing here", value: .string(value: ""))
let signResult = keystore.signTypedMessage([typedData], for: account)
guard case let .success(data) = signResult else {
Expand All @@ -187,6 +183,7 @@ class TypedMessageEncodingTests: XCTestCase {
}

func testType_bytes_Value_int() {
let account = importAccount()
let typedData = EthTypedData(type: "bytes", name: "a number?", value: .uint(value: 1100))
let signResult = keystore.signTypedMessage([typedData], for: account)
guard case let .success(data) = signResult else {
Expand All @@ -195,4 +192,13 @@ class TypedMessageEncodingTests: XCTestCase {

XCTAssertEqual(data.hexEncoded, "0xd90e257771b64fbc4ecd963f27ea371eb6a656c1912afda8a9184a4b81130dd71487f525d1ecca0d6008530c1bfcf5afc0c2224670ae68080d264ec96468c9bb1c")
}

private func importAccount() -> Account {
let privateKey = PrivateKey(data: Data(hexString: "0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318")!)!
let result = keystore.importPrivateKey(privateKey: privateKey, password: TestKeyStore.password, coin: .ethereum)
guard case let .success(account1) = result else {
return .make()
}
return account1.currentAccount
}
}

0 comments on commit 32fac30

Please sign in to comment.