Skip to content
Closed
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 src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ BITCOIN_CORE_H = \
wallet/crypter.h \
wallet/db.h \
wallet/fees.h \
wallet/keypool.h \
wallet/psbtwallet.h \
wallet/rpcwallet.h \
wallet/wallet.h \
Expand Down Expand Up @@ -440,6 +441,7 @@ libdash_wallet_a_SOURCES = \
wallet/db.cpp \
wallet/fees.cpp \
wallet/init.cpp \
wallet/keypool.cpp \
wallet/psbtwallet.cpp \
wallet/rpcdump.cpp \
wallet/rpcwallet.cpp \
Expand Down
63 changes: 62 additions & 1 deletion src/bls/bls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ static const std::unique_ptr<bls::CoreMPL>& Scheme(const bool fLegacy)
return fLegacy ? pSchemeLegacy : pScheme;
}

CBLSId::CBLSId(const uint256& nHash) : CBLSWrapper<CBLSIdImplicit, BLS_CURVE_ID_SIZE, CBLSId>()
CBLSId::CBLSId(const uint256& nHash) : CBLSWrapper<CBLSIdImplicit<uint256>, BLS_CURVE_ID_SIZE, CBLSId>()
{
impl = nHash;
fValid = true;
cachedHash.SetNull();
}

CBLSKeyID::CBLSKeyID(const uint160& nHash) : CBLSWrapper<CBLSIdImplicit<uint160>, BLS_CURVE_ID_SIZE, CBLSKeyID>()
{
impl = nHash;
fValid = true;
Expand Down Expand Up @@ -393,3 +400,57 @@ bool BLSInit()
#endif
return true;
}

bool CSecretKey::Load(const std::vector<unsigned char, secure_allocator<unsigned char> >& privkey, const CPublicKey& vchPubKey, bool fSkipCheck)
{
CPublicKey pkTmp;
std::vector<unsigned char> vchTmp;
vchTmp.assign(privkey.begin(), privkey.end());
pkTmp.SetByteVector(vchTmp);
SetByteVector(vchTmp);
return fSkipCheck ? IsValid() : IsValid() && vchPubKey == pkTmp;
}

bool CSecretKey::Sign(const uint256& hash, std::vector<unsigned char>& vchSig) const
{
CBLSSignature sig = Sign(hash);
if (sig.IsValid()) {
vchSig = sig.ToByteVector();
return true;
}
return false;
}

bool CSecretKey::VerifyPubKey(const CPublicKey& vchPubKey) const
{
CPublicKey vchTmp;
vchTmp.SetByteVector(ToByteVector());
return vchPubKey == vchTmp;
}

CBLSKeyID CPublicKey::GetID() const
{
return CBLSKeyID(Hash160(ToByteVector()));
}

uint256 CPublicKey::GetHash() const
{
return uint256(ToByteVector());
}

bool CPublicKey::Verify(const uint256& hash, const std::vector<unsigned char>& vchSig) const
{
return CBLSSignature(vchSig, false).VerifyInsecure(*this, hash);
}

std::vector<unsigned char, secure_allocator<unsigned char> > CSecretKey::GetPrivKey() const
{
return std::vector<unsigned char, secure_allocator<unsigned char> >(ToByteVector().begin(), ToByteVector().end());
}

CPublicKey CSecretKey::GetPubKey() const
{
CPublicKey vchPubKey;
vchPubKey.SetByteVector(ToByteVector());
return vchPubKey;
}
70 changes: 63 additions & 7 deletions src/bls/bls.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <uint256.h>
#include <util/strencodings.h>

#include <support/allocators/secure.h>

// bls-dash uses relic, which may define DEBUG and ERROR, which leads to many warnings in some build setups
#undef ERROR
#undef DEBUG
Expand All @@ -32,6 +34,8 @@ static const bool fLegacyDefault{true};
#define BLS_CURVE_PUBKEY_SIZE 48
#define BLS_CURVE_SIG_SIZE 96

class CPublicKey;
class CSecretKey;
class CBLSSignature;
class CBLSPublicKey;

