micw/server/logic/validate.go
2024-11-13 22:10:13 +01:00

32 lines
814 B
Go

package logic
import (
"bytes"
"errors"
"github.com/matchsystems/werr"
"math"
"mic-wallet/common"
"mic-wallet/server/repository/entities"
"time"
)
func ValidateNewTransaction(tx *entities.NewTransactionOpts, txHash []byte) error {
if bytes.Compare(tx.ReceiverPublicKey, tx.SenderPublicKey) == 0 {
return errors.New("cannot send to yourself")
}
valid, err := common.VerifyTransactionSignature(tx.SenderPublicKey, tx.Signature, txHash, tx.CreatedAt)
if err != nil {
return werr.Wrapf(err, "failed to verify signature")
}
if !valid {
return errors.New("invalid signature")
}
now := time.Now().UTC()
// Sent data has to be in UTC timezone
if time.Duration(math.Abs(float64(tx.CreatedAt.Sub(now)))) > time.Minute*1 {
return errors.New("invalid transaction created_at time")
}
return nil
}