Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose StoredKey encryption parameters #1766

Merged
merged 13 commits into from
Nov 23, 2021
4 changes: 4 additions & 0 deletions include/TrustWalletCore/TWStoredKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,8 @@ TWData* _Nullable TWStoredKeyExportJSON(struct TWStoredKey* _Nonnull key);
TW_EXPORT_METHOD
bool TWStoredKeyFixAddresses(struct TWStoredKey* _Nonnull key, TWData* _Nonnull password);

/// Retrieve stored key encoding parameters, as JSON string.
TW_EXPORT_PROPERTY
TWString* _Nullable TWStoredKeyEncryptionParameters(struct TWStoredKey* _Nonnull key);

TW_EXTERN_C_END
8 changes: 8 additions & 0 deletions src/interface/TWStoredKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,11 @@ bool TWStoredKeyFixAddresses(struct TWStoredKey* _Nonnull key, TWData* _Nonnull
return false;
}
}

TWString* _Nullable TWStoredKeyEncryptionParameters(struct TWStoredKey* _Nonnull key) {
if (!key->impl.id) {
return nullptr;
}
const std::string params = key->impl.payload.json().dump();
return TWStringCreateWithUTF8Bytes(params.c_str());
}
21 changes: 21 additions & 0 deletions swift/Tests/Keystore/KeystoreKeyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,25 @@ class KeystoreKeyTests: XCTestCase {
let data = keystore.decryptPrivateKey(password: password)
XCTAssertEqual(data?.hexString, "4357b2f9a6150ba969bc52f01c98cce5313595fe49f2d08303759c73e5c7a46c")
}

struct KdfParams: Decodable {
let dklen: Int
let n: Int
}

struct EncryptionParameters: Decodable {
optout21 marked this conversation as resolved.
Show resolved Hide resolved
let kdf: String
let kdfparams: KdfParams
optout21 marked this conversation as resolved.
Show resolved Hide resolved
}

func testEncryptionParameters() {
optout21 marked this conversation as resolved.
Show resolved Hide resolved
let url = Bundle(for: type(of: self)).url(forResource: "key", withExtension: "json")!
let key = StoredKey.load(path: url.path)!

let paramsData = key.encryptionParameters!.data(using: .utf8)!
let params = try! JSONDecoder().decode(EncryptionParameters.self, from: paramsData)

XCTAssertEqual(params.kdf, "scrypt");
XCTAssertEqual(params.kdfparams.n, 262144);
}
}
35 changes: 35 additions & 0 deletions tests/interface/TWStoredKeyTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "../src/HexCoding.h"

#include <gtest/gtest.h>
#include <nlohmann/json.hpp>

#include <fstream>

Expand Down Expand Up @@ -225,3 +226,37 @@ TEST(TWStoredKey, getWalletPasswordInvalid) {
ASSERT_NE(WRAP(TWHDWallet, TWStoredKeyWallet(key.get(), password.get())).get(), nullptr);
ASSERT_EQ(WRAP(TWHDWallet, TWStoredKeyWallet(key.get(), passwordInvalid.get())).get(), nullptr);
}

TEST(TWStoredKey, encryptionParameters) {
const auto key = createDefaultStoredKey();
const auto params = WRAPS(TWStoredKeyEncryptionParameters(key.get()));

nlohmann::json jsonParams = nlohmann::json::parse(string(TWStringUTF8Bytes(params.get())));

// compare some specific parameters
EXPECT_EQ(jsonParams["kdfparams"]["n"], 4096);
EXPECT_EQ(std::string(jsonParams["cipherparams"]["iv"]).length(), 32);

// compare all keys, except dynamic ones (like cipherparams/iv)
optout21 marked this conversation as resolved.
Show resolved Hide resolved
jsonParams["cipherparams"] = {};
jsonParams["ciphertext"] = "<ciphertext>";
jsonParams["kdfparams"]["salt"] = "<salt>";
jsonParams["mac"] = "<mac>";
const auto params2 = jsonParams.dump();
assertJSONEqual(params2, R"(
{
"cipher": "aes-128-ctr",
"cipherparams": null,
"ciphertext": "<ciphertext>",
"kdf": "scrypt",
"kdfparams": {
"dklen": 32,
"n": 4096,
"p": 6,
"r": 8,
"salt": "<salt>"
},
"mac": "<mac>"
}
)");
}