Skip to content

Commit

Permalink
Also fixed #242 and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
v57 committed Oct 22, 2018
1 parent d8e939a commit 143d9b5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 45 deletions.
35 changes: 19 additions & 16 deletions web3swift/Convenience/CryptoExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 0 additions & 4 deletions web3swift/Convenience/LibSecp256k1Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion web3swift/KeystoreManager/EthereumKeystoreV3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions web3swift/KeystoreManager/KeystoreManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down
4 changes: 2 additions & 2 deletions web3swiftTests/BetterABI/BetterERC20Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
35 changes: 17 additions & 18 deletions web3swiftTests/KeystoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 143d9b5

Please sign in to comment.