Skip to content

Instantly share code, notes, and snippets.

@jl2012
Last active November 29, 2018 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jl2012/a06f6921c149f359b0c8a8fce37e6280 to your computer and use it in GitHub Desktop.
Save jl2012/a06f6921c149f359b0c8a8fce37e6280 to your computer and use it in GitHub Desktop.
const int inputtype = nHashType & SIGHASH2_INPUT_MASK;
const int outputtype = nHashType & SIGHASH2_OUTPUT_MASK;
CHashWriter ss();
ss << static_cast<uint8_t>(nHashType);
// tx-level data
ss << txTo.nVersion;
if (inputtype == SIGHASH2_ALL) {
ss << single_sha256_prevouts;
ss << single_sha256_amounts;
ss << single_sha256_sequence;
}
// else { do nothing }
if (outputtype == SIGHASH2_ALLOUTPUT) {
ss << single_sha256_outputs;
}
else if (outputtype == SIGHASH2_SINGLEOUTPUT) {
assert(nIn < txTo.vout.size());
ss << txTo.vout[nIn];
}
// else if (outputtype == SIGHASH2_NOOUTPUT) { do nothing }
else if (outputtype != SIGHASH2_NOOUTPUT) {
assert(!"invalid SIGHASH2");
}
ss << txTo.nLockTime;
// input-level data
if (inputtype == SIGHASH2_ALL) {
ss << static_cast<uint16_t>(nIn);
}
else {
if (inputtype == SIGHASH2_SINGLEINPUT) {
ss << txTo.vin[nIn].prevout;
}
ss << amount;
ss << txTo.vin[nIn].nSequence;
}
if (inputtype == SIGHASH2_NOINPUT_MASKEDSCRIPT) {
ss << static_cast<uint8_t>(scriptPubKey.IsPayToScriptHash());
}
else {
ss << scriptPubKey;
}
// script-level data
if (is_direct_spending) {
assert(inputtype != SIGHASH2_NOINPUT_MASKEDSCRIPT);
}
else {
if (inputtype == SIGHASH2_NOINPUT_MASKEDSCRIPT) {
ss << single_sha256_masked_scriptcode;
}
else {
ss << single_sha256_scriptcode;
}
ss << static_cast<uint8_t>(codeseparator_position);
}
return ss.GetSingleSHA256();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment