Skip to content

Commit

Permalink
ARCO-53: add test for SkipScriptValidation
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 committed Aug 30, 2024
1 parent 53b352a commit 46181c5
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
64 changes: 63 additions & 1 deletion test/submit_single_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

sdkTx "github.com/bitcoin-sv/go-sdk/transaction"

"github.com/libsv/go-bc"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -602,3 +601,66 @@ func TestPostCumulativeFeesValidation(t *testing.T) {
})
}
}

func TestScriptValidation(t *testing.T) {
tt := []struct {
name string
skipScriptValidation bool
skipTxValidation bool

expectedStatusCode int
}{
{
name: "post transaction with invalid script with validation",
skipScriptValidation: false,
skipTxValidation: false,

expectedStatusCode: 461, // ErrStatusUnlockingScripts
},
{
name: "post transaction with invalid script without script validation",
skipScriptValidation: true,
skipTxValidation: false,

expectedStatusCode: http.StatusOK,
},
{
name: "post transaction with invalid script without tx validation",
skipScriptValidation: false,
skipTxValidation: true,

expectedStatusCode: http.StatusOK,
},
}

for _, tc := range tt {
address, privateKey := fundNewWallet(t)

utxos := getUtxos(t, address)
require.True(t, len(utxos) > 0, "No UTXOs available for the address")

fee := uint64(10)

lowFeeTx, err := createTx(privateKey, address, utxos[0], fee)
require.NoError(t, err)

sc, err := generateNewUnlockingScriptFromRandomKey()
require.NoError(t, err)

lowFeeTx.Outputs[0].LockingScript = sc

lowFeeRawTx, err := lowFeeTx.EFHex()
require.NoError(t, err)

resp := postRequest[TransactionResponse](t, arcEndpointV1Tx, createPayload(t, TransactionRequest{RawTx: lowFeeRawTx}),
map[string]string{
"X-SkipScriptValidation": strconv.FormatBool(tc.skipScriptValidation),
"X-SkipTxValidation": strconv.FormatBool(tc.skipTxValidation),
}, tc.expectedStatusCode)

fmt.Println(resp)
fmt.Println(resp.Status)

require.Equal(t, resp.Status, tc.expectedStatusCode)
}
}
25 changes: 22 additions & 3 deletions test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import (
"context"
"encoding/json"
"fmt"
ec "github.com/bitcoin-sv/go-sdk/primitives/ec"
sdkTx "github.com/bitcoin-sv/go-sdk/transaction"
"github.com/bitcoin-sv/go-sdk/transaction/template/p2pkh"
"io"
"math/rand"
"net/http"
Expand All @@ -16,6 +13,10 @@ import (
"testing"
"time"

ec "github.com/bitcoin-sv/go-sdk/primitives/ec"
"github.com/bitcoin-sv/go-sdk/script"
sdkTx "github.com/bitcoin-sv/go-sdk/transaction"
"github.com/bitcoin-sv/go-sdk/transaction/template/p2pkh"
"github.com/bitcoinsv/bsvutil"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -484,3 +485,21 @@ func prepareCallback(t *testing.T, callbackNumbers int) (chan *TransactionRespon
}
return callbackReceivedChan, callbackErrChan, calbackResponseFn
}

func generateNewUnlockingScriptFromRandomKey() (*script.Script, error) {
privKey, err := ec.NewPrivateKey()
if err != nil {
return nil, err
}

address, err := script.NewAddressFromPublicKey(privKey.PubKey(), false)
if err != nil {
return nil, err
}

sc, err := p2pkh.Lock(address)
if err != nil {
return nil, err
}
return sc, nil
}

0 comments on commit 46181c5

Please sign in to comment.