Skip to content

Commit

Permalink
[ARMORY-29]
Browse files Browse the repository at this point in the history
- create wallets from ClearTextSeed
- create EncryptedSeed at from ClearTextSeed at wallet creation
- convert EncryptedSeed to ClearTextSeed
- create WalletBackup from ClearTextSeed and vice versa
  • Loading branch information
goatpig committed May 28, 2023
1 parent 0076e1c commit 1e16f07
Show file tree
Hide file tree
Showing 31 changed files with 3,650 additions and 1,571 deletions.
536 changes: 493 additions & 43 deletions build-aux/m4/ax_cxx_compile_stdcxx.m4

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions build-aux/m4/ax_cxx_compile_stdcxx_17.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# =============================================================================
# https://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx_17.html
# =============================================================================
#
# SYNOPSIS
#
# AX_CXX_COMPILE_STDCXX_17([ext|noext], [mandatory|optional])
#
# DESCRIPTION
#
# Check for baseline language coverage in the compiler for the C++17
# standard; if necessary, add switches to CXX and CXXCPP to enable
# support.
#
# This macro is a convenience alias for calling the AX_CXX_COMPILE_STDCXX
# macro with the version set to C++17. The two optional arguments are
# forwarded literally as the second and third argument respectively.
# Please see the documentation for the AX_CXX_COMPILE_STDCXX macro for
# more information. If you want to use this macro, you also need to
# download the ax_cxx_compile_stdcxx.m4 file.
#
# LICENSE
#
# Copyright (c) 2015 Moritz Klammler <moritz@klammler.eu>
# Copyright (c) 2016 Krzesimir Nowak <qdlacz@gmail.com>
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice
# and this notice are preserved. This file is offered as-is, without any
# warranty.

#serial 2

AX_REQUIRE_DEFINED([AX_CXX_COMPILE_STDCXX])
AC_DEFUN([AX_CXX_COMPILE_STDCXX_17], [AX_CXX_COMPILE_STDCXX([17], [$1], [$2])])
4 changes: 2 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ AC_CONFIG_MACRO_DIR([build-aux/m4])
m4_include([build-aux/m4/ax_check_compile_flag.m4])
m4_include([build-aux/m4/ax_cxx_compile_stdcxx.m4])

#check for c++14
AX_CXX_COMPILE_STDCXX([14], [noext], [mandatory], [nodefault])
#check for c++17
AX_CXX_COMPILE_STDCXX_17([noext], [mandatory])

# Make the compilation flags quiet unless V=1 is used.
m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
Expand Down
6 changes: 2 additions & 4 deletions cppForSwig/BinaryData.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class BinaryData
/////////////////////////////////////////////////////////////////////////////
BinaryData(void) : data_(0) { }
explicit BinaryData(size_t sz) { alloc(sz); }
BinaryData(uint8_t const * inData, size_t sz)
BinaryData(uint8_t const * inData, size_t sz)
{ copyFrom(inData, sz); }
BinaryData(char const * inData, size_t sz) { copyFrom(inData, sz); }
BinaryData(uint8_t const * dstart, uint8_t const * dend )
Expand Down Expand Up @@ -561,16 +561,14 @@ class BinaryData
std::vector<uint8_t> data_;

private:
void alloc(size_t sz)
void alloc(size_t sz)
{
if(sz != getSize())
{
data_.clear();
data_.resize(sz);
}

}

};


Expand Down
61 changes: 47 additions & 14 deletions cppForSwig/BridgeAPI/CppBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ void CppBridge::createBackupStringForWallet(const string& waaId,
});
auto lbd = passPromptObj->getLambda();

