For SIGHASH_SINGLE do the outputs other than at the input index, have 8 bytes or 9 bytes including a VarInt?

3

The bitcoin wiki says:

All other txCopy outputs aside from the output that is the same as the current input index are set to a blank script and a value of (long) -1.

https://en.bitcoin.it/wiki/OP_CHECKSIG#Hashtype_SIGHASH_SINGLE

It's hard finding what this means in the C++ client. Does it mean that the outputs, other than the output with the same index as the input being signed for, only have 8 bytes for the value or does it mean they also have another byte for the VarInt set to zero; 9 bytes in total?

Thanks.

Matthew Mitchell

Posted 2012-06-04T13:05:04.087

Reputation: 253

Answers

1

Transaction outputs have a "value" field that's an 8-byte signed integer (always 8 bytes) indicating how many satoshis (0.00000001 BTC each) are available for that output. That is the thing that gets set to -1 on the other outputs in SIGHASH_SINGLE mode.

This looks to be the code responsible for making this happen, from script.cpp:

/** Serialize an output of txTo */
template<typename S>
void SerializeOutput(S &s, unsigned int nOutput, int nType, int nVersion) const {
    if (fHashSingle && nOutput != nIn)
        // Do not lock-in the txout payee at other indices as txin
        ::Serialize(s, CTxOut(), nType, nVersion);
    else
        ::Serialize(s, txTo.vout[nOutput], nType, nVersion);
}

If we're in SIGHASH_SINGLE mode, and the index of the output is different from the index of the input, then the thing that we sign in this spot is just a dummy CTxOut object, whose parameterless constructor (in core.h) calls SetNull(), which sets the field called "nValue" to -1 (-0.00000001 BTC) and clears the scriptPubKey, which ensures that it's a 0-byte vector.

When CTxOut gets serialized, it writes the nValue and scriptPubKey per this (core.h again):

IMPLEMENT_SERIALIZE
(
    READWRITE(nValue);
    READWRITE(scriptPubKey);
)

nValue serializes as the 8-byte twos-complement representation of -1, or 0xFFFFFFFFFFFFFFFF. scriptPubKey serializes as a 1-byte CompactSize that indicates that the script is 0 bytes long, or 0x0.

Joe Amenta

Posted 2012-06-04T13:05:04.087

Reputation: 381

so, the short answer is: 9 bytes. ;>dthorpe 2014-10-28T00:10:54.340