Skip to content

Commit 985c795

Browse files
committed
Improve witness destination types and use them more
1 parent cbe1974 commit 985c795

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/script/standard.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -348,19 +348,14 @@ CScript GetScriptForWitness(const CScript& redeemscript)
348348
std::vector<std::vector<unsigned char> > vSolutions;
349349
if (Solver(redeemscript, typ, vSolutions)) {
350350
if (typ == TX_PUBKEY) {
351-
unsigned char h160[20];
352-
CHash160().Write(&vSolutions[0][0], vSolutions[0].size()).Finalize(h160);
353-
ret << OP_0 << std::vector<unsigned char>(&h160[0], &h160[20]);
354-
return ret;
351+
return GetScriptForDestination(WitnessV0KeyHash(Hash160(vSolutions[0].begin(), vSolutions[0].end())));
355352
} else if (typ == TX_PUBKEYHASH) {
356-
ret << OP_0 << vSolutions[0];
357-
return ret;
353+
return GetScriptForDestination(WitnessV0KeyHash(vSolutions[0]));
358354
}
359355
}
360356
uint256 hash;
361357
CSHA256().Write(&redeemscript[0], redeemscript.size()).Finalize(hash.begin());
362-
ret << OP_0 << ToByteVector(hash);
363-
return ret;
358+
return GetScriptForDestination(WitnessV0ScriptHash(hash));
364359
}
365360

366361
bool IsValidDestination(const CTxDestination& dest) {

src/script/standard.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,19 @@ class CNoDestination {
7373
friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
7474
};
7575

76-
struct WitnessV0ScriptHash : public uint256 {};
77-
struct WitnessV0KeyHash : public uint160 {};
76+
struct WitnessV0ScriptHash : public uint256
77+
{
78+
WitnessV0ScriptHash() : uint256() {}
79+
explicit WitnessV0ScriptHash(const uint256& hash) : uint256(hash) {}
80+
using uint256::uint256;
81+
};
82+
83+
struct WitnessV0KeyHash : public uint160
84+
{
85+
WitnessV0KeyHash() : uint160() {}
86+
explicit WitnessV0KeyHash(const uint160& hash) : uint160(hash) {}
87+
using uint160::uint160;
88+
};
7889

7990
//! CTxDestination subtype to encode any future Witness version
8091
struct WitnessUnknown
@@ -164,6 +175,9 @@ CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys);
164175
* Generate a pay-to-witness script for the given redeem script. If the redeem
165176
* script is P2PK or P2PKH, this returns a P2WPKH script, otherwise it returns a
166177
* P2WSH script.
178+
*
179+
* TODO: replace calls to GetScriptForWitness with GetScriptForDestination using
180+
* the various witness-specific CTxDestination subtypes.
167181
*/
168182
CScript GetScriptForWitness(const CScript& redeemscript);
169183

0 commit comments

Comments
 (0)