|
|
|
@ -22,8 +22,8 @@ class EtherKeystoreTests: XCTestCase { |
|
|
|
|
XCTAssertEqual(1, keystore.wallets.count) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func testEmptyPassword() { |
|
|
|
|
let keystore = try! LegacyFileBasedKeystore(keystore: FakeKeystore()) |
|
|
|
|
func testEmptyPassword() throws { |
|
|
|
|
let keystore = try LegacyFileBasedKeystore(keystore: FakeKeystore()) |
|
|
|
|
let password = keystore.getPassword(for: .make()) |
|
|
|
|
XCTAssertNil(password) |
|
|
|
|
} |
|
|
|
@ -33,7 +33,10 @@ class EtherKeystoreTests: XCTestCase { |
|
|
|
|
let expectation = self.expectation(description: "completion block called") |
|
|
|
|
keystore.importWallet(type: .keystore(string: TestKeyStore.keystore, password: TestKeyStore.password)) { result in |
|
|
|
|
expectation.fulfill() |
|
|
|
|
let wallet = try! result.dematerialize() |
|
|
|
|
guard let wallet = try? result.dematerialize() else { |
|
|
|
|
XCTFail("Failure to import wallet") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
XCTAssertEqual("0x5E9c27156a612a2D516C74c7a80af107856F8539", wallet.address.eip55String) |
|
|
|
|
XCTAssertEqual(1, keystore.wallets.count) |
|
|
|
|
} |
|
|
|
@ -48,7 +51,10 @@ class EtherKeystoreTests: XCTestCase { |
|
|
|
|
let expectations = [expectation1, expectation2] |
|
|
|
|
keystore.importWallet(type: .keystore(string: TestKeyStore.keystore, password: TestKeyStore.password)) { result in |
|
|
|
|
expectation1.fulfill() |
|
|
|
|
let wallet = try! result.dematerialize() |
|
|
|
|
guard let wallet = try? result.dematerialize() else { |
|
|
|
|
XCTFail("Failure to import wallet") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
address = wallet.address |
|
|
|
|
} |
|
|
|
|
keystore.importWallet(type: .keystore(string: TestKeyStore.keystore, password: TestKeyStore.password)) { result in |
|
|
|
@ -76,42 +82,47 @@ class EtherKeystoreTests: XCTestCase { |
|
|
|
|
XCTAssertEqual(0, keystore.wallets.count) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func testExportHdWalletToSeedPhrase() { |
|
|
|
|
func testExportHdWalletToSeedPhrase() throws { |
|
|
|
|
let keystore = FakeEtherKeystore() |
|
|
|
|
let result = keystore.createAccount() |
|
|
|
|
let account = try! result.dematerialize() |
|
|
|
|
let account = try result.dematerialize() |
|
|
|
|
let expectation = self.expectation(description: "completion block called") |
|
|
|
|
keystore.exportSeedPhraseOfHdWallet(forAccount: account, context: .init(), reason: .backup) { result in |
|
|
|
|
expectation.fulfill() |
|
|
|
|
let seedPhrase = try! result.dematerialize() |
|
|
|
|
guard let seedPhrase = try? result.dematerialize() else { |
|
|
|
|
XCTFail("Failure to import wallet") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
XCTAssertEqual(seedPhrase.split(separator: " ").count, 12) |
|
|
|
|
} |
|
|
|
|
wait(for: [expectation], timeout: 0.01) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func testExportRawPrivateKeyToKeystoreFile() { |
|
|
|
|
func testExportRawPrivateKeyToKeystoreFile() throws { |
|
|
|
|
let keystore = FakeEtherKeystore() |
|
|
|
|
let password = "test" |
|
|
|
|
|
|
|
|
|
XCTAssertEqual(keystore.wallets.count, 0) |
|
|
|
|
let result = keystore.importWallet(type: .privateKey(privateKey: Data(hexString: TestKeyStore.testPrivateKey)!)) |
|
|
|
|
let wallet = try! result.dematerialize() |
|
|
|
|
let wallet = try result.dematerialize() |
|
|
|
|
XCTAssertEqual(keystore.wallets.count, 1) |
|
|
|
|
|
|
|
|
|
let expectation = self.expectation(description: "completion block called") |
|
|
|
|
keystore.exportRawPrivateKeyForNonHdWalletForBackup(forAccount: wallet.address, newPassword: password) { result in |
|
|
|
|
let v = try? result.dematerialize() |
|
|
|
|
XCTAssertNotNil(v) |
|
|
|
|
|
|
|
|
|
expectation.fulfill() |
|
|
|
|
let _ = try! result.dematerialize() |
|
|
|
|
} |
|
|
|
|
wait(for: [expectation], timeout: 0.01) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func testRecentlyUsedAccount() { |
|
|
|
|
func testRecentlyUsedAccount() throws { |
|
|
|
|
let keystore = FakeEtherKeystore() |
|
|
|
|
|
|
|
|
|
XCTAssertNil(keystore.recentlyUsedWallet) |
|
|
|
|
|
|
|
|
|
let account = try! Wallet(type: .real(keystore.createAccount().dematerialize())) |
|
|
|
|
let account = try Wallet(type: .real(keystore.createAccount().dematerialize())) |
|
|
|
|
|
|
|
|
|
keystore.recentlyUsedWallet = account |
|
|
|
|
|
|
|
|
@ -123,9 +134,9 @@ class EtherKeystoreTests: XCTestCase { |
|
|
|
|
XCTAssertNil(keystore.recentlyUsedWallet) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func testDeleteAccount() { |
|
|
|
|
func testDeleteAccount() throws { |
|
|
|
|
let keystore = FakeEtherKeystore() |
|
|
|
|
let wallet = try! Wallet(type: .real(keystore.createAccount().dematerialize())) |
|
|
|
|
let wallet = try Wallet(type: .real(keystore.createAccount().dematerialize())) |
|
|
|
|
|
|
|
|
|
XCTAssertEqual(1, keystore.wallets.count) |
|
|
|
|
|
|
|
|
@ -136,13 +147,16 @@ class EtherKeystoreTests: XCTestCase { |
|
|
|
|
XCTAssertTrue(keystore.wallets.isEmpty) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func testConvertPrivateKeyToKeyStore() { |
|
|
|
|
func testConvertPrivateKeyToKeyStore() throws { |
|
|
|
|
let passphrase = "MyHardPassword!" |
|
|
|
|
let keystore = FakeEtherKeystore() |
|
|
|
|
let result = (try! LegacyFileBasedKeystore(keystore: keystore)).convertPrivateKeyToKeystoreFile(privateKey: Data(hexString: TestKeyStore.testPrivateKey)!, passphrase: passphrase) |
|
|
|
|
let dict = try! result.dematerialize() |
|
|
|
|
let dict = try result.dematerialize() |
|
|
|
|
keystore.importWallet(type: .keystore(string: dict.jsonString!, password: passphrase)) { result in |
|
|
|
|
let wallet = try! result.dematerialize() |
|
|
|
|
guard let wallet = try? result.dematerialize() else { |
|
|
|
|
XCTFail("Failure to import wallet") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
XCTAssertEqual(wallet.address.eip55String, "0x95fc7381950Db9d7ab116099c4E84AFD686e3e9C") |
|
|
|
|
XCTAssertEqual(1, keystore.wallets.count) |
|
|
|
|
} |
|
|
|
@ -154,9 +168,15 @@ class EtherKeystoreTests: XCTestCase { |
|
|
|
|
|
|
|
|
|
keystore.importWallet(type: .privateKey(privateKey: Data(hexString: "0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318")!)) { result in |
|
|
|
|
expectation.fulfill() |
|
|
|
|
let wallet = try! result.dematerialize() |
|
|
|
|
guard let wallet = try? result.dematerialize() else { |
|
|
|
|
XCTFail("Failure to import wallet") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
let signResult = keystore.signPersonalMessage("Some data".data(using: .utf8)!, for: wallet.address) |
|
|
|
|
let data = try! signResult.dematerialize() |
|
|
|
|
guard let data = try? signResult.dematerialize() else { |
|
|
|
|
XCTFail("Failure to import wallet") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
let expected = Data(hexString: "0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a0291c") |
|
|
|
|
XCTAssertEqual(expected, data) |
|
|
|
|
} |
|
|
|
@ -178,9 +198,15 @@ class EtherKeystoreTests: XCTestCase { |
|
|
|
|
|
|
|
|
|
keystore.importWallet(type: .mnemonic(words: ["nuclear", "you", "cage", "screen", "tribe", "trick", "limb", "smart", "dad", "voice", "nut", "jealous"], password: "")) { result in |
|
|
|
|
expectation.fulfill() |
|
|
|
|
let wallet = try! result.dematerialize() |
|
|
|
|
guard let wallet = try? result.dematerialize() else { |
|
|
|
|
XCTFail("Failure to import wallet") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
let signResult = keystore.signPersonalMessage("Some data".data(using: .utf8)!, for: wallet.address) |
|
|
|
|
let data = try! signResult.dematerialize() |
|
|
|
|
guard let data = try? signResult.dematerialize() else { |
|
|
|
|
XCTFail("Failure to import wallet") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
let expected = Data(hexString: "0x03f79a4efa290627cf3e134debd95f6effb60b1119997050fba7f6fd34db17144c8873b8a7a312797623f21a3e69e895d2afe3e1cb334f4bf46c58c5aaab9dac1c") |
|
|
|
|
XCTAssertEqual(expected, data) |
|
|
|
|
} |
|
|
|
@ -191,9 +217,15 @@ class EtherKeystoreTests: XCTestCase { |
|
|
|
|
let keystore = FakeEtherKeystore() |
|
|
|
|
|
|
|
|
|
keystore.importWallet(type: .privateKey(privateKey: Data(hexString: "0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318")!)) { result in |
|
|
|
|
let wallet = try! result.dematerialize() |
|
|
|
|
guard let wallet = try? result.dematerialize() else { |
|
|
|
|
XCTFail("Failure to import wallet") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
let signResult = keystore.signPersonalMessage("0x3f44c2dfea365f01c1ada3b7600db9e2999dfea9fe6c6017441eafcfbc06a543".data(using: .utf8)!, for: wallet.address) |
|
|
|
|
let data = try! signResult.dematerialize() |
|
|
|
|
guard let data = try? signResult.dematerialize() else { |
|
|
|
|
XCTFail("Failure to import wallet") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
let expected = Data(hexString: "0x619b03743672e31ad1d7ee0e43f6802860082d161acc602030c495a12a68b791666764ca415a2b3083595aee448402874a5a376ea91855051e04c7b3e4693d201c") |
|
|
|
|
XCTAssertEqual(expected, data) |
|
|
|
|
} |
|
|
|
@ -214,7 +246,10 @@ class EtherKeystoreTests: XCTestCase { |
|
|
|
|
|
|
|
|
|
// TODO. Move this into sync calls |
|
|
|
|
keystore.importWallet(type: ImportType.watch(address: address)) { result in |
|
|
|
|
let wallet = try! result.dematerialize() |
|
|
|
|
guard let wallet = try? result.dematerialize() else { |
|
|
|
|
XCTFail("Failure to import wallet") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
XCTAssertEqual(1, keystore.wallets.count) |
|
|
|
|
XCTAssertEqual(address, keystore.wallets[0].address) |
|
|
|
|
|
|
|
|
|