-
Notifications
You must be signed in to change notification settings - Fork 79
/
witness_scope.go
108 lines (100 loc) · 3.32 KB
/
witness_scope.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package transaction
//go:generate stringer -type=WitnessScope -linecomment -output=witness_scope_string.go
import (
"encoding/json"
"fmt"
"strings"
)
// WitnessScope represents set of witness flags for Transaction signer.
type WitnessScope byte
const (
// None specifies that no contract was witnessed. Only sign the transaction.
None WitnessScope = 0
// CalledByEntry witness is only valid in entry script and ones directly called by it.
// No params is needed, as the witness/permission/signature given on first invocation will
// automatically expire if entering deeper internal invokes. This can be default safe
// choice for native NEO/GAS (previously used on Neo 2 as "attach" mode).
CalledByEntry WitnessScope = 0x01
// CustomContracts define custom hash for contract-specific.
CustomContracts WitnessScope = 0x10
// CustomGroups define custom pubkey for group members.
CustomGroups WitnessScope = 0x20
// Rules is a set of conditions with boolean operators.
Rules WitnessScope = 0x40 // WitnessRules
// Global allows this witness in all contexts (default Neo2 behavior).
// This cannot be combined with other flags.
Global WitnessScope = 0x80
)
// ScopesFromString converts string of comma-separated scopes to a set of scopes
// (case-sensitive). String can combine several scopes, e.g. be any of: 'Global',
// 'CalledByEntry,CustomGroups' etc. In case of an empty string an error will be
// returned.
func ScopesFromString(s string) (WitnessScope, error) {
var result WitnessScope
scopes := strings.Split(s, ",")
for i, scope := range scopes {
scopes[i] = strings.TrimSpace(scope)
}
dict := map[string]WitnessScope{
Global.String(): Global,
CalledByEntry.String(): CalledByEntry,
CustomContracts.String(): CustomContracts,
CustomGroups.String(): CustomGroups,
Rules.String(): Rules,
None.String(): None,
}
var isGlobal bool
for _, scopeStr := range scopes {
scope, ok := dict[scopeStr]
if !ok {
return result, fmt.Errorf("invalid witness scope: %v", scopeStr)
}
if isGlobal && !(scope == Global) {
return result, fmt.Errorf("Global scope can not be combined with other scopes")
}
result |= scope
if scope == Global {
isGlobal = true
}
}
return result, nil
}
func appendScopeString(str string, scopes WitnessScope, scope WitnessScope) string {
if scopes&scope != 0 {
if len(str) != 0 {
str += ", "
}
str += scope.String()
}
return str
}
// scopesToString converts witness scope to it's string representation. It uses
// `, ` to separate scope names.
func scopesToString(scopes WitnessScope) string {
if scopes&Global != 0 || scopes == None {
return scopes.String()
}
var res string
res = appendScopeString(res, scopes, CalledByEntry)
res = appendScopeString(res, scopes, CustomContracts)
res = appendScopeString(res, scopes, CustomGroups)
res = appendScopeString(res, scopes, Rules)
return res
}
// MarshalJSON implements the json.Marshaler interface.
func (s WitnessScope) MarshalJSON() ([]byte, error) {
return []byte(`"` + scopesToString(s) + `"`), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (s *WitnessScope) UnmarshalJSON(data []byte) error {
var js string
if err := json.Unmarshal(data, &js); err != nil {
return err
}
scopes, err := ScopesFromString(js)
if err != nil {
return err
}
*s = scopes
return nil
}