/
annotate.go
47 lines (40 loc) · 1.29 KB
/
annotate.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
package asset
import (
"encoding/json"
"github.com/janotchain/janeta/blockchain/query"
chainjson "github.com/janotchain/janeta/encoding/json"
"github.com/janotchain/janeta/protocol/vm/vmutil"
)
func isValidJSON(b []byte) bool {
var v interface{}
err := json.Unmarshal(b, &v)
return err == nil
}
//Annotated annotate the asset
func Annotated(a *Asset) (*query.AnnotatedAsset, error) {
jsonDefinition := json.RawMessage(`{}`)
// a.RawDefinitionByte is the asset definition as it appears on the
// blockchain, so it's untrusted and may not be valid json.
if isValidJSON(a.RawDefinitionByte) {
jsonDefinition = json.RawMessage(a.RawDefinitionByte)
}
annotatedAsset := &query.AnnotatedAsset{
ID: a.AssetID,
Alias: *a.Alias,
VMVersion: a.VMVersion,
RawDefinitionByte: a.RawDefinitionByte,
Definition: &jsonDefinition,
IssuanceProgram: chainjson.HexBytes(a.IssuanceProgram),
}
annotatedAsset.LimitHeight = vmutil.GetIssuanceProgramRestrictHeight(a.IssuanceProgram)
if a.Signer != nil {
annotatedAsset.AnnotatedSigner = query.AnnotatedSigner{
Type: a.Signer.Type,
XPubs: a.Signer.XPubs,
Quorum: a.Signer.Quorum,
KeyIndex: a.Signer.KeyIndex,
DeriveRule: a.Signer.DeriveRule,
}
}
return annotatedAsset, nil
}