-
Notifications
You must be signed in to change notification settings - Fork 9
/
rpcWitness.go
65 lines (60 loc) · 2.21 KB
/
rpcWitness.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
package models
import (
"github.com/joeqian10/neo3-gogogo/crypto"
"github.com/joeqian10/neo3-gogogo/helper"
"github.com/joeqian10/neo3-gogogo/tx/conditions"
)
type RpcWitness struct {
Invocation string `json:"invocation"`
Verification string `json:"verification"`
}
type RpcWitnessRule struct {
Action string `json:"action"`
Condition RpcWitnessCondition `json:"condition"`
}
// RpcWitnessCondition combines all types of conditions into one struct
type RpcWitnessCondition struct {
Type string `json:"type"` // type of this condition
Expression interface{} `json:"expression,omitempty"` // BooleanCondition: "true" || "false" | NotCondition: !RpcWitnessCondition
Expressions []RpcWitnessCondition `json:"expressions,omitempty"` // AndCondition: RpcWitnessCondition && RpcWitnessCondition | OrCondition: RpcWitnessCondition || RpcWitnessCondition
Hash string `json:"hash,omitempty"` // ScriptHashCondition | CalledByContractCondition: UInt160.ToString()
Group string `json:"group,omitempty"` // GroupCondition | CalledByGroupCondition: ECPoint.ToString()
}
func CreateRpcWitnessCondition(c *conditions.WitnessCondition) RpcWitnessCondition {
r := RpcWitnessCondition{Type: c.Type.String()}
switch c.Type {
case conditions.Boolean:
b := c.GetCondition().(*bool)
if *b {
r.Expression = "true"
} else {
r.Expression = "false"
}
break
case conditions.Not:
inner := CreateRpcWitnessCondition(c.GetCondition().(*conditions.WitnessCondition))
r.Expression = inner // get the value from the pointer
break
case conditions.And, conditions.Or:
cs := c.GetCondition().([]*conditions.WitnessCondition)
inners := make([]RpcWitnessCondition, len(cs))
for i, _ := range cs {
inners[i] = CreateRpcWitnessCondition(cs[i])
}
r.Expressions = inners
break
case conditions.ScriptHash, conditions.CalledByContract:
hash := c.GetCondition().(*helper.UInt160)
r.Hash = hash.String()
break
case conditions.Group, conditions.CalledByGroup:
group := c.GetCondition().(*crypto.ECPoint)
r.Group = group.String()
break
case conditions.CalledByEntryType:
break
default:
break
}
return r
}