OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.git] / asset / annotate.go
1 package asset
2
3 import (
4         "encoding/json"
5
6         "github.com/vapor/blockchain/query"
7         "github.com/vapor/blockchain/signers"
8         chainjson "github.com/vapor/encoding/json"
9         "github.com/vapor/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         aa := &query.AnnotatedAsset{
29                 ID:              a.AssetID,
30                 Definition:      &jsonDefinition,
31                 IssuanceProgram: chainjson.HexBytes(a.IssuanceProgram),
32         }
33         if a.Alias != nil {
34                 aa.Alias = *a.Alias
35         }
36         if a.Signer != nil {
37                 path := signers.GetBip0032Path(a.Signer, signers.AssetKeySpace)
38                 var jsonPath []chainjson.HexBytes
39                 for _, p := range path {
40                         jsonPath = append(jsonPath, p)
41                 }
42                 for _, xpub := range a.Signer.XPubs {
43                         derived := xpub.Derive(path)
44                         aa.Keys = append(aa.Keys, &query.AssetKey{
45                                 RootXPub:            xpub,
46                                 AssetPubkey:         derived[:],
47                                 AssetDerivationPath: jsonPath,
48                         })
49                 }
50                 aa.Quorum = a.Signer.Quorum
51         } else {
52                 pubkeys, quorum, err := vmutil.ParseP2SPMultiSigProgram(a.IssuanceProgram)
53                 if err == nil {
54                         for _, pubkey := range pubkeys {
55                                 pubkey := pubkey
56                                 aa.Keys = append(aa.Keys, &query.AssetKey{
57                                         AssetPubkey: chainjson.HexBytes(pubkey[:]),
58                                 })
59                         }
60                         aa.Quorum = quorum
61                 }
62         }
63         return aa, nil
64 }