Keystore.swift
Keystore.swift copied to clipboard
JsonKey is different between decoding and encoding?
Hi, im testing this and the jsonKey i decoded is not the same as the encoded. Is this correct? thanks Here is the unit test:
let password = "2222"
let jsonKey = "{\"address\":\"87e37a827e17f45fb0e203cfcbef0a330648c989\",\"crypto\":{\"cipher\":\"aes-128-ctr\",\"ciphertext\":\"b7bba8edb2a4b59404e8230d42d33b758c98b79287f8622beea5b5f535289adb\",\"cipherparams\":{\"iv\":\"d2492f4188e855d9a95355e15eab2c3e\"},\"kdf\":\"scrypt\",\"kdfparams\":{\"dklen\":32,\"n\":4096,\"p\":6,\"r\":8,\"salt\":\"79a80d0c914d8ea89d3a61ff05f2f5a6a3292652059b334b0e96d896f25bd693\"},\"mac\":\"54757125290da40d2d63ef48f862b8321e9c98ef6fcd41eec1464ba10898b2da\"},\"id\":\"cd1dcc57-d7d5-4fb1-8076-b96cf7f2be5d\",\"version\":3}"
let decoder = JSONDecoder()
guard let keystoreData = jsonKey.data(using: .utf8) else {
XCTFail()
return
}
let keystore = try! decoder.decode(Keystore.self, from: keystoreData)
let address = keystore.address
print("address = ", address)
let privateKey = try! keystore.privateKey(password: password)
print("exported private key = ", privateKey.toHexString())
//testImportPrivateKeyToKeystore
let restoredKeystore = try! Keystore(privateKey: privateKey, password: password)
let restoredAddress = restoredKeystore.address
print("restored address = ", restoredAddress)
XCTAssert(address == restoredAddress, "addresses are not the same")
let keystoreJson = try! JSONEncoder().encode(restoredKeystore)
//convert exportedKeysData to utf8 encoded string (jsonKeyString)
guard let jsonKeyString = String(data: keystoreJson, encoding: .utf8) else {
XCTFail()
return
}
print("jsonKey = ", jsonKeyString)
//jsonKey is different from original
//"{\"address\":\"87e37a827e17f45fb0e203cfcbef0a330648c989\",\"id\":\"5cec584f-3145-454f-84a4-bb8040e8df0f\",\"crypto\":{\"ciphertext\":\"86249561804026d568680afd7910153df886b8d3397ba21af9d2352f84ab36b79c136fca214c68a3559a42d7663cb4c7\",\"cipherparams\":{\"iv\":\"9150cd05c17c3b17274324aeb19da704\"},\"kdf\":\"scrypt\",\"kdfparams\":{\"r\":8,\"p\":1,\"n\":8192,\"dklen\":32,\"salt\":\"8378fb3859a4916cda7d0b985ab281c8f394c254a4e59b5b35bab99832a159d8\"},\"mac\":\"73071160f59ff2af6b2aab02d2516ef2ebaf22e41fd1768292b7a0401cf40860\",\"cipher\":\"aes-128-ctr\"},\"version\":3}"
XCTAssert(jsonKeyString != "", "jsonKey is empty")
```