Skip to content

Instantly share code, notes, and snippets.

@jl2012
Last active November 29, 2018 18:46

Revisions

  1. jl2012 revised this gist Nov 29, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion SIGHASH2 pseudo code
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@ 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;

    @@ -58,6 +60,5 @@ else {
    ss << static_cast<uint8_t>(codeseparator_position);
    }

    ss << static_cast<uint8_t>(nHashType);

    return ss.GetSingleSHA256();
  2. jl2012 revised this gist Nov 29, 2018. 1 changed file with 8 additions and 3 deletions.
    11 changes: 8 additions & 3 deletions SIGHASH2 pseudo code
    Original file line number Diff line number Diff line change
    @@ -37,22 +37,27 @@ else {
    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 << static_cast<uint8_t>(scriptPubKey.IsPayToScriptHash());
    ss << single_sha256_masked_scriptcode;
    }
    else {
    ss << scriptPubKey;
    ss << single_sha256_scriptcode;
    }
    ss << static_cast<uint8_t>(codeseparator_position);
    }

    ss << static_cast<uint8_t>(nHashType);

    return ss.GetSingleSHA256();
    return ss.GetSingleSHA256();
  3. jl2012 revised this gist Nov 29, 2018. 1 changed file with 2 additions and 7 deletions.
    9 changes: 2 additions & 7 deletions SIGHASH2 pseudo code
    Original file line number Diff line number Diff line change
    @@ -37,22 +37,17 @@ else {
    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 << static_cast<uint8_t>(scriptPubKey.IsPayToScriptHash());
    ss << single_sha256_masked_scriptcode;
    }
    else {
    ss << scriptPubKey;
    ss << single_sha256_scriptcode;
    }
    ss << static_cast<uint8_t>(codeseparator_position);
  4. jl2012 created this gist Nov 29, 2018.
    63 changes: 63 additions & 0 deletions SIGHASH2 pseudo code
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    const int inputtype = nHashType & SIGHASH2_INPUT_MASK;
    const int outputtype = nHashType & SIGHASH2_OUTPUT_MASK;
    CHashWriter ss();

    // 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);
    }

    ss << static_cast<uint8_t>(nHashType);

    return ss.GetSingleSHA256();