package tx import ( "crypto/sha256" "encoding/binary" "errors" "fmt" ) // SIGHASH_UNIFIED is the XBT fork's opt-in signature hash bit. // // A signature carrying it commits to the unified message rather than BIP143 // or the legacy message. A chain that does not implement the bit computes a // different message for the same byte, so the signature does not verify there. const SighashUnified uint32 = 0x20 // SighashAllUnified is the type the wallet signs on the XBT chain. const SighashAllUnified uint32 = SighashAll | SighashUnified // SpentOutput is one output being spent: its value and its scriptPubKey. // // The unified message commits to every input's value and scriptPubKey, not // just the one being signed. A wallet that only knows the current input cannot // produce this digest; PSBT already carries the records, and the send path has // the coins. type SpentOutput struct { Value uint64 Script []byte } // SighashUnifiedV0 is the XBT fork's unified digest for input index of a // segwit v0 spend. // // It is not BIP143 with extra fields: it is a tagged hash over the BIP341-shaped // message in doc/unified-sighash.md. The aggregates are single SHA-256, the // transaction locktime is committed to as five bytes, and the script type byte // separates bare/P2SH, segwit v0, taproot and tapscript. This implementation // covers segwit v0, which is what this wallet signs. func (t *Tx) SighashUnifiedV0( index int, scriptCode []byte, spent []SpentOutput, sighashType uint32, ) ([32]byte, error) { if sighashType&SighashUnified == 0 { return [32]byte{}, ErrSighashType } outputType := sighashType & 0x1f anyoneCanPay := sighashType&0x80 != 0 // The wallet signs ALL|UNIFIED. ANYONECANPAY is carried because the node's // vectors cover it; NONE and SINGLE are not produced here. if outputType != SighashAll { return [32]byte{}, fmt.Errorf("%w: output type %d", ErrSighashType, outputType) } if sighashType&^(SighashUnified|SighashAll|0x80) != 0 { return [32]byte{}, fmt.Errorf("%w: unsupported bits in %#x", ErrSighashType, sighashType) } if index < 0 || index >= len(t.Inputs) { return [32]byte{}, ErrInputIndex } if len(spent) != len(t.Inputs) { return [32]byte{}, fmt.Errorf( "tx: %d spent outputs for %d inputs; the unified digest commits to every input", len(spent), len(t.Inputs), ) } if len(scriptCode) == 0 { return [32]byte{}, ErrScriptTooLong } var prevouts, amounts, scripts, sequences, outputs [32]byte if !anyoneCanPay { prevouts = sha256.Sum256(serializePrevouts(t.Inputs)) amounts = sha256.Sum256(serializeAmounts(spent)) scripts = sha256.Sum256(serializeScripts(spent)) sequences = sha256.Sum256(serializeSequences(t.Inputs)) } outputs = sha256.Sum256(serializeOutputs(t.Outputs)) message := make([]byte, 0, 256+len(scriptCode)) // Epoch, then the hash type as one byte. Consensus reads the type from the // last byte of the signature, so it is exactly this byte. message = append(message, 0) message = append(message, byte(sighashType)) // Version, then locktime as five bytes: four now and a zero fifth for a // future widened field. message = binary.LittleEndian.AppendUint32(message, t.Version) message = binary.LittleEndian.AppendUint32(message, t.LockTime) message = append(message, 0) if !anyoneCanPay { message = append(message, prevouts[:]...) message = append(message, amounts[:]...) message = append(message, scripts[:]...) message = append(message, sequences[:]...) } message = append(message, outputs[:]...) // Script type 1 is segwit v0. message = append(message, 1) if anyoneCanPay { input := t.Inputs[index] message = append(message, input.Hash[:]...) message = binary.LittleEndian.AppendUint32(message, input.Index) message = binary.LittleEndian.AppendUint64(message, spent[index].Value) message = appendCompactSize(message, len(spent[index].Script)) message = append(message, spent[index].Script...) message = binary.LittleEndian.AppendUint32(message, input.Sequence) } else { message = binary.LittleEndian.AppendUint32(message, uint32(index)) } message = appendCompactSize(message, len(scriptCode)) message = append(message, scriptCode...) return taggedHash("UnifiedSighash", message), nil } func serializePrevouts(inputs []Input) []byte { out := make([]byte, 0, 36*len(inputs)) for _, input := range inputs { out = append(out, input.Hash[:]...) out = binary.LittleEndian.AppendUint32(out, input.Index) } return out } func serializeAmounts(spent []SpentOutput) []byte { out := make([]byte, 0, 8*len(spent)) for _, coin := range spent { out = binary.LittleEndian.AppendUint64(out, coin.Value) } return out } func serializeScripts(spent []SpentOutput) []byte { out := make([]byte, 0, 34*len(spent)) for _, coin := range spent { out = appendCompactSize(out, len(coin.Script)) out = append(out, coin.Script...) } return out } func serializeSequences(inputs []Input) []byte { out := make([]byte, 0, 4*len(inputs)) for _, input := range inputs { out = binary.LittleEndian.AppendUint32(out, input.Sequence) } return out } func serializeOutputs(outputs []Output) []byte { out := make([]byte, 0, 34*len(outputs)) for _, output := range outputs { out = binary.LittleEndian.AppendUint64(out, output.Value) out = appendCompactSize(out, len(output.Script)) out = append(out, output.Script...) } return out } // taggedHash is BIP340's tagged hash. func taggedHash(tag string, message []byte) [32]byte { tagHash := sha256.Sum256([]byte(tag)) h := sha256.New() h.Write(tagHash[:]) h.Write(tagHash[:]) h.Write(message) var out [32]byte copy(out[:], h.Sum(nil)) return out } // ErrUnifiedUnavailable is a chain where the fork is not scheduled, so the node // would refuse an opted-in signature. var ErrUnifiedUnavailable = errors.New("tx: the unified signature hash is not active on this chain")