/
verify.go
57 lines (43 loc) · 1.11 KB
/
verify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package structures
import (
"bytes"
"errors"
"fmt"
"github.com/gelembjuk/oursql/lib/utils"
)
// Check if all TXt currency outputs are only to given addresses
func CheckTXOutputsAreOnlyToGivenAddresses(tx *Transaction, PubKeyHashes [][]byte) error {
for _, out := range tx.Vout {
found := false
for _, pubKeyHash := range PubKeyHashes {
if bytes.Compare(pubKeyHash, out.PubKeyHash) == 0 {
found = true
break
}
}
if !found {
addr, err := utils.PubKeyHashToAddres(out.PubKeyHash)
if err != nil {
return err
}
return errors.New(fmt.Sprintf("Out address %s is not found in the given list", addr))
}
}
return nil
}
func CheckTXOutputValueToAddress(tx *Transaction, PubKeyHash []byte, amount float64) error {
total := float64(0)
for _, out := range tx.Vout {
if bytes.Compare(out.PubKeyHash, PubKeyHash) == 0 {
total = total + out.Value
}
}
if total != amount {
addr, err := utils.PubKeyHashToAddres(PubKeyHash)
if err != nil {
return err
}
return errors.New(fmt.Sprintf("TX output value for %s is %f, not %f as expected", addr, total, amount))
}
return nil
}