Armory::Seeds::WalletBackup backupData;
unique_ptr<Armory::Seeds::WalletBackup> backupData = nullptr;
try
{
//grab wallet
Expand All @@ -290,33 +290,66 @@ void CppBridge::createBackupStringForWallet(const string& waaId,
auto reply = payload->mutable_reply();
reply->set_reference_id(msgId);

if (backupData.rootClear_.empty())
if (backupData == nullptr)
{
//return on error
reply->set_success(false);
writeToClient(move(payload));
return;
}

auto backupStringProto = reply->mutable_wallet()->mutable_backup_string();
for (auto& line : backupData.rootClear_)
backupStringProto->add_root_clear(line);
auto backupE16 = dynamic_cast<Armory::Seeds::Backup_Easy16*>(
backupData.get());
if (backupE16 == nullptr)
{
throw runtime_error("[createBackupStringForWallet]"
" invalid backup type");
}

for (auto& line : backupData.rootEncr_)
backupStringProto->add_root_encr(line);
auto backupStringProto = reply->mutable_wallet()->mutable_backup_string();

if (!backupData.chaincodeClear_.empty())
//cleartext root
{
for (auto& line : backupData.chaincodeClear_)
backupStringProto->add_chain_clear(line);
auto line1 = backupE16->getRoot(
Armory::Seeds::Backup_Easy16::LineIndex::One, false);
backupStringProto->add_root_clear(line1.data(), line1.size());
auto line2 = backupE16->getRoot(
Armory::Seeds::Backup_Easy16::LineIndex::Two, false);
backupStringProto->add_root_clear(line2.data(), line2.size());

//encrypted root
auto line3 = backupE16->getRoot(
Armory::Seeds::Backup_Easy16::LineIndex::One, true);
backupStringProto->add_root_encr(line3.data(), line3.size());
auto line4 = backupE16->getRoot(
Armory::Seeds::Backup_Easy16::LineIndex::Two, true);
backupStringProto->add_root_encr(line3.data(), line3.size());
}

for (auto& line : backupData.chaincodeEncr_)
backupStringProto->add_chain_encr(line);
if (backupE16->hasChaincode())
{
//cleartext chaincode
auto line1 = backupE16->getChaincode(
Armory::Seeds::Backup_Easy16::LineIndex::One, false);
backupStringProto->add_chain_clear(line1.data(), line1.size());

auto line2 = backupE16->getChaincode(
Armory::Seeds::Backup_Easy16::LineIndex::Two, false);
backupStringProto->add_chain_clear(line2.data(), line2.size());

//encrypted chaincode
auto line3 = backupE16->getChaincode(
Armory::Seeds::Backup_Easy16::LineIndex::One, true);
backupStringProto->add_chain_encr(line3.data(), line3.size());

auto line4 = backupE16->getChaincode(
Armory::Seeds::Backup_Easy16::LineIndex::Two, true);
backupStringProto->add_chain_encr(line4.data(), line4.size());
}

//secure print passphrase
backupStringProto->set_sp_pass(
backupData.spPass_.toCharPtr(), backupData.spPass_.getSize());
auto spPass = backupE16->getSpPass();
backupStringProto->set_sp_pass(spPass.data(), spPass.size());

reply->set_success(true);
writeToClient(move(payload));
Expand Down
21 changes: 14 additions & 7 deletions cppForSwig/BridgeAPI/WalletManager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2016-20, goatpig //
// Copyright (C) 2016-2023, goatpig //
// Distributed under the MIT license //
// See LICENSE-MIT or https://opensource.org/licenses/MIT //
// //
Expand All @@ -9,6 +9,7 @@
#include "WalletManager.h"
#include "Wallets/Seeds/Backups.h"
#include "PassphrasePrompt.h"
#include "../Wallets/Seeds/Seeds.h"

#ifdef _WIN32
#include "leveldb_windows_port\win32_posix\dirent_win32.h"
Expand All @@ -20,6 +21,7 @@ using namespace std;
using namespace Armory::Signer;
using namespace Armory::Accounts;
using namespace Armory::Wallets;
using namespace Armory::Seeds;

#define WALLET_135_HEADER "\xbaWALLET\x00"
#define PYBTC_ADDRESS_SIZE 237
Expand Down Expand Up @@ -151,8 +153,11 @@ shared_ptr<WalletContainer> WalletManager::createNewWallet(
if (extraEntropy.getSize() >= 32)
root.XOR(extraEntropy);

auto wallet = AssetWallet_Single::createFromPrivateRoot_Armory135(
path_, root, {}, pass, controlPass, lookup);
unique_ptr<ClearTextSeed> seed(new ClearTextSeed_Armory135(root,
ClearTextSeed_Armory135::LegacyType::Armory200));
auto wallet = AssetWallet_Single::createFromSeed(move(seed),
pass, controlPass,
path_, lookup);
return addWallet(wallet, wallet->getMainAccountID());
}

Expand Down Expand Up @@ -635,7 +640,7 @@ map<BinaryData, shared_ptr<AddressEntry>> WalletContainer::getUpdatedAddressMap(
}

////////////////////////////////////////////////////////////////////////////////
Armory::Seeds::WalletBackup WalletContainer::getBackupStrings(
unique_ptr<Armory::Seeds::WalletBackup> WalletContainer::getBackupStrings(
const PassphraseLambda& passLbd) const
{
auto wltSingle = dynamic_pointer_cast<AssetWallet_Single>(wallet_);
Expand Down Expand Up @@ -932,9 +937,11 @@ shared_ptr<AssetWallet_Single> Armory135Header::migrate(
}
else
{
wallet = AssetWallet_Single::createFromPrivateRoot_Armory135(
folder, decryptedRoot, chaincodeCopy,
privKeyPass, controlPass, highestIndex);
unique_ptr<ClearTextSeed> seed(new ClearTextSeed_Armory135(
decryptedRoot, chaincodeCopy));
wallet = AssetWallet_Single::createFromSeed(move(seed),
privKeyPass, controlPass,
folder, highestIndex);
}

//main account id, check it matches armory wallet id
Expand Down
3 changes: 2 additions & 1 deletion cppForSwig/BridgeAPI/WalletManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ class WalletContainer
Armory::Wallets::AssetKeyType getHighestUsedIndex(void) const;
std::map<BinaryData, std::shared_ptr<AddressEntry>> getUpdatedAddressMap();

Armory::Seeds::WalletBackup getBackupStrings(const PassphraseLambda&) const;
std::unique_ptr<Armory::Seeds::WalletBackup> getBackupStrings(
const PassphraseLambda&) const;

void setComment(const std::string&, const std::string&);
void setLabels(const std::string&, const std::string&);
Expand Down
12 changes: 11 additions & 1 deletion cppForSwig/SecureBinaryData.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,17 @@ class SecureBinaryData : public BinaryData
return {};

SecureBinaryData sbd(str.size());
memcpy(sbd.getPtr(), str.c_str(), str.size());
memcpy(sbd.getPtr(), str.data(), str.size());
return sbd;
}

static SecureBinaryData fromStringView(const std::string_view& strv)
{
if (strv.empty())
return {};

SecureBinaryData sbd(strv.size());
memcpy(sbd.getPtr(), strv.data(), strv.size());
return sbd;
}
};
Expand Down
4 changes: 3 additions & 1 deletion cppForSwig/Signer/ResolverFeed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ uint32_t BIP32_PublicDerivedRoot::getThisFingerprint() const
if (thisFingerprint_ == UINT32_MAX)
{
BIP32_Node node;
node.initFromBase58(SecureBinaryData::fromString(xpub_));
BinaryDataRef xpubRef;
xpubRef.setRef(xpub_);
node.initFromBase58(xpubRef);
thisFingerprint_ = node.getThisFingerprint();
}

Expand Down
1 change: 0 additions & 1 deletion cppForSwig/Wallets/AssetEncryption.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#define PRIVKEY_BYTE 0x82
#define ENCRYPTIONKEY_BYTE 0x83
#define WALLET_SEED_BYTE 0x84

#define CIPHER_DATA_VERSION 0x00000001
#define ENCRYPTION_KEY_VERSION 0x00000001
Expand Down
10 changes: 6 additions & 4 deletions cppForSwig/Wallets/AuthorizedPeers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
#include "AuthorizedPeers.h"
#include "btc/ecc.h"
#include "WalletFileInterface.h"
#include "Seeds/Seeds.h"

#include "TerminalPassphrasePrompt.h"

using namespace std;
using namespace Armory::Assets;
using namespace Armory::Accounts;
using namespace Armory::Wallets;
using namespace Armory::Seeds;

////////////////////////////////////////////////////////////////////////////////
AuthorizedPeers::AuthorizedPeers(
Expand Down Expand Up @@ -170,10 +172,10 @@ void AuthorizedPeers::createWallet(
derPath.push_back(0xF0000000);

//generate bip32 node from random seed
auto&& seed = CryptoPRNG::generateRandom(32);

wallet_ = AssetWallet_Single::createFromSeed_BIP32_Blank(
baseDir, seed, password, controlPassphrase);
wallet_ = AssetWallet_Single::createFromSeed(
make_unique<ClearTextSeed_BIP32>(
CryptoPRNG::generateRandom(32), SeedType::BIP32_Virgin),
password, controlPassphrase, baseDir);
auto wltSingle = dynamic_pointer_cast<AssetWallet_Single>(wallet_);

auto rootBip32 = dynamic_pointer_cast<AssetEntry_BIP32Root>(
Expand Down
2 changes: 1 addition & 1 deletion cppForSwig/Wallets/BIP32_Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void BIP32_Node::initFromSeed(const SecureBinaryData& seed)
}

////////////////////////////////////////////////////////////////////////////////
void BIP32_Node::initFromBase58(const SecureBinaryData& b58)
void BIP32_Node::initFromBase58(BinaryDataRef b58)
{
//sbd doesnt 0 terminate strings as it is not specialized for char strings,
//have to set it manually since libbtc b58 code derives string length from
Expand Down
2 changes: 1 addition & 1 deletion cppForSwig/Wallets/BIP32_Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class BIP32_Node

//init
void initFromSeed(const SecureBinaryData&);
void initFromBase58(const SecureBinaryData&);
void initFromBase58(BinaryDataRef);
void initFromPrivateKey(uint8_t depth, unsigned leaf_id, unsigned fingerprint,
const SecureBinaryData& privKey, const SecureBinaryData& chaincode);
void initFromPublicKey(uint8_t depth, unsigned leaf_id, unsigned fingerprint,
Expand Down
Loading

0 comments on commit 1e16f07

Please sign in to comment.