Skip to content

Commit

Permalink
Improve witness destination types and use them more
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed Jan 3, 2018
1 parent cbe1974 commit 985c795
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
11 changes: 3 additions & 8 deletions src/script/standard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,19 +348,14 @@ CScript GetScriptForWitness(const CScript& redeemscript)
std::vector<std::vector<unsigned char> > vSolutions;
if (Solver(redeemscript, typ, vSolutions)) {
if (typ == TX_PUBKEY) {
unsigned char h160[20];
CHash160().Write(&vSolutions[0][0], vSolutions[0].size()).Finalize(h160);
ret << OP_0 << std::vector<unsigned char>(&h160[0], &h160[20]);
return ret;
return GetScriptForDestination(WitnessV0KeyHash(Hash160(vSolutions[0].begin(), vSolutions[0].end())));
} else if (typ == TX_PUBKEYHASH) {
ret << OP_0 << vSolutions[0];
return ret;
return GetScriptForDestination(WitnessV0KeyHash(vSolutions[0]));
}
}
uint256 hash;
CSHA256().Write(&redeemscript[0], redeemscript.size()).Finalize(hash.begin());
ret << OP_0 << ToByteVector(hash);
return ret;
return GetScriptForDestination(WitnessV0ScriptHash(hash));
}

bool IsValidDestination(const CTxDestination& dest) {
Expand Down
18 changes: 16 additions & 2 deletions src/script/standard.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,19 @@ class CNoDestination {
friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
};

struct WitnessV0ScriptHash : public uint256 {};
struct WitnessV0KeyHash : public uint160 {};
struct WitnessV0ScriptHash : public uint256
{
WitnessV0ScriptHash() : uint256() {}
explicit WitnessV0ScriptHash(const uint256& hash) : uint256(hash) {}
using uint256::uint256;
};

struct WitnessV0KeyHash : public uint160
{
WitnessV0KeyHash() : uint160() {}
explicit WitnessV0KeyHash(const uint160& hash) : uint160(hash) {}
using uint160::uint160;
};

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

Expand Down

0 comments on commit 985c795

Please sign in to comment.