OSDN Git Service

list assets response with annotated asset (#1682)
[bytom/bytom.git] / asset / annotate.go
1 package asset
2
3 import (
4         "encoding/json"
5
6         "github.com/bytom/blockchain/query"
7         "github.com/bytom/blockchain/signers"
8         chainjson "github.com/bytom/encoding/json"
9         "github.com/bytom/protocol/vm/vmutil"
10 )
11
12 func isValidJSON(b []byte) bool {
13         var v interface{}
14         err := json.Unmarshal(b, &v)
15         return err == nil
16 }
17
18 //Annotated annotate the asset
19 func Annotated(a *Asset) (*query.AnnotatedAsset, error) {
20         jsonDefinition := json.RawMessage(`{}`)
21
22         // a.RawDefinitionByte is the asset definition as it appears on the
23         // blockchain, so it's untrusted and may not be valid json.
24         if isValidJSON(a.RawDefinitionByte) {
25                 jsonDefinition = json.RawMessage(a.RawDefinitionByte)
26         }
27
28         annotatedAsset := &query.AnnotatedAsset{
29                 ID:                a.AssetID,
30                 Alias:             *a.Alias,
31                 RawDefinitionByte: a.RawDefinitionByte,
32                 Definition:        &jsonDefinition,
33                 IssuanceProgram:   chainjson.HexBytes(a.IssuanceProgram),
34         }
35
36         annotatedAsset.LimitHeight, _ = vmutil.GetIssuanceProgramRestrictHeight(a.IssuanceProgram)
37         if a.Signer != nil {
38                 path := signers.GetBip0032Path(a.Signer, signers.AssetKeySpace)
39                 var jsonPath []chainjson.HexBytes
40                 for _, p := range path {
41                         jsonPath = append(jsonPath, p)
42                 }
43                 for _, xpub := range a.Signer.XPubs {
44                         derived := xpub.Derive(path)
45                         annotatedAsset.Keys = append(annotatedAsset.Keys, &query.AssetKey{
46                                 RootXPub:            xpub,
47                                 AssetPubkey:         derived[:],
48                                 AssetDerivationPath: jsonPath,
49                         })
50                 }
51                 annotatedAsset.Quorum = a.Signer.Quorum
52         }
53         return annotatedAsset, nil
54 }