diff --git a/web3swift/Convenience/CryptoExtensions.swift b/web3swift/Convenience/CryptoExtensions.swift index 5c9b483e..593a278d 100644 --- a/web3swift/Convenience/CryptoExtensions.swift +++ b/web3swift/Convenience/CryptoExtensions.swift @@ -78,6 +78,7 @@ private class Scrypt { /* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */ let barray = try PKCS5.PBKDF2(password: password, salt: [UInt8](salt), iterations: 1, keyLength: p * 128 * r, variant: .sha256).calculate() + barray.withUnsafeBytes { p in B.copyMemory(from: p.baseAddress!, byteCount: barray.count) } @@ -109,11 +110,13 @@ private class Scrypt { for k in 0 ..< 32 * r { X[k] = (block + 4 * k).load(as: UInt32.self) } + /* 2: for i = 0 to N - 1 do */ for i in stride(from: 0, to: N, by: 2) { /* 3: V_i <-- X */ UnsafeMutableRawPointer(v + i * (32 * r)).copyMemory(from: X, byteCount: 128 * r) + /* 4: X <-- H(X) */ blockMixSalsa8(X, Y, Z) @@ -240,22 +243,22 @@ private class Scrypt { x14 ^= rotate(x13 &+ x12, 13) x15 ^= rotate(x14 &+ x13, 18) } - block[0] = x0 - block[1] = x1 - block[2] = x2 - block[3] = x3 - block[4] = x4 - block[5] = x5 - block[6] = x6 - block[7] = x7 - block[8] = x8 - block[9] = x9 - block[10] = x10 - block[11] = x11 - block[12] = x12 - block[13] = x13 - block[14] = x14 - block[15] = x15 + block[0] = block[0] &+ x0 + block[1] = block[1] &+ x1 + block[2] = block[2] &+ x2 + block[3] = block[3] &+ x3 + block[4] = block[4] &+ x4 + block[5] = block[5] &+ x5 + block[6] = block[6] &+ x6 + block[7] = block[7] &+ x7 + block[8] = block[8] &+ x8 + block[9] = block[9] &+ x9 + block[10] = block[10] &+ x10 + block[11] = block[11] &+ x11 + block[12] = block[12] &+ x12 + block[13] = block[13] &+ x13 + block[14] = block[14] &+ x14 + block[15] = block[15] &+ x15 } private func blockXor(_ dest: UnsafeMutableRawPointer, _ src: UnsafeRawPointer, _ len: Int) { diff --git a/web3swift/Convenience/LibSecp256k1Extension.swift b/web3swift/Convenience/LibSecp256k1Extension.swift index 2963b5e0..1074698a 100644 --- a/web3swift/Convenience/LibSecp256k1Extension.swift +++ b/web3swift/Convenience/LibSecp256k1Extension.swift @@ -286,10 +286,6 @@ extension SECP256K1 { guard result == 1 else { throw SECP256K1Error.invalidPrivateKey } } - static func generatePrivateKey() -> Data { - return .random(length: 32) - } - static func unmarshalSignature(signatureData: Data) throws -> UnmarshaledSignature { try signatureData.checkSignatureSize() let bytes = signatureData.bytes diff --git a/web3swift/KeystoreManager/EthereumKeystoreV3.swift b/web3swift/KeystoreManager/EthereumKeystoreV3.swift index 2c9f83c2..32c78d5c 100644 --- a/web3swift/KeystoreManager/EthereumKeystoreV3.swift +++ b/web3swift/KeystoreManager/EthereumKeystoreV3.swift @@ -61,7 +61,7 @@ public class EthereumKeystoreV3: AbstractKeystore { } public init? (password: String = "BANKEXFOUNDATION", aesMode: String = "aes-128-cbc") throws { - var newPrivateKey = SECP256K1.generatePrivateKey() + var newPrivateKey = Data.random(length: 32) defer { Data.zero(&newPrivateKey) } try encryptDataToStorage(password, keyData: newPrivateKey, aesMode: aesMode) } diff --git a/web3swift/KeystoreManager/KeystoreManager.swift b/web3swift/KeystoreManager/KeystoreManager.swift index 530580ee..ea21d39d 100644 --- a/web3swift/KeystoreManager/KeystoreManager.swift +++ b/web3swift/KeystoreManager/KeystoreManager.swift @@ -41,12 +41,12 @@ public class KeystoreManager: AbstractKeystore { return try keystore.UNSAFE_getPrivateKeyData(password: password, account: account) } - public static var allManagers = [KeystoreManager]() - public static var defaultManager: KeystoreManager? { - if KeystoreManager.allManagers.count == 0 { + public static var all = [KeystoreManager]() + public static var `default`: KeystoreManager? { + if KeystoreManager.all.count == 0 { return nil } - return KeystoreManager.allManagers[0] + return KeystoreManager.all[0] } public static func managerForPath(_ path: String, scanForHDwallets: Bool = false, suffix: String? = nil) -> KeystoreManager? { diff --git a/web3swiftTests/BetterABI/BetterERC20Tests.swift b/web3swiftTests/BetterABI/BetterERC20Tests.swift index 0b02e683..8e9f9857 100644 --- a/web3swiftTests/BetterABI/BetterERC20Tests.swift +++ b/web3swiftTests/BetterABI/BetterERC20Tests.swift @@ -47,12 +47,12 @@ class BetterERC20Tests: XCTestCase { } func testERC20Name() throws { - let name = try contract.call("name()").string() + let name = try contract.call("name()").wait().string() XCTAssertEqual(name, "\"BANKEX\" project utility token") } func testERC20Balance() throws { - let balance = try contract.call("balanceOf(address)",user).uint256() + let balance = try contract.call("balanceOf(address)",user).wait().uint256() print(balance) } diff --git a/web3swiftTests/KeystoreTests.swift b/web3swiftTests/KeystoreTests.swift index b68c4d65..74186d00 100644 --- a/web3swiftTests/KeystoreTests.swift +++ b/web3swiftTests/KeystoreTests.swift @@ -12,17 +12,6 @@ import XCTest @testable import web3swift_iOS class KeystoresTests: XCTestCase { - var time = CFAbsoluteTimeGetCurrent() - func checkTime() { - print(CFAbsoluteTimeGetCurrent() - time) - time = CFAbsoluteTimeGetCurrent() - } - override func setUp() { - checkTime() - } - override func tearDown() { - checkTime() - } func testBIP39() throws { // 2.159708023071289 sec to complete @@ -39,6 +28,22 @@ class KeystoresTests: XCTestCase { seed = mnemonics.seed() XCTAssert(seed.toHexString() == "64c87cde7e12ecf6704ab95bb1408bef047c22db4cc7491c4271d170a1b213d20b385bc1588d9c7b38f1b39d415665b8a9030c9ec653d75e65f847d8fc1fc440") } + + func testImportAndExport() throws { + let json = """ +{"version":3,"id":"8b60fda9-5f27-4478-9cc9-72059571aa6e","crypto":{"ciphertext":"d34e78640359a599970a58b3b4b7c987945e56c69411028ea62394e8d1ea7e4b","cipherparams":{"iv":"6e4a429a30807ab9202a9aefad152398"},"kdf":"scrypt","kdfparams":{"r":6,"p":1,"n":4096,"dklen":32,"salt":"0000000000000000000000000000000000000000000000000000000000000000"},"mac":"79888d6ce3a2a24d6b70d07ca9067b57e4a57bd9416a3abb336900cacf82e29a","cipher":"aes-128-cbc"},"address":"0x0b0f7a95485060973726d03e7c326a6542bcb55b"} +""" + let keystore = EthereumKeystoreV3(json)! + let data = try keystore.serialize()! + let key = try keystore.UNSAFE_getPrivateKeyData(password: "hello world", account: keystore.addresses[0]).toHexString() + + let keystore2 = EthereumKeystoreV3(data)! + let data2 = try keystore2.serialize()! + let key2 = try keystore2.UNSAFE_getPrivateKeyData(password: "hello world", account: keystore.addresses[0]).toHexString() + + XCTAssertEqual(data,data2) + XCTAssertEqual(key,key2) + } func testHMAC() { // 0.0021849870681762695 sec to complete @@ -63,16 +68,11 @@ class KeystoresTests: XCTestCase { func testV3keystoreExportPrivateKey() { // 5.033522009849548 sec to complete let keystore = try! EthereumKeystoreV3(password: "") - checkTime() XCTAssertNotNil(keystore) let account = keystore!.addresses[0] - print(account) - checkTime() let data = try! keystore!.serialize() - checkTime() - print(try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions(rawValue: 0))) + _ = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions(rawValue: 0)) let key = try! keystore!.UNSAFE_getPrivateKeyData(password: "", account: account) - checkTime() XCTAssertNotNil(key) } @@ -151,7 +151,6 @@ class KeystoresTests: XCTestCase { } func testByBIP32keystoreSaveAndDeriva() throws { - // sec to complete let mnemonics = try Mnemonics("normal dune pole key case cradle unfold require tornado mercy hospital buyer") let keystore = try! BIP32Keystore(mnemonics: mnemonics, password: "", prefixPath: "m/44'/60'/0'") XCTAssertNotNil(keystore)