Skip to content

Commit de5af41

Browse files
committed
Merge #15452: Replace CScriptID and CKeyID in CTxDestination with dedicated types
78e407a GetKeyBirthTimes should return key ids, not destinations (Gregory Sanders) 70946e7 Replace CScriptID and CKeyID in CTxDestination with dedicated types (Gregory Sanders) Pull request description: The current usage seems to be an overloading of meanings. `CScriptID` is used in the wallet as a lookup key, as well as a destination, and `CKeyID` likewise. Instead, have all destinations be dedicated types. New types: `CScriptID`->`ScriptHash` `CKeyID`->`PKHash` ACKs for commit 78e407: ryanofsky: utACK 78e407a. Only changes are removing extra CScriptID()s and fixing the test case. Sjors: utACK 78e407a meshcollider: utACK bitcoin/bitcoin@78e407a Tree-SHA512: 437f59fc3afb83a40540da3351507aef5aed44e3a7f15b01ddad6226854edeee762ff0b0ef336fe3654c4cd99a205cef175211de8b639abe1130c8a6313337b9
2 parents aebe990 + 78e407a commit de5af41

32 files changed

+184
-163
lines changed

src/bench/ccoins_caching.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ SetupDummyInputs(CBasicKeyStore& keystoreRet, CCoinsViewCache& coinsRet)
3939

4040
dummyTransactions[1].vout.resize(2);
4141
dummyTransactions[1].vout[0].nValue = 21 * COIN;
42-
dummyTransactions[1].vout[0].scriptPubKey = GetScriptForDestination(key[2].GetPubKey().GetID());
42+
dummyTransactions[1].vout[0].scriptPubKey = GetScriptForDestination(PKHash(key[2].GetPubKey()));
4343
dummyTransactions[1].vout[1].nValue = 22 * COIN;
44-
dummyTransactions[1].vout[1].scriptPubKey = GetScriptForDestination(key[3].GetPubKey().GetID());
44+
dummyTransactions[1].vout[1].scriptPubKey = GetScriptForDestination(PKHash(key[3].GetPubKey()));
4545
AddCoins(coinsRet, CTransaction(dummyTransactions[1]), 0);
4646

4747
return dummyTransactions;

src/bitcoin-tx.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ static void MutateTxAddOutPubKey(CMutableTransaction& tx, const std::string& str
323323
}
324324
if (bScriptHash) {
325325
// Get the ID for the script, and then construct a P2SH destination for it.
326-
scriptPubKey = GetScriptForDestination(CScriptID(scriptPubKey));
326+
scriptPubKey = GetScriptForDestination(ScriptHash(scriptPubKey));
327327
}
328328

329329
// construct TxOut, append to transaction output list
@@ -397,7 +397,7 @@ static void MutateTxAddOutMultiSig(CMutableTransaction& tx, const std::string& s
397397
"redeemScript exceeds size limit: %d > %d", scriptPubKey.size(), MAX_SCRIPT_ELEMENT_SIZE));
398398
}
399399
// Get the ID for the script, and then construct a P2SH destination for it.
400-
scriptPubKey = GetScriptForDestination(CScriptID(scriptPubKey));
400+
scriptPubKey = GetScriptForDestination(ScriptHash(scriptPubKey));
401401
}
402402

403403
// construct TxOut, append to transaction output list
@@ -469,7 +469,7 @@ static void MutateTxAddOutScript(CMutableTransaction& tx, const std::string& str
469469
throw std::runtime_error(strprintf(
470470
"redeemScript exceeds size limit: %d > %d", scriptPubKey.size(), MAX_SCRIPT_ELEMENT_SIZE));
471471
}
472-
scriptPubKey = GetScriptForDestination(CScriptID(scriptPubKey));
472+
scriptPubKey = GetScriptForDestination(ScriptHash(scriptPubKey));
473473
}
474474

475475
// construct TxOut, append to transaction output list

src/key_io.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ class DestinationEncoder : public boost::static_visitor<std::string>
2626
public:
2727
explicit DestinationEncoder(const CChainParams& params) : m_params(params) {}
2828

29-
std::string operator()(const CKeyID& id) const
29+
std::string operator()(const PKHash& id) const
3030
{
3131
std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
3232
data.insert(data.end(), id.begin(), id.end());
3333
return EncodeBase58Check(data);
3434
}
3535

36-
std::string operator()(const CScriptID& id) const
36+
std::string operator()(const ScriptHash& id) const
3737
{
3838
std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
3939
data.insert(data.end(), id.begin(), id.end());
@@ -81,14 +81,14 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
8181
const std::vector<unsigned char>& pubkey_prefix = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
8282
if (data.size() == hash.size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) {
8383
std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.begin());
84-
return CKeyID(hash);
84+
return PKHash(hash);
8585
}
8686
// Script-hash-addresses have version 5 (or 196 testnet).
8787
// The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
8888
const std::vector<unsigned char>& script_prefix = params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
8989
if (data.size() == hash.size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) {
9090
std::copy(data.begin() + script_prefix.size(), data.end(), hash.begin());
91-
return CScriptID(hash);
91+
return ScriptHash(hash);
9292
}
9393
}
9494
data.clear();

