package transactions_test

import (
	"crypto/ecdsa"
	"crypto/elliptic"
	"crypto/rand"
	"dejo_node/internal/transactions"
	"testing"
)

func TestTransaction_IsValid(t *testing.T) {
	priv, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
	pub := &priv.PublicKey

	tx := transactions.Transaction{
		From:  "0xabc",
		To:    "0xdef",
		Value: 50,
		Gas:   10,
		Nonce: 1,
	}
	tx.Signature = tx.GenerateSignature(priv)

	err := tx.IsValid(pub, 100, 1)
	if err != nil {
		t.Errorf("esperava transação válida, mas falhou: %v", err)
	}
}

func TestTransaction_IsValid_InvalidSignature(t *testing.T) {
	priv, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
	wrongPriv, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
	pub := &priv.PublicKey

	tx := transactions.Transaction{
		From:  "0xabc",
		To:    "0xdef",
		Value: 50,
		Gas:   10,
		Nonce: 1,
	}
	tx.Signature = tx.GenerateSignature(wrongPriv)

	err := tx.IsValid(pub, 100, 1)
	if err == nil {
		t.Error("esperava falha na assinatura, mas foi aceita")
	}
}