-
Notifications
You must be signed in to change notification settings - Fork 115
/
assetregistry.go
304 lines (246 loc) · 7.55 KB
/
assetregistry.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package wallet
import (
"context"
"strconv"
"github.com/capossele/asset-registry/pkg/registryclient"
"github.com/capossele/asset-registry/pkg/registryservice"
"github.com/cockroachdb/errors"
"github.com/go-resty/resty/v2"
"github.com/iotaledger/hive.go/marshalutil"
"github.com/iotaledger/hive.go/typeutils"
"github.com/iotaledger/goshimmer/packages/ledgerstate"
)
const (
// RegistryHostURL is the host url of the central registry.
RegistryHostURL = "http://asset-registry.tokenizedassetsdemo.iota.cafe"
)
// AssetRegistry represents a registry for colored coins, that stores the relevant metadata in a dictionary.
type AssetRegistry struct {
assets map[ledgerstate.Color]Asset
// client communicates with the central registry
client *registryclient.HTTPClient
network string
}
// NewAssetRegistry is the constructor for the AssetRegistry.
func NewAssetRegistry(network string, registryURL ...string) *AssetRegistry {
hostURL := RegistryHostURL
if len(registryURL) > 0 {
hostURL = registryURL[0]
}
client := registryclient.NewHTTPClient(resty.New().SetHostURL(hostURL))
return &AssetRegistry{
make(map[ledgerstate.Color]Asset),
client,
network,
}
}
// ParseAssetRegistry is a utility function that can be used to parse a marshaled version of the registry.
func ParseAssetRegistry(marshalUtil *marshalutil.MarshalUtil) (assetRegistry *AssetRegistry, consumedBytes int, err error) {
startingOffset := marshalUtil.ReadOffset()
networkLength, parseErr := marshalUtil.ReadUint32()
if parseErr != nil {
err = parseErr
return
}
networkBytes, parseErr := marshalUtil.ReadBytes(int(networkLength))
if parseErr != nil {
err = parseErr
return
}
network := typeutils.BytesToString(networkBytes)
if !registryservice.Networks[network] {
err = errors.Errorf("unsupported asset registry network: %s", network)
return
}
assetRegistry = NewAssetRegistry(network)
assetCount, err := marshalUtil.ReadUint64()
if err != nil {
return
}
for i := uint64(0); i < assetCount; i++ {
asset := Asset{}
colorBytes, parseErr := marshalUtil.ReadBytes(ledgerstate.ColorLength)
if parseErr != nil {
err = parseErr
return
}
color, _, parseErr := ledgerstate.ColorFromBytes(colorBytes)
if parseErr != nil {
err = parseErr
return
}
nameLength, parseErr := marshalUtil.ReadUint32()
if parseErr != nil {
err = parseErr
return
}
nameBytes, parseErr := marshalUtil.ReadBytes(int(nameLength))
if parseErr != nil {
err = parseErr
return
}
symbolLength, parseErr := marshalUtil.ReadUint32()
if parseErr != nil {
err = parseErr
return
}
symbolBytes, parseErr := marshalUtil.ReadBytes(int(symbolLength))
if parseErr != nil {
err = parseErr
return
}
precision, parseErr := marshalUtil.ReadUint32()
if parseErr != nil {
err = parseErr
return
}
supply, parseErr := marshalUtil.ReadUint64()
if parseErr != nil {
err = parseErr
return
}
txID, parseErr := ledgerstate.TransactionIDFromMarshalUtil(marshalUtil)
if parseErr != nil {
err = parseErr
return
}
asset.Color = color
asset.Name = typeutils.BytesToString(nameBytes)
asset.Symbol = typeutils.BytesToString(symbolBytes)
asset.Precision = int(precision)
asset.Supply = supply
asset.TransactionID = txID
assetRegistry.assets[color] = asset
}
consumedBytes = marshalUtil.ReadOffset() - startingOffset
return
}
// SetRegistryURL sets the url of the registry api server.
func (a *AssetRegistry) SetRegistryURL(url string) {
a.client = registryclient.NewHTTPClient(resty.New().SetHostURL(url))
}
// Network returns the current network the asset registry connects to.
func (a *AssetRegistry) Network() string {
return a.network
}
// LoadAsset returns an asset either from local or from central registry.
func (a *AssetRegistry) LoadAsset(id ledgerstate.Color) (*Asset, error) {
_, ok := a.assets[id]
if !ok {
success := a.updateLocalFromCentral(id)
if !success {
return nil, errors.Errorf("no asset found with assetID (color) %s", id.Base58())
}
}
asset := a.assets[id]
return &asset, nil
}
// RegisterAsset registers an asset in the registry, so we can look up names and symbol of colored coins.
func (a *AssetRegistry) RegisterAsset(color ledgerstate.Color, asset Asset) error {
a.assets[color] = asset
return a.client.SaveAsset(context.TODO(), a.network, asset.ToRegistry())
}
// Name returns the name of the given asset.
func (a *AssetRegistry) Name(color ledgerstate.Color) string {
// check in local registry
if asset, assetExists := a.assets[color]; assetExists {
return asset.Name
}
if color == ledgerstate.ColorIOTA {
return "IOTA"
}
// not in local
// fetch from central, update local
if a.updateLocalFromCentral(color) {
return a.assets[color].Name
}
// fallback if we fetch was not successful, just use the color as name
return color.String()
}
// Symbol return the symbol of the token.
func (a *AssetRegistry) Symbol(color ledgerstate.Color) string {
if asset, assetExists := a.assets[color]; assetExists {
return asset.Symbol
}
if color == ledgerstate.ColorIOTA {
return "I"
}
// not in local
// fetch from central, update local
if a.updateLocalFromCentral(color) {
return a.assets[color].Symbol
}
return "cI"
}
// Supply returns the initial supply of the token.
func (a *AssetRegistry) Supply(color ledgerstate.Color) string {
if asset, assetExists := a.assets[color]; assetExists {
return strconv.FormatUint(asset.Supply, 10)
}
if color == ledgerstate.ColorIOTA {
return ""
}
// not in local
// fetch from central, update local
if a.updateLocalFromCentral(color) {
return strconv.FormatUint(a.assets[color].Supply, 10)
}
return "unknown"
}
// TransactionID returns the ID of the transaction that created the token.
func (a *AssetRegistry) TransactionID(color ledgerstate.Color) string {
if asset, assetExists := a.assets[color]; assetExists {
return asset.TransactionID.Base58()
}
if color == ledgerstate.ColorIOTA {
return ""
}
// not in local
// fetch from central, update local
if a.updateLocalFromCentral(color) {
return a.assets[color].TransactionID.Base58()
}
return "unknown"
}
// Precision returns the amount of decimal places that this token uses.
func (a *AssetRegistry) Precision(color ledgerstate.Color) int {
if asset, assetExists := a.assets[color]; assetExists {
return asset.Precision
}
return 0
}
// Bytes marshal the registry into a sequence of bytes.
func (a *AssetRegistry) Bytes() []byte {
marshalUtil := marshalutil.New()
networkBytes := typeutils.StringToBytes(a.network)
marshalUtil.WriteUint32(uint32(len(networkBytes)))
marshalUtil.WriteBytes(networkBytes)
assetCount := len(a.assets)
marshalUtil.WriteUint64(uint64(assetCount))
for color, asset := range a.assets {
marshalUtil.WriteBytes(color.Bytes())
nameBytes := typeutils.StringToBytes(asset.Name)
marshalUtil.WriteUint32(uint32(len(nameBytes)))
marshalUtil.WriteBytes(nameBytes)
symbolBytes := typeutils.StringToBytes(asset.Symbol)
marshalUtil.WriteUint32(uint32(len(symbolBytes)))
marshalUtil.WriteBytes(symbolBytes)
marshalUtil.WriteUint32(uint32(asset.Precision))
marshalUtil.WriteUint64(asset.Supply)
marshalUtil.WriteBytes(asset.TransactionID.Bytes())
}
return marshalUtil.Bytes()
}
func (a *AssetRegistry) updateLocalFromCentral(color ledgerstate.Color) (success bool) {
loadedAsset, err := a.client.LoadAsset(context.TODO(), a.network, color.Base58())
if err == nil {
// save it locally
var walletAsset *Asset
walletAsset, err = AssetFromRegistryEntry(loadedAsset)
if err == nil {
a.assets[walletAsset.Color] = *walletAsset
return true
}
}
return false
}