Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Add tests for creating public key with no known private key #10838

Merged
merged 1 commit into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions unittests/auth_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,44 @@ try {

} FC_LOG_AND_RETHROW() }

BOOST_AUTO_TEST_CASE(update_auth_unknown_private_key) {
try {
TESTER chain;
chain.create_account(name("alice"));

// Change owner permission
const auto new_owner_priv_key = chain.get_private_key(name("alice"), "new_owner");

// public key with no corresponding private key
fc::ecc::public_key_data data;
data.data[0] = 0x80; // not necessary, 0 also works
fc::sha256 hash = fc::sha256::hash("unknown key");
std::memcpy(&data.data[1], hash.data(), hash.data_size() );
fc::ecc::public_key_shim shim(data);
fc::crypto::public_key new_owner_pub_key(std::move(shim));

chain.set_authority(name("alice"), name("owner"), authority(new_owner_pub_key), {});
chain.produce_blocks();

// Ensure the permission is updated
permission_object::id_type owner_id;
{
auto obj = chain.find<permission_object, by_owner>(boost::make_tuple(name("alice"), name("owner")));
BOOST_TEST(obj != nullptr);
BOOST_TEST(obj->owner == name("alice"));
BOOST_TEST(obj->name == name("owner"));
BOOST_TEST(obj->parent == 0);
owner_id = obj->id;
auto auth = obj->auth.to_authority();
BOOST_TEST(auth.threshold == 1u);
BOOST_TEST(auth.keys.size() == 1u);
BOOST_TEST(auth.accounts.size() == 0u);
BOOST_TEST(auth.keys[0].key == new_owner_pub_key);
BOOST_TEST(auth.keys[0].weight == 1);
}
} FC_LOG_AND_RETHROW()
}

BOOST_AUTO_TEST_CASE(link_auths) { try {
TESTER chain;

Expand Down
20 changes: 20 additions & 0 deletions unittests/misc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,26 @@ BOOST_AUTO_TEST_CASE(bad_alloc_test) {
BOOST_CHECK( ptr == nullptr );
}

BOOST_AUTO_TEST_CASE(public_key_from_hash) {
auto private_key_string = std::string("5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3");
auto expected_public_key = std::string("EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV");
auto test_private_key = fc::crypto::private_key(private_key_string);
auto test_public_key = test_private_key.get_public_key();
fc::crypto::public_key eos_pk(expected_public_key);

BOOST_CHECK_EQUAL(private_key_string, test_private_key.to_string());
BOOST_CHECK_EQUAL(expected_public_key, test_public_key.to_string());
BOOST_CHECK_EQUAL(expected_public_key, eos_pk.to_string());

fc::ecc::public_key_data data;
data.data[0] = 0x80; // not necessary, 0 also works
fc::sha256 hash = fc::sha256::hash("unknown private key");
std::memcpy(&data.data[1], hash.data(), hash.data_size() );
fc::ecc::public_key_shim shim(data);
fc::crypto::public_key eos_unknown_pk(std::move(shim));
ilog( "public key with no known private key: ${k}", ("k", eos_unknown_pk) );
}

BOOST_AUTO_TEST_SUITE_END()

} // namespace eosio