forked from Onyx-Protocol/Onyx
/
asset.go
327 lines (289 loc) · 8.73 KB
/
asset.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Package asset maintains a registry of all assets on a
// blockchain.
package asset
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"sync"
"github.com/golang/groupcache/lru"
"github.com/lib/pq"
"chain/core/pin"
"chain/core/signers"
"chain/crypto/ed25519"
"chain/crypto/ed25519/chainkd"
"chain/database/pg"
"chain/errors"
"chain/protocol"
"chain/protocol/bc"
"chain/protocol/vm"
"chain/protocol/vmutil"
)
const maxAssetCache = 100
var ErrDuplicateAlias = errors.New("duplicate asset alias")
func NewRegistry(db pg.DB, chain *protocol.Chain) *Registry {
return &Registry{
db: db,
chain: chain,
initialBlockHash: chain.InitialBlockHash,
cache: lru.New(maxAssetCache),
}
}
// Registry tracks and stores all known assets on a blockchain.
type Registry struct {
db pg.DB
chain *protocol.Chain
indexer Saver
initialBlockHash bc.Hash
pinStore *pin.Store
cacheMu sync.Mutex
cache *lru.Cache
}
func (reg *Registry) IndexAssets(indexer Saver, pinStore *pin.Store) {
reg.indexer = indexer
reg.pinStore = pinStore
}
type Asset struct {
AssetID bc.AssetID
Alias *string
Definition map[string]interface{}
IssuanceProgram []byte
InitialBlockHash bc.Hash
Signer *signers.Signer
Tags map[string]interface{}
sortID string
}
// Define defines a new Asset.
func (reg *Registry) Define(ctx context.Context, xpubs []string, quorum int, definition map[string]interface{}, alias string, tags map[string]interface{}, clientToken *string) (*Asset, error) {
assetSigner, err := signers.Create(ctx, reg.db, "asset", xpubs, quorum, clientToken)
if err != nil {
return nil, err
}
serializedDef, err := serializeAssetDef(definition)
if err != nil {
return nil, errors.Wrap(err, "serializing asset definition")
}
path := signers.Path(assetSigner, signers.AssetKeySpace)
derivedXPubs := chainkd.DeriveXPubs(assetSigner.XPubs, path)
derivedPKs := chainkd.XPubKeys(derivedXPubs)
issuanceProgram, err := programWithDefinition(derivedPKs, assetSigner.Quorum, serializedDef)
if err != nil {
return nil, err
}
asset := &Asset{
Definition: definition,
IssuanceProgram: issuanceProgram,
InitialBlockHash: reg.initialBlockHash,
AssetID: bc.ComputeAssetID(issuanceProgram, reg.initialBlockHash, 1),
Signer: assetSigner,
Tags: tags,
}
if alias != "" {
asset.Alias = &alias
}
asset, err = reg.insertAsset(ctx, asset, clientToken)
if err != nil {
return nil, errors.Wrap(err, "inserting asset")
}
err = insertAssetTags(ctx, reg.db, asset.AssetID, tags)
if err != nil {
return nil, errors.Wrap(err, "inserting asset tags")
}
err = reg.indexAnnotatedAsset(ctx, asset)
if err != nil {
return nil, errors.Wrap(err, "indexing annotated asset")
}
return asset, nil
}
// findByID retrieves an Asset record along with its signer, given an assetID.
func (reg *Registry) findByID(ctx context.Context, id bc.AssetID) (*Asset, error) {
reg.cacheMu.Lock()
cached, ok := reg.cache.Get(id)
reg.cacheMu.Unlock()
if ok {
return cached.(*Asset), nil
}
asset, err := assetQuery(ctx, reg.db, "assets.id=$1", id)
if err != nil {
return nil, err
}
reg.cacheMu.Lock()
reg.cache.Add(id, asset)
reg.cacheMu.Unlock()
return asset, nil
}
// FindByAlias retrieves an Asset record along with its signer,
// given an asset alias.
func (reg *Registry) FindByAlias(ctx context.Context, alias string) (*Asset, error) {
return assetQuery(ctx, reg.db, "assets.alias=$1", alias)
}
// insertAsset adds the asset to the database. If the asset has a client token,
// and there already exists an asset with that client token, insertAsset will
// lookup and return the existing asset instead.
func (reg *Registry) insertAsset(ctx context.Context, asset *Asset, clientToken *string) (*Asset, error) {
const q = `
INSERT INTO assets
(id, alias, signer_id, initial_block_hash, issuance_program, definition, client_token)
VALUES($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (client_token) DO NOTHING
RETURNING sort_id
`
defParams, err := mapToNullString(asset.Definition)
if err != nil {
return nil, err
}
var signerID sql.NullString
if asset.Signer != nil {
signerID = sql.NullString{Valid: true, String: asset.Signer.ID}
}
err = reg.db.QueryRow(
ctx, q,
asset.AssetID, asset.Alias, signerID,
asset.InitialBlockHash, asset.IssuanceProgram,
defParams, clientToken,
).Scan(&asset.sortID)
if pg.IsUniqueViolation(err) {
return nil, errors.WithDetail(ErrDuplicateAlias, "an asset with the provided alias already exists")
} else if err == sql.ErrNoRows && clientToken != nil {
// There is already an asset with the provided client
// token. We should return the existing asset.
asset, err = assetByClientToken(ctx, reg.db, *clientToken)
if err != nil {
return nil, errors.Wrap(err, "retrieving existing asset")
}
} else if err != nil {
return nil, err
}
return asset, nil
}
// insertAssetTags inserts a set of tags for the given assetID.
// It must take place inside a database transaction.
func insertAssetTags(ctx context.Context, db pg.DB, assetID bc.AssetID, tags map[string]interface{}) error {
tagsParam, err := mapToNullString(tags)
if err != nil {
return errors.Wrap(err)
}
const q = `
INSERT INTO asset_tags (asset_id, tags) VALUES ($1, $2)
ON CONFLICT (asset_id) DO UPDATE SET tags = $2
`
_, err = db.Exec(ctx, q, assetID.String(), tagsParam)
if err != nil {
return errors.Wrap(err)
}
return nil
}
// assetByClientToken loads an asset from the database using its client token.
func assetByClientToken(ctx context.Context, db pg.DB, clientToken string) (*Asset, error) {
return assetQuery(ctx, db, "assets.client_token=$1", clientToken)
}
func assetQuery(ctx context.Context, db pg.DB, pred string, args ...interface{}) (*Asset, error) {
const baseQ = `
SELECT assets.id, assets.alias, assets.issuance_program, assets.definition,
assets.initial_block_hash, assets.sort_id,
signers.id, COALESCE(signers.type, ''), COALESCE(signers.xpubs, '{}'),
COALESCE(signers.quorum, 0), COALESCE(signers.key_index, 0),
asset_tags.tags
FROM assets
LEFT JOIN signers ON signers.id=assets.signer_id
LEFT JOIN asset_tags ON asset_tags.asset_id=assets.id
WHERE %s
LIMIT 1
`
var (
a Asset
alias sql.NullString
definition []byte
signerID sql.NullString
signerType string
quorum int
keyIndex uint64
xpubs []string
tags []byte
)
err := db.QueryRow(ctx, fmt.Sprintf(baseQ, pred), args...).Scan(
&a.AssetID,
&a.Alias,
&a.IssuanceProgram,
&definition,
&a.InitialBlockHash,
&a.sortID,
&signerID,
&signerType,
(*pq.StringArray)(&xpubs),
&quorum,
&keyIndex,
&tags,
)
if err == sql.ErrNoRows {
return nil, pg.ErrUserInputNotFound
} else if err != nil {
return nil, err
}
if signerID.Valid {
a.Signer, err = signers.New(signerID.String, signerType, xpubs, quorum, keyIndex)
if err != nil {
return nil, err
}
}
if len(definition) > 0 {
err := json.Unmarshal(definition, &a.Definition)
if err != nil {
return nil, errors.Wrap(err)
}
}
if alias.Valid {
a.Alias = &alias.String
}
if len(tags) > 0 {
err := json.Unmarshal(tags, &a.Tags)
if err != nil {
return nil, errors.Wrap(err)
}
}
return &a, nil
}
// serializeAssetDef produces a canonical byte representation of an asset
// definition. Currently, this is implemented using pretty-printed JSON.
// As is the standard for Go's map[string] serialization, object keys will
// appear in lexicographic order. Although this is mostly meant for machine
// consumption, the JSON is pretty-printed for easy reading.
// The empty asset def is an empty byte slice.
func serializeAssetDef(def map[string]interface{}) ([]byte, error) {
return json.MarshalIndent(def, "", " ")
}
func programWithDefinition(pubkeys []ed25519.PublicKey, nrequired int, definition []byte) ([]byte, error) {
issuanceProg, err := vmutil.P2SPMultiSigProgram(pubkeys, nrequired)
if err != nil {
return nil, err
}
builder := vmutil.NewBuilder()
builder.AddData(definition).AddOp(vm.OP_DROP)
builder.AddRawBytes(issuanceProg)
return builder.Program, nil
}
func definitionFromProgram(program []byte) ([]byte, error) {
pops, err := vm.ParseProgram(program)
if err != nil {
return nil, err
}
if len(pops) < 2 {
return nil, errors.New("bad issuance program")
}
if pops[1].Op != vm.OP_DROP {
return nil, errors.New("bad issuance program")
}
return pops[0].Data, nil
}
func mapToNullString(in map[string]interface{}) (*sql.NullString, error) {
var mapJSON []byte
if len(in) != 0 {
var err error
mapJSON, err = json.Marshal(in)
if err != nil {
return nil, errors.Wrap(err)
}
}
return &sql.NullString{String: string(mapJSON), Valid: len(mapJSON) > 0}, nil
}