package tx import ( "bytes" "encoding/hex" "encoding/json" "os" "testing" "github.com/btcsuite/btcd/btcec/v2" btcecdsa "github.com/btcsuite/btcd/btcec/v2/ecdsa" ) // The node's own vectors live in the Knots checkout. The test skips when the // checkout is absent; when it is there, the digest must match byte for byte. const vectorPath = "/home/mleku/s/bitcoinknots/bitcoin/src/test/data/unified_sighash.json" type vector struct { ScriptCode string `json:"scriptCode"` RawTx string `json:"rawTx"` InIdx int `json:"inIdx"` HashType uint32 `json:"hashType"` ScriptType uint32 `json:"scriptType"` SpentOutputs [][2]any `json:"spentOutputs"` Sighash string `json:"sighash"` } func TestSighashUnifiedMatchesNodeVector(t *testing.T) { raw, err := os.ReadFile(vectorPath) if err != nil { t.Skipf("node vectors not present: %v", err) } var vectors []json.RawMessage if err := json.Unmarshal(raw, &vectors); err != nil { t.Fatal(err) } var checked int for i, item := range vectors { var row []any if err := json.Unmarshal(item, &row); err != nil || len(row) != 7 { continue } hashType, ok := row[3].(float64) if !ok || uint32(hashType) != 33 && uint32(hashType) != 161 { continue } scriptType, ok := row[4].(float64) if !ok || uint32(scriptType) != 1 { continue } scriptCode, err := hex.DecodeString(row[0].(string)) if err != nil { t.Fatalf("vector %d scriptCode: %v", i, err) } rawTx, err := hex.DecodeString(row[1].(string)) if err != nil { t.Fatalf("vector %d rawTx: %v", i, err) } transaction, err := Decode(rawTx) if err != nil { t.Fatalf("vector %d decode: %v", i, err) } spentRaw := row[5].([]any) spent := make([]SpentOutput, 0, len(spentRaw)) for _, entry := range spentRaw { pair := entry.([]any) value := uint64(pair[0].(float64)) script, err := hex.DecodeString(pair[1].(string)) if err != nil { t.Fatalf("vector %d spent script: %v", i, err) } spent = append(spent, SpentOutput{Value: value, Script: script}) } digest, err := transaction.SighashUnifiedV0( int(row[2].(float64)), scriptCode, spent, uint32(hashType), ) if err != nil { t.Fatalf("vector %d: %v", i, err) } want, _ := hex.DecodeString(row[6].(string)) if hex.EncodeToString(digest[:]) != hex.EncodeToString(want) { t.Fatalf("vector %d: got %x want %x", i, digest, want) } checked++ } if checked == 0 { t.Skip("no segwit v0 ALL|UNIFIED vectors found") } } func TestSighashUnifiedCommitsToEverySpentOutput(t *testing.T) { transaction := New() transaction.AddInput(Outpoint{Hash: [32]byte{1}, Index: 0}, SequenceFinal) transaction.AddInput(Outpoint{Hash: [32]byte{2}, Index: 1}, SequenceFinal) transaction.AddOutput(1000, []byte{0x51}) spent := []SpentOutput{ {Value: 2000, Script: []byte{0x51}}, {Value: 3000, Script: []byte{0x52}}, } first, err := transaction.SighashUnifiedV0(0, []byte{0x51}, spent, SighashAllUnified) if err != nil { t.Fatal(err) } changed := []SpentOutput{ {Value: 2000, Script: []byte{0x51}}, {Value: 3001, Script: []byte{0x52}}, } second, err := transaction.SighashUnifiedV0(0, []byte{0x51}, changed, SighashAllUnified) if err != nil { t.Fatal(err) } if first == second { t.Fatal("the digest did not commit to the other input's value") } if _, err := transaction.SighashUnifiedV0(0, []byte{0x51}, spent[:1], SighashAllUnified); err == nil { t.Fatal("a digest accepted a missing spent output") } } func TestSignP2WPKHUnifiedUsesTheUnifiedDigest(t *testing.T) { secret := bytes.Repeat([]byte{0x11}, 32) program := [20]byte{1, 2, 3} script := append([]byte{0x00, 0x14}, program[:]...) transaction := New() transaction.AddInput(Outpoint{Hash: [32]byte{1}, Index: 0}, SequenceFinal) transaction.AddInput(Outpoint{Hash: [32]byte{2}, Index: 1}, SequenceFinal) transaction.AddOutput(40_000, []byte{0x51}) spent := []SpentOutput{ {Value: 50_000, Script: script}, {Value: 60_000, Script: []byte{0x51}}, } if err := SignP2WPKHUnified(transaction, 0, secret, program, spent); err != nil { t.Fatal(err) } witness := transaction.Inputs[0].Witness if len(witness) != 2 { t.Fatalf("witness has %d items", len(witness)) } der, hashType := witness[0][:len(witness[0])-1], witness[0][len(witness[0])-1] if hashType != byte(SighashAllUnified) { t.Fatalf("hash type %#x, want %#x", hashType, byte(SighashAllUnified)) } digest, err := transaction.SighashUnifiedV0(0, P2WPKHScriptCode(program), spent, SighashAllUnified) if err != nil { t.Fatal(err) } signature, err := btcecdsa.ParseDERSignature(der) if err != nil { t.Fatal(err) } pubkey, err := btcec.ParsePubKey(witness[1]) if err != nil { t.Fatal(err) } if !signature.Verify(digest[:], pubkey) { t.Fatal("the unified signature does not verify over the unified digest") } // A BIP143 digest over the same input must not verify: the two messages are // different, and that is the replay separation. legacy, err := transaction.SighashV0(0, P2WPKHScriptCode(program), spent[0].Value, SighashAll) if err != nil { t.Fatal(err) } if signature.Verify(legacy[:], pubkey) { t.Fatal("the unified signature verified over the legacy digest") } } func TestSignHTLCRefundUnified(t *testing.T) { secret := bytes.Repeat([]byte{0x22}, 32) witnessScript := []byte{0x51, 0x52} transaction := New() transaction.AddInput(Outpoint{Hash: [32]byte{3}, Index: 0}, 0xfffffffe) transaction.AddOutput(40_000, []byte{0x51}) spent := []SpentOutput{{Value: 50_000, Script: append([]byte{0x00, 0x20}, bytes.Repeat([]byte{0x33}, 32)...)}} if err := SignHTLCRefundUnified(transaction, 0, secret, witnessScript, spent); err != nil { t.Fatal(err) } witness := transaction.Inputs[0].Witness if len(witness) != 3 || len(witness[0]) == 0 || witness[0][len(witness[0])-1] != byte(SighashAllUnified) { t.Fatalf("unified refund witness = %d items, type %#x", len(witness), witness[0][len(witness[0])-1]) } } func TestSignHTLCClaimUnified(t *testing.T) { secret := bytes.Repeat([]byte{0x33}, 32) witnessScript := []byte{0x51, 0x52} preimage := bytes.Repeat([]byte{0x44}, 32) transaction := New() transaction.AddInput(Outpoint{Hash: [32]byte{6}, Index: 0}, 0xfffffffe) transaction.AddOutput(40_000, []byte{0x51}) spent := []SpentOutput{{Value: 50_000, Script: append([]byte{0x00, 0x20}, bytes.Repeat([]byte{0x55}, 32)...)}} if err := SignHTLCClaimUnified(transaction, 0, secret, witnessScript, preimage, spent); err != nil { t.Fatal(err) } witness := transaction.Inputs[0].Witness if len(witness) != 4 || !bytes.Equal(witness[0], preimage) || witness[1][len(witness[1])-1] != byte(SighashAllUnified) { t.Fatalf("unified claim witness = %d items", len(witness)) } }