-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtwin.go
184 lines (151 loc) · 4.07 KB
/
twin.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package substrate
import (
"fmt"
"github.com/centrifuge/go-substrate-rpc-client/v4/scale"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/pkg/errors"
)
// EntityProof struct
type EntityProof struct {
EntityID types.U32 `json:"entity_id"`
Signature string `json:"signature"`
}
// Twin struct
type Twin struct {
ID types.U32 `json:"id"`
Account AccountID `json:"acocunt_id"`
Relay OptionRelay `json:"relay"`
Entities []EntityProof `json:"entities"`
Pk types.OptionBytes `json:"pk"`
}
// OptionRelay type
type OptionRelay struct {
HasValue bool `json:"has_value"`
AsValue string `json:"as_value"`
}
// Encode implementation
func (m OptionRelay) Encode(encoder scale.Encoder) (err error) {
var i byte
if m.HasValue {
i = 1
}
err = encoder.PushByte(i)
if err != nil {
return err
}
if m.HasValue {
err = encoder.Encode(m.AsValue)
}
return
}
// Decode implementation for the enum type
func (r *OptionRelay) Decode(decoder scale.Decoder) error {
b, err := decoder.ReadOneByte()
if err != nil {
return err
}
switch b {
case 0:
r.HasValue = false
r.AsValue = ""
case 1:
r.HasValue = true
return decoder.Decode(&r.AsValue)
default:
return fmt.Errorf("invalid relay value")
}
return nil
}
// GetTwinByPubKey gets a twin with public key
func (s *Substrate) GetTwinByPubKey(pk []byte) (uint32, error) {
cl, meta, err := s.GetClient()
if err != nil {
return 0, err
}
key, err := types.CreateStorageKey(meta, "TfgridModule", "TwinIdByAccountID", pk, nil)
if err != nil {
return 0, errors.Wrap(err, "failed to create substrate query key")
}
var id types.U32
ok, err := cl.RPC.State.GetStorageLatest(key, &id)
if err != nil {
return 0, errors.Wrap(err, "failed to lookup entity")
}
if !ok || id == 0 {
return 0, errors.Wrap(ErrNotFound, "twin not found")
}
return uint32(id), nil
}
// GetTwin gets a twin
func (s *Substrate) GetTwin(id uint32) (*Twin, error) {
cl, meta, err := s.GetClient()
if err != nil {
return nil, err
}
bytes, err := Encode(id)
if err != nil {
return nil, errors.Wrap(err, "substrate: encoding error building query arguments")
}
key, err := types.CreateStorageKey(meta, "TfgridModule", "Twins", bytes, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create substrate query key")
}
raw, err := cl.RPC.State.GetStorageRawLatest(key)
if err != nil {
return nil, errors.Wrap(err, "failed to lookup entity")
}
if len(*raw) == 0 {
return nil, errors.Wrap(ErrNotFound, "twin not found")
}
var twin Twin
if err := Decode(*raw, &twin); err != nil {
return nil, errors.Wrap(err, "failed to load object")
}
return &twin, nil
}
// CreateTwin creates a twin
func (s *Substrate) CreateTwin(identity Identity, relay string, pk []byte) (uint32, error) {
cl, meta, err := s.GetClient()
if err != nil {
return 0, err
}
relayOption := OptionRelay{}
if relay != "" {
relayOption = OptionRelay{HasValue: true, AsValue: relay}
}
pkOption := types.NewOptionBytesEmpty()
if pk != nil {
pkOption = types.NewOptionBytes(pk)
}
c, err := types.NewCall(meta, "TfgridModule.create_twin", relayOption, pkOption)
if err != nil {
return 0, errors.Wrap(err, "failed to create call")
}
if _, err := s.Call(cl, meta, identity, c); err != nil {
return 0, errors.Wrap(err, "failed to create twin")
}
return s.GetTwinByPubKey(identity.PublicKey())
}
// UpdateTwin updates a twin
func (s *Substrate) UpdateTwin(identity Identity, relay string, pk []byte) (uint32, error) {
cl, meta, err := s.GetClient()
if err != nil {
return 0, err
}
relayOption := OptionRelay{}
if relay != "" {
relayOption = OptionRelay{HasValue: true, AsValue: relay}
}
pk_bytes := types.OptionBytes{}
if pk != nil {
pk_bytes = types.NewOptionBytes(pk)
}
c, err := types.NewCall(meta, "TfgridModule.update_twin", relayOption, pk_bytes)
if err != nil {
return 0, errors.Wrap(err, "failed to create call")
}
if _, err := s.Call(cl, meta, identity, c); err != nil {
return 0, errors.Wrap(err, "failed to update twin")
}
return s.GetTwinByPubKey(identity.PublicKey())
}