From d2dfdb999528d57d9b14f88b7c68d11ceb980e45 Mon Sep 17 00:00:00 2001 From: Satchmo Date: Tue, 10 Sep 2024 09:28:08 -0400 Subject: [PATCH] add tests for signunsigned --- transaction/transaction_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/transaction/transaction_test.go b/transaction/transaction_test.go index 90c2c81..c7debac 100644 --- a/transaction/transaction_test.go +++ b/transaction/transaction_test.go @@ -101,3 +101,30 @@ func TestUncomputedFee(t *testing.T) { err := tx.Sign() require.Error(t, err) } + +func TestSignUnsigned(t *testing.T) { + tx, err := transaction.NewTransactionFromBEEFHex(BRC62Hex) + require.NoError(t, err) + + cloneTx := tx.ShallowClone() + pk, _ := ec.NewPrivateKey() + + // Adding a script template with random key so sigs will be different + for i := range tx.Inputs { + cloneTx.Inputs[i].UnlockingScriptTemplate, err = p2pkh.Unlock(pk, nil) + require.NoError(t, err) + } + + // This should do nothing because the inputs from hex are already signed + err = cloneTx.SignUnsigned() + require.NoError(t, err) + for i := range cloneTx.Inputs { + require.Equal(t, tx.Inputs[i].UnlockingScript, cloneTx.Inputs[i].UnlockingScript) + } + + // This should sign the inputs with the incorrect key which should change the sigs + cloneTx.Sign() + for i := range tx.Inputs { + require.NotEqual(t, tx.Inputs[i].UnlockingScript, cloneTx.Inputs[i].UnlockingScript) + } +}