Expand All @@ -42,10 +46,9 @@ class CBLSWrapper
friend class CBLSPublicKey;
friend class CBLSSignature;

bool fLegacy;

protected:
ImplType impl;
bool fLegacy = fLegacyDefault;
bool fValid{false};
mutable uint256 cachedHash;

Expand Down Expand Up @@ -152,6 +155,12 @@ class CBLSWrapper
return IsValid();
}

//! CKey-like read only interface to access byte vector
unsigned int size() const { return (new std::vector<uint8_t>(ToByteVector()))->size(); }
const unsigned char* begin() const { return (new std::vector<uint8_t>(ToByteVector()))->data(); }
const unsigned char* end() const { return (new std::vector<uint8_t>(ToByteVector()))->data() + size(); }
const unsigned char& operator[](unsigned int pos) const { return (new std::vector<uint8_t>(ToByteVector()))->data()[pos]; }

public:
inline void Serialize(CSizeComputer& s) const
{
Expand Down Expand Up @@ -193,12 +202,13 @@ class CBLSWrapper
}
};

struct CBLSIdImplicit : public uint256
template <typename T1>
struct CBLSIdImplicit : public T1
{
CBLSIdImplicit() = default;
CBLSIdImplicit(const uint256& id)
CBLSIdImplicit(const T1& id)
{
memcpy(begin(), id.begin(), sizeof(uint256));
memcpy(this->begin(), id.begin(), sizeof(T1));
}
static CBLSIdImplicit FromBytes(const uint8_t* buffer, const bool fLegacy = false)
{
Expand All @@ -208,11 +218,11 @@ struct CBLSIdImplicit : public uint256
}
std::vector<uint8_t> Serialize(const bool fLegacy = false) const
{
return {begin(), end()};
return {this->begin(), this->end()};
}
};

class CBLSId : public CBLSWrapper<CBLSIdImplicit, BLS_CURVE_ID_SIZE, CBLSId>
class CBLSId : public CBLSWrapper<CBLSIdImplicit<uint256>, BLS_CURVE_ID_SIZE, CBLSId>
{
public:
using CBLSWrapper::operator=;
Expand All @@ -224,6 +234,20 @@ class CBLSId : public CBLSWrapper<CBLSIdImplicit, BLS_CURVE_ID_SIZE, CBLSId>
explicit CBLSId(const uint256& nHash);
};

class CBLSKeyID : public CBLSWrapper<CBLSIdImplicit<uint160>, BLS_CURVE_ID_SIZE, CBLSKeyID>
{
public:
using CBLSWrapper::operator=;
using CBLSWrapper::operator==;
using CBLSWrapper::operator!=;
using CBLSWrapper::CBLSWrapper;

CBLSKeyID() = default;
explicit CBLSKeyID(const uint160& nHash);

friend inline bool operator<(const CBLSKeyID& a, const CBLSKeyID& b) { return a.impl.Compare(b.impl) < 0; }
};

class CBLSSecretKey : public CBLSWrapper<bls::PrivateKey, BLS_CURVE_SECKEY_SIZE, CBLSSecretKey>
{
public:
Expand All @@ -248,6 +272,25 @@ class CBLSSecretKey : public CBLSWrapper<bls::PrivateKey, BLS_CURVE_SECKEY_SIZE,
CBLSSignature Sign(const uint256& hash) const;
};

class CSecretKey : public CBLSSecretKey
{
protected:
bool fLegacy = false;
public:
using CBLSSecretKey::Sign;

CSecretKey() = default;
CSecretKey(const CSecretKey&) = default;
CSecretKey& operator=(const CSecretKey&) = default;

std::vector<unsigned char, secure_allocator<unsigned char> > GetPrivKey() const;
CPublicKey GetPubKey() const;

bool Load(const std::vector<unsigned char, secure_allocator<unsigned char> >& privkey, const CPublicKey& vchPubKey, bool fSkipCheck);
bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig) const;
bool VerifyPubKey(const CPublicKey& vchPubKey) const;
};

