You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1008 B

  1. package core
  2. import "crypto/ecdsa"
  3. type Input struct {
  4. TxId Hash
  5. Vout int // index of the output from the TxId
  6. Value uint64
  7. }
  8. type Output struct {
  9. Value uint64
  10. }
  11. // Tx holds the data structure of a transaction
  12. type Tx struct {
  13. From *ecdsa.PublicKey
  14. To *ecdsa.PublicKey
  15. InputCount uint64
  16. Inputs []Input
  17. Outputs []Output
  18. Signature []byte
  19. }
  20. func NewTx(from, to *ecdsa.PublicKey, in []Input, out []Output) *Tx {
  21. tx := &Tx{
  22. From: from,
  23. To: to,
  24. InputCount: uint64(len(in)),
  25. Inputs: in,
  26. Outputs: out,
  27. }
  28. return tx
  29. }
  30. // CheckTx checks if the transaction is consistent
  31. func CheckTx(tx *Tx) bool {
  32. // TODO check that inputs and outputs are not empty
  33. // check that inputs == outputs
  34. totalIn := 0
  35. for _, in := range tx.Inputs {
  36. totalIn = totalIn + int(in.Value)
  37. }
  38. totalOut := 0
  39. for _, out := range tx.Outputs {
  40. totalOut = totalOut + int(out.Value)
  41. }
  42. if totalIn < totalOut {
  43. return false
  44. }
  45. // TODO check signature
  46. return true
  47. }