Skip to content

Commit

Permalink
Fix Non-void return type missing warnings (#2356)
Browse files Browse the repository at this point in the history
* format(code): apply clang format
- apply clang format on Transaction.cpp, StoredKey.cpp, Extrinsic.cpp and PublicKey.cpp
* fix(non_void_functions): apply fixes from #2351
* tweak ColumnLimit
* add comments
Co-authored-by: hewigovens <[email protected]>
  • Loading branch information
Milerius authored Jul 11, 2022
1 parent c91bb4e commit 85689ef
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 229 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ AllowShortFunctionsOnASingleLine: Inline
AlwaysBreakTemplateDeclarations: Yes
BasedOnStyle: LLVM
BreakConstructorInitializers: BeforeComma
ColumnLimit: 100
ColumnLimit: 0
ConstructorInitializerAllOnOneLineOrOnePerLine: true
IndentWidth: 4
IndentAccessModifiers: false
Expand Down
29 changes: 17 additions & 12 deletions src/Bitcoin/Transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.

#include "SegwitAddress.h"
#include "Transaction.h"
#include "SegwitAddress.h"
#include "SigHashType.h"
#include "../BinaryCoding.h"
#include "../Hash.h"
#include "../Data.h"
#include "../Hash.h"

#include "SignatureVersion.h"

Expand All @@ -36,8 +36,8 @@ Data Transaction::getPreImage(const Script& scriptCode, size_t index,
}

// Input nSequence (none/all, depending on flags)
if ((hashType & TWBitcoinSigHashTypeAnyoneCanPay) == 0 &&
!hashTypeIsSingle(hashType) && !hashTypeIsNone(hashType)) {
if ((hashType & TWBitcoinSigHashTypeAnyoneCanPay) == 0 && !hashTypeIsSingle(hashType) &&
!hashTypeIsNone(hashType)) {
auto hashSequence = getSequenceHash();
std::copy(std::begin(hashSequence), std::end(hashSequence), std::back_inserter(data));
} else {
Expand Down Expand Up @@ -106,9 +106,15 @@ Data Transaction::getOutputsHash() const {
void Transaction::encode(Data& data, enum SegwitFormatMode segwitFormat) const {
bool useWitnessFormat = true;
switch (segwitFormat) {
case NonSegwit: useWitnessFormat = false; break;
case IfHasWitness: useWitnessFormat = hasWitness(); break;
case Segwit: useWitnessFormat = true; break;
case NonSegwit:
useWitnessFormat = false;
break;
case IfHasWitness:
useWitnessFormat = hasWitness();
break;
case Segwit:
useWitnessFormat = true;
break;
}

encode32LE(version, data);
Expand Down Expand Up @@ -145,18 +151,17 @@ void Transaction::encodeWitness(Data& data) const {
}

bool Transaction::hasWitness() const {
return std::any_of(inputs.begin(), inputs.end(), [](auto& input) { return !input.scriptWitness.empty(); });
return std::any_of(inputs.begin(), inputs.end(), [](auto& input) { return !input.scriptWitness.empty(); });
}

Data Transaction::getSignatureHash(const Script& scriptCode, size_t index,
enum TWBitcoinSigHashType hashType, uint64_t amount,
enum SignatureVersion version) const {
switch (version) {
case BASE:
if (version == BASE) {
return getSignatureHashBase(scriptCode, index, hashType);
case WITNESS_V0:
return getSignatureHashWitnessV0(scriptCode, index, hashType, amount);
}
// version == WITNESS_V0
return getSignatureHashWitnessV0(scriptCode, index, hashType, amount);
}

/// Generates the signature hash for Witness version 0 scripts.
Expand Down
132 changes: 62 additions & 70 deletions src/Keystore/StoredKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
#include <boost/uuid/uuid_io.hpp>
#include <nlohmann/json.hpp>

#include <cassert>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <cassert>

using namespace TW;
using namespace TW::Keystore;
Expand All @@ -32,19 +32,17 @@ StoredKey StoredKey::createWithMnemonic(const std::string& name, const Data& pas
if (!Mnemonic::isValid(mnemonic)) {
throw std::invalid_argument("Invalid mnemonic");
}

Data mnemonicData = TW::Data(mnemonic.begin(), mnemonic.end());
StoredKey key = StoredKey(StoredKeyType::mnemonicPhrase, name, password, mnemonicData, encryptionLevel);
return key;
return StoredKey(StoredKeyType::mnemonicPhrase, name, password, mnemonicData, encryptionLevel);
}

StoredKey StoredKey::createWithMnemonicRandom(const std::string& name, const Data& password, TWStoredKeyEncryptionLevel encryptionLevel) {
const auto wallet = TW::HDWallet(128, "");
const auto& mnemonic = wallet.getMnemonic();
assert(Mnemonic::isValid(mnemonic));
Data mnemonicData = TW::Data(mnemonic.begin(), mnemonic.end());
StoredKey key = StoredKey(StoredKeyType::mnemonicPhrase, name, password, mnemonicData, encryptionLevel);
return key;
return StoredKey(StoredKeyType::mnemonicPhrase, name, password, mnemonicData, encryptionLevel);
}

StoredKey StoredKey::createWithMnemonicAddDefaultAddress(const std::string& name, const Data& password, const std::string& mnemonic, TWCoinType coin) {
Expand All @@ -55,8 +53,7 @@ StoredKey StoredKey::createWithMnemonicAddDefaultAddress(const std::string& name
}

StoredKey StoredKey::createWithPrivateKey(const std::string& name, const Data& password, const Data& privateKeyData) {
StoredKey key = StoredKey(StoredKeyType::privateKey, name, password, privateKeyData, TWStoredKeyEncryptionLevelDefault);
return key;
return StoredKey(StoredKeyType::privateKey, name, password, privateKeyData, TWStoredKeyEncryptionLevelDefault);
}

StoredKey StoredKey::createWithPrivateKeyAddDefaultAddress(const std::string& name, const Data& password, TWCoinType coin, const Data& privateKeyData) {
Expand Down Expand Up @@ -93,7 +90,7 @@ const HDWallet StoredKey::wallet(const Data& password) const {

std::vector<Account> StoredKey::getAccounts(TWCoinType coin) const {
std::vector<Account> result;
for (auto& account: accounts) {
for (auto& account : accounts) {
if (account.coin == coin) {
result.push_back(account);
}
Expand All @@ -112,7 +109,7 @@ std::optional<Account> StoredKey::getDefaultAccount(TWCoinType coin, const HDWal
}
// no wallet or not found, rely on derivation=0 condition
const auto coinAccounts = getAccounts(coin);
for (auto& account: coinAccounts) {
for (auto& account : coinAccounts) {
if (account.derivation == TWDerivationDefault) {
return account;
}
Expand Down Expand Up @@ -219,8 +216,7 @@ void StoredKey::addAccount(
TWDerivation derivation,
const DerivationPath& derivationPath,
const std::string& publicKey,
const std::string& extendedPublicKey
) {
const std::string& extendedPublicKey) {
if (getAccount(coin, address).has_value()) {
// address already present
return;
Expand All @@ -229,78 +225,73 @@ void StoredKey::addAccount(
}

void StoredKey::removeAccount(TWCoinType coin) {
accounts.erase(std::remove_if(accounts.begin(), accounts.end(), [coin](Account& account) -> bool {
return account.coin == coin;
}), accounts.end());
accounts.erase(
std::remove_if(accounts.begin(), accounts.end(), [coin](Account& account) -> bool { return account.coin == coin; }),
accounts.end());
}

void StoredKey::removeAccount(TWCoinType coin, TWDerivation derivation) {
accounts.erase(std::remove_if(accounts.begin(), accounts.end(), [coin, derivation](Account& account) -> bool {
return account.coin == coin && account.derivation == derivation;
}), accounts.end());
accounts.erase(
std::remove_if(accounts.begin(), accounts.end(), [coin, derivation](Account& account) -> bool {
return account.coin == coin && account.derivation == derivation;
}),
accounts.end());
}

void StoredKey::removeAccount(TWCoinType coin, DerivationPath derivationPath) {
accounts.erase(std::remove_if(accounts.begin(), accounts.end(), [coin, derivationPath](Account& account) -> bool {
return account.coin == coin && account.derivationPath == derivationPath;
}), accounts.end());
accounts.erase(
std::remove_if(accounts.begin(), accounts.end(), [coin, derivationPath](Account& account) -> bool {
return account.coin == coin && account.derivationPath == derivationPath;
}),
accounts.end());
}

const PrivateKey StoredKey::privateKey(TWCoinType coin, const Data& password) {
return privateKey(coin, TWDerivationDefault, password);
}

const PrivateKey StoredKey::privateKey(TWCoinType coin, TWDerivation derivation, const Data& password) {
switch (type) {
case StoredKeyType::mnemonicPhrase: {
if (type == StoredKeyType::mnemonicPhrase) {
const auto wallet = this->wallet(password);
const auto account = this->account(coin, &wallet);
return wallet.getKey(coin, account->derivationPath);
}
case StoredKeyType::privateKey:
return PrivateKey(payload.decrypt(password));
}
// type == StoredKeyType::privateKey
return PrivateKey(payload.decrypt(password));
}

void StoredKey::fixAddresses(const Data& password) {
switch (type) {
case StoredKeyType::mnemonicPhrase: {
const auto wallet = this->wallet(password);
for (auto& account : accounts) {
if (!account.address.empty() &&
!account.publicKey.empty() &&
TW::validateAddress(account.coin, account.address)
) {
continue;
}
const auto& derivationPath = account.derivationPath;
const auto key = wallet.getKey(account.coin, derivationPath);
const auto pubKey = key.getPublicKey(TW::publicKeyType(account.coin));
account.address = TW::deriveAddress(account.coin, pubKey, account.derivation);
account.publicKey = hex(pubKey.bytes);
}
case StoredKeyType::mnemonicPhrase: {
const auto wallet = this->wallet(password);
for (auto& account : accounts) {
if (!account.address.empty() && !account.publicKey.empty() &&
TW::validateAddress(account.coin, account.address)) {
continue;
}
break;

case StoredKeyType::privateKey: {
auto key = PrivateKey(payload.decrypt(password));
for (auto& account : accounts) {
if (!account.address.empty() &&
!account.publicKey.empty() &&
TW::validateAddress(account.coin, account.address)
) {
continue;
}
const auto pubKey = key.getPublicKey(TW::publicKeyType(account.coin));
account.address = TW::deriveAddress(account.coin, pubKey, account.derivation);
account.publicKey = hex(pubKey.bytes);
}
const auto& derivationPath = account.derivationPath;
const auto key = wallet.getKey(account.coin, derivationPath);
const auto pubKey = key.getPublicKey(TW::publicKeyType(account.coin));
account.address = TW::deriveAddress(account.coin, pubKey, account.derivation);
account.publicKey = hex(pubKey.bytes);
}
} break;

case StoredKeyType::privateKey: {
auto key = PrivateKey(payload.decrypt(password));
for (auto& account : accounts) {
if (!account.address.empty() && !account.publicKey.empty() &&
TW::validateAddress(account.coin, account.address)) {
continue;
}
break;
const auto pubKey = key.getPublicKey(TW::publicKeyType(account.coin));
account.address = TW::deriveAddress(account.coin, pubKey, account.derivation);
account.publicKey = hex(pubKey.bytes);
}
} break;
}
}


// -----------------
// Encoding/Decoding
// -----------------
Expand All @@ -312,23 +303,23 @@ StoredKey StoredKey::createWithJson(const nlohmann::json& json) {
}

namespace CodingKeys {
static const auto address = "address";
static const auto type = "type";
static const auto name = "name";
static const auto id = "id";
static const auto crypto = "crypto";
static const auto activeAccounts = "activeAccounts";
static const auto version = "version";
static const auto coin = "coin";
static const auto address = "address";
static const auto type = "type";
static const auto name = "name";
static const auto id = "id";
static const auto crypto = "crypto";
static const auto activeAccounts = "activeAccounts";
static const auto version = "version";
static const auto coin = "coin";
} // namespace CodingKeys

namespace UppercaseCodingKeys {
static const auto crypto = "Crypto";
static const auto crypto = "Crypto";
} // namespace UppercaseCodingKeys

namespace TypeString {
static const auto privateKey = "private-key";
static const auto mnemonic = "mnemonic";
static const auto privateKey = "private-key";
static const auto mnemonic = "mnemonic";
} // namespace TypeString

void StoredKey::loadJson(const nlohmann::json& json) {
Expand Down Expand Up @@ -363,7 +354,8 @@ void StoredKey::loadJson(const nlohmann::json& json) {
}
}

if (accounts.empty() && json.count(CodingKeys::address) != 0 && json[CodingKeys::address].is_string()) {
if (accounts.empty() && json.count(CodingKeys::address) != 0 &&
json[CodingKeys::address].is_string()) {
TWCoinType coin = TWCoinTypeEthereum;
if (json.count(CodingKeys::coin) != 0) {
coin = json[CodingKeys::coin].get<TWCoinType>();
Expand Down
Loading

0 comments on commit 85689ef

Please sign in to comment.