class CBLSPublicKey : public CBLSWrapper<bls::G1Element, BLS_CURVE_PUBKEY_SIZE, CBLSPublicKey>
{
friend class CBLSSecretKey;
Expand All @@ -269,6 +312,19 @@ class CBLSPublicKey : public CBLSWrapper<bls::G1Element, BLS_CURVE_PUBKEY_SIZE,

};

class CPublicKey : public CBLSPublicKey
{
protected:
bool fLegacy = false;
public:
CPublicKey() = default;

CBLSKeyID GetID() const;
uint256 GetHash() const;

bool Verify(const uint256& hash, const std::vector<unsigned char>& vchSig) const;
};

class CBLSSignature : public CBLSWrapper<bls::G2Element, BLS_CURVE_SIG_SIZE, CBLSSignature>
{
friend class CBLSSecretKey;
Expand Down
8 changes: 8 additions & 0 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,8 @@ class CMainParams : public CChainParams {
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,16);
// Dash private keys start with '7' or 'X'
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,204);
// Dash BLS addresses start with 'D'
base58Prefixes[PUBKEY_ADDRESS_BLS] = std::vector<unsigned char>(1,30);
// Dash BIP32 pubkeys start with 'xpub' (Bitcoin defaults)
base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x88, 0xB2, 0x1E};
// Dash BIP32 prvkeys start with 'xprv' (Bitcoin defaults)
Expand Down Expand Up @@ -645,6 +647,8 @@ class CTestNetParams : public CChainParams {
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,19);
// Testnet private keys start with '9' or 'c' (Bitcoin defaults)
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,239);
// Testnet Dash BLS addresses start with '5'
base58Prefixes[PUBKEY_ADDRESS_BLS] = std::vector<unsigned char>(1,10);
// Testnet Dash BIP32 pubkeys start with 'tpub' (Bitcoin defaults)
base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x35, 0x87, 0xCF};
// Testnet Dash BIP32 prvkeys start with 'tprv' (Bitcoin defaults)
Expand Down Expand Up @@ -827,6 +831,8 @@ class CDevNetParams : public CChainParams {
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,19);
// Testnet private keys start with '9' or 'c' (Bitcoin defaults)
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,239);
// Testnet Dash BLS addresses start with '5'
base58Prefixes[PUBKEY_ADDRESS_BLS] = std::vector<unsigned char>(1,10);
// Testnet Dash BIP32 pubkeys start with 'tpub' (Bitcoin defaults)
base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x35, 0x87, 0xCF};
// Testnet Dash BIP32 prvkeys start with 'tprv' (Bitcoin defaults)
Expand Down Expand Up @@ -1007,6 +1013,8 @@ class CRegTestParams : public CChainParams {
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,19);
// Regtest private keys start with '9' or 'c' (Bitcoin defaults)
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,239);
// Regtest Dash BLS addresses start with '5'
base58Prefixes[PUBKEY_ADDRESS_BLS] = std::vector<unsigned char>(1,10);
// Regtest Dash BIP32 pubkeys start with 'tpub' (Bitcoin defaults)
base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x35, 0x87, 0xCF};
// Regtest Dash BIP32 prvkeys start with 'tprv' (Bitcoin defaults)
Expand Down
1 change: 1 addition & 0 deletions src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class CChainParams
SECRET_KEY, // BIP16
EXT_PUBLIC_KEY, // BIP32
EXT_SECRET_KEY, // BIP32
PUBKEY_ADDRESS_BLS,

MAX_BASE58_TYPES
};
Expand Down
2 changes: 1 addition & 1 deletion src/coinjoin/coinjoin-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,7 @@ bool CCoinJoinClientSession::CreateCollateralTransaction(CMutableTransaction& tx
// make our change address
CScript scriptChange;
CPubKey vchPubKey;
CReserveKey reservekey(&mixingWallet);
CReserveKey<CPubKey> reservekey(&mixingWallet);
bool success = reservekey.GetReservedKey(vchPubKey, true);
assert(success); // should never fail, as we just unlocked
scriptChange = GetScriptForDestination(vchPubKey.GetID());
Expand Down
6 changes: 3 additions & 3 deletions src/coinjoin/coinjoin-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CTransactionBuilder;
class CKeyHolder
{
private:
CReserveKey reserveKey;
CReserveKey<CPubKey> reserveKey;
CPubKey pubKey;

public:
Expand Down Expand Up @@ -46,7 +46,7 @@ class CTransactionBuilderOutput
/// Used for amount updates
CTransactionBuilder* pTxBuilder{nullptr};
/// Reserve key where the amount of this output will end up
CReserveKey key;
CReserveKey<CPubKey> key;
/// Amount this output will receive
CAmount nAmount{0};
/// ScriptPubKey of this output
Expand Down Expand Up @@ -82,7 +82,7 @@ class CTransactionBuilder
/// Dummy since we anyway use tallyItem's destination as change destination in coincontrol.
/// Its a member just to make sure ReturnKey can be called in destructor just in case it gets generated/kept
/// somewhere in CWallet code.
CReserveKey dummyReserveKey;
CReserveKey<CPubKey> dummyReserveKey;
/// Contains all utxos available to generate this transactions. They are all from the same address.
CompactTallyItem tallyItem;
/// Contains the number of bytes required for a transaction with only the inputs of tallyItems, no outputs
Expand Down
1 change: 1 addition & 0 deletions src/compressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <compressor.h>

#include <bls/bls.h>
#include <hash.h>
#include <pubkey.h>
#include <script/standard.h>
Expand Down
1 change: 1 addition & 0 deletions src/evo/simplifiedmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <evo/simplifiedmns.h>
#include <evo/specialtx.h>

#include <bls/bls.h>
#include <pubkey.h>
#include <serialize.h>
#include <version.h>
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class PendingWalletTxImpl : public PendingWalletTx

CTransactionRef m_tx;
CWallet& m_wallet;
CReserveKey m_key;
CReserveKey<CPubKey> m_key;
};

//! Construct wallet tx struct.
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <amount.h> // For CAmount
#include <fs.h> // For fs::path
#include <pubkey.h> // For CKeyID and CScriptID (definitions needed in CTxDestination instantiation)
#include <bls/bls.h> // For CBLSKeyID (definition needed in CTxDestination instantiation)
#include <script/ismine.h> // For isminefilter, isminetype
#include <script/standard.h> // For CTxDestination
#include <support/allocators/secure.h> // For SecureString
Expand Down
7 changes: 7 additions & 0 deletions src/key_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class DestinationEncoder : public boost::static_visitor<std::string>
return EncodeBase58Check(data);
}

std::string operator()(const CBLSKeyID& id) const
{
std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::PUBKEY_ADDRESS_BLS);
data.insert(data.end(), id.begin(), id.end());
return EncodeBase58Check(data);
}

std::string operator()(const CScriptID& id) const
{
std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
Expand Down
1 change: 1 addition & 0 deletions src/key_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef BITCOIN_KEY_IO_H
#define BITCOIN_KEY_IO_H

#include <bls/bls.h>
#include <chainparams.h>
#include <key.h>
#include <pubkey.h>
Expand Down
2 changes: 1 addition & 1 deletion src/keystore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
pubKeyOut = CPubKey(vch);
if (!pubKeyOut.IsFullyValid())
return false;
if (!dest.GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG || dest.GetOp(pc, opcode, vch))
if (!dest.GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG || opcode != OP_BLS_CHECKSIG || dest.GetOp(pc, opcode, vch))
return false;
return true;
}
Expand Down
1 change: 1 addition & 0 deletions src/keystore.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef BITCOIN_KEYSTORE_H
#define BITCOIN_KEYSTORE_H

#include <bls/bls.h>
#include <hdchain.h>
#include <key.h>
#include <pubkey.h>
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ static UniValue gobject_prepare(const JSONRPCRequest& request)
}

// -- make our change address
CReserveKey reservekey(pwallet);
CReserveKey<CPubKey> reservekey(pwallet);
// -- send the tx to the network
CValidationState state;
if (!pwallet->CommitTransaction(tx, {}, {}, {}, reservekey, g_connman.get(), state)) {
Expand Down
Loading