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

Add option to use static address #926

Merged
merged 3 commits into from
Dec 7, 2022
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
2 changes: 2 additions & 0 deletions core/idl/wallet/configuration.djinni
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ Configuration = interface +c {
const KEYCHAIN_DERIVATION_SCHEME: string = "KEYCHAIN_DERIVATION_SCHEME";
# Sets the observable range for HD keychains (BIP32 based).
const KEYCHAIN_OBSERVABLE_RANGE: string = "KEYCHAIN_OBSERVABLE_RANGE";
# Use a static address for receive & change.
const KEYCHAIN_STATIC_ADDRESS: string = "KEYCHAIN_STATIC_ADDRESS";

# Selects the blockchain explorer engine (Ledger's API, Electrum server, RPC):
const BLOCKCHAIN_EXPLORER_ENGINE: string = "BLOCKCHAIN_EXPLORER_ENGINE";
Expand Down
2 changes: 2 additions & 0 deletions core/src/api/Configuration.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions core/src/api/Configuration.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ namespace ledger {

BitcoinLikeKeychain::Address CommonBitcoinLikeKeychains::getFreshAddress(BitcoinLikeKeychain::KeyPurpose purpose) {
KeychainPersistentState state = getState();
if (getConfiguration()->getBoolean(api::Configuration::KEYCHAIN_STATIC_ADDRESS).value_or(false)) {
return derive(purpose, 0);
}
return derive(purpose, (purpose == KeyPurpose::RECEIVE ? state.maxConsecutiveReceiveIndex : state.maxConsecutiveChangeIndex));
}

Expand All @@ -214,6 +217,10 @@ namespace ledger {

std::vector<BitcoinLikeKeychain::Address>
CommonBitcoinLikeKeychains::getFreshAddresses(BitcoinLikeKeychain::KeyPurpose purpose, size_t n) {
if (getConfiguration()->getBoolean(api::Configuration::KEYCHAIN_STATIC_ADDRESS).value_or(false)) {
return {derive(purpose, 0)};
}

KeychainPersistentState state = getState();
auto startOffset = (purpose == KeyPurpose::RECEIVE) ? state.maxConsecutiveReceiveIndex : state.maxConsecutiveChangeIndex;
std::vector<BitcoinLikeKeychain::Address> result(n);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,47 @@ TEST_F(CommonBitcoinKeychains, CorrectStateUsedAtMarkPathAsUsed) {
EXPECT_EQ(0, state.nonConsecutiveReceiveIndexes.size());
});
}

template <enum ledger::core::BitcoinLikeKeychain::KeyPurpose PURPOSE, bool STATIC>
void check(const std::string &addr0, ConcreteCommonBitcoinLikeKeychains &keychain) {
EXPECT_EQ(keychain.getFreshAddress(PURPOSE)->toBase58(), addr0);
EXPECT_TRUE(keychain.markAsUsed(addr0));
if constexpr (STATIC) {
EXPECT_EQ(keychain.getFreshAddress(PURPOSE)->toBase58(), addr0);
} else {
EXPECT_NE(keychain.getFreshAddress(PURPOSE)->toBase58(), addr0);
}

const auto addrs = keychain.getFreshAddresses(PURPOSE, 10);
if constexpr (STATIC) {
EXPECT_EQ(addrs.size(), 1);
EXPECT_EQ(addrs[0]->toBase58(), addr0);
} else {
EXPECT_EQ(addrs.size(), 10);
EXPECT_NE(addrs[0]->toBase58(), addr0);
}
}

TEST_F(CommonBitcoinKeychains, StaticAddressGivesSameAddress) {
auto configuration = std::make_shared<DynamicObject>();
configuration->putBoolean(api::Configuration::KEYCHAIN_STATIC_ADDRESS, true);

constexpr auto changeAddr0 = "3FgusxCLY23j7xcWsqAwC4DthKjrVhxbwx";
constexpr auto receiveAddr0 = "3HMhvAEtfDyQSZAmM6qvLGuZYUCyZrgNr3";

testKeychain(
BTC_DATA, [changeAddr0, receiveAddr0](ConcreteCommonBitcoinLikeKeychains &keychain) {
check<ledger::core::BitcoinLikeKeychain::CHANGE, true>(changeAddr0, keychain);
check<ledger::core::BitcoinLikeKeychain::RECEIVE, true>(receiveAddr0, keychain);
},
configuration);

configuration->putBoolean(api::Configuration::KEYCHAIN_STATIC_ADDRESS, false);

testKeychain(
BTC_DATA, [changeAddr0, receiveAddr0](ConcreteCommonBitcoinLikeKeychains &keychain) {
check<ledger::core::BitcoinLikeKeychain::CHANGE, false>(changeAddr0, keychain);
check<ledger::core::BitcoinLikeKeychain::RECEIVE, false>(receiveAddr0, keychain);
},
configuration);
}
7 changes: 3 additions & 4 deletions core/test/integration/keychains/keychain_test_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,12 @@ extern KeychainTestData ETHEREUM_DATA;
template <class Keychain>
class KeychainFixture : public BaseFixture {
public:
void testKeychain(const KeychainTestData &data, std::function<void(Keychain &)> f) {
void testKeychain(const KeychainTestData &data, std::function<void(Keychain &)> f, std::shared_ptr<DynamicObject> configuration = std::make_shared<DynamicObject>()) {
auto backend = std::make_shared<ledger::core::test::MemPreferencesBackend>();
testKeychain(data, std::move(backend), f);
testKeychain(data, std::move(backend), f, configuration);
};

void testKeychain(const KeychainTestData &data, std::shared_ptr<api::PreferencesBackend> backend, std::function<void(Keychain &)> f) {
auto configuration = std::make_shared<DynamicObject>();
void testKeychain(const KeychainTestData &data, std::shared_ptr<api::PreferencesBackend> backend, std::function<void(Keychain &)> f, std::shared_ptr<DynamicObject> configuration = std::make_shared<DynamicObject>()) {
{
Keychain temp_keychain(
configuration,
Expand Down