/
validate.go
61 lines (55 loc) · 1.58 KB
/
validate.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
58
59
60
61
package threebot
import (
"fmt"
"github.com/threefoldtech/rivine/types"
tbtypes "github.com/threefoldfoundation/tfchain/extensions/threebot/types"
)
func validateUniquenessOfNetworkAddresses(addresses []tbtypes.NetworkAddress) error {
dm := make(map[string]struct{}, len(addresses))
var (
str string
exists bool
)
for _, addr := range addresses {
str = addr.String()
if _, exists = dm[str]; exists {
return fmt.Errorf("address %s is not unique within the given slice", str)
}
dm[str] = struct{}{}
}
return nil
}
func validateUniquenessOfBotNames(names []tbtypes.BotName) error {
dm := make(map[string]struct{}, len(names))
var (
str string
exists bool
)
for _, name := range names {
str = name.String()
if _, exists = dm[str]; exists {
return fmt.Errorf("name %s is not unique within the given slice", str)
}
dm[str] = struct{}{}
}
return nil
}
func validateBotSignature(t types.Transaction, publicKey types.PublicKey, signature types.ByteSlice, ctx types.TransactionValidationContext, extraObjects ...interface{}) error {
uh, err := types.NewPubKeyUnlockHash(publicKey)
if err != nil {
return err
}
condition := types.NewCondition(types.NewUnlockHashCondition(uh))
// and a matching single-signature fulfillment
fulfillment := types.NewFulfillment(&types.SingleSignatureFulfillment{
PublicKey: publicKey,
Signature: signature,
})
// validate the signature is correct
return condition.Fulfill(fulfillment, types.FulfillContext{
ExtraObjects: extraObjects,
BlockHeight: ctx.BlockHeight,
BlockTime: ctx.BlockTime,
Transaction: t,
})
}