-
Notifications
You must be signed in to change notification settings - Fork 3
/
annotate.go
64 lines (58 loc) · 1.59 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package asset
import (
"encoding/json"
"github.com/bytom/blockchain/query"
"github.com/bytom/blockchain/signers"
chainjson "github.com/bytom/encoding/json"
"github.com/bytom/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)
}
aa := &query.AnnotatedAsset{
ID: a.AssetID,
Definition: &jsonDefinition,
IssuanceProgram: chainjson.HexBytes(a.IssuanceProgram),
}
if a.Alias != nil {
aa.Alias = *a.Alias
}
if a.Signer != nil {
path := signers.Path(a.Signer, signers.AssetKeySpace)
var jsonPath []chainjson.HexBytes
for _, p := range path {
jsonPath = append(jsonPath, p)
}
for _, xpub := range a.Signer.XPubs {
derived := xpub.Derive(path)
aa.Keys = append(aa.Keys, &query.AssetKey{
RootXPub: xpub,
AssetPubkey: derived[:],
AssetDerivationPath: jsonPath,
})
}
aa.Quorum = a.Signer.Quorum
} else {
pubkeys, quorum, err := vmutil.ParseP2SPMultiSigProgram(a.IssuanceProgram)
if err == nil {
for _, pubkey := range pubkeys {
pubkey := pubkey
aa.Keys = append(aa.Keys, &query.AssetKey{
AssetPubkey: chainjson.HexBytes(pubkey[:]),
})
}
aa.Quorum = quorum
}
}
return aa, nil
}