src/keystore.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,17 @@ CKeyID GetKeyForDestination(const CKeyStore& store, const CTxDestination& dest)
178178
{
179179
// Only supports destinations which map to single public keys, i.e. P2PKH,
180180
// P2WPKH, and P2SH-P2WPKH.
181-
if (auto id = boost::get<CKeyID>(&dest)) {
182-
return *id;
181+
if (auto id = boost::get<PKHash>(&dest)) {
182+
return CKeyID(*id);
183183
}
184184
if (auto witness_id = boost::get<WitnessV0KeyHash>(&dest)) {
185185
return CKeyID(*witness_id);
186186
}
187-
if (auto script_id = boost::get<CScriptID>(&dest)) {
187+
if (auto script_hash = boost::get<ScriptHash>(&dest)) {
188188
CScript script;
189+
CScriptID script_id(*script_hash);
189190
CTxDestination inner_dest;
190-
if (store.GetCScript(*script_id, script) && ExtractDestination(script, inner_dest)) {
191+
if (store.GetCScript(script_id, script) && ExtractDestination(script, inner_dest)) {
191192
if (auto inner_witness_id = boost::get<WitnessV0KeyHash>(&inner_dest)) {
192193
return CKeyID(*inner_witness_id);
193194
}

src/outputtype.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ const std::string& FormatOutputType(OutputType type)
4545
CTxDestination GetDestinationForKey(const CPubKey& key, OutputType type)
4646
{
4747
switch (type) {
48-
case OutputType::LEGACY: return key.GetID();
48+
case OutputType::LEGACY: return PKHash(key);
4949
case OutputType::P2SH_SEGWIT:
5050
case OutputType::BECH32: {
51-
if (!key.IsCompressed()) return key.GetID();
52-
CTxDestination witdest = WitnessV0KeyHash(key.GetID());
51+
if (!key.IsCompressed()) return PKHash(key);
52+
CTxDestination witdest = WitnessV0KeyHash(PKHash(key));
5353
CScript witprog = GetScriptForDestination(witdest);
5454
if (type == OutputType::P2SH_SEGWIT) {
55-
return CScriptID(witprog);
55+
return ScriptHash(witprog);
5656
} else {
5757
return witdest;
5858
}
@@ -63,10 +63,10 @@ CTxDestination GetDestinationForKey(const CPubKey& key, OutputType type)
6363

6464
std::vector<CTxDestination> GetAllDestinationsForKey(const CPubKey& key)
6565
{
66-
CKeyID keyid = key.GetID();
66+
PKHash keyid(key);
6767
if (key.IsCompressed()) {
6868
CTxDestination segwit = WitnessV0KeyHash(keyid);
69-
CTxDestination p2sh = CScriptID(GetScriptForDestination(segwit));
69+
CTxDestination p2sh = ScriptHash(GetScriptForDestination(segwit));
7070
return std::vector<CTxDestination>{std::move(keyid), std::move(p2sh), std::move(segwit)};
7171
} else {
7272
return std::vector<CTxDestination>{std::move(keyid)};
@@ -80,19 +80,19 @@ CTxDestination AddAndGetDestinationForScript(CKeyStore& keystore, const CScript&
8080
// Note that scripts over 520 bytes are not yet supported.
8181
switch (type) {
8282
case OutputType::LEGACY:
83-
return CScriptID(script);
83+
return ScriptHash(script);
8484
case OutputType::P2SH_SEGWIT:
8585
case OutputType::BECH32: {
8686
CTxDestination witdest = WitnessV0ScriptHash(script);
8787
CScript witprog = GetScriptForDestination(witdest);
8888
// Check if the resulting program is solvable (i.e. doesn't use an uncompressed key)
89-
if (!IsSolvable(keystore, witprog)) return CScriptID(script);
89+
if (!IsSolvable(keystore, witprog)) return ScriptHash(script);
9090
// Add the redeemscript, so that P2WSH and P2SH-P2WSH outputs are recognized as ours.
9191
keystore.AddCScript(witprog);
9292
if (type == OutputType::BECH32) {
9393
return witdest;
9494
} else {
95-
return CScriptID(witprog);
95+
return ScriptHash(witprog);
9696
}
9797
}
9898
default: assert(false);

src/qt/coincontroldialog.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,8 @@ void CoinControlDialog::updateLabels(WalletModel *model, QDialog* dialog)
471471
else if(ExtractDestination(out.txout.scriptPubKey, address))
472472
{
473473
CPubKey pubkey;
474-
CKeyID *keyid = boost::get<CKeyID>(&address);
475-
if (keyid && model->wallet().getPubKey(*keyid, pubkey))
474+
PKHash *pkhash = boost::get<PKHash>(&address);
475+
if (pkhash && model->wallet().getPubKey(CKeyID(*pkhash), pubkey))
476476
{
477477
nBytesInputs += (pubkey.IsCompressed() ? 148 : 180);
478478
}

src/qt/signverifymessagedialog.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ void SignVerifyMessageDialog::on_signMessageButton_SM_clicked()
120120
ui->statusLabel_SM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
121121
return;
122122
}
123-
const CKeyID* keyID = boost::get<CKeyID>(&destination);
124-
if (!keyID) {
123+
const PKHash* pkhash = boost::get<PKHash>(&destination);
124+
if (!pkhash) {
125125
ui->addressIn_SM->setValid(false);
126126
ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
127127
ui->statusLabel_SM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
@@ -137,7 +137,7 @@ void SignVerifyMessageDialog::on_signMessageButton_SM_clicked()
137137
}
138138

139139
CKey key;
140-
if (!model->wallet().getPrivKey(*keyID, key))
140+
if (!model->wallet().getPrivKey(CKeyID(*pkhash), key))
141141
{
142142
ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
143143
ui->statusLabel_SM->setText(tr("Private key for the entered address is not available."));
@@ -198,7 +198,7 @@ void SignVerifyMessageDialog::on_verifyMessageButton_VM_clicked()
198198
ui->statusLabel_VM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
199199
return;
200200
}
201-
if (!boost::get<CKeyID>(&destination)) {
201+
if (!boost::get<PKHash>(&destination)) {
202202
ui->addressIn_VM->setValid(false);
203203
ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
204204
ui->statusLabel_VM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
@@ -229,7 +229,7 @@ void SignVerifyMessageDialog::on_verifyMessageButton_VM_clicked()
229229
return;
230230
}
231231

232-
if (!(CTxDestination(pubkey.GetID()) == destination)) {
232+
if (!(CTxDestination(PKHash(pubkey)) == destination)) {
233233
ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
234234
ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verification failed.") + QString("</nobr>"));
235235
return;

src/qt/test/wallettests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ void TestGUI()
169169
// Send two transactions, and verify they are added to transaction list.
170170
TransactionTableModel* transactionTableModel = walletModel.getTransactionTableModel();
171171
QCOMPARE(transactionTableModel->rowCount({}), 105);
172-
uint256 txid1 = SendCoins(*wallet.get(), sendCoinsDialog, CKeyID(), 5 * COIN, false /* rbf */);
173-
uint256 txid2 = SendCoins(*wallet.get(), sendCoinsDialog, CKeyID(), 10 * COIN, true /* rbf */);
172+
uint256 txid1 = SendCoins(*wallet.get(), sendCoinsDialog, PKHash(), 5 * COIN, false /* rbf */);
173+
uint256 txid2 = SendCoins(*wallet.get(), sendCoinsDialog, PKHash(), 10 * COIN, true /* rbf */);
174174
QCOMPARE(transactionTableModel->rowCount({}), 107);
175175
QVERIFY(FindTx(*transactionTableModel, txid1).isValid());
176176
QVERIFY(FindTx(*transactionTableModel, txid2).isValid());

src/rpc/misc.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ static UniValue verifymessage(const JSONRPCRequest& request)
307307
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid address");
308308
}
309309

310-
const CKeyID *keyID = boost::get<CKeyID>(&destination);
311-
if (!keyID) {
310+
const PKHash *pkhash = boost::get<PKHash>(&destination);
311+
if (!pkhash) {
312312
throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");
313313
}
314314

@@ -326,7 +326,7 @@ static UniValue verifymessage(const JSONRPCRequest& request)
326326
if (!pubkey.RecoverCompact(ss.GetHash(), vchSig))
327327
return false;
328328

329-
return (pubkey.GetID() == *keyID);
329+
return (pubkey.GetID() == *pkhash);
330330
}
331331

332332
static UniValue signmessagewithprivkey(const JSONRPCRequest& request)

src/rpc/rawtransaction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ static UniValue decodescript(const JSONRPCRequest& request)
572572
if (type.isStr() && type.get_str() != "scripthash") {
573573
// P2SH cannot be wrapped in a P2SH. If this script is already a P2SH,
574574
// don't return the address for a P2SH of the P2SH.
575-
r.pushKV("p2sh", EncodeDestination(CScriptID(script)));
575+
r.pushKV("p2sh", EncodeDestination(ScriptHash(script)));
576576
// P2SH and witness programs cannot be wrapped in P2WSH, if this script
577577
// is a witness program, don't return addresses for a segwit programs.
578578
if (type.get_str() == "pubkey" || type.get_str() == "pubkeyhash" || type.get_str() == "multisig" || type.get_str() == "nonstandard") {
@@ -599,7 +599,7 @@ static UniValue decodescript(const JSONRPCRequest& request)
599599
segwitScr = GetScriptForDestination(WitnessV0ScriptHash(script));
600600
}
601601
ScriptPubKeyToUniv(segwitScr, sr, /* fIncludeHex */ true);
602-
sr.pushKV("p2sh-segwit", EncodeDestination(CScriptID(segwitScr)));
602+
sr.pushKV("p2sh-segwit", EncodeDestination(ScriptHash(segwitScr)));
603603
r.pushKV("segwit", sr);
604604
}
605605
}

0 commit comments

Comments
